blob: da2015f319a355a6ff5c883d3642e5e06908e78f [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 != ' ')
2628 xp->xp_context = EXPAND_SETTINGS;
2629 }
2630 else if (c == '$')
2631 {
2632 /* environment variable */
2633 xp->xp_context = EXPAND_ENV_VARS;
2634 }
2635 else if (c == '=')
2636 {
2637 got_eq = TRUE;
2638 xp->xp_context = EXPAND_EXPRESSION;
2639 }
2640 else if (c == '<'
2641 && xp->xp_context == EXPAND_FUNCTIONS
2642 && vim_strchr(xp->xp_pattern, '(') == NULL)
2643 {
2644 /* Function name can start with "<SNR>" */
2645 break;
2646 }
2647 else if (cmdidx != CMD_let || got_eq)
2648 {
2649 if (c == '"') /* string */
2650 {
2651 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2652 if (c == '\\' && xp->xp_pattern[1] != NUL)
2653 ++xp->xp_pattern;
2654 xp->xp_context = EXPAND_NOTHING;
2655 }
2656 else if (c == '\'') /* literal string */
2657 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2660 /* skip */ ;
2661 xp->xp_context = EXPAND_NOTHING;
2662 }
2663 else if (c == '|')
2664 {
2665 if (xp->xp_pattern[1] == '|')
2666 {
2667 ++xp->xp_pattern;
2668 xp->xp_context = EXPAND_EXPRESSION;
2669 }
2670 else
2671 xp->xp_context = EXPAND_COMMANDS;
2672 }
2673 else
2674 xp->xp_context = EXPAND_EXPRESSION;
2675 }
2676 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002677 /* Doesn't look like something valid, expand as an expression
2678 * anyway. */
2679 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 arg = xp->xp_pattern;
2681 if (*arg != NUL)
2682 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2683 /* skip */ ;
2684 }
2685 xp->xp_pattern = arg;
2686}
2687
2688#endif /* FEAT_CMDL_COMPL */
2689
2690/*
2691 * ":1,25call func(arg1, arg2)" function call.
2692 */
2693 void
2694ex_call(eap)
2695 exarg_T *eap;
2696{
2697 char_u *arg = eap->arg;
2698 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002700 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002702 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 linenr_T lnum;
2704 int doesrange;
2705 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002706 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002708 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2709 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002710 if (tofree == NULL)
2711 return;
2712
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002713 /* Increase refcount on dictionary, it could get deleted when evaluating
2714 * the arguments. */
2715 if (fudi.fd_dict != NULL)
2716 ++fudi.fd_dict->dv_refcount;
2717
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002718 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2719 len = STRLEN(tofree);
2720 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721
Bram Moolenaar532c7802005-01-27 14:44:31 +00002722 /* Skip white space to allow ":call func ()". Not good, but required for
2723 * backward compatibility. */
2724 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002725 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726
2727 if (*startarg != '(')
2728 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00002729 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 goto end;
2731 }
2732
2733 /*
2734 * When skipping, evaluate the function once, to find the end of the
2735 * arguments.
2736 * When the function takes a range, this is discovered after the first
2737 * call, and the loop is broken.
2738 */
2739 if (eap->skip)
2740 {
2741 ++emsg_skip;
2742 lnum = eap->line2; /* do it once, also with an invalid range */
2743 }
2744 else
2745 lnum = eap->line1;
2746 for ( ; lnum <= eap->line2; ++lnum)
2747 {
2748 if (!eap->skip && eap->addr_count > 0)
2749 {
2750 curwin->w_cursor.lnum = lnum;
2751 curwin->w_cursor.col = 0;
2752 }
2753 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002754 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002755 eap->line1, eap->line2, &doesrange,
2756 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 {
2758 failed = TRUE;
2759 break;
2760 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002761 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 if (doesrange || eap->skip)
2763 break;
2764 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002765 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002766 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002767 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 if (aborting())
2769 break;
2770 }
2771 if (eap->skip)
2772 --emsg_skip;
2773
2774 if (!failed)
2775 {
2776 /* Check for trailing illegal characters and a following command. */
2777 if (!ends_excmd(*arg))
2778 {
2779 emsg_severe = TRUE;
2780 EMSG(_(e_trailing));
2781 }
2782 else
2783 eap->nextcmd = check_nextcmd(arg);
2784 }
2785
2786end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002787 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002788 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789}
2790
2791/*
2792 * ":unlet[!] var1 ... " command.
2793 */
2794 void
2795ex_unlet(eap)
2796 exarg_T *eap;
2797{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002798 ex_unletlock(eap, eap->arg, 0);
2799}
2800
2801/*
2802 * ":lockvar" and ":unlockvar" commands
2803 */
2804 void
2805ex_lockvar(eap)
2806 exarg_T *eap;
2807{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002809 int deep = 2;
2810
2811 if (eap->forceit)
2812 deep = -1;
2813 else if (vim_isdigit(*arg))
2814 {
2815 deep = getdigits(&arg);
2816 arg = skipwhite(arg);
2817 }
2818
2819 ex_unletlock(eap, arg, deep);
2820}
2821
2822/*
2823 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2824 */
2825 static void
2826ex_unletlock(eap, argstart, deep)
2827 exarg_T *eap;
2828 char_u *argstart;
2829 int deep;
2830{
2831 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002834 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835
2836 do
2837 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 /* Parse the name and find the end. */
2839 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE);
2840 if (lv.ll_name == NULL)
2841 error = TRUE; /* error but continue parsing */
2842 if (name_end == NULL || (!vim_iswhite(*name_end)
2843 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 if (name_end != NULL)
2846 {
2847 emsg_severe = TRUE;
2848 EMSG(_(e_trailing));
2849 }
2850 if (!(eap->skip || error))
2851 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 break;
2853 }
2854
2855 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002856 {
2857 if (eap->cmdidx == CMD_unlet)
2858 {
2859 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2860 error = TRUE;
2861 }
2862 else
2863 {
2864 if (do_lock_var(&lv, name_end, deep,
2865 eap->cmdidx == CMD_lockvar) == FAIL)
2866 error = TRUE;
2867 }
2868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 if (!eap->skip)
2871 clear_lval(&lv);
2872
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 arg = skipwhite(name_end);
2874 } while (!ends_excmd(*arg));
2875
2876 eap->nextcmd = check_nextcmd(arg);
2877}
2878
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 int forceit;
2884{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 int ret = OK;
2886 int cc;
2887
2888 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 cc = *name_end;
2891 *name_end = NUL;
2892
2893 /* Normal name or expanded name. */
2894 if (check_changedtick(lp->ll_name))
2895 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002896 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002899 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002900 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
2901 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 else if (lp->ll_range)
2903 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002904 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905
2906 /* Delete a range of List items. */
2907 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2908 {
2909 li = lp->ll_li->li_next;
2910 listitem_remove(lp->ll_list, lp->ll_li);
2911 lp->ll_li = li;
2912 ++lp->ll_n1;
2913 }
2914 }
2915 else
2916 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 /* unlet a List item. */
2919 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002922 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 }
2924
2925 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002926}
2927
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928/*
2929 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002930 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 */
2932 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002933do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002935 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936{
Bram Moolenaar33570922005-01-25 22:26:29 +00002937 hashtab_T *ht;
2938 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002939 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940
Bram Moolenaar33570922005-01-25 22:26:29 +00002941 ht = find_var_ht(name, &varname);
2942 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002944 hi = hash_find(ht, varname);
2945 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002946 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002947 if (var_check_ro(HI2DI(hi)->di_flags, name))
2948 return FAIL;
2949 delete_var(ht, hi);
2950 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002953 if (forceit)
2954 return OK;
2955 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 return FAIL;
2957}
2958
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002959/*
2960 * Lock or unlock variable indicated by "lp".
2961 * "deep" is the levels to go (-1 for unlimited);
2962 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2963 */
2964 static int
2965do_lock_var(lp, name_end, deep, lock)
2966 lval_T *lp;
2967 char_u *name_end;
2968 int deep;
2969 int lock;
2970{
2971 int ret = OK;
2972 int cc;
2973 dictitem_T *di;
2974
2975 if (deep == 0) /* nothing to do */
2976 return OK;
2977
2978 if (lp->ll_tv == NULL)
2979 {
2980 cc = *name_end;
2981 *name_end = NUL;
2982
2983 /* Normal name or expanded name. */
2984 if (check_changedtick(lp->ll_name))
2985 ret = FAIL;
2986 else
2987 {
2988 di = find_var(lp->ll_name, NULL);
2989 if (di == NULL)
2990 ret = FAIL;
2991 else
2992 {
2993 if (lock)
2994 di->di_flags |= DI_FLAGS_LOCK;
2995 else
2996 di->di_flags &= ~DI_FLAGS_LOCK;
2997 item_lock(&di->di_tv, deep, lock);
2998 }
2999 }
3000 *name_end = cc;
3001 }
3002 else if (lp->ll_range)
3003 {
3004 listitem_T *li = lp->ll_li;
3005
3006 /* (un)lock a range of List items. */
3007 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3008 {
3009 item_lock(&li->li_tv, deep, lock);
3010 li = li->li_next;
3011 ++lp->ll_n1;
3012 }
3013 }
3014 else if (lp->ll_list != NULL)
3015 /* (un)lock a List item. */
3016 item_lock(&lp->ll_li->li_tv, deep, lock);
3017 else
3018 /* un(lock) a Dictionary item. */
3019 item_lock(&lp->ll_di->di_tv, deep, lock);
3020
3021 return ret;
3022}
3023
3024/*
3025 * Lock or unlock an item. "deep" is nr of levels to go.
3026 */
3027 static void
3028item_lock(tv, deep, lock)
3029 typval_T *tv;
3030 int deep;
3031 int lock;
3032{
3033 static int recurse = 0;
3034 list_T *l;
3035 listitem_T *li;
3036 dict_T *d;
3037 hashitem_T *hi;
3038 int todo;
3039
3040 if (recurse >= DICT_MAXNEST)
3041 {
3042 EMSG(_("E743: variable nested too deep for (un)lock"));
3043 return;
3044 }
3045 if (deep == 0)
3046 return;
3047 ++recurse;
3048
3049 /* lock/unlock the item itself */
3050 if (lock)
3051 tv->v_lock |= VAR_LOCKED;
3052 else
3053 tv->v_lock &= ~VAR_LOCKED;
3054
3055 switch (tv->v_type)
3056 {
3057 case VAR_LIST:
3058 if ((l = tv->vval.v_list) != NULL)
3059 {
3060 if (lock)
3061 l->lv_lock |= VAR_LOCKED;
3062 else
3063 l->lv_lock &= ~VAR_LOCKED;
3064 if (deep < 0 || deep > 1)
3065 /* recursive: lock/unlock the items the List contains */
3066 for (li = l->lv_first; li != NULL; li = li->li_next)
3067 item_lock(&li->li_tv, deep - 1, lock);
3068 }
3069 break;
3070 case VAR_DICT:
3071 if ((d = tv->vval.v_dict) != NULL)
3072 {
3073 if (lock)
3074 d->dv_lock |= VAR_LOCKED;
3075 else
3076 d->dv_lock &= ~VAR_LOCKED;
3077 if (deep < 0 || deep > 1)
3078 {
3079 /* recursive: lock/unlock the items the List contains */
3080 todo = d->dv_hashtab.ht_used;
3081 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3082 {
3083 if (!HASHITEM_EMPTY(hi))
3084 {
3085 --todo;
3086 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3087 }
3088 }
3089 }
3090 }
3091 }
3092 --recurse;
3093}
3094
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3096/*
3097 * Delete all "menutrans_" variables.
3098 */
3099 void
3100del_menutrans_vars()
3101{
Bram Moolenaar33570922005-01-25 22:26:29 +00003102 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003103 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104
Bram Moolenaar33570922005-01-25 22:26:29 +00003105 hash_lock(&globvarht);
3106 todo = globvarht.ht_used;
3107 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003108 {
3109 if (!HASHITEM_EMPTY(hi))
3110 {
3111 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003112 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3113 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003114 }
3115 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003116 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117}
3118#endif
3119
3120#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3121
3122/*
3123 * Local string buffer for the next two functions to store a variable name
3124 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3125 * get_user_var_name().
3126 */
3127
3128static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3129
3130static char_u *varnamebuf = NULL;
3131static int varnamebuflen = 0;
3132
3133/*
3134 * Function to concatenate a prefix and a variable name.
3135 */
3136 static char_u *
3137cat_prefix_varname(prefix, name)
3138 int prefix;
3139 char_u *name;
3140{
3141 int len;
3142
3143 len = (int)STRLEN(name) + 3;
3144 if (len > varnamebuflen)
3145 {
3146 vim_free(varnamebuf);
3147 len += 10; /* some additional space */
3148 varnamebuf = alloc(len);
3149 if (varnamebuf == NULL)
3150 {
3151 varnamebuflen = 0;
3152 return NULL;
3153 }
3154 varnamebuflen = len;
3155 }
3156 *varnamebuf = prefix;
3157 varnamebuf[1] = ':';
3158 STRCPY(varnamebuf + 2, name);
3159 return varnamebuf;
3160}
3161
3162/*
3163 * Function given to ExpandGeneric() to obtain the list of user defined
3164 * (global/buffer/window/built-in) variable names.
3165 */
3166/*ARGSUSED*/
3167 char_u *
3168get_user_var_name(xp, idx)
3169 expand_T *xp;
3170 int idx;
3171{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003172 static long_u gdone;
3173 static long_u bdone;
3174 static long_u wdone;
3175 static int vidx;
3176 static hashitem_T *hi;
3177 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
3179 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003180 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003181
3182 /* Global variables */
3183 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003185 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003186 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003187 else
3188 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003189 while (HASHITEM_EMPTY(hi))
3190 ++hi;
3191 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3192 return cat_prefix_varname('g', hi->hi_key);
3193 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003195
3196 /* b: variables */
3197 ht = &curbuf->b_vars.dv_hashtab;
3198 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003200 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003201 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003202 else
3203 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003204 while (HASHITEM_EMPTY(hi))
3205 ++hi;
3206 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003208 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003210 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 return (char_u *)"b:changedtick";
3212 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003213
3214 /* w: variables */
3215 ht = &curwin->w_vars.dv_hashtab;
3216 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003218 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003219 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003220 else
3221 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003222 while (HASHITEM_EMPTY(hi))
3223 ++hi;
3224 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003226
3227 /* v: variables */
3228 if (vidx < VV_LEN)
3229 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230
3231 vim_free(varnamebuf);
3232 varnamebuf = NULL;
3233 varnamebuflen = 0;
3234 return NULL;
3235}
3236
3237#endif /* FEAT_CMDL_COMPL */
3238
3239/*
3240 * types for expressions.
3241 */
3242typedef enum
3243{
3244 TYPE_UNKNOWN = 0
3245 , TYPE_EQUAL /* == */
3246 , TYPE_NEQUAL /* != */
3247 , TYPE_GREATER /* > */
3248 , TYPE_GEQUAL /* >= */
3249 , TYPE_SMALLER /* < */
3250 , TYPE_SEQUAL /* <= */
3251 , TYPE_MATCH /* =~ */
3252 , TYPE_NOMATCH /* !~ */
3253} exptype_T;
3254
3255/*
3256 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003257 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3259 */
3260
3261/*
3262 * Handle zero level expression.
3263 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003264 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 * Return OK or FAIL.
3266 */
3267 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003268eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 char_u **nextcmd;
3272 int evaluate;
3273{
3274 int ret;
3275 char_u *p;
3276
3277 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003278 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 if (ret == FAIL || !ends_excmd(*p))
3280 {
3281 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003282 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 /*
3284 * Report the invalid expression unless the expression evaluation has
3285 * been cancelled due to an aborting error, an interrupt, or an
3286 * exception.
3287 */
3288 if (!aborting())
3289 EMSG2(_(e_invexpr2), arg);
3290 ret = FAIL;
3291 }
3292 if (nextcmd != NULL)
3293 *nextcmd = check_nextcmd(p);
3294
3295 return ret;
3296}
3297
3298/*
3299 * Handle top level expression:
3300 * expr1 ? expr0 : expr0
3301 *
3302 * "arg" must point to the first non-white of the expression.
3303 * "arg" is advanced to the next non-white after the recognized expression.
3304 *
3305 * Return OK or FAIL.
3306 */
3307 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003308eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 int evaluate;
3312{
3313 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003314 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
3316 /*
3317 * Get the first variable.
3318 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003319 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 return FAIL;
3321
3322 if ((*arg)[0] == '?')
3323 {
3324 result = FALSE;
3325 if (evaluate)
3326 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003327 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003329 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 }
3331
3332 /*
3333 * Get the second variable.
3334 */
3335 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003336 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 return FAIL;
3338
3339 /*
3340 * Check for the ":".
3341 */
3342 if ((*arg)[0] != ':')
3343 {
3344 EMSG(_("E109: Missing ':' after '?'"));
3345 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003346 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 return FAIL;
3348 }
3349
3350 /*
3351 * Get the third variable.
3352 */
3353 *arg = skipwhite(*arg + 1);
3354 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3355 {
3356 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003357 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 return FAIL;
3359 }
3360 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003361 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 }
3363
3364 return OK;
3365}
3366
3367/*
3368 * Handle first level expression:
3369 * expr2 || expr2 || expr2 logical OR
3370 *
3371 * "arg" must point to the first non-white of the expression.
3372 * "arg" is advanced to the next non-white after the recognized expression.
3373 *
3374 * Return OK or FAIL.
3375 */
3376 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003377eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 int evaluate;
3381{
Bram Moolenaar33570922005-01-25 22:26:29 +00003382 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 long result;
3384 int first;
3385
3386 /*
3387 * Get the first variable.
3388 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003389 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 return FAIL;
3391
3392 /*
3393 * Repeat until there is no following "||".
3394 */
3395 first = TRUE;
3396 result = FALSE;
3397 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3398 {
3399 if (evaluate && first)
3400 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003401 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003403 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 first = FALSE;
3405 }
3406
3407 /*
3408 * Get the second variable.
3409 */
3410 *arg = skipwhite(*arg + 2);
3411 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3412 return FAIL;
3413
3414 /*
3415 * Compute the result.
3416 */
3417 if (evaluate && !result)
3418 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003419 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003421 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
3423 if (evaluate)
3424 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003425 rettv->v_type = VAR_NUMBER;
3426 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
3428 }
3429
3430 return OK;
3431}
3432
3433/*
3434 * Handle second level expression:
3435 * expr3 && expr3 && expr3 logical AND
3436 *
3437 * "arg" must point to the first non-white of the expression.
3438 * "arg" is advanced to the next non-white after the recognized expression.
3439 *
3440 * Return OK or FAIL.
3441 */
3442 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003443eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 int evaluate;
3447{
Bram Moolenaar33570922005-01-25 22:26:29 +00003448 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 long result;
3450 int first;
3451
3452 /*
3453 * Get the first variable.
3454 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003455 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 return FAIL;
3457
3458 /*
3459 * Repeat until there is no following "&&".
3460 */
3461 first = TRUE;
3462 result = TRUE;
3463 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3464 {
3465 if (evaluate && first)
3466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003467 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003469 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 first = FALSE;
3471 }
3472
3473 /*
3474 * Get the second variable.
3475 */
3476 *arg = skipwhite(*arg + 2);
3477 if (eval4(arg, &var2, evaluate && result) == FAIL)
3478 return FAIL;
3479
3480 /*
3481 * Compute the result.
3482 */
3483 if (evaluate && result)
3484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003485 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003487 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 }
3489 if (evaluate)
3490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003491 rettv->v_type = VAR_NUMBER;
3492 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494 }
3495
3496 return OK;
3497}
3498
3499/*
3500 * Handle third level expression:
3501 * var1 == var2
3502 * var1 =~ var2
3503 * var1 != var2
3504 * var1 !~ var2
3505 * var1 > var2
3506 * var1 >= var2
3507 * var1 < var2
3508 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003509 * var1 is var2
3510 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 *
3512 * "arg" must point to the first non-white of the expression.
3513 * "arg" is advanced to the next non-white after the recognized expression.
3514 *
3515 * Return OK or FAIL.
3516 */
3517 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003518eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 int evaluate;
3522{
Bram Moolenaar33570922005-01-25 22:26:29 +00003523 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 char_u *p;
3525 int i;
3526 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003527 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 int len = 2;
3529 long n1, n2;
3530 char_u *s1, *s2;
3531 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3532 regmatch_T regmatch;
3533 int ic;
3534 char_u *save_cpo;
3535
3536 /*
3537 * Get the first variable.
3538 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 return FAIL;
3541
3542 p = *arg;
3543 switch (p[0])
3544 {
3545 case '=': if (p[1] == '=')
3546 type = TYPE_EQUAL;
3547 else if (p[1] == '~')
3548 type = TYPE_MATCH;
3549 break;
3550 case '!': if (p[1] == '=')
3551 type = TYPE_NEQUAL;
3552 else if (p[1] == '~')
3553 type = TYPE_NOMATCH;
3554 break;
3555 case '>': if (p[1] != '=')
3556 {
3557 type = TYPE_GREATER;
3558 len = 1;
3559 }
3560 else
3561 type = TYPE_GEQUAL;
3562 break;
3563 case '<': if (p[1] != '=')
3564 {
3565 type = TYPE_SMALLER;
3566 len = 1;
3567 }
3568 else
3569 type = TYPE_SEQUAL;
3570 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003571 case 'i': if (p[1] == 's')
3572 {
3573 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3574 len = 5;
3575 if (!vim_isIDc(p[len]))
3576 {
3577 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3578 type_is = TRUE;
3579 }
3580 }
3581 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 }
3583
3584 /*
3585 * If there is a comparitive operator, use it.
3586 */
3587 if (type != TYPE_UNKNOWN)
3588 {
3589 /* extra question mark appended: ignore case */
3590 if (p[len] == '?')
3591 {
3592 ic = TRUE;
3593 ++len;
3594 }
3595 /* extra '#' appended: match case */
3596 else if (p[len] == '#')
3597 {
3598 ic = FALSE;
3599 ++len;
3600 }
3601 /* nothing appened: use 'ignorecase' */
3602 else
3603 ic = p_ic;
3604
3605 /*
3606 * Get the second variable.
3607 */
3608 *arg = skipwhite(p + len);
3609 if (eval5(arg, &var2, evaluate) == FAIL)
3610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003611 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 return FAIL;
3613 }
3614
3615 if (evaluate)
3616 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003617 if (type_is && rettv->v_type != var2.v_type)
3618 {
3619 /* For "is" a different type always means FALSE, for "notis"
3620 * it means TRUE. */
3621 n1 = (type == TYPE_NEQUAL);
3622 }
3623 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3624 {
3625 if (type_is)
3626 {
3627 n1 = (rettv->v_type == var2.v_type
3628 && rettv->vval.v_list == var2.vval.v_list);
3629 if (type == TYPE_NEQUAL)
3630 n1 = !n1;
3631 }
3632 else if (rettv->v_type != var2.v_type
3633 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3634 {
3635 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003636 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003637 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003638 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003639 clear_tv(rettv);
3640 clear_tv(&var2);
3641 return FAIL;
3642 }
3643 else
3644 {
3645 /* Compare two Lists for being equal or unequal. */
3646 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3647 if (type == TYPE_NEQUAL)
3648 n1 = !n1;
3649 }
3650 }
3651
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003652 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3653 {
3654 if (type_is)
3655 {
3656 n1 = (rettv->v_type == var2.v_type
3657 && rettv->vval.v_dict == var2.vval.v_dict);
3658 if (type == TYPE_NEQUAL)
3659 n1 = !n1;
3660 }
3661 else if (rettv->v_type != var2.v_type
3662 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3663 {
3664 if (rettv->v_type != var2.v_type)
3665 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3666 else
3667 EMSG(_("E736: Invalid operation for Dictionary"));
3668 clear_tv(rettv);
3669 clear_tv(&var2);
3670 return FAIL;
3671 }
3672 else
3673 {
3674 /* Compare two Dictionaries for being equal or unequal. */
3675 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3676 if (type == TYPE_NEQUAL)
3677 n1 = !n1;
3678 }
3679 }
3680
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003681 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3682 {
3683 if (rettv->v_type != var2.v_type
3684 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3685 {
3686 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003687 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003688 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003689 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003690 clear_tv(rettv);
3691 clear_tv(&var2);
3692 return FAIL;
3693 }
3694 else
3695 {
3696 /* Compare two Funcrefs for being equal or unequal. */
3697 if (rettv->vval.v_string == NULL
3698 || var2.vval.v_string == NULL)
3699 n1 = FALSE;
3700 else
3701 n1 = STRCMP(rettv->vval.v_string,
3702 var2.vval.v_string) == 0;
3703 if (type == TYPE_NEQUAL)
3704 n1 = !n1;
3705 }
3706 }
3707
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 /*
3709 * If one of the two variables is a number, compare as a number.
3710 * When using "=~" or "!~", always compare as string.
3711 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003712 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003715 n1 = get_tv_number(rettv);
3716 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 switch (type)
3718 {
3719 case TYPE_EQUAL: n1 = (n1 == n2); break;
3720 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3721 case TYPE_GREATER: n1 = (n1 > n2); break;
3722 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3723 case TYPE_SMALLER: n1 = (n1 < n2); break;
3724 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3725 case TYPE_UNKNOWN:
3726 case TYPE_MATCH:
3727 case TYPE_NOMATCH: break; /* avoid gcc warning */
3728 }
3729 }
3730 else
3731 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003732 s1 = get_tv_string_buf(rettv, buf1);
3733 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3735 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3736 else
3737 i = 0;
3738 n1 = FALSE;
3739 switch (type)
3740 {
3741 case TYPE_EQUAL: n1 = (i == 0); break;
3742 case TYPE_NEQUAL: n1 = (i != 0); break;
3743 case TYPE_GREATER: n1 = (i > 0); break;
3744 case TYPE_GEQUAL: n1 = (i >= 0); break;
3745 case TYPE_SMALLER: n1 = (i < 0); break;
3746 case TYPE_SEQUAL: n1 = (i <= 0); break;
3747
3748 case TYPE_MATCH:
3749 case TYPE_NOMATCH:
3750 /* avoid 'l' flag in 'cpoptions' */
3751 save_cpo = p_cpo;
3752 p_cpo = (char_u *)"";
3753 regmatch.regprog = vim_regcomp(s2,
3754 RE_MAGIC + RE_STRING);
3755 regmatch.rm_ic = ic;
3756 if (regmatch.regprog != NULL)
3757 {
3758 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3759 vim_free(regmatch.regprog);
3760 if (type == TYPE_NOMATCH)
3761 n1 = !n1;
3762 }
3763 p_cpo = save_cpo;
3764 break;
3765
3766 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3767 }
3768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003769 clear_tv(rettv);
3770 clear_tv(&var2);
3771 rettv->v_type = VAR_NUMBER;
3772 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 }
3774 }
3775
3776 return OK;
3777}
3778
3779/*
3780 * Handle fourth level expression:
3781 * + number addition
3782 * - number subtraction
3783 * . string concatenation
3784 *
3785 * "arg" must point to the first non-white of the expression.
3786 * "arg" is advanced to the next non-white after the recognized expression.
3787 *
3788 * Return OK or FAIL.
3789 */
3790 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003791eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 int evaluate;
3795{
Bram Moolenaar33570922005-01-25 22:26:29 +00003796 typval_T var2;
3797 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 int op;
3799 long n1, n2;
3800 char_u *s1, *s2;
3801 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3802 char_u *p;
3803
3804 /*
3805 * Get the first variable.
3806 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003807 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 return FAIL;
3809
3810 /*
3811 * Repeat computing, until no '+', '-' or '.' is following.
3812 */
3813 for (;;)
3814 {
3815 op = **arg;
3816 if (op != '+' && op != '-' && op != '.')
3817 break;
3818
3819 /*
3820 * Get the second variable.
3821 */
3822 *arg = skipwhite(*arg + 1);
3823 if (eval6(arg, &var2, evaluate) == FAIL)
3824 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003825 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 return FAIL;
3827 }
3828
3829 if (evaluate)
3830 {
3831 /*
3832 * Compute the result.
3833 */
3834 if (op == '.')
3835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003836 s1 = get_tv_string_buf(rettv, buf1);
3837 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003838 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003839 clear_tv(rettv);
3840 rettv->v_type = VAR_STRING;
3841 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003843 else if (op == '+' && rettv->v_type == VAR_LIST
3844 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003845 {
3846 /* concatenate Lists */
3847 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3848 &var3) == FAIL)
3849 {
3850 clear_tv(rettv);
3851 clear_tv(&var2);
3852 return FAIL;
3853 }
3854 clear_tv(rettv);
3855 *rettv = var3;
3856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 else
3858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003859 n1 = get_tv_number(rettv);
3860 n2 = get_tv_number(&var2);
3861 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 if (op == '+')
3863 n1 = n1 + n2;
3864 else
3865 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003866 rettv->v_type = VAR_NUMBER;
3867 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003869 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 }
3871 }
3872 return OK;
3873}
3874
3875/*
3876 * Handle fifth level expression:
3877 * * number multiplication
3878 * / number division
3879 * % number modulo
3880 *
3881 * "arg" must point to the first non-white of the expression.
3882 * "arg" is advanced to the next non-white after the recognized expression.
3883 *
3884 * Return OK or FAIL.
3885 */
3886 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003887eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 int evaluate;
3891{
Bram Moolenaar33570922005-01-25 22:26:29 +00003892 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 int op;
3894 long n1, n2;
3895
3896 /*
3897 * Get the first variable.
3898 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003899 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 return FAIL;
3901
3902 /*
3903 * Repeat computing, until no '*', '/' or '%' is following.
3904 */
3905 for (;;)
3906 {
3907 op = **arg;
3908 if (op != '*' && op != '/' && op != '%')
3909 break;
3910
3911 if (evaluate)
3912 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003913 n1 = get_tv_number(rettv);
3914 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916 else
3917 n1 = 0;
3918
3919 /*
3920 * Get the second variable.
3921 */
3922 *arg = skipwhite(*arg + 1);
3923 if (eval7(arg, &var2, evaluate) == FAIL)
3924 return FAIL;
3925
3926 if (evaluate)
3927 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003928 n2 = get_tv_number(&var2);
3929 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
3931 /*
3932 * Compute the result.
3933 */
3934 if (op == '*')
3935 n1 = n1 * n2;
3936 else if (op == '/')
3937 {
3938 if (n2 == 0) /* give an error message? */
3939 n1 = 0x7fffffffL;
3940 else
3941 n1 = n1 / n2;
3942 }
3943 else
3944 {
3945 if (n2 == 0) /* give an error message? */
3946 n1 = 0;
3947 else
3948 n1 = n1 % n2;
3949 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003950 rettv->v_type = VAR_NUMBER;
3951 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
3953 }
3954
3955 return OK;
3956}
3957
3958/*
3959 * Handle sixth level expression:
3960 * number number constant
3961 * "string" string contstant
3962 * 'string' literal string contstant
3963 * &option-name option value
3964 * @r register contents
3965 * identifier variable value
3966 * function() function call
3967 * $VAR environment variable
3968 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003969 * [expr, expr] List
3970 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 *
3972 * Also handle:
3973 * ! in front logical NOT
3974 * - in front unary minus
3975 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003976 * trailing [] subscript in String or List
3977 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 *
3979 * "arg" must point to the first non-white of the expression.
3980 * "arg" is advanced to the next non-white after the recognized expression.
3981 *
3982 * Return OK or FAIL.
3983 */
3984 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003985eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 int evaluate;
3989{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 long n;
3991 int len;
3992 char_u *s;
3993 int val;
3994 char_u *start_leader, *end_leader;
3995 int ret = OK;
3996 char_u *alias;
3997
3998 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003999 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004000 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004002 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003
4004 /*
4005 * Skip '!' and '-' characters. They are handled later.
4006 */
4007 start_leader = *arg;
4008 while (**arg == '!' || **arg == '-' || **arg == '+')
4009 *arg = skipwhite(*arg + 1);
4010 end_leader = *arg;
4011
4012 switch (**arg)
4013 {
4014 /*
4015 * Number constant.
4016 */
4017 case '0':
4018 case '1':
4019 case '2':
4020 case '3':
4021 case '4':
4022 case '5':
4023 case '6':
4024 case '7':
4025 case '8':
4026 case '9':
4027 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4028 *arg += len;
4029 if (evaluate)
4030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004031 rettv->v_type = VAR_NUMBER;
4032 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034 break;
4035
4036 /*
4037 * String constant: "string".
4038 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004039 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 break;
4041
4042 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004043 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004045 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004046 break;
4047
4048 /*
4049 * List: [expr, expr]
4050 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004051 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 break;
4053
4054 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004055 * Dictionary: {key: val, key: val}
4056 */
4057 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4058 break;
4059
4060 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004061 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004063 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 break;
4065
4066 /*
4067 * Environment variable: $VAR.
4068 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 break;
4071
4072 /*
4073 * Register contents: @r.
4074 */
4075 case '@': ++*arg;
4076 if (evaluate)
4077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004078 rettv->v_type = VAR_STRING;
4079 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 }
4081 if (**arg != NUL)
4082 ++*arg;
4083 break;
4084
4085 /*
4086 * nested expression: (expression).
4087 */
4088 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004089 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 if (**arg == ')')
4091 ++*arg;
4092 else if (ret == OK)
4093 {
4094 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004095 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 ret = FAIL;
4097 }
4098 break;
4099
Bram Moolenaar8c711452005-01-14 21:53:12 +00004100 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 break;
4102 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004103
4104 if (ret == NOTDONE)
4105 {
4106 /*
4107 * Must be a variable or function name.
4108 * Can also be a curly-braces kind of name: {expr}.
4109 */
4110 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004111 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004112 if (alias != NULL)
4113 s = alias;
4114
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004115 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004116 ret = FAIL;
4117 else
4118 {
4119 if (**arg == '(') /* recursive! */
4120 {
4121 /* If "s" is the name of a variable of type VAR_FUNC
4122 * use its contents. */
4123 s = deref_func_name(s, &len);
4124
4125 /* Invoke the function. */
4126 ret = get_func_tv(s, len, rettv, arg,
4127 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004128 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004129 /* Stop the expression evaluation when immediately
4130 * aborting on error, or when an interrupt occurred or
4131 * an exception was thrown but not caught. */
4132 if (aborting())
4133 {
4134 if (ret == OK)
4135 clear_tv(rettv);
4136 ret = FAIL;
4137 }
4138 }
4139 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004140 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004141 else
4142 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004143 }
4144
4145 if (alias != NULL)
4146 vim_free(alias);
4147 }
4148
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 *arg = skipwhite(*arg);
4150
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004151 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4152 * expr(expr). */
4153 if (ret == OK)
4154 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156 /*
4157 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4158 */
4159 if (ret == OK && evaluate && end_leader > start_leader)
4160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004161 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 while (end_leader > start_leader)
4163 {
4164 --end_leader;
4165 if (*end_leader == '!')
4166 val = !val;
4167 else if (*end_leader == '-')
4168 val = -val;
4169 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170 clear_tv(rettv);
4171 rettv->v_type = VAR_NUMBER;
4172 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 }
4174
4175 return ret;
4176}
4177
4178/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004179 * Evaluate an "[expr]" or "[expr:expr]" index.
4180 * "*arg" points to the '['.
4181 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4182 */
4183 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004184eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004185 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004186 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004187 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004188 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004189{
4190 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004191 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004192 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004193 long len = -1;
4194 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004195 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004196 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004197
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004199 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004200 if (verbose)
4201 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004202 return FAIL;
4203 }
4204
Bram Moolenaar8c711452005-01-14 21:53:12 +00004205 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004206 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004207 /*
4208 * dict.name
4209 */
4210 key = *arg + 1;
4211 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4212 ;
4213 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004214 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004215 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004216 }
4217 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004218 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004219 /*
4220 * something[idx]
4221 *
4222 * Get the (first) variable from inside the [].
4223 */
4224 *arg = skipwhite(*arg + 1);
4225 if (**arg == ':')
4226 empty1 = TRUE;
4227 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4228 return FAIL;
4229
4230 /*
4231 * Get the second variable from inside the [:].
4232 */
4233 if (**arg == ':')
4234 {
4235 range = TRUE;
4236 *arg = skipwhite(*arg + 1);
4237 if (**arg == ']')
4238 empty2 = TRUE;
4239 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4240 {
4241 clear_tv(&var1);
4242 return FAIL;
4243 }
4244 }
4245
4246 /* Check for the ']'. */
4247 if (**arg != ']')
4248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004249 if (verbose)
4250 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004251 clear_tv(&var1);
4252 if (range)
4253 clear_tv(&var2);
4254 return FAIL;
4255 }
4256 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004257 }
4258
4259 if (evaluate)
4260 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004261 n1 = 0;
4262 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 n1 = get_tv_number(&var1);
4265 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004266 }
4267 if (range)
4268 {
4269 if (empty2)
4270 n2 = -1;
4271 else
4272 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 n2 = get_tv_number(&var2);
4274 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004275 }
4276 }
4277
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004279 {
4280 case VAR_NUMBER:
4281 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004283 len = (long)STRLEN(s);
4284 if (range)
4285 {
4286 /* The resulting variable is a substring. If the indexes
4287 * are out of range the result is empty. */
4288 if (n1 < 0)
4289 {
4290 n1 = len + n1;
4291 if (n1 < 0)
4292 n1 = 0;
4293 }
4294 if (n2 < 0)
4295 n2 = len + n2;
4296 else if (n2 >= len)
4297 n2 = len;
4298 if (n1 >= len || n2 < 0 || n1 > n2)
4299 s = NULL;
4300 else
4301 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4302 }
4303 else
4304 {
4305 /* The resulting variable is a string of a single
4306 * character. If the index is too big or negative the
4307 * result is empty. */
4308 if (n1 >= len || n1 < 0)
4309 s = NULL;
4310 else
4311 s = vim_strnsave(s + n1, 1);
4312 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313 clear_tv(rettv);
4314 rettv->v_type = VAR_STRING;
4315 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004316 break;
4317
4318 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004320 if (n1 < 0)
4321 n1 = len + n1;
4322 if (!empty1 && (n1 < 0 || n1 >= len))
4323 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004324 if (verbose)
4325 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004326 return FAIL;
4327 }
4328 if (range)
4329 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004330 list_T *l;
4331 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004332
4333 if (n2 < 0)
4334 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004335 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004336 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004337 if (verbose)
4338 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004339 return FAIL;
4340 }
4341 l = list_alloc();
4342 if (l == NULL)
4343 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004344 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004345 n1 <= n2; ++n1)
4346 {
4347 if (list_append_tv(l, &item->li_tv) == FAIL)
4348 {
4349 list_free(l);
4350 return FAIL;
4351 }
4352 item = item->li_next;
4353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004354 clear_tv(rettv);
4355 rettv->v_type = VAR_LIST;
4356 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004357 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004358 }
4359 else
4360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004361 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004362 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004363 clear_tv(rettv);
4364 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004365 }
4366 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004367
4368 case VAR_DICT:
4369 if (range)
4370 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004371 if (verbose)
4372 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004373 if (len == -1)
4374 clear_tv(&var1);
4375 return FAIL;
4376 }
4377 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004378 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004379
4380 if (len == -1)
4381 {
4382 key = get_tv_string(&var1);
4383 if (*key == NUL)
4384 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004385 if (verbose)
4386 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004387 clear_tv(&var1);
4388 return FAIL;
4389 }
4390 }
4391
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004392 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004393
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004394 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004395 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004396 if (len == -1)
4397 clear_tv(&var1);
4398 if (item == NULL)
4399 return FAIL;
4400
4401 copy_tv(&item->di_tv, &var1);
4402 clear_tv(rettv);
4403 *rettv = var1;
4404 }
4405 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004406 }
4407 }
4408
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004409 return OK;
4410}
4411
4412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 * Get an option value.
4414 * "arg" points to the '&' or '+' before the option name.
4415 * "arg" is advanced to character after the option name.
4416 * Return OK or FAIL.
4417 */
4418 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004419get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004421 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 int evaluate;
4423{
4424 char_u *option_end;
4425 long numval;
4426 char_u *stringval;
4427 int opt_type;
4428 int c;
4429 int working = (**arg == '+'); /* has("+option") */
4430 int ret = OK;
4431 int opt_flags;
4432
4433 /*
4434 * Isolate the option name and find its value.
4435 */
4436 option_end = find_option_end(arg, &opt_flags);
4437 if (option_end == NULL)
4438 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004439 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 EMSG2(_("E112: Option name missing: %s"), *arg);
4441 return FAIL;
4442 }
4443
4444 if (!evaluate)
4445 {
4446 *arg = option_end;
4447 return OK;
4448 }
4449
4450 c = *option_end;
4451 *option_end = NUL;
4452 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004453 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454
4455 if (opt_type == -3) /* invalid name */
4456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004457 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 EMSG2(_("E113: Unknown option: %s"), *arg);
4459 ret = FAIL;
4460 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004461 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 {
4463 if (opt_type == -2) /* hidden string option */
4464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004465 rettv->v_type = VAR_STRING;
4466 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468 else if (opt_type == -1) /* hidden number option */
4469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 rettv->v_type = VAR_NUMBER;
4471 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473 else if (opt_type == 1) /* number option */
4474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004475 rettv->v_type = VAR_NUMBER;
4476 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478 else /* string option */
4479 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004480 rettv->v_type = VAR_STRING;
4481 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483 }
4484 else if (working && (opt_type == -2 || opt_type == -1))
4485 ret = FAIL;
4486
4487 *option_end = c; /* put back for error messages */
4488 *arg = option_end;
4489
4490 return ret;
4491}
4492
4493/*
4494 * Allocate a variable for a string constant.
4495 * Return OK or FAIL.
4496 */
4497 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004498get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 int evaluate;
4502{
4503 char_u *p;
4504 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 int extra = 0;
4506
4507 /*
4508 * Find the end of the string, skipping backslashed characters.
4509 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004510 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 {
4512 if (*p == '\\' && p[1] != NUL)
4513 {
4514 ++p;
4515 /* A "\<x>" form occupies at least 4 characters, and produces up
4516 * to 6 characters: reserve space for 2 extra */
4517 if (*p == '<')
4518 extra += 2;
4519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
4521
4522 if (*p != '"')
4523 {
4524 EMSG2(_("E114: Missing quote: %s"), *arg);
4525 return FAIL;
4526 }
4527
4528 /* If only parsing, set *arg and return here */
4529 if (!evaluate)
4530 {
4531 *arg = p + 1;
4532 return OK;
4533 }
4534
4535 /*
4536 * Copy the string into allocated memory, handling backslashed
4537 * characters.
4538 */
4539 name = alloc((unsigned)(p - *arg + extra));
4540 if (name == NULL)
4541 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004542 rettv->v_type = VAR_STRING;
4543 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
Bram Moolenaar8c711452005-01-14 21:53:12 +00004545 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 {
4547 if (*p == '\\')
4548 {
4549 switch (*++p)
4550 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004551 case 'b': *name++ = BS; ++p; break;
4552 case 'e': *name++ = ESC; ++p; break;
4553 case 'f': *name++ = FF; ++p; break;
4554 case 'n': *name++ = NL; ++p; break;
4555 case 'r': *name++ = CAR; ++p; break;
4556 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
4558 case 'X': /* hex: "\x1", "\x12" */
4559 case 'x':
4560 case 'u': /* Unicode: "\u0023" */
4561 case 'U':
4562 if (vim_isxdigit(p[1]))
4563 {
4564 int n, nr;
4565 int c = toupper(*p);
4566
4567 if (c == 'X')
4568 n = 2;
4569 else
4570 n = 4;
4571 nr = 0;
4572 while (--n >= 0 && vim_isxdigit(p[1]))
4573 {
4574 ++p;
4575 nr = (nr << 4) + hex2nr(*p);
4576 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004577 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578#ifdef FEAT_MBYTE
4579 /* For "\u" store the number according to
4580 * 'encoding'. */
4581 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004582 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 else
4584#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004585 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 break;
4588
4589 /* octal: "\1", "\12", "\123" */
4590 case '0':
4591 case '1':
4592 case '2':
4593 case '3':
4594 case '4':
4595 case '5':
4596 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004597 case '7': *name = *p++ - '0';
4598 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004600 *name = (*name << 3) + *p++ - '0';
4601 if (*p >= '0' && *p <= '7')
4602 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004604 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 break;
4606
4607 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004608 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 if (extra != 0)
4610 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004611 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 break;
4613 }
4614 /* FALLTHROUGH */
4615
Bram Moolenaar8c711452005-01-14 21:53:12 +00004616 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 break;
4618 }
4619 }
4620 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004621 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004624 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 *arg = p + 1;
4626
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 return OK;
4628}
4629
4630/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004631 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 * Return OK or FAIL.
4633 */
4634 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004635get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 int evaluate;
4639{
4640 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004641 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004642 int reduce = 0;
4643
4644 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004645 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004646 */
4647 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4648 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004649 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004651 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004652 break;
4653 ++reduce;
4654 ++p;
4655 }
4656 }
4657
Bram Moolenaar8c711452005-01-14 21:53:12 +00004658 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004660 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004661 return FAIL;
4662 }
4663
Bram Moolenaar8c711452005-01-14 21:53:12 +00004664 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004665 if (!evaluate)
4666 {
4667 *arg = p + 1;
4668 return OK;
4669 }
4670
4671 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004672 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004673 */
4674 str = alloc((unsigned)((p - *arg) - reduce));
4675 if (str == NULL)
4676 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 rettv->v_type = VAR_STRING;
4678 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004679
Bram Moolenaar8c711452005-01-14 21:53:12 +00004680 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004682 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004683 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004684 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004685 break;
4686 ++p;
4687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004688 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004690 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004691 *arg = p + 1;
4692
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004693 return OK;
4694}
4695
4696/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004697 * Allocate a variable for a List and fill it from "*arg".
4698 * Return OK or FAIL.
4699 */
4700 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004701get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004702 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004703 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004704 int evaluate;
4705{
Bram Moolenaar33570922005-01-25 22:26:29 +00004706 list_T *l = NULL;
4707 typval_T tv;
4708 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004709
4710 if (evaluate)
4711 {
4712 l = list_alloc();
4713 if (l == NULL)
4714 return FAIL;
4715 }
4716
4717 *arg = skipwhite(*arg + 1);
4718 while (**arg != ']' && **arg != NUL)
4719 {
4720 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4721 goto failret;
4722 if (evaluate)
4723 {
4724 item = listitem_alloc();
4725 if (item != NULL)
4726 {
4727 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004728 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004729 list_append(l, item);
4730 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004731 else
4732 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004733 }
4734
4735 if (**arg == ']')
4736 break;
4737 if (**arg != ',')
4738 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004739 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004740 goto failret;
4741 }
4742 *arg = skipwhite(*arg + 1);
4743 }
4744
4745 if (**arg != ']')
4746 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004747 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004748failret:
4749 if (evaluate)
4750 list_free(l);
4751 return FAIL;
4752 }
4753
4754 *arg = skipwhite(*arg + 1);
4755 if (evaluate)
4756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004757 rettv->v_type = VAR_LIST;
4758 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004759 ++l->lv_refcount;
4760 }
4761
4762 return OK;
4763}
4764
4765/*
4766 * Allocate an empty header for a list.
4767 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004768 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769list_alloc()
4770{
Bram Moolenaar33570922005-01-25 22:26:29 +00004771 return (list_T *)alloc_clear(sizeof(list_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772}
4773
4774/*
4775 * Unreference a list: decrement the reference count and free it when it
4776 * becomes zero.
4777 */
4778 static void
4779list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004780 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004781{
4782 if (l != NULL && --l->lv_refcount <= 0)
4783 list_free(l);
4784}
4785
4786/*
4787 * Free a list, including all items it points to.
4788 * Ignores the reference count.
4789 */
4790 static void
4791list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004792 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004793{
Bram Moolenaar33570922005-01-25 22:26:29 +00004794 listitem_T *item;
4795 listitem_T *next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004796
4797 for (item = l->lv_first; item != NULL; item = next)
4798 {
4799 next = item->li_next;
4800 listitem_free(item);
4801 }
4802 vim_free(l);
4803}
4804
4805/*
4806 * Allocate a list item.
4807 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004808 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004809listitem_alloc()
4810{
Bram Moolenaar33570922005-01-25 22:26:29 +00004811 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004812}
4813
4814/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004815 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004816 */
4817 static void
4818listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004819 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004820{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004822 vim_free(item);
4823}
4824
4825/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004826 * Remove a list item from a List and free it. Also clears the value.
4827 */
4828 static void
4829listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004830 list_T *l;
4831 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004832{
4833 list_remove(l, item, item);
4834 listitem_free(item);
4835}
4836
4837/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004838 * Get the number of items in a list.
4839 */
4840 static long
4841list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004842 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004843{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004844 if (l == NULL)
4845 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004846 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004847}
4848
4849/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004850 * Return TRUE when two lists have exactly the same values.
4851 */
4852 static int
4853list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004854 list_T *l1;
4855 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004856 int ic; /* ignore case for strings */
4857{
Bram Moolenaar33570922005-01-25 22:26:29 +00004858 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004859
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004860 if (list_len(l1) != list_len(l2))
4861 return FALSE;
4862
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004863 for (item1 = l1->lv_first, item2 = l2->lv_first;
4864 item1 != NULL && item2 != NULL;
4865 item1 = item1->li_next, item2 = item2->li_next)
4866 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4867 return FALSE;
4868 return item1 == NULL && item2 == NULL;
4869}
4870
4871/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004872 * Return TRUE when two dictionaries have exactly the same key/values.
4873 */
4874 static int
4875dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004876 dict_T *d1;
4877 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004878 int ic; /* ignore case for strings */
4879{
Bram Moolenaar33570922005-01-25 22:26:29 +00004880 hashitem_T *hi;
4881 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004882 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004883
4884 if (dict_len(d1) != dict_len(d2))
4885 return FALSE;
4886
Bram Moolenaar33570922005-01-25 22:26:29 +00004887 todo = d1->dv_hashtab.ht_used;
4888 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004889 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004890 if (!HASHITEM_EMPTY(hi))
4891 {
4892 item2 = dict_find(d2, hi->hi_key, -1);
4893 if (item2 == NULL)
4894 return FALSE;
4895 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
4896 return FALSE;
4897 --todo;
4898 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004899 }
4900 return TRUE;
4901}
4902
4903/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004904 * Return TRUE if "tv1" and "tv2" have the same value.
4905 * Compares the items just like "==" would compare them.
4906 */
4907 static int
4908tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004909 typval_T *tv1;
4910 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004911 int ic; /* ignore case */
4912{
4913 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4914
4915 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4916 {
4917 /* recursive! */
4918 if (tv1->v_type != tv2->v_type
4919 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4920 return FALSE;
4921 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004922 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
4923 {
4924 /* recursive! */
4925 if (tv1->v_type != tv2->v_type
4926 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
4927 return FALSE;
4928 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004929 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4930 {
4931 if (tv1->v_type != tv2->v_type
4932 || tv1->vval.v_string == NULL
4933 || tv2->vval.v_string == NULL
4934 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4935 return FALSE;
4936 }
4937 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4938 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004939 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4940 * Don't consider "4x" to be equal to 4. */
4941 if ((tv1->v_type == VAR_STRING
4942 && !string_isa_number(tv1->vval.v_string))
4943 || (tv2->v_type == VAR_STRING
4944 && !string_isa_number(tv2->vval.v_string)))
4945 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004946 if (get_tv_number(tv1) != get_tv_number(tv2))
4947 return FALSE;
4948 }
4949 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4950 get_tv_string_buf(tv2, buf2)) != 0)
4951 return FALSE;
4952 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4953 get_tv_string_buf(tv2, buf2)) != 0)
4954 return FALSE;
4955 return TRUE;
4956}
4957
4958/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004959 * Return TRUE if "tv" is a number without other non-white characters.
4960 */
4961 static int
4962string_isa_number(s)
4963 char_u *s;
4964{
4965 int len;
4966
4967 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4968 return len > 0 && *skipwhite(s + len) == NUL;
4969}
4970
4971/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004972 * Locate item with index "n" in list "l" and return it.
4973 * A negative index is counted from the end; -1 is the last item.
4974 * Returns NULL when "n" is out of range.
4975 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004976 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004977list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00004978 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004979 long n;
4980{
Bram Moolenaar33570922005-01-25 22:26:29 +00004981 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004982 long idx;
4983
4984 if (l == NULL)
4985 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004986
4987 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004988 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00004989 n = l->lv_len + n;
4990
4991 /* Check for index out of range. */
4992 if (n < 0 || n >= l->lv_len)
4993 return NULL;
4994
4995 /* When there is a cached index may start search from there. */
4996 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004997 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00004998 if (n < l->lv_idx / 2)
4999 {
5000 /* closest to the start of the list */
5001 item = l->lv_first;
5002 idx = 0;
5003 }
5004 else if (n > (l->lv_idx + l->lv_len) / 2)
5005 {
5006 /* closest to the end of the list */
5007 item = l->lv_last;
5008 idx = l->lv_len - 1;
5009 }
5010 else
5011 {
5012 /* closest to the cached index */
5013 item = l->lv_idx_item;
5014 idx = l->lv_idx;
5015 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005016 }
5017 else
5018 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005019 if (n < l->lv_len / 2)
5020 {
5021 /* closest to the start of the list */
5022 item = l->lv_first;
5023 idx = 0;
5024 }
5025 else
5026 {
5027 /* closest to the end of the list */
5028 item = l->lv_last;
5029 idx = l->lv_len - 1;
5030 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005031 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005032
5033 while (n > idx)
5034 {
5035 /* search forward */
5036 item = item->li_next;
5037 ++idx;
5038 }
5039 while (n < idx)
5040 {
5041 /* search backward */
5042 item = item->li_prev;
5043 --idx;
5044 }
5045
5046 /* cache the used index */
5047 l->lv_idx = idx;
5048 l->lv_idx_item = item;
5049
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005050 return item;
5051}
5052
5053/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005054 * Locate "item" list "l" and return its index.
5055 * Returns -1 when "item" is not in the list.
5056 */
5057 static long
5058list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005059 list_T *l;
5060 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005061{
5062 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005063 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005064
5065 if (l == NULL)
5066 return -1;
5067 idx = 0;
5068 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5069 ++idx;
5070 if (li == NULL)
5071 return -1;
5072 return idx;;
5073}
5074
5075/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005076 * Append item "item" to the end of list "l".
5077 */
5078 static void
5079list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005080 list_T *l;
5081 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005082{
5083 if (l->lv_last == NULL)
5084 {
5085 /* empty list */
5086 l->lv_first = item;
5087 l->lv_last = item;
5088 item->li_prev = NULL;
5089 }
5090 else
5091 {
5092 l->lv_last->li_next = item;
5093 item->li_prev = l->lv_last;
5094 l->lv_last = item;
5095 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005096 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005097 item->li_next = NULL;
5098}
5099
5100/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005101 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005102 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005103 */
5104 static int
5105list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005106 list_T *l;
5107 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005108{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005109 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005110
Bram Moolenaar05159a02005-02-26 23:04:13 +00005111 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005112 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005113 copy_tv(tv, &li->li_tv);
5114 list_append(l, li);
5115 return OK;
5116}
5117
5118/*
5119 * Add a dictionary to a list. Used by errorlist().
5120 * Return FAIL when out of memory.
5121 */
5122 int
5123list_append_dict(list, dict)
5124 list_T *list;
5125 dict_T *dict;
5126{
5127 listitem_T *li = listitem_alloc();
5128
5129 if (li == NULL)
5130 return FAIL;
5131 li->li_tv.v_type = VAR_DICT;
5132 li->li_tv.v_lock = 0;
5133 li->li_tv.vval.v_dict = dict;
5134 list_append(list, li);
5135 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005136 return OK;
5137}
5138
5139/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005140 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005141 * If "item" is NULL append at the end.
5142 * Return FAIL when out of memory.
5143 */
5144 static int
5145list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005146 list_T *l;
5147 typval_T *tv;
5148 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005149{
Bram Moolenaar33570922005-01-25 22:26:29 +00005150 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005151
5152 if (ni == NULL)
5153 return FAIL;
5154 copy_tv(tv, &ni->li_tv);
5155 if (item == NULL)
5156 /* Append new item at end of list. */
5157 list_append(l, ni);
5158 else
5159 {
5160 /* Insert new item before existing item. */
5161 ni->li_prev = item->li_prev;
5162 ni->li_next = item;
5163 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005164 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005165 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005166 ++l->lv_idx;
5167 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005168 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005169 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005170 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005171 l->lv_idx_item = NULL;
5172 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005173 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005174 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005175 }
5176 return OK;
5177}
5178
5179/*
5180 * Extend "l1" with "l2".
5181 * If "bef" is NULL append at the end, otherwise insert before this item.
5182 * Returns FAIL when out of memory.
5183 */
5184 static int
5185list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005186 list_T *l1;
5187 list_T *l2;
5188 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005189{
Bram Moolenaar33570922005-01-25 22:26:29 +00005190 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005191
5192 for (item = l2->lv_first; item != NULL; item = item->li_next)
5193 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5194 return FAIL;
5195 return OK;
5196}
5197
5198/*
5199 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5200 * Return FAIL when out of memory.
5201 */
5202 static int
5203list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005204 list_T *l1;
5205 list_T *l2;
5206 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005207{
Bram Moolenaar33570922005-01-25 22:26:29 +00005208 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005209
5210 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005211 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005212 if (l == NULL)
5213 return FAIL;
5214 tv->v_type = VAR_LIST;
5215 tv->vval.v_list = l;
5216
5217 /* append all items from the second list */
5218 return list_extend(l, l2, NULL);
5219}
5220
5221/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005222 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005223 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005224 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225 * Returns NULL when out of memory.
5226 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005227 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005228list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005229 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005230 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005231 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005232{
Bram Moolenaar33570922005-01-25 22:26:29 +00005233 list_T *copy;
5234 listitem_T *item;
5235 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005236
5237 if (orig == NULL)
5238 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239
5240 copy = list_alloc();
5241 if (copy != NULL)
5242 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005243 if (copyID != 0)
5244 {
5245 /* Do this before adding the items, because one of the items may
5246 * refer back to this list. */
5247 orig->lv_copyID = copyID;
5248 orig->lv_copylist = copy;
5249 }
5250 for (item = orig->lv_first; item != NULL && !got_int;
5251 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 {
5253 ni = listitem_alloc();
5254 if (ni == NULL)
5255 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005256 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005257 {
5258 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5259 {
5260 vim_free(ni);
5261 break;
5262 }
5263 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005265 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 list_append(copy, ni);
5267 }
5268 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005269 if (item != NULL)
5270 {
5271 list_unref(copy);
5272 copy = NULL;
5273 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 }
5275
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 return copy;
5277}
5278
5279/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005280 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005281 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005283 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005284list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005285 list_T *l;
5286 listitem_T *item;
5287 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005288{
Bram Moolenaar33570922005-01-25 22:26:29 +00005289 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005291 /* notify watchers */
5292 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005294 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005295 list_fix_watch(l, ip);
5296 if (ip == item2)
5297 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005299
5300 if (item2->li_next == NULL)
5301 l->lv_last = item->li_prev;
5302 else
5303 item2->li_next->li_prev = item->li_prev;
5304 if (item->li_prev == NULL)
5305 l->lv_first = item2->li_next;
5306 else
5307 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005308 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309}
5310
5311/*
5312 * Return an allocated string with the string representation of a list.
5313 * May return NULL.
5314 */
5315 static char_u *
5316list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005317 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005318{
5319 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320
5321 if (tv->vval.v_list == NULL)
5322 return NULL;
5323 ga_init2(&ga, (int)sizeof(char), 80);
5324 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005325 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5326 {
5327 vim_free(ga.ga_data);
5328 return NULL;
5329 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330 ga_append(&ga, ']');
5331 ga_append(&ga, NUL);
5332 return (char_u *)ga.ga_data;
5333}
5334
5335/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005336 * Join list "l" into a string in "*gap", using separator "sep".
5337 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005338 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005339 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005340 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005341list_join(gap, l, sep, echo)
5342 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005343 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005344 char_u *sep;
5345 int echo;
5346{
5347 int first = TRUE;
5348 char_u *tofree;
5349 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005350 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005351 char_u *s;
5352
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005353 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005354 {
5355 if (first)
5356 first = FALSE;
5357 else
5358 ga_concat(gap, sep);
5359
5360 if (echo)
5361 s = echo_string(&item->li_tv, &tofree, numbuf);
5362 else
5363 s = tv2string(&item->li_tv, &tofree, numbuf);
5364 if (s != NULL)
5365 ga_concat(gap, s);
5366 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005367 if (s == NULL)
5368 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005369 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005370 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005371}
5372
5373/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374 * Allocate an empty header for a dictionary.
5375 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005376 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005377dict_alloc()
5378{
Bram Moolenaar33570922005-01-25 22:26:29 +00005379 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005380
Bram Moolenaar33570922005-01-25 22:26:29 +00005381 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005382 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005383 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005384 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005385 d->dv_lock = 0;
5386 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005387 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005388 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005389 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005390}
5391
5392/*
5393 * Unreference a Dictionary: decrement the reference count and free it when it
5394 * becomes zero.
5395 */
5396 static void
5397dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005398 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005399{
5400 if (d != NULL && --d->dv_refcount <= 0)
5401 dict_free(d);
5402}
5403
5404/*
5405 * Free a Dictionary, including all items it contains.
5406 * Ignores the reference count.
5407 */
5408 static void
5409dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005410 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005411{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005412 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005413 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005414
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005415 /* Careful: we free the dictitems while they still appear in the
Bram Moolenaar33570922005-01-25 22:26:29 +00005416 * hashtab. Must not try to resize the hashtab! */
5417 todo = d->dv_hashtab.ht_used;
5418 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005419 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005420 if (!HASHITEM_EMPTY(hi))
5421 {
5422 dictitem_free(HI2DI(hi));
5423 --todo;
5424 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005425 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005426 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005427 vim_free(d);
5428}
5429
5430/*
5431 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005432 * The "key" is copied to the new item.
5433 * Note that the value of the item "di_tv" still needs to be initialized!
5434 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005435 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005436 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005437dictitem_alloc(key)
5438 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005439{
Bram Moolenaar33570922005-01-25 22:26:29 +00005440 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005441
Bram Moolenaar33570922005-01-25 22:26:29 +00005442 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005443 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005444 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005445 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005446 di->di_flags = 0;
5447 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005448 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005449}
5450
5451/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005452 * Make a copy of a Dictionary item.
5453 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005454 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005455dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005456 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005457{
Bram Moolenaar33570922005-01-25 22:26:29 +00005458 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005459
Bram Moolenaar33570922005-01-25 22:26:29 +00005460 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005461 if (di != NULL)
5462 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005463 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005464 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005465 copy_tv(&org->di_tv, &di->di_tv);
5466 }
5467 return di;
5468}
5469
5470/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005471 * Remove item "item" from Dictionary "dict" and free it.
5472 */
5473 static void
5474dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005475 dict_T *dict;
5476 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005477{
Bram Moolenaar33570922005-01-25 22:26:29 +00005478 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005479
Bram Moolenaar33570922005-01-25 22:26:29 +00005480 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005481 if (HASHITEM_EMPTY(hi))
5482 EMSG2(_(e_intern2), "dictitem_remove()");
5483 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005484 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005485 dictitem_free(item);
5486}
5487
5488/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489 * Free a dict item. Also clears the value.
5490 */
5491 static void
5492dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005493 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005494{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005495 clear_tv(&item->di_tv);
5496 vim_free(item);
5497}
5498
5499/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005500 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5501 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005502 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00005503 * Returns NULL when out of memory.
5504 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005505 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005506dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005507 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005508 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005509 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005510{
Bram Moolenaar33570922005-01-25 22:26:29 +00005511 dict_T *copy;
5512 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005513 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005514 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005515
5516 if (orig == NULL)
5517 return NULL;
5518
5519 copy = dict_alloc();
5520 if (copy != NULL)
5521 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005522 if (copyID != 0)
5523 {
5524 orig->dv_copyID = copyID;
5525 orig->dv_copydict = copy;
5526 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005527 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005528 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005529 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005530 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005531 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005532 --todo;
5533
5534 di = dictitem_alloc(hi->hi_key);
5535 if (di == NULL)
5536 break;
5537 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005538 {
5539 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
5540 copyID) == FAIL)
5541 {
5542 vim_free(di);
5543 break;
5544 }
5545 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005546 else
5547 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5548 if (dict_add(copy, di) == FAIL)
5549 {
5550 dictitem_free(di);
5551 break;
5552 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005553 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005554 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005555
Bram Moolenaare9a41262005-01-15 22:18:47 +00005556 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005557 if (todo > 0)
5558 {
5559 dict_unref(copy);
5560 copy = NULL;
5561 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005562 }
5563
5564 return copy;
5565}
5566
5567/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005568 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005569 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005570 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005571 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005572dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005573 dict_T *d;
5574 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575{
Bram Moolenaar33570922005-01-25 22:26:29 +00005576 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577}
5578
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005580 * Add a number or string entry to dictionary "d".
5581 * When "str" is NULL use number "nr", otherwise use "str".
5582 * Returns FAIL when out of memory and when key already exists.
5583 */
5584 int
5585dict_add_nr_str(d, key, nr, str)
5586 dict_T *d;
5587 char *key;
5588 long nr;
5589 char_u *str;
5590{
5591 dictitem_T *item;
5592
5593 item = dictitem_alloc((char_u *)key);
5594 if (item == NULL)
5595 return FAIL;
5596 item->di_tv.v_lock = 0;
5597 if (str == NULL)
5598 {
5599 item->di_tv.v_type = VAR_NUMBER;
5600 item->di_tv.vval.v_number = nr;
5601 }
5602 else
5603 {
5604 item->di_tv.v_type = VAR_STRING;
5605 item->di_tv.vval.v_string = vim_strsave(str);
5606 }
5607 if (dict_add(d, item) == FAIL)
5608 {
5609 dictitem_free(item);
5610 return FAIL;
5611 }
5612 return OK;
5613}
5614
5615/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005616 * Get the number of items in a Dictionary.
5617 */
5618 static long
5619dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005620 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005621{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005622 if (d == NULL)
5623 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00005624 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005625}
5626
5627/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005628 * Find item "key[len]" in Dictionary "d".
5629 * If "len" is negative use strlen(key).
5630 * Returns NULL when not found.
5631 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005632 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005633dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00005634 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635 char_u *key;
5636 int len;
5637{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005638#define AKEYLEN 200
5639 char_u buf[AKEYLEN];
5640 char_u *akey;
5641 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00005642 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005643
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005644 if (len < 0)
5645 akey = key;
5646 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005647 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005648 tofree = akey = vim_strnsave(key, len);
5649 if (akey == NULL)
5650 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005651 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005652 else
5653 {
5654 /* Avoid a malloc/free by using buf[]. */
5655 STRNCPY(buf, key, len);
5656 buf[len] = NUL;
5657 akey = buf;
5658 }
5659
Bram Moolenaar33570922005-01-25 22:26:29 +00005660 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005661 vim_free(tofree);
5662 if (HASHITEM_EMPTY(hi))
5663 return NULL;
5664 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005665}
5666
5667/*
5668 * Return an allocated string with the string representation of a Dictionary.
5669 * May return NULL.
5670 */
5671 static char_u *
5672dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005673 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005674{
5675 garray_T ga;
5676 int first = TRUE;
5677 char_u *tofree;
5678 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005679 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00005681 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005682 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005683
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005684 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005685 return NULL;
5686 ga_init2(&ga, (int)sizeof(char), 80);
5687 ga_append(&ga, '{');
5688
Bram Moolenaar33570922005-01-25 22:26:29 +00005689 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005690 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005691 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005692 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005694 --todo;
5695
5696 if (first)
5697 first = FALSE;
5698 else
5699 ga_concat(&ga, (char_u *)", ");
5700
5701 tofree = string_quote(hi->hi_key, FALSE);
5702 if (tofree != NULL)
5703 {
5704 ga_concat(&ga, tofree);
5705 vim_free(tofree);
5706 }
5707 ga_concat(&ga, (char_u *)": ");
5708 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5709 if (s != NULL)
5710 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005712 if (s == NULL)
5713 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005715 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005716 if (todo > 0)
5717 {
5718 vim_free(ga.ga_data);
5719 return NULL;
5720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005721
5722 ga_append(&ga, '}');
5723 ga_append(&ga, NUL);
5724 return (char_u *)ga.ga_data;
5725}
5726
5727/*
5728 * Allocate a variable for a Dictionary and fill it from "*arg".
5729 * Return OK or FAIL. Returns NOTDONE for {expr}.
5730 */
5731 static int
5732get_dict_tv(arg, rettv, evaluate)
5733 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005734 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 int evaluate;
5736{
Bram Moolenaar33570922005-01-25 22:26:29 +00005737 dict_T *d = NULL;
5738 typval_T tvkey;
5739 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00005741 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005743 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744
5745 /*
5746 * First check if it's not a curly-braces thing: {expr}.
5747 * Must do this without evaluating, otherwise a function may be called
5748 * twice. Unfortunately this means we need to call eval1() twice for the
5749 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005750 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005752 if (*start != '}')
5753 {
5754 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5755 return FAIL;
5756 if (*start == '}')
5757 return NOTDONE;
5758 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759
5760 if (evaluate)
5761 {
5762 d = dict_alloc();
5763 if (d == NULL)
5764 return FAIL;
5765 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005766 tvkey.v_type = VAR_UNKNOWN;
5767 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005768
5769 *arg = skipwhite(*arg + 1);
5770 while (**arg != '}' && **arg != NUL)
5771 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005772 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 goto failret;
5774 if (**arg != ':')
5775 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005776 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005777 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 goto failret;
5779 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005780 key = get_tv_string_buf(&tvkey, buf);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 if (*key == NUL)
5782 {
5783 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005784 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 goto failret;
5786 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787
5788 *arg = skipwhite(*arg + 1);
5789 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5790 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005791 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 goto failret;
5793 }
5794 if (evaluate)
5795 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005796 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 if (item != NULL)
5798 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005799 EMSG(_("E721: Duplicate key in Dictionary"));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005800 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 clear_tv(&tv);
5802 goto failret;
5803 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005804 item = dictitem_alloc(key);
5805 clear_tv(&tvkey);
5806 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005809 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005810 if (dict_add(d, item) == FAIL)
5811 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 }
5813 }
5814
5815 if (**arg == '}')
5816 break;
5817 if (**arg != ',')
5818 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005819 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 goto failret;
5821 }
5822 *arg = skipwhite(*arg + 1);
5823 }
5824
5825 if (**arg != '}')
5826 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005827 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828failret:
5829 if (evaluate)
5830 dict_free(d);
5831 return FAIL;
5832 }
5833
5834 *arg = skipwhite(*arg + 1);
5835 if (evaluate)
5836 {
5837 rettv->v_type = VAR_DICT;
5838 rettv->vval.v_dict = d;
5839 ++d->dv_refcount;
5840 }
5841
5842 return OK;
5843}
5844
Bram Moolenaar8c711452005-01-14 21:53:12 +00005845/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005846 * Return a string with the string representation of a variable.
5847 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005848 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850 * May return NULL;
5851 */
5852 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005854 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005856 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005858 static int recurse = 0;
5859 char_u *r = NULL;
5860
Bram Moolenaar33570922005-01-25 22:26:29 +00005861 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005862 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005863 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005864 *tofree = NULL;
5865 return NULL;
5866 }
5867 ++recurse;
5868
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005869 switch (tv->v_type)
5870 {
5871 case VAR_FUNC:
5872 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005873 r = tv->vval.v_string;
5874 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875 case VAR_LIST:
5876 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005877 r = *tofree;
5878 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 case VAR_DICT:
5880 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005881 r = *tofree;
5882 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005883 case VAR_STRING:
5884 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005885 *tofree = NULL;
5886 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005888 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00005890 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005892
5893 --recurse;
5894 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895}
5896
5897/*
5898 * Return a string with the string representation of a variable.
5899 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5900 * "numbuf" is used for a number.
5901 * Puts quotes around strings, so that they can be parsed back by eval().
5902 * May return NULL;
5903 */
5904 static char_u *
5905tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005906 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005907 char_u **tofree;
5908 char_u *numbuf;
5909{
5910 switch (tv->v_type)
5911 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912 case VAR_FUNC:
5913 *tofree = string_quote(tv->vval.v_string, TRUE);
5914 return *tofree;
5915 case VAR_STRING:
5916 *tofree = string_quote(tv->vval.v_string, FALSE);
5917 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005918 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005919 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005920 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005921 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005922 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005923 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005924 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005925 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926}
5927
5928/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005929 * Return string "str" in ' quotes, doubling ' characters.
5930 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005932 */
5933 static char_u *
5934string_quote(str, function)
5935 char_u *str;
5936 int function;
5937{
Bram Moolenaar33570922005-01-25 22:26:29 +00005938 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005939 char_u *p, *r, *s;
5940
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 len = (function ? 13 : 3);
5942 if (str != NULL)
5943 {
5944 len += STRLEN(str);
5945 for (p = str; *p != NUL; mb_ptr_adv(p))
5946 if (*p == '\'')
5947 ++len;
5948 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005949 s = r = alloc(len);
5950 if (r != NULL)
5951 {
5952 if (function)
5953 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005954 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005955 r += 10;
5956 }
5957 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005958 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005959 if (str != NULL)
5960 for (p = str; *p != NUL; )
5961 {
5962 if (*p == '\'')
5963 *r++ = '\'';
5964 MB_COPY_CHAR(p, r);
5965 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005966 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005967 if (function)
5968 *r++ = ')';
5969 *r++ = NUL;
5970 }
5971 return s;
5972}
5973
5974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 * Get the value of an environment variable.
5976 * "arg" is pointing to the '$'. It is advanced to after the name.
5977 * If the environment variable was not set, silently assume it is empty.
5978 * Always return OK.
5979 */
5980 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005981get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 int evaluate;
5985{
5986 char_u *string = NULL;
5987 int len;
5988 int cc;
5989 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005990 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991
5992 ++*arg;
5993 name = *arg;
5994 len = get_env_len(arg);
5995 if (evaluate)
5996 {
5997 if (len != 0)
5998 {
5999 cc = name[len];
6000 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006001 /* first try vim_getenv(), fast for normal environment vars */
6002 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006004 {
6005 if (!mustfree)
6006 string = vim_strsave(string);
6007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 else
6009 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006010 if (mustfree)
6011 vim_free(string);
6012
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 /* next try expanding things like $VIM and ${HOME} */
6014 string = expand_env_save(name - 1);
6015 if (string != NULL && *string == '$')
6016 {
6017 vim_free(string);
6018 string = NULL;
6019 }
6020 }
6021 name[len] = cc;
6022 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006023 rettv->v_type = VAR_STRING;
6024 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 }
6026
6027 return OK;
6028}
6029
6030/*
6031 * Array with names and number of arguments of all internal functions
6032 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6033 */
6034static struct fst
6035{
6036 char *f_name; /* function name */
6037 char f_min_argc; /* minimal number of arguments */
6038 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006039 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041} functions[] =
6042{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006043 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 {"append", 2, 2, f_append},
6045 {"argc", 0, 0, f_argc},
6046 {"argidx", 0, 0, f_argidx},
6047 {"argv", 1, 1, f_argv},
6048 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006049 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 {"bufexists", 1, 1, f_bufexists},
6051 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6052 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6053 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6054 {"buflisted", 1, 1, f_buflisted},
6055 {"bufloaded", 1, 1, f_bufloaded},
6056 {"bufname", 1, 1, f_bufname},
6057 {"bufnr", 1, 1, f_bufnr},
6058 {"bufwinnr", 1, 1, f_bufwinnr},
6059 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006060 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006061 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 {"char2nr", 1, 1, f_char2nr},
6063 {"cindent", 1, 1, f_cindent},
6064 {"col", 1, 1, f_col},
6065 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006067 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 {"cscope_connection",0,3, f_cscope_connection},
6069 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006070 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 {"delete", 1, 1, f_delete},
6072 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006073 {"diff_filler", 1, 1, f_diff_filler},
6074 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006075 {"empty", 1, 1, f_empty},
Bram Moolenaar05159a02005-02-26 23:04:13 +00006076 {"errorlist", 0, 0, f_errorlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006078 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 {"eventhandler", 0, 0, f_eventhandler},
6080 {"executable", 1, 1, f_executable},
6081 {"exists", 1, 1, f_exists},
6082 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006083 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6085 {"filereadable", 1, 1, f_filereadable},
6086 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006087 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006088 {"finddir", 1, 3, f_finddir},
6089 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 {"fnamemodify", 2, 2, f_fnamemodify},
6091 {"foldclosed", 1, 1, f_foldclosed},
6092 {"foldclosedend", 1, 1, f_foldclosedend},
6093 {"foldlevel", 1, 1, f_foldlevel},
6094 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006095 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006098 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 {"getbufvar", 2, 2, f_getbufvar},
6100 {"getchar", 0, 1, f_getchar},
6101 {"getcharmod", 0, 0, f_getcharmod},
6102 {"getcmdline", 0, 0, f_getcmdline},
6103 {"getcmdpos", 0, 0, f_getcmdpos},
6104 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006105 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006106 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 {"getfsize", 1, 1, f_getfsize},
6108 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006109 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006110 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 {"getreg", 0, 1, f_getreg},
6112 {"getregtype", 0, 1, f_getregtype},
6113 {"getwinposx", 0, 0, f_getwinposx},
6114 {"getwinposy", 0, 0, f_getwinposy},
6115 {"getwinvar", 2, 2, f_getwinvar},
6116 {"glob", 1, 1, f_glob},
6117 {"globpath", 2, 2, f_globpath},
6118 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006119 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 {"hasmapto", 1, 2, f_hasmapto},
6121 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6122 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6123 {"histadd", 2, 2, f_histadd},
6124 {"histdel", 1, 2, f_histdel},
6125 {"histget", 1, 2, f_histget},
6126 {"histnr", 1, 1, f_histnr},
6127 {"hlID", 1, 1, f_hlID},
6128 {"hlexists", 1, 1, f_hlexists},
6129 {"hostname", 0, 0, f_hostname},
6130 {"iconv", 3, 3, f_iconv},
6131 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006132 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 {"input", 1, 2, f_input},
6134 {"inputdialog", 1, 3, f_inputdialog},
6135 {"inputrestore", 0, 0, f_inputrestore},
6136 {"inputsave", 0, 0, f_inputsave},
6137 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006138 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006140 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006141 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006142 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006143 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 {"libcall", 3, 3, f_libcall},
6147 {"libcallnr", 3, 3, f_libcallnr},
6148 {"line", 1, 1, f_line},
6149 {"line2byte", 1, 1, f_line2byte},
6150 {"lispindent", 1, 1, f_lispindent},
6151 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006152 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 {"maparg", 1, 2, f_maparg},
6154 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006155 {"match", 2, 4, f_match},
6156 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006157 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006158 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006159 {"max", 1, 1, f_max},
6160 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006161#ifdef vim_mkdir
6162 {"mkdir", 1, 3, f_mkdir},
6163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 {"mode", 0, 0, f_mode},
6165 {"nextnonblank", 1, 1, f_nextnonblank},
6166 {"nr2char", 1, 1, f_nr2char},
6167 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006168 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006169 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 {"remote_expr", 2, 3, f_remote_expr},
6171 {"remote_foreground", 1, 1, f_remote_foreground},
6172 {"remote_peek", 1, 2, f_remote_peek},
6173 {"remote_read", 1, 1, f_remote_read},
6174 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006175 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006177 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006179 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 {"search", 1, 2, f_search},
6181 {"searchpair", 3, 5, f_searchpair},
6182 {"server2client", 2, 2, f_server2client},
6183 {"serverlist", 0, 0, f_serverlist},
6184 {"setbufvar", 3, 3, f_setbufvar},
6185 {"setcmdpos", 1, 1, f_setcmdpos},
6186 {"setline", 2, 2, f_setline},
6187 {"setreg", 2, 3, f_setreg},
6188 {"setwinvar", 3, 3, f_setwinvar},
6189 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006190 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006191 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192#ifdef HAVE_STRFTIME
6193 {"strftime", 1, 2, f_strftime},
6194#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006195 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006196 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 {"strlen", 1, 1, f_strlen},
6198 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006199 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 {"strtrans", 1, 1, f_strtrans},
6201 {"submatch", 1, 1, f_submatch},
6202 {"substitute", 4, 4, f_substitute},
6203 {"synID", 3, 3, f_synID},
6204 {"synIDattr", 2, 3, f_synIDattr},
6205 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006206 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006207 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 {"tempname", 0, 0, f_tempname},
6209 {"tolower", 1, 1, f_tolower},
6210 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006211 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006213 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {"virtcol", 1, 1, f_virtcol},
6215 {"visualmode", 0, 1, f_visualmode},
6216 {"winbufnr", 1, 1, f_winbufnr},
6217 {"wincol", 0, 0, f_wincol},
6218 {"winheight", 1, 1, f_winheight},
6219 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006220 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 {"winrestcmd", 0, 0, f_winrestcmd},
6222 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006223 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224};
6225
6226#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6227
6228/*
6229 * Function given to ExpandGeneric() to obtain the list of internal
6230 * or user defined function names.
6231 */
6232 char_u *
6233get_function_name(xp, idx)
6234 expand_T *xp;
6235 int idx;
6236{
6237 static int intidx = -1;
6238 char_u *name;
6239
6240 if (idx == 0)
6241 intidx = -1;
6242 if (intidx < 0)
6243 {
6244 name = get_user_func_name(xp, idx);
6245 if (name != NULL)
6246 return name;
6247 }
6248 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6249 {
6250 STRCPY(IObuff, functions[intidx].f_name);
6251 STRCAT(IObuff, "(");
6252 if (functions[intidx].f_max_argc == 0)
6253 STRCAT(IObuff, ")");
6254 return IObuff;
6255 }
6256
6257 return NULL;
6258}
6259
6260/*
6261 * Function given to ExpandGeneric() to obtain the list of internal or
6262 * user defined variable or function names.
6263 */
6264/*ARGSUSED*/
6265 char_u *
6266get_expr_name(xp, idx)
6267 expand_T *xp;
6268 int idx;
6269{
6270 static int intidx = -1;
6271 char_u *name;
6272
6273 if (idx == 0)
6274 intidx = -1;
6275 if (intidx < 0)
6276 {
6277 name = get_function_name(xp, idx);
6278 if (name != NULL)
6279 return name;
6280 }
6281 return get_user_var_name(xp, ++intidx);
6282}
6283
6284#endif /* FEAT_CMDL_COMPL */
6285
6286/*
6287 * Find internal function in table above.
6288 * Return index, or -1 if not found
6289 */
6290 static int
6291find_internal_func(name)
6292 char_u *name; /* name of the function */
6293{
6294 int first = 0;
6295 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6296 int cmp;
6297 int x;
6298
6299 /*
6300 * Find the function name in the table. Binary search.
6301 */
6302 while (first <= last)
6303 {
6304 x = first + ((unsigned)(last - first) >> 1);
6305 cmp = STRCMP(name, functions[x].f_name);
6306 if (cmp < 0)
6307 last = x - 1;
6308 else if (cmp > 0)
6309 first = x + 1;
6310 else
6311 return x;
6312 }
6313 return -1;
6314}
6315
6316/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6318 * name it contains, otherwise return "name".
6319 */
6320 static char_u *
6321deref_func_name(name, lenp)
6322 char_u *name;
6323 int *lenp;
6324{
Bram Moolenaar33570922005-01-25 22:26:29 +00006325 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326 int cc;
6327
6328 cc = name[*lenp];
6329 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006330 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006332 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006333 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006334 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006335 {
6336 *lenp = 0;
6337 return (char_u *)""; /* just in case */
6338 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006339 *lenp = STRLEN(v->di_tv.vval.v_string);
6340 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006341 }
6342
6343 return name;
6344}
6345
6346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 * Allocate a variable for the result of a function.
6348 * Return OK or FAIL.
6349 */
6350 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006351get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6352 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 char_u *name; /* name of the function */
6354 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 char_u **arg; /* argument, pointing to the '(' */
6357 linenr_T firstline; /* first line of range */
6358 linenr_T lastline; /* last line of range */
6359 int *doesrange; /* return: function handled range */
6360 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006361 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362{
6363 char_u *argp;
6364 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006365 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 int argcount = 0; /* number of arguments found */
6367
6368 /*
6369 * Get the arguments.
6370 */
6371 argp = *arg;
6372 while (argcount < MAX_FUNC_ARGS)
6373 {
6374 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6375 if (*argp == ')' || *argp == ',' || *argp == NUL)
6376 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6378 {
6379 ret = FAIL;
6380 break;
6381 }
6382 ++argcount;
6383 if (*argp != ',')
6384 break;
6385 }
6386 if (*argp == ')')
6387 ++argp;
6388 else
6389 ret = FAIL;
6390
6391 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006392 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006393 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006395 {
6396 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006397 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006399 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401
6402 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006403 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404
6405 *arg = skipwhite(argp);
6406 return ret;
6407}
6408
6409
6410/*
6411 * Call a function with its resolved parameters
6412 * Return OK or FAIL.
6413 */
6414 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006415call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006416 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 char_u *name; /* name of the function */
6418 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006421 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 linenr_T firstline; /* first line of range */
6423 linenr_T lastline; /* last line of range */
6424 int *doesrange; /* return: function handled range */
6425 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
6428 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429#define ERROR_UNKNOWN 0
6430#define ERROR_TOOMANY 1
6431#define ERROR_TOOFEW 2
6432#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006433#define ERROR_DICT 4
6434#define ERROR_NONE 5
6435#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 int error = ERROR_NONE;
6437 int i;
6438 int llen;
6439 ufunc_T *fp;
6440 int cc;
6441#define FLEN_FIXED 40
6442 char_u fname_buf[FLEN_FIXED + 1];
6443 char_u *fname;
6444
6445 /*
6446 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6447 * Change <SNR>123_name() to K_SNR 123_name().
6448 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6449 */
6450 cc = name[len];
6451 name[len] = NUL;
6452 llen = eval_fname_script(name);
6453 if (llen > 0)
6454 {
6455 fname_buf[0] = K_SPECIAL;
6456 fname_buf[1] = KS_EXTRA;
6457 fname_buf[2] = (int)KE_SNR;
6458 i = 3;
6459 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6460 {
6461 if (current_SID <= 0)
6462 error = ERROR_SCRIPT;
6463 else
6464 {
6465 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6466 i = (int)STRLEN(fname_buf);
6467 }
6468 }
6469 if (i + STRLEN(name + llen) < FLEN_FIXED)
6470 {
6471 STRCPY(fname_buf + i, name + llen);
6472 fname = fname_buf;
6473 }
6474 else
6475 {
6476 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
6477 if (fname == NULL)
6478 error = ERROR_OTHER;
6479 else
6480 {
6481 mch_memmove(fname, fname_buf, (size_t)i);
6482 STRCPY(fname + i, name + llen);
6483 }
6484 }
6485 }
6486 else
6487 fname = name;
6488
6489 *doesrange = FALSE;
6490
6491
6492 /* execute the function if no errors detected and executing */
6493 if (evaluate && error == ERROR_NONE)
6494 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006495 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 error = ERROR_UNKNOWN;
6497
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006498 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 {
6500 /*
6501 * User defined function.
6502 */
6503 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006504
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006506 /* Trigger FuncUndefined event, may load the function. */
6507 if (fp == NULL
6508 && apply_autocmds(EVENT_FUNCUNDEFINED,
6509 fname, fname, TRUE, NULL)
6510 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006512 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 fp = find_func(fname);
6514 }
6515#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006516 /* Try loading a package. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006517 if (fp == NULL && script_autoload(fname) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006518 {
6519 /* loaded a package, search for the function again */
6520 fp = find_func(fname);
6521 }
6522
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 if (fp != NULL)
6524 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006525 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006527 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006529 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006531 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006532 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 else
6534 {
6535 /*
6536 * Call the user function.
6537 * Save and restore search patterns, script variables and
6538 * redo buffer.
6539 */
6540 save_search_patterns();
6541 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006542 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006543 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006544 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006545 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
6546 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
6547 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006548 /* Function was unreferenced while being used, free it
6549 * now. */
6550 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 restoreRedobuff();
6552 restore_search_patterns();
6553 error = ERROR_NONE;
6554 }
6555 }
6556 }
6557 else
6558 {
6559 /*
6560 * Find the function name in the table, call its implementation.
6561 */
6562 i = find_internal_func(fname);
6563 if (i >= 0)
6564 {
6565 if (argcount < functions[i].f_min_argc)
6566 error = ERROR_TOOFEW;
6567 else if (argcount > functions[i].f_max_argc)
6568 error = ERROR_TOOMANY;
6569 else
6570 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006571 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006572 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 error = ERROR_NONE;
6574 }
6575 }
6576 }
6577 /*
6578 * The function call (or "FuncUndefined" autocommand sequence) might
6579 * have been aborted by an error, an interrupt, or an explicitly thrown
6580 * exception that has not been caught so far. This situation can be
6581 * tested for by calling aborting(). For an error in an internal
6582 * function or for the "E132" error in call_user_func(), however, the
6583 * throw point at which the "force_abort" flag (temporarily reset by
6584 * emsg()) is normally updated has not been reached yet. We need to
6585 * update that flag first to make aborting() reliable.
6586 */
6587 update_force_abort();
6588 }
6589 if (error == ERROR_NONE)
6590 ret = OK;
6591
6592 /*
6593 * Report an error unless the argument evaluation or function call has been
6594 * cancelled due to an aborting error, an interrupt, or an exception.
6595 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006596 if (!aborting())
6597 {
6598 switch (error)
6599 {
6600 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006601 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006602 break;
6603 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006604 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006605 break;
6606 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006608 name);
6609 break;
6610 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006611 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006612 name);
6613 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006614 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006615 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00006616 name);
6617 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006618 }
6619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620
6621 name[len] = cc;
6622 if (fname != name && fname != fname_buf)
6623 vim_free(fname);
6624
6625 return ret;
6626}
6627
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006628/*
6629 * Give an error message with a function name. Handle <SNR> things.
6630 */
6631 static void
6632emsg_funcname(msg, name)
6633 char *msg;
6634 char_u *name;
6635{
6636 char_u *p;
6637
6638 if (*name == K_SPECIAL)
6639 p = concat_str((char_u *)"<SNR>", name + 3);
6640 else
6641 p = name;
6642 EMSG2(_(msg), p);
6643 if (p != name)
6644 vim_free(p);
6645}
6646
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647/*********************************************
6648 * Implementation of the built-in functions
6649 */
6650
6651/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006652 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 */
6654 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006655f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006656 typval_T *argvars;
6657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658{
Bram Moolenaar33570922005-01-25 22:26:29 +00006659 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006661 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006664 if ((l = argvars[0].vval.v_list) != NULL
6665 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
6666 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006667 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006668 }
6669 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006670 EMSG(_(e_listreq));
6671}
6672
6673/*
6674 * "append(lnum, string/list)" function
6675 */
6676 static void
6677f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006678 typval_T *argvars;
6679 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006680{
6681 long lnum;
Bram Moolenaar33570922005-01-25 22:26:29 +00006682 list_T *l = NULL;
6683 listitem_T *li = NULL;
6684 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006685 long added = 0;
6686
6687 rettv->vval.v_number = 1; /* Default: Failed */
6688 lnum = get_tv_lnum(argvars);
6689 if (lnum >= 0
6690 && lnum <= curbuf->b_ml.ml_line_count
6691 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006692 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006693 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006695 l = argvars[1].vval.v_list;
6696 if (l == NULL)
6697 return;
6698 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006699 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00006700 for (;;)
6701 {
6702 if (l == NULL)
6703 tv = &argvars[1]; /* append a string */
6704 else if (li == NULL)
6705 break; /* end of list */
6706 else
6707 tv = &li->li_tv; /* append item from list */
6708 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
6709 ++added;
6710 if (l == NULL)
6711 break;
6712 li = li->li_next;
6713 }
6714
6715 appended_lines_mark(lnum, added);
6716 if (curwin->w_cursor.lnum > lnum)
6717 curwin->w_cursor.lnum += added;
6718 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 }
6720}
6721
6722/*
6723 * "argc()" function
6724 */
6725/* ARGSUSED */
6726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006727f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006728 typval_T *argvars;
6729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006731 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732}
6733
6734/*
6735 * "argidx()" function
6736 */
6737/* ARGSUSED */
6738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006739f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006740 typval_T *argvars;
6741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006743 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744}
6745
6746/*
6747 * "argv(nr)" function
6748 */
6749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006750f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006751 typval_T *argvars;
6752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753{
6754 int idx;
6755
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006756 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006758 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006760 rettv->vval.v_string = NULL;
6761 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762}
6763
6764/*
6765 * "browse(save, title, initdir, default)" function
6766 */
6767/* ARGSUSED */
6768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006769f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006770 typval_T *argvars;
6771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772{
6773#ifdef FEAT_BROWSE
6774 int save;
6775 char_u *title;
6776 char_u *initdir;
6777 char_u *defname;
6778 char_u buf[NUMBUFLEN];
6779 char_u buf2[NUMBUFLEN];
6780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006781 save = get_tv_number(&argvars[0]);
6782 title = get_tv_string(&argvars[1]);
6783 initdir = get_tv_string_buf(&argvars[2], buf);
6784 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006786 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006787 do_browse(save ? BROWSE_SAVE : 0,
6788 title, defname, NULL, initdir, NULL, curbuf);
6789#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006790 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006791#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006792 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006793}
6794
6795/*
6796 * "browsedir(title, initdir)" function
6797 */
6798/* ARGSUSED */
6799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006800f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006801 typval_T *argvars;
6802 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006803{
6804#ifdef FEAT_BROWSE
6805 char_u *title;
6806 char_u *initdir;
6807 char_u buf[NUMBUFLEN];
6808
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006809 title = get_tv_string(&argvars[0]);
6810 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006811
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006812 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006813 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006815 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006817 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818}
6819
Bram Moolenaar33570922005-01-25 22:26:29 +00006820static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006821
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822/*
6823 * Find a buffer by number or exact name.
6824 */
6825 static buf_T *
6826find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00006827 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828{
6829 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006831 if (avar->v_type == VAR_NUMBER)
6832 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006833 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006835 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006836 if (buf == NULL)
6837 {
6838 /* No full path name match, try a match with a URL or a "nofile"
6839 * buffer, these don't use the full path. */
6840 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6841 if (buf->b_fname != NULL
6842 && (path_with_url(buf->b_fname)
6843#ifdef FEAT_QUICKFIX
6844 || bt_nofile(buf)
6845#endif
6846 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006847 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006848 break;
6849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 }
6851 return buf;
6852}
6853
6854/*
6855 * "bufexists(expr)" function
6856 */
6857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006858f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006859 typval_T *argvars;
6860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006862 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863}
6864
6865/*
6866 * "buflisted(expr)" function
6867 */
6868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006869f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006870 typval_T *argvars;
6871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872{
6873 buf_T *buf;
6874
6875 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006876 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877}
6878
6879/*
6880 * "bufloaded(expr)" function
6881 */
6882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006883f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006884 typval_T *argvars;
6885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886{
6887 buf_T *buf;
6888
6889 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006890 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891}
6892
Bram Moolenaar33570922005-01-25 22:26:29 +00006893static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006894
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895/*
6896 * Get buffer by number or pattern.
6897 */
6898 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006899get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006900 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006902 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 int save_magic;
6904 char_u *save_cpo;
6905 buf_T *buf;
6906
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006907 if (tv->v_type == VAR_NUMBER)
6908 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006909 if (tv->v_type != VAR_STRING)
6910 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 if (name == NULL || *name == NUL)
6912 return curbuf;
6913 if (name[0] == '$' && name[1] == NUL)
6914 return lastbuf;
6915
6916 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
6917 save_magic = p_magic;
6918 p_magic = TRUE;
6919 save_cpo = p_cpo;
6920 p_cpo = (char_u *)"";
6921
6922 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
6923 TRUE, FALSE));
6924
6925 p_magic = save_magic;
6926 p_cpo = save_cpo;
6927
6928 /* If not found, try expanding the name, like done for bufexists(). */
6929 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006930 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931
6932 return buf;
6933}
6934
6935/*
6936 * "bufname(expr)" function
6937 */
6938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006939f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006940 typval_T *argvars;
6941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942{
6943 buf_T *buf;
6944
6945 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006946 buf = get_buf_tv(&argvars[0]);
6947 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006949 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006951 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 --emsg_off;
6953}
6954
6955/*
6956 * "bufnr(expr)" function
6957 */
6958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006959f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006960 typval_T *argvars;
6961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962{
6963 buf_T *buf;
6964
6965 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006966 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006968 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006970 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 --emsg_off;
6972}
6973
6974/*
6975 * "bufwinnr(nr)" function
6976 */
6977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006978f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006979 typval_T *argvars;
6980 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981{
6982#ifdef FEAT_WINDOWS
6983 win_T *wp;
6984 int winnr = 0;
6985#endif
6986 buf_T *buf;
6987
6988 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006989 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990#ifdef FEAT_WINDOWS
6991 for (wp = firstwin; wp; wp = wp->w_next)
6992 {
6993 ++winnr;
6994 if (wp->w_buffer == buf)
6995 break;
6996 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006997 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006999 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000#endif
7001 --emsg_off;
7002}
7003
7004/*
7005 * "byte2line(byte)" function
7006 */
7007/*ARGSUSED*/
7008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007009f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007010 typval_T *argvars;
7011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012{
7013#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007014 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015#else
7016 long boff = 0;
7017
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007018 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007020 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007022 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 (linenr_T)0, &boff);
7024#endif
7025}
7026
7027/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007028 * "byteidx()" function
7029 */
7030/*ARGSUSED*/
7031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007032f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007033 typval_T *argvars;
7034 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007035{
7036#ifdef FEAT_MBYTE
7037 char_u *t;
7038#endif
7039 char_u *str;
7040 long idx;
7041
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007042 str = get_tv_string(&argvars[0]);
7043 idx = get_tv_number(&argvars[1]);
7044 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007045 if (idx < 0)
7046 return;
7047
7048#ifdef FEAT_MBYTE
7049 t = str;
7050 for ( ; idx > 0; idx--)
7051 {
7052 if (*t == NUL) /* EOL reached */
7053 return;
7054 t += mb_ptr2len_check(t);
7055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007056 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007057#else
7058 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007059 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007060#endif
7061}
7062
7063/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007064 * "call(func, arglist)" function
7065 */
7066 static void
7067f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007068 typval_T *argvars;
7069 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007070{
7071 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007072 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007073 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007074 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007075 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007076 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007077
7078 rettv->vval.v_number = 0;
7079 if (argvars[1].v_type != VAR_LIST)
7080 {
7081 EMSG(_(e_listreq));
7082 return;
7083 }
7084 if (argvars[1].vval.v_list == NULL)
7085 return;
7086
7087 if (argvars[0].v_type == VAR_FUNC)
7088 func = argvars[0].vval.v_string;
7089 else
7090 func = get_tv_string(&argvars[0]);
7091
Bram Moolenaare9a41262005-01-15 22:18:47 +00007092 if (argvars[2].v_type != VAR_UNKNOWN)
7093 {
7094 if (argvars[2].v_type != VAR_DICT)
7095 {
7096 EMSG(_(e_dictreq));
7097 return;
7098 }
7099 selfdict = argvars[2].vval.v_dict;
7100 }
7101
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007102 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7103 item = item->li_next)
7104 {
7105 if (argc == MAX_FUNC_ARGS)
7106 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007107 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007108 break;
7109 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007110 /* Make a copy of each argument. This is needed to be able to set
7111 * v_lock to VAR_FIXED in the copy without changing the original list.
7112 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007113 copy_tv(&item->li_tv, &argv[argc++]);
7114 }
7115
7116 if (item == NULL)
7117 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007118 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7119 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007120
7121 /* Free the arguments. */
7122 while (argc > 0)
7123 clear_tv(&argv[--argc]);
7124}
7125
7126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 * "char2nr(string)" function
7128 */
7129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007130f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007131 typval_T *argvars;
7132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133{
7134#ifdef FEAT_MBYTE
7135 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007136 rettv->vval.v_number =
7137 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 else
7139#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007140 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141}
7142
7143/*
7144 * "cindent(lnum)" function
7145 */
7146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007147f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007148 typval_T *argvars;
7149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150{
7151#ifdef FEAT_CINDENT
7152 pos_T pos;
7153 linenr_T lnum;
7154
7155 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007156 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7158 {
7159 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007160 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 curwin->w_cursor = pos;
7162 }
7163 else
7164#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007165 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166}
7167
7168/*
7169 * "col(string)" function
7170 */
7171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007172f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 typval_T *argvars;
7174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
7176 colnr_T col = 0;
7177 pos_T *fp;
7178
7179 fp = var2fpos(&argvars[0], FALSE);
7180 if (fp != NULL)
7181 {
7182 if (fp->col == MAXCOL)
7183 {
7184 /* '> can be MAXCOL, get the length of the line then */
7185 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7186 col = STRLEN(ml_get(fp->lnum)) + 1;
7187 else
7188 col = MAXCOL;
7189 }
7190 else
7191 {
7192 col = fp->col + 1;
7193#ifdef FEAT_VIRTUALEDIT
7194 /* col(".") when the cursor is on the NUL at the end of the line
7195 * because of "coladd" can be seen as an extra column. */
7196 if (virtual_active() && fp == &curwin->w_cursor)
7197 {
7198 char_u *p = ml_get_cursor();
7199
7200 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7201 curwin->w_virtcol - curwin->w_cursor.coladd))
7202 {
7203# ifdef FEAT_MBYTE
7204 int l;
7205
7206 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7207 col += l;
7208# else
7209 if (*p != NUL && p[1] == NUL)
7210 ++col;
7211# endif
7212 }
7213 }
7214#endif
7215 }
7216 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007217 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218}
7219
7220/*
7221 * "confirm(message, buttons[, default [, type]])" function
7222 */
7223/*ARGSUSED*/
7224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007225f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007226 typval_T *argvars;
7227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228{
7229#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7230 char_u *message;
7231 char_u *buttons = NULL;
7232 char_u buf[NUMBUFLEN];
7233 char_u buf2[NUMBUFLEN];
7234 int def = 1;
7235 int type = VIM_GENERIC;
7236 int c;
7237
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007238 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007239 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007241 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007242 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007244 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007245 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007247 /* avoid that TOUPPER_ASC calls get_tv_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007248 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 switch (TOUPPER_ASC(c))
7250 {
7251 case 'E': type = VIM_ERROR; break;
7252 case 'Q': type = VIM_QUESTION; break;
7253 case 'I': type = VIM_INFO; break;
7254 case 'W': type = VIM_WARNING; break;
7255 case 'G': type = VIM_GENERIC; break;
7256 }
7257 }
7258 }
7259 }
7260
7261 if (buttons == NULL || *buttons == NUL)
7262 buttons = (char_u *)_("&Ok");
7263
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007264 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 def, NULL);
7266#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007267 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268#endif
7269}
7270
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007271/*
7272 * "copy()" function
7273 */
7274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007275f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007276 typval_T *argvars;
7277 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007278{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007279 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007280}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281
7282/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007283 * "count()" function
7284 */
7285 static void
7286f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 typval_T *argvars;
7288 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007289{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007290 long n = 0;
7291 int ic = FALSE;
7292
Bram Moolenaare9a41262005-01-15 22:18:47 +00007293 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007294 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007295 listitem_T *li;
7296 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007298
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299 if ((l = argvars[0].vval.v_list) != NULL)
7300 {
7301 li = l->lv_first;
7302 if (argvars[2].v_type != VAR_UNKNOWN)
7303 {
7304 ic = get_tv_number(&argvars[2]);
7305 if (argvars[3].v_type != VAR_UNKNOWN)
7306 {
7307 idx = get_tv_number(&argvars[3]);
7308 li = list_find(l, idx);
7309 if (li == NULL)
7310 EMSGN(_(e_listidx), idx);
7311 }
7312 }
7313
7314 for ( ; li != NULL; li = li->li_next)
7315 if (tv_equal(&li->li_tv, &argvars[1], ic))
7316 ++n;
7317 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007318 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007319 else if (argvars[0].v_type == VAR_DICT)
7320 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 int todo;
7322 dict_T *d;
7323 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 if ((d = argvars[0].vval.v_dict) != NULL)
7326 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 if (argvars[2].v_type != VAR_UNKNOWN)
7328 {
7329 ic = get_tv_number(&argvars[2]);
7330 if (argvars[3].v_type != VAR_UNKNOWN)
7331 EMSG(_(e_invarg));
7332 }
7333
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 todo = d->dv_hashtab.ht_used;
7335 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007336 {
7337 if (!HASHITEM_EMPTY(hi))
7338 {
7339 --todo;
7340 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7341 ++n;
7342 }
7343 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007344 }
7345 }
7346 else
7347 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007348 rettv->vval.v_number = n;
7349}
7350
7351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7353 *
7354 * Checks the existence of a cscope connection.
7355 */
7356/*ARGSUSED*/
7357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007358f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 typval_T *argvars;
7360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361{
7362#ifdef FEAT_CSCOPE
7363 int num = 0;
7364 char_u *dbpath = NULL;
7365 char_u *prepend = NULL;
7366 char_u buf[NUMBUFLEN];
7367
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007368 if (argvars[0].v_type != VAR_UNKNOWN
7369 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007371 num = (int)get_tv_number(&argvars[0]);
7372 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007373 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007374 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 }
7376
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007377 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007379 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380#endif
7381}
7382
7383/*
7384 * "cursor(lnum, col)" function
7385 *
7386 * Moves the cursor to the specified line and column
7387 */
7388/*ARGSUSED*/
7389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007390f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 typval_T *argvars;
7392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393{
7394 long line, col;
7395
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007396 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 if (line > 0)
7398 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007399 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 if (col > 0)
7401 curwin->w_cursor.col = col - 1;
7402#ifdef FEAT_VIRTUALEDIT
7403 curwin->w_cursor.coladd = 0;
7404#endif
7405
7406 /* Make sure the cursor is in a valid position. */
7407 check_cursor();
7408#ifdef FEAT_MBYTE
7409 /* Correct cursor for multi-byte character. */
7410 if (has_mbyte)
7411 mb_adjust_cursor();
7412#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007413
7414 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415}
7416
7417/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007418 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 */
7420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007421f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007422 typval_T *argvars;
7423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007425 static int copyID = 0;
7426 int noref = 0;
7427
7428 if (argvars[1].v_type != VAR_UNKNOWN)
7429 noref = get_tv_number(&argvars[1]);
7430 if (noref < 0 || noref > 1)
7431 EMSG(_(e_invarg));
7432 else
7433 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434}
7435
7436/*
7437 * "delete()" function
7438 */
7439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007440f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007441 typval_T *argvars;
7442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443{
7444 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007445 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007447 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448}
7449
7450/*
7451 * "did_filetype()" function
7452 */
7453/*ARGSUSED*/
7454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007455f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 typval_T *argvars;
7457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458{
7459#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007460 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007462 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463#endif
7464}
7465
7466/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00007467 * "diff_filler()" function
7468 */
7469/*ARGSUSED*/
7470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007471f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007472 typval_T *argvars;
7473 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007474{
7475#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007476 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00007477#endif
7478}
7479
7480/*
7481 * "diff_hlID()" function
7482 */
7483/*ARGSUSED*/
7484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007485f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007486 typval_T *argvars;
7487 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007488{
7489#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007490 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00007491 static linenr_T prev_lnum = 0;
7492 static int changedtick = 0;
7493 static int fnum = 0;
7494 static int change_start = 0;
7495 static int change_end = 0;
7496 static enum hlf_value hlID = 0;
7497 int filler_lines;
7498 int col;
7499
7500 if (lnum != prev_lnum
7501 || changedtick != curbuf->b_changedtick
7502 || fnum != curbuf->b_fnum)
7503 {
7504 /* New line, buffer, change: need to get the values. */
7505 filler_lines = diff_check(curwin, lnum);
7506 if (filler_lines < 0)
7507 {
7508 if (filler_lines == -1)
7509 {
7510 change_start = MAXCOL;
7511 change_end = -1;
7512 if (diff_find_change(curwin, lnum, &change_start, &change_end))
7513 hlID = HLF_ADD; /* added line */
7514 else
7515 hlID = HLF_CHD; /* changed line */
7516 }
7517 else
7518 hlID = HLF_ADD; /* added line */
7519 }
7520 else
7521 hlID = (enum hlf_value)0;
7522 prev_lnum = lnum;
7523 changedtick = curbuf->b_changedtick;
7524 fnum = curbuf->b_fnum;
7525 }
7526
7527 if (hlID == HLF_CHD || hlID == HLF_TXD)
7528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007529 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007530 if (col >= change_start && col <= change_end)
7531 hlID = HLF_TXD; /* changed text */
7532 else
7533 hlID = HLF_CHD; /* changed line */
7534 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007535 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007536#endif
7537}
7538
7539/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007540 * "empty({expr})" function
7541 */
7542 static void
7543f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 typval_T *argvars;
7545 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007546{
7547 int n;
7548
7549 switch (argvars[0].v_type)
7550 {
7551 case VAR_STRING:
7552 case VAR_FUNC:
7553 n = argvars[0].vval.v_string == NULL
7554 || *argvars[0].vval.v_string == NUL;
7555 break;
7556 case VAR_NUMBER:
7557 n = argvars[0].vval.v_number == 0;
7558 break;
7559 case VAR_LIST:
7560 n = argvars[0].vval.v_list == NULL
7561 || argvars[0].vval.v_list->lv_first == NULL;
7562 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007563 case VAR_DICT:
7564 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00007565 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007566 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007567 default:
7568 EMSG2(_(e_intern2), "f_empty()");
7569 n = 0;
7570 }
7571
7572 rettv->vval.v_number = n;
7573}
7574
7575/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007576 * "errorlist()" function
7577 */
7578/*ARGSUSED*/
7579 static void
7580f_errorlist(argvars, rettv)
7581 typval_T *argvars;
7582 typval_T *rettv;
7583{
7584#ifdef FEAT_QUICKFIX
7585 list_T *l;
7586#endif
7587
7588 rettv->vval.v_number = FALSE;
7589#ifdef FEAT_QUICKFIX
7590 l = list_alloc();
7591 if (l != NULL)
7592 {
7593 if (get_errorlist(l) != FAIL)
7594 {
7595 rettv->vval.v_list = l;
7596 rettv->v_type = VAR_LIST;
7597 ++l->lv_refcount;
7598 }
7599 else
7600 list_free(l);
7601 }
7602#endif
7603}
7604
7605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 * "escape({string}, {chars})" function
7607 */
7608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007609f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007610 typval_T *argvars;
7611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612{
7613 char_u buf[NUMBUFLEN];
7614
Bram Moolenaar758711c2005-02-02 23:11:38 +00007615 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
7616 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007617 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618}
7619
7620/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007621 * "eval()" function
7622 */
7623/*ARGSUSED*/
7624 static void
7625f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 typval_T *argvars;
7627 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007628{
7629 char_u *s;
7630
7631 s = get_tv_string(&argvars[0]);
7632 s = skipwhite(s);
7633
7634 if (eval1(&s, rettv, TRUE) == FAIL)
7635 rettv->vval.v_number = 0;
7636 else if (*s != NUL)
7637 EMSG(_(e_trailing));
7638}
7639
7640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 * "eventhandler()" function
7642 */
7643/*ARGSUSED*/
7644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007645f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007646 typval_T *argvars;
7647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007649 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650}
7651
7652/*
7653 * "executable()" function
7654 */
7655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007656f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007657 typval_T *argvars;
7658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007660 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661}
7662
7663/*
7664 * "exists()" function
7665 */
7666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007667f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 typval_T *argvars;
7669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
7671 char_u *p;
7672 char_u *name;
7673 int n = FALSE;
7674 int len = 0;
7675
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007676 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 if (*p == '$') /* environment variable */
7678 {
7679 /* first try "normal" environment variables (fast) */
7680 if (mch_getenv(p + 1) != NULL)
7681 n = TRUE;
7682 else
7683 {
7684 /* try expanding things like $VIM and ${HOME} */
7685 p = expand_env_save(p);
7686 if (p != NULL && *p != '$')
7687 n = TRUE;
7688 vim_free(p);
7689 }
7690 }
7691 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007692 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 else if (*p == '*') /* internal or user defined function */
7694 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007695 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 }
7697 else if (*p == ':')
7698 {
7699 n = cmd_exists(p + 1);
7700 }
7701 else if (*p == '#')
7702 {
7703#ifdef FEAT_AUTOCMD
7704 name = p + 1;
7705 p = vim_strchr(name, '#');
7706 if (p != NULL)
7707 n = au_exists(name, p, p + 1);
7708 else
7709 n = au_exists(name, name + STRLEN(name), NULL);
7710#endif
7711 }
7712 else /* internal variable */
7713 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007714 char_u *tofree;
7715 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007717 /* get_name_len() takes care of expanding curly braces */
7718 name = p;
7719 len = get_name_len(&p, &tofree, TRUE, FALSE);
7720 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007722 if (tofree != NULL)
7723 name = tofree;
7724 n = (get_var_tv(name, len, &tv, FALSE) == OK);
7725 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007727 /* handle d.key, l[idx], f(expr) */
7728 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
7729 if (n)
7730 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 }
7732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007734 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 }
7736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007737 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738}
7739
7740/*
7741 * "expand()" function
7742 */
7743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007744f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007745 typval_T *argvars;
7746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747{
7748 char_u *s;
7749 int len;
7750 char_u *errormsg;
7751 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7752 expand_T xpc;
7753
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007754 rettv->v_type = VAR_STRING;
7755 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 if (*s == '%' || *s == '#' || *s == '<')
7757 {
7758 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007759 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 --emsg_off;
7761 }
7762 else
7763 {
7764 /* When the optional second argument is non-zero, don't remove matches
7765 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007766 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 flags |= WILD_KEEP_ALL;
7768 ExpandInit(&xpc);
7769 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007770 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 ExpandCleanup(&xpc);
7772 }
7773}
7774
7775/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007776 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00007777 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007778 */
7779 static void
7780f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 typval_T *argvars;
7782 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007783{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007784 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007785 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007786 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007787 list_T *l1, *l2;
7788 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007789 long before;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007790
Bram Moolenaare9a41262005-01-15 22:18:47 +00007791 l1 = argvars[0].vval.v_list;
7792 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007793 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
7794 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007795 {
7796 if (argvars[2].v_type != VAR_UNKNOWN)
7797 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007798 before = get_tv_number(&argvars[2]);
7799 if (before == l1->lv_len)
7800 item = NULL;
7801 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007802 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007803 item = list_find(l1, before);
7804 if (item == NULL)
7805 {
7806 EMSGN(_(e_listidx), before);
7807 return;
7808 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007809 }
7810 }
7811 else
7812 item = NULL;
7813 list_extend(l1, l2, item);
7814
7815 ++l1->lv_refcount;
7816 copy_tv(&argvars[0], rettv);
7817 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007818 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007819 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
7820 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007821 dict_T *d1, *d2;
7822 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007823 char_u *action;
7824 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007825 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007826 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007827
7828 d1 = argvars[0].vval.v_dict;
7829 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007830 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
7831 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007832 {
7833 /* Check the third argument. */
7834 if (argvars[2].v_type != VAR_UNKNOWN)
7835 {
7836 static char *(av[]) = {"keep", "force", "error"};
7837
7838 action = get_tv_string(&argvars[2]);
7839 for (i = 0; i < 3; ++i)
7840 if (STRCMP(action, av[i]) == 0)
7841 break;
7842 if (i == 3)
7843 {
7844 EMSGN(_(e_invarg2), action);
7845 return;
7846 }
7847 }
7848 else
7849 action = (char_u *)"force";
7850
7851 /* Go over all entries in the second dict and add them to the
7852 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007853 todo = d2->dv_hashtab.ht_used;
7854 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007855 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007856 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007857 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007858 --todo;
7859 di1 = dict_find(d1, hi2->hi_key, -1);
7860 if (di1 == NULL)
7861 {
7862 di1 = dictitem_copy(HI2DI(hi2));
7863 if (di1 != NULL && dict_add(d1, di1) == FAIL)
7864 dictitem_free(di1);
7865 }
7866 else if (*action == 'e')
7867 {
7868 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
7869 break;
7870 }
7871 else if (*action == 'f')
7872 {
7873 clear_tv(&di1->di_tv);
7874 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
7875 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007876 }
7877 }
7878
7879 ++d1->dv_refcount;
7880 copy_tv(&argvars[0], rettv);
7881 }
7882 }
7883 else
7884 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007885}
7886
7887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 * "filereadable()" function
7889 */
7890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007891f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007892 typval_T *argvars;
7893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
7895 FILE *fd;
7896 char_u *p;
7897 int n;
7898
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007899 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
7901 {
7902 n = TRUE;
7903 fclose(fd);
7904 }
7905 else
7906 n = FALSE;
7907
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007908 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909}
7910
7911/*
7912 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
7913 * rights to write into.
7914 */
7915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007916f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007917 typval_T *argvars;
7918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919{
7920 char_u *p;
7921 int retval = 0;
7922#if defined(UNIX) || defined(VMS)
7923 int perm = 0;
7924#endif
7925
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007926 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927#if defined(UNIX) || defined(VMS)
7928 perm = mch_getperm(p);
7929#endif
7930#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
7931 if (
7932# ifdef WIN3264
7933 mch_writable(p) &&
7934# else
7935# if defined(UNIX) || defined(VMS)
7936 (perm & 0222) &&
7937# endif
7938# endif
7939 mch_access((char *)p, W_OK) == 0
7940 )
7941#endif
7942 {
7943 ++retval;
7944 if (mch_isdir(p))
7945 ++retval;
7946 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007947 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948}
7949
Bram Moolenaar33570922005-01-25 22:26:29 +00007950static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007951
7952 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007953findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00007954 typval_T *argvars;
7955 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007956 int dir;
7957{
7958#ifdef FEAT_SEARCHPATH
7959 char_u *fname;
7960 char_u *fresult = NULL;
7961 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
7962 char_u *p;
7963 char_u pathbuf[NUMBUFLEN];
7964 int count = 1;
7965 int first = TRUE;
7966
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007967 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007968
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007969 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007970 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007971 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007972 if (*p != NUL)
7973 path = p;
7974
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007975 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007976 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007977 }
7978
7979 do
7980 {
7981 vim_free(fresult);
7982 fresult = find_file_in_path_option(first ? fname : NULL,
7983 first ? (int)STRLEN(fname) : 0,
7984 0, first, path, dir, NULL);
7985 first = FALSE;
7986 } while (--count > 0 && fresult != NULL);
7987
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007988 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007989#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007990 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007991#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007992 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007993}
7994
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007995static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
7996static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00007997static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
7998static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007999
8000/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008001 * Prepare v: variable "idx" to be used.
8002 * Save the current typeval in "save_tv".
8003 * When not used yet add the variable to the v: hashtable.
8004 */
8005 static void
8006prepare_vimvar(idx, save_tv)
8007 int idx;
8008 typval_T *save_tv;
8009{
8010 *save_tv = vimvars[idx].vv_tv;
8011 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8012 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
8013}
8014
8015/*
8016 * Restore v: variable "idx" to typeval "save_tv".
8017 * When no longer defined, remove the variable from the v: hashtable.
8018 */
8019 static void
8020restore_vimvar(idx, save_tv)
8021 int idx;
8022 typval_T *save_tv;
8023{
8024 hashitem_T *hi;
8025
8026 clear_tv(&vimvars[idx].vv_tv);
8027 vimvars[idx].vv_tv = *save_tv;
8028 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8029 {
8030 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
8031 if (HASHITEM_EMPTY(hi))
8032 EMSG2(_(e_intern2), "restore_vimvar()");
8033 else
8034 hash_remove(&vimvarht, hi);
8035 }
8036}
8037
8038/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008039 * Implementation of map() and filter().
8040 */
8041 static void
8042filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008043 typval_T *argvars;
8044 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008045 int map;
8046{
8047 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008048 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008049 listitem_T *li, *nli;
8050 list_T *l = NULL;
8051 dictitem_T *di;
8052 hashtab_T *ht;
8053 hashitem_T *hi;
8054 dict_T *d = NULL;
8055 typval_T save_val;
8056 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008057 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008058 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008059 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8060
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008061
8062 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008063 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008064 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008065 if ((l = argvars[0].vval.v_list) == NULL
8066 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008067 return;
8068 }
8069 else if (argvars[0].v_type == VAR_DICT)
8070 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008071 if ((d = argvars[0].vval.v_dict) == NULL
8072 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008073 return;
8074 }
8075 else
8076 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008077 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008078 return;
8079 }
8080
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008081 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008082 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaare9a41262005-01-15 22:18:47 +00008083
8084 if (argvars[0].v_type == VAR_DICT)
8085 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008086 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar33570922005-01-25 22:26:29 +00008087 vimvars[VV_KEY].vv_type = VAR_STRING;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008088
Bram Moolenaar33570922005-01-25 22:26:29 +00008089 ht = &d->dv_hashtab;
Bram Moolenaara7043832005-01-21 11:56:39 +00008090 hash_lock(ht);
8091 todo = ht->ht_used;
8092 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008093 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008094 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008095 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008096 --todo;
8097 di = HI2DI(hi);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008098 if (tv_check_lock(di->di_tv.v_lock, msg))
8099 break;
Bram Moolenaar33570922005-01-25 22:26:29 +00008100 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008101 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8102 break;
8103 if (!map && rem)
8104 dictitem_remove(d, di);
Bram Moolenaar33570922005-01-25 22:26:29 +00008105 clear_tv(&vimvars[VV_KEY].vv_tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008106 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008107 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008108 hash_unlock(ht);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008109
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008110 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008111 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008112 else
8113 {
8114 for (li = l->lv_first; li != NULL; li = nli)
8115 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008116 if (tv_check_lock(li->li_tv.v_lock, msg))
8117 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008118 nli = li->li_next;
8119 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
8120 break;
8121 if (!map && rem)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00008122 listitem_remove(l, li);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008123 }
8124 }
8125
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008126 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008127
8128 copy_tv(&argvars[0], rettv);
8129}
8130
8131 static int
8132filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008133 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008134 char_u *expr;
8135 int map;
8136 int *remp;
8137{
Bram Moolenaar33570922005-01-25 22:26:29 +00008138 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008139 char_u *s;
8140
Bram Moolenaar33570922005-01-25 22:26:29 +00008141 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008142 s = expr;
8143 if (eval1(&s, &rettv, TRUE) == FAIL)
8144 return FAIL;
8145 if (*s != NUL) /* check for trailing chars after expr */
8146 {
8147 EMSG2(_(e_invexpr2), s);
8148 return FAIL;
8149 }
8150 if (map)
8151 {
8152 /* map(): replace the list item value */
8153 clear_tv(tv);
8154 *tv = rettv;
8155 }
8156 else
8157 {
8158 /* filter(): when expr is zero remove the item */
8159 *remp = (get_tv_number(&rettv) == 0);
8160 clear_tv(&rettv);
8161 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008162 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008163 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008164}
8165
8166/*
8167 * "filter()" function
8168 */
8169 static void
8170f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008171 typval_T *argvars;
8172 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008173{
8174 filter_map(argvars, rettv, FALSE);
8175}
8176
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008177/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008178 * "finddir({fname}[, {path}[, {count}]])" function
8179 */
8180 static void
8181f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008182 typval_T *argvars;
8183 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008184{
8185 findfilendir(argvars, rettv, TRUE);
8186}
8187
8188/*
8189 * "findfile({fname}[, {path}[, {count}]])" function
8190 */
8191 static void
8192f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008193 typval_T *argvars;
8194 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008195{
8196 findfilendir(argvars, rettv, FALSE);
8197}
8198
8199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 * "fnamemodify({fname}, {mods})" function
8201 */
8202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008203f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008204 typval_T *argvars;
8205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206{
8207 char_u *fname;
8208 char_u *mods;
8209 int usedlen = 0;
8210 int len;
8211 char_u *fbuf = NULL;
8212 char_u buf[NUMBUFLEN];
8213
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008214 fname = get_tv_string(&argvars[0]);
8215 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 len = (int)STRLEN(fname);
8217
8218 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8219
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008220 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008222 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008224 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 vim_free(fbuf);
8226}
8227
Bram Moolenaar33570922005-01-25 22:26:29 +00008228static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229
8230/*
8231 * "foldclosed()" function
8232 */
8233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008234foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008235 typval_T *argvars;
8236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 int end;
8238{
8239#ifdef FEAT_FOLDING
8240 linenr_T lnum;
8241 linenr_T first, last;
8242
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008243 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8245 {
8246 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8247 {
8248 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008249 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008251 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 return;
8253 }
8254 }
8255#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008256 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257}
8258
8259/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008260 * "foldclosed()" function
8261 */
8262 static void
8263f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008264 typval_T *argvars;
8265 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008266{
8267 foldclosed_both(argvars, rettv, FALSE);
8268}
8269
8270/*
8271 * "foldclosedend()" function
8272 */
8273 static void
8274f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008275 typval_T *argvars;
8276 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008277{
8278 foldclosed_both(argvars, rettv, TRUE);
8279}
8280
8281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 * "foldlevel()" function
8283 */
8284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008285f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008286 typval_T *argvars;
8287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288{
8289#ifdef FEAT_FOLDING
8290 linenr_T lnum;
8291
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008292 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008294 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 else
8296#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008297 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298}
8299
8300/*
8301 * "foldtext()" function
8302 */
8303/*ARGSUSED*/
8304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008305f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008306 typval_T *argvars;
8307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308{
8309#ifdef FEAT_FOLDING
8310 linenr_T lnum;
8311 char_u *s;
8312 char_u *r;
8313 int len;
8314 char *txt;
8315#endif
8316
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008317 rettv->v_type = VAR_STRING;
8318 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008320 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8321 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8322 <= curbuf->b_ml.ml_line_count
8323 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 {
8325 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008326 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8327 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {
8329 if (!linewhite(lnum))
8330 break;
8331 ++lnum;
8332 }
8333
8334 /* Find interesting text in this line. */
8335 s = skipwhite(ml_get(lnum));
8336 /* skip C comment-start */
8337 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008338 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008340 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008341 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008342 {
8343 s = skipwhite(ml_get(lnum + 1));
8344 if (*s == '*')
8345 s = skipwhite(s + 1);
8346 }
8347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 txt = _("+-%s%3ld lines: ");
8349 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008350 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 + 20 /* for %3ld */
8352 + STRLEN(s))); /* concatenated */
8353 if (r != NULL)
8354 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008355 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8356 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8357 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 len = (int)STRLEN(r);
8359 STRCAT(r, s);
8360 /* remove 'foldmarker' and 'commentstring' */
8361 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008362 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 }
8364 }
8365#endif
8366}
8367
8368/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008369 * "foldtextresult(lnum)" function
8370 */
8371/*ARGSUSED*/
8372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008373f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008374 typval_T *argvars;
8375 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008376{
8377#ifdef FEAT_FOLDING
8378 linenr_T lnum;
8379 char_u *text;
8380 char_u buf[51];
8381 foldinfo_T foldinfo;
8382 int fold_count;
8383#endif
8384
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008385 rettv->v_type = VAR_STRING;
8386 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008387#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008388 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008389 fold_count = foldedCount(curwin, lnum, &foldinfo);
8390 if (fold_count > 0)
8391 {
8392 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8393 &foldinfo, buf);
8394 if (text == buf)
8395 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008396 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008397 }
8398#endif
8399}
8400
8401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 * "foreground()" function
8403 */
8404/*ARGSUSED*/
8405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008406f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008407 typval_T *argvars;
8408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008410 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411#ifdef FEAT_GUI
8412 if (gui.in_use)
8413 gui_mch_set_foreground();
8414#else
8415# ifdef WIN32
8416 win32_set_foreground();
8417# endif
8418#endif
8419}
8420
8421/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008422 * "function()" function
8423 */
8424/*ARGSUSED*/
8425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008426f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008427 typval_T *argvars;
8428 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008429{
8430 char_u *s;
8431
Bram Moolenaara7043832005-01-21 11:56:39 +00008432 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008433 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008434 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008435 EMSG2(_(e_invarg2), s);
8436 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008437 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008438 else
8439 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008440 rettv->vval.v_string = vim_strsave(s);
8441 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008442 }
8443}
8444
8445/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008446 * "get()" function
8447 */
8448 static void
8449f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008450 typval_T *argvars;
8451 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008452{
Bram Moolenaar33570922005-01-25 22:26:29 +00008453 listitem_T *li;
8454 list_T *l;
8455 dictitem_T *di;
8456 dict_T *d;
8457 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008458
Bram Moolenaare9a41262005-01-15 22:18:47 +00008459 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008460 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008461 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008462 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008463 li = list_find(l, get_tv_number(&argvars[1]));
8464 if (li != NULL)
8465 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008466 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008467 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008468 else if (argvars[0].v_type == VAR_DICT)
8469 {
8470 if ((d = argvars[0].vval.v_dict) != NULL)
8471 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008472 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008473 if (di != NULL)
8474 tv = &di->di_tv;
8475 }
8476 }
8477 else
8478 EMSG2(_(e_listdictarg), "get()");
8479
8480 if (tv == NULL)
8481 {
8482 if (argvars[2].v_type == VAR_UNKNOWN)
8483 rettv->vval.v_number = 0;
8484 else
8485 copy_tv(&argvars[2], rettv);
8486 }
8487 else
8488 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008489}
8490
8491/*
8492 * "getbufvar()" function
8493 */
8494 static void
8495f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008496 typval_T *argvars;
8497 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008498{
8499 buf_T *buf;
8500 buf_T *save_curbuf;
8501 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008502 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008503
8504 ++emsg_off;
8505 buf = get_buf_tv(&argvars[0]);
8506 varname = get_tv_string(&argvars[1]);
8507
8508 rettv->v_type = VAR_STRING;
8509 rettv->vval.v_string = NULL;
8510
8511 if (buf != NULL && varname != NULL)
8512 {
8513 if (*varname == '&') /* buffer-local-option */
8514 {
8515 /* set curbuf to be our buf, temporarily */
8516 save_curbuf = curbuf;
8517 curbuf = buf;
8518
8519 get_option_tv(&varname, rettv, TRUE);
8520
8521 /* restore previous notion of curbuf */
8522 curbuf = save_curbuf;
8523 }
8524 else
8525 {
8526 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008527 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008528 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008529 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008530 }
8531 }
8532
8533 --emsg_off;
8534}
8535
8536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 * "getchar()" function
8538 */
8539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008540f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008541 typval_T *argvars;
8542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
8544 varnumber_T n;
8545
8546 ++no_mapping;
8547 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008548 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 /* getchar(): blocking wait. */
8550 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008551 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 /* getchar(1): only check if char avail */
8553 n = vpeekc();
8554 else if (vpeekc() == NUL)
8555 /* getchar(0) and no char avail: return zero */
8556 n = 0;
8557 else
8558 /* getchar(0) and char avail: return char */
8559 n = safe_vgetc();
8560 --no_mapping;
8561 --allow_keys;
8562
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008563 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 if (IS_SPECIAL(n) || mod_mask != 0)
8565 {
8566 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
8567 int i = 0;
8568
8569 /* Turn a special key into three bytes, plus modifier. */
8570 if (mod_mask != 0)
8571 {
8572 temp[i++] = K_SPECIAL;
8573 temp[i++] = KS_MODIFIER;
8574 temp[i++] = mod_mask;
8575 }
8576 if (IS_SPECIAL(n))
8577 {
8578 temp[i++] = K_SPECIAL;
8579 temp[i++] = K_SECOND(n);
8580 temp[i++] = K_THIRD(n);
8581 }
8582#ifdef FEAT_MBYTE
8583 else if (has_mbyte)
8584 i += (*mb_char2bytes)(n, temp + i);
8585#endif
8586 else
8587 temp[i++] = n;
8588 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008589 rettv->v_type = VAR_STRING;
8590 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 }
8592}
8593
8594/*
8595 * "getcharmod()" function
8596 */
8597/*ARGSUSED*/
8598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008599f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008600 typval_T *argvars;
8601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008603 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604}
8605
8606/*
8607 * "getcmdline()" function
8608 */
8609/*ARGSUSED*/
8610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008611f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008612 typval_T *argvars;
8613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008615 rettv->v_type = VAR_STRING;
8616 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617}
8618
8619/*
8620 * "getcmdpos()" function
8621 */
8622/*ARGSUSED*/
8623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008624f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008625 typval_T *argvars;
8626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008628 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629}
8630
8631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 * "getcwd()" function
8633 */
8634/*ARGSUSED*/
8635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008636f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008637 typval_T *argvars;
8638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639{
8640 char_u cwd[MAXPATHL];
8641
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008642 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008644 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 else
8646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008647 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650#endif
8651 }
8652}
8653
8654/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008655 * "getfontname()" function
8656 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008657/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008659f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008660 typval_T *argvars;
8661 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008662{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008663 rettv->v_type = VAR_STRING;
8664 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008665#ifdef FEAT_GUI
8666 if (gui.in_use)
8667 {
8668 GuiFont font;
8669 char_u *name = NULL;
8670
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008671 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008672 {
8673 /* Get the "Normal" font. Either the name saved by
8674 * hl_set_font_name() or from the font ID. */
8675 font = gui.norm_font;
8676 name = hl_get_font_name();
8677 }
8678 else
8679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008680 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008681 if (STRCMP(name, "*") == 0) /* don't use font dialog */
8682 return;
8683 font = gui_mch_get_font(name, FALSE);
8684 if (font == NOFONT)
8685 return; /* Invalid font name, return empty string. */
8686 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008687 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008688 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008689 gui_mch_free_font(font);
8690 }
8691#endif
8692}
8693
8694/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008695 * "getfperm({fname})" function
8696 */
8697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008698f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008699 typval_T *argvars;
8700 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008701{
8702 char_u *fname;
8703 struct stat st;
8704 char_u *perm = NULL;
8705 char_u flags[] = "rwx";
8706 int i;
8707
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008708 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008709
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008710 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008711 if (mch_stat((char *)fname, &st) >= 0)
8712 {
8713 perm = vim_strsave((char_u *)"---------");
8714 if (perm != NULL)
8715 {
8716 for (i = 0; i < 9; i++)
8717 {
8718 if (st.st_mode & (1 << (8 - i)))
8719 perm[i] = flags[i % 3];
8720 }
8721 }
8722 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008723 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008724}
8725
8726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 * "getfsize({fname})" function
8728 */
8729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008730f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008731 typval_T *argvars;
8732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733{
8734 char_u *fname;
8735 struct stat st;
8736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008739 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740
8741 if (mch_stat((char *)fname, &st) >= 0)
8742 {
8743 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008744 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008746 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 }
8748 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008749 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750}
8751
8752/*
8753 * "getftime({fname})" function
8754 */
8755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008756f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008757 typval_T *argvars;
8758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759{
8760 char_u *fname;
8761 struct stat st;
8762
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008763 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764
8765 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008766 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008768 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769}
8770
8771/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008772 * "getftype({fname})" function
8773 */
8774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008775f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008776 typval_T *argvars;
8777 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008778{
8779 char_u *fname;
8780 struct stat st;
8781 char_u *type = NULL;
8782 char *t;
8783
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008784 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008785
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008786 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008787 if (mch_lstat((char *)fname, &st) >= 0)
8788 {
8789#ifdef S_ISREG
8790 if (S_ISREG(st.st_mode))
8791 t = "file";
8792 else if (S_ISDIR(st.st_mode))
8793 t = "dir";
8794# ifdef S_ISLNK
8795 else if (S_ISLNK(st.st_mode))
8796 t = "link";
8797# endif
8798# ifdef S_ISBLK
8799 else if (S_ISBLK(st.st_mode))
8800 t = "bdev";
8801# endif
8802# ifdef S_ISCHR
8803 else if (S_ISCHR(st.st_mode))
8804 t = "cdev";
8805# endif
8806# ifdef S_ISFIFO
8807 else if (S_ISFIFO(st.st_mode))
8808 t = "fifo";
8809# endif
8810# ifdef S_ISSOCK
8811 else if (S_ISSOCK(st.st_mode))
8812 t = "fifo";
8813# endif
8814 else
8815 t = "other";
8816#else
8817# ifdef S_IFMT
8818 switch (st.st_mode & S_IFMT)
8819 {
8820 case S_IFREG: t = "file"; break;
8821 case S_IFDIR: t = "dir"; break;
8822# ifdef S_IFLNK
8823 case S_IFLNK: t = "link"; break;
8824# endif
8825# ifdef S_IFBLK
8826 case S_IFBLK: t = "bdev"; break;
8827# endif
8828# ifdef S_IFCHR
8829 case S_IFCHR: t = "cdev"; break;
8830# endif
8831# ifdef S_IFIFO
8832 case S_IFIFO: t = "fifo"; break;
8833# endif
8834# ifdef S_IFSOCK
8835 case S_IFSOCK: t = "socket"; break;
8836# endif
8837 default: t = "other";
8838 }
8839# else
8840 if (mch_isdir(fname))
8841 t = "dir";
8842 else
8843 t = "file";
8844# endif
8845#endif
8846 type = vim_strsave((char_u *)t);
8847 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008849}
8850
8851/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008852 * "getline(lnum)" function
8853 */
8854 static void
8855f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008856 typval_T *argvars;
8857 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008858{
8859 linenr_T lnum;
8860 linenr_T end;
8861 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00008862 list_T *l;
8863 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008864
8865 lnum = get_tv_lnum(argvars);
8866
8867 if (argvars[1].v_type == VAR_UNKNOWN)
8868 {
8869 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8870 p = ml_get(lnum);
8871 else
8872 p = (char_u *)"";
8873
8874 rettv->v_type = VAR_STRING;
8875 rettv->vval.v_string = vim_strsave(p);
8876 }
8877 else
8878 {
8879 end = get_tv_lnum(&argvars[1]);
8880 if (end < lnum)
8881 {
8882 EMSG(_(e_invrange));
8883 rettv->vval.v_number = 0;
8884 }
8885 else
8886 {
8887 l = list_alloc();
8888 if (l != NULL)
8889 {
8890 if (lnum < 1)
8891 lnum = 1;
8892 if (end > curbuf->b_ml.ml_line_count)
8893 end = curbuf->b_ml.ml_line_count;
8894 while (lnum <= end)
8895 {
8896 li = listitem_alloc();
8897 if (li == NULL)
8898 break;
8899 list_append(l, li);
8900 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008901 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008902 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
8903 }
8904 rettv->vval.v_list = l;
8905 rettv->v_type = VAR_LIST;
8906 ++l->lv_refcount;
8907 }
8908 }
8909 }
8910}
8911
8912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 * "getreg()" function
8914 */
8915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008916f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008917 typval_T *argvars;
8918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919{
8920 char_u *strregname;
8921 int regname;
8922
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008923 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008926 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 regname = (strregname == NULL ? '"' : *strregname);
8928 if (regname == 0)
8929 regname = '"';
8930
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008931 rettv->v_type = VAR_STRING;
8932 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933}
8934
8935/*
8936 * "getregtype()" function
8937 */
8938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008939f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008940 typval_T *argvars;
8941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942{
8943 char_u *strregname;
8944 int regname;
8945 char_u buf[NUMBUFLEN + 2];
8946 long reglen = 0;
8947
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008948 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 else
8951 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008952 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
8954 regname = (strregname == NULL ? '"' : *strregname);
8955 if (regname == 0)
8956 regname = '"';
8957
8958 buf[0] = NUL;
8959 buf[1] = NUL;
8960 switch (get_reg_type(regname, &reglen))
8961 {
8962 case MLINE: buf[0] = 'V'; break;
8963 case MCHAR: buf[0] = 'v'; break;
8964#ifdef FEAT_VISUAL
8965 case MBLOCK:
8966 buf[0] = Ctrl_V;
8967 sprintf((char *)buf + 1, "%ld", reglen + 1);
8968 break;
8969#endif
8970 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008971 rettv->v_type = VAR_STRING;
8972 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973}
8974
8975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 * "getwinposx()" function
8977 */
8978/*ARGSUSED*/
8979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008981 typval_T *argvars;
8982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008984 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985#ifdef FEAT_GUI
8986 if (gui.in_use)
8987 {
8988 int x, y;
8989
8990 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008991 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 }
8993#endif
8994}
8995
8996/*
8997 * "getwinposy()" function
8998 */
8999/*ARGSUSED*/
9000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009001f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009002 typval_T *argvars;
9003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009005 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006#ifdef FEAT_GUI
9007 if (gui.in_use)
9008 {
9009 int x, y;
9010
9011 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 }
9014#endif
9015}
9016
9017/*
9018 * "getwinvar()" function
9019 */
9020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009021f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009022 typval_T *argvars;
9023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024{
9025 win_T *win, *oldcurwin;
9026 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009027 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028
9029 ++emsg_off;
9030 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009031 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009033 rettv->v_type = VAR_STRING;
9034 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035
9036 if (win != NULL && varname != NULL)
9037 {
9038 if (*varname == '&') /* window-local-option */
9039 {
9040 /* set curwin to be our win, temporarily */
9041 oldcurwin = curwin;
9042 curwin = win;
9043
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009044 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045
9046 /* restore previous notion of curwin */
9047 curwin = oldcurwin;
9048 }
9049 else
9050 {
9051 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009052 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009054 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 }
9056 }
9057
9058 --emsg_off;
9059}
9060
9061/*
9062 * "glob()" function
9063 */
9064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009065f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009066 typval_T *argvars;
9067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068{
9069 expand_T xpc;
9070
9071 ExpandInit(&xpc);
9072 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009073 rettv->v_type = VAR_STRING;
9074 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9076 ExpandCleanup(&xpc);
9077}
9078
9079/*
9080 * "globpath()" function
9081 */
9082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009083f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009084 typval_T *argvars;
9085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086{
9087 char_u buf1[NUMBUFLEN];
9088
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 rettv->v_type = VAR_STRING;
9090 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
9091 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092}
9093
9094/*
9095 * "has()" function
9096 */
9097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *argvars;
9100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
9102 int i;
9103 char_u *name;
9104 int n = FALSE;
9105 static char *(has_list[]) =
9106 {
9107#ifdef AMIGA
9108 "amiga",
9109# ifdef FEAT_ARP
9110 "arp",
9111# endif
9112#endif
9113#ifdef __BEOS__
9114 "beos",
9115#endif
9116#ifdef MSDOS
9117# ifdef DJGPP
9118 "dos32",
9119# else
9120 "dos16",
9121# endif
9122#endif
9123#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9124 "mac",
9125#endif
9126#if defined(MACOS_X_UNIX)
9127 "macunix",
9128#endif
9129#ifdef OS2
9130 "os2",
9131#endif
9132#ifdef __QNX__
9133 "qnx",
9134#endif
9135#ifdef RISCOS
9136 "riscos",
9137#endif
9138#ifdef UNIX
9139 "unix",
9140#endif
9141#ifdef VMS
9142 "vms",
9143#endif
9144#ifdef WIN16
9145 "win16",
9146#endif
9147#ifdef WIN32
9148 "win32",
9149#endif
9150#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9151 "win32unix",
9152#endif
9153#ifdef WIN64
9154 "win64",
9155#endif
9156#ifdef EBCDIC
9157 "ebcdic",
9158#endif
9159#ifndef CASE_INSENSITIVE_FILENAME
9160 "fname_case",
9161#endif
9162#ifdef FEAT_ARABIC
9163 "arabic",
9164#endif
9165#ifdef FEAT_AUTOCMD
9166 "autocmd",
9167#endif
9168#ifdef FEAT_BEVAL
9169 "balloon_eval",
9170#endif
9171#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9172 "builtin_terms",
9173# ifdef ALL_BUILTIN_TCAPS
9174 "all_builtin_terms",
9175# endif
9176#endif
9177#ifdef FEAT_BYTEOFF
9178 "byte_offset",
9179#endif
9180#ifdef FEAT_CINDENT
9181 "cindent",
9182#endif
9183#ifdef FEAT_CLIENTSERVER
9184 "clientserver",
9185#endif
9186#ifdef FEAT_CLIPBOARD
9187 "clipboard",
9188#endif
9189#ifdef FEAT_CMDL_COMPL
9190 "cmdline_compl",
9191#endif
9192#ifdef FEAT_CMDHIST
9193 "cmdline_hist",
9194#endif
9195#ifdef FEAT_COMMENTS
9196 "comments",
9197#endif
9198#ifdef FEAT_CRYPT
9199 "cryptv",
9200#endif
9201#ifdef FEAT_CSCOPE
9202 "cscope",
9203#endif
9204#ifdef DEBUG
9205 "debug",
9206#endif
9207#ifdef FEAT_CON_DIALOG
9208 "dialog_con",
9209#endif
9210#ifdef FEAT_GUI_DIALOG
9211 "dialog_gui",
9212#endif
9213#ifdef FEAT_DIFF
9214 "diff",
9215#endif
9216#ifdef FEAT_DIGRAPHS
9217 "digraphs",
9218#endif
9219#ifdef FEAT_DND
9220 "dnd",
9221#endif
9222#ifdef FEAT_EMACS_TAGS
9223 "emacs_tags",
9224#endif
9225 "eval", /* always present, of course! */
9226#ifdef FEAT_EX_EXTRA
9227 "ex_extra",
9228#endif
9229#ifdef FEAT_SEARCH_EXTRA
9230 "extra_search",
9231#endif
9232#ifdef FEAT_FKMAP
9233 "farsi",
9234#endif
9235#ifdef FEAT_SEARCHPATH
9236 "file_in_path",
9237#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009238#if defined(UNIX) && !defined(USE_SYSTEM)
9239 "filterpipe",
9240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241#ifdef FEAT_FIND_ID
9242 "find_in_path",
9243#endif
9244#ifdef FEAT_FOLDING
9245 "folding",
9246#endif
9247#ifdef FEAT_FOOTER
9248 "footer",
9249#endif
9250#if !defined(USE_SYSTEM) && defined(UNIX)
9251 "fork",
9252#endif
9253#ifdef FEAT_GETTEXT
9254 "gettext",
9255#endif
9256#ifdef FEAT_GUI
9257 "gui",
9258#endif
9259#ifdef FEAT_GUI_ATHENA
9260# ifdef FEAT_GUI_NEXTAW
9261 "gui_neXtaw",
9262# else
9263 "gui_athena",
9264# endif
9265#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009266#ifdef FEAT_GUI_KDE
9267 "gui_kde",
9268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269#ifdef FEAT_GUI_GTK
9270 "gui_gtk",
9271# ifdef HAVE_GTK2
9272 "gui_gtk2",
9273# endif
9274#endif
9275#ifdef FEAT_GUI_MAC
9276 "gui_mac",
9277#endif
9278#ifdef FEAT_GUI_MOTIF
9279 "gui_motif",
9280#endif
9281#ifdef FEAT_GUI_PHOTON
9282 "gui_photon",
9283#endif
9284#ifdef FEAT_GUI_W16
9285 "gui_win16",
9286#endif
9287#ifdef FEAT_GUI_W32
9288 "gui_win32",
9289#endif
9290#ifdef FEAT_HANGULIN
9291 "hangul_input",
9292#endif
9293#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
9294 "iconv",
9295#endif
9296#ifdef FEAT_INS_EXPAND
9297 "insert_expand",
9298#endif
9299#ifdef FEAT_JUMPLIST
9300 "jumplist",
9301#endif
9302#ifdef FEAT_KEYMAP
9303 "keymap",
9304#endif
9305#ifdef FEAT_LANGMAP
9306 "langmap",
9307#endif
9308#ifdef FEAT_LIBCALL
9309 "libcall",
9310#endif
9311#ifdef FEAT_LINEBREAK
9312 "linebreak",
9313#endif
9314#ifdef FEAT_LISP
9315 "lispindent",
9316#endif
9317#ifdef FEAT_LISTCMDS
9318 "listcmds",
9319#endif
9320#ifdef FEAT_LOCALMAP
9321 "localmap",
9322#endif
9323#ifdef FEAT_MENU
9324 "menu",
9325#endif
9326#ifdef FEAT_SESSION
9327 "mksession",
9328#endif
9329#ifdef FEAT_MODIFY_FNAME
9330 "modify_fname",
9331#endif
9332#ifdef FEAT_MOUSE
9333 "mouse",
9334#endif
9335#ifdef FEAT_MOUSESHAPE
9336 "mouseshape",
9337#endif
9338#if defined(UNIX) || defined(VMS)
9339# ifdef FEAT_MOUSE_DEC
9340 "mouse_dec",
9341# endif
9342# ifdef FEAT_MOUSE_GPM
9343 "mouse_gpm",
9344# endif
9345# ifdef FEAT_MOUSE_JSB
9346 "mouse_jsbterm",
9347# endif
9348# ifdef FEAT_MOUSE_NET
9349 "mouse_netterm",
9350# endif
9351# ifdef FEAT_MOUSE_PTERM
9352 "mouse_pterm",
9353# endif
9354# ifdef FEAT_MOUSE_XTERM
9355 "mouse_xterm",
9356# endif
9357#endif
9358#ifdef FEAT_MBYTE
9359 "multi_byte",
9360#endif
9361#ifdef FEAT_MBYTE_IME
9362 "multi_byte_ime",
9363#endif
9364#ifdef FEAT_MULTI_LANG
9365 "multi_lang",
9366#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009367#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00009368#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009369 "mzscheme",
9370#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372#ifdef FEAT_OLE
9373 "ole",
9374#endif
9375#ifdef FEAT_OSFILETYPE
9376 "osfiletype",
9377#endif
9378#ifdef FEAT_PATH_EXTRA
9379 "path_extra",
9380#endif
9381#ifdef FEAT_PERL
9382#ifndef DYNAMIC_PERL
9383 "perl",
9384#endif
9385#endif
9386#ifdef FEAT_PYTHON
9387#ifndef DYNAMIC_PYTHON
9388 "python",
9389#endif
9390#endif
9391#ifdef FEAT_POSTSCRIPT
9392 "postscript",
9393#endif
9394#ifdef FEAT_PRINTER
9395 "printer",
9396#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00009397#ifdef FEAT_PROFILE
9398 "profile",
9399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400#ifdef FEAT_QUICKFIX
9401 "quickfix",
9402#endif
9403#ifdef FEAT_RIGHTLEFT
9404 "rightleft",
9405#endif
9406#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
9407 "ruby",
9408#endif
9409#ifdef FEAT_SCROLLBIND
9410 "scrollbind",
9411#endif
9412#ifdef FEAT_CMDL_INFO
9413 "showcmd",
9414 "cmdline_info",
9415#endif
9416#ifdef FEAT_SIGNS
9417 "signs",
9418#endif
9419#ifdef FEAT_SMARTINDENT
9420 "smartindent",
9421#endif
9422#ifdef FEAT_SNIFF
9423 "sniff",
9424#endif
9425#ifdef FEAT_STL_OPT
9426 "statusline",
9427#endif
9428#ifdef FEAT_SUN_WORKSHOP
9429 "sun_workshop",
9430#endif
9431#ifdef FEAT_NETBEANS_INTG
9432 "netbeans_intg",
9433#endif
9434#ifdef FEAT_SYN_HL
9435 "syntax",
9436#endif
9437#if defined(USE_SYSTEM) || !defined(UNIX)
9438 "system",
9439#endif
9440#ifdef FEAT_TAG_BINS
9441 "tag_binary",
9442#endif
9443#ifdef FEAT_TAG_OLDSTATIC
9444 "tag_old_static",
9445#endif
9446#ifdef FEAT_TAG_ANYWHITE
9447 "tag_any_white",
9448#endif
9449#ifdef FEAT_TCL
9450# ifndef DYNAMIC_TCL
9451 "tcl",
9452# endif
9453#endif
9454#ifdef TERMINFO
9455 "terminfo",
9456#endif
9457#ifdef FEAT_TERMRESPONSE
9458 "termresponse",
9459#endif
9460#ifdef FEAT_TEXTOBJ
9461 "textobjects",
9462#endif
9463#ifdef HAVE_TGETENT
9464 "tgetent",
9465#endif
9466#ifdef FEAT_TITLE
9467 "title",
9468#endif
9469#ifdef FEAT_TOOLBAR
9470 "toolbar",
9471#endif
9472#ifdef FEAT_USR_CMDS
9473 "user-commands", /* was accidentally included in 5.4 */
9474 "user_commands",
9475#endif
9476#ifdef FEAT_VIMINFO
9477 "viminfo",
9478#endif
9479#ifdef FEAT_VERTSPLIT
9480 "vertsplit",
9481#endif
9482#ifdef FEAT_VIRTUALEDIT
9483 "virtualedit",
9484#endif
9485#ifdef FEAT_VISUAL
9486 "visual",
9487#endif
9488#ifdef FEAT_VISUALEXTRA
9489 "visualextra",
9490#endif
9491#ifdef FEAT_VREPLACE
9492 "vreplace",
9493#endif
9494#ifdef FEAT_WILDIGN
9495 "wildignore",
9496#endif
9497#ifdef FEAT_WILDMENU
9498 "wildmenu",
9499#endif
9500#ifdef FEAT_WINDOWS
9501 "windows",
9502#endif
9503#ifdef FEAT_WAK
9504 "winaltkeys",
9505#endif
9506#ifdef FEAT_WRITEBACKUP
9507 "writebackup",
9508#endif
9509#ifdef FEAT_XIM
9510 "xim",
9511#endif
9512#ifdef FEAT_XFONTSET
9513 "xfontset",
9514#endif
9515#ifdef USE_XSMP
9516 "xsmp",
9517#endif
9518#ifdef USE_XSMP_INTERACT
9519 "xsmp_interact",
9520#endif
9521#ifdef FEAT_XCLIPBOARD
9522 "xterm_clipboard",
9523#endif
9524#ifdef FEAT_XTERM_SAVE
9525 "xterm_save",
9526#endif
9527#if defined(UNIX) && defined(FEAT_X11)
9528 "X11",
9529#endif
9530 NULL
9531 };
9532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 for (i = 0; has_list[i] != NULL; ++i)
9535 if (STRICMP(name, has_list[i]) == 0)
9536 {
9537 n = TRUE;
9538 break;
9539 }
9540
9541 if (n == FALSE)
9542 {
9543 if (STRNICMP(name, "patch", 5) == 0)
9544 n = has_patch(atoi((char *)name + 5));
9545 else if (STRICMP(name, "vim_starting") == 0)
9546 n = (starting != 0);
9547#ifdef DYNAMIC_TCL
9548 else if (STRICMP(name, "tcl") == 0)
9549 n = tcl_enabled(FALSE);
9550#endif
9551#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
9552 else if (STRICMP(name, "iconv") == 0)
9553 n = iconv_enabled(FALSE);
9554#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009555#ifdef DYNAMIC_MZSCHEME
9556 else if (STRICMP(name, "mzscheme") == 0)
9557 n = mzscheme_enabled(FALSE);
9558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559#ifdef DYNAMIC_RUBY
9560 else if (STRICMP(name, "ruby") == 0)
9561 n = ruby_enabled(FALSE);
9562#endif
9563#ifdef DYNAMIC_PYTHON
9564 else if (STRICMP(name, "python") == 0)
9565 n = python_enabled(FALSE);
9566#endif
9567#ifdef DYNAMIC_PERL
9568 else if (STRICMP(name, "perl") == 0)
9569 n = perl_enabled(FALSE);
9570#endif
9571#ifdef FEAT_GUI
9572 else if (STRICMP(name, "gui_running") == 0)
9573 n = (gui.in_use || gui.starting);
9574# ifdef FEAT_GUI_W32
9575 else if (STRICMP(name, "gui_win32s") == 0)
9576 n = gui_is_win32s();
9577# endif
9578# ifdef FEAT_BROWSE
9579 else if (STRICMP(name, "browse") == 0)
9580 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
9581# endif
9582#endif
9583#ifdef FEAT_SYN_HL
9584 else if (STRICMP(name, "syntax_items") == 0)
9585 n = syntax_present(curbuf);
9586#endif
9587#if defined(WIN3264)
9588 else if (STRICMP(name, "win95") == 0)
9589 n = mch_windows95();
9590#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00009591#ifdef FEAT_NETBEANS_INTG
9592 else if (STRICMP(name, "netbeans_enabled") == 0)
9593 n = usingNetbeans;
9594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 }
9596
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009597 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598}
9599
9600/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00009601 * "has_key()" function
9602 */
9603 static void
9604f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009605 typval_T *argvars;
9606 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009607{
9608 rettv->vval.v_number = 0;
9609 if (argvars[0].v_type != VAR_DICT)
9610 {
9611 EMSG(_(e_dictreq));
9612 return;
9613 }
9614 if (argvars[0].vval.v_dict == NULL)
9615 return;
9616
9617 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009618 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009619}
9620
9621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 * "hasmapto()" function
9623 */
9624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009626 typval_T *argvars;
9627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628{
9629 char_u *name;
9630 char_u *mode;
9631 char_u buf[NUMBUFLEN];
9632
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009633 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009634 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 mode = (char_u *)"nvo";
9636 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009637 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638
9639 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009642 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643}
9644
9645/*
9646 * "histadd()" function
9647 */
9648/*ARGSUSED*/
9649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009651 typval_T *argvars;
9652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653{
9654#ifdef FEAT_CMDHIST
9655 int histype;
9656 char_u *str;
9657 char_u buf[NUMBUFLEN];
9658#endif
9659
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 if (check_restricted() || check_secure())
9662 return;
9663#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009664 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 if (histype >= 0)
9666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009667 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 if (*str != NUL)
9669 {
9670 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 return;
9673 }
9674 }
9675#endif
9676}
9677
9678/*
9679 * "histdel()" function
9680 */
9681/*ARGSUSED*/
9682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009684 typval_T *argvars;
9685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686{
9687#ifdef FEAT_CMDHIST
9688 int n;
9689 char_u buf[NUMBUFLEN];
9690
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009691 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009693 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009694 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
9697 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 else
9699 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
9701 get_tv_string_buf(&argvars[1], buf));
9702 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705#endif
9706}
9707
9708/*
9709 * "histget()" function
9710 */
9711/*ARGSUSED*/
9712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009713f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009714 typval_T *argvars;
9715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716{
9717#ifdef FEAT_CMDHIST
9718 int type;
9719 int idx;
9720
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009721 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009722 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 idx = get_history_idx(type);
9724 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009725 idx = (int)get_tv_number(&argvars[1]);
9726 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009728 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009730 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731}
9732
9733/*
9734 * "histnr()" function
9735 */
9736/*ARGSUSED*/
9737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009739 typval_T *argvars;
9740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741{
9742 int i;
9743
9744#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009745 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 if (i >= HIST_CMD && i < HIST_COUNT)
9747 i = get_history_idx(i);
9748 else
9749#endif
9750 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009751 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752}
9753
9754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 * "highlightID(name)" function
9756 */
9757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009758f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009759 typval_T *argvars;
9760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763}
9764
9765/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009766 * "highlight_exists()" function
9767 */
9768 static void
9769f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009770 typval_T *argvars;
9771 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009772{
9773 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
9774}
9775
9776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 * "hostname()" function
9778 */
9779/*ARGSUSED*/
9780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009781f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009782 typval_T *argvars;
9783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784{
9785 char_u hostname[256];
9786
9787 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009788 rettv->v_type = VAR_STRING;
9789 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790}
9791
9792/*
9793 * iconv() function
9794 */
9795/*ARGSUSED*/
9796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009797f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009798 typval_T *argvars;
9799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800{
9801#ifdef FEAT_MBYTE
9802 char_u buf1[NUMBUFLEN];
9803 char_u buf2[NUMBUFLEN];
9804 char_u *from, *to, *str;
9805 vimconv_T vimconv;
9806#endif
9807
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009808 rettv->v_type = VAR_STRING;
9809 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810
9811#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812 str = get_tv_string(&argvars[0]);
9813 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
9814 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 vimconv.vc_type = CONV_NONE;
9816 convert_setup(&vimconv, from, to);
9817
9818 /* If the encodings are equal, no conversion needed. */
9819 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009820 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823
9824 convert_setup(&vimconv, NULL, NULL);
9825 vim_free(from);
9826 vim_free(to);
9827#endif
9828}
9829
9830/*
9831 * "indent()" function
9832 */
9833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009834f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009835 typval_T *argvars;
9836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837{
9838 linenr_T lnum;
9839
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009840 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009844 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845}
9846
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009847/*
9848 * "index()" function
9849 */
9850 static void
9851f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009852 typval_T *argvars;
9853 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009854{
Bram Moolenaar33570922005-01-25 22:26:29 +00009855 list_T *l;
9856 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009857 long idx = 0;
9858 int ic = FALSE;
9859
9860 rettv->vval.v_number = -1;
9861 if (argvars[0].v_type != VAR_LIST)
9862 {
9863 EMSG(_(e_listreq));
9864 return;
9865 }
9866 l = argvars[0].vval.v_list;
9867 if (l != NULL)
9868 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009869 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009870 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009871 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009872 /* Start at specified item. Use the cached index that list_find()
9873 * sets, so that a negative number also works. */
9874 item = list_find(l, get_tv_number(&argvars[2]));
9875 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009876 if (argvars[3].v_type != VAR_UNKNOWN)
9877 ic = get_tv_number(&argvars[3]);
9878 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009879
Bram Moolenaar758711c2005-02-02 23:11:38 +00009880 for ( ; item != NULL; item = item->li_next, ++idx)
9881 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009882 {
9883 rettv->vval.v_number = idx;
9884 break;
9885 }
9886 }
9887}
9888
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889static int inputsecret_flag = 0;
9890
9891/*
9892 * "input()" function
9893 * Also handles inputsecret() when inputsecret is set.
9894 */
9895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009896f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009897 typval_T *argvars;
9898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009900 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 char_u *p = NULL;
9902 int c;
9903 char_u buf[NUMBUFLEN];
9904 int cmd_silent_save = cmd_silent;
9905
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009906 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907
9908#ifdef NO_CONSOLE_INPUT
9909 /* While starting up, there is no place to enter text. */
9910 if (no_console_input())
9911 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009912 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 return;
9914 }
9915#endif
9916
9917 cmd_silent = FALSE; /* Want to see the prompt. */
9918 if (prompt != NULL)
9919 {
9920 /* Only the part of the message after the last NL is considered as
9921 * prompt for the command line */
9922 p = vim_strrchr(prompt, '\n');
9923 if (p == NULL)
9924 p = prompt;
9925 else
9926 {
9927 ++p;
9928 c = *p;
9929 *p = NUL;
9930 msg_start();
9931 msg_clr_eos();
9932 msg_puts_attr(prompt, echo_attr);
9933 msg_didout = FALSE;
9934 msg_starthere();
9935 *p = c;
9936 }
9937 cmdline_row = msg_row;
9938 }
9939
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009940 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009941 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009943 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
9945
9946 /* since the user typed this, no need to wait for return */
9947 need_wait_return = FALSE;
9948 msg_didout = FALSE;
9949 cmd_silent = cmd_silent_save;
9950}
9951
9952/*
9953 * "inputdialog()" function
9954 */
9955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009957 typval_T *argvars;
9958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959{
9960#if defined(FEAT_GUI_TEXTDIALOG)
9961 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
9962 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
9963 {
9964 char_u *message;
9965 char_u buf[NUMBUFLEN];
9966
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009968 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009970 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 IObuff[IOSIZE - 1] = NUL;
9972 }
9973 else
9974 IObuff[0] = NUL;
9975 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
9976 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 else
9979 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009980 if (argvars[1].v_type != VAR_UNKNOWN
9981 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982 rettv->vval.v_string = vim_strsave(
9983 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009987 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 }
9989 else
9990#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009991 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992}
9993
9994static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
9995
9996/*
9997 * "inputrestore()" function
9998 */
9999/*ARGSUSED*/
10000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010002 typval_T *argvars;
10003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004{
10005 if (ga_userinput.ga_len > 0)
10006 {
10007 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10009 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010010 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 }
10012 else if (p_verbose > 1)
10013 {
10014 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010015 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 }
10017}
10018
10019/*
10020 * "inputsave()" function
10021 */
10022/*ARGSUSED*/
10023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010024f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010025 typval_T *argvars;
10026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027{
10028 /* Add an entry to the stack of typehead storage. */
10029 if (ga_grow(&ga_userinput, 1) == OK)
10030 {
10031 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10032 + ga_userinput.ga_len);
10033 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010034 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 }
10036 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010037 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038}
10039
10040/*
10041 * "inputsecret()" function
10042 */
10043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010044f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010045 typval_T *argvars;
10046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047{
10048 ++cmdline_star;
10049 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010050 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 --cmdline_star;
10052 --inputsecret_flag;
10053}
10054
10055/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010056 * "insert()" function
10057 */
10058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010060 typval_T *argvars;
10061 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010062{
10063 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010064 listitem_T *item;
10065 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010066
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010067 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010068 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010069 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010070 else if ((l = argvars[0].vval.v_list) != NULL
10071 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010072 {
10073 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010074 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010075
Bram Moolenaar758711c2005-02-02 23:11:38 +000010076 if (before == l->lv_len)
10077 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010078 else
10079 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010080 item = list_find(l, before);
10081 if (item == NULL)
10082 {
10083 EMSGN(_(e_listidx), before);
10084 l = NULL;
10085 }
10086 }
10087 if (l != NULL)
10088 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010089 list_insert_tv(l, &argvars[1], item);
10090 ++l->lv_refcount;
10091 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010092 }
10093 }
10094}
10095
10096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 * "isdirectory()" function
10098 */
10099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010101 typval_T *argvars;
10102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010104 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105}
10106
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010107/*
10108 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
10109 * it refers to a List or Dictionary that is locked.
10110 */
10111 static int
10112tv_islocked(tv)
10113 typval_T *tv;
10114{
10115 return (tv->v_lock & VAR_LOCKED)
10116 || (tv->v_type == VAR_LIST
10117 && tv->vval.v_list != NULL
10118 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
10119 || (tv->v_type == VAR_DICT
10120 && tv->vval.v_dict != NULL
10121 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
10122}
10123
10124/*
10125 * "islocked()" function
10126 */
10127 static void
10128f_islocked(argvars, rettv)
10129 typval_T *argvars;
10130 typval_T *rettv;
10131{
10132 lval_T lv;
10133 char_u *end;
10134 dictitem_T *di;
10135
10136 rettv->vval.v_number = -1;
10137 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE);
10138 if (end != NULL && lv.ll_name != NULL)
10139 {
10140 if (*end != NUL)
10141 EMSG(_(e_trailing));
10142 else
10143 {
10144 if (lv.ll_tv == NULL)
10145 {
10146 if (check_changedtick(lv.ll_name))
10147 rettv->vval.v_number = 1; /* always locked */
10148 else
10149 {
10150 di = find_var(lv.ll_name, NULL);
10151 if (di != NULL)
10152 {
10153 /* Consider a variable locked when:
10154 * 1. the variable itself is locked
10155 * 2. the value of the variable is locked.
10156 * 3. the List or Dict value is locked.
10157 */
10158 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10159 || tv_islocked(&di->di_tv));
10160 }
10161 }
10162 }
10163 else if (lv.ll_range)
10164 EMSG(_("E745: Range not allowed"));
10165 else if (lv.ll_newkey != NULL)
10166 EMSG2(_(e_dictkey), lv.ll_newkey);
10167 else if (lv.ll_list != NULL)
10168 /* List item. */
10169 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10170 else
10171 /* Dictionary item. */
10172 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10173 }
10174 }
10175
10176 clear_lval(&lv);
10177}
10178
Bram Moolenaar33570922005-01-25 22:26:29 +000010179static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010180
10181/*
10182 * Turn a dict into a list:
10183 * "what" == 0: list of keys
10184 * "what" == 1: list of values
10185 * "what" == 2: list of items
10186 */
10187 static void
10188dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010189 typval_T *argvars;
10190 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010191 int what;
10192{
Bram Moolenaar33570922005-01-25 22:26:29 +000010193 list_T *l;
10194 list_T *l2;
10195 dictitem_T *di;
10196 hashitem_T *hi;
10197 listitem_T *li;
10198 listitem_T *li2;
10199 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010200 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010201
10202 rettv->vval.v_number = 0;
10203 if (argvars[0].v_type != VAR_DICT)
10204 {
10205 EMSG(_(e_dictreq));
10206 return;
10207 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010208 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010209 return;
10210
10211 l = list_alloc();
10212 if (l == NULL)
10213 return;
10214 rettv->v_type = VAR_LIST;
10215 rettv->vval.v_list = l;
10216 ++l->lv_refcount;
10217
Bram Moolenaar33570922005-01-25 22:26:29 +000010218 todo = d->dv_hashtab.ht_used;
10219 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010220 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010221 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010222 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010223 --todo;
10224 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010225
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010226 li = listitem_alloc();
10227 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010228 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010229 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010230
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010231 if (what == 0)
10232 {
10233 /* keys() */
10234 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010235 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010236 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10237 }
10238 else if (what == 1)
10239 {
10240 /* values() */
10241 copy_tv(&di->di_tv, &li->li_tv);
10242 }
10243 else
10244 {
10245 /* items() */
10246 l2 = list_alloc();
10247 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010248 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010249 li->li_tv.vval.v_list = l2;
10250 if (l2 == NULL)
10251 break;
10252 ++l2->lv_refcount;
10253
10254 li2 = listitem_alloc();
10255 if (li2 == NULL)
10256 break;
10257 list_append(l2, li2);
10258 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010259 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010260 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
10261
10262 li2 = listitem_alloc();
10263 if (li2 == NULL)
10264 break;
10265 list_append(l2, li2);
10266 copy_tv(&di->di_tv, &li2->li_tv);
10267 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010268 }
10269 }
10270}
10271
10272/*
10273 * "items(dict)" function
10274 */
10275 static void
10276f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010277 typval_T *argvars;
10278 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010279{
10280 dict_list(argvars, rettv, 2);
10281}
10282
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010284 * "join()" function
10285 */
10286 static void
10287f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010288 typval_T *argvars;
10289 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010290{
10291 garray_T ga;
10292 char_u *sep;
10293
10294 rettv->vval.v_number = 0;
10295 if (argvars[0].v_type != VAR_LIST)
10296 {
10297 EMSG(_(e_listreq));
10298 return;
10299 }
10300 if (argvars[0].vval.v_list == NULL)
10301 return;
10302 if (argvars[1].v_type == VAR_UNKNOWN)
10303 sep = (char_u *)" ";
10304 else
10305 sep = get_tv_string(&argvars[1]);
10306
10307 ga_init2(&ga, (int)sizeof(char), 80);
10308 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
10309 ga_append(&ga, NUL);
10310
10311 rettv->v_type = VAR_STRING;
10312 rettv->vval.v_string = (char_u *)ga.ga_data;
10313}
10314
10315/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010316 * "keys()" function
10317 */
10318 static void
10319f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010320 typval_T *argvars;
10321 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010322{
10323 dict_list(argvars, rettv, 0);
10324}
10325
10326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 * "last_buffer_nr()" function.
10328 */
10329/*ARGSUSED*/
10330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010331f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010332 typval_T *argvars;
10333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334{
10335 int n = 0;
10336 buf_T *buf;
10337
10338 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10339 if (n < buf->b_fnum)
10340 n = buf->b_fnum;
10341
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010342 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010343}
10344
10345/*
10346 * "len()" function
10347 */
10348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010349f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010350 typval_T *argvars;
10351 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010352{
10353 switch (argvars[0].v_type)
10354 {
10355 case VAR_STRING:
10356 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010357 rettv->vval.v_number = (varnumber_T)STRLEN(
10358 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010359 break;
10360 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010361 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010362 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010363 case VAR_DICT:
10364 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
10365 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010366 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010367 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010368 break;
10369 }
10370}
10371
Bram Moolenaar33570922005-01-25 22:26:29 +000010372static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010373
10374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010375libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010376 typval_T *argvars;
10377 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010378 int type;
10379{
10380#ifdef FEAT_LIBCALL
10381 char_u *string_in;
10382 char_u **string_result;
10383 int nr_result;
10384#endif
10385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010386 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010387 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010388 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010389 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010390 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010391
10392 if (check_restricted() || check_secure())
10393 return;
10394
10395#ifdef FEAT_LIBCALL
10396 /* The first two args must be strings, otherwise its meaningless */
10397 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
10398 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010399 string_in = NULL;
10400 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010401 string_in = argvars[2].vval.v_string;
10402 if (type == VAR_NUMBER)
10403 string_result = NULL;
10404 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010405 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010406 if (mch_libcall(argvars[0].vval.v_string,
10407 argvars[1].vval.v_string,
10408 string_in,
10409 argvars[2].vval.v_number,
10410 string_result,
10411 &nr_result) == OK
10412 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010413 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010414 }
10415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416}
10417
10418/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010419 * "libcall()" function
10420 */
10421 static void
10422f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010423 typval_T *argvars;
10424 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010425{
10426 libcall_common(argvars, rettv, VAR_STRING);
10427}
10428
10429/*
10430 * "libcallnr()" function
10431 */
10432 static void
10433f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010434 typval_T *argvars;
10435 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010436{
10437 libcall_common(argvars, rettv, VAR_NUMBER);
10438}
10439
10440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 * "line(string)" function
10442 */
10443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010444f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010445 typval_T *argvars;
10446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447{
10448 linenr_T lnum = 0;
10449 pos_T *fp;
10450
10451 fp = var2fpos(&argvars[0], TRUE);
10452 if (fp != NULL)
10453 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010454 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455}
10456
10457/*
10458 * "line2byte(lnum)" function
10459 */
10460/*ARGSUSED*/
10461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010462f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010463 typval_T *argvars;
10464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465{
10466#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010467 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468#else
10469 linenr_T lnum;
10470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010471 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010473 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010475 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
10476 if (rettv->vval.v_number >= 0)
10477 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478#endif
10479}
10480
10481/*
10482 * "lispindent(lnum)" function
10483 */
10484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010485f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010486 typval_T *argvars;
10487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488{
10489#ifdef FEAT_LISP
10490 pos_T pos;
10491 linenr_T lnum;
10492
10493 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010494 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10496 {
10497 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010498 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 curwin->w_cursor = pos;
10500 }
10501 else
10502#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010503 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504}
10505
10506/*
10507 * "localtime()" function
10508 */
10509/*ARGSUSED*/
10510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010511f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010512 typval_T *argvars;
10513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010515 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516}
10517
Bram Moolenaar33570922005-01-25 22:26:29 +000010518static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519
10520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010521get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000010522 typval_T *argvars;
10523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 int exact;
10525{
10526 char_u *keys;
10527 char_u *which;
10528 char_u buf[NUMBUFLEN];
10529 char_u *keys_buf = NULL;
10530 char_u *rhs;
10531 int mode;
10532 garray_T ga;
10533
10534 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010535 rettv->v_type = VAR_STRING;
10536 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010538 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 if (*keys == NUL)
10540 return;
10541
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010542 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010543 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 else
10545 which = (char_u *)"";
10546 mode = get_map_mode(&which, 0);
10547
10548 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
10549 rhs = check_map(keys, mode, exact);
10550 vim_free(keys_buf);
10551 if (rhs != NULL)
10552 {
10553 ga_init(&ga);
10554 ga.ga_itemsize = 1;
10555 ga.ga_growsize = 40;
10556
10557 while (*rhs != NUL)
10558 ga_concat(&ga, str2special(&rhs, FALSE));
10559
10560 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010561 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 }
10563}
10564
10565/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010566 * "map()" function
10567 */
10568 static void
10569f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010570 typval_T *argvars;
10571 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010572{
10573 filter_map(argvars, rettv, TRUE);
10574}
10575
10576/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010577 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 */
10579 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010580f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010581 typval_T *argvars;
10582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010584 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585}
10586
10587/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010588 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 */
10590 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010591f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010592 typval_T *argvars;
10593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010595 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596}
10597
Bram Moolenaar33570922005-01-25 22:26:29 +000010598static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599
10600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010601find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010602 typval_T *argvars;
10603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 int type;
10605{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010606 char_u *str = NULL;
10607 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 char_u *pat;
10609 regmatch_T regmatch;
10610 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010611 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 char_u *save_cpo;
10613 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010614 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010615 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010616 list_T *l = NULL;
10617 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010618 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010619 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620
10621 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10622 save_cpo = p_cpo;
10623 p_cpo = (char_u *)"";
10624
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010625 rettv->vval.v_number = -1;
10626 if (type == 3)
10627 {
10628 /* return empty list when there are no matches */
10629 if ((rettv->vval.v_list = list_alloc()) == NULL)
10630 goto theend;
10631 rettv->v_type = VAR_LIST;
10632 ++rettv->vval.v_list->lv_refcount;
10633 }
10634 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010636 rettv->v_type = VAR_STRING;
10637 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010640 if (argvars[0].v_type == VAR_LIST)
10641 {
10642 if ((l = argvars[0].vval.v_list) == NULL)
10643 goto theend;
10644 li = l->lv_first;
10645 }
10646 else
10647 expr = str = get_tv_string(&argvars[0]);
10648
10649 pat = get_tv_string_buf(&argvars[1], patbuf);
10650
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010651 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010653 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010654 if (l != NULL)
10655 {
10656 li = list_find(l, start);
10657 if (li == NULL)
10658 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000010659 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010660 }
10661 else
10662 {
10663 if (start < 0)
10664 start = 0;
10665 if (start > (long)STRLEN(str))
10666 goto theend;
10667 str += start;
10668 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010669
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010670 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010671 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 }
10673
10674 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10675 if (regmatch.regprog != NULL)
10676 {
10677 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010678
10679 while (1)
10680 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010681 if (l != NULL)
10682 {
10683 if (li == NULL)
10684 {
10685 match = FALSE;
10686 break;
10687 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010688 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010689 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010690 if (str == NULL)
10691 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010692 }
10693
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010694 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010695
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010696 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010697 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010698 if (l == NULL && !match)
10699 break;
10700
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010701 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010702 if (l != NULL)
10703 {
10704 li = li->li_next;
10705 ++idx;
10706 }
10707 else
10708 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010709#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010710 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010711#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010712 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010713#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010714 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010715 }
10716
10717 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010719 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010720 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010721 int i;
10722
10723 /* return list with matched string and submatches */
10724 for (i = 0; i < NSUBEXP; ++i)
10725 {
10726 if (regmatch.endp[i] == NULL)
10727 break;
10728 li = listitem_alloc();
10729 if (li == NULL)
10730 break;
10731 li->li_tv.v_type = VAR_STRING;
10732 li->li_tv.v_lock = 0;
10733 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
10734 (int)(regmatch.endp[i] - regmatch.startp[i]));
10735 list_append(rettv->vval.v_list, li);
10736 }
10737 }
10738 else if (type == 2)
10739 {
10740 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010741 if (l != NULL)
10742 copy_tv(&li->li_tv, rettv);
10743 else
10744 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010746 }
10747 else if (l != NULL)
10748 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 else
10750 {
10751 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 (varnumber_T)(regmatch.startp[0] - str);
10754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010755 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010757 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 }
10759 }
10760 vim_free(regmatch.regprog);
10761 }
10762
10763theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010764 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 p_cpo = save_cpo;
10766}
10767
10768/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010769 * "match()" function
10770 */
10771 static void
10772f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010773 typval_T *argvars;
10774 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010775{
10776 find_some_match(argvars, rettv, 1);
10777}
10778
10779/*
10780 * "matchend()" function
10781 */
10782 static void
10783f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010784 typval_T *argvars;
10785 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010786{
10787 find_some_match(argvars, rettv, 0);
10788}
10789
10790/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010791 * "matchlist()" function
10792 */
10793 static void
10794f_matchlist(argvars, rettv)
10795 typval_T *argvars;
10796 typval_T *rettv;
10797{
10798 find_some_match(argvars, rettv, 3);
10799}
10800
10801/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010802 * "matchstr()" function
10803 */
10804 static void
10805f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010806 typval_T *argvars;
10807 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010808{
10809 find_some_match(argvars, rettv, 2);
10810}
10811
Bram Moolenaar33570922005-01-25 22:26:29 +000010812static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010813
10814 static void
10815max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000010816 typval_T *argvars;
10817 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010818 int domax;
10819{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010820 long n = 0;
10821 long i;
10822
10823 if (argvars[0].v_type == VAR_LIST)
10824 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010825 list_T *l;
10826 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010827
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010828 l = argvars[0].vval.v_list;
10829 if (l != NULL)
10830 {
10831 li = l->lv_first;
10832 if (li != NULL)
10833 {
10834 n = get_tv_number(&li->li_tv);
10835 while (1)
10836 {
10837 li = li->li_next;
10838 if (li == NULL)
10839 break;
10840 i = get_tv_number(&li->li_tv);
10841 if (domax ? i > n : i < n)
10842 n = i;
10843 }
10844 }
10845 }
10846 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010847 else if (argvars[0].v_type == VAR_DICT)
10848 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010849 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010850 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000010851 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010852 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010853
10854 d = argvars[0].vval.v_dict;
10855 if (d != NULL)
10856 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010857 todo = d->dv_hashtab.ht_used;
10858 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010859 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010860 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010861 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010862 --todo;
10863 i = get_tv_number(&HI2DI(hi)->di_tv);
10864 if (first)
10865 {
10866 n = i;
10867 first = FALSE;
10868 }
10869 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010870 n = i;
10871 }
10872 }
10873 }
10874 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010875 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000010876 EMSG(_(e_listdictarg));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010877 rettv->vval.v_number = n;
10878}
10879
10880/*
10881 * "max()" function
10882 */
10883 static void
10884f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010885 typval_T *argvars;
10886 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010887{
10888 max_min(argvars, rettv, TRUE);
10889}
10890
10891/*
10892 * "min()" function
10893 */
10894 static void
10895f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010896 typval_T *argvars;
10897 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010898{
10899 max_min(argvars, rettv, FALSE);
10900}
10901
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010902static int mkdir_recurse __ARGS((char_u *dir, int prot));
10903
10904/*
10905 * Create the directory in which "dir" is located, and higher levels when
10906 * needed.
10907 */
10908 static int
10909mkdir_recurse(dir, prot)
10910 char_u *dir;
10911 int prot;
10912{
10913 char_u *p;
10914 char_u *updir;
10915 int r = FAIL;
10916
10917 /* Get end of directory name in "dir".
10918 * We're done when it's "/" or "c:/". */
10919 p = gettail_sep(dir);
10920 if (p <= get_past_head(dir))
10921 return OK;
10922
10923 /* If the directory exists we're done. Otherwise: create it.*/
10924 updir = vim_strnsave(dir, (int)(p - dir));
10925 if (updir == NULL)
10926 return FAIL;
10927 if (mch_isdir(updir))
10928 r = OK;
10929 else if (mkdir_recurse(updir, prot) == OK)
10930 r = vim_mkdir_emsg(updir, prot);
10931 vim_free(updir);
10932 return r;
10933}
10934
10935#ifdef vim_mkdir
10936/*
10937 * "mkdir()" function
10938 */
10939 static void
10940f_mkdir(argvars, rettv)
10941 typval_T *argvars;
10942 typval_T *rettv;
10943{
10944 char_u *dir;
10945 char_u buf[NUMBUFLEN];
10946 int prot = 0755;
10947
10948 rettv->vval.v_number = FAIL;
10949 if (check_restricted() || check_secure())
10950 return;
10951
10952 dir = get_tv_string_buf(&argvars[0], buf);
10953 if (argvars[1].v_type != VAR_UNKNOWN)
10954 {
10955 if (argvars[2].v_type != VAR_UNKNOWN)
10956 prot = get_tv_number(&argvars[2]);
10957 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
10958 mkdir_recurse(dir, prot);
10959 }
10960 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
10961}
10962#endif
10963
Bram Moolenaar0d660222005-01-07 21:51:51 +000010964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 * "mode()" function
10966 */
10967/*ARGSUSED*/
10968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010969f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010970 typval_T *argvars;
10971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972{
10973 char_u buf[2];
10974
10975#ifdef FEAT_VISUAL
10976 if (VIsual_active)
10977 {
10978 if (VIsual_select)
10979 buf[0] = VIsual_mode + 's' - 'v';
10980 else
10981 buf[0] = VIsual_mode;
10982 }
10983 else
10984#endif
10985 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
10986 buf[0] = 'r';
10987 else if (State & INSERT)
10988 {
10989 if (State & REPLACE_FLAG)
10990 buf[0] = 'R';
10991 else
10992 buf[0] = 'i';
10993 }
10994 else if (State & CMDLINE)
10995 buf[0] = 'c';
10996 else
10997 buf[0] = 'n';
10998
10999 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011000 rettv->vval.v_string = vim_strsave(buf);
11001 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002}
11003
11004/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011005 * "nextnonblank()" function
11006 */
11007 static void
11008f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011009 typval_T *argvars;
11010 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011011{
11012 linenr_T lnum;
11013
11014 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11015 {
11016 if (lnum > curbuf->b_ml.ml_line_count)
11017 {
11018 lnum = 0;
11019 break;
11020 }
11021 if (*skipwhite(ml_get(lnum)) != NUL)
11022 break;
11023 }
11024 rettv->vval.v_number = lnum;
11025}
11026
11027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 * "nr2char()" function
11029 */
11030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011031f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011032 typval_T *argvars;
11033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034{
11035 char_u buf[NUMBUFLEN];
11036
11037#ifdef FEAT_MBYTE
11038 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 else
11041#endif
11042 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011043 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 buf[1] = NUL;
11045 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046 rettv->v_type = VAR_STRING;
11047 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011048}
11049
11050/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011051 * "prevnonblank()" function
11052 */
11053 static void
11054f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011055 typval_T *argvars;
11056 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011057{
11058 linenr_T lnum;
11059
11060 lnum = get_tv_lnum(argvars);
11061 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11062 lnum = 0;
11063 else
11064 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11065 --lnum;
11066 rettv->vval.v_number = lnum;
11067}
11068
Bram Moolenaar8c711452005-01-14 21:53:12 +000011069/*
11070 * "range()" function
11071 */
11072 static void
11073f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011074 typval_T *argvars;
11075 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011076{
11077 long start;
11078 long end;
11079 long stride = 1;
11080 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011081 list_T *l;
11082 listitem_T *li;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011083
11084 start = get_tv_number(&argvars[0]);
11085 if (argvars[1].v_type == VAR_UNKNOWN)
11086 {
11087 end = start - 1;
11088 start = 0;
11089 }
11090 else
11091 {
11092 end = get_tv_number(&argvars[1]);
11093 if (argvars[2].v_type != VAR_UNKNOWN)
11094 stride = get_tv_number(&argvars[2]);
11095 }
11096
11097 rettv->vval.v_number = 0;
11098 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011099 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011100 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011101 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011102 else
11103 {
11104 l = list_alloc();
11105 if (l != NULL)
11106 {
11107 rettv->v_type = VAR_LIST;
11108 rettv->vval.v_list = l;
11109 ++l->lv_refcount;
11110
11111 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11112 {
11113 li = listitem_alloc();
11114 if (li == NULL)
11115 break;
11116 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011117 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011118 li->li_tv.vval.v_number = i;
11119 list_append(l, li);
11120 }
11121 }
11122 }
11123}
11124
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011125/*
11126 * "readfile()" function
11127 */
11128 static void
11129f_readfile(argvars, rettv)
11130 typval_T *argvars;
11131 typval_T *rettv;
11132{
11133 int binary = FALSE;
11134 char_u *fname;
11135 FILE *fd;
11136 list_T *l;
11137 listitem_T *li;
11138#define FREAD_SIZE 200 /* optimized for text lines */
11139 char_u buf[FREAD_SIZE];
11140 int readlen; /* size of last fread() */
11141 int buflen; /* nr of valid chars in buf[] */
11142 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11143 int tolist; /* first byte in buf[] still to be put in list */
11144 int chop; /* how many CR to chop off */
11145 char_u *prev = NULL; /* previously read bytes, if any */
11146 int prevlen = 0; /* length of "prev" if not NULL */
11147 char_u *s;
11148 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011149 long maxline = MAXLNUM;
11150 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011151
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011152 if (argvars[1].v_type != VAR_UNKNOWN)
11153 {
11154 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11155 binary = TRUE;
11156 if (argvars[2].v_type != VAR_UNKNOWN)
11157 maxline = get_tv_number(&argvars[2]);
11158 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011159
11160 l = list_alloc();
11161 if (l == NULL)
11162 return;
11163 rettv->v_type = VAR_LIST;
11164 rettv->vval.v_list = l;
11165 l->lv_refcount = 1;
11166
11167 /* Always open the file in binary mode, library functions have a mind of
11168 * their own about CR-LF conversion. */
11169 fname = get_tv_string(&argvars[0]);
11170 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11171 {
11172 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11173 return;
11174 }
11175
11176 filtd = 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011177 while (cnt < maxline)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011178 {
11179 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11180 buflen = filtd + readlen;
11181 tolist = 0;
11182 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11183 {
11184 if (buf[filtd] == '\n' || readlen <= 0)
11185 {
11186 /* Only when in binary mode add an empty list item when the
11187 * last line ends in a '\n'. */
11188 if (!binary && readlen == 0 && filtd == 0)
11189 break;
11190
11191 /* Found end-of-line or end-of-file: add a text line to the
11192 * list. */
11193 chop = 0;
11194 if (!binary)
11195 while (filtd - chop - 1 >= tolist
11196 && buf[filtd - chop - 1] == '\r')
11197 ++chop;
11198 len = filtd - tolist - chop;
11199 if (prev == NULL)
11200 s = vim_strnsave(buf + tolist, len);
11201 else
11202 {
11203 s = alloc((unsigned)(prevlen + len + 1));
11204 if (s != NULL)
11205 {
11206 mch_memmove(s, prev, prevlen);
11207 vim_free(prev);
11208 prev = NULL;
11209 mch_memmove(s + prevlen, buf + tolist, len);
11210 s[prevlen + len] = NUL;
11211 }
11212 }
11213 tolist = filtd + 1;
11214
11215 li = listitem_alloc();
11216 if (li == NULL)
11217 {
11218 vim_free(s);
11219 break;
11220 }
11221 li->li_tv.v_type = VAR_STRING;
11222 li->li_tv.v_lock = 0;
11223 li->li_tv.vval.v_string = s;
11224 list_append(l, li);
11225
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011226 if (++cnt >= maxline)
11227 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011228 if (readlen <= 0)
11229 break;
11230 }
11231 else if (buf[filtd] == NUL)
11232 buf[filtd] = '\n';
11233 }
11234 if (readlen <= 0)
11235 break;
11236
11237 if (tolist == 0)
11238 {
11239 /* "buf" is full, need to move text to an allocated buffer */
11240 if (prev == NULL)
11241 {
11242 prev = vim_strnsave(buf, buflen);
11243 prevlen = buflen;
11244 }
11245 else
11246 {
11247 s = alloc((unsigned)(prevlen + buflen));
11248 if (s != NULL)
11249 {
11250 mch_memmove(s, prev, prevlen);
11251 mch_memmove(s + prevlen, buf, buflen);
11252 vim_free(prev);
11253 prev = s;
11254 prevlen += buflen;
11255 }
11256 }
11257 filtd = 0;
11258 }
11259 else
11260 {
11261 mch_memmove(buf, buf + tolist, buflen - tolist);
11262 filtd -= tolist;
11263 }
11264 }
11265
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011266 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011267 fclose(fd);
11268}
11269
11270
Bram Moolenaar0d660222005-01-07 21:51:51 +000011271#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
11272static void make_connection __ARGS((void));
11273static int check_connection __ARGS((void));
11274
11275 static void
11276make_connection()
11277{
11278 if (X_DISPLAY == NULL
11279# ifdef FEAT_GUI
11280 && !gui.in_use
11281# endif
11282 )
11283 {
11284 x_force_connect = TRUE;
11285 setup_term_clip();
11286 x_force_connect = FALSE;
11287 }
11288}
11289
11290 static int
11291check_connection()
11292{
11293 make_connection();
11294 if (X_DISPLAY == NULL)
11295 {
11296 EMSG(_("E240: No connection to Vim server"));
11297 return FAIL;
11298 }
11299 return OK;
11300}
11301#endif
11302
11303#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011304static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011305
11306 static void
11307remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000011308 typval_T *argvars;
11309 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011310 int expr;
11311{
11312 char_u *server_name;
11313 char_u *keys;
11314 char_u *r = NULL;
11315 char_u buf[NUMBUFLEN];
11316# ifdef WIN32
11317 HWND w;
11318# else
11319 Window w;
11320# endif
11321
11322 if (check_restricted() || check_secure())
11323 return;
11324
11325# ifdef FEAT_X11
11326 if (check_connection() == FAIL)
11327 return;
11328# endif
11329
11330 server_name = get_tv_string(&argvars[0]);
11331 keys = get_tv_string_buf(&argvars[1], buf);
11332# ifdef WIN32
11333 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
11334# else
11335 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
11336 < 0)
11337# endif
11338 {
11339 if (r != NULL)
11340 EMSG(r); /* sending worked but evaluation failed */
11341 else
11342 EMSG2(_("E241: Unable to send to %s"), server_name);
11343 return;
11344 }
11345
11346 rettv->vval.v_string = r;
11347
11348 if (argvars[2].v_type != VAR_UNKNOWN)
11349 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011350 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011351 char_u str[30];
11352
11353 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000011354 v.di_tv.v_type = VAR_STRING;
11355 v.di_tv.vval.v_string = vim_strsave(str);
11356 set_var(get_tv_string(&argvars[2]), &v.di_tv, FALSE);
11357 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011358 }
11359}
11360#endif
11361
11362/*
11363 * "remote_expr()" function
11364 */
11365/*ARGSUSED*/
11366 static void
11367f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011368 typval_T *argvars;
11369 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011370{
11371 rettv->v_type = VAR_STRING;
11372 rettv->vval.v_string = NULL;
11373#ifdef FEAT_CLIENTSERVER
11374 remote_common(argvars, rettv, TRUE);
11375#endif
11376}
11377
11378/*
11379 * "remote_foreground()" function
11380 */
11381/*ARGSUSED*/
11382 static void
11383f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011384 typval_T *argvars;
11385 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011386{
11387 rettv->vval.v_number = 0;
11388#ifdef FEAT_CLIENTSERVER
11389# ifdef WIN32
11390 /* On Win32 it's done in this application. */
11391 serverForeground(get_tv_string(&argvars[0]));
11392# else
11393 /* Send a foreground() expression to the server. */
11394 argvars[1].v_type = VAR_STRING;
11395 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
11396 argvars[2].v_type = VAR_UNKNOWN;
11397 remote_common(argvars, rettv, TRUE);
11398 vim_free(argvars[1].vval.v_string);
11399# endif
11400#endif
11401}
11402
11403/*ARGSUSED*/
11404 static void
11405f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011406 typval_T *argvars;
11407 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011408{
11409#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011410 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011411 char_u *s = NULL;
11412# ifdef WIN32
11413 int n = 0;
11414# endif
11415
11416 if (check_restricted() || check_secure())
11417 {
11418 rettv->vval.v_number = -1;
11419 return;
11420 }
11421# ifdef WIN32
11422 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11423 if (n == 0)
11424 rettv->vval.v_number = -1;
11425 else
11426 {
11427 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
11428 rettv->vval.v_number = (s != NULL);
11429 }
11430# else
11431 rettv->vval.v_number = 0;
11432 if (check_connection() == FAIL)
11433 return;
11434
11435 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
11436 serverStrToWin(get_tv_string(&argvars[0])), &s);
11437# endif
11438
11439 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
11440 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011441 v.di_tv.v_type = VAR_STRING;
11442 v.di_tv.vval.v_string = vim_strsave(s);
11443 set_var(get_tv_string(&argvars[1]), &v.di_tv, FALSE);
11444 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011445 }
11446#else
11447 rettv->vval.v_number = -1;
11448#endif
11449}
11450
11451/*ARGSUSED*/
11452 static void
11453f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011454 typval_T *argvars;
11455 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011456{
11457 char_u *r = NULL;
11458
11459#ifdef FEAT_CLIENTSERVER
11460 if (!check_restricted() && !check_secure())
11461 {
11462# ifdef WIN32
11463 /* The server's HWND is encoded in the 'id' parameter */
11464 int n = 0;
11465
11466 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11467 if (n != 0)
11468 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
11469 if (r == NULL)
11470# else
11471 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
11472 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
11473# endif
11474 EMSG(_("E277: Unable to read a server reply"));
11475 }
11476#endif
11477 rettv->v_type = VAR_STRING;
11478 rettv->vval.v_string = r;
11479}
11480
11481/*
11482 * "remote_send()" function
11483 */
11484/*ARGSUSED*/
11485 static void
11486f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011487 typval_T *argvars;
11488 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011489{
11490 rettv->v_type = VAR_STRING;
11491 rettv->vval.v_string = NULL;
11492#ifdef FEAT_CLIENTSERVER
11493 remote_common(argvars, rettv, FALSE);
11494#endif
11495}
11496
11497/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011498 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011499 */
11500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 typval_T *argvars;
11503 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011504{
Bram Moolenaar33570922005-01-25 22:26:29 +000011505 list_T *l;
11506 listitem_T *item, *item2;
11507 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011508 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011509 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011510 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000011511 dict_T *d;
11512 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011513
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011514 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011515 if (argvars[0].v_type == VAR_DICT)
11516 {
11517 if (argvars[2].v_type != VAR_UNKNOWN)
11518 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011519 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000011520 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011521 {
11522 key = get_tv_string(&argvars[1]);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011523 di = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011524 if (di == NULL)
11525 EMSG2(_(e_dictkey), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011526 else
11527 {
11528 *rettv = di->di_tv;
11529 init_tv(&di->di_tv);
11530 dictitem_remove(d, di);
11531 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011532 }
11533 }
11534 else if (argvars[0].v_type != VAR_LIST)
11535 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011536 else if ((l = argvars[0].vval.v_list) != NULL
11537 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011539 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011540 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011541 if (item == NULL)
11542 EMSGN(_(e_listidx), idx);
11543 else
11544 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011545 if (argvars[2].v_type == VAR_UNKNOWN)
11546 {
11547 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011548 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011549 *rettv = item->li_tv;
11550 vim_free(item);
11551 }
11552 else
11553 {
11554 /* Remove range of items, return list with values. */
11555 end = get_tv_number(&argvars[2]);
11556 item2 = list_find(l, end);
11557 if (item2 == NULL)
11558 EMSGN(_(e_listidx), end);
11559 else
11560 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011561 int cnt = 0;
11562
11563 for (li = item; li != NULL; li = li->li_next)
11564 {
11565 ++cnt;
11566 if (li == item2)
11567 break;
11568 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011569 if (li == NULL) /* didn't find "item2" after "item" */
11570 EMSG(_(e_invrange));
11571 else
11572 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011573 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011574 l = list_alloc();
11575 if (l != NULL)
11576 {
11577 rettv->v_type = VAR_LIST;
11578 rettv->vval.v_list = l;
11579 l->lv_first = item;
11580 l->lv_last = item2;
11581 l->lv_refcount = 1;
11582 item->li_prev = NULL;
11583 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011584 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011585 }
11586 }
11587 }
11588 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011589 }
11590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591}
11592
11593/*
11594 * "rename({from}, {to})" function
11595 */
11596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011597f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011598 typval_T *argvars;
11599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600{
11601 char_u buf[NUMBUFLEN];
11602
11603 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011604 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011606 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
11607 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608}
11609
11610/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011611 * "repeat()" function
11612 */
11613/*ARGSUSED*/
11614 static void
11615f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011616 typval_T *argvars;
11617 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011618{
11619 char_u *p;
11620 int n;
11621 int slen;
11622 int len;
11623 char_u *r;
11624 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011625 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011626
11627 n = get_tv_number(&argvars[1]);
11628 if (argvars[0].v_type == VAR_LIST)
11629 {
11630 l = list_alloc();
11631 if (l != NULL && argvars[0].vval.v_list != NULL)
11632 {
11633 l->lv_refcount = 1;
11634 while (n-- > 0)
11635 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
11636 break;
11637 }
11638 rettv->v_type = VAR_LIST;
11639 rettv->vval.v_list = l;
11640 }
11641 else
11642 {
11643 p = get_tv_string(&argvars[0]);
11644 rettv->v_type = VAR_STRING;
11645 rettv->vval.v_string = NULL;
11646
11647 slen = (int)STRLEN(p);
11648 len = slen * n;
11649 if (len <= 0)
11650 return;
11651
11652 r = alloc(len + 1);
11653 if (r != NULL)
11654 {
11655 for (i = 0; i < n; i++)
11656 mch_memmove(r + i * slen, p, (size_t)slen);
11657 r[len] = NUL;
11658 }
11659
11660 rettv->vval.v_string = r;
11661 }
11662}
11663
11664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665 * "resolve()" function
11666 */
11667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011668f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011669 typval_T *argvars;
11670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671{
11672 char_u *p;
11673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011674 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675#ifdef FEAT_SHORTCUT
11676 {
11677 char_u *v = NULL;
11678
11679 v = mch_resolve_shortcut(p);
11680 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011681 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011683 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684 }
11685#else
11686# ifdef HAVE_READLINK
11687 {
11688 char_u buf[MAXPATHL + 1];
11689 char_u *cpy;
11690 int len;
11691 char_u *remain = NULL;
11692 char_u *q;
11693 int is_relative_to_current = FALSE;
11694 int has_trailing_pathsep = FALSE;
11695 int limit = 100;
11696
11697 p = vim_strsave(p);
11698
11699 if (p[0] == '.' && (vim_ispathsep(p[1])
11700 || (p[1] == '.' && (vim_ispathsep(p[2])))))
11701 is_relative_to_current = TRUE;
11702
11703 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011704 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 has_trailing_pathsep = TRUE;
11706
11707 q = getnextcomp(p);
11708 if (*q != NUL)
11709 {
11710 /* Separate the first path component in "p", and keep the
11711 * remainder (beginning with the path separator). */
11712 remain = vim_strsave(q - 1);
11713 q[-1] = NUL;
11714 }
11715
11716 for (;;)
11717 {
11718 for (;;)
11719 {
11720 len = readlink((char *)p, (char *)buf, MAXPATHL);
11721 if (len <= 0)
11722 break;
11723 buf[len] = NUL;
11724
11725 if (limit-- == 0)
11726 {
11727 vim_free(p);
11728 vim_free(remain);
11729 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 goto fail;
11732 }
11733
11734 /* Ensure that the result will have a trailing path separator
11735 * if the argument has one. */
11736 if (remain == NULL && has_trailing_pathsep)
11737 add_pathsep(buf);
11738
11739 /* Separate the first path component in the link value and
11740 * concatenate the remainders. */
11741 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
11742 if (*q != NUL)
11743 {
11744 if (remain == NULL)
11745 remain = vim_strsave(q - 1);
11746 else
11747 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011748 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 if (cpy != NULL)
11750 {
11751 STRCAT(cpy, remain);
11752 vim_free(remain);
11753 remain = cpy;
11754 }
11755 }
11756 q[-1] = NUL;
11757 }
11758
11759 q = gettail(p);
11760 if (q > p && *q == NUL)
11761 {
11762 /* Ignore trailing path separator. */
11763 q[-1] = NUL;
11764 q = gettail(p);
11765 }
11766 if (q > p && !mch_isFullName(buf))
11767 {
11768 /* symlink is relative to directory of argument */
11769 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
11770 if (cpy != NULL)
11771 {
11772 STRCPY(cpy, p);
11773 STRCPY(gettail(cpy), buf);
11774 vim_free(p);
11775 p = cpy;
11776 }
11777 }
11778 else
11779 {
11780 vim_free(p);
11781 p = vim_strsave(buf);
11782 }
11783 }
11784
11785 if (remain == NULL)
11786 break;
11787
11788 /* Append the first path component of "remain" to "p". */
11789 q = getnextcomp(remain + 1);
11790 len = q - remain - (*q != NUL);
11791 cpy = vim_strnsave(p, STRLEN(p) + len);
11792 if (cpy != NULL)
11793 {
11794 STRNCAT(cpy, remain, len);
11795 vim_free(p);
11796 p = cpy;
11797 }
11798 /* Shorten "remain". */
11799 if (*q != NUL)
11800 STRCPY(remain, q - 1);
11801 else
11802 {
11803 vim_free(remain);
11804 remain = NULL;
11805 }
11806 }
11807
11808 /* If the result is a relative path name, make it explicitly relative to
11809 * the current directory if and only if the argument had this form. */
11810 if (!vim_ispathsep(*p))
11811 {
11812 if (is_relative_to_current
11813 && *p != NUL
11814 && !(p[0] == '.'
11815 && (p[1] == NUL
11816 || vim_ispathsep(p[1])
11817 || (p[1] == '.'
11818 && (p[2] == NUL
11819 || vim_ispathsep(p[2]))))))
11820 {
11821 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011822 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823 if (cpy != NULL)
11824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 vim_free(p);
11826 p = cpy;
11827 }
11828 }
11829 else if (!is_relative_to_current)
11830 {
11831 /* Strip leading "./". */
11832 q = p;
11833 while (q[0] == '.' && vim_ispathsep(q[1]))
11834 q += 2;
11835 if (q > p)
11836 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
11837 }
11838 }
11839
11840 /* Ensure that the result will have no trailing path separator
11841 * if the argument had none. But keep "/" or "//". */
11842 if (!has_trailing_pathsep)
11843 {
11844 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011845 if (after_pathsep(p, q))
11846 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847 }
11848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011849 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850 }
11851# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011852 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853# endif
11854#endif
11855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011856 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857
11858#ifdef HAVE_READLINK
11859fail:
11860#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011861 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862}
11863
11864/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011865 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866 */
11867 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011868f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011869 typval_T *argvars;
11870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871{
Bram Moolenaar33570922005-01-25 22:26:29 +000011872 list_T *l;
11873 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874
Bram Moolenaar0d660222005-01-07 21:51:51 +000011875 rettv->vval.v_number = 0;
11876 if (argvars[0].v_type != VAR_LIST)
11877 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011878 else if ((l = argvars[0].vval.v_list) != NULL
11879 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000011880 {
11881 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011882 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011883 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011884 while (li != NULL)
11885 {
11886 ni = li->li_prev;
11887 list_append(l, li);
11888 li = ni;
11889 }
11890 rettv->vval.v_list = l;
11891 rettv->v_type = VAR_LIST;
11892 ++l->lv_refcount;
11893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894}
11895
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011896#define SP_NOMOVE 1 /* don't move cursor */
11897#define SP_REPEAT 2 /* repeat to find outer pair */
11898#define SP_RETCOUNT 4 /* return matchcount */
11899
Bram Moolenaar33570922005-01-25 22:26:29 +000011900static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011901
11902/*
11903 * Get flags for a search function.
11904 * Possibly sets "p_ws".
11905 * Returns BACKWARD, FORWARD or zero (for an error).
11906 */
11907 static int
11908get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011909 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011910 int *flagsp;
11911{
11912 int dir = FORWARD;
11913 char_u *flags;
11914 char_u nbuf[NUMBUFLEN];
11915 int mask;
11916
11917 if (varp->v_type != VAR_UNKNOWN)
11918 {
11919 flags = get_tv_string_buf(varp, nbuf);
11920 while (*flags != NUL)
11921 {
11922 switch (*flags)
11923 {
11924 case 'b': dir = BACKWARD; break;
11925 case 'w': p_ws = TRUE; break;
11926 case 'W': p_ws = FALSE; break;
11927 default: mask = 0;
11928 if (flagsp != NULL)
11929 switch (*flags)
11930 {
11931 case 'n': mask = SP_NOMOVE; break;
11932 case 'r': mask = SP_REPEAT; break;
11933 case 'm': mask = SP_RETCOUNT; break;
11934 }
11935 if (mask == 0)
11936 {
11937 EMSG2(_(e_invarg2), flags);
11938 dir = 0;
11939 }
11940 else
11941 *flagsp |= mask;
11942 }
11943 if (dir == 0)
11944 break;
11945 ++flags;
11946 }
11947 }
11948 return dir;
11949}
11950
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951/*
11952 * "search()" function
11953 */
11954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011955f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011956 typval_T *argvars;
11957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958{
11959 char_u *pat;
11960 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011961 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962 int save_p_ws = p_ws;
11963 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011964 int flags = 0;
11965
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011966 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011969 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
11970 if (dir == 0)
11971 goto theend;
11972 if ((flags & ~SP_NOMOVE) != 0)
11973 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011974 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011975 goto theend;
11976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011978 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
11980 SEARCH_KEEP, RE_SEARCH) != FAIL)
11981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011982 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983 curwin->w_cursor = pos;
11984 /* "/$" will put the cursor after the end of the line, may need to
11985 * correct that here */
11986 check_cursor();
11987 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011988
11989 /* If 'n' flag is used: restore cursor position. */
11990 if (flags & SP_NOMOVE)
11991 curwin->w_cursor = save_cursor;
11992theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 p_ws = save_p_ws;
11994}
11995
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996/*
11997 * "searchpair()" function
11998 */
11999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012000f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012001 typval_T *argvars;
12002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003{
12004 char_u *spat, *mpat, *epat;
12005 char_u *skip;
12006 char_u *pat, *pat2, *pat3;
12007 pos_T pos;
12008 pos_T firstpos;
12009 pos_T save_cursor;
12010 pos_T save_pos;
12011 int save_p_ws = p_ws;
12012 char_u *save_cpo;
12013 int dir;
12014 int flags = 0;
12015 char_u nbuf1[NUMBUFLEN];
12016 char_u nbuf2[NUMBUFLEN];
12017 char_u nbuf3[NUMBUFLEN];
12018 int n;
12019 int r;
12020 int nest = 1;
12021 int err;
12022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012023 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024
12025 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12026 save_cpo = p_cpo;
12027 p_cpo = (char_u *)"";
12028
12029 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012030 spat = get_tv_string(&argvars[0]);
12031 mpat = get_tv_string_buf(&argvars[1], nbuf1);
12032 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033
12034 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12035 * start/middle/end (pat3, for the top pair). */
12036 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12037 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12038 if (pat2 == NULL || pat3 == NULL)
12039 goto theend;
12040 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12041 if (*mpat == NUL)
12042 STRCPY(pat3, pat2);
12043 else
12044 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12045 spat, epat, mpat);
12046
12047 /* Handle the optional fourth argument: flags */
12048 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012049 if (dir == 0)
12050 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051
12052 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012053 if (argvars[3].v_type == VAR_UNKNOWN
12054 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 skip = (char_u *)"";
12056 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012057 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058
12059 save_cursor = curwin->w_cursor;
12060 pos = curwin->w_cursor;
12061 firstpos.lnum = 0;
12062 pat = pat3;
12063 for (;;)
12064 {
12065 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12066 SEARCH_KEEP, RE_SEARCH);
12067 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12068 /* didn't find it or found the first match again: FAIL */
12069 break;
12070
12071 if (firstpos.lnum == 0)
12072 firstpos = pos;
12073
12074 /* If the skip pattern matches, ignore this match. */
12075 if (*skip != NUL)
12076 {
12077 save_pos = curwin->w_cursor;
12078 curwin->w_cursor = pos;
12079 r = eval_to_bool(skip, &err, NULL, FALSE);
12080 curwin->w_cursor = save_pos;
12081 if (err)
12082 {
12083 /* Evaluating {skip} caused an error, break here. */
12084 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012085 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 break;
12087 }
12088 if (r)
12089 continue;
12090 }
12091
12092 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12093 {
12094 /* Found end when searching backwards or start when searching
12095 * forward: nested pair. */
12096 ++nest;
12097 pat = pat2; /* nested, don't search for middle */
12098 }
12099 else
12100 {
12101 /* Found end when searching forward or start when searching
12102 * backward: end of (nested) pair; or found middle in outer pair. */
12103 if (--nest == 1)
12104 pat = pat3; /* outer level, search for middle */
12105 }
12106
12107 if (nest == 0)
12108 {
12109 /* Found the match: return matchcount or line number. */
12110 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012111 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012113 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114 curwin->w_cursor = pos;
12115 if (!(flags & SP_REPEAT))
12116 break;
12117 nest = 1; /* search for next unmatched */
12118 }
12119 }
12120
12121 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012122 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123 curwin->w_cursor = save_cursor;
12124
12125theend:
12126 vim_free(pat2);
12127 vim_free(pat3);
12128 p_ws = save_p_ws;
12129 p_cpo = save_cpo;
12130}
12131
Bram Moolenaar0d660222005-01-07 21:51:51 +000012132/*ARGSUSED*/
12133 static void
12134f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012135 typval_T *argvars;
12136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012138#ifdef FEAT_CLIENTSERVER
12139 char_u buf[NUMBUFLEN];
12140 char_u *server = get_tv_string(&argvars[0]);
12141 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142
Bram Moolenaar0d660222005-01-07 21:51:51 +000012143 rettv->vval.v_number = -1;
12144 if (check_restricted() || check_secure())
12145 return;
12146# ifdef FEAT_X11
12147 if (check_connection() == FAIL)
12148 return;
12149# endif
12150
12151 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012153 EMSG(_("E258: Unable to send to client"));
12154 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012156 rettv->vval.v_number = 0;
12157#else
12158 rettv->vval.v_number = -1;
12159#endif
12160}
12161
12162/*ARGSUSED*/
12163 static void
12164f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012165 typval_T *argvars;
12166 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012167{
12168 char_u *r = NULL;
12169
12170#ifdef FEAT_CLIENTSERVER
12171# ifdef WIN32
12172 r = serverGetVimNames();
12173# else
12174 make_connection();
12175 if (X_DISPLAY != NULL)
12176 r = serverGetVimNames(X_DISPLAY);
12177# endif
12178#endif
12179 rettv->v_type = VAR_STRING;
12180 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181}
12182
12183/*
12184 * "setbufvar()" function
12185 */
12186/*ARGSUSED*/
12187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012188f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012189 typval_T *argvars;
12190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191{
12192 buf_T *buf;
12193#ifdef FEAT_AUTOCMD
12194 aco_save_T aco;
12195#else
12196 buf_T *save_curbuf;
12197#endif
12198 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012199 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 char_u nbuf[NUMBUFLEN];
12201
12202 if (check_restricted() || check_secure())
12203 return;
12204 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012205 buf = get_buf_tv(&argvars[0]);
12206 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207 varp = &argvars[2];
12208
12209 if (buf != NULL && varname != NULL && varp != NULL)
12210 {
12211 /* set curbuf to be our buf, temporarily */
12212#ifdef FEAT_AUTOCMD
12213 aucmd_prepbuf(&aco, buf);
12214#else
12215 save_curbuf = curbuf;
12216 curbuf = buf;
12217#endif
12218
12219 if (*varname == '&')
12220 {
12221 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012222 set_option_value(varname, get_tv_number(varp),
12223 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224 }
12225 else
12226 {
12227 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
12228 if (bufvarname != NULL)
12229 {
12230 STRCPY(bufvarname, "b:");
12231 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012232 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233 vim_free(bufvarname);
12234 }
12235 }
12236
12237 /* reset notion of buffer */
12238#ifdef FEAT_AUTOCMD
12239 aucmd_restbuf(&aco);
12240#else
12241 curbuf = save_curbuf;
12242#endif
12243 }
12244 --emsg_off;
12245}
12246
12247/*
12248 * "setcmdpos()" function
12249 */
12250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012251f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012252 typval_T *argvars;
12253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012255 rettv->vval.v_number = set_cmdline_pos(
12256 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257}
12258
12259/*
12260 * "setline()" function
12261 */
12262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012263f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012264 typval_T *argvars;
12265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266{
12267 linenr_T lnum;
12268 char_u *line;
12269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012270 lnum = get_tv_lnum(argvars);
12271 line = get_tv_string(&argvars[1]);
12272 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273
12274 if (lnum >= 1
12275 && lnum <= curbuf->b_ml.ml_line_count
12276 && u_savesub(lnum) == OK
12277 && ml_replace(lnum, line, TRUE) == OK)
12278 {
12279 changed_bytes(lnum, 0);
12280 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012281 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282 }
12283}
12284
12285/*
12286 * "setreg()" function
12287 */
12288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012289f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012290 typval_T *argvars;
12291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292{
12293 int regname;
12294 char_u *strregname;
12295 char_u *stropt;
12296 int append;
12297 char_u yank_type;
12298 long block_len;
12299
12300 block_len = -1;
12301 yank_type = MAUTO;
12302 append = FALSE;
12303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012304 strregname = get_tv_string(argvars);
12305 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306
12307 regname = (strregname == NULL ? '"' : *strregname);
12308 if (regname == 0 || regname == '@')
12309 regname = '"';
12310 else if (regname == '=')
12311 return;
12312
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012313 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012315 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316 switch (*stropt)
12317 {
12318 case 'a': case 'A': /* append */
12319 append = TRUE;
12320 break;
12321 case 'v': case 'c': /* character-wise selection */
12322 yank_type = MCHAR;
12323 break;
12324 case 'V': case 'l': /* line-wise selection */
12325 yank_type = MLINE;
12326 break;
12327#ifdef FEAT_VISUAL
12328 case 'b': case Ctrl_V: /* block-wise selection */
12329 yank_type = MBLOCK;
12330 if (VIM_ISDIGIT(stropt[1]))
12331 {
12332 ++stropt;
12333 block_len = getdigits(&stropt) - 1;
12334 --stropt;
12335 }
12336 break;
12337#endif
12338 }
12339 }
12340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012341 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012343 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344}
12345
12346
12347/*
12348 * "setwinvar(expr)" function
12349 */
12350/*ARGSUSED*/
12351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012352f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012353 typval_T *argvars;
12354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355{
12356 win_T *win;
12357#ifdef FEAT_WINDOWS
12358 win_T *save_curwin;
12359#endif
12360 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012361 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362 char_u nbuf[NUMBUFLEN];
12363
12364 if (check_restricted() || check_secure())
12365 return;
12366 ++emsg_off;
12367 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369 varp = &argvars[2];
12370
12371 if (win != NULL && varname != NULL && varp != NULL)
12372 {
12373#ifdef FEAT_WINDOWS
12374 /* set curwin to be our win, temporarily */
12375 save_curwin = curwin;
12376 curwin = win;
12377 curbuf = curwin->w_buffer;
12378#endif
12379
12380 if (*varname == '&')
12381 {
12382 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012383 set_option_value(varname, get_tv_number(varp),
12384 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385 }
12386 else
12387 {
12388 winvarname = alloc((unsigned)STRLEN(varname) + 3);
12389 if (winvarname != NULL)
12390 {
12391 STRCPY(winvarname, "w:");
12392 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012393 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394 vim_free(winvarname);
12395 }
12396 }
12397
12398#ifdef FEAT_WINDOWS
12399 /* Restore current window, if it's still valid (autocomands can make
12400 * it invalid). */
12401 if (win_valid(save_curwin))
12402 {
12403 curwin = save_curwin;
12404 curbuf = curwin->w_buffer;
12405 }
12406#endif
12407 }
12408 --emsg_off;
12409}
12410
12411/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012412 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413 */
12414 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012415f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012416 typval_T *argvars;
12417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012419 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420
Bram Moolenaar0d660222005-01-07 21:51:51 +000012421 p = get_tv_string(&argvars[0]);
12422 rettv->vval.v_string = vim_strsave(p);
12423 simplify_filename(rettv->vval.v_string); /* simplify in place */
12424 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425}
12426
Bram Moolenaar0d660222005-01-07 21:51:51 +000012427static int
12428#ifdef __BORLANDC__
12429 _RTLENTRYF
12430#endif
12431 item_compare __ARGS((const void *s1, const void *s2));
12432static int
12433#ifdef __BORLANDC__
12434 _RTLENTRYF
12435#endif
12436 item_compare2 __ARGS((const void *s1, const void *s2));
12437
12438static int item_compare_ic;
12439static char_u *item_compare_func;
12440#define ITEM_COMPARE_FAIL 999
12441
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012443 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012445 static int
12446#ifdef __BORLANDC__
12447_RTLENTRYF
12448#endif
12449item_compare(s1, s2)
12450 const void *s1;
12451 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012453 char_u *p1, *p2;
12454 char_u *tofree1, *tofree2;
12455 int res;
12456 char_u numbuf1[NUMBUFLEN];
12457 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458
Bram Moolenaar33570922005-01-25 22:26:29 +000012459 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
12460 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012461 if (item_compare_ic)
12462 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012464 res = STRCMP(p1, p2);
12465 vim_free(tofree1);
12466 vim_free(tofree2);
12467 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468}
12469
12470 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000012471#ifdef __BORLANDC__
12472_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000012474item_compare2(s1, s2)
12475 const void *s1;
12476 const void *s2;
12477{
12478 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000012479 typval_T rettv;
12480 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000012481 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012483 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
12484 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012485 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
12486 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012487
12488 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12489 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000012490 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012491 clear_tv(&argv[0]);
12492 clear_tv(&argv[1]);
12493
12494 if (res == FAIL)
12495 res = ITEM_COMPARE_FAIL;
12496 else
12497 res = get_tv_number(&rettv);
12498 clear_tv(&rettv);
12499 return res;
12500}
12501
12502/*
12503 * "sort({list})" function
12504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012506f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012507 typval_T *argvars;
12508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509{
Bram Moolenaar33570922005-01-25 22:26:29 +000012510 list_T *l;
12511 listitem_T *li;
12512 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012513 long len;
12514 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515
Bram Moolenaar0d660222005-01-07 21:51:51 +000012516 rettv->vval.v_number = 0;
12517 if (argvars[0].v_type != VAR_LIST)
12518 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519 else
12520 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012521 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012522 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012523 return;
12524 rettv->vval.v_list = l;
12525 rettv->v_type = VAR_LIST;
12526 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527
Bram Moolenaar0d660222005-01-07 21:51:51 +000012528 len = list_len(l);
12529 if (len <= 1)
12530 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531
Bram Moolenaar0d660222005-01-07 21:51:51 +000012532 item_compare_ic = FALSE;
12533 item_compare_func = NULL;
12534 if (argvars[1].v_type != VAR_UNKNOWN)
12535 {
12536 if (argvars[1].v_type == VAR_FUNC)
12537 item_compare_func = argvars[0].vval.v_string;
12538 else
12539 {
12540 i = get_tv_number(&argvars[1]);
12541 if (i == 1)
12542 item_compare_ic = TRUE;
12543 else
12544 item_compare_func = get_tv_string(&argvars[1]);
12545 }
12546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012547
Bram Moolenaar0d660222005-01-07 21:51:51 +000012548 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012549 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012550 if (ptrs == NULL)
12551 return;
12552 i = 0;
12553 for (li = l->lv_first; li != NULL; li = li->li_next)
12554 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555
Bram Moolenaar0d660222005-01-07 21:51:51 +000012556 /* test the compare function */
12557 if (item_compare_func != NULL
12558 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12559 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012560 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012562 {
12563 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012564 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000012565 item_compare_func == NULL ? item_compare : item_compare2);
12566
12567 /* Clear the List and append the items in the sorted order. */
12568 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012569 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012570 for (i = 0; i < len; ++i)
12571 list_append(l, ptrs[i]);
12572 }
12573
12574 vim_free(ptrs);
12575 }
12576}
12577
12578 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012579f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012580 typval_T *argvars;
12581 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012582{
12583 char_u *str;
12584 char_u *end;
12585 char_u *pat;
12586 regmatch_T regmatch;
12587 char_u patbuf[NUMBUFLEN];
12588 char_u *save_cpo;
12589 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000012590 listitem_T *ni;
12591 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012592 colnr_T col = 0;
12593
12594 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12595 save_cpo = p_cpo;
12596 p_cpo = (char_u *)"";
12597
12598 str = get_tv_string(&argvars[0]);
12599 if (argvars[1].v_type == VAR_UNKNOWN)
12600 pat = (char_u *)"[\\x01- ]\\+";
12601 else
12602 pat = get_tv_string_buf(&argvars[1], patbuf);
12603
12604 l = list_alloc();
12605 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012607 rettv->v_type = VAR_LIST;
12608 rettv->vval.v_list = l;
12609 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012610
Bram Moolenaar0d660222005-01-07 21:51:51 +000012611 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12612 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012614 regmatch.rm_ic = FALSE;
12615 while (*str != NUL)
12616 {
12617 match = vim_regexec_nl(&regmatch, str, col);
12618 if (match)
12619 end = regmatch.startp[0];
12620 else
12621 end = str + STRLEN(str);
12622 if (end > str)
12623 {
12624 ni = listitem_alloc();
12625 if (ni == NULL)
12626 break;
12627 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012628 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012629 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
12630 list_append(l, ni);
12631 }
12632 if (!match)
12633 break;
12634 /* Advance to just after the match. */
12635 if (regmatch.endp[0] > str)
12636 col = 0;
12637 else
12638 {
12639 /* Don't get stuck at the same match. */
12640#ifdef FEAT_MBYTE
12641 col = mb_ptr2len_check(regmatch.endp[0]);
12642#else
12643 col = 1;
12644#endif
12645 }
12646 str = regmatch.endp[0];
12647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648
Bram Moolenaar0d660222005-01-07 21:51:51 +000012649 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651
Bram Moolenaar0d660222005-01-07 21:51:51 +000012652 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653}
12654
12655#ifdef HAVE_STRFTIME
12656/*
12657 * "strftime({format}[, {time}])" function
12658 */
12659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012660f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012661 typval_T *argvars;
12662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663{
12664 char_u result_buf[256];
12665 struct tm *curtime;
12666 time_t seconds;
12667 char_u *p;
12668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012669 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012671 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012672 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673 seconds = time(NULL);
12674 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012675 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676 curtime = localtime(&seconds);
12677 /* MSVC returns NULL for an invalid value of seconds. */
12678 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012679 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680 else
12681 {
12682# ifdef FEAT_MBYTE
12683 vimconv_T conv;
12684 char_u *enc;
12685
12686 conv.vc_type = CONV_NONE;
12687 enc = enc_locale();
12688 convert_setup(&conv, p_enc, enc);
12689 if (conv.vc_type != CONV_NONE)
12690 p = string_convert(&conv, p, NULL);
12691# endif
12692 if (p != NULL)
12693 (void)strftime((char *)result_buf, sizeof(result_buf),
12694 (char *)p, curtime);
12695 else
12696 result_buf[0] = NUL;
12697
12698# ifdef FEAT_MBYTE
12699 if (conv.vc_type != CONV_NONE)
12700 vim_free(p);
12701 convert_setup(&conv, enc, p_enc);
12702 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012703 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704 else
12705# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012706 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707
12708# ifdef FEAT_MBYTE
12709 /* Release conversion descriptors */
12710 convert_setup(&conv, NULL, NULL);
12711 vim_free(enc);
12712# endif
12713 }
12714}
12715#endif
12716
12717/*
12718 * "stridx()" function
12719 */
12720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012721f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012722 typval_T *argvars;
12723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724{
12725 char_u buf[NUMBUFLEN];
12726 char_u *needle;
12727 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000012728 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000012730 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732 needle = get_tv_string(&argvars[1]);
Bram Moolenaar33570922005-01-25 22:26:29 +000012733 save_haystack = haystack = get_tv_string_buf(&argvars[0], buf);
12734 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735
Bram Moolenaar33570922005-01-25 22:26:29 +000012736 if (argvars[2].v_type != VAR_UNKNOWN)
12737 {
12738 start_idx = get_tv_number(&argvars[2]);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012739 if (start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000012740 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012741 if (start_idx >= 0)
12742 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000012743 }
12744
12745 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12746 if (pos != NULL)
12747 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748}
12749
12750/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012751 * "string()" function
12752 */
12753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012755 typval_T *argvars;
12756 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012757{
12758 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012759 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012761 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012762 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012763 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012764 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765}
12766
12767/*
12768 * "strlen()" function
12769 */
12770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012772 typval_T *argvars;
12773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012775 rettv->vval.v_number = (varnumber_T)(STRLEN(
12776 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777}
12778
12779/*
12780 * "strpart()" function
12781 */
12782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012783f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012784 typval_T *argvars;
12785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786{
12787 char_u *p;
12788 int n;
12789 int len;
12790 int slen;
12791
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012792 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793 slen = (int)STRLEN(p);
12794
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012795 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012796 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012797 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798 else
12799 len = slen - n; /* default len: all bytes that are available. */
12800
12801 /*
12802 * Only return the overlap between the specified part and the actual
12803 * string.
12804 */
12805 if (n < 0)
12806 {
12807 len += n;
12808 n = 0;
12809 }
12810 else if (n > slen)
12811 n = slen;
12812 if (len < 0)
12813 len = 0;
12814 else if (n + len > slen)
12815 len = slen - n;
12816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817 rettv->v_type = VAR_STRING;
12818 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819}
12820
12821/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012822 * "strridx()" function
12823 */
12824 static void
12825f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012826 typval_T *argvars;
12827 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012828{
12829 char_u buf[NUMBUFLEN];
12830 char_u *needle;
12831 char_u *haystack;
12832 char_u *rest;
12833 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012834 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012835
12836 needle = get_tv_string(&argvars[1]);
12837 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012838 haystack_len = STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000012839 if (argvars[2].v_type != VAR_UNKNOWN)
12840 {
12841 /* Third argument: upper limit for index */
12842 end_idx = get_tv_number(&argvars[2]);
12843 if (end_idx < 0)
12844 {
12845 /* can never find a match */
12846 rettv->vval.v_number = -1;
12847 return;
12848 }
12849 }
12850 else
12851 end_idx = haystack_len;
12852
Bram Moolenaar0d660222005-01-07 21:51:51 +000012853 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000012854 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012855 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000012856 lastmatch = haystack + end_idx;
12857 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012858 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000012859 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012860 for (rest = haystack; *rest != '\0'; ++rest)
12861 {
12862 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012863 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012864 break;
12865 lastmatch = rest;
12866 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000012867 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012868
12869 if (lastmatch == NULL)
12870 rettv->vval.v_number = -1;
12871 else
12872 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12873}
12874
12875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 * "strtrans()" function
12877 */
12878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012880 typval_T *argvars;
12881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 rettv->v_type = VAR_STRING;
12884 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885}
12886
12887/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012888 * "submatch()" function
12889 */
12890 static void
12891f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012892 typval_T *argvars;
12893 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012894{
12895 rettv->v_type = VAR_STRING;
12896 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
12897}
12898
12899/*
12900 * "substitute()" function
12901 */
12902 static void
12903f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012904 typval_T *argvars;
12905 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012906{
12907 char_u patbuf[NUMBUFLEN];
12908 char_u subbuf[NUMBUFLEN];
12909 char_u flagsbuf[NUMBUFLEN];
12910
12911 rettv->v_type = VAR_STRING;
12912 rettv->vval.v_string = do_string_sub(
12913 get_tv_string(&argvars[0]),
12914 get_tv_string_buf(&argvars[1], patbuf),
12915 get_tv_string_buf(&argvars[2], subbuf),
12916 get_tv_string_buf(&argvars[3], flagsbuf));
12917}
12918
12919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920 * "synID(line, col, trans)" function
12921 */
12922/*ARGSUSED*/
12923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012924f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012925 typval_T *argvars;
12926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927{
12928 int id = 0;
12929#ifdef FEAT_SYN_HL
12930 long line;
12931 long col;
12932 int trans;
12933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012934 line = get_tv_lnum(argvars);
12935 col = get_tv_number(&argvars[1]) - 1;
12936 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937
12938 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
12939 && col >= 0 && col < (long)STRLEN(ml_get(line)))
12940 id = syn_get_id(line, col, trans);
12941#endif
12942
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012943 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944}
12945
12946/*
12947 * "synIDattr(id, what [, mode])" function
12948 */
12949/*ARGSUSED*/
12950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012951f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012952 typval_T *argvars;
12953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954{
12955 char_u *p = NULL;
12956#ifdef FEAT_SYN_HL
12957 int id;
12958 char_u *what;
12959 char_u *mode;
12960 char_u modebuf[NUMBUFLEN];
12961 int modec;
12962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012963 id = get_tv_number(&argvars[0]);
12964 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012965 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012967 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968 modec = TOLOWER_ASC(mode[0]);
12969 if (modec != 't' && modec != 'c'
12970#ifdef FEAT_GUI
12971 && modec != 'g'
12972#endif
12973 )
12974 modec = 0; /* replace invalid with current */
12975 }
12976 else
12977 {
12978#ifdef FEAT_GUI
12979 if (gui.in_use)
12980 modec = 'g';
12981 else
12982#endif
12983 if (t_colors > 1)
12984 modec = 'c';
12985 else
12986 modec = 't';
12987 }
12988
12989
12990 switch (TOLOWER_ASC(what[0]))
12991 {
12992 case 'b':
12993 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12994 p = highlight_color(id, what, modec);
12995 else /* bold */
12996 p = highlight_has_attr(id, HL_BOLD, modec);
12997 break;
12998
12999 case 'f': /* fg[#] */
13000 p = highlight_color(id, what, modec);
13001 break;
13002
13003 case 'i':
13004 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13005 p = highlight_has_attr(id, HL_INVERSE, modec);
13006 else /* italic */
13007 p = highlight_has_attr(id, HL_ITALIC, modec);
13008 break;
13009
13010 case 'n': /* name */
13011 p = get_highlight_name(NULL, id - 1);
13012 break;
13013
13014 case 'r': /* reverse */
13015 p = highlight_has_attr(id, HL_INVERSE, modec);
13016 break;
13017
13018 case 's': /* standout */
13019 p = highlight_has_attr(id, HL_STANDOUT, modec);
13020 break;
13021
13022 case 'u': /* underline */
13023 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13024 break;
13025 }
13026
13027 if (p != NULL)
13028 p = vim_strsave(p);
13029#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013030 rettv->v_type = VAR_STRING;
13031 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032}
13033
13034/*
13035 * "synIDtrans(id)" function
13036 */
13037/*ARGSUSED*/
13038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013039f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013040 typval_T *argvars;
13041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042{
13043 int id;
13044
13045#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013046 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047
13048 if (id > 0)
13049 id = syn_get_final_id(id);
13050 else
13051#endif
13052 id = 0;
13053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013054 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055}
13056
13057/*
13058 * "system()" function
13059 */
13060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013061f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013062 typval_T *argvars;
13063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013065 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013067 char_u *infile = NULL;
13068 char_u buf[NUMBUFLEN];
13069 int err = FALSE;
13070 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013072 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013073 {
13074 /*
13075 * Write the string to a temp file, to be used for input of the shell
13076 * command.
13077 */
13078 if ((infile = vim_tempname('i')) == NULL)
13079 {
13080 EMSG(_(e_notmp));
13081 return;
13082 }
13083
13084 fd = mch_fopen((char *)infile, WRITEBIN);
13085 if (fd == NULL)
13086 {
13087 EMSG2(_(e_notopen), infile);
13088 goto done;
13089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013090 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013091 if (fwrite(p, STRLEN(p), 1, fd) != 1)
13092 err = TRUE;
13093 if (fclose(fd) != 0)
13094 err = TRUE;
13095 if (err)
13096 {
13097 EMSG(_("E677: Error writing temp file"));
13098 goto done;
13099 }
13100 }
13101
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013102 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013103
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104#ifdef USE_CR
13105 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013106 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 {
13108 char_u *s;
13109
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013110 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111 {
13112 if (*s == CAR)
13113 *s = NL;
13114 }
13115 }
13116#else
13117# ifdef USE_CRNL
13118 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013119 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120 {
13121 char_u *s, *d;
13122
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013123 d = res;
13124 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125 {
13126 if (s[0] == CAR && s[1] == NL)
13127 ++s;
13128 *d++ = *s;
13129 }
13130 *d = NUL;
13131 }
13132# endif
13133#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013134
13135done:
13136 if (infile != NULL)
13137 {
13138 mch_remove(infile);
13139 vim_free(infile);
13140 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141 rettv->v_type = VAR_STRING;
13142 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143}
13144
13145/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000013146 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000013147 */
13148 static void
13149f_taglist(argvars, rettv)
13150 typval_T *argvars;
13151 typval_T *rettv;
13152{
13153 char_u *tag_pattern;
13154 list_T *l;
13155
13156 tag_pattern = get_tv_string(&argvars[0]);
13157
13158 rettv->vval.v_number = FALSE;
13159
13160 l = list_alloc();
13161 if (l != NULL)
13162 {
13163 if (get_tags(l, tag_pattern) != FAIL)
13164 {
13165 rettv->vval.v_list = l;
13166 rettv->v_type = VAR_LIST;
13167 ++l->lv_refcount;
13168 }
13169 else
13170 list_free(l);
13171 }
13172}
13173
13174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175 * "tempname()" function
13176 */
13177/*ARGSUSED*/
13178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013179f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013180 typval_T *argvars;
13181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182{
13183 static int x = 'A';
13184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013185 rettv->v_type = VAR_STRING;
13186 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187
13188 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13189 * names. Skip 'I' and 'O', they are used for shell redirection. */
13190 do
13191 {
13192 if (x == 'Z')
13193 x = '0';
13194 else if (x == '9')
13195 x = 'A';
13196 else
13197 {
13198#ifdef EBCDIC
13199 if (x == 'I')
13200 x = 'J';
13201 else if (x == 'R')
13202 x = 'S';
13203 else
13204#endif
13205 ++x;
13206 }
13207 } while (x == 'I' || x == 'O');
13208}
13209
13210/*
13211 * "tolower(string)" function
13212 */
13213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013214f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013215 typval_T *argvars;
13216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217{
13218 char_u *p;
13219
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013220 p = vim_strsave(get_tv_string(&argvars[0]));
13221 rettv->v_type = VAR_STRING;
13222 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223
13224 if (p != NULL)
13225 while (*p != NUL)
13226 {
13227#ifdef FEAT_MBYTE
13228 int l;
13229
13230 if (enc_utf8)
13231 {
13232 int c, lc;
13233
13234 c = utf_ptr2char(p);
13235 lc = utf_tolower(c);
13236 l = utf_ptr2len_check(p);
13237 /* TODO: reallocate string when byte count changes. */
13238 if (utf_char2len(lc) == l)
13239 utf_char2bytes(lc, p);
13240 p += l;
13241 }
13242 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13243 p += l; /* skip multi-byte character */
13244 else
13245#endif
13246 {
13247 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
13248 ++p;
13249 }
13250 }
13251}
13252
13253/*
13254 * "toupper(string)" function
13255 */
13256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013257f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013258 typval_T *argvars;
13259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260{
13261 char_u *p;
13262
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013263 p = vim_strsave(get_tv_string(&argvars[0]));
13264 rettv->v_type = VAR_STRING;
13265 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266
13267 if (p != NULL)
13268 while (*p != NUL)
13269 {
13270#ifdef FEAT_MBYTE
13271 int l;
13272
13273 if (enc_utf8)
13274 {
13275 int c, uc;
13276
13277 c = utf_ptr2char(p);
13278 uc = utf_toupper(c);
13279 l = utf_ptr2len_check(p);
13280 /* TODO: reallocate string when byte count changes. */
13281 if (utf_char2len(uc) == l)
13282 utf_char2bytes(uc, p);
13283 p += l;
13284 }
13285 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13286 p += l; /* skip multi-byte character */
13287 else
13288#endif
13289 {
13290 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
13291 p++;
13292 }
13293 }
13294}
13295
13296/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000013297 * "tr(string, fromstr, tostr)" function
13298 */
13299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013300f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013301 typval_T *argvars;
13302 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013303{
13304 char_u *instr;
13305 char_u *fromstr;
13306 char_u *tostr;
13307 char_u *p;
13308#ifdef FEAT_MBYTE
13309 int inlen;
13310 int fromlen;
13311 int tolen;
13312 int idx;
13313 char_u *cpstr;
13314 int cplen;
13315 int first = TRUE;
13316#endif
13317 char_u buf[NUMBUFLEN];
13318 char_u buf2[NUMBUFLEN];
13319 garray_T ga;
13320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013321 instr = get_tv_string(&argvars[0]);
13322 fromstr = get_tv_string_buf(&argvars[1], buf);
13323 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013324
13325 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013326 rettv->v_type = VAR_STRING;
13327 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013328 ga_init2(&ga, (int)sizeof(char), 80);
13329
13330#ifdef FEAT_MBYTE
13331 if (!has_mbyte)
13332#endif
13333 /* not multi-byte: fromstr and tostr must be the same length */
13334 if (STRLEN(fromstr) != STRLEN(tostr))
13335 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013336#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000013337error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013338#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000013339 EMSG2(_(e_invarg2), fromstr);
13340 ga_clear(&ga);
13341 return;
13342 }
13343
13344 /* fromstr and tostr have to contain the same number of chars */
13345 while (*instr != NUL)
13346 {
13347#ifdef FEAT_MBYTE
13348 if (has_mbyte)
13349 {
13350 inlen = mb_ptr2len_check(instr);
13351 cpstr = instr;
13352 cplen = inlen;
13353 idx = 0;
13354 for (p = fromstr; *p != NUL; p += fromlen)
13355 {
13356 fromlen = mb_ptr2len_check(p);
13357 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
13358 {
13359 for (p = tostr; *p != NUL; p += tolen)
13360 {
13361 tolen = mb_ptr2len_check(p);
13362 if (idx-- == 0)
13363 {
13364 cplen = tolen;
13365 cpstr = p;
13366 break;
13367 }
13368 }
13369 if (*p == NUL) /* tostr is shorter than fromstr */
13370 goto error;
13371 break;
13372 }
13373 ++idx;
13374 }
13375
13376 if (first && cpstr == instr)
13377 {
13378 /* Check that fromstr and tostr have the same number of
13379 * (multi-byte) characters. Done only once when a character
13380 * of instr doesn't appear in fromstr. */
13381 first = FALSE;
13382 for (p = tostr; *p != NUL; p += tolen)
13383 {
13384 tolen = mb_ptr2len_check(p);
13385 --idx;
13386 }
13387 if (idx != 0)
13388 goto error;
13389 }
13390
13391 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000013392 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013393 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013394
13395 instr += inlen;
13396 }
13397 else
13398#endif
13399 {
13400 /* When not using multi-byte chars we can do it faster. */
13401 p = vim_strchr(fromstr, *instr);
13402 if (p != NULL)
13403 ga_append(&ga, tostr[p - fromstr]);
13404 else
13405 ga_append(&ga, *instr);
13406 ++instr;
13407 }
13408 }
13409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013410 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013411}
13412
13413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414 * "type(expr)" function
13415 */
13416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013417f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013418 typval_T *argvars;
13419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013421 int n;
13422
13423 switch (argvars[0].v_type)
13424 {
13425 case VAR_NUMBER: n = 0; break;
13426 case VAR_STRING: n = 1; break;
13427 case VAR_FUNC: n = 2; break;
13428 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013429 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013430 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
13431 }
13432 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433}
13434
13435/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013436 * "values(dict)" function
13437 */
13438 static void
13439f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013440 typval_T *argvars;
13441 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013442{
13443 dict_list(argvars, rettv, 1);
13444}
13445
13446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447 * "virtcol(string)" function
13448 */
13449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013450f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013451 typval_T *argvars;
13452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453{
13454 colnr_T vcol = 0;
13455 pos_T *fp;
13456
13457 fp = var2fpos(&argvars[0], FALSE);
13458 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
13459 {
13460 getvvcol(curwin, fp, NULL, NULL, &vcol);
13461 ++vcol;
13462 }
13463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013464 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465}
13466
13467/*
13468 * "visualmode()" function
13469 */
13470/*ARGSUSED*/
13471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013472f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013473 typval_T *argvars;
13474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475{
13476#ifdef FEAT_VISUAL
13477 char_u str[2];
13478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013479 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480 str[0] = curbuf->b_visual_mode_eval;
13481 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013482 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483
13484 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013485 if ((argvars[0].v_type == VAR_NUMBER
13486 && argvars[0].vval.v_number != 0)
13487 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013488 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489 curbuf->b_visual_mode_eval = NUL;
13490#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013491 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492#endif
13493}
13494
13495/*
13496 * "winbufnr(nr)" function
13497 */
13498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013499f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013500 typval_T *argvars;
13501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502{
13503 win_T *wp;
13504
13505 wp = find_win_by_nr(&argvars[0]);
13506 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013507 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013509 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510}
13511
13512/*
13513 * "wincol()" function
13514 */
13515/*ARGSUSED*/
13516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013517f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013518 typval_T *argvars;
13519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520{
13521 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013522 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523}
13524
13525/*
13526 * "winheight(nr)" function
13527 */
13528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013529f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013530 typval_T *argvars;
13531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532{
13533 win_T *wp;
13534
13535 wp = find_win_by_nr(&argvars[0]);
13536 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013537 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013539 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540}
13541
13542/*
13543 * "winline()" function
13544 */
13545/*ARGSUSED*/
13546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013547f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013548 typval_T *argvars;
13549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550{
13551 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013552 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553}
13554
13555/*
13556 * "winnr()" function
13557 */
13558/* ARGSUSED */
13559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013560f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013561 typval_T *argvars;
13562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563{
13564 int nr = 1;
13565#ifdef FEAT_WINDOWS
13566 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013567 win_T *twin = curwin;
13568 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013570 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013571 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013572 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013573 if (STRCMP(arg, "$") == 0)
13574 twin = lastwin;
13575 else if (STRCMP(arg, "#") == 0)
13576 {
13577 twin = prevwin;
13578 if (prevwin == NULL)
13579 nr = 0;
13580 }
13581 else
13582 {
13583 EMSG2(_(e_invexpr2), arg);
13584 nr = 0;
13585 }
13586 }
13587
13588 if (nr > 0)
13589 for (wp = firstwin; wp != twin; wp = wp->w_next)
13590 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593}
13594
13595/*
13596 * "winrestcmd()" function
13597 */
13598/* ARGSUSED */
13599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013600f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013601 typval_T *argvars;
13602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603{
13604#ifdef FEAT_WINDOWS
13605 win_T *wp;
13606 int winnr = 1;
13607 garray_T ga;
13608 char_u buf[50];
13609
13610 ga_init2(&ga, (int)sizeof(char), 70);
13611 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13612 {
13613 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13614 ga_concat(&ga, buf);
13615# ifdef FEAT_VERTSPLIT
13616 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13617 ga_concat(&ga, buf);
13618# endif
13619 ++winnr;
13620 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000013621 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628}
13629
13630/*
13631 * "winwidth(nr)" function
13632 */
13633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013634f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013635 typval_T *argvars;
13636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637{
13638 win_T *wp;
13639
13640 wp = find_win_by_nr(&argvars[0]);
13641 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013642 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 else
13644#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013645 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013647 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648#endif
13649}
13650
13651 static win_T *
13652find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013653 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654{
13655#ifdef FEAT_WINDOWS
13656 win_T *wp;
13657#endif
13658 int nr;
13659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013660 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661
13662#ifdef FEAT_WINDOWS
13663 if (nr == 0)
13664 return curwin;
13665
13666 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13667 if (--nr <= 0)
13668 break;
13669 return wp;
13670#else
13671 if (nr == 0 || nr == 1)
13672 return curwin;
13673 return NULL;
13674#endif
13675}
13676
13677/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013678 * "writefile()" function
13679 */
13680 static void
13681f_writefile(argvars, rettv)
13682 typval_T *argvars;
13683 typval_T *rettv;
13684{
13685 int binary = FALSE;
13686 char_u *fname;
13687 FILE *fd;
13688 listitem_T *li;
13689 char_u *s;
13690 int ret = 0;
13691 int c;
13692
13693 if (argvars[0].v_type != VAR_LIST)
13694 {
13695 EMSG2(_(e_listarg), "writefile()");
13696 return;
13697 }
13698 if (argvars[0].vval.v_list == NULL)
13699 return;
13700
13701 if (argvars[2].v_type != VAR_UNKNOWN
13702 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
13703 binary = TRUE;
13704
13705 /* Always open the file in binary mode, library functions have a mind of
13706 * their own about CR-LF conversion. */
13707 fname = get_tv_string(&argvars[1]);
13708 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
13709 {
13710 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13711 ret = -1;
13712 }
13713 else
13714 {
13715 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
13716 li = li->li_next)
13717 {
13718 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
13719 {
13720 if (*s == '\n')
13721 c = putc(NUL, fd);
13722 else
13723 c = putc(*s, fd);
13724 if (c == EOF)
13725 {
13726 ret = -1;
13727 break;
13728 }
13729 }
13730 if (!binary || li->li_next != NULL)
13731 if (putc('\n', fd) == EOF)
13732 {
13733 ret = -1;
13734 break;
13735 }
13736 if (ret < 0)
13737 {
13738 EMSG(_(e_write));
13739 break;
13740 }
13741 }
13742 fclose(fd);
13743 }
13744
13745 rettv->vval.v_number = ret;
13746}
13747
13748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 * Translate a String variable into a position.
13750 */
13751 static pos_T *
13752var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000013753 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754 int lnum; /* TRUE when $ is last line */
13755{
13756 char_u *name;
13757 static pos_T pos;
13758 pos_T *pp;
13759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013760 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761 if (name[0] == '.') /* cursor */
13762 return &curwin->w_cursor;
13763 if (name[0] == '\'') /* mark */
13764 {
13765 pp = getmark(name[1], FALSE);
13766 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
13767 return NULL;
13768 return pp;
13769 }
13770 if (name[0] == '$') /* last column or line */
13771 {
13772 if (lnum)
13773 {
13774 pos.lnum = curbuf->b_ml.ml_line_count;
13775 pos.col = 0;
13776 }
13777 else
13778 {
13779 pos.lnum = curwin->w_cursor.lnum;
13780 pos.col = (colnr_T)STRLEN(ml_get_curline());
13781 }
13782 return &pos;
13783 }
13784 return NULL;
13785}
13786
13787/*
13788 * Get the length of an environment variable name.
13789 * Advance "arg" to the first character after the name.
13790 * Return 0 for error.
13791 */
13792 static int
13793get_env_len(arg)
13794 char_u **arg;
13795{
13796 char_u *p;
13797 int len;
13798
13799 for (p = *arg; vim_isIDc(*p); ++p)
13800 ;
13801 if (p == *arg) /* no name found */
13802 return 0;
13803
13804 len = (int)(p - *arg);
13805 *arg = p;
13806 return len;
13807}
13808
13809/*
13810 * Get the length of the name of a function or internal variable.
13811 * "arg" is advanced to the first non-white character after the name.
13812 * Return 0 if something is wrong.
13813 */
13814 static int
13815get_id_len(arg)
13816 char_u **arg;
13817{
13818 char_u *p;
13819 int len;
13820
13821 /* Find the end of the name. */
13822 for (p = *arg; eval_isnamec(*p); ++p)
13823 ;
13824 if (p == *arg) /* no name found */
13825 return 0;
13826
13827 len = (int)(p - *arg);
13828 *arg = skipwhite(p);
13829
13830 return len;
13831}
13832
13833/*
Bram Moolenaara7043832005-01-21 11:56:39 +000013834 * Get the length of the name of a variable or function.
13835 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013837 * Return -1 if curly braces expansion failed.
13838 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839 * If the name contains 'magic' {}'s, expand them and return the
13840 * expanded name in an allocated string via 'alias' - caller must free.
13841 */
13842 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013843get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844 char_u **arg;
13845 char_u **alias;
13846 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013847 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848{
13849 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850 char_u *p;
13851 char_u *expr_start;
13852 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853
13854 *alias = NULL; /* default to no alias */
13855
13856 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
13857 && (*arg)[2] == (int)KE_SNR)
13858 {
13859 /* hard coded <SNR>, already translated */
13860 *arg += 3;
13861 return get_id_len(arg) + 3;
13862 }
13863 len = eval_fname_script(*arg);
13864 if (len > 0)
13865 {
13866 /* literal "<SID>", "s:" or "<SNR>" */
13867 *arg += len;
13868 }
13869
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013871 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013873 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874 if (expr_start != NULL)
13875 {
13876 char_u *temp_string;
13877
13878 if (!evaluate)
13879 {
13880 len += (int)(p - *arg);
13881 *arg = skipwhite(p);
13882 return len;
13883 }
13884
13885 /*
13886 * Include any <SID> etc in the expanded string:
13887 * Thus the -len here.
13888 */
13889 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
13890 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013891 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892 *alias = temp_string;
13893 *arg = skipwhite(p);
13894 return (int)STRLEN(temp_string);
13895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896
13897 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013898 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899 EMSG2(_(e_invexpr2), *arg);
13900
13901 return len;
13902}
13903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013904/*
13905 * Find the end of a variable or function name, taking care of magic braces.
13906 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
13907 * start and end of the first magic braces item.
13908 * Return a pointer to just after the name. Equal to "arg" if there is no
13909 * valid name.
13910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013912find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 char_u *arg;
13914 char_u **expr_start;
13915 char_u **expr_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013916 int incl_br; /* Include [] indexes and .name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013918 int mb_nest = 0;
13919 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920 char_u *p;
13921
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013922 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013924 *expr_start = NULL;
13925 *expr_end = NULL;
13926 }
13927
13928 for (p = arg; *p != NUL
13929 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013930 || *p == '{'
Bram Moolenaar8c711452005-01-14 21:53:12 +000013931 || (incl_br && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013932 || mb_nest != 0
13933 || br_nest != 0); ++p)
13934 {
13935 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013937 if (*p == '[')
13938 ++br_nest;
13939 else if (*p == ']')
13940 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013942 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013944 if (*p == '{')
13945 {
13946 mb_nest++;
13947 if (expr_start != NULL && *expr_start == NULL)
13948 *expr_start = p;
13949 }
13950 else if (*p == '}')
13951 {
13952 mb_nest--;
13953 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
13954 *expr_end = p;
13955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957 }
13958
13959 return p;
13960}
13961
13962/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013963 * Expands out the 'magic' {}'s in a variable/function name.
13964 * Note that this can call itself recursively, to deal with
13965 * constructs like foo{bar}{baz}{bam}
13966 * The four pointer arguments point to "foo{expre}ss{ion}bar"
13967 * "in_start" ^
13968 * "expr_start" ^
13969 * "expr_end" ^
13970 * "in_end" ^
13971 *
13972 * Returns a new allocated string, which the caller must free.
13973 * Returns NULL for failure.
13974 */
13975 static char_u *
13976make_expanded_name(in_start, expr_start, expr_end, in_end)
13977 char_u *in_start;
13978 char_u *expr_start;
13979 char_u *expr_end;
13980 char_u *in_end;
13981{
13982 char_u c1;
13983 char_u *retval = NULL;
13984 char_u *temp_result;
13985 char_u *nextcmd = NULL;
13986
13987 if (expr_end == NULL || in_end == NULL)
13988 return NULL;
13989 *expr_start = NUL;
13990 *expr_end = NUL;
13991 c1 = *in_end;
13992 *in_end = NUL;
13993
13994 temp_result = eval_to_string(expr_start + 1, &nextcmd);
13995 if (temp_result != NULL && nextcmd == NULL)
13996 {
13997 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
13998 + (in_end - expr_end) + 1));
13999 if (retval != NULL)
14000 {
14001 STRCPY(retval, in_start);
14002 STRCAT(retval, temp_result);
14003 STRCAT(retval, expr_end + 1);
14004 }
14005 }
14006 vim_free(temp_result);
14007
14008 *in_end = c1; /* put char back for error messages */
14009 *expr_start = '{';
14010 *expr_end = '}';
14011
14012 if (retval != NULL)
14013 {
14014 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
14015 if (expr_start != NULL)
14016 {
14017 /* Further expansion! */
14018 temp_result = make_expanded_name(retval, expr_start,
14019 expr_end, temp_result);
14020 vim_free(retval);
14021 retval = temp_result;
14022 }
14023 }
14024
14025 return retval;
14026}
14027
14028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014030 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 */
14032 static int
14033eval_isnamec(c)
14034 int c;
14035{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014036 return (ASCII_ISALNUM(c) || c == '_' || c == ':');
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037}
14038
14039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040 * Set number v: variable to "val".
14041 */
14042 void
14043set_vim_var_nr(idx, val)
14044 int idx;
14045 long val;
14046{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014047 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048}
14049
14050/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014051 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052 */
14053 long
14054get_vim_var_nr(idx)
14055 int idx;
14056{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014057 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058}
14059
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014060#if defined(FEAT_AUTOCMD) || defined(PROTO)
14061/*
14062 * Get string v: variable value. Uses a static buffer, can only be used once.
14063 */
14064 char_u *
14065get_vim_var_str(idx)
14066 int idx;
14067{
14068 return get_tv_string(&vimvars[idx].vv_tv);
14069}
14070#endif
14071
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072/*
14073 * Set v:count, v:count1 and v:prevcount.
14074 */
14075 void
14076set_vcount(count, count1)
14077 long count;
14078 long count1;
14079{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014080 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
14081 vimvars[VV_COUNT].vv_nr = count;
14082 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083}
14084
14085/*
14086 * Set string v: variable to a copy of "val".
14087 */
14088 void
14089set_vim_var_string(idx, val, len)
14090 int idx;
14091 char_u *val;
14092 int len; /* length of "val" to use or -1 (whole string) */
14093{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014094 /* Need to do this (at least) once, since we can't initialize a union.
14095 * Will always be invoked when "v:progname" is set. */
14096 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
14097
Bram Moolenaare9a41262005-01-15 22:18:47 +000014098 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014100 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014102 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000014104 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105}
14106
14107/*
14108 * Set v:register if needed.
14109 */
14110 void
14111set_reg_var(c)
14112 int c;
14113{
14114 char_u regname;
14115
14116 if (c == 0 || c == ' ')
14117 regname = '"';
14118 else
14119 regname = c;
14120 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014121 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 set_vim_var_string(VV_REG, &regname, 1);
14123}
14124
14125/*
14126 * Get or set v:exception. If "oldval" == NULL, return the current value.
14127 * Otherwise, restore the value to "oldval" and return NULL.
14128 * Must always be called in pairs to save and restore v:exception! Does not
14129 * take care of memory allocations.
14130 */
14131 char_u *
14132v_exception(oldval)
14133 char_u *oldval;
14134{
14135 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014136 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137
Bram Moolenaare9a41262005-01-15 22:18:47 +000014138 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139 return NULL;
14140}
14141
14142/*
14143 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
14144 * Otherwise, restore the value to "oldval" and return NULL.
14145 * Must always be called in pairs to save and restore v:throwpoint! Does not
14146 * take care of memory allocations.
14147 */
14148 char_u *
14149v_throwpoint(oldval)
14150 char_u *oldval;
14151{
14152 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014153 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154
Bram Moolenaare9a41262005-01-15 22:18:47 +000014155 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156 return NULL;
14157}
14158
14159#if defined(FEAT_AUTOCMD) || defined(PROTO)
14160/*
14161 * Set v:cmdarg.
14162 * If "eap" != NULL, use "eap" to generate the value and return the old value.
14163 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
14164 * Must always be called in pairs!
14165 */
14166 char_u *
14167set_cmdarg(eap, oldarg)
14168 exarg_T *eap;
14169 char_u *oldarg;
14170{
14171 char_u *oldval;
14172 char_u *newval;
14173 unsigned len;
14174
Bram Moolenaare9a41262005-01-15 22:18:47 +000014175 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014176 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014178 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000014179 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014180 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181 }
14182
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014183 if (eap->force_bin == FORCE_BIN)
14184 len = 6;
14185 else if (eap->force_bin == FORCE_NOBIN)
14186 len = 8;
14187 else
14188 len = 0;
14189 if (eap->force_ff != 0)
14190 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
14191# ifdef FEAT_MBYTE
14192 if (eap->force_enc != 0)
14193 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
14194# endif
14195
14196 newval = alloc(len + 1);
14197 if (newval == NULL)
14198 return NULL;
14199
14200 if (eap->force_bin == FORCE_BIN)
14201 sprintf((char *)newval, " ++bin");
14202 else if (eap->force_bin == FORCE_NOBIN)
14203 sprintf((char *)newval, " ++nobin");
14204 else
14205 *newval = NUL;
14206 if (eap->force_ff != 0)
14207 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
14208 eap->cmd + eap->force_ff);
14209# ifdef FEAT_MBYTE
14210 if (eap->force_enc != 0)
14211 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
14212 eap->cmd + eap->force_enc);
14213# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000014214 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014215 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216}
14217#endif
14218
14219/*
14220 * Get the value of internal variable "name".
14221 * Return OK or FAIL.
14222 */
14223 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014224get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225 char_u *name;
14226 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000014227 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014228 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229{
14230 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000014231 typval_T *tv = NULL;
14232 typval_T atv;
14233 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235
14236 /* truncate the name, so that we can use strcmp() */
14237 cc = name[len];
14238 name[len] = NUL;
14239
14240 /*
14241 * Check for "b:changedtick".
14242 */
14243 if (STRCMP(name, "b:changedtick") == 0)
14244 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000014245 atv.v_type = VAR_NUMBER;
14246 atv.vval.v_number = curbuf->b_changedtick;
14247 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248 }
14249
14250 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251 * Check for user-defined variables.
14252 */
14253 else
14254 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014255 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014257 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258 }
14259
Bram Moolenaare9a41262005-01-15 22:18:47 +000014260 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014262 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014263 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264 ret = FAIL;
14265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014266 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014267 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268
14269 name[len] = cc;
14270
14271 return ret;
14272}
14273
14274/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014275 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
14276 * Also handle function call with Funcref variable: func(expr)
14277 * Can all be combined: dict.func(expr)[idx]['func'](expr)
14278 */
14279 static int
14280handle_subscript(arg, rettv, evaluate, verbose)
14281 char_u **arg;
14282 typval_T *rettv;
14283 int evaluate; /* do more than finding the end */
14284 int verbose; /* give error messages */
14285{
14286 int ret = OK;
14287 dict_T *selfdict = NULL;
14288 char_u *s;
14289 int len;
14290
14291 while (ret == OK
14292 && (**arg == '['
14293 || (**arg == '.' && rettv->v_type == VAR_DICT)
14294 || (**arg == '(' && rettv->v_type == VAR_FUNC))
14295 && !vim_iswhite(*(*arg - 1)))
14296 {
14297 if (**arg == '(')
14298 {
14299 s = rettv->vval.v_string;
14300
14301 /* Invoke the function. Recursive! */
14302 ret = get_func_tv(s, STRLEN(s), rettv, arg,
14303 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
14304 &len, evaluate, selfdict);
14305
14306 /* Stop the expression evaluation when immediately aborting on
14307 * error, or when an interrupt occurred or an exception was thrown
14308 * but not caught. */
14309 if (aborting())
14310 {
14311 if (ret == OK)
14312 clear_tv(rettv);
14313 ret = FAIL;
14314 }
14315 dict_unref(selfdict);
14316 selfdict = NULL;
14317 }
14318 else /* **arg == '[' || **arg == '.' */
14319 {
14320 dict_unref(selfdict);
14321 if (rettv->v_type == VAR_DICT)
14322 {
14323 selfdict = rettv->vval.v_dict;
14324 if (selfdict != NULL)
14325 ++selfdict->dv_refcount;
14326 }
14327 else
14328 selfdict = NULL;
14329 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
14330 {
14331 clear_tv(rettv);
14332 ret = FAIL;
14333 }
14334 }
14335 }
14336 dict_unref(selfdict);
14337 return ret;
14338}
14339
14340/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014341 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
14342 * value).
14343 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014344 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014345alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014346{
Bram Moolenaar33570922005-01-25 22:26:29 +000014347 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014348}
14349
14350/*
14351 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352 * The string "s" must have been allocated, it is consumed.
14353 * Return NULL for out of memory, the variable otherwise.
14354 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014355 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014356alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357 char_u *s;
14358{
Bram Moolenaar33570922005-01-25 22:26:29 +000014359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014361 rettv = alloc_tv();
14362 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014364 rettv->v_type = VAR_STRING;
14365 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 }
14367 else
14368 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014369 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370}
14371
14372/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014373 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374 */
14375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014376free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014377 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378{
14379 if (varp != NULL)
14380 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014381 switch (varp->v_type)
14382 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014383 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014384 func_unref(varp->vval.v_string);
14385 /*FALLTHROUGH*/
14386 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014387 vim_free(varp->vval.v_string);
14388 break;
14389 case VAR_LIST:
14390 list_unref(varp->vval.v_list);
14391 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014392 case VAR_DICT:
14393 dict_unref(varp->vval.v_dict);
14394 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014395 case VAR_NUMBER:
14396 case VAR_UNKNOWN:
14397 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014398 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014399 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014400 break;
14401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402 vim_free(varp);
14403 }
14404}
14405
14406/*
14407 * Free the memory for a variable value and set the value to NULL or 0.
14408 */
14409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014410clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014411 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412{
14413 if (varp != NULL)
14414 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014415 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014417 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014418 func_unref(varp->vval.v_string);
14419 /*FALLTHROUGH*/
14420 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014421 vim_free(varp->vval.v_string);
14422 varp->vval.v_string = NULL;
14423 break;
14424 case VAR_LIST:
14425 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014426 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014427 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014428 case VAR_DICT:
14429 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014430 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014431 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014432 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014433 varp->vval.v_number = 0;
14434 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014435 case VAR_UNKNOWN:
14436 break;
14437 default:
14438 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014440 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441 }
14442}
14443
14444/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014445 * Set the value of a variable to NULL without freeing items.
14446 */
14447 static void
14448init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014449 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014450{
14451 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014452 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014453}
14454
14455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 * Get the number value of a variable.
14457 * If it is a String variable, uses vim_str2nr().
14458 */
14459 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014460get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014461 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014463 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014465 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014467 case VAR_NUMBER:
14468 n = (long)(varp->vval.v_number);
14469 break;
14470 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014471 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014472 break;
14473 case VAR_STRING:
14474 if (varp->vval.v_string != NULL)
14475 vim_str2nr(varp->vval.v_string, NULL, NULL,
14476 TRUE, TRUE, &n, NULL);
14477 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014478 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014479 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014480 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014481 case VAR_DICT:
14482 EMSG(_("E728: Using a Dictionary as a number"));
14483 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014484 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014485 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014486 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014488 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489}
14490
14491/*
14492 * Get the lnum from the first argument. Also accepts ".", "$", etc.
14493 */
14494 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014495get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000014496 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497{
Bram Moolenaar33570922005-01-25 22:26:29 +000014498 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 linenr_T lnum;
14500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014501 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 if (lnum == 0) /* no valid number, try using line() */
14503 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014504 rettv.v_type = VAR_NUMBER;
14505 f_line(argvars, &rettv);
14506 lnum = rettv.vval.v_number;
14507 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508 }
14509 return lnum;
14510}
14511
14512/*
14513 * Get the string value of a variable.
14514 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000014515 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
14516 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517 * If the String variable has never been set, return an empty string.
14518 * Never returns NULL;
14519 */
14520 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014521get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014522 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523{
14524 static char_u mybuf[NUMBUFLEN];
14525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014526 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527}
14528
14529 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014530get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000014531 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014532 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014534 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014536 case VAR_NUMBER:
14537 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
14538 return buf;
14539 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014540 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014541 break;
14542 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014543 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014544 break;
14545 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014546 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014547 break;
14548 case VAR_STRING:
14549 if (varp->vval.v_string != NULL)
14550 return varp->vval.v_string;
14551 break;
14552 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014553 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014554 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014556 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014557}
14558
14559/*
14560 * Find variable "name" in the list of variables.
14561 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014562 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014563 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000014564 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014566 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014567find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014569 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014571 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014572 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573
Bram Moolenaara7043832005-01-21 11:56:39 +000014574 ht = find_var_ht(name, &varname);
14575 if (htp != NULL)
14576 *htp = ht;
14577 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014579 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580}
14581
14582/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014583 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000014584 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014586 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014587find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000014588 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000014589 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014590 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000014591{
Bram Moolenaar33570922005-01-25 22:26:29 +000014592 hashitem_T *hi;
14593
14594 if (*varname == NUL)
14595 {
14596 /* Must be something like "s:", otherwise "ht" would be NULL. */
14597 switch (varname[-2])
14598 {
14599 case 's': return &SCRIPT_SV(current_SID).sv_var;
14600 case 'g': return &globvars_var;
14601 case 'v': return &vimvars_var;
14602 case 'b': return &curbuf->b_bufvar;
14603 case 'w': return &curwin->w_winvar;
14604 case 'l': return &current_funccal->l_vars_var;
14605 case 'a': return &current_funccal->l_avars_var;
14606 }
14607 return NULL;
14608 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014609
14610 hi = hash_find(ht, varname);
14611 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014612 {
14613 /* For global variables we may try auto-loading the script. If it
14614 * worked find the variable again. */
14615 if (ht == &globvarht && !writing
14616 && script_autoload(varname) && !aborting())
14617 hi = hash_find(ht, varname);
14618 if (HASHITEM_EMPTY(hi))
14619 return NULL;
14620 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014621 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014622}
14623
14624/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014625 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014626 * Set "varname" to the start of name without ':'.
14627 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014628 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014629find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630 char_u *name;
14631 char_u **varname;
14632{
14633 if (name[1] != ':')
14634 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014635 /* The name must not start with a colon. */
14636 if (name[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637 return NULL;
14638 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014639
14640 /* "version" is "v:version" in all scopes */
14641 if (!HASHITEM_EMPTY(hash_find(&compat_hashtab, name)))
14642 return &compat_hashtab;
14643
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014645 return &globvarht; /* global variable */
14646 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647 }
14648 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014649 if (*name == 'g') /* global variable */
14650 return &globvarht;
14651 /* There must be no ':' in the rest of the name, unless g: is used */
14652 if (vim_strchr(name + 2, ':') != NULL)
14653 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014655 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014657 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000014658 if (*name == 'v') /* v: variable */
14659 return &vimvarht;
14660 if (*name == 'a' && current_funccal != NULL) /* function argument */
14661 return &current_funccal->l_avars.dv_hashtab;
14662 if (*name == 'l' && current_funccal != NULL) /* local function variable */
14663 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664 if (*name == 's' /* script variable */
14665 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
14666 return &SCRIPT_VARS(current_SID);
14667 return NULL;
14668}
14669
14670/*
14671 * Get the string value of a (global/local) variable.
14672 * Returns NULL when it doesn't exist.
14673 */
14674 char_u *
14675get_var_value(name)
14676 char_u *name;
14677{
Bram Moolenaar33570922005-01-25 22:26:29 +000014678 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679
Bram Moolenaara7043832005-01-21 11:56:39 +000014680 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681 if (v == NULL)
14682 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014683 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684}
14685
14686/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014687 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688 * sourcing this script and when executing functions defined in the script.
14689 */
14690 void
14691new_script_vars(id)
14692 scid_T id;
14693{
Bram Moolenaara7043832005-01-21 11:56:39 +000014694 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000014695 hashtab_T *ht;
14696 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000014697
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
14699 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014700 /* Re-allocating ga_data means that an ht_array pointing to
14701 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000014702 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000014703 for (i = 1; i <= ga_scripts.ga_len; ++i)
14704 {
14705 ht = &SCRIPT_VARS(i);
14706 if (ht->ht_mask == HT_INIT_SIZE - 1)
14707 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000014708 sv = &SCRIPT_SV(i);
14709 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000014710 }
14711
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712 while (ga_scripts.ga_len < id)
14713 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014714 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
14715 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717 }
14718 }
14719}
14720
14721/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014722 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
14723 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724 */
14725 void
Bram Moolenaar33570922005-01-25 22:26:29 +000014726init_var_dict(dict, dict_var)
14727 dict_T *dict;
14728 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729{
Bram Moolenaar33570922005-01-25 22:26:29 +000014730 hash_init(&dict->dv_hashtab);
14731 dict->dv_refcount = 99999;
14732 dict_var->di_tv.vval.v_dict = dict;
14733 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014734 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014735 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
14736 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737}
14738
14739/*
14740 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000014741 * Frees all allocated variables and the value they contain.
14742 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743 */
14744 void
Bram Moolenaara7043832005-01-21 11:56:39 +000014745vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000014746 hashtab_T *ht;
14747{
14748 vars_clear_ext(ht, TRUE);
14749}
14750
14751/*
14752 * Like vars_clear(), but only free the value if "free_val" is TRUE.
14753 */
14754 static void
14755vars_clear_ext(ht, free_val)
14756 hashtab_T *ht;
14757 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758{
Bram Moolenaara7043832005-01-21 11:56:39 +000014759 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000014760 hashitem_T *hi;
14761 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014762
Bram Moolenaar33570922005-01-25 22:26:29 +000014763 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000014764 todo = ht->ht_used;
14765 for (hi = ht->ht_array; todo > 0; ++hi)
14766 {
14767 if (!HASHITEM_EMPTY(hi))
14768 {
14769 --todo;
14770
Bram Moolenaar33570922005-01-25 22:26:29 +000014771 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000014772 * ht_array might change then. hash_clear() takes care of it
14773 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014774 v = HI2DI(hi);
14775 if (free_val)
14776 clear_tv(&v->di_tv);
14777 if ((v->di_flags & DI_FLAGS_FIX) == 0)
14778 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000014779 }
14780 }
14781 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782}
14783
Bram Moolenaara7043832005-01-21 11:56:39 +000014784/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014785 * Delete a variable from hashtab "ht" at item "hi".
14786 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000014787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000014789delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000014790 hashtab_T *ht;
14791 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792{
Bram Moolenaar33570922005-01-25 22:26:29 +000014793 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014794
14795 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000014796 clear_tv(&di->di_tv);
14797 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798}
14799
14800/*
14801 * List the value of one internal variable.
14802 */
14803 static void
14804list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000014805 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806 char_u *prefix;
14807{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014808 char_u *tofree;
14809 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014810 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014811
Bram Moolenaar33570922005-01-25 22:26:29 +000014812 s = echo_string(&v->di_tv, &tofree, numbuf);
14813 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014814 s == NULL ? (char_u *)"" : s);
14815 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816}
14817
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818 static void
14819list_one_var_a(prefix, name, type, string)
14820 char_u *prefix;
14821 char_u *name;
14822 int type;
14823 char_u *string;
14824{
14825 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
14826 if (name != NULL) /* "a:" vars don't have a name stored */
14827 msg_puts(name);
14828 msg_putchar(' ');
14829 msg_advance(22);
14830 if (type == VAR_NUMBER)
14831 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014832 else if (type == VAR_FUNC)
14833 msg_putchar('*');
14834 else if (type == VAR_LIST)
14835 {
14836 msg_putchar('[');
14837 if (*string == '[')
14838 ++string;
14839 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014840 else if (type == VAR_DICT)
14841 {
14842 msg_putchar('{');
14843 if (*string == '{')
14844 ++string;
14845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014846 else
14847 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014848
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014850
14851 if (type == VAR_FUNC)
14852 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853}
14854
14855/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014856 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857 * If the variable already exists, the value is updated.
14858 * Otherwise the variable is created.
14859 */
14860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014861set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014863 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014864 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014865{
Bram Moolenaar33570922005-01-25 22:26:29 +000014866 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014868 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014869
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014870 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014871 {
14872 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
14873 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
14874 ? name[2] : name[0]))
14875 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014876 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014877 return;
14878 }
14879 if (function_exists(name))
14880 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014881 EMSG2(_("705: Variable name conflicts with existing function: %s"),
14882 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014883 return;
14884 }
14885 }
14886
Bram Moolenaara7043832005-01-21 11:56:39 +000014887 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014888 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000014889 {
14890 EMSG2(_("E461: Illegal variable name: %s"), name);
14891 return;
14892 }
14893
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014894 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014895 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014896 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014897 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014898 if (var_check_ro(v->di_flags, name)
14899 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000014900 return;
14901 if (v->di_tv.v_type != tv->v_type
14902 && !((v->di_tv.v_type == VAR_STRING
14903 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014904 && (tv->v_type == VAR_STRING
14905 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014906 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014907 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014908 return;
14909 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014910
14911 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000014912 * Handle setting internal v: variables separately: we don't change
14913 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000014914 */
14915 if (ht == &vimvarht)
14916 {
14917 if (v->di_tv.v_type == VAR_STRING)
14918 {
14919 vim_free(v->di_tv.vval.v_string);
14920 if (copy || tv->v_type != VAR_STRING)
14921 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
14922 else
14923 {
14924 /* Take over the string to avoid an extra alloc/free. */
14925 v->di_tv.vval.v_string = tv->vval.v_string;
14926 tv->vval.v_string = NULL;
14927 }
14928 }
14929 else if (v->di_tv.v_type != VAR_NUMBER)
14930 EMSG2(_(e_intern2), "set_var()");
14931 else
14932 v->di_tv.vval.v_number = get_tv_number(tv);
14933 return;
14934 }
14935
14936 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937 }
14938 else /* add a new variable */
14939 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014940 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
14941 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000014942 if (v == NULL)
14943 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000014944 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014945 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014947 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014948 return;
14949 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014950 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014953 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000014954 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014955 else
14956 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014957 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014958 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014959 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961}
14962
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014963/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014964 * Return TRUE if di_flags "flags" indicate read-only variable "name".
14965 * Also give an error message.
14966 */
14967 static int
14968var_check_ro(flags, name)
14969 int flags;
14970 char_u *name;
14971{
14972 if (flags & DI_FLAGS_RO)
14973 {
14974 EMSG2(_(e_readonlyvar), name);
14975 return TRUE;
14976 }
14977 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
14978 {
14979 EMSG2(_(e_readonlysbx), name);
14980 return TRUE;
14981 }
14982 return FALSE;
14983}
14984
14985/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014986 * Return TRUE if typeval "tv" is set to be locked (immutable).
14987 * Also give an error message, using "name".
14988 */
14989 static int
14990tv_check_lock(lock, name)
14991 int lock;
14992 char_u *name;
14993{
14994 if (lock & VAR_LOCKED)
14995 {
14996 EMSG2(_("E741: Value is locked: %s"),
14997 name == NULL ? (char_u *)_("Unknown") : name);
14998 return TRUE;
14999 }
15000 if (lock & VAR_FIXED)
15001 {
15002 EMSG2(_("E742: Cannot change value of %s"),
15003 name == NULL ? (char_u *)_("Unknown") : name);
15004 return TRUE;
15005 }
15006 return FALSE;
15007}
15008
15009/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015010 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015011 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015012 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015015copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000015016 typval_T *from;
15017 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015019 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015020 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015021 switch (from->v_type)
15022 {
15023 case VAR_NUMBER:
15024 to->vval.v_number = from->vval.v_number;
15025 break;
15026 case VAR_STRING:
15027 case VAR_FUNC:
15028 if (from->vval.v_string == NULL)
15029 to->vval.v_string = NULL;
15030 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015031 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015032 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015033 if (from->v_type == VAR_FUNC)
15034 func_ref(to->vval.v_string);
15035 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015036 break;
15037 case VAR_LIST:
15038 if (from->vval.v_list == NULL)
15039 to->vval.v_list = NULL;
15040 else
15041 {
15042 to->vval.v_list = from->vval.v_list;
15043 ++to->vval.v_list->lv_refcount;
15044 }
15045 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015046 case VAR_DICT:
15047 if (from->vval.v_dict == NULL)
15048 to->vval.v_dict = NULL;
15049 else
15050 {
15051 to->vval.v_dict = from->vval.v_dict;
15052 ++to->vval.v_dict->dv_refcount;
15053 }
15054 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015055 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015056 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015057 break;
15058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059}
15060
15061/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000015062 * Make a copy of an item.
15063 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015064 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
15065 * reference to an already copied list/dict can be used.
15066 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015067 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015068 static int
15069item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000015070 typval_T *from;
15071 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015072 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015073 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015074{
15075 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015076 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015077
Bram Moolenaar33570922005-01-25 22:26:29 +000015078 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015079 {
15080 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015081 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015082 }
15083 ++recurse;
15084
15085 switch (from->v_type)
15086 {
15087 case VAR_NUMBER:
15088 case VAR_STRING:
15089 case VAR_FUNC:
15090 copy_tv(from, to);
15091 break;
15092 case VAR_LIST:
15093 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015094 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015095 if (from->vval.v_list == NULL)
15096 to->vval.v_list = NULL;
15097 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
15098 {
15099 /* use the copy made earlier */
15100 to->vval.v_list = from->vval.v_list->lv_copylist;
15101 ++to->vval.v_list->lv_refcount;
15102 }
15103 else
15104 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
15105 if (to->vval.v_list == NULL)
15106 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015107 break;
15108 case VAR_DICT:
15109 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015110 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015111 if (from->vval.v_dict == NULL)
15112 to->vval.v_dict = NULL;
15113 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
15114 {
15115 /* use the copy made earlier */
15116 to->vval.v_dict = from->vval.v_dict->dv_copydict;
15117 ++to->vval.v_dict->dv_refcount;
15118 }
15119 else
15120 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
15121 if (to->vval.v_dict == NULL)
15122 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015123 break;
15124 default:
15125 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015126 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015127 }
15128 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015129 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015130}
15131
15132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133 * ":echo expr1 ..." print each argument separated with a space, add a
15134 * newline at the end.
15135 * ":echon expr1 ..." print each argument plain.
15136 */
15137 void
15138ex_echo(eap)
15139 exarg_T *eap;
15140{
15141 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015142 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015143 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144 char_u *p;
15145 int needclr = TRUE;
15146 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015147 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148
15149 if (eap->skip)
15150 ++emsg_skip;
15151 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
15152 {
15153 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015154 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155 {
15156 /*
15157 * Report the invalid expression unless the expression evaluation
15158 * has been cancelled due to an aborting error, an interrupt, or an
15159 * exception.
15160 */
15161 if (!aborting())
15162 EMSG2(_(e_invexpr2), p);
15163 break;
15164 }
15165 if (!eap->skip)
15166 {
15167 if (atstart)
15168 {
15169 atstart = FALSE;
15170 /* Call msg_start() after eval1(), evaluating the expression
15171 * may cause a message to appear. */
15172 if (eap->cmdidx == CMD_echo)
15173 msg_start();
15174 }
15175 else if (eap->cmdidx == CMD_echo)
15176 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015177 p = echo_string(&rettv, &tofree, numbuf);
15178 if (p != NULL)
15179 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015181 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015183 if (*p != TAB && needclr)
15184 {
15185 /* remove any text still there from the command */
15186 msg_clr_eos();
15187 needclr = FALSE;
15188 }
15189 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190 }
15191 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015192 {
15193#ifdef FEAT_MBYTE
15194 if (has_mbyte)
15195 {
15196 int i = (*mb_ptr2len_check)(p);
15197
15198 (void)msg_outtrans_len_attr(p, i, echo_attr);
15199 p += i - 1;
15200 }
15201 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015203 (void)msg_outtrans_len_attr(p, 1, echo_attr);
15204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015206 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015207 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015208 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209 arg = skipwhite(arg);
15210 }
15211 eap->nextcmd = check_nextcmd(arg);
15212
15213 if (eap->skip)
15214 --emsg_skip;
15215 else
15216 {
15217 /* remove text that may still be there from the command */
15218 if (needclr)
15219 msg_clr_eos();
15220 if (eap->cmdidx == CMD_echo)
15221 msg_end();
15222 }
15223}
15224
15225/*
15226 * ":echohl {name}".
15227 */
15228 void
15229ex_echohl(eap)
15230 exarg_T *eap;
15231{
15232 int id;
15233
15234 id = syn_name2id(eap->arg);
15235 if (id == 0)
15236 echo_attr = 0;
15237 else
15238 echo_attr = syn_id2attr(id);
15239}
15240
15241/*
15242 * ":execute expr1 ..." execute the result of an expression.
15243 * ":echomsg expr1 ..." Print a message
15244 * ":echoerr expr1 ..." Print an error
15245 * Each gets spaces around each argument and a newline at the end for
15246 * echo commands
15247 */
15248 void
15249ex_execute(eap)
15250 exarg_T *eap;
15251{
15252 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015253 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015254 int ret = OK;
15255 char_u *p;
15256 garray_T ga;
15257 int len;
15258 int save_did_emsg;
15259
15260 ga_init2(&ga, 1, 80);
15261
15262 if (eap->skip)
15263 ++emsg_skip;
15264 while (*arg != NUL && *arg != '|' && *arg != '\n')
15265 {
15266 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015267 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268 {
15269 /*
15270 * Report the invalid expression unless the expression evaluation
15271 * has been cancelled due to an aborting error, an interrupt, or an
15272 * exception.
15273 */
15274 if (!aborting())
15275 EMSG2(_(e_invexpr2), p);
15276 ret = FAIL;
15277 break;
15278 }
15279
15280 if (!eap->skip)
15281 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015282 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015283 len = (int)STRLEN(p);
15284 if (ga_grow(&ga, len + 2) == FAIL)
15285 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015286 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287 ret = FAIL;
15288 break;
15289 }
15290 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015292 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015293 ga.ga_len += len;
15294 }
15295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015296 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015297 arg = skipwhite(arg);
15298 }
15299
15300 if (ret != FAIL && ga.ga_data != NULL)
15301 {
15302 if (eap->cmdidx == CMD_echomsg)
15303 MSG_ATTR(ga.ga_data, echo_attr);
15304 else if (eap->cmdidx == CMD_echoerr)
15305 {
15306 /* We don't want to abort following commands, restore did_emsg. */
15307 save_did_emsg = did_emsg;
15308 EMSG((char_u *)ga.ga_data);
15309 if (!force_abort)
15310 did_emsg = save_did_emsg;
15311 }
15312 else if (eap->cmdidx == CMD_execute)
15313 do_cmdline((char_u *)ga.ga_data,
15314 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
15315 }
15316
15317 ga_clear(&ga);
15318
15319 if (eap->skip)
15320 --emsg_skip;
15321
15322 eap->nextcmd = check_nextcmd(arg);
15323}
15324
15325/*
15326 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
15327 * "arg" points to the "&" or '+' when called, to "option" when returning.
15328 * Returns NULL when no option name found. Otherwise pointer to the char
15329 * after the option name.
15330 */
15331 static char_u *
15332find_option_end(arg, opt_flags)
15333 char_u **arg;
15334 int *opt_flags;
15335{
15336 char_u *p = *arg;
15337
15338 ++p;
15339 if (*p == 'g' && p[1] == ':')
15340 {
15341 *opt_flags = OPT_GLOBAL;
15342 p += 2;
15343 }
15344 else if (*p == 'l' && p[1] == ':')
15345 {
15346 *opt_flags = OPT_LOCAL;
15347 p += 2;
15348 }
15349 else
15350 *opt_flags = 0;
15351
15352 if (!ASCII_ISALPHA(*p))
15353 return NULL;
15354 *arg = p;
15355
15356 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
15357 p += 4; /* termcap option */
15358 else
15359 while (ASCII_ISALPHA(*p))
15360 ++p;
15361 return p;
15362}
15363
15364/*
15365 * ":function"
15366 */
15367 void
15368ex_function(eap)
15369 exarg_T *eap;
15370{
15371 char_u *theline;
15372 int j;
15373 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375 char_u *name = NULL;
15376 char_u *p;
15377 char_u *arg;
15378 garray_T newargs;
15379 garray_T newlines;
15380 int varargs = FALSE;
15381 int mustend = FALSE;
15382 int flags = 0;
15383 ufunc_T *fp;
15384 int indent;
15385 int nesting;
15386 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015387 dictitem_T *v;
15388 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015389 static int func_nr = 0; /* number for nameless function */
15390 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015391 hashtab_T *ht;
15392 int todo;
15393 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394
15395 /*
15396 * ":function" without argument: list functions.
15397 */
15398 if (ends_excmd(*eap->arg))
15399 {
15400 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015401 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000015402 todo = func_hashtab.ht_used;
15403 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015404 {
15405 if (!HASHITEM_EMPTY(hi))
15406 {
15407 --todo;
15408 fp = HI2UF(hi);
15409 if (!isdigit(*fp->uf_name))
15410 list_func_head(fp, FALSE);
15411 }
15412 }
15413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 eap->nextcmd = check_nextcmd(eap->arg);
15415 return;
15416 }
15417
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015418 /*
15419 * Get the function name. There are these situations:
15420 * func normal function name
15421 * "name" == func, "fudi.fd_dict" == NULL
15422 * dict.func new dictionary entry
15423 * "name" == NULL, "fudi.fd_dict" set,
15424 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
15425 * dict.func existing dict entry with a Funcref
15426 * "name" == fname, "fudi.fd_dict" set,
15427 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15428 * dict.func existing dict entry that's not a Funcref
15429 * "name" == NULL, "fudi.fd_dict" set,
15430 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015432 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015433 name = trans_function_name(&p, eap->skip, 0, &fudi);
15434 paren = (vim_strchr(p, '(') != NULL);
15435 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015436 {
15437 /*
15438 * Return on an invalid expression in braces, unless the expression
15439 * evaluation has been cancelled due to an aborting error, an
15440 * interrupt, or an exception.
15441 */
15442 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015443 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015444 if (!eap->skip && fudi.fd_newkey != NULL)
15445 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015446 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015449 else
15450 eap->skip = TRUE;
15451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452 /* An error in a function call during evaluation of an expression in magic
15453 * braces should not cause the function not to be defined. */
15454 saved_did_emsg = did_emsg;
15455 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015456
15457 /*
15458 * ":function func" with only function name: list function.
15459 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015460 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461 {
15462 if (!ends_excmd(*skipwhite(p)))
15463 {
15464 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015465 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466 }
15467 eap->nextcmd = check_nextcmd(p);
15468 if (eap->nextcmd != NULL)
15469 *p = NUL;
15470 if (!eap->skip && !got_int)
15471 {
15472 fp = find_func(name);
15473 if (fp != NULL)
15474 {
15475 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015476 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477 {
15478 msg_putchar('\n');
15479 msg_outnum((long)(j + 1));
15480 if (j < 9)
15481 msg_putchar(' ');
15482 if (j < 99)
15483 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015484 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485 out_flush(); /* show a line at a time */
15486 ui_breakcheck();
15487 }
15488 if (!got_int)
15489 {
15490 msg_putchar('\n');
15491 msg_puts((char_u *)" endfunction");
15492 }
15493 }
15494 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015495 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015497 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498 }
15499
15500 /*
15501 * ":function name(arg1, arg2)" Define function.
15502 */
15503 p = skipwhite(p);
15504 if (*p != '(')
15505 {
15506 if (!eap->skip)
15507 {
15508 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015509 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510 }
15511 /* attempt to continue by skipping some text */
15512 if (vim_strchr(p, '(') != NULL)
15513 p = vim_strchr(p, '(');
15514 }
15515 p = skipwhite(p + 1);
15516
15517 ga_init2(&newargs, (int)sizeof(char_u *), 3);
15518 ga_init2(&newlines, (int)sizeof(char_u *), 3);
15519
15520 /*
15521 * Isolate the arguments: "arg1, arg2, ...)"
15522 */
15523 while (*p != ')')
15524 {
15525 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
15526 {
15527 varargs = TRUE;
15528 p += 3;
15529 mustend = TRUE;
15530 }
15531 else
15532 {
15533 arg = p;
15534 while (ASCII_ISALNUM(*p) || *p == '_')
15535 ++p;
15536 if (arg == p || isdigit(*arg)
15537 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
15538 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
15539 {
15540 if (!eap->skip)
15541 EMSG2(_("E125: Illegal argument: %s"), arg);
15542 break;
15543 }
15544 if (ga_grow(&newargs, 1) == FAIL)
15545 goto erret;
15546 c = *p;
15547 *p = NUL;
15548 arg = vim_strsave(arg);
15549 if (arg == NULL)
15550 goto erret;
15551 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
15552 *p = c;
15553 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554 if (*p == ',')
15555 ++p;
15556 else
15557 mustend = TRUE;
15558 }
15559 p = skipwhite(p);
15560 if (mustend && *p != ')')
15561 {
15562 if (!eap->skip)
15563 EMSG2(_(e_invarg2), eap->arg);
15564 break;
15565 }
15566 }
15567 ++p; /* skip the ')' */
15568
Bram Moolenaare9a41262005-01-15 22:18:47 +000015569 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570 for (;;)
15571 {
15572 p = skipwhite(p);
15573 if (STRNCMP(p, "range", 5) == 0)
15574 {
15575 flags |= FC_RANGE;
15576 p += 5;
15577 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015578 else if (STRNCMP(p, "dict", 4) == 0)
15579 {
15580 flags |= FC_DICT;
15581 p += 4;
15582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583 else if (STRNCMP(p, "abort", 5) == 0)
15584 {
15585 flags |= FC_ABORT;
15586 p += 5;
15587 }
15588 else
15589 break;
15590 }
15591
15592 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
15593 EMSG(_(e_trailing));
15594
15595 /*
15596 * Read the body of the function, until ":endfunction" is found.
15597 */
15598 if (KeyTyped)
15599 {
15600 /* Check if the function already exists, don't let the user type the
15601 * whole function before telling him it doesn't work! For a script we
15602 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015603 if (!eap->skip && !eap->forceit)
15604 {
15605 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
15606 EMSG(_(e_funcdict));
15607 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015608 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610
15611 msg_putchar('\n'); /* don't overwrite the function name */
15612 cmdline_row = msg_row;
15613 }
15614
15615 indent = 2;
15616 nesting = 0;
15617 for (;;)
15618 {
15619 msg_scroll = TRUE;
15620 need_wait_return = FALSE;
15621 if (eap->getline == NULL)
15622 theline = getcmdline(':', 0L, indent);
15623 else
15624 theline = eap->getline(':', eap->cookie, indent);
15625 if (KeyTyped)
15626 lines_left = Rows - 1;
15627 if (theline == NULL)
15628 {
15629 EMSG(_("E126: Missing :endfunction"));
15630 goto erret;
15631 }
15632
15633 if (skip_until != NULL)
15634 {
15635 /* between ":append" and "." and between ":python <<EOF" and "EOF"
15636 * don't check for ":endfunc". */
15637 if (STRCMP(theline, skip_until) == 0)
15638 {
15639 vim_free(skip_until);
15640 skip_until = NULL;
15641 }
15642 }
15643 else
15644 {
15645 /* skip ':' and blanks*/
15646 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
15647 ;
15648
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015649 /* Check for "endfunction". */
15650 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651 {
15652 vim_free(theline);
15653 break;
15654 }
15655
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015656 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657 * at "end". */
15658 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
15659 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015660 else if (STRNCMP(p, "if", 2) == 0
15661 || STRNCMP(p, "wh", 2) == 0
15662 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663 || STRNCMP(p, "try", 3) == 0)
15664 indent += 2;
15665
15666 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015667 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015669 if (*p == '!')
15670 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 p += eval_fname_script(p);
15672 if (ASCII_ISALPHA(*p))
15673 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015674 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675 if (*skipwhite(p) == '(')
15676 {
15677 ++nesting;
15678 indent += 2;
15679 }
15680 }
15681 }
15682
15683 /* Check for ":append" or ":insert". */
15684 p = skip_range(p, NULL);
15685 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
15686 || (p[0] == 'i'
15687 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
15688 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
15689 skip_until = vim_strsave((char_u *)".");
15690
15691 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
15692 arg = skipwhite(skiptowhite(p));
15693 if (arg[0] == '<' && arg[1] =='<'
15694 && ((p[0] == 'p' && p[1] == 'y'
15695 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
15696 || (p[0] == 'p' && p[1] == 'e'
15697 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
15698 || (p[0] == 't' && p[1] == 'c'
15699 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
15700 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
15701 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000015702 || (p[0] == 'm' && p[1] == 'z'
15703 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704 ))
15705 {
15706 /* ":python <<" continues until a dot, like ":append" */
15707 p = skipwhite(arg + 2);
15708 if (*p == NUL)
15709 skip_until = vim_strsave((char_u *)".");
15710 else
15711 skip_until = vim_strsave(p);
15712 }
15713 }
15714
15715 /* Add the line to the function. */
15716 if (ga_grow(&newlines, 1) == FAIL)
15717 goto erret;
15718 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
15719 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720 }
15721
15722 /* Don't define the function when skipping commands or when an error was
15723 * detected. */
15724 if (eap->skip || did_emsg)
15725 goto erret;
15726
15727 /*
15728 * If there are no errors, add the function
15729 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015730 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015731 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015732 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000015733 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015734 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015735 emsg_funcname("E707: Function name conflicts with variable: %s",
15736 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015737 goto erret;
15738 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015739
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015740 fp = find_func(name);
15741 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015743 if (!eap->forceit)
15744 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015745 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015746 goto erret;
15747 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015748 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015749 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015750 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015751 name);
15752 goto erret;
15753 }
15754 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015755 ga_clear_strings(&(fp->uf_args));
15756 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015757 vim_free(name);
15758 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 }
15761 else
15762 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015763 char numbuf[20];
15764
15765 fp = NULL;
15766 if (fudi.fd_newkey == NULL && !eap->forceit)
15767 {
15768 EMSG(_(e_funcdict));
15769 goto erret;
15770 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000015771 if (fudi.fd_di == NULL)
15772 {
15773 /* Can't add a function to a locked dictionary */
15774 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
15775 goto erret;
15776 }
15777 /* Can't change an existing function if it is locked */
15778 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
15779 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015780
15781 /* Give the function a sequential number. Can only be used with a
15782 * Funcref! */
15783 vim_free(name);
15784 sprintf(numbuf, "%d", ++func_nr);
15785 name = vim_strsave((char_u *)numbuf);
15786 if (name == NULL)
15787 goto erret;
15788 }
15789
15790 if (fp == NULL)
15791 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015792 if (fudi.fd_dict == NULL && vim_strchr(name, ':') != NULL)
15793 {
15794 int slen, plen;
15795 char_u *scriptname;
15796
15797 /* Check that the autoload name matches the script name. */
15798 j = FAIL;
15799 if (sourcing_name != NULL)
15800 {
15801 scriptname = autoload_name(name);
15802 if (scriptname != NULL)
15803 {
15804 p = vim_strchr(scriptname, '/');
15805 plen = STRLEN(p);
15806 slen = STRLEN(sourcing_name);
15807 if (slen > plen && fnamecmp(p,
15808 sourcing_name + slen - plen) == 0)
15809 j = OK;
15810 vim_free(scriptname);
15811 }
15812 }
15813 if (j == FAIL)
15814 {
15815 EMSG2(_("E746: Function name does not match script file name: %s"), name);
15816 goto erret;
15817 }
15818 }
15819
15820 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821 if (fp == NULL)
15822 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015823
15824 if (fudi.fd_dict != NULL)
15825 {
15826 if (fudi.fd_di == NULL)
15827 {
15828 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015829 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015830 if (fudi.fd_di == NULL)
15831 {
15832 vim_free(fp);
15833 goto erret;
15834 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015835 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
15836 {
15837 vim_free(fudi.fd_di);
15838 goto erret;
15839 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015840 }
15841 else
15842 /* overwrite existing dict entry */
15843 clear_tv(&fudi.fd_di->di_tv);
15844 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015845 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015846 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015847 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015848 }
15849
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015851 STRCPY(fp->uf_name, name);
15852 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015853 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015854 fp->uf_args = newargs;
15855 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000015856#ifdef FEAT_PROFILE
15857 fp->uf_tml_count = NULL;
15858 fp->uf_tml_total = NULL;
15859 fp->uf_tml_self = NULL;
15860 fp->uf_profiling = FALSE;
15861 if (prof_def_func())
15862 func_do_profile(fp);
15863#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015864 fp->uf_varargs = varargs;
15865 fp->uf_flags = flags;
15866 fp->uf_calls = 0;
15867 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015868 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869
15870erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871 ga_clear_strings(&newargs);
15872 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015873ret_free:
15874 vim_free(skip_until);
15875 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878}
15879
15880/*
15881 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000015882 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015883 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015884 * flags:
15885 * TFN_INT: internal function name OK
15886 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887 * Advances "pp" to just after the function name (if no error).
15888 */
15889 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015890trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015891 char_u **pp;
15892 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015893 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000015894 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015896 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897 char_u *start;
15898 char_u *end;
15899 int lead;
15900 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000015902 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015903
15904 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015905 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000015907
15908 /* Check for hard coded <SNR>: already translated function ID (from a user
15909 * command). */
15910 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
15911 && (*pp)[2] == (int)KE_SNR)
15912 {
15913 *pp += 3;
15914 len = get_id_len(pp) + 3;
15915 return vim_strnsave(start, len);
15916 }
15917
15918 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
15919 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015920 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000015921 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015923
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015924 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015925 if (end == start)
15926 {
15927 if (!skip)
15928 EMSG(_("E129: Function name required"));
15929 goto theend;
15930 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015931 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015932 {
15933 /*
15934 * Report an invalid expression in braces, unless the expression
15935 * evaluation has been cancelled due to an aborting error, an
15936 * interrupt, or an exception.
15937 */
15938 if (!aborting())
15939 {
15940 if (end != NULL)
15941 EMSG2(_(e_invarg2), start);
15942 }
15943 else
15944 *pp = find_name_end(start, NULL, NULL, TRUE);
15945 goto theend;
15946 }
15947
15948 if (lv.ll_tv != NULL)
15949 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015950 if (fdp != NULL)
15951 {
15952 fdp->fd_dict = lv.ll_dict;
15953 fdp->fd_newkey = lv.ll_newkey;
15954 lv.ll_newkey = NULL;
15955 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015956 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015957 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
15958 {
15959 name = vim_strsave(lv.ll_tv->vval.v_string);
15960 *pp = end;
15961 }
15962 else
15963 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015964 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
15965 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015966 EMSG(_(e_funcref));
15967 else
15968 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015969 name = NULL;
15970 }
15971 goto theend;
15972 }
15973
15974 if (lv.ll_name == NULL)
15975 {
15976 /* Error found, but continue after the function name. */
15977 *pp = end;
15978 goto theend;
15979 }
15980
15981 if (lv.ll_exp_name != NULL)
15982 len = STRLEN(lv.ll_exp_name);
15983 else
Bram Moolenaara7043832005-01-21 11:56:39 +000015984 {
15985 if (lead == 2) /* skip over "s:" */
15986 lv.ll_name += 2;
15987 len = (int)(end - lv.ll_name);
15988 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015989
15990 /*
15991 * Copy the function name to allocated memory.
15992 * Accept <SID>name() inside a script, translate into <SNR>123_name().
15993 * Accept <SNR>123_name() outside a script.
15994 */
15995 if (skip)
15996 lead = 0; /* do nothing */
15997 else if (lead > 0)
15998 {
15999 lead = 3;
16000 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
16001 {
16002 if (current_SID <= 0)
16003 {
16004 EMSG(_(e_usingsid));
16005 goto theend;
16006 }
16007 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
16008 lead += (int)STRLEN(sid_buf);
16009 }
16010 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016011 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016012 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016013 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016014 goto theend;
16015 }
16016 name = alloc((unsigned)(len + lead + 1));
16017 if (name != NULL)
16018 {
16019 if (lead > 0)
16020 {
16021 name[0] = K_SPECIAL;
16022 name[1] = KS_EXTRA;
16023 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000016024 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016025 STRCPY(name + 3, sid_buf);
16026 }
16027 mch_memmove(name + lead, lv.ll_name, (size_t)len);
16028 name[len + lead] = NUL;
16029 }
16030 *pp = end;
16031
16032theend:
16033 clear_lval(&lv);
16034 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016035}
16036
16037/*
16038 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
16039 * Return 2 if "p" starts with "s:".
16040 * Return 0 otherwise.
16041 */
16042 static int
16043eval_fname_script(p)
16044 char_u *p;
16045{
16046 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
16047 || STRNICMP(p + 1, "SNR>", 4) == 0))
16048 return 5;
16049 if (p[0] == 's' && p[1] == ':')
16050 return 2;
16051 return 0;
16052}
16053
16054/*
16055 * Return TRUE if "p" starts with "<SID>" or "s:".
16056 * Only works if eval_fname_script() returned non-zero for "p"!
16057 */
16058 static int
16059eval_fname_sid(p)
16060 char_u *p;
16061{
16062 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
16063}
16064
16065/*
16066 * List the head of the function: "name(arg1, arg2)".
16067 */
16068 static void
16069list_func_head(fp, indent)
16070 ufunc_T *fp;
16071 int indent;
16072{
16073 int j;
16074
16075 msg_start();
16076 if (indent)
16077 MSG_PUTS(" ");
16078 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016079 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016080 {
16081 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016082 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083 }
16084 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016085 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016087 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088 {
16089 if (j)
16090 MSG_PUTS(", ");
16091 msg_puts(FUNCARG(fp, j));
16092 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016093 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094 {
16095 if (j)
16096 MSG_PUTS(", ");
16097 MSG_PUTS("...");
16098 }
16099 msg_putchar(')');
16100}
16101
16102/*
16103 * Find a function by name, return pointer to it in ufuncs.
16104 * Return NULL for unknown function.
16105 */
16106 static ufunc_T *
16107find_func(name)
16108 char_u *name;
16109{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016110 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016112 hi = hash_find(&func_hashtab, name);
16113 if (!HASHITEM_EMPTY(hi))
16114 return HI2UF(hi);
16115 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116}
16117
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016118/*
16119 * Return TRUE if a function "name" exists.
16120 */
16121 static int
16122function_exists(name)
16123 char_u *name;
16124{
16125 char_u *p = name;
16126 int n = FALSE;
16127
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016128 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016129 if (p != NULL)
16130 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016131 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016132 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016133 else
16134 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016135 vim_free(p);
16136 }
16137 return n;
16138}
16139
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016140/*
16141 * Return TRUE if "name" looks like a builtin function name: starts with a
16142 * lower case letter and doesn't contain a ':'.
16143 */
16144 static int
16145builtin_function(name)
16146 char_u *name;
16147{
16148 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL;
16149}
16150
Bram Moolenaar05159a02005-02-26 23:04:13 +000016151#if defined(FEAT_PROFILE) || defined(PROTO)
16152/*
16153 * Start profiling function "fp".
16154 */
16155 static void
16156func_do_profile(fp)
16157 ufunc_T *fp;
16158{
16159 fp->uf_tm_count = 0;
16160 profile_zero(&fp->uf_tm_self);
16161 profile_zero(&fp->uf_tm_total);
16162 if (fp->uf_tml_count == NULL)
16163 fp->uf_tml_count = (int *)alloc_clear((unsigned)
16164 (sizeof(int) * fp->uf_lines.ga_len));
16165 if (fp->uf_tml_total == NULL)
16166 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
16167 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16168 if (fp->uf_tml_self == NULL)
16169 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
16170 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16171 fp->uf_tml_idx = -1;
16172 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
16173 || fp->uf_tml_self == NULL)
16174 return; /* out of memory */
16175
16176 fp->uf_profiling = TRUE;
16177}
16178
16179/*
16180 * Dump the profiling results for all functions in file "fd".
16181 */
16182 void
16183func_dump_profile(fd)
16184 FILE *fd;
16185{
16186 hashitem_T *hi;
16187 int todo;
16188 ufunc_T *fp;
16189 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000016190 ufunc_T **sorttab;
16191 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016192
16193 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000016194 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
16195
Bram Moolenaar05159a02005-02-26 23:04:13 +000016196 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
16197 {
16198 if (!HASHITEM_EMPTY(hi))
16199 {
16200 --todo;
16201 fp = HI2UF(hi);
16202 if (fp->uf_profiling)
16203 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016204 if (sorttab != NULL)
16205 sorttab[st_len++] = fp;
16206
Bram Moolenaar05159a02005-02-26 23:04:13 +000016207 if (fp->uf_name[0] == K_SPECIAL)
16208 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
16209 else
16210 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
16211 if (fp->uf_tm_count == 1)
16212 fprintf(fd, "Called 1 time\n");
16213 else
16214 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
16215 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
16216 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
16217 fprintf(fd, "\n");
16218 fprintf(fd, "count total (s) self (s)\n");
16219
16220 for (i = 0; i < fp->uf_lines.ga_len; ++i)
16221 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016222 prof_func_line(fd, fp->uf_tml_count[i],
16223 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016224 fprintf(fd, "%s\n", FUNCLINE(fp, i));
16225 }
16226 fprintf(fd, "\n");
16227 }
16228 }
16229 }
Bram Moolenaar73830342005-02-28 22:48:19 +000016230
16231 if (sorttab != NULL && st_len > 0)
16232 {
16233 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16234 prof_total_cmp);
16235 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
16236 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16237 prof_self_cmp);
16238 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
16239 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016240}
Bram Moolenaar73830342005-02-28 22:48:19 +000016241
16242 static void
16243prof_sort_list(fd, sorttab, st_len, title, prefer_self)
16244 FILE *fd;
16245 ufunc_T **sorttab;
16246 int st_len;
16247 char *title;
16248 int prefer_self; /* when equal print only self time */
16249{
16250 int i;
16251 ufunc_T *fp;
16252
16253 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
16254 fprintf(fd, "count total (s) self (s) function\n");
16255 for (i = 0; i < 20 && i < st_len; ++i)
16256 {
16257 fp = sorttab[i];
16258 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
16259 prefer_self);
16260 if (fp->uf_name[0] == K_SPECIAL)
16261 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
16262 else
16263 fprintf(fd, " %s()\n", fp->uf_name);
16264 }
16265 fprintf(fd, "\n");
16266}
16267
16268/*
16269 * Print the count and times for one function or function line.
16270 */
16271 static void
16272prof_func_line(fd, count, total, self, prefer_self)
16273 FILE *fd;
16274 int count;
16275 proftime_T *total;
16276 proftime_T *self;
16277 int prefer_self; /* when equal print only self time */
16278{
16279 if (count > 0)
16280 {
16281 fprintf(fd, "%5d ", count);
16282 if (prefer_self && profile_equal(total, self))
16283 fprintf(fd, " ");
16284 else
16285 fprintf(fd, "%s ", profile_msg(total));
16286 if (!prefer_self && profile_equal(total, self))
16287 fprintf(fd, " ");
16288 else
16289 fprintf(fd, "%s ", profile_msg(self));
16290 }
16291 else
16292 fprintf(fd, " ");
16293}
16294
16295/*
16296 * Compare function for total time sorting.
16297 */
16298 static int
16299#ifdef __BORLANDC__
16300_RTLENTRYF
16301#endif
16302prof_total_cmp(s1, s2)
16303 const void *s1;
16304 const void *s2;
16305{
16306 ufunc_T *p1, *p2;
16307
16308 p1 = *(ufunc_T **)s1;
16309 p2 = *(ufunc_T **)s2;
16310 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
16311}
16312
16313/*
16314 * Compare function for self time sorting.
16315 */
16316 static int
16317#ifdef __BORLANDC__
16318_RTLENTRYF
16319#endif
16320prof_self_cmp(s1, s2)
16321 const void *s1;
16322 const void *s2;
16323{
16324 ufunc_T *p1, *p2;
16325
16326 p1 = *(ufunc_T **)s1;
16327 p2 = *(ufunc_T **)s2;
16328 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
16329}
16330
Bram Moolenaar05159a02005-02-26 23:04:13 +000016331#endif
16332
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016333/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016334 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016335 * Return TRUE if a package was loaded.
16336 */
16337 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016338script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016339 char_u *name;
16340{
16341 char_u *p;
16342 char_u *scriptname;
16343 int ret = FALSE;
16344
16345 /* If there is no colon after name[1] there is no package name. */
16346 p = vim_strchr(name, ':');
16347 if (p == NULL || p <= name + 2)
16348 return FALSE;
16349
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016350 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
16351 scriptname = autoload_name(name);
16352 if (cmd_runtime(scriptname, FALSE) == OK)
16353 ret = TRUE;
16354
16355 vim_free(scriptname);
16356 return ret;
16357}
16358
16359/*
16360 * Return the autoload script name for a function or variable name.
16361 * Returns NULL when out of memory.
16362 */
16363 static char_u *
16364autoload_name(name)
16365 char_u *name;
16366{
16367 char_u *p;
16368 char_u *scriptname;
16369
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016370 /* Get the script file name: replace ':' with '/', append ".vim". */
16371 scriptname = alloc((unsigned)(STRLEN(name) + 14));
16372 if (scriptname == NULL)
16373 return FALSE;
16374 STRCPY(scriptname, "autoload/");
16375 STRCAT(scriptname, name);
16376 *vim_strrchr(scriptname, ':') = NUL;
16377 STRCAT(scriptname, ".vim");
16378 while ((p = vim_strchr(scriptname, ':')) != NULL)
16379 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016380 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016381}
16382
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
16384
16385/*
16386 * Function given to ExpandGeneric() to obtain the list of user defined
16387 * function names.
16388 */
16389 char_u *
16390get_user_func_name(xp, idx)
16391 expand_T *xp;
16392 int idx;
16393{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016394 static long_u done;
16395 static hashitem_T *hi;
16396 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397
16398 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016400 done = 0;
16401 hi = func_hashtab.ht_array;
16402 }
16403 if (done < func_hashtab.ht_used)
16404 {
16405 if (done++ > 0)
16406 ++hi;
16407 while (HASHITEM_EMPTY(hi))
16408 ++hi;
16409 fp = HI2UF(hi);
16410
16411 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
16412 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413
16414 cat_func_name(IObuff, fp);
16415 if (xp->xp_context != EXPAND_USER_FUNC)
16416 {
16417 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016418 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419 STRCAT(IObuff, ")");
16420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016421 return IObuff;
16422 }
16423 return NULL;
16424}
16425
16426#endif /* FEAT_CMDL_COMPL */
16427
16428/*
16429 * Copy the function name of "fp" to buffer "buf".
16430 * "buf" must be able to hold the function name plus three bytes.
16431 * Takes care of script-local function names.
16432 */
16433 static void
16434cat_func_name(buf, fp)
16435 char_u *buf;
16436 ufunc_T *fp;
16437{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016438 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016439 {
16440 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016441 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442 }
16443 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016444 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445}
16446
16447/*
16448 * ":delfunction {name}"
16449 */
16450 void
16451ex_delfunction(eap)
16452 exarg_T *eap;
16453{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016454 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455 char_u *p;
16456 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016457 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458
16459 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016460 name = trans_function_name(&p, eap->skip, 0, &fudi);
16461 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016463 {
16464 if (fudi.fd_dict != NULL && !eap->skip)
16465 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468 if (!ends_excmd(*skipwhite(p)))
16469 {
16470 vim_free(name);
16471 EMSG(_(e_trailing));
16472 return;
16473 }
16474 eap->nextcmd = check_nextcmd(p);
16475 if (eap->nextcmd != NULL)
16476 *p = NUL;
16477
16478 if (!eap->skip)
16479 fp = find_func(name);
16480 vim_free(name);
16481
16482 if (!eap->skip)
16483 {
16484 if (fp == NULL)
16485 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000016486 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016487 return;
16488 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016489 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016490 {
16491 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
16492 return;
16493 }
16494
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016495 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016497 /* Delete the dict item that refers to the function, it will
16498 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016499 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016500 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016501 else
16502 func_free(fp);
16503 }
16504}
16505
16506/*
16507 * Free a function and remove it from the list of functions.
16508 */
16509 static void
16510func_free(fp)
16511 ufunc_T *fp;
16512{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016513 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016514
16515 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016516 ga_clear_strings(&(fp->uf_args));
16517 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000016518#ifdef FEAT_PROFILE
16519 vim_free(fp->uf_tml_count);
16520 vim_free(fp->uf_tml_total);
16521 vim_free(fp->uf_tml_self);
16522#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016523
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016524 /* remove the function from the function hashtable */
16525 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
16526 if (HASHITEM_EMPTY(hi))
16527 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016528 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016529 hash_remove(&func_hashtab, hi);
16530
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016531 vim_free(fp);
16532}
16533
16534/*
16535 * Unreference a Function: decrement the reference count and free it when it
16536 * becomes zero. Only for numbered functions.
16537 */
16538 static void
16539func_unref(name)
16540 char_u *name;
16541{
16542 ufunc_T *fp;
16543
16544 if (name != NULL && isdigit(*name))
16545 {
16546 fp = find_func(name);
16547 if (fp == NULL)
16548 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016549 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016550 {
16551 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016552 * when "uf_calls" becomes zero. */
16553 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016554 func_free(fp);
16555 }
16556 }
16557}
16558
16559/*
16560 * Count a reference to a Function.
16561 */
16562 static void
16563func_ref(name)
16564 char_u *name;
16565{
16566 ufunc_T *fp;
16567
16568 if (name != NULL && isdigit(*name))
16569 {
16570 fp = find_func(name);
16571 if (fp == NULL)
16572 EMSG2(_(e_intern2), "func_ref()");
16573 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016574 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 }
16576}
16577
16578/*
16579 * Call a user function.
16580 */
16581 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000016582call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583 ufunc_T *fp; /* pointer to function */
16584 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000016585 typval_T *argvars; /* arguments */
16586 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587 linenr_T firstline; /* first line of range */
16588 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000016589 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590{
Bram Moolenaar33570922005-01-25 22:26:29 +000016591 char_u *save_sourcing_name;
16592 linenr_T save_sourcing_lnum;
16593 scid_T save_current_SID;
16594 funccall_T fc;
16595 funccall_T *save_fcp = current_funccal;
16596 int save_did_emsg;
16597 static int depth = 0;
16598 dictitem_T *v;
16599 int fixvar_idx = 0; /* index in fixvar[] */
16600 int i;
16601 int ai;
16602 char_u numbuf[NUMBUFLEN];
16603 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016604#ifdef FEAT_PROFILE
16605 proftime_T wait_start;
16606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016607
16608 /* If depth of calling is getting too high, don't execute the function */
16609 if (depth >= p_mfd)
16610 {
16611 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016612 rettv->v_type = VAR_NUMBER;
16613 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614 return;
16615 }
16616 ++depth;
16617
16618 line_breakcheck(); /* check for CTRL-C hit */
16619
Bram Moolenaar33570922005-01-25 22:26:29 +000016620 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016622 fc.rettv = rettv;
16623 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624 fc.linenr = 0;
16625 fc.returned = FALSE;
16626 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016628 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629 fc.dbg_tick = debug_tick;
16630
Bram Moolenaar33570922005-01-25 22:26:29 +000016631 /*
16632 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
16633 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
16634 * each argument variable and saves a lot of time.
16635 */
16636 /*
16637 * Init l: variables.
16638 */
16639 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000016640 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016641 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016642 /* Set l:self to "selfdict". */
16643 v = &fc.fixvar[fixvar_idx++].var;
16644 STRCPY(v->di_key, "self");
16645 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
16646 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
16647 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016648 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000016649 v->di_tv.vval.v_dict = selfdict;
16650 ++selfdict->dv_refcount;
16651 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016652
Bram Moolenaar33570922005-01-25 22:26:29 +000016653 /*
16654 * Init a: variables.
16655 * Set a:0 to "argcount".
16656 * Set a:000 to a list with room for the "..." arguments.
16657 */
16658 init_var_dict(&fc.l_avars, &fc.l_avars_var);
16659 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016660 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000016661 v = &fc.fixvar[fixvar_idx++].var;
16662 STRCPY(v->di_key, "000");
16663 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16664 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16665 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016666 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016667 v->di_tv.vval.v_list = &fc.l_varlist;
16668 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
16669 fc.l_varlist.lv_refcount = 99999;
16670
16671 /*
16672 * Set a:firstline to "firstline" and a:lastline to "lastline".
16673 * Set a:name to named arguments.
16674 * Set a:N to the "..." arguments.
16675 */
16676 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
16677 (varnumber_T)firstline);
16678 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
16679 (varnumber_T)lastline);
16680 for (i = 0; i < argcount; ++i)
16681 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016682 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016683 if (ai < 0)
16684 /* named argument a:name */
16685 name = FUNCARG(fp, i);
16686 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016687 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016688 /* "..." argument a:1, a:2, etc. */
16689 sprintf((char *)numbuf, "%d", ai + 1);
16690 name = numbuf;
16691 }
16692 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
16693 {
16694 v = &fc.fixvar[fixvar_idx++].var;
16695 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16696 }
16697 else
16698 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016699 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16700 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000016701 if (v == NULL)
16702 break;
16703 v->di_flags = DI_FLAGS_RO;
16704 }
16705 STRCPY(v->di_key, name);
16706 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16707
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016708 /* Note: the values are copied directly to avoid alloc/free.
16709 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016710 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016711 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016712
16713 if (ai >= 0 && ai < MAX_FUNC_ARGS)
16714 {
16715 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
16716 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016717 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016718 }
16719 }
16720
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 /* Don't redraw while executing the function. */
16722 ++RedrawingDisabled;
16723 save_sourcing_name = sourcing_name;
16724 save_sourcing_lnum = sourcing_lnum;
16725 sourcing_lnum = 1;
16726 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016727 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728 if (sourcing_name != NULL)
16729 {
16730 if (save_sourcing_name != NULL
16731 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
16732 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
16733 else
16734 STRCPY(sourcing_name, "function ");
16735 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
16736
16737 if (p_verbose >= 12)
16738 {
16739 ++no_wait_return;
16740 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16741 msg_str((char_u *)_("calling %s"), sourcing_name);
16742 if (p_verbose >= 14)
16743 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000016745 char_u numbuf[NUMBUFLEN];
16746 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016747
16748 msg_puts((char_u *)"(");
16749 for (i = 0; i < argcount; ++i)
16750 {
16751 if (i > 0)
16752 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016753 if (argvars[i].v_type == VAR_NUMBER)
16754 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755 else
16756 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016757 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758 buf, MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016760 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761 }
16762 }
16763 msg_puts((char_u *)")");
16764 }
16765 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16766 cmdline_row = msg_row;
16767 --no_wait_return;
16768 }
16769 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016770#ifdef FEAT_PROFILE
16771 if (do_profiling)
16772 {
16773 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
16774 func_do_profile(fp);
16775 if (fp->uf_profiling
16776 || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16777 {
16778 ++fp->uf_tm_count;
16779 profile_start(&fp->uf_tm_start);
16780 profile_zero(&fp->uf_tm_children);
16781 }
16782 script_prof_save(&wait_start);
16783 }
16784#endif
16785
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016787 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788 save_did_emsg = did_emsg;
16789 did_emsg = FALSE;
16790
16791 /* call do_cmdline() to execute the lines */
16792 do_cmdline(NULL, get_func_line, (void *)&fc,
16793 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
16794
16795 --RedrawingDisabled;
16796
16797 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016798 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016800 clear_tv(rettv);
16801 rettv->v_type = VAR_NUMBER;
16802 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 }
16804
Bram Moolenaar05159a02005-02-26 23:04:13 +000016805#ifdef FEAT_PROFILE
16806 if (fp->uf_profiling || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16807 {
16808 profile_end(&fp->uf_tm_start);
16809 profile_sub_wait(&wait_start, &fp->uf_tm_start);
16810 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
16811 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
16812 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
16813 if (save_fcp != NULL && &save_fcp->func->uf_profiling)
16814 {
16815 profile_add(&save_fcp->func->uf_tm_children, &fp->uf_tm_start);
16816 profile_add(&save_fcp->func->uf_tml_children, &fp->uf_tm_start);
16817 }
16818 }
16819#endif
16820
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821 /* when being verbose, mention the return value */
16822 if (p_verbose >= 12)
16823 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016824 char_u *sn;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825
16826 ++no_wait_return;
16827 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16828
16829 /* Make sure the output fits in IObuff. */
16830 sn = sourcing_name;
16831 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
16832 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
16833
16834 if (aborting())
16835 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016836 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016837 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016838 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016839 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016841 char_u buf[MSG_BUF_LEN];
16842 char_u numbuf[NUMBUFLEN];
16843 char_u *tofree;
16844
16845 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
16846 buf, MSG_BUF_LEN);
16847 smsg((char_u *)_("%s returning %s"), sn, buf);
16848 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849 }
16850 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16851 cmdline_row = msg_row;
16852 --no_wait_return;
16853 }
16854
16855 vim_free(sourcing_name);
16856 sourcing_name = save_sourcing_name;
16857 sourcing_lnum = save_sourcing_lnum;
16858 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016859#ifdef FEAT_PROFILE
16860 if (do_profiling)
16861 script_prof_restore(&wait_start);
16862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863
16864 if (p_verbose >= 12 && sourcing_name != NULL)
16865 {
16866 ++no_wait_return;
16867 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16868 msg_str((char_u *)_("continuing in %s"), sourcing_name);
16869 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16870 cmdline_row = msg_row;
16871 --no_wait_return;
16872 }
16873
16874 did_emsg |= save_did_emsg;
16875 current_funccal = save_fcp;
16876
Bram Moolenaar33570922005-01-25 22:26:29 +000016877 /* The a: variables typevals were not alloced, only free the allocated
16878 * variables. */
16879 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
16880
16881 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882 --depth;
16883}
16884
16885/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016886 * Add a number variable "name" to dict "dp" with value "nr".
16887 */
16888 static void
16889add_nr_var(dp, v, name, nr)
16890 dict_T *dp;
16891 dictitem_T *v;
16892 char *name;
16893 varnumber_T nr;
16894{
16895 STRCPY(v->di_key, name);
16896 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16897 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
16898 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016899 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016900 v->di_tv.vval.v_number = nr;
16901}
16902
16903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016904 * ":return [expr]"
16905 */
16906 void
16907ex_return(eap)
16908 exarg_T *eap;
16909{
16910 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016911 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016912 int returning = FALSE;
16913
16914 if (current_funccal == NULL)
16915 {
16916 EMSG(_("E133: :return not inside a function"));
16917 return;
16918 }
16919
16920 if (eap->skip)
16921 ++emsg_skip;
16922
16923 eap->nextcmd = NULL;
16924 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016925 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926 {
16927 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016928 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016930 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016931 }
16932 /* It's safer to return also on error. */
16933 else if (!eap->skip)
16934 {
16935 /*
16936 * Return unless the expression evaluation has been cancelled due to an
16937 * aborting error, an interrupt, or an exception.
16938 */
16939 if (!aborting())
16940 returning = do_return(eap, FALSE, TRUE, NULL);
16941 }
16942
16943 /* When skipping or the return gets pending, advance to the next command
16944 * in this line (!returning). Otherwise, ignore the rest of the line.
16945 * Following lines will be ignored by get_func_line(). */
16946 if (returning)
16947 eap->nextcmd = NULL;
16948 else if (eap->nextcmd == NULL) /* no argument */
16949 eap->nextcmd = check_nextcmd(arg);
16950
16951 if (eap->skip)
16952 --emsg_skip;
16953}
16954
16955/*
16956 * Return from a function. Possibly makes the return pending. Also called
16957 * for a pending return at the ":endtry" or after returning from an extra
16958 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000016959 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016960 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961 * FALSE when the return gets pending.
16962 */
16963 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016964do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016965 exarg_T *eap;
16966 int reanimate;
16967 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016968 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969{
16970 int idx;
16971 struct condstack *cstack = eap->cstack;
16972
16973 if (reanimate)
16974 /* Undo the return. */
16975 current_funccal->returned = FALSE;
16976
16977 /*
16978 * Cleanup (and inactivate) conditionals, but stop when a try conditional
16979 * not in its finally clause (which then is to be executed next) is found.
16980 * In this case, make the ":return" pending for execution at the ":endtry".
16981 * Otherwise, return normally.
16982 */
16983 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
16984 if (idx >= 0)
16985 {
16986 cstack->cs_pending[idx] = CSTP_RETURN;
16987
16988 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016989 /* A pending return again gets pending. "rettv" points to an
16990 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016992 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993 else
16994 {
16995 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016996 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016998 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017000 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 {
17002 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017003 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017004 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005 else
17006 EMSG(_(e_outofmem));
17007 }
17008 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017009 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010
17011 if (reanimate)
17012 {
17013 /* The pending return value could be overwritten by a ":return"
17014 * without argument in a finally clause; reset the default
17015 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017016 current_funccal->rettv->v_type = VAR_NUMBER;
17017 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 }
17019 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017020 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 }
17022 else
17023 {
17024 current_funccal->returned = TRUE;
17025
17026 /* If the return is carried out now, store the return value. For
17027 * a return immediately after reanimation, the value is already
17028 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017029 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017031 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000017032 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017034 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035 }
17036 }
17037
17038 return idx < 0;
17039}
17040
17041/*
17042 * Free the variable with a pending return value.
17043 */
17044 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017045discard_pending_return(rettv)
17046 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047{
Bram Moolenaar33570922005-01-25 22:26:29 +000017048 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049}
17050
17051/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017052 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053 * is an allocated string. Used by report_pending() for verbose messages.
17054 */
17055 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017056get_return_cmd(rettv)
17057 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017059 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017060 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017061 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017063 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017064 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017065 if (s == NULL)
17066 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017067
17068 STRCPY(IObuff, ":return ");
17069 STRNCPY(IObuff + 8, s, IOSIZE - 8);
17070 if (STRLEN(s) + 8 >= IOSIZE)
17071 STRCPY(IObuff + IOSIZE - 4, "...");
17072 vim_free(tofree);
17073 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074}
17075
17076/*
17077 * Get next function line.
17078 * Called by do_cmdline() to get the next line.
17079 * Returns allocated string, or NULL for end of function.
17080 */
17081/* ARGSUSED */
17082 char_u *
17083get_func_line(c, cookie, indent)
17084 int c; /* not used */
17085 void *cookie;
17086 int indent; /* not used */
17087{
Bram Moolenaar33570922005-01-25 22:26:29 +000017088 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017089 ufunc_T *fp = fcp->func;
17090 char_u *retval;
17091 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092
17093 /* If breakpoints have been added/deleted need to check for it. */
17094 if (fcp->dbg_tick != debug_tick)
17095 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017096 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097 sourcing_lnum);
17098 fcp->dbg_tick = debug_tick;
17099 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017100#ifdef FEAT_PROFILE
17101 if (do_profiling)
17102 func_line_end(cookie);
17103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104
Bram Moolenaar05159a02005-02-26 23:04:13 +000017105 gap = &fp->uf_lines;
17106 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107 retval = NULL;
17108 else if (fcp->returned || fcp->linenr >= gap->ga_len)
17109 retval = NULL;
17110 else
17111 {
17112 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
17113 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017114#ifdef FEAT_PROFILE
17115 if (do_profiling)
17116 func_line_start(cookie);
17117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118 }
17119
17120 /* Did we encounter a breakpoint? */
17121 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
17122 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017123 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017125 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126 sourcing_lnum);
17127 fcp->dbg_tick = debug_tick;
17128 }
17129
17130 return retval;
17131}
17132
Bram Moolenaar05159a02005-02-26 23:04:13 +000017133#if defined(FEAT_PROFILE) || defined(PROTO)
17134/*
17135 * Called when starting to read a function line.
17136 * "sourcing_lnum" must be correct!
17137 * When skipping lines it may not actually be executed, but we won't find out
17138 * until later and we need to store the time now.
17139 */
17140 void
17141func_line_start(cookie)
17142 void *cookie;
17143{
17144 funccall_T *fcp = (funccall_T *)cookie;
17145 ufunc_T *fp = fcp->func;
17146
17147 if (fp->uf_profiling && sourcing_lnum >= 1
17148 && sourcing_lnum <= fp->uf_lines.ga_len)
17149 {
17150 fp->uf_tml_idx = sourcing_lnum - 1;
17151 fp->uf_tml_execed = FALSE;
17152 profile_start(&fp->uf_tml_start);
17153 profile_zero(&fp->uf_tml_children);
17154 profile_get_wait(&fp->uf_tml_wait);
17155 }
17156}
17157
17158/*
17159 * Called when actually executing a function line.
17160 */
17161 void
17162func_line_exec(cookie)
17163 void *cookie;
17164{
17165 funccall_T *fcp = (funccall_T *)cookie;
17166 ufunc_T *fp = fcp->func;
17167
17168 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17169 fp->uf_tml_execed = TRUE;
17170}
17171
17172/*
17173 * Called when done with a function line.
17174 */
17175 void
17176func_line_end(cookie)
17177 void *cookie;
17178{
17179 funccall_T *fcp = (funccall_T *)cookie;
17180 ufunc_T *fp = fcp->func;
17181
17182 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17183 {
17184 if (fp->uf_tml_execed)
17185 {
17186 ++fp->uf_tml_count[fp->uf_tml_idx];
17187 profile_end(&fp->uf_tml_start);
17188 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
17189 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
17190 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
17191 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
17192 }
17193 fp->uf_tml_idx = -1;
17194 }
17195}
17196#endif
17197
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198/*
17199 * Return TRUE if the currently active function should be ended, because a
17200 * return was encountered or an error occured. Used inside a ":while".
17201 */
17202 int
17203func_has_ended(cookie)
17204 void *cookie;
17205{
Bram Moolenaar33570922005-01-25 22:26:29 +000017206 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207
17208 /* Ignore the "abort" flag if the abortion behavior has been changed due to
17209 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017210 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211 || fcp->returned);
17212}
17213
17214/*
17215 * return TRUE if cookie indicates a function which "abort"s on errors.
17216 */
17217 int
17218func_has_abort(cookie)
17219 void *cookie;
17220{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017221 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017222}
17223
17224#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
17225typedef enum
17226{
17227 VAR_FLAVOUR_DEFAULT,
17228 VAR_FLAVOUR_SESSION,
17229 VAR_FLAVOUR_VIMINFO
17230} var_flavour_T;
17231
17232static var_flavour_T var_flavour __ARGS((char_u *varname));
17233
17234 static var_flavour_T
17235var_flavour(varname)
17236 char_u *varname;
17237{
17238 char_u *p = varname;
17239
17240 if (ASCII_ISUPPER(*p))
17241 {
17242 while (*(++p))
17243 if (ASCII_ISLOWER(*p))
17244 return VAR_FLAVOUR_SESSION;
17245 return VAR_FLAVOUR_VIMINFO;
17246 }
17247 else
17248 return VAR_FLAVOUR_DEFAULT;
17249}
17250#endif
17251
17252#if defined(FEAT_VIMINFO) || defined(PROTO)
17253/*
17254 * Restore global vars that start with a capital from the viminfo file
17255 */
17256 int
17257read_viminfo_varlist(virp, writing)
17258 vir_T *virp;
17259 int writing;
17260{
17261 char_u *tab;
17262 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000017263 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264
17265 if (!writing && (find_viminfo_parameter('!') != NULL))
17266 {
17267 tab = vim_strchr(virp->vir_line + 1, '\t');
17268 if (tab != NULL)
17269 {
17270 *tab++ = '\0'; /* isolate the variable name */
17271 if (*tab == 'S') /* string var */
17272 is_string = TRUE;
17273
17274 tab = vim_strchr(tab, '\t');
17275 if (tab != NULL)
17276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277 if (is_string)
17278 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017279 tv.v_type = VAR_STRING;
17280 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 }
17283 else
17284 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017285 tv.v_type = VAR_NUMBER;
17286 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017288 set_var(virp->vir_line + 1, &tv, FALSE);
17289 if (is_string)
17290 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017291 }
17292 }
17293 }
17294
17295 return viminfo_readline(virp);
17296}
17297
17298/*
17299 * Write global vars that start with a capital to the viminfo file
17300 */
17301 void
17302write_viminfo_varlist(fp)
17303 FILE *fp;
17304{
Bram Moolenaar33570922005-01-25 22:26:29 +000017305 hashitem_T *hi;
17306 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017307 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017308 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017309 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017310 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017311 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312
17313 if (find_viminfo_parameter('!') == NULL)
17314 return;
17315
17316 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000017317
Bram Moolenaar33570922005-01-25 22:26:29 +000017318 todo = globvarht.ht_used;
17319 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017321 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017323 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017324 this_var = HI2DI(hi);
17325 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017326 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017327 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000017328 {
17329 case VAR_STRING: s = "STR"; break;
17330 case VAR_NUMBER: s = "NUM"; break;
17331 default: continue;
17332 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017333 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017334 p = echo_string(&this_var->di_tv, &tofree, numbuf);
17335 if (p != NULL)
17336 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000017337 vim_free(tofree);
17338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339 }
17340 }
17341}
17342#endif
17343
17344#if defined(FEAT_SESSION) || defined(PROTO)
17345 int
17346store_session_globals(fd)
17347 FILE *fd;
17348{
Bram Moolenaar33570922005-01-25 22:26:29 +000017349 hashitem_T *hi;
17350 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017351 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 char_u *p, *t;
17353
Bram Moolenaar33570922005-01-25 22:26:29 +000017354 todo = globvarht.ht_used;
17355 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017357 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017359 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017360 this_var = HI2DI(hi);
17361 if ((this_var->di_tv.v_type == VAR_NUMBER
17362 || this_var->di_tv.v_type == VAR_STRING)
17363 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017364 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017365 /* Escape special characters with a backslash. Turn a LF and
17366 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017367 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000017368 (char_u *)"\\\"\n\r");
17369 if (p == NULL) /* out of memory */
17370 break;
17371 for (t = p; *t != NUL; ++t)
17372 if (*t == '\n')
17373 *t = 'n';
17374 else if (*t == '\r')
17375 *t = 'r';
17376 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000017377 this_var->di_key,
17378 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17379 : ' ',
17380 p,
17381 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17382 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000017383 || put_eol(fd) == FAIL)
17384 {
17385 vim_free(p);
17386 return FAIL;
17387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 vim_free(p);
17389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 }
17391 }
17392 return OK;
17393}
17394#endif
17395
17396#endif /* FEAT_EVAL */
17397
17398#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
17399
17400
17401#ifdef WIN3264
17402/*
17403 * Functions for ":8" filename modifier: get 8.3 version of a filename.
17404 */
17405static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17406static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
17407static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17408
17409/*
17410 * Get the short pathname of a file.
17411 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
17412 */
17413 static int
17414get_short_pathname(fnamep, bufp, fnamelen)
17415 char_u **fnamep;
17416 char_u **bufp;
17417 int *fnamelen;
17418{
17419 int l,len;
17420 char_u *newbuf;
17421
17422 len = *fnamelen;
17423
17424 l = GetShortPathName(*fnamep, *fnamep, len);
17425 if (l > len - 1)
17426 {
17427 /* If that doesn't work (not enough space), then save the string
17428 * and try again with a new buffer big enough
17429 */
17430 newbuf = vim_strnsave(*fnamep, l);
17431 if (newbuf == NULL)
17432 return 0;
17433
17434 vim_free(*bufp);
17435 *fnamep = *bufp = newbuf;
17436
17437 l = GetShortPathName(*fnamep,*fnamep,l+1);
17438
17439 /* Really should always succeed, as the buffer is big enough */
17440 }
17441
17442 *fnamelen = l;
17443 return 1;
17444}
17445
17446/*
17447 * Create a short path name. Returns the length of the buffer it needs.
17448 * Doesn't copy over the end of the buffer passed in.
17449 */
17450 static int
17451shortpath_for_invalid_fname(fname, bufp, fnamelen)
17452 char_u **fname;
17453 char_u **bufp;
17454 int *fnamelen;
17455{
17456 char_u *s, *p, *pbuf2, *pbuf3;
17457 char_u ch;
17458 int l,len,len2,plen,slen;
17459
17460 /* Make a copy */
17461 len2 = *fnamelen;
17462 pbuf2 = vim_strnsave(*fname, len2);
17463 pbuf3 = NULL;
17464
17465 s = pbuf2 + len2 - 1; /* Find the end */
17466 slen = 1;
17467 plen = len2;
17468
17469 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017470 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 {
17472 --s;
17473 ++slen;
17474 --plen;
17475 }
17476
17477 do
17478 {
17479 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017480 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481 {
17482 --s;
17483 ++slen;
17484 --plen;
17485 }
17486 if (s <= pbuf2)
17487 break;
17488
17489 /* Remeber the character that is about to be blatted */
17490 ch = *s;
17491 *s = 0; /* get_short_pathname requires a null-terminated string */
17492
17493 /* Try it in situ */
17494 p = pbuf2;
17495 if (!get_short_pathname(&p, &pbuf3, &plen))
17496 {
17497 vim_free(pbuf2);
17498 return -1;
17499 }
17500 *s = ch; /* Preserve the string */
17501 } while (plen == 0);
17502
17503 if (plen > 0)
17504 {
17505 /* Remeber the length of the new string. */
17506 *fnamelen = len = plen + slen;
17507 vim_free(*bufp);
17508 if (len > len2)
17509 {
17510 /* If there's not enough space in the currently allocated string,
17511 * then copy it to a buffer big enough.
17512 */
17513 *fname= *bufp = vim_strnsave(p, len);
17514 if (*fname == NULL)
17515 return -1;
17516 }
17517 else
17518 {
17519 /* Transfer pbuf2 to being the main buffer (it's big enough) */
17520 *fname = *bufp = pbuf2;
17521 if (p != pbuf2)
17522 strncpy(*fname, p, plen);
17523 pbuf2 = NULL;
17524 }
17525 /* Concat the next bit */
17526 strncpy(*fname + plen, s, slen);
17527 (*fname)[len] = '\0';
17528 }
17529 vim_free(pbuf3);
17530 vim_free(pbuf2);
17531 return 0;
17532}
17533
17534/*
17535 * Get a pathname for a partial path.
17536 */
17537 static int
17538shortpath_for_partial(fnamep, bufp, fnamelen)
17539 char_u **fnamep;
17540 char_u **bufp;
17541 int *fnamelen;
17542{
17543 int sepcount, len, tflen;
17544 char_u *p;
17545 char_u *pbuf, *tfname;
17546 int hasTilde;
17547
17548 /* Count up the path seperators from the RHS.. so we know which part
17549 * of the path to return.
17550 */
17551 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017552 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 if (vim_ispathsep(*p))
17554 ++sepcount;
17555
17556 /* Need full path first (use expand_env() to remove a "~/") */
17557 hasTilde = (**fnamep == '~');
17558 if (hasTilde)
17559 pbuf = tfname = expand_env_save(*fnamep);
17560 else
17561 pbuf = tfname = FullName_save(*fnamep, FALSE);
17562
17563 len = tflen = STRLEN(tfname);
17564
17565 if (!get_short_pathname(&tfname, &pbuf, &len))
17566 return -1;
17567
17568 if (len == 0)
17569 {
17570 /* Don't have a valid filename, so shorten the rest of the
17571 * path if we can. This CAN give us invalid 8.3 filenames, but
17572 * there's not a lot of point in guessing what it might be.
17573 */
17574 len = tflen;
17575 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
17576 return -1;
17577 }
17578
17579 /* Count the paths backward to find the beginning of the desired string. */
17580 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017581 {
17582#ifdef FEAT_MBYTE
17583 if (has_mbyte)
17584 p -= mb_head_off(tfname, p);
17585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586 if (vim_ispathsep(*p))
17587 {
17588 if (sepcount == 0 || (hasTilde && sepcount == 1))
17589 break;
17590 else
17591 sepcount --;
17592 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594 if (hasTilde)
17595 {
17596 --p;
17597 if (p >= tfname)
17598 *p = '~';
17599 else
17600 return -1;
17601 }
17602 else
17603 ++p;
17604
17605 /* Copy in the string - p indexes into tfname - allocated at pbuf */
17606 vim_free(*bufp);
17607 *fnamelen = (int)STRLEN(p);
17608 *bufp = pbuf;
17609 *fnamep = p;
17610
17611 return 0;
17612}
17613#endif /* WIN3264 */
17614
17615/*
17616 * Adjust a filename, according to a string of modifiers.
17617 * *fnamep must be NUL terminated when called. When returning, the length is
17618 * determined by *fnamelen.
17619 * Returns valid flags.
17620 * When there is an error, *fnamep is set to NULL.
17621 */
17622 int
17623modify_fname(src, usedlen, fnamep, bufp, fnamelen)
17624 char_u *src; /* string with modifiers */
17625 int *usedlen; /* characters after src that are used */
17626 char_u **fnamep; /* file name so far */
17627 char_u **bufp; /* buffer for allocated file name or NULL */
17628 int *fnamelen; /* length of fnamep */
17629{
17630 int valid = 0;
17631 char_u *tail;
17632 char_u *s, *p, *pbuf;
17633 char_u dirname[MAXPATHL];
17634 int c;
17635 int has_fullname = 0;
17636#ifdef WIN3264
17637 int has_shortname = 0;
17638#endif
17639
17640repeat:
17641 /* ":p" - full path/file_name */
17642 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
17643 {
17644 has_fullname = 1;
17645
17646 valid |= VALID_PATH;
17647 *usedlen += 2;
17648
17649 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
17650 if ((*fnamep)[0] == '~'
17651#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
17652 && ((*fnamep)[1] == '/'
17653# ifdef BACKSLASH_IN_FILENAME
17654 || (*fnamep)[1] == '\\'
17655# endif
17656 || (*fnamep)[1] == NUL)
17657
17658#endif
17659 )
17660 {
17661 *fnamep = expand_env_save(*fnamep);
17662 vim_free(*bufp); /* free any allocated file name */
17663 *bufp = *fnamep;
17664 if (*fnamep == NULL)
17665 return -1;
17666 }
17667
17668 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017669 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670 {
17671 if (vim_ispathsep(*p)
17672 && p[1] == '.'
17673 && (p[2] == NUL
17674 || vim_ispathsep(p[2])
17675 || (p[2] == '.'
17676 && (p[3] == NUL || vim_ispathsep(p[3])))))
17677 break;
17678 }
17679
17680 /* FullName_save() is slow, don't use it when not needed. */
17681 if (*p != NUL || !vim_isAbsName(*fnamep))
17682 {
17683 *fnamep = FullName_save(*fnamep, *p != NUL);
17684 vim_free(*bufp); /* free any allocated file name */
17685 *bufp = *fnamep;
17686 if (*fnamep == NULL)
17687 return -1;
17688 }
17689
17690 /* Append a path separator to a directory. */
17691 if (mch_isdir(*fnamep))
17692 {
17693 /* Make room for one or two extra characters. */
17694 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
17695 vim_free(*bufp); /* free any allocated file name */
17696 *bufp = *fnamep;
17697 if (*fnamep == NULL)
17698 return -1;
17699 add_pathsep(*fnamep);
17700 }
17701 }
17702
17703 /* ":." - path relative to the current directory */
17704 /* ":~" - path relative to the home directory */
17705 /* ":8" - shortname path - postponed till after */
17706 while (src[*usedlen] == ':'
17707 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
17708 {
17709 *usedlen += 2;
17710 if (c == '8')
17711 {
17712#ifdef WIN3264
17713 has_shortname = 1; /* Postpone this. */
17714#endif
17715 continue;
17716 }
17717 pbuf = NULL;
17718 /* Need full path first (use expand_env() to remove a "~/") */
17719 if (!has_fullname)
17720 {
17721 if (c == '.' && **fnamep == '~')
17722 p = pbuf = expand_env_save(*fnamep);
17723 else
17724 p = pbuf = FullName_save(*fnamep, FALSE);
17725 }
17726 else
17727 p = *fnamep;
17728
17729 has_fullname = 0;
17730
17731 if (p != NULL)
17732 {
17733 if (c == '.')
17734 {
17735 mch_dirname(dirname, MAXPATHL);
17736 s = shorten_fname(p, dirname);
17737 if (s != NULL)
17738 {
17739 *fnamep = s;
17740 if (pbuf != NULL)
17741 {
17742 vim_free(*bufp); /* free any allocated file name */
17743 *bufp = pbuf;
17744 pbuf = NULL;
17745 }
17746 }
17747 }
17748 else
17749 {
17750 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
17751 /* Only replace it when it starts with '~' */
17752 if (*dirname == '~')
17753 {
17754 s = vim_strsave(dirname);
17755 if (s != NULL)
17756 {
17757 *fnamep = s;
17758 vim_free(*bufp);
17759 *bufp = s;
17760 }
17761 }
17762 }
17763 vim_free(pbuf);
17764 }
17765 }
17766
17767 tail = gettail(*fnamep);
17768 *fnamelen = (int)STRLEN(*fnamep);
17769
17770 /* ":h" - head, remove "/file_name", can be repeated */
17771 /* Don't remove the first "/" or "c:\" */
17772 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
17773 {
17774 valid |= VALID_HEAD;
17775 *usedlen += 2;
17776 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017777 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778 --tail;
17779 *fnamelen = (int)(tail - *fnamep);
17780#ifdef VMS
17781 if (*fnamelen > 0)
17782 *fnamelen += 1; /* the path separator is part of the path */
17783#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017784 while (tail > s && !after_pathsep(s, tail))
17785 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786 }
17787
17788 /* ":8" - shortname */
17789 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
17790 {
17791 *usedlen += 2;
17792#ifdef WIN3264
17793 has_shortname = 1;
17794#endif
17795 }
17796
17797#ifdef WIN3264
17798 /* Check shortname after we have done 'heads' and before we do 'tails'
17799 */
17800 if (has_shortname)
17801 {
17802 pbuf = NULL;
17803 /* Copy the string if it is shortened by :h */
17804 if (*fnamelen < (int)STRLEN(*fnamep))
17805 {
17806 p = vim_strnsave(*fnamep, *fnamelen);
17807 if (p == 0)
17808 return -1;
17809 vim_free(*bufp);
17810 *bufp = *fnamep = p;
17811 }
17812
17813 /* Split into two implementations - makes it easier. First is where
17814 * there isn't a full name already, second is where there is.
17815 */
17816 if (!has_fullname && !vim_isAbsName(*fnamep))
17817 {
17818 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
17819 return -1;
17820 }
17821 else
17822 {
17823 int l;
17824
17825 /* Simple case, already have the full-name
17826 * Nearly always shorter, so try first time. */
17827 l = *fnamelen;
17828 if (!get_short_pathname(fnamep, bufp, &l))
17829 return -1;
17830
17831 if (l == 0)
17832 {
17833 /* Couldn't find the filename.. search the paths.
17834 */
17835 l = *fnamelen;
17836 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
17837 return -1;
17838 }
17839 *fnamelen = l;
17840 }
17841 }
17842#endif /* WIN3264 */
17843
17844 /* ":t" - tail, just the basename */
17845 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
17846 {
17847 *usedlen += 2;
17848 *fnamelen -= (int)(tail - *fnamep);
17849 *fnamep = tail;
17850 }
17851
17852 /* ":e" - extension, can be repeated */
17853 /* ":r" - root, without extension, can be repeated */
17854 while (src[*usedlen] == ':'
17855 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
17856 {
17857 /* find a '.' in the tail:
17858 * - for second :e: before the current fname
17859 * - otherwise: The last '.'
17860 */
17861 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
17862 s = *fnamep - 2;
17863 else
17864 s = *fnamep + *fnamelen - 1;
17865 for ( ; s > tail; --s)
17866 if (s[0] == '.')
17867 break;
17868 if (src[*usedlen + 1] == 'e') /* :e */
17869 {
17870 if (s > tail)
17871 {
17872 *fnamelen += (int)(*fnamep - (s + 1));
17873 *fnamep = s + 1;
17874#ifdef VMS
17875 /* cut version from the extension */
17876 s = *fnamep + *fnamelen - 1;
17877 for ( ; s > *fnamep; --s)
17878 if (s[0] == ';')
17879 break;
17880 if (s > *fnamep)
17881 *fnamelen = s - *fnamep;
17882#endif
17883 }
17884 else if (*fnamep <= tail)
17885 *fnamelen = 0;
17886 }
17887 else /* :r */
17888 {
17889 if (s > tail) /* remove one extension */
17890 *fnamelen = (int)(s - *fnamep);
17891 }
17892 *usedlen += 2;
17893 }
17894
17895 /* ":s?pat?foo?" - substitute */
17896 /* ":gs?pat?foo?" - global substitute */
17897 if (src[*usedlen] == ':'
17898 && (src[*usedlen + 1] == 's'
17899 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
17900 {
17901 char_u *str;
17902 char_u *pat;
17903 char_u *sub;
17904 int sep;
17905 char_u *flags;
17906 int didit = FALSE;
17907
17908 flags = (char_u *)"";
17909 s = src + *usedlen + 2;
17910 if (src[*usedlen + 1] == 'g')
17911 {
17912 flags = (char_u *)"g";
17913 ++s;
17914 }
17915
17916 sep = *s++;
17917 if (sep)
17918 {
17919 /* find end of pattern */
17920 p = vim_strchr(s, sep);
17921 if (p != NULL)
17922 {
17923 pat = vim_strnsave(s, (int)(p - s));
17924 if (pat != NULL)
17925 {
17926 s = p + 1;
17927 /* find end of substitution */
17928 p = vim_strchr(s, sep);
17929 if (p != NULL)
17930 {
17931 sub = vim_strnsave(s, (int)(p - s));
17932 str = vim_strnsave(*fnamep, *fnamelen);
17933 if (sub != NULL && str != NULL)
17934 {
17935 *usedlen = (int)(p + 1 - src);
17936 s = do_string_sub(str, pat, sub, flags);
17937 if (s != NULL)
17938 {
17939 *fnamep = s;
17940 *fnamelen = (int)STRLEN(s);
17941 vim_free(*bufp);
17942 *bufp = s;
17943 didit = TRUE;
17944 }
17945 }
17946 vim_free(sub);
17947 vim_free(str);
17948 }
17949 vim_free(pat);
17950 }
17951 }
17952 /* after using ":s", repeat all the modifiers */
17953 if (didit)
17954 goto repeat;
17955 }
17956 }
17957
17958 return valid;
17959}
17960
17961/*
17962 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
17963 * "flags" can be "g" to do a global substitute.
17964 * Returns an allocated string, NULL for error.
17965 */
17966 char_u *
17967do_string_sub(str, pat, sub, flags)
17968 char_u *str;
17969 char_u *pat;
17970 char_u *sub;
17971 char_u *flags;
17972{
17973 int sublen;
17974 regmatch_T regmatch;
17975 int i;
17976 int do_all;
17977 char_u *tail;
17978 garray_T ga;
17979 char_u *ret;
17980 char_u *save_cpo;
17981
17982 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
17983 save_cpo = p_cpo;
17984 p_cpo = (char_u *)"";
17985
17986 ga_init2(&ga, 1, 200);
17987
17988 do_all = (flags[0] == 'g');
17989
17990 regmatch.rm_ic = p_ic;
17991 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17992 if (regmatch.regprog != NULL)
17993 {
17994 tail = str;
17995 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
17996 {
17997 /*
17998 * Get some space for a temporary buffer to do the substitution
17999 * into. It will contain:
18000 * - The text up to where the match is.
18001 * - The substituted text.
18002 * - The text after the match.
18003 */
18004 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
18005 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
18006 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
18007 {
18008 ga_clear(&ga);
18009 break;
18010 }
18011
18012 /* copy the text up to where the match is */
18013 i = (int)(regmatch.startp[0] - tail);
18014 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
18015 /* add the substituted text */
18016 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
18017 + ga.ga_len + i, TRUE, TRUE, FALSE);
18018 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019 /* avoid getting stuck on a match with an empty string */
18020 if (tail == regmatch.endp[0])
18021 {
18022 if (*tail == NUL)
18023 break;
18024 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
18025 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026 }
18027 else
18028 {
18029 tail = regmatch.endp[0];
18030 if (*tail == NUL)
18031 break;
18032 }
18033 if (!do_all)
18034 break;
18035 }
18036
18037 if (ga.ga_data != NULL)
18038 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
18039
18040 vim_free(regmatch.regprog);
18041 }
18042
18043 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
18044 ga_clear(&ga);
18045 p_cpo = save_cpo;
18046
18047 return ret;
18048}
18049
18050#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */