blob: c7aba51456b46835a15241d4f8bc40d136a70cc7 [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 Moolenaar33570922005-01-25 22:26:29 +0000321};
322
323/* shorthand */
324#define vv_type vv_di.di_tv.v_type
325#define vv_nr vv_di.di_tv.vval.v_number
326#define vv_str vv_di.di_tv.vval.v_string
327#define vv_tv vv_di.di_tv
328
329/*
330 * The v: variables are stored in dictionary "vimvardict".
331 * "vimvars_var" is the variable that is used for the "l:" scope.
332 */
333static dict_T vimvardict;
334static dictitem_T vimvars_var;
335#define vimvarht vimvardict.dv_hashtab
336
337static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
338static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
339static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
340static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
341static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
342static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
343static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
344static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000345static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000346static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
347static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
348static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
349static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
350static list_T *list_alloc __ARGS((void));
351static void list_unref __ARGS((list_T *l));
352static void list_free __ARGS((list_T *l));
353static listitem_T *listitem_alloc __ARGS((void));
354static void listitem_free __ARGS((listitem_T *item));
355static void listitem_remove __ARGS((list_T *l, listitem_T *item));
356static long list_len __ARGS((list_T *l));
357static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
358static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
359static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
360static int string_isa_number __ARGS((char_u *s));
361static listitem_T *list_find __ARGS((list_T *l, long n));
362static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000363static void list_append __ARGS((list_T *l, listitem_T *item));
364static int list_append_tv __ARGS((list_T *l, typval_T *tv));
365static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
366static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
367static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000368static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000369static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
370static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000371static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar33570922005-01-25 22:26:29 +0000372
Bram Moolenaar33570922005-01-25 22:26:29 +0000373static void dict_unref __ARGS((dict_T *d));
374static void dict_free __ARGS((dict_T *d));
375static dictitem_T *dictitem_alloc __ARGS((char_u *key));
376static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
377static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
378static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000379static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000380static int dict_add __ARGS((dict_T *d, dictitem_T *item));
381static long dict_len __ARGS((dict_T *d));
382static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
383static char_u *dict2string __ARGS((typval_T *tv));
384static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
385
386static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
387static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
388static char_u *string_quote __ARGS((char_u *str, int function));
389static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
390static int find_internal_func __ARGS((char_u *name));
391static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
392static 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));
393static 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 +0000394static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000395
396static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
397static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
398static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
399static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
400static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
401static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
402static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
403static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
404static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
405static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
406static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
407static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
408static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
409static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
410static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
411static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
412static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
413static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
414static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
415static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
416static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
417static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
418static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
419static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
420static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
421static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
422static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
423static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
424static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
425static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000426static void f_errorlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
428static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
429static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
430static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
431static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
432static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
433static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
434static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
435static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
436static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
437static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
438static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
439static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
440static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
441static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
442static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
443static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
444static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000487static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000504static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000508#ifdef vim_mkdir
509static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
510#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000516static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
539#ifdef HAVE_STRFTIME
540static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
541#endif
542static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000554static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000570static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000571
572static win_T *find_win_by_nr __ARGS((typval_T *vp));
573static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
574static int get_env_len __ARGS((char_u **arg));
575static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000576static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int incl_br));
578static int eval_isnamec __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000579static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
580static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static typval_T *alloc_tv __ARGS((void));
582static typval_T *alloc_string_tv __ARGS((char_u *string));
583static void free_tv __ARGS((typval_T *varp));
584static void clear_tv __ARGS((typval_T *varp));
585static void init_tv __ARGS((typval_T *varp));
586static long get_tv_number __ARGS((typval_T *varp));
587static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
588static char_u *get_tv_string __ARGS((typval_T *varp));
589static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
590static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000591static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
593static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
594static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
595static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
596static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
597static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
598static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000599static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000601static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
603static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
604static int eval_fname_script __ARGS((char_u *p));
605static int eval_fname_sid __ARGS((char_u *p));
606static void list_func_head __ARGS((ufunc_T *fp, int indent));
607static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
608static ufunc_T *find_func __ARGS((char_u *name));
609static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000610static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000611#ifdef FEAT_PROFILE
612static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000613static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
614static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
615static int
616# ifdef __BORLANDC__
617 _RTLENTRYF
618# endif
619 prof_total_cmp __ARGS((const void *s1, const void *s2));
620static int
621# ifdef __BORLANDC__
622 _RTLENTRYF
623# endif
624 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000625#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000626static int script_autoload __ARGS((char_u *name));
627static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void func_free __ARGS((ufunc_T *fp));
629static void func_unref __ARGS((char_u *name));
630static void func_ref __ARGS((char_u *name));
631static 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));
632static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
633
634static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
635
636static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
637static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
638static char_u *skip_var_one __ARGS((char_u *arg));
639static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
640static void list_glob_vars __ARGS((void));
641static void list_buf_vars __ARGS((void));
642static void list_win_vars __ARGS((void));
643static void list_vim_vars __ARGS((void));
644static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
645static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
646static int check_changedtick __ARGS((char_u *arg));
647static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet));
648static void clear_lval __ARGS((lval_T *lp));
649static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
650static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
651static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
652static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
653static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000654static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000656static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
657static void item_lock __ARGS((typval_T *tv, int deep, int lock));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000658static int tv_islocked __ARGS((typval_T *tv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659
660/*
661 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000662 */
663 void
664eval_init()
665{
Bram Moolenaar33570922005-01-25 22:26:29 +0000666 int i;
667 struct vimvar *p;
668
669 init_var_dict(&globvardict, &globvars_var);
670 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000671 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000672 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000673
674 for (i = 0; i < VV_LEN; ++i)
675 {
676 p = &vimvars[i];
677 STRCPY(p->vv_di.di_key, p->vv_name);
678 if (p->vv_flags & VV_RO)
679 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
680 else if (p->vv_flags & VV_RO_SBX)
681 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
682 else
683 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000684
685 /* add to v: scope dict, unless the value is not always available */
686 if (p->vv_type != VAR_UNKNOWN)
687 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000688 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000689 /* add to compat scope dict */
690 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000691 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000692}
693
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 * Return the name of the executed function.
696 */
697 char_u *
698func_name(cookie)
699 void *cookie;
700{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000701 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702}
703
704/*
705 * Return the address holding the next breakpoint line for a funccall cookie.
706 */
707 linenr_T *
708func_breakpoint(cookie)
709 void *cookie;
710{
Bram Moolenaar33570922005-01-25 22:26:29 +0000711 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712}
713
714/*
715 * Return the address holding the debug tick for a funccall cookie.
716 */
717 int *
718func_dbg_tick(cookie)
719 void *cookie;
720{
Bram Moolenaar33570922005-01-25 22:26:29 +0000721 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722}
723
724/*
725 * Return the nesting level for a funccall cookie.
726 */
727 int
728func_level(cookie)
729 void *cookie;
730{
Bram Moolenaar33570922005-01-25 22:26:29 +0000731 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732}
733
734/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000735funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736
737/*
738 * Return TRUE when a function was ended by a ":return" command.
739 */
740 int
741current_func_returned()
742{
743 return current_funccal->returned;
744}
745
746
747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 * Set an internal variable to a string value. Creates the variable if it does
749 * not already exist.
750 */
751 void
752set_internal_string_var(name, value)
753 char_u *name;
754 char_u *value;
755{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000756 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000757 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758
759 val = vim_strsave(value);
760 if (val != NULL)
761 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000762 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000763 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000765 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000766 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 }
768 }
769}
770
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000771static lval_T *redir_lval = NULL;
772static char_u *redir_endp = NULL;
773static char_u *redir_varname = NULL;
774
775/*
776 * Start recording command output to a variable
777 * Returns OK if successfully completed the setup. FAIL otherwise.
778 */
779 int
780var_redir_start(name, append)
781 char_u *name;
782 int append; /* append to an existing variable */
783{
784 int save_emsg;
785 int err;
786 typval_T tv;
787
788 /* Make sure a valid variable name is specified */
789 if (!eval_isnamec(*name) || VIM_ISDIGIT(*name))
790 {
791 EMSG(_(e_invarg));
792 return FAIL;
793 }
794
795 redir_varname = vim_strsave(name);
796 if (redir_varname == NULL)
797 return FAIL;
798
799 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
800 if (redir_lval == NULL)
801 {
802 var_redir_stop();
803 return FAIL;
804 }
805
806 /* Parse the variable name (can be a dict or list entry). */
807 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE);
808 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
809 {
810 if (redir_endp != NULL && *redir_endp != NUL)
811 /* Trailing characters are present after the variable name */
812 EMSG(_(e_trailing));
813 else
814 EMSG(_(e_invarg));
815 var_redir_stop();
816 return FAIL;
817 }
818
819 /* check if we can write to the variable: set it to or append an empty
820 * string */
821 save_emsg = did_emsg;
822 did_emsg = FALSE;
823 tv.v_type = VAR_STRING;
824 tv.vval.v_string = (char_u *)"";
825 if (append)
826 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
827 else
828 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
829 err = did_emsg;
830 did_emsg += save_emsg;
831 if (err)
832 {
833 var_redir_stop();
834 return FAIL;
835 }
836 if (redir_lval->ll_newkey != NULL)
837 {
838 /* Dictionary item was created, don't do it again. */
839 vim_free(redir_lval->ll_newkey);
840 redir_lval->ll_newkey = NULL;
841 }
842
843 return OK;
844}
845
846/*
847 * Append "value[len]" to the variable set by var_redir_start().
848 */
849 void
850var_redir_str(value, len)
851 char_u *value;
852 int len;
853{
854 char_u *val;
855 typval_T tv;
856 int save_emsg;
857 int err;
858
859 if (redir_lval == NULL)
860 return;
861
862 if (len == -1)
863 /* Append the entire string */
864 val = vim_strsave(value);
865 else
866 /* Append only the specified number of characters */
867 val = vim_strnsave(value, len);
868 if (val == NULL)
869 return;
870
871 tv.v_type = VAR_STRING;
872 tv.vval.v_string = val;
873
874 save_emsg = did_emsg;
875 did_emsg = FALSE;
876 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
877 err = did_emsg;
878 did_emsg += save_emsg;
879 if (err)
880 var_redir_stop();
881
882 vim_free(tv.vval.v_string);
883}
884
885/*
886 * Stop redirecting command output to a variable.
887 */
888 void
889var_redir_stop()
890{
891 if (redir_lval != NULL)
892 {
893 clear_lval(redir_lval);
894 vim_free(redir_lval);
895 redir_lval = NULL;
896 }
897 vim_free(redir_varname);
898 redir_varname = NULL;
899}
900
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901# if defined(FEAT_MBYTE) || defined(PROTO)
902 int
903eval_charconvert(enc_from, enc_to, fname_from, fname_to)
904 char_u *enc_from;
905 char_u *enc_to;
906 char_u *fname_from;
907 char_u *fname_to;
908{
909 int err = FALSE;
910
911 set_vim_var_string(VV_CC_FROM, enc_from, -1);
912 set_vim_var_string(VV_CC_TO, enc_to, -1);
913 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
914 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
915 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
916 err = TRUE;
917 set_vim_var_string(VV_CC_FROM, NULL, -1);
918 set_vim_var_string(VV_CC_TO, NULL, -1);
919 set_vim_var_string(VV_FNAME_IN, NULL, -1);
920 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
921
922 if (err)
923 return FAIL;
924 return OK;
925}
926# endif
927
928# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
929 int
930eval_printexpr(fname, args)
931 char_u *fname;
932 char_u *args;
933{
934 int err = FALSE;
935
936 set_vim_var_string(VV_FNAME_IN, fname, -1);
937 set_vim_var_string(VV_CMDARG, args, -1);
938 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
939 err = TRUE;
940 set_vim_var_string(VV_FNAME_IN, NULL, -1);
941 set_vim_var_string(VV_CMDARG, NULL, -1);
942
943 if (err)
944 {
945 mch_remove(fname);
946 return FAIL;
947 }
948 return OK;
949}
950# endif
951
952# if defined(FEAT_DIFF) || defined(PROTO)
953 void
954eval_diff(origfile, newfile, outfile)
955 char_u *origfile;
956 char_u *newfile;
957 char_u *outfile;
958{
959 int err = FALSE;
960
961 set_vim_var_string(VV_FNAME_IN, origfile, -1);
962 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
963 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
964 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
965 set_vim_var_string(VV_FNAME_IN, NULL, -1);
966 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
967 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
968}
969
970 void
971eval_patch(origfile, difffile, outfile)
972 char_u *origfile;
973 char_u *difffile;
974 char_u *outfile;
975{
976 int err;
977
978 set_vim_var_string(VV_FNAME_IN, origfile, -1);
979 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
980 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
981 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
982 set_vim_var_string(VV_FNAME_IN, NULL, -1);
983 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
984 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
985}
986# endif
987
988/*
989 * Top level evaluation function, returning a boolean.
990 * Sets "error" to TRUE if there was an error.
991 * Return TRUE or FALSE.
992 */
993 int
994eval_to_bool(arg, error, nextcmd, skip)
995 char_u *arg;
996 int *error;
997 char_u **nextcmd;
998 int skip; /* only parse, don't execute */
999{
Bram Moolenaar33570922005-01-25 22:26:29 +00001000 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 int retval = FALSE;
1002
1003 if (skip)
1004 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001005 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 else
1008 {
1009 *error = FALSE;
1010 if (!skip)
1011 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001012 retval = (get_tv_number(&tv) != 0);
1013 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 }
1015 }
1016 if (skip)
1017 --emsg_skip;
1018
1019 return retval;
1020}
1021
1022/*
1023 * Top level evaluation function, returning a string. If "skip" is TRUE,
1024 * only parsing to "nextcmd" is done, without reporting errors. Return
1025 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1026 */
1027 char_u *
1028eval_to_string_skip(arg, nextcmd, skip)
1029 char_u *arg;
1030 char_u **nextcmd;
1031 int skip; /* only parse, don't execute */
1032{
Bram Moolenaar33570922005-01-25 22:26:29 +00001033 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 char_u *retval;
1035
1036 if (skip)
1037 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001038 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 retval = NULL;
1040 else
1041 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001042 retval = vim_strsave(get_tv_string(&tv));
1043 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 }
1045 if (skip)
1046 --emsg_skip;
1047
1048 return retval;
1049}
1050
1051/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001052 * Skip over an expression at "*pp".
1053 * Return FAIL for an error, OK otherwise.
1054 */
1055 int
1056skip_expr(pp)
1057 char_u **pp;
1058{
Bram Moolenaar33570922005-01-25 22:26:29 +00001059 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001060
1061 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001062 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001063}
1064
1065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 * Top level evaluation function, returning a string.
1067 * Return pointer to allocated memory, or NULL for failure.
1068 */
1069 char_u *
1070eval_to_string(arg, nextcmd)
1071 char_u *arg;
1072 char_u **nextcmd;
1073{
Bram Moolenaar33570922005-01-25 22:26:29 +00001074 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 char_u *retval;
1076
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001077 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 retval = NULL;
1079 else
1080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001081 retval = vim_strsave(get_tv_string(&tv));
1082 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 }
1084
1085 return retval;
1086}
1087
1088/*
1089 * Call eval_to_string() with "sandbox" set and not using local variables.
1090 */
1091 char_u *
1092eval_to_string_safe(arg, nextcmd)
1093 char_u *arg;
1094 char_u **nextcmd;
1095{
1096 char_u *retval;
1097 void *save_funccalp;
1098
1099 save_funccalp = save_funccal();
1100 ++sandbox;
1101 retval = eval_to_string(arg, nextcmd);
1102 --sandbox;
1103 restore_funccal(save_funccalp);
1104 return retval;
1105}
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107/*
1108 * Top level evaluation function, returning a number.
1109 * Evaluates "expr" silently.
1110 * Returns -1 for an error.
1111 */
1112 int
1113eval_to_number(expr)
1114 char_u *expr;
1115{
Bram Moolenaar33570922005-01-25 22:26:29 +00001116 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001118 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119
1120 ++emsg_off;
1121
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001122 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 retval = -1;
1124 else
1125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001126 retval = get_tv_number(&rettv);
1127 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 }
1129 --emsg_off;
1130
1131 return retval;
1132}
1133
1134#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1135/*
1136 * Call some vimL function and return the result as a string
1137 * Uses argv[argc] for the function arguments.
1138 */
1139 char_u *
1140call_vim_function(func, argc, argv, safe)
1141 char_u *func;
1142 int argc;
1143 char_u **argv;
1144 int safe; /* use the sandbox */
1145{
1146 char_u *retval = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001147 typval_T rettv;
1148 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 long n;
1150 int len;
1151 int i;
1152 int doesrange;
1153 void *save_funccalp = NULL;
1154
Bram Moolenaar33570922005-01-25 22:26:29 +00001155 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (argvars == NULL)
1157 return NULL;
1158
1159 for (i = 0; i < argc; i++)
1160 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001161 /* Pass a NULL or empty argument as an empty string */
1162 if (argv[i] == NULL || *argv[i] == NUL)
1163 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001164 argvars[i].v_type = VAR_STRING;
1165 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001166 continue;
1167 }
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 /* Recognize a number argument, the others must be strings. */
1170 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1171 if (len != 0 && len == (int)STRLEN(argv[i]))
1172 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001173 argvars[i].v_type = VAR_NUMBER;
1174 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 }
1176 else
1177 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001178 argvars[i].v_type = VAR_STRING;
1179 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 }
1181 }
1182
1183 if (safe)
1184 {
1185 save_funccalp = save_funccal();
1186 ++sandbox;
1187 }
1188
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001189 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1190 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001192 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001193 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001195 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 vim_free(argvars);
1197
1198 if (safe)
1199 {
1200 --sandbox;
1201 restore_funccal(save_funccalp);
1202 }
1203 return retval;
1204}
1205#endif
1206
1207/*
1208 * Save the current function call pointer, and set it to NULL.
1209 * Used when executing autocommands and for ":source".
1210 */
1211 void *
1212save_funccal()
1213{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001214 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 current_funccal = NULL;
1217 return (void *)fc;
1218}
1219
1220 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001221restore_funccal(vfc)
1222 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001224 funccall_T *fc = (funccall_T *)vfc;
1225
1226 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227}
1228
Bram Moolenaar05159a02005-02-26 23:04:13 +00001229#if defined(FEAT_PROFILE) || defined(PROTO)
1230/*
1231 * Prepare profiling for entering a child or something else that is not
1232 * counted for the script/function itself.
1233 * Should always be called in pair with prof_child_exit().
1234 */
1235 void
1236prof_child_enter(tm)
1237 proftime_T *tm; /* place to store waittime */
1238{
1239 funccall_T *fc = current_funccal;
1240
1241 if (fc != NULL && fc->func->uf_profiling)
1242 profile_start(&fc->prof_child);
1243 script_prof_save(tm);
1244}
1245
1246/*
1247 * Take care of time spent in a child.
1248 * Should always be called after prof_child_enter().
1249 */
1250 void
1251prof_child_exit(tm)
1252 proftime_T *tm; /* where waittime was stored */
1253{
1254 funccall_T *fc = current_funccal;
1255
1256 if (fc != NULL && fc->func->uf_profiling)
1257 {
1258 profile_end(&fc->prof_child);
1259 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1260 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1261 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1262 }
1263 script_prof_restore(tm);
1264}
1265#endif
1266
1267
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268#ifdef FEAT_FOLDING
1269/*
1270 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1271 * it in "*cp". Doesn't give error messages.
1272 */
1273 int
1274eval_foldexpr(arg, cp)
1275 char_u *arg;
1276 int *cp;
1277{
Bram Moolenaar33570922005-01-25 22:26:29 +00001278 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 int retval;
1280 char_u *s;
1281
1282 ++emsg_off;
1283 ++sandbox;
1284 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001285 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 retval = 0;
1287 else
1288 {
1289 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 if (tv.v_type == VAR_NUMBER)
1291 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001292 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 retval = 0;
1294 else
1295 {
1296 /* If the result is a string, check if there is a non-digit before
1297 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001298 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if (!VIM_ISDIGIT(*s) && *s != '-')
1300 *cp = *s++;
1301 retval = atol((char *)s);
1302 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 }
1305 --emsg_off;
1306 --sandbox;
1307
1308 return retval;
1309}
1310#endif
1311
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 * ":let" list all variable values
1314 * ":let var1 var2" list variable values
1315 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001316 * ":let var += expr" assignment command.
1317 * ":let var -= expr" assignment command.
1318 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 */
1321 void
1322ex_let(eap)
1323 exarg_T *eap;
1324{
1325 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001327 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 int var_count = 0;
1330 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001331 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001333 expr = skip_var_list(arg, &var_count, &semicolon);
1334 if (expr == NULL)
1335 return;
1336 expr = vim_strchr(expr, '=');
1337 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001339 /*
1340 * ":let" without "=": list variables
1341 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001342 if (*arg == '[')
1343 EMSG(_(e_invarg));
1344 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001345 /* ":let var1 var2" */
1346 arg = list_arg_vars(eap, arg);
1347 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001348 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001349 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001350 list_glob_vars();
1351 list_buf_vars();
1352 list_win_vars();
1353 list_vim_vars();
1354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 eap->nextcmd = check_nextcmd(arg);
1356 }
1357 else
1358 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001359 op[0] = '=';
1360 op[1] = NUL;
1361 if (expr > arg)
1362 {
1363 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1364 op[0] = expr[-1]; /* +=, -= or .= */
1365 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001366 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001367
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 if (eap->skip)
1369 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001370 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (eap->skip)
1372 {
1373 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001374 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 --emsg_skip;
1376 }
1377 else if (i != FAIL)
1378 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001379 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001380 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001381 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
1383 }
1384}
1385
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001386/*
1387 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1388 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001389 * When "nextchars" is not NULL it points to a string with characters that
1390 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1391 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001392 * Returns OK or FAIL;
1393 */
1394 static int
1395ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1396 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001397 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001398 int copy; /* copy values from "tv", don't move */
1399 int semicolon; /* from skip_var_list() */
1400 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001401 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001402{
1403 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001404 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001405 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001406 listitem_T *item;
1407 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001408
1409 if (*arg != '[')
1410 {
1411 /*
1412 * ":let var = expr" or ":for var in list"
1413 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001414 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001415 return FAIL;
1416 return OK;
1417 }
1418
1419 /*
1420 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1421 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001422 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001423 {
1424 EMSG(_(e_listreq));
1425 return FAIL;
1426 }
1427
1428 i = list_len(l);
1429 if (semicolon == 0 && var_count < i)
1430 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001431 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001432 return FAIL;
1433 }
1434 if (var_count - semicolon > i)
1435 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001436 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001437 return FAIL;
1438 }
1439
1440 item = l->lv_first;
1441 while (*arg != ']')
1442 {
1443 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001444 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001445 item = item->li_next;
1446 if (arg == NULL)
1447 return FAIL;
1448
1449 arg = skipwhite(arg);
1450 if (*arg == ';')
1451 {
1452 /* Put the rest of the list (may be empty) in the var after ';'.
1453 * Create a new list for this. */
1454 l = list_alloc();
1455 if (l == NULL)
1456 return FAIL;
1457 while (item != NULL)
1458 {
1459 list_append_tv(l, &item->li_tv);
1460 item = item->li_next;
1461 }
1462
1463 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001464 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001465 ltv.vval.v_list = l;
1466 l->lv_refcount = 1;
1467
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001468 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1469 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001470 clear_tv(&ltv);
1471 if (arg == NULL)
1472 return FAIL;
1473 break;
1474 }
1475 else if (*arg != ',' && *arg != ']')
1476 {
1477 EMSG2(_(e_intern2), "ex_let_vars()");
1478 return FAIL;
1479 }
1480 }
1481
1482 return OK;
1483}
1484
1485/*
1486 * Skip over assignable variable "var" or list of variables "[var, var]".
1487 * Used for ":let varvar = expr" and ":for varvar in expr".
1488 * For "[var, var]" increment "*var_count" for each variable.
1489 * for "[var, var; var]" set "semicolon".
1490 * Return NULL for an error.
1491 */
1492 static char_u *
1493skip_var_list(arg, var_count, semicolon)
1494 char_u *arg;
1495 int *var_count;
1496 int *semicolon;
1497{
1498 char_u *p, *s;
1499
1500 if (*arg == '[')
1501 {
1502 /* "[var, var]": find the matching ']'. */
1503 p = arg;
1504 while (1)
1505 {
1506 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1507 s = skip_var_one(p);
1508 if (s == p)
1509 {
1510 EMSG2(_(e_invarg2), p);
1511 return NULL;
1512 }
1513 ++*var_count;
1514
1515 p = skipwhite(s);
1516 if (*p == ']')
1517 break;
1518 else if (*p == ';')
1519 {
1520 if (*semicolon == 1)
1521 {
1522 EMSG(_("Double ; in list of variables"));
1523 return NULL;
1524 }
1525 *semicolon = 1;
1526 }
1527 else if (*p != ',')
1528 {
1529 EMSG2(_(e_invarg2), p);
1530 return NULL;
1531 }
1532 }
1533 return p + 1;
1534 }
1535 else
1536 return skip_var_one(arg);
1537}
1538
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001539/*
1540 * Skip one (assignable) variable name, includig $VAR, d.key, l[idx].
1541 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001542 static char_u *
1543skip_var_one(arg)
1544 char_u *arg;
1545{
1546 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1547 ++arg;
1548 return find_name_end(arg, NULL, NULL, TRUE);
1549}
1550
Bram Moolenaara7043832005-01-21 11:56:39 +00001551/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001552 * List variables for hashtab "ht" with prefix "prefix".
1553 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001554 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001555 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001556list_hashtable_vars(ht, prefix, empty)
1557 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001558 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001559 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001560{
Bram Moolenaar33570922005-01-25 22:26:29 +00001561 hashitem_T *hi;
1562 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001563 int todo;
1564
1565 todo = ht->ht_used;
1566 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1567 {
1568 if (!HASHITEM_EMPTY(hi))
1569 {
1570 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001571 di = HI2DI(hi);
1572 if (empty || di->di_tv.v_type != VAR_STRING
1573 || di->di_tv.vval.v_string != NULL)
1574 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001575 }
1576 }
1577}
1578
1579/*
1580 * List global variables.
1581 */
1582 static void
1583list_glob_vars()
1584{
Bram Moolenaar33570922005-01-25 22:26:29 +00001585 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001586}
1587
1588/*
1589 * List buffer variables.
1590 */
1591 static void
1592list_buf_vars()
1593{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001594 char_u numbuf[NUMBUFLEN];
1595
Bram Moolenaar33570922005-01-25 22:26:29 +00001596 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001597
1598 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1599 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001600}
1601
1602/*
1603 * List window variables.
1604 */
1605 static void
1606list_win_vars()
1607{
Bram Moolenaar33570922005-01-25 22:26:29 +00001608 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001609}
1610
1611/*
1612 * List Vim variables.
1613 */
1614 static void
1615list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001616{
Bram Moolenaar33570922005-01-25 22:26:29 +00001617 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001618}
1619
1620/*
1621 * List variables in "arg".
1622 */
1623 static char_u *
1624list_arg_vars(eap, arg)
1625 exarg_T *eap;
1626 char_u *arg;
1627{
1628 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001629 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001630 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001631 char_u *name_start;
1632 char_u *arg_subsc;
1633 char_u *tofree;
1634 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001635
1636 while (!ends_excmd(*arg) && !got_int)
1637 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001638 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001639 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001640 arg = find_name_end(arg, NULL, NULL, TRUE);
1641 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1642 {
1643 emsg_severe = TRUE;
1644 EMSG(_(e_trailing));
1645 break;
1646 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001647 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001648 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001649 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001650 /* get_name_len() takes care of expanding curly braces */
1651 name_start = name = arg;
1652 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1653 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001654 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001655 /* This is mainly to keep test 49 working: when expanding
1656 * curly braces fails overrule the exception error message. */
1657 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001659 emsg_severe = TRUE;
1660 EMSG2(_(e_invarg2), arg);
1661 break;
1662 }
1663 error = TRUE;
1664 }
1665 else
1666 {
1667 if (tofree != NULL)
1668 name = tofree;
1669 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001670 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001671 else
1672 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001673 /* handle d.key, l[idx], f(expr) */
1674 arg_subsc = arg;
1675 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001676 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001677 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001678 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001679 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001680 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001681 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001682 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001683 case 'g': list_glob_vars(); break;
1684 case 'b': list_buf_vars(); break;
1685 case 'w': list_win_vars(); break;
1686 case 'v': list_vim_vars(); break;
1687 default:
1688 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001689 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001690 }
1691 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001692 {
1693 char_u numbuf[NUMBUFLEN];
1694 char_u *tf;
1695 int c;
1696 char_u *s;
1697
1698 s = echo_string(&tv, &tf, numbuf);
1699 c = *arg;
1700 *arg = NUL;
1701 list_one_var_a((char_u *)"",
1702 arg == arg_subsc ? name : name_start,
1703 tv.v_type, s == NULL ? (char_u *)"" : s);
1704 *arg = c;
1705 vim_free(tf);
1706 }
1707 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001708 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001709 }
1710 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001711
1712 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001713 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001714
1715 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001716 }
1717
1718 return arg;
1719}
1720
1721/*
1722 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1723 * Returns a pointer to the char just after the var name.
1724 * Returns NULL if there is an error.
1725 */
1726 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001727ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001728 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001729 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001730 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001731 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001732 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001733{
1734 int c1;
1735 char_u *name;
1736 char_u *p;
1737 char_u *arg_end = NULL;
1738 int len;
1739 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001740 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001741
1742 /*
1743 * ":let $VAR = expr": Set environment variable.
1744 */
1745 if (*arg == '$')
1746 {
1747 /* Find the end of the name. */
1748 ++arg;
1749 name = arg;
1750 len = get_env_len(&arg);
1751 if (len == 0)
1752 EMSG2(_(e_invarg2), name - 1);
1753 else
1754 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001755 if (op != NULL && (*op == '+' || *op == '-'))
1756 EMSG2(_(e_letwrong), op);
1757 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001758 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 EMSG(_(e_letunexp));
1760 else
1761 {
1762 c1 = name[len];
1763 name[len] = NUL;
1764 p = get_tv_string(tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001765 if (op != NULL && *op == '.')
1766 {
1767 int mustfree = FALSE;
1768 char_u *s = vim_getenv(name, &mustfree);
1769
1770 if (s != NULL)
1771 {
1772 p = tofree = concat_str(s, p);
1773 if (mustfree)
1774 vim_free(s);
1775 }
1776 }
1777 if (p != NULL)
1778 vim_setenv(name, p);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779 if (STRICMP(name, "HOME") == 0)
1780 init_homedir();
1781 else if (didset_vim && STRICMP(name, "VIM") == 0)
1782 didset_vim = FALSE;
1783 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1784 didset_vimruntime = FALSE;
1785 name[len] = c1;
1786 arg_end = arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001787 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 }
1789 }
1790 }
1791
1792 /*
1793 * ":let &option = expr": Set option value.
1794 * ":let &l:option = expr": Set local option value.
1795 * ":let &g:option = expr": Set global option value.
1796 */
1797 else if (*arg == '&')
1798 {
1799 /* Find the end of the name. */
1800 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801 if (p == NULL || (endchars != NULL
1802 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 EMSG(_(e_letunexp));
1804 else
1805 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001806 long n;
1807 int opt_type;
1808 long numval;
1809 char_u *stringval = NULL;
1810 char_u *s;
1811
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 c1 = *p;
1813 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001814
1815 n = get_tv_number(tv);
1816 s = get_tv_string(tv);
1817 if (op != NULL && *op != '=')
1818 {
1819 opt_type = get_option_value(arg, &numval,
1820 &stringval, opt_flags);
1821 if ((opt_type == 1 && *op == '.')
1822 || (opt_type == 0 && *op != '.'))
1823 EMSG2(_(e_letwrong), op);
1824 else
1825 {
1826 if (opt_type == 1) /* number */
1827 {
1828 if (*op == '+')
1829 n = numval + n;
1830 else
1831 n = numval - n;
1832 }
1833 else if (opt_type == 0 && stringval != NULL) /* string */
1834 {
1835 s = concat_str(stringval, s);
1836 vim_free(stringval);
1837 stringval = s;
1838 }
1839 }
1840 }
1841 set_option_value(arg, n, s, opt_flags);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842 *p = c1;
1843 arg_end = p;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001844 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001845 }
1846 }
1847
1848 /*
1849 * ":let @r = expr": Set register contents.
1850 */
1851 else if (*arg == '@')
1852 {
1853 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001854 if (op != NULL && (*op == '+' || *op == '-'))
1855 EMSG2(_(e_letwrong), op);
1856 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 EMSG(_(e_letunexp));
1859 else
1860 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001861 char_u *tofree = NULL;
1862 char_u *s;
1863
1864 p = get_tv_string(tv);
1865 if (op != NULL && *op == '.')
1866 {
1867 s = get_reg_contents(*arg == '@' ? '"' : *arg, FALSE);
1868 if (s != NULL)
1869 {
1870 p = tofree = concat_str(s, p);
1871 vim_free(s);
1872 }
1873 }
1874 if (p != NULL)
1875 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 arg_end = arg + 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001877 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 }
1879 }
1880
1881 /*
1882 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001883 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00001885 else if ((eval_isnamec(*arg) && !VIM_ISDIGIT(*arg)) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001887 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001889 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE);
1890 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001892 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1893 EMSG(_(e_letunexp));
1894 else
1895 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001896 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001897 arg_end = p;
1898 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001900 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 }
1902
1903 else
1904 EMSG2(_(e_invarg2), arg);
1905
1906 return arg_end;
1907}
1908
1909/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001910 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1911 */
1912 static int
1913check_changedtick(arg)
1914 char_u *arg;
1915{
1916 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1917 {
1918 EMSG2(_(e_readonlyvar), arg);
1919 return TRUE;
1920 }
1921 return FALSE;
1922}
1923
1924/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001925 * Get an lval: variable, Dict item or List item that can be assigned a value
1926 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1927 * "name.key", "name.key[expr]" etc.
1928 * Indexing only works if "name" is an existing List or Dictionary.
1929 * "name" points to the start of the name.
1930 * If "rettv" is not NULL it points to the value to be assigned.
1931 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1932 * wrong; must end in space or cmd separator.
1933 *
1934 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001935 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001936 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 */
1938 static char_u *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001939get_lval(name, rettv, lp, unlet, skip, quiet)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00001941 typval_T *rettv;
1942 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001943 int unlet;
1944 int skip;
1945 int quiet; /* don't give error messages */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001948 char_u *expr_start, *expr_end;
1949 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001950 dictitem_T *v;
1951 typval_T var1;
1952 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001953 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001954 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001955 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001956 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001957 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001959 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001960 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001961
1962 if (skip)
1963 {
1964 /* When skipping just find the end of the name. */
1965 lp->ll_name = name;
1966 return find_name_end(name, NULL, NULL, TRUE);
1967 }
1968
1969 /* Find the end of the name. */
1970 p = find_name_end(name, &expr_start, &expr_end, FALSE);
1971 if (expr_start != NULL)
1972 {
1973 /* Don't expand the name when we already know there is an error. */
1974 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1975 && *p != '[' && *p != '.')
1976 {
1977 EMSG(_(e_trailing));
1978 return NULL;
1979 }
1980
1981 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1982 if (lp->ll_exp_name == NULL)
1983 {
1984 /* Report an invalid expression in braces, unless the
1985 * expression evaluation has been cancelled due to an
1986 * aborting error, an interrupt, or an exception. */
1987 if (!aborting() && !quiet)
1988 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001989 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 EMSG2(_(e_invarg2), name);
1991 return NULL;
1992 }
1993 }
1994 lp->ll_name = lp->ll_exp_name;
1995 }
1996 else
1997 lp->ll_name = name;
1998
1999 /* Without [idx] or .key we are done. */
2000 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2001 return p;
2002
2003 cc = *p;
2004 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002005 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002006 if (v == NULL && !quiet)
2007 EMSG2(_(e_undefvar), lp->ll_name);
2008 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002009 if (v == NULL)
2010 return NULL;
2011
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002012 /*
2013 * Loop until no more [idx] or .key is following.
2014 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002015 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002016 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002017 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2019 && !(lp->ll_tv->v_type == VAR_DICT
2020 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002021 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002022 if (!quiet)
2023 EMSG(_("E689: Can only index a List or Dictionary"));
2024 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002025 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002026 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002027 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002028 if (!quiet)
2029 EMSG(_("E708: [:] must come last"));
2030 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002032
Bram Moolenaar8c711452005-01-14 21:53:12 +00002033 len = -1;
2034 if (*p == '.')
2035 {
2036 key = p + 1;
2037 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2038 ;
2039 if (len == 0)
2040 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002041 if (!quiet)
2042 EMSG(_(e_emptykey));
2043 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002044 }
2045 p = key + len;
2046 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002047 else
2048 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002049 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002050 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002051 if (*p == ':')
2052 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002053 else
2054 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002055 empty1 = FALSE;
2056 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002057 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002058 }
2059
2060 /* Optionally get the second index [ :expr]. */
2061 if (*p == ':')
2062 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002065 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002066 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002067 if (!empty1)
2068 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002069 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002070 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 if (rettv != NULL && (rettv->v_type != VAR_LIST
2072 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002073 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002074 if (!quiet)
2075 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002076 if (!empty1)
2077 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002078 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002079 }
2080 p = skipwhite(p + 1);
2081 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002082 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002083 else
2084 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002085 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002086 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2087 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002088 if (!empty1)
2089 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002090 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002091 }
2092 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002093 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002094 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002095 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002096 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002097
Bram Moolenaar8c711452005-01-14 21:53:12 +00002098 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002099 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002100 if (!quiet)
2101 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002102 if (!empty1)
2103 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002104 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002105 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002106 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002107 }
2108
2109 /* Skip to past ']'. */
2110 ++p;
2111 }
2112
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002113 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002114 {
2115 if (len == -1)
2116 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002117 /* "[key]": get key from "var1" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002118 key = get_tv_string(&var1);
2119 if (*key == NUL)
2120 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002121 if (!quiet)
2122 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002123 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002124 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002125 }
2126 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002127 lp->ll_list = NULL;
2128 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002129 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002130 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002131 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002132 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002133 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002134 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002136 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002137 if (len == -1)
2138 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002140 }
2141 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002142 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002143 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002144 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002145 if (len == -1)
2146 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002147 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002148 p = NULL;
2149 break;
2150 }
2151 if (len == -1)
2152 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002153 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002154 }
2155 else
2156 {
2157 /*
2158 * Get the number and item for the only or first index of the List.
2159 */
2160 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002161 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002162 else
2163 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002164 lp->ll_n1 = get_tv_number(&var1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002165 clear_tv(&var1);
2166 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002167 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002168 lp->ll_list = lp->ll_tv->vval.v_list;
2169 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2170 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002171 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002172 if (!quiet)
2173 EMSGN(_(e_listidx), lp->ll_n1);
2174 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002175 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002176 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002177 }
2178
2179 /*
2180 * May need to find the item or absolute index for the second
2181 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182 * When no index given: "lp->ll_empty2" is TRUE.
2183 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002184 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002185 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002186 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 lp->ll_n2 = get_tv_number(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002188 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002189 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002190 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002192 if (ni == NULL)
2193 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002194 if (!quiet)
2195 EMSGN(_(e_listidx), lp->ll_n2);
2196 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002197 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002198 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002199 }
2200
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002201 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2202 if (lp->ll_n1 < 0)
2203 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2204 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002205 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002206 if (!quiet)
2207 EMSGN(_(e_listidx), lp->ll_n2);
2208 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002209 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002210 }
2211
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002212 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002213 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 }
2215
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 return p;
2217}
2218
2219/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002220 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002221 */
2222 static void
2223clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002224 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225{
2226 vim_free(lp->ll_exp_name);
2227 vim_free(lp->ll_newkey);
2228}
2229
2230/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002231 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002232 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002233 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 */
2235 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002236set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002237 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002238 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002239 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002240 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002241 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002242{
2243 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002244 listitem_T *ni;
2245 listitem_T *ri;
2246 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247
2248 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002250 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252 cc = *endp;
2253 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002254 if (op != NULL && *op != '=')
2255 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002256 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002257
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002258 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2260 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002261 {
2262 if (tv_op(&tv, rettv, op) == OK)
2263 set_var(lp->ll_name, &tv, FALSE);
2264 clear_tv(&tv);
2265 }
2266 }
2267 else
2268 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002271 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002272 else if (tv_check_lock(lp->ll_newkey == NULL
2273 ? lp->ll_tv->v_lock
2274 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2275 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002276 else if (lp->ll_range)
2277 {
2278 /*
2279 * Assign the List values to the list items.
2280 */
2281 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002282 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002283 if (op != NULL && *op != '=')
2284 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2285 else
2286 {
2287 clear_tv(&lp->ll_li->li_tv);
2288 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2289 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002290 ri = ri->li_next;
2291 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2292 break;
2293 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002294 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 /* Need to add an empty item. */
2296 ni = listitem_alloc();
2297 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002298 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002299 ri = NULL;
2300 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002301 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002302 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002303 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 ni->li_tv.vval.v_number = 0;
2305 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002306 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 lp->ll_li = lp->ll_li->li_next;
2308 ++lp->ll_n1;
2309 }
2310 if (ri != NULL)
2311 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002312 else if (lp->ll_empty2
2313 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 : lp->ll_n1 != lp->ll_n2)
2315 EMSG(_("E711: List value has not enough items"));
2316 }
2317 else
2318 {
2319 /*
2320 * Assign to a List or Dictionary item.
2321 */
2322 if (lp->ll_newkey != NULL)
2323 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002324 if (op != NULL && *op != '=')
2325 {
2326 EMSG2(_(e_letwrong), op);
2327 return;
2328 }
2329
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002331 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 if (di == NULL)
2333 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002334 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2335 {
2336 vim_free(di);
2337 return;
2338 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002339 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002340 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341 else if (op != NULL && *op != '=')
2342 {
2343 tv_op(lp->ll_tv, rettv, op);
2344 return;
2345 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002347 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002348
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 /*
2350 * Assign the value to the variable or list item.
2351 */
2352 if (copy)
2353 copy_tv(rettv, lp->ll_tv);
2354 else
2355 {
2356 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002357 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002358 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 }
2360 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361}
2362
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002363/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2365 * Returns OK or FAIL.
2366 */
2367 static int
2368tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002369 typval_T *tv1;
2370 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002371 char_u *op;
2372{
2373 long n;
2374 char_u numbuf[NUMBUFLEN];
2375 char_u *s;
2376
2377 /* Can't do anything with a Funcref or a Dict on the right. */
2378 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2379 {
2380 switch (tv1->v_type)
2381 {
2382 case VAR_DICT:
2383 case VAR_FUNC:
2384 break;
2385
2386 case VAR_LIST:
2387 if (*op != '+' || tv2->v_type != VAR_LIST)
2388 break;
2389 /* List += List */
2390 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2391 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2392 return OK;
2393
2394 case VAR_NUMBER:
2395 case VAR_STRING:
2396 if (tv2->v_type == VAR_LIST)
2397 break;
2398 if (*op == '+' || *op == '-')
2399 {
2400 /* nr += nr or nr -= nr*/
2401 n = get_tv_number(tv1);
2402 if (*op == '+')
2403 n += get_tv_number(tv2);
2404 else
2405 n -= get_tv_number(tv2);
2406 clear_tv(tv1);
2407 tv1->v_type = VAR_NUMBER;
2408 tv1->vval.v_number = n;
2409 }
2410 else
2411 {
2412 /* str .= str */
2413 s = get_tv_string(tv1);
2414 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2415 clear_tv(tv1);
2416 tv1->v_type = VAR_STRING;
2417 tv1->vval.v_string = s;
2418 }
2419 return OK;
2420 }
2421 }
2422
2423 EMSG2(_(e_letwrong), op);
2424 return FAIL;
2425}
2426
2427/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002428 * Add a watcher to a list.
2429 */
2430 static void
2431list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002432 list_T *l;
2433 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002434{
2435 lw->lw_next = l->lv_watch;
2436 l->lv_watch = lw;
2437}
2438
2439/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002440 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002441 * No warning when it isn't found...
2442 */
2443 static void
2444list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002445 list_T *l;
2446 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002447{
Bram Moolenaar33570922005-01-25 22:26:29 +00002448 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002449
2450 lwp = &l->lv_watch;
2451 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2452 {
2453 if (lw == lwrem)
2454 {
2455 *lwp = lw->lw_next;
2456 break;
2457 }
2458 lwp = &lw->lw_next;
2459 }
2460}
2461
2462/*
2463 * Just before removing an item from a list: advance watchers to the next
2464 * item.
2465 */
2466 static void
2467list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002468 list_T *l;
2469 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002470{
Bram Moolenaar33570922005-01-25 22:26:29 +00002471 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002472
2473 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2474 if (lw->lw_item == item)
2475 lw->lw_item = item->li_next;
2476}
2477
2478/*
2479 * Evaluate the expression used in a ":for var in expr" command.
2480 * "arg" points to "var".
2481 * Set "*errp" to TRUE for an error, FALSE otherwise;
2482 * Return a pointer that holds the info. Null when there is an error.
2483 */
2484 void *
2485eval_for_line(arg, errp, nextcmdp, skip)
2486 char_u *arg;
2487 int *errp;
2488 char_u **nextcmdp;
2489 int skip;
2490{
Bram Moolenaar33570922005-01-25 22:26:29 +00002491 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002492 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002493 typval_T tv;
2494 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002495
2496 *errp = TRUE; /* default: there is an error */
2497
Bram Moolenaar33570922005-01-25 22:26:29 +00002498 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002499 if (fi == NULL)
2500 return NULL;
2501
2502 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2503 if (expr == NULL)
2504 return fi;
2505
2506 expr = skipwhite(expr);
2507 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2508 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002509 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002510 return fi;
2511 }
2512
2513 if (skip)
2514 ++emsg_skip;
2515 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2516 {
2517 *errp = FALSE;
2518 if (!skip)
2519 {
2520 l = tv.vval.v_list;
2521 if (tv.v_type != VAR_LIST || l == NULL)
2522 EMSG(_(e_listreq));
2523 else
2524 {
2525 fi->fi_list = l;
2526 list_add_watch(l, &fi->fi_lw);
2527 fi->fi_lw.lw_item = l->lv_first;
2528 }
2529 }
2530 }
2531 if (skip)
2532 --emsg_skip;
2533
2534 return fi;
2535}
2536
2537/*
2538 * Use the first item in a ":for" list. Advance to the next.
2539 * Assign the values to the variable (list). "arg" points to the first one.
2540 * Return TRUE when a valid item was found, FALSE when at end of list or
2541 * something wrong.
2542 */
2543 int
2544next_for_item(fi_void, arg)
2545 void *fi_void;
2546 char_u *arg;
2547{
Bram Moolenaar33570922005-01-25 22:26:29 +00002548 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002549 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002550 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002551
2552 item = fi->fi_lw.lw_item;
2553 if (item == NULL)
2554 result = FALSE;
2555 else
2556 {
2557 fi->fi_lw.lw_item = item->li_next;
2558 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2559 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2560 }
2561 return result;
2562}
2563
2564/*
2565 * Free the structure used to store info used by ":for".
2566 */
2567 void
2568free_for_info(fi_void)
2569 void *fi_void;
2570{
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002572
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002573 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002574 list_rem_watch(fi->fi_list, &fi->fi_lw);
2575 vim_free(fi);
2576}
2577
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2579
2580 void
2581set_context_for_expression(xp, arg, cmdidx)
2582 expand_T *xp;
2583 char_u *arg;
2584 cmdidx_T cmdidx;
2585{
2586 int got_eq = FALSE;
2587 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002588 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002590 if (cmdidx == CMD_let)
2591 {
2592 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002593 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002594 {
2595 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002596 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002597 {
2598 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002599 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002600 if (vim_iswhite(*p))
2601 break;
2602 }
2603 return;
2604 }
2605 }
2606 else
2607 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2608 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 while ((xp->xp_pattern = vim_strpbrk(arg,
2610 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2611 {
2612 c = *xp->xp_pattern;
2613 if (c == '&')
2614 {
2615 c = xp->xp_pattern[1];
2616 if (c == '&')
2617 {
2618 ++xp->xp_pattern;
2619 xp->xp_context = cmdidx != CMD_let || got_eq
2620 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2621 }
2622 else if (c != ' ')
2623 xp->xp_context = EXPAND_SETTINGS;
2624 }
2625 else if (c == '$')
2626 {
2627 /* environment variable */
2628 xp->xp_context = EXPAND_ENV_VARS;
2629 }
2630 else if (c == '=')
2631 {
2632 got_eq = TRUE;
2633 xp->xp_context = EXPAND_EXPRESSION;
2634 }
2635 else if (c == '<'
2636 && xp->xp_context == EXPAND_FUNCTIONS
2637 && vim_strchr(xp->xp_pattern, '(') == NULL)
2638 {
2639 /* Function name can start with "<SNR>" */
2640 break;
2641 }
2642 else if (cmdidx != CMD_let || got_eq)
2643 {
2644 if (c == '"') /* string */
2645 {
2646 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2647 if (c == '\\' && xp->xp_pattern[1] != NUL)
2648 ++xp->xp_pattern;
2649 xp->xp_context = EXPAND_NOTHING;
2650 }
2651 else if (c == '\'') /* literal string */
2652 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2655 /* skip */ ;
2656 xp->xp_context = EXPAND_NOTHING;
2657 }
2658 else if (c == '|')
2659 {
2660 if (xp->xp_pattern[1] == '|')
2661 {
2662 ++xp->xp_pattern;
2663 xp->xp_context = EXPAND_EXPRESSION;
2664 }
2665 else
2666 xp->xp_context = EXPAND_COMMANDS;
2667 }
2668 else
2669 xp->xp_context = EXPAND_EXPRESSION;
2670 }
2671 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002672 /* Doesn't look like something valid, expand as an expression
2673 * anyway. */
2674 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 arg = xp->xp_pattern;
2676 if (*arg != NUL)
2677 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2678 /* skip */ ;
2679 }
2680 xp->xp_pattern = arg;
2681}
2682
2683#endif /* FEAT_CMDL_COMPL */
2684
2685/*
2686 * ":1,25call func(arg1, arg2)" function call.
2687 */
2688 void
2689ex_call(eap)
2690 exarg_T *eap;
2691{
2692 char_u *arg = eap->arg;
2693 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002695 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002697 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 linenr_T lnum;
2699 int doesrange;
2700 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002701 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002703 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2704 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002705 if (tofree == NULL)
2706 return;
2707
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002708 /* Increase refcount on dictionary, it could get deleted when evaluating
2709 * the arguments. */
2710 if (fudi.fd_dict != NULL)
2711 ++fudi.fd_dict->dv_refcount;
2712
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002713 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2714 len = STRLEN(tofree);
2715 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
Bram Moolenaar532c7802005-01-27 14:44:31 +00002717 /* Skip white space to allow ":call func ()". Not good, but required for
2718 * backward compatibility. */
2719 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002720 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721
2722 if (*startarg != '(')
2723 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00002724 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 goto end;
2726 }
2727
2728 /*
2729 * When skipping, evaluate the function once, to find the end of the
2730 * arguments.
2731 * When the function takes a range, this is discovered after the first
2732 * call, and the loop is broken.
2733 */
2734 if (eap->skip)
2735 {
2736 ++emsg_skip;
2737 lnum = eap->line2; /* do it once, also with an invalid range */
2738 }
2739 else
2740 lnum = eap->line1;
2741 for ( ; lnum <= eap->line2; ++lnum)
2742 {
2743 if (!eap->skip && eap->addr_count > 0)
2744 {
2745 curwin->w_cursor.lnum = lnum;
2746 curwin->w_cursor.col = 0;
2747 }
2748 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002749 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002750 eap->line1, eap->line2, &doesrange,
2751 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 {
2753 failed = TRUE;
2754 break;
2755 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002756 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 if (doesrange || eap->skip)
2758 break;
2759 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002760 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002761 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002762 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 if (aborting())
2764 break;
2765 }
2766 if (eap->skip)
2767 --emsg_skip;
2768
2769 if (!failed)
2770 {
2771 /* Check for trailing illegal characters and a following command. */
2772 if (!ends_excmd(*arg))
2773 {
2774 emsg_severe = TRUE;
2775 EMSG(_(e_trailing));
2776 }
2777 else
2778 eap->nextcmd = check_nextcmd(arg);
2779 }
2780
2781end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002782 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002783 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784}
2785
2786/*
2787 * ":unlet[!] var1 ... " command.
2788 */
2789 void
2790ex_unlet(eap)
2791 exarg_T *eap;
2792{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002793 ex_unletlock(eap, eap->arg, 0);
2794}
2795
2796/*
2797 * ":lockvar" and ":unlockvar" commands
2798 */
2799 void
2800ex_lockvar(eap)
2801 exarg_T *eap;
2802{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002804 int deep = 2;
2805
2806 if (eap->forceit)
2807 deep = -1;
2808 else if (vim_isdigit(*arg))
2809 {
2810 deep = getdigits(&arg);
2811 arg = skipwhite(arg);
2812 }
2813
2814 ex_unletlock(eap, arg, deep);
2815}
2816
2817/*
2818 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2819 */
2820 static void
2821ex_unletlock(eap, argstart, deep)
2822 exarg_T *eap;
2823 char_u *argstart;
2824 int deep;
2825{
2826 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002829 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
2831 do
2832 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 /* Parse the name and find the end. */
2834 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE);
2835 if (lv.ll_name == NULL)
2836 error = TRUE; /* error but continue parsing */
2837 if (name_end == NULL || (!vim_iswhite(*name_end)
2838 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 if (name_end != NULL)
2841 {
2842 emsg_severe = TRUE;
2843 EMSG(_(e_trailing));
2844 }
2845 if (!(eap->skip || error))
2846 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 break;
2848 }
2849
2850 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002851 {
2852 if (eap->cmdidx == CMD_unlet)
2853 {
2854 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2855 error = TRUE;
2856 }
2857 else
2858 {
2859 if (do_lock_var(&lv, name_end, deep,
2860 eap->cmdidx == CMD_lockvar) == FAIL)
2861 error = TRUE;
2862 }
2863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 if (!eap->skip)
2866 clear_lval(&lv);
2867
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 arg = skipwhite(name_end);
2869 } while (!ends_excmd(*arg));
2870
2871 eap->nextcmd = check_nextcmd(arg);
2872}
2873
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00002876 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 int forceit;
2879{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 int ret = OK;
2881 int cc;
2882
2883 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 cc = *name_end;
2886 *name_end = NUL;
2887
2888 /* Normal name or expanded name. */
2889 if (check_changedtick(lp->ll_name))
2890 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002891 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002895 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
2896 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 else if (lp->ll_range)
2898 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900
2901 /* Delete a range of List items. */
2902 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2903 {
2904 li = lp->ll_li->li_next;
2905 listitem_remove(lp->ll_list, lp->ll_li);
2906 lp->ll_li = li;
2907 ++lp->ll_n1;
2908 }
2909 }
2910 else
2911 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 /* unlet a List item. */
2914 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002917 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 }
2919
2920 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002921}
2922
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923/*
2924 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002925 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 */
2927 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002928do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002930 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
Bram Moolenaar33570922005-01-25 22:26:29 +00002932 hashtab_T *ht;
2933 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002934 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935
Bram Moolenaar33570922005-01-25 22:26:29 +00002936 ht = find_var_ht(name, &varname);
2937 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 hi = hash_find(ht, varname);
2940 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002941 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002942 if (var_check_ro(HI2DI(hi)->di_flags, name))
2943 return FAIL;
2944 delete_var(ht, hi);
2945 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002948 if (forceit)
2949 return OK;
2950 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 return FAIL;
2952}
2953
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002954/*
2955 * Lock or unlock variable indicated by "lp".
2956 * "deep" is the levels to go (-1 for unlimited);
2957 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2958 */
2959 static int
2960do_lock_var(lp, name_end, deep, lock)
2961 lval_T *lp;
2962 char_u *name_end;
2963 int deep;
2964 int lock;
2965{
2966 int ret = OK;
2967 int cc;
2968 dictitem_T *di;
2969
2970 if (deep == 0) /* nothing to do */
2971 return OK;
2972
2973 if (lp->ll_tv == NULL)
2974 {
2975 cc = *name_end;
2976 *name_end = NUL;
2977
2978 /* Normal name or expanded name. */
2979 if (check_changedtick(lp->ll_name))
2980 ret = FAIL;
2981 else
2982 {
2983 di = find_var(lp->ll_name, NULL);
2984 if (di == NULL)
2985 ret = FAIL;
2986 else
2987 {
2988 if (lock)
2989 di->di_flags |= DI_FLAGS_LOCK;
2990 else
2991 di->di_flags &= ~DI_FLAGS_LOCK;
2992 item_lock(&di->di_tv, deep, lock);
2993 }
2994 }
2995 *name_end = cc;
2996 }
2997 else if (lp->ll_range)
2998 {
2999 listitem_T *li = lp->ll_li;
3000
3001 /* (un)lock a range of List items. */
3002 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3003 {
3004 item_lock(&li->li_tv, deep, lock);
3005 li = li->li_next;
3006 ++lp->ll_n1;
3007 }
3008 }
3009 else if (lp->ll_list != NULL)
3010 /* (un)lock a List item. */
3011 item_lock(&lp->ll_li->li_tv, deep, lock);
3012 else
3013 /* un(lock) a Dictionary item. */
3014 item_lock(&lp->ll_di->di_tv, deep, lock);
3015
3016 return ret;
3017}
3018
3019/*
3020 * Lock or unlock an item. "deep" is nr of levels to go.
3021 */
3022 static void
3023item_lock(tv, deep, lock)
3024 typval_T *tv;
3025 int deep;
3026 int lock;
3027{
3028 static int recurse = 0;
3029 list_T *l;
3030 listitem_T *li;
3031 dict_T *d;
3032 hashitem_T *hi;
3033 int todo;
3034
3035 if (recurse >= DICT_MAXNEST)
3036 {
3037 EMSG(_("E743: variable nested too deep for (un)lock"));
3038 return;
3039 }
3040 if (deep == 0)
3041 return;
3042 ++recurse;
3043
3044 /* lock/unlock the item itself */
3045 if (lock)
3046 tv->v_lock |= VAR_LOCKED;
3047 else
3048 tv->v_lock &= ~VAR_LOCKED;
3049
3050 switch (tv->v_type)
3051 {
3052 case VAR_LIST:
3053 if ((l = tv->vval.v_list) != NULL)
3054 {
3055 if (lock)
3056 l->lv_lock |= VAR_LOCKED;
3057 else
3058 l->lv_lock &= ~VAR_LOCKED;
3059 if (deep < 0 || deep > 1)
3060 /* recursive: lock/unlock the items the List contains */
3061 for (li = l->lv_first; li != NULL; li = li->li_next)
3062 item_lock(&li->li_tv, deep - 1, lock);
3063 }
3064 break;
3065 case VAR_DICT:
3066 if ((d = tv->vval.v_dict) != NULL)
3067 {
3068 if (lock)
3069 d->dv_lock |= VAR_LOCKED;
3070 else
3071 d->dv_lock &= ~VAR_LOCKED;
3072 if (deep < 0 || deep > 1)
3073 {
3074 /* recursive: lock/unlock the items the List contains */
3075 todo = d->dv_hashtab.ht_used;
3076 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3077 {
3078 if (!HASHITEM_EMPTY(hi))
3079 {
3080 --todo;
3081 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3082 }
3083 }
3084 }
3085 }
3086 }
3087 --recurse;
3088}
3089
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3091/*
3092 * Delete all "menutrans_" variables.
3093 */
3094 void
3095del_menutrans_vars()
3096{
Bram Moolenaar33570922005-01-25 22:26:29 +00003097 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003098 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099
Bram Moolenaar33570922005-01-25 22:26:29 +00003100 hash_lock(&globvarht);
3101 todo = globvarht.ht_used;
3102 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003103 {
3104 if (!HASHITEM_EMPTY(hi))
3105 {
3106 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003107 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3108 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003109 }
3110 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003111 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112}
3113#endif
3114
3115#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3116
3117/*
3118 * Local string buffer for the next two functions to store a variable name
3119 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3120 * get_user_var_name().
3121 */
3122
3123static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3124
3125static char_u *varnamebuf = NULL;
3126static int varnamebuflen = 0;
3127
3128/*
3129 * Function to concatenate a prefix and a variable name.
3130 */
3131 static char_u *
3132cat_prefix_varname(prefix, name)
3133 int prefix;
3134 char_u *name;
3135{
3136 int len;
3137
3138 len = (int)STRLEN(name) + 3;
3139 if (len > varnamebuflen)
3140 {
3141 vim_free(varnamebuf);
3142 len += 10; /* some additional space */
3143 varnamebuf = alloc(len);
3144 if (varnamebuf == NULL)
3145 {
3146 varnamebuflen = 0;
3147 return NULL;
3148 }
3149 varnamebuflen = len;
3150 }
3151 *varnamebuf = prefix;
3152 varnamebuf[1] = ':';
3153 STRCPY(varnamebuf + 2, name);
3154 return varnamebuf;
3155}
3156
3157/*
3158 * Function given to ExpandGeneric() to obtain the list of user defined
3159 * (global/buffer/window/built-in) variable names.
3160 */
3161/*ARGSUSED*/
3162 char_u *
3163get_user_var_name(xp, idx)
3164 expand_T *xp;
3165 int idx;
3166{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003167 static long_u gdone;
3168 static long_u bdone;
3169 static long_u wdone;
3170 static int vidx;
3171 static hashitem_T *hi;
3172 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
3174 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003175 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003176
3177 /* Global variables */
3178 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003180 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003182 else
3183 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003184 while (HASHITEM_EMPTY(hi))
3185 ++hi;
3186 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3187 return cat_prefix_varname('g', hi->hi_key);
3188 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003190
3191 /* b: variables */
3192 ht = &curbuf->b_vars.dv_hashtab;
3193 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003195 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003197 else
3198 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003199 while (HASHITEM_EMPTY(hi))
3200 ++hi;
3201 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003203 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003205 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 return (char_u *)"b:changedtick";
3207 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003208
3209 /* w: variables */
3210 ht = &curwin->w_vars.dv_hashtab;
3211 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003213 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003214 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003215 else
3216 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003217 while (HASHITEM_EMPTY(hi))
3218 ++hi;
3219 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003221
3222 /* v: variables */
3223 if (vidx < VV_LEN)
3224 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225
3226 vim_free(varnamebuf);
3227 varnamebuf = NULL;
3228 varnamebuflen = 0;
3229 return NULL;
3230}
3231
3232#endif /* FEAT_CMDL_COMPL */
3233
3234/*
3235 * types for expressions.
3236 */
3237typedef enum
3238{
3239 TYPE_UNKNOWN = 0
3240 , TYPE_EQUAL /* == */
3241 , TYPE_NEQUAL /* != */
3242 , TYPE_GREATER /* > */
3243 , TYPE_GEQUAL /* >= */
3244 , TYPE_SMALLER /* < */
3245 , TYPE_SEQUAL /* <= */
3246 , TYPE_MATCH /* =~ */
3247 , TYPE_NOMATCH /* !~ */
3248} exptype_T;
3249
3250/*
3251 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003252 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3254 */
3255
3256/*
3257 * Handle zero level expression.
3258 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003259 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 * Return OK or FAIL.
3261 */
3262 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003263eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 char_u **nextcmd;
3267 int evaluate;
3268{
3269 int ret;
3270 char_u *p;
3271
3272 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003273 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 if (ret == FAIL || !ends_excmd(*p))
3275 {
3276 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003277 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 /*
3279 * Report the invalid expression unless the expression evaluation has
3280 * been cancelled due to an aborting error, an interrupt, or an
3281 * exception.
3282 */
3283 if (!aborting())
3284 EMSG2(_(e_invexpr2), arg);
3285 ret = FAIL;
3286 }
3287 if (nextcmd != NULL)
3288 *nextcmd = check_nextcmd(p);
3289
3290 return ret;
3291}
3292
3293/*
3294 * Handle top level expression:
3295 * expr1 ? expr0 : expr0
3296 *
3297 * "arg" must point to the first non-white of the expression.
3298 * "arg" is advanced to the next non-white after the recognized expression.
3299 *
3300 * Return OK or FAIL.
3301 */
3302 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003303eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 int evaluate;
3307{
3308 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003309 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310
3311 /*
3312 * Get the first variable.
3313 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003314 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 return FAIL;
3316
3317 if ((*arg)[0] == '?')
3318 {
3319 result = FALSE;
3320 if (evaluate)
3321 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003322 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003324 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 }
3326
3327 /*
3328 * Get the second variable.
3329 */
3330 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003331 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 return FAIL;
3333
3334 /*
3335 * Check for the ":".
3336 */
3337 if ((*arg)[0] != ':')
3338 {
3339 EMSG(_("E109: Missing ':' after '?'"));
3340 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003341 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 return FAIL;
3343 }
3344
3345 /*
3346 * Get the third variable.
3347 */
3348 *arg = skipwhite(*arg + 1);
3349 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3350 {
3351 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003352 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 return FAIL;
3354 }
3355 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003356 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 }
3358
3359 return OK;
3360}
3361
3362/*
3363 * Handle first level expression:
3364 * expr2 || expr2 || expr2 logical OR
3365 *
3366 * "arg" must point to the first non-white of the expression.
3367 * "arg" is advanced to the next non-white after the recognized expression.
3368 *
3369 * Return OK or FAIL.
3370 */
3371 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003372eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 int evaluate;
3376{
Bram Moolenaar33570922005-01-25 22:26:29 +00003377 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 long result;
3379 int first;
3380
3381 /*
3382 * Get the first variable.
3383 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003384 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 return FAIL;
3386
3387 /*
3388 * Repeat until there is no following "||".
3389 */
3390 first = TRUE;
3391 result = FALSE;
3392 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3393 {
3394 if (evaluate && first)
3395 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003396 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003398 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 first = FALSE;
3400 }
3401
3402 /*
3403 * Get the second variable.
3404 */
3405 *arg = skipwhite(*arg + 2);
3406 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3407 return FAIL;
3408
3409 /*
3410 * Compute the result.
3411 */
3412 if (evaluate && !result)
3413 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003414 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003416 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
3418 if (evaluate)
3419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003420 rettv->v_type = VAR_NUMBER;
3421 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
3423 }
3424
3425 return OK;
3426}
3427
3428/*
3429 * Handle second level expression:
3430 * expr3 && expr3 && expr3 logical AND
3431 *
3432 * "arg" must point to the first non-white of the expression.
3433 * "arg" is advanced to the next non-white after the recognized expression.
3434 *
3435 * Return OK or FAIL.
3436 */
3437 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003438eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 int evaluate;
3442{
Bram Moolenaar33570922005-01-25 22:26:29 +00003443 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 long result;
3445 int first;
3446
3447 /*
3448 * Get the first variable.
3449 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003450 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 return FAIL;
3452
3453 /*
3454 * Repeat until there is no following "&&".
3455 */
3456 first = TRUE;
3457 result = TRUE;
3458 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3459 {
3460 if (evaluate && first)
3461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003462 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003464 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 first = FALSE;
3466 }
3467
3468 /*
3469 * Get the second variable.
3470 */
3471 *arg = skipwhite(*arg + 2);
3472 if (eval4(arg, &var2, evaluate && result) == FAIL)
3473 return FAIL;
3474
3475 /*
3476 * Compute the result.
3477 */
3478 if (evaluate && result)
3479 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003480 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003482 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 }
3484 if (evaluate)
3485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003486 rettv->v_type = VAR_NUMBER;
3487 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 }
3489 }
3490
3491 return OK;
3492}
3493
3494/*
3495 * Handle third level expression:
3496 * var1 == var2
3497 * var1 =~ var2
3498 * var1 != var2
3499 * var1 !~ var2
3500 * var1 > var2
3501 * var1 >= var2
3502 * var1 < var2
3503 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003504 * var1 is var2
3505 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 *
3507 * "arg" must point to the first non-white of the expression.
3508 * "arg" is advanced to the next non-white after the recognized expression.
3509 *
3510 * Return OK or FAIL.
3511 */
3512 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003513eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 int evaluate;
3517{
Bram Moolenaar33570922005-01-25 22:26:29 +00003518 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 char_u *p;
3520 int i;
3521 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003522 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 int len = 2;
3524 long n1, n2;
3525 char_u *s1, *s2;
3526 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3527 regmatch_T regmatch;
3528 int ic;
3529 char_u *save_cpo;
3530
3531 /*
3532 * Get the first variable.
3533 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003534 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 return FAIL;
3536
3537 p = *arg;
3538 switch (p[0])
3539 {
3540 case '=': if (p[1] == '=')
3541 type = TYPE_EQUAL;
3542 else if (p[1] == '~')
3543 type = TYPE_MATCH;
3544 break;
3545 case '!': if (p[1] == '=')
3546 type = TYPE_NEQUAL;
3547 else if (p[1] == '~')
3548 type = TYPE_NOMATCH;
3549 break;
3550 case '>': if (p[1] != '=')
3551 {
3552 type = TYPE_GREATER;
3553 len = 1;
3554 }
3555 else
3556 type = TYPE_GEQUAL;
3557 break;
3558 case '<': if (p[1] != '=')
3559 {
3560 type = TYPE_SMALLER;
3561 len = 1;
3562 }
3563 else
3564 type = TYPE_SEQUAL;
3565 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003566 case 'i': if (p[1] == 's')
3567 {
3568 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3569 len = 5;
3570 if (!vim_isIDc(p[len]))
3571 {
3572 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3573 type_is = TRUE;
3574 }
3575 }
3576 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
3578
3579 /*
3580 * If there is a comparitive operator, use it.
3581 */
3582 if (type != TYPE_UNKNOWN)
3583 {
3584 /* extra question mark appended: ignore case */
3585 if (p[len] == '?')
3586 {
3587 ic = TRUE;
3588 ++len;
3589 }
3590 /* extra '#' appended: match case */
3591 else if (p[len] == '#')
3592 {
3593 ic = FALSE;
3594 ++len;
3595 }
3596 /* nothing appened: use 'ignorecase' */
3597 else
3598 ic = p_ic;
3599
3600 /*
3601 * Get the second variable.
3602 */
3603 *arg = skipwhite(p + len);
3604 if (eval5(arg, &var2, evaluate) == FAIL)
3605 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003606 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 return FAIL;
3608 }
3609
3610 if (evaluate)
3611 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003612 if (type_is && rettv->v_type != var2.v_type)
3613 {
3614 /* For "is" a different type always means FALSE, for "notis"
3615 * it means TRUE. */
3616 n1 = (type == TYPE_NEQUAL);
3617 }
3618 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3619 {
3620 if (type_is)
3621 {
3622 n1 = (rettv->v_type == var2.v_type
3623 && rettv->vval.v_list == var2.vval.v_list);
3624 if (type == TYPE_NEQUAL)
3625 n1 = !n1;
3626 }
3627 else if (rettv->v_type != var2.v_type
3628 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3629 {
3630 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003631 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003632 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003633 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003634 clear_tv(rettv);
3635 clear_tv(&var2);
3636 return FAIL;
3637 }
3638 else
3639 {
3640 /* Compare two Lists for being equal or unequal. */
3641 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3642 if (type == TYPE_NEQUAL)
3643 n1 = !n1;
3644 }
3645 }
3646
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003647 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3648 {
3649 if (type_is)
3650 {
3651 n1 = (rettv->v_type == var2.v_type
3652 && rettv->vval.v_dict == var2.vval.v_dict);
3653 if (type == TYPE_NEQUAL)
3654 n1 = !n1;
3655 }
3656 else if (rettv->v_type != var2.v_type
3657 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3658 {
3659 if (rettv->v_type != var2.v_type)
3660 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3661 else
3662 EMSG(_("E736: Invalid operation for Dictionary"));
3663 clear_tv(rettv);
3664 clear_tv(&var2);
3665 return FAIL;
3666 }
3667 else
3668 {
3669 /* Compare two Dictionaries for being equal or unequal. */
3670 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3671 if (type == TYPE_NEQUAL)
3672 n1 = !n1;
3673 }
3674 }
3675
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003676 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3677 {
3678 if (rettv->v_type != var2.v_type
3679 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3680 {
3681 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003682 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003683 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003684 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003685 clear_tv(rettv);
3686 clear_tv(&var2);
3687 return FAIL;
3688 }
3689 else
3690 {
3691 /* Compare two Funcrefs for being equal or unequal. */
3692 if (rettv->vval.v_string == NULL
3693 || var2.vval.v_string == NULL)
3694 n1 = FALSE;
3695 else
3696 n1 = STRCMP(rettv->vval.v_string,
3697 var2.vval.v_string) == 0;
3698 if (type == TYPE_NEQUAL)
3699 n1 = !n1;
3700 }
3701 }
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 /*
3704 * If one of the two variables is a number, compare as a number.
3705 * When using "=~" or "!~", always compare as string.
3706 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003707 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003710 n1 = get_tv_number(rettv);
3711 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 switch (type)
3713 {
3714 case TYPE_EQUAL: n1 = (n1 == n2); break;
3715 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3716 case TYPE_GREATER: n1 = (n1 > n2); break;
3717 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3718 case TYPE_SMALLER: n1 = (n1 < n2); break;
3719 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3720 case TYPE_UNKNOWN:
3721 case TYPE_MATCH:
3722 case TYPE_NOMATCH: break; /* avoid gcc warning */
3723 }
3724 }
3725 else
3726 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003727 s1 = get_tv_string_buf(rettv, buf1);
3728 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3730 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3731 else
3732 i = 0;
3733 n1 = FALSE;
3734 switch (type)
3735 {
3736 case TYPE_EQUAL: n1 = (i == 0); break;
3737 case TYPE_NEQUAL: n1 = (i != 0); break;
3738 case TYPE_GREATER: n1 = (i > 0); break;
3739 case TYPE_GEQUAL: n1 = (i >= 0); break;
3740 case TYPE_SMALLER: n1 = (i < 0); break;
3741 case TYPE_SEQUAL: n1 = (i <= 0); break;
3742
3743 case TYPE_MATCH:
3744 case TYPE_NOMATCH:
3745 /* avoid 'l' flag in 'cpoptions' */
3746 save_cpo = p_cpo;
3747 p_cpo = (char_u *)"";
3748 regmatch.regprog = vim_regcomp(s2,
3749 RE_MAGIC + RE_STRING);
3750 regmatch.rm_ic = ic;
3751 if (regmatch.regprog != NULL)
3752 {
3753 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3754 vim_free(regmatch.regprog);
3755 if (type == TYPE_NOMATCH)
3756 n1 = !n1;
3757 }
3758 p_cpo = save_cpo;
3759 break;
3760
3761 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3762 }
3763 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003764 clear_tv(rettv);
3765 clear_tv(&var2);
3766 rettv->v_type = VAR_NUMBER;
3767 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
3769 }
3770
3771 return OK;
3772}
3773
3774/*
3775 * Handle fourth level expression:
3776 * + number addition
3777 * - number subtraction
3778 * . string concatenation
3779 *
3780 * "arg" must point to the first non-white of the expression.
3781 * "arg" is advanced to the next non-white after the recognized expression.
3782 *
3783 * Return OK or FAIL.
3784 */
3785 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003786eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 int evaluate;
3790{
Bram Moolenaar33570922005-01-25 22:26:29 +00003791 typval_T var2;
3792 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 int op;
3794 long n1, n2;
3795 char_u *s1, *s2;
3796 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3797 char_u *p;
3798
3799 /*
3800 * Get the first variable.
3801 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003802 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 return FAIL;
3804
3805 /*
3806 * Repeat computing, until no '+', '-' or '.' is following.
3807 */
3808 for (;;)
3809 {
3810 op = **arg;
3811 if (op != '+' && op != '-' && op != '.')
3812 break;
3813
3814 /*
3815 * Get the second variable.
3816 */
3817 *arg = skipwhite(*arg + 1);
3818 if (eval6(arg, &var2, evaluate) == FAIL)
3819 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003820 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 return FAIL;
3822 }
3823
3824 if (evaluate)
3825 {
3826 /*
3827 * Compute the result.
3828 */
3829 if (op == '.')
3830 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831 s1 = get_tv_string_buf(rettv, buf1);
3832 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003833 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003834 clear_tv(rettv);
3835 rettv->v_type = VAR_STRING;
3836 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003838 else if (op == '+' && rettv->v_type == VAR_LIST
3839 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003840 {
3841 /* concatenate Lists */
3842 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3843 &var3) == FAIL)
3844 {
3845 clear_tv(rettv);
3846 clear_tv(&var2);
3847 return FAIL;
3848 }
3849 clear_tv(rettv);
3850 *rettv = var3;
3851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 else
3853 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003854 n1 = get_tv_number(rettv);
3855 n2 = get_tv_number(&var2);
3856 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 if (op == '+')
3858 n1 = n1 + n2;
3859 else
3860 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003861 rettv->v_type = VAR_NUMBER;
3862 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003864 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 }
3866 }
3867 return OK;
3868}
3869
3870/*
3871 * Handle fifth level expression:
3872 * * number multiplication
3873 * / number division
3874 * % number modulo
3875 *
3876 * "arg" must point to the first non-white of the expression.
3877 * "arg" is advanced to the next non-white after the recognized expression.
3878 *
3879 * Return OK or FAIL.
3880 */
3881 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003882eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 int evaluate;
3886{
Bram Moolenaar33570922005-01-25 22:26:29 +00003887 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 int op;
3889 long n1, n2;
3890
3891 /*
3892 * Get the first variable.
3893 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003894 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 return FAIL;
3896
3897 /*
3898 * Repeat computing, until no '*', '/' or '%' is following.
3899 */
3900 for (;;)
3901 {
3902 op = **arg;
3903 if (op != '*' && op != '/' && op != '%')
3904 break;
3905
3906 if (evaluate)
3907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003908 n1 = get_tv_number(rettv);
3909 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911 else
3912 n1 = 0;
3913
3914 /*
3915 * Get the second variable.
3916 */
3917 *arg = skipwhite(*arg + 1);
3918 if (eval7(arg, &var2, evaluate) == FAIL)
3919 return FAIL;
3920
3921 if (evaluate)
3922 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003923 n2 = get_tv_number(&var2);
3924 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
3926 /*
3927 * Compute the result.
3928 */
3929 if (op == '*')
3930 n1 = n1 * n2;
3931 else if (op == '/')
3932 {
3933 if (n2 == 0) /* give an error message? */
3934 n1 = 0x7fffffffL;
3935 else
3936 n1 = n1 / n2;
3937 }
3938 else
3939 {
3940 if (n2 == 0) /* give an error message? */
3941 n1 = 0;
3942 else
3943 n1 = n1 % n2;
3944 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003945 rettv->v_type = VAR_NUMBER;
3946 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
3948 }
3949
3950 return OK;
3951}
3952
3953/*
3954 * Handle sixth level expression:
3955 * number number constant
3956 * "string" string contstant
3957 * 'string' literal string contstant
3958 * &option-name option value
3959 * @r register contents
3960 * identifier variable value
3961 * function() function call
3962 * $VAR environment variable
3963 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003964 * [expr, expr] List
3965 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 *
3967 * Also handle:
3968 * ! in front logical NOT
3969 * - in front unary minus
3970 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003971 * trailing [] subscript in String or List
3972 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 *
3974 * "arg" must point to the first non-white of the expression.
3975 * "arg" is advanced to the next non-white after the recognized expression.
3976 *
3977 * Return OK or FAIL.
3978 */
3979 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003980eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 int evaluate;
3984{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 long n;
3986 int len;
3987 char_u *s;
3988 int val;
3989 char_u *start_leader, *end_leader;
3990 int ret = OK;
3991 char_u *alias;
3992
3993 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003994 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003995 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003997 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
3999 /*
4000 * Skip '!' and '-' characters. They are handled later.
4001 */
4002 start_leader = *arg;
4003 while (**arg == '!' || **arg == '-' || **arg == '+')
4004 *arg = skipwhite(*arg + 1);
4005 end_leader = *arg;
4006
4007 switch (**arg)
4008 {
4009 /*
4010 * Number constant.
4011 */
4012 case '0':
4013 case '1':
4014 case '2':
4015 case '3':
4016 case '4':
4017 case '5':
4018 case '6':
4019 case '7':
4020 case '8':
4021 case '9':
4022 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4023 *arg += len;
4024 if (evaluate)
4025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004026 rettv->v_type = VAR_NUMBER;
4027 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029 break;
4030
4031 /*
4032 * String constant: "string".
4033 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004034 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 break;
4036
4037 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004038 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004040 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004041 break;
4042
4043 /*
4044 * List: [expr, expr]
4045 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004046 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 break;
4048
4049 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004050 * Dictionary: {key: val, key: val}
4051 */
4052 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4053 break;
4054
4055 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004056 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004058 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 break;
4060
4061 /*
4062 * Environment variable: $VAR.
4063 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004064 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 break;
4066
4067 /*
4068 * Register contents: @r.
4069 */
4070 case '@': ++*arg;
4071 if (evaluate)
4072 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004073 rettv->v_type = VAR_STRING;
4074 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
4076 if (**arg != NUL)
4077 ++*arg;
4078 break;
4079
4080 /*
4081 * nested expression: (expression).
4082 */
4083 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004084 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 if (**arg == ')')
4086 ++*arg;
4087 else if (ret == OK)
4088 {
4089 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 ret = FAIL;
4092 }
4093 break;
4094
Bram Moolenaar8c711452005-01-14 21:53:12 +00004095 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 break;
4097 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004098
4099 if (ret == NOTDONE)
4100 {
4101 /*
4102 * Must be a variable or function name.
4103 * Can also be a curly-braces kind of name: {expr}.
4104 */
4105 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004106 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004107 if (alias != NULL)
4108 s = alias;
4109
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004110 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004111 ret = FAIL;
4112 else
4113 {
4114 if (**arg == '(') /* recursive! */
4115 {
4116 /* If "s" is the name of a variable of type VAR_FUNC
4117 * use its contents. */
4118 s = deref_func_name(s, &len);
4119
4120 /* Invoke the function. */
4121 ret = get_func_tv(s, len, rettv, arg,
4122 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004123 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004124 /* Stop the expression evaluation when immediately
4125 * aborting on error, or when an interrupt occurred or
4126 * an exception was thrown but not caught. */
4127 if (aborting())
4128 {
4129 if (ret == OK)
4130 clear_tv(rettv);
4131 ret = FAIL;
4132 }
4133 }
4134 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004135 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004136 else
4137 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004138 }
4139
4140 if (alias != NULL)
4141 vim_free(alias);
4142 }
4143
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 *arg = skipwhite(*arg);
4145
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004146 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4147 * expr(expr). */
4148 if (ret == OK)
4149 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150
4151 /*
4152 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4153 */
4154 if (ret == OK && evaluate && end_leader > start_leader)
4155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 while (end_leader > start_leader)
4158 {
4159 --end_leader;
4160 if (*end_leader == '!')
4161 val = !val;
4162 else if (*end_leader == '-')
4163 val = -val;
4164 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004165 clear_tv(rettv);
4166 rettv->v_type = VAR_NUMBER;
4167 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 }
4169
4170 return ret;
4171}
4172
4173/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004174 * Evaluate an "[expr]" or "[expr:expr]" index.
4175 * "*arg" points to the '['.
4176 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4177 */
4178 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004179eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004180 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004181 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004182 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004183 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004184{
4185 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004186 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004187 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004188 long len = -1;
4189 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004190 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004191 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004192
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004193 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004194 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004195 if (verbose)
4196 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004197 return FAIL;
4198 }
4199
Bram Moolenaar8c711452005-01-14 21:53:12 +00004200 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004201 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004202 /*
4203 * dict.name
4204 */
4205 key = *arg + 1;
4206 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4207 ;
4208 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004209 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004210 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004211 }
4212 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004213 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004214 /*
4215 * something[idx]
4216 *
4217 * Get the (first) variable from inside the [].
4218 */
4219 *arg = skipwhite(*arg + 1);
4220 if (**arg == ':')
4221 empty1 = TRUE;
4222 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4223 return FAIL;
4224
4225 /*
4226 * Get the second variable from inside the [:].
4227 */
4228 if (**arg == ':')
4229 {
4230 range = TRUE;
4231 *arg = skipwhite(*arg + 1);
4232 if (**arg == ']')
4233 empty2 = TRUE;
4234 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4235 {
4236 clear_tv(&var1);
4237 return FAIL;
4238 }
4239 }
4240
4241 /* Check for the ']'. */
4242 if (**arg != ']')
4243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004244 if (verbose)
4245 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004246 clear_tv(&var1);
4247 if (range)
4248 clear_tv(&var2);
4249 return FAIL;
4250 }
4251 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004252 }
4253
4254 if (evaluate)
4255 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004256 n1 = 0;
4257 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004258 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 n1 = get_tv_number(&var1);
4260 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004261 }
4262 if (range)
4263 {
4264 if (empty2)
4265 n2 = -1;
4266 else
4267 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 n2 = get_tv_number(&var2);
4269 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004270 }
4271 }
4272
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004274 {
4275 case VAR_NUMBER:
4276 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004278 len = (long)STRLEN(s);
4279 if (range)
4280 {
4281 /* The resulting variable is a substring. If the indexes
4282 * are out of range the result is empty. */
4283 if (n1 < 0)
4284 {
4285 n1 = len + n1;
4286 if (n1 < 0)
4287 n1 = 0;
4288 }
4289 if (n2 < 0)
4290 n2 = len + n2;
4291 else if (n2 >= len)
4292 n2 = len;
4293 if (n1 >= len || n2 < 0 || n1 > n2)
4294 s = NULL;
4295 else
4296 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4297 }
4298 else
4299 {
4300 /* The resulting variable is a string of a single
4301 * character. If the index is too big or negative the
4302 * result is empty. */
4303 if (n1 >= len || n1 < 0)
4304 s = NULL;
4305 else
4306 s = vim_strnsave(s + n1, 1);
4307 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004308 clear_tv(rettv);
4309 rettv->v_type = VAR_STRING;
4310 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004311 break;
4312
4313 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004315 if (n1 < 0)
4316 n1 = len + n1;
4317 if (!empty1 && (n1 < 0 || n1 >= len))
4318 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004319 if (verbose)
4320 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004321 return FAIL;
4322 }
4323 if (range)
4324 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004325 list_T *l;
4326 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004327
4328 if (n2 < 0)
4329 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004330 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004331 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004332 if (verbose)
4333 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004334 return FAIL;
4335 }
4336 l = list_alloc();
4337 if (l == NULL)
4338 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004339 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004340 n1 <= n2; ++n1)
4341 {
4342 if (list_append_tv(l, &item->li_tv) == FAIL)
4343 {
4344 list_free(l);
4345 return FAIL;
4346 }
4347 item = item->li_next;
4348 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004349 clear_tv(rettv);
4350 rettv->v_type = VAR_LIST;
4351 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004352 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004353 }
4354 else
4355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004357 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004358 clear_tv(rettv);
4359 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004360 }
4361 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004362
4363 case VAR_DICT:
4364 if (range)
4365 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004366 if (verbose)
4367 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004368 if (len == -1)
4369 clear_tv(&var1);
4370 return FAIL;
4371 }
4372 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004373 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004374
4375 if (len == -1)
4376 {
4377 key = get_tv_string(&var1);
4378 if (*key == NUL)
4379 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004380 if (verbose)
4381 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004382 clear_tv(&var1);
4383 return FAIL;
4384 }
4385 }
4386
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004387 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004388
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004389 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004390 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004391 if (len == -1)
4392 clear_tv(&var1);
4393 if (item == NULL)
4394 return FAIL;
4395
4396 copy_tv(&item->di_tv, &var1);
4397 clear_tv(rettv);
4398 *rettv = var1;
4399 }
4400 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004401 }
4402 }
4403
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004404 return OK;
4405}
4406
4407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 * Get an option value.
4409 * "arg" points to the '&' or '+' before the option name.
4410 * "arg" is advanced to character after the option name.
4411 * Return OK or FAIL.
4412 */
4413 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004414get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004416 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 int evaluate;
4418{
4419 char_u *option_end;
4420 long numval;
4421 char_u *stringval;
4422 int opt_type;
4423 int c;
4424 int working = (**arg == '+'); /* has("+option") */
4425 int ret = OK;
4426 int opt_flags;
4427
4428 /*
4429 * Isolate the option name and find its value.
4430 */
4431 option_end = find_option_end(arg, &opt_flags);
4432 if (option_end == NULL)
4433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004434 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 EMSG2(_("E112: Option name missing: %s"), *arg);
4436 return FAIL;
4437 }
4438
4439 if (!evaluate)
4440 {
4441 *arg = option_end;
4442 return OK;
4443 }
4444
4445 c = *option_end;
4446 *option_end = NUL;
4447 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004448 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
4450 if (opt_type == -3) /* invalid name */
4451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004452 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 EMSG2(_("E113: Unknown option: %s"), *arg);
4454 ret = FAIL;
4455 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004456 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
4458 if (opt_type == -2) /* hidden string option */
4459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004460 rettv->v_type = VAR_STRING;
4461 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 }
4463 else if (opt_type == -1) /* hidden number option */
4464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004465 rettv->v_type = VAR_NUMBER;
4466 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468 else if (opt_type == 1) /* number option */
4469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 rettv->v_type = VAR_NUMBER;
4471 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473 else /* string option */
4474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004475 rettv->v_type = VAR_STRING;
4476 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478 }
4479 else if (working && (opt_type == -2 || opt_type == -1))
4480 ret = FAIL;
4481
4482 *option_end = c; /* put back for error messages */
4483 *arg = option_end;
4484
4485 return ret;
4486}
4487
4488/*
4489 * Allocate a variable for a string constant.
4490 * Return OK or FAIL.
4491 */
4492 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004493get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 int evaluate;
4497{
4498 char_u *p;
4499 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 int extra = 0;
4501
4502 /*
4503 * Find the end of the string, skipping backslashed characters.
4504 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004505 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
4507 if (*p == '\\' && p[1] != NUL)
4508 {
4509 ++p;
4510 /* A "\<x>" form occupies at least 4 characters, and produces up
4511 * to 6 characters: reserve space for 2 extra */
4512 if (*p == '<')
4513 extra += 2;
4514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 }
4516
4517 if (*p != '"')
4518 {
4519 EMSG2(_("E114: Missing quote: %s"), *arg);
4520 return FAIL;
4521 }
4522
4523 /* If only parsing, set *arg and return here */
4524 if (!evaluate)
4525 {
4526 *arg = p + 1;
4527 return OK;
4528 }
4529
4530 /*
4531 * Copy the string into allocated memory, handling backslashed
4532 * characters.
4533 */
4534 name = alloc((unsigned)(p - *arg + extra));
4535 if (name == NULL)
4536 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004537 rettv->v_type = VAR_STRING;
4538 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
Bram Moolenaar8c711452005-01-14 21:53:12 +00004540 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 {
4542 if (*p == '\\')
4543 {
4544 switch (*++p)
4545 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004546 case 'b': *name++ = BS; ++p; break;
4547 case 'e': *name++ = ESC; ++p; break;
4548 case 'f': *name++ = FF; ++p; break;
4549 case 'n': *name++ = NL; ++p; break;
4550 case 'r': *name++ = CAR; ++p; break;
4551 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
4553 case 'X': /* hex: "\x1", "\x12" */
4554 case 'x':
4555 case 'u': /* Unicode: "\u0023" */
4556 case 'U':
4557 if (vim_isxdigit(p[1]))
4558 {
4559 int n, nr;
4560 int c = toupper(*p);
4561
4562 if (c == 'X')
4563 n = 2;
4564 else
4565 n = 4;
4566 nr = 0;
4567 while (--n >= 0 && vim_isxdigit(p[1]))
4568 {
4569 ++p;
4570 nr = (nr << 4) + hex2nr(*p);
4571 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004572 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573#ifdef FEAT_MBYTE
4574 /* For "\u" store the number according to
4575 * 'encoding'. */
4576 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004577 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 else
4579#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004580 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 break;
4583
4584 /* octal: "\1", "\12", "\123" */
4585 case '0':
4586 case '1':
4587 case '2':
4588 case '3':
4589 case '4':
4590 case '5':
4591 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004592 case '7': *name = *p++ - '0';
4593 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004595 *name = (*name << 3) + *p++ - '0';
4596 if (*p >= '0' && *p <= '7')
4597 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004599 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 break;
4601
4602 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004603 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if (extra != 0)
4605 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004606 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 break;
4608 }
4609 /* FALLTHROUGH */
4610
Bram Moolenaar8c711452005-01-14 21:53:12 +00004611 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 break;
4613 }
4614 }
4615 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004616 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004619 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 *arg = p + 1;
4621
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 return OK;
4623}
4624
4625/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004626 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 * Return OK or FAIL.
4628 */
4629 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004630get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 int evaluate;
4634{
4635 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004636 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004637 int reduce = 0;
4638
4639 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004640 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004641 */
4642 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4643 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004644 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004645 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004646 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004647 break;
4648 ++reduce;
4649 ++p;
4650 }
4651 }
4652
Bram Moolenaar8c711452005-01-14 21:53:12 +00004653 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004654 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004655 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004656 return FAIL;
4657 }
4658
Bram Moolenaar8c711452005-01-14 21:53:12 +00004659 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004660 if (!evaluate)
4661 {
4662 *arg = p + 1;
4663 return OK;
4664 }
4665
4666 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004667 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004668 */
4669 str = alloc((unsigned)((p - *arg) - reduce));
4670 if (str == NULL)
4671 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004672 rettv->v_type = VAR_STRING;
4673 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004674
Bram Moolenaar8c711452005-01-14 21:53:12 +00004675 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004679 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004680 break;
4681 ++p;
4682 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004683 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004684 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004685 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004686 *arg = p + 1;
4687
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004688 return OK;
4689}
4690
4691/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004692 * Allocate a variable for a List and fill it from "*arg".
4693 * Return OK or FAIL.
4694 */
4695 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004696get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004697 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004698 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004699 int evaluate;
4700{
Bram Moolenaar33570922005-01-25 22:26:29 +00004701 list_T *l = NULL;
4702 typval_T tv;
4703 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004704
4705 if (evaluate)
4706 {
4707 l = list_alloc();
4708 if (l == NULL)
4709 return FAIL;
4710 }
4711
4712 *arg = skipwhite(*arg + 1);
4713 while (**arg != ']' && **arg != NUL)
4714 {
4715 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4716 goto failret;
4717 if (evaluate)
4718 {
4719 item = listitem_alloc();
4720 if (item != NULL)
4721 {
4722 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004723 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004724 list_append(l, item);
4725 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004726 else
4727 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004728 }
4729
4730 if (**arg == ']')
4731 break;
4732 if (**arg != ',')
4733 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004734 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004735 goto failret;
4736 }
4737 *arg = skipwhite(*arg + 1);
4738 }
4739
4740 if (**arg != ']')
4741 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004742 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004743failret:
4744 if (evaluate)
4745 list_free(l);
4746 return FAIL;
4747 }
4748
4749 *arg = skipwhite(*arg + 1);
4750 if (evaluate)
4751 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004752 rettv->v_type = VAR_LIST;
4753 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754 ++l->lv_refcount;
4755 }
4756
4757 return OK;
4758}
4759
4760/*
4761 * Allocate an empty header for a list.
4762 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004763 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764list_alloc()
4765{
Bram Moolenaar33570922005-01-25 22:26:29 +00004766 return (list_T *)alloc_clear(sizeof(list_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004767}
4768
4769/*
4770 * Unreference a list: decrement the reference count and free it when it
4771 * becomes zero.
4772 */
4773 static void
4774list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004775 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776{
4777 if (l != NULL && --l->lv_refcount <= 0)
4778 list_free(l);
4779}
4780
4781/*
4782 * Free a list, including all items it points to.
4783 * Ignores the reference count.
4784 */
4785 static void
4786list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004787 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004788{
Bram Moolenaar33570922005-01-25 22:26:29 +00004789 listitem_T *item;
4790 listitem_T *next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004791
4792 for (item = l->lv_first; item != NULL; item = next)
4793 {
4794 next = item->li_next;
4795 listitem_free(item);
4796 }
4797 vim_free(l);
4798}
4799
4800/*
4801 * Allocate a list item.
4802 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004803 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004804listitem_alloc()
4805{
Bram Moolenaar33570922005-01-25 22:26:29 +00004806 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004807}
4808
4809/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004810 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004811 */
4812 static void
4813listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004814 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004815{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004816 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004817 vim_free(item);
4818}
4819
4820/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004821 * Remove a list item from a List and free it. Also clears the value.
4822 */
4823 static void
4824listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004825 list_T *l;
4826 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004827{
4828 list_remove(l, item, item);
4829 listitem_free(item);
4830}
4831
4832/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004833 * Get the number of items in a list.
4834 */
4835 static long
4836list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004837 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004838{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004839 if (l == NULL)
4840 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004841 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004842}
4843
4844/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004845 * Return TRUE when two lists have exactly the same values.
4846 */
4847 static int
4848list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004849 list_T *l1;
4850 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004851 int ic; /* ignore case for strings */
4852{
Bram Moolenaar33570922005-01-25 22:26:29 +00004853 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004854
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004855 if (list_len(l1) != list_len(l2))
4856 return FALSE;
4857
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004858 for (item1 = l1->lv_first, item2 = l2->lv_first;
4859 item1 != NULL && item2 != NULL;
4860 item1 = item1->li_next, item2 = item2->li_next)
4861 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4862 return FALSE;
4863 return item1 == NULL && item2 == NULL;
4864}
4865
4866/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004867 * Return TRUE when two dictionaries have exactly the same key/values.
4868 */
4869 static int
4870dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004871 dict_T *d1;
4872 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004873 int ic; /* ignore case for strings */
4874{
Bram Moolenaar33570922005-01-25 22:26:29 +00004875 hashitem_T *hi;
4876 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004877 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004878
4879 if (dict_len(d1) != dict_len(d2))
4880 return FALSE;
4881
Bram Moolenaar33570922005-01-25 22:26:29 +00004882 todo = d1->dv_hashtab.ht_used;
4883 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004884 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004885 if (!HASHITEM_EMPTY(hi))
4886 {
4887 item2 = dict_find(d2, hi->hi_key, -1);
4888 if (item2 == NULL)
4889 return FALSE;
4890 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
4891 return FALSE;
4892 --todo;
4893 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004894 }
4895 return TRUE;
4896}
4897
4898/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004899 * Return TRUE if "tv1" and "tv2" have the same value.
4900 * Compares the items just like "==" would compare them.
4901 */
4902 static int
4903tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004904 typval_T *tv1;
4905 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004906 int ic; /* ignore case */
4907{
4908 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4909
4910 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4911 {
4912 /* recursive! */
4913 if (tv1->v_type != tv2->v_type
4914 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4915 return FALSE;
4916 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004917 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
4918 {
4919 /* recursive! */
4920 if (tv1->v_type != tv2->v_type
4921 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
4922 return FALSE;
4923 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004924 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4925 {
4926 if (tv1->v_type != tv2->v_type
4927 || tv1->vval.v_string == NULL
4928 || tv2->vval.v_string == NULL
4929 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4930 return FALSE;
4931 }
4932 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4933 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004934 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4935 * Don't consider "4x" to be equal to 4. */
4936 if ((tv1->v_type == VAR_STRING
4937 && !string_isa_number(tv1->vval.v_string))
4938 || (tv2->v_type == VAR_STRING
4939 && !string_isa_number(tv2->vval.v_string)))
4940 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004941 if (get_tv_number(tv1) != get_tv_number(tv2))
4942 return FALSE;
4943 }
4944 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4945 get_tv_string_buf(tv2, buf2)) != 0)
4946 return FALSE;
4947 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4948 get_tv_string_buf(tv2, buf2)) != 0)
4949 return FALSE;
4950 return TRUE;
4951}
4952
4953/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004954 * Return TRUE if "tv" is a number without other non-white characters.
4955 */
4956 static int
4957string_isa_number(s)
4958 char_u *s;
4959{
4960 int len;
4961
4962 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4963 return len > 0 && *skipwhite(s + len) == NUL;
4964}
4965
4966/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004967 * Locate item with index "n" in list "l" and return it.
4968 * A negative index is counted from the end; -1 is the last item.
4969 * Returns NULL when "n" is out of range.
4970 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004971 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004972list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00004973 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004974 long n;
4975{
Bram Moolenaar33570922005-01-25 22:26:29 +00004976 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004977 long idx;
4978
4979 if (l == NULL)
4980 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004981
4982 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004983 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00004984 n = l->lv_len + n;
4985
4986 /* Check for index out of range. */
4987 if (n < 0 || n >= l->lv_len)
4988 return NULL;
4989
4990 /* When there is a cached index may start search from there. */
4991 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004992 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00004993 if (n < l->lv_idx / 2)
4994 {
4995 /* closest to the start of the list */
4996 item = l->lv_first;
4997 idx = 0;
4998 }
4999 else if (n > (l->lv_idx + l->lv_len) / 2)
5000 {
5001 /* closest to the end of the list */
5002 item = l->lv_last;
5003 idx = l->lv_len - 1;
5004 }
5005 else
5006 {
5007 /* closest to the cached index */
5008 item = l->lv_idx_item;
5009 idx = l->lv_idx;
5010 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005011 }
5012 else
5013 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005014 if (n < l->lv_len / 2)
5015 {
5016 /* closest to the start of the list */
5017 item = l->lv_first;
5018 idx = 0;
5019 }
5020 else
5021 {
5022 /* closest to the end of the list */
5023 item = l->lv_last;
5024 idx = l->lv_len - 1;
5025 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005026 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005027
5028 while (n > idx)
5029 {
5030 /* search forward */
5031 item = item->li_next;
5032 ++idx;
5033 }
5034 while (n < idx)
5035 {
5036 /* search backward */
5037 item = item->li_prev;
5038 --idx;
5039 }
5040
5041 /* cache the used index */
5042 l->lv_idx = idx;
5043 l->lv_idx_item = item;
5044
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005045 return item;
5046}
5047
5048/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005049 * Locate "item" list "l" and return its index.
5050 * Returns -1 when "item" is not in the list.
5051 */
5052 static long
5053list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005054 list_T *l;
5055 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005056{
5057 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005058 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005059
5060 if (l == NULL)
5061 return -1;
5062 idx = 0;
5063 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5064 ++idx;
5065 if (li == NULL)
5066 return -1;
5067 return idx;;
5068}
5069
5070/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005071 * Append item "item" to the end of list "l".
5072 */
5073 static void
5074list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005075 list_T *l;
5076 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005077{
5078 if (l->lv_last == NULL)
5079 {
5080 /* empty list */
5081 l->lv_first = item;
5082 l->lv_last = item;
5083 item->li_prev = NULL;
5084 }
5085 else
5086 {
5087 l->lv_last->li_next = item;
5088 item->li_prev = l->lv_last;
5089 l->lv_last = item;
5090 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005091 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005092 item->li_next = NULL;
5093}
5094
5095/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005096 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005097 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005098 */
5099 static int
5100list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005101 list_T *l;
5102 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005103{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005104 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005105
Bram Moolenaar05159a02005-02-26 23:04:13 +00005106 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005107 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005108 copy_tv(tv, &li->li_tv);
5109 list_append(l, li);
5110 return OK;
5111}
5112
5113/*
5114 * Add a dictionary to a list. Used by errorlist().
5115 * Return FAIL when out of memory.
5116 */
5117 int
5118list_append_dict(list, dict)
5119 list_T *list;
5120 dict_T *dict;
5121{
5122 listitem_T *li = listitem_alloc();
5123
5124 if (li == NULL)
5125 return FAIL;
5126 li->li_tv.v_type = VAR_DICT;
5127 li->li_tv.v_lock = 0;
5128 li->li_tv.vval.v_dict = dict;
5129 list_append(list, li);
5130 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005131 return OK;
5132}
5133
5134/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005135 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005136 * If "item" is NULL append at the end.
5137 * Return FAIL when out of memory.
5138 */
5139 static int
5140list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005141 list_T *l;
5142 typval_T *tv;
5143 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005144{
Bram Moolenaar33570922005-01-25 22:26:29 +00005145 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005146
5147 if (ni == NULL)
5148 return FAIL;
5149 copy_tv(tv, &ni->li_tv);
5150 if (item == NULL)
5151 /* Append new item at end of list. */
5152 list_append(l, ni);
5153 else
5154 {
5155 /* Insert new item before existing item. */
5156 ni->li_prev = item->li_prev;
5157 ni->li_next = item;
5158 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005159 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005160 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005161 ++l->lv_idx;
5162 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005163 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005164 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005165 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005166 l->lv_idx_item = NULL;
5167 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005168 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005169 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005170 }
5171 return OK;
5172}
5173
5174/*
5175 * Extend "l1" with "l2".
5176 * If "bef" is NULL append at the end, otherwise insert before this item.
5177 * Returns FAIL when out of memory.
5178 */
5179 static int
5180list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005181 list_T *l1;
5182 list_T *l2;
5183 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005184{
Bram Moolenaar33570922005-01-25 22:26:29 +00005185 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005186
5187 for (item = l2->lv_first; item != NULL; item = item->li_next)
5188 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5189 return FAIL;
5190 return OK;
5191}
5192
5193/*
5194 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5195 * Return FAIL when out of memory.
5196 */
5197 static int
5198list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005199 list_T *l1;
5200 list_T *l2;
5201 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005202{
Bram Moolenaar33570922005-01-25 22:26:29 +00005203 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005204
5205 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005206 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005207 if (l == NULL)
5208 return FAIL;
5209 tv->v_type = VAR_LIST;
5210 tv->vval.v_list = l;
5211
5212 /* append all items from the second list */
5213 return list_extend(l, l2, NULL);
5214}
5215
5216/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005217 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005218 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005219 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005220 * Returns NULL when out of memory.
5221 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005222 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005223list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005224 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005226 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005227{
Bram Moolenaar33570922005-01-25 22:26:29 +00005228 list_T *copy;
5229 listitem_T *item;
5230 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005231
5232 if (orig == NULL)
5233 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005234
5235 copy = list_alloc();
5236 if (copy != NULL)
5237 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005238 if (copyID != 0)
5239 {
5240 /* Do this before adding the items, because one of the items may
5241 * refer back to this list. */
5242 orig->lv_copyID = copyID;
5243 orig->lv_copylist = copy;
5244 }
5245 for (item = orig->lv_first; item != NULL && !got_int;
5246 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247 {
5248 ni = listitem_alloc();
5249 if (ni == NULL)
5250 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005251 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005252 {
5253 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5254 {
5255 vim_free(ni);
5256 break;
5257 }
5258 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005260 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 list_append(copy, ni);
5262 }
5263 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005264 if (item != NULL)
5265 {
5266 list_unref(copy);
5267 copy = NULL;
5268 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 }
5270
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 return copy;
5272}
5273
5274/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005275 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005276 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005278 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005279list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005280 list_T *l;
5281 listitem_T *item;
5282 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005283{
Bram Moolenaar33570922005-01-25 22:26:29 +00005284 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005286 /* notify watchers */
5287 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005289 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005290 list_fix_watch(l, ip);
5291 if (ip == item2)
5292 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005294
5295 if (item2->li_next == NULL)
5296 l->lv_last = item->li_prev;
5297 else
5298 item2->li_next->li_prev = item->li_prev;
5299 if (item->li_prev == NULL)
5300 l->lv_first = item2->li_next;
5301 else
5302 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005303 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304}
5305
5306/*
5307 * Return an allocated string with the string representation of a list.
5308 * May return NULL.
5309 */
5310 static char_u *
5311list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005312 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005313{
5314 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315
5316 if (tv->vval.v_list == NULL)
5317 return NULL;
5318 ga_init2(&ga, (int)sizeof(char), 80);
5319 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005320 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5321 {
5322 vim_free(ga.ga_data);
5323 return NULL;
5324 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 ga_append(&ga, ']');
5326 ga_append(&ga, NUL);
5327 return (char_u *)ga.ga_data;
5328}
5329
5330/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005331 * Join list "l" into a string in "*gap", using separator "sep".
5332 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005333 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005334 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005335 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005336list_join(gap, l, sep, echo)
5337 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005338 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005339 char_u *sep;
5340 int echo;
5341{
5342 int first = TRUE;
5343 char_u *tofree;
5344 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005345 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005346 char_u *s;
5347
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005348 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005349 {
5350 if (first)
5351 first = FALSE;
5352 else
5353 ga_concat(gap, sep);
5354
5355 if (echo)
5356 s = echo_string(&item->li_tv, &tofree, numbuf);
5357 else
5358 s = tv2string(&item->li_tv, &tofree, numbuf);
5359 if (s != NULL)
5360 ga_concat(gap, s);
5361 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005362 if (s == NULL)
5363 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005364 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005365 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005366}
5367
5368/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005369 * Allocate an empty header for a dictionary.
5370 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005371 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372dict_alloc()
5373{
Bram Moolenaar33570922005-01-25 22:26:29 +00005374 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005375
Bram Moolenaar33570922005-01-25 22:26:29 +00005376 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005377 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005378 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005379 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005380 d->dv_lock = 0;
5381 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005382 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005383 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005384 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005385}
5386
5387/*
5388 * Unreference a Dictionary: decrement the reference count and free it when it
5389 * becomes zero.
5390 */
5391 static void
5392dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005393 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394{
5395 if (d != NULL && --d->dv_refcount <= 0)
5396 dict_free(d);
5397}
5398
5399/*
5400 * Free a Dictionary, including all items it contains.
5401 * Ignores the reference count.
5402 */
5403 static void
5404dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005405 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005407 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005408 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005409
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005410 /* Careful: we free the dictitems while they still appear in the
Bram Moolenaar33570922005-01-25 22:26:29 +00005411 * hashtab. Must not try to resize the hashtab! */
5412 todo = d->dv_hashtab.ht_used;
5413 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005414 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005415 if (!HASHITEM_EMPTY(hi))
5416 {
5417 dictitem_free(HI2DI(hi));
5418 --todo;
5419 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005420 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005421 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422 vim_free(d);
5423}
5424
5425/*
5426 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005427 * The "key" is copied to the new item.
5428 * Note that the value of the item "di_tv" still needs to be initialized!
5429 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005431 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005432dictitem_alloc(key)
5433 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005434{
Bram Moolenaar33570922005-01-25 22:26:29 +00005435 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005436
Bram Moolenaar33570922005-01-25 22:26:29 +00005437 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005438 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005439 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005440 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005441 di->di_flags = 0;
5442 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005443 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444}
5445
5446/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005447 * Make a copy of a Dictionary item.
5448 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005449 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005450dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005451 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005452{
Bram Moolenaar33570922005-01-25 22:26:29 +00005453 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005454
Bram Moolenaar33570922005-01-25 22:26:29 +00005455 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005456 if (di != NULL)
5457 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005458 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005459 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005460 copy_tv(&org->di_tv, &di->di_tv);
5461 }
5462 return di;
5463}
5464
5465/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005466 * Remove item "item" from Dictionary "dict" and free it.
5467 */
5468 static void
5469dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005470 dict_T *dict;
5471 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005472{
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005474
Bram Moolenaar33570922005-01-25 22:26:29 +00005475 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005476 if (HASHITEM_EMPTY(hi))
5477 EMSG2(_(e_intern2), "dictitem_remove()");
5478 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005479 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005480 dictitem_free(item);
5481}
5482
5483/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005484 * Free a dict item. Also clears the value.
5485 */
5486 static void
5487dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005488 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005490 clear_tv(&item->di_tv);
5491 vim_free(item);
5492}
5493
5494/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005495 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5496 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005497 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00005498 * Returns NULL when out of memory.
5499 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005500 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005501dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005502 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005503 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005504 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005505{
Bram Moolenaar33570922005-01-25 22:26:29 +00005506 dict_T *copy;
5507 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005508 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005509 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005510
5511 if (orig == NULL)
5512 return NULL;
5513
5514 copy = dict_alloc();
5515 if (copy != NULL)
5516 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005517 if (copyID != 0)
5518 {
5519 orig->dv_copyID = copyID;
5520 orig->dv_copydict = copy;
5521 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005522 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005523 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005524 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005525 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005526 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005527 --todo;
5528
5529 di = dictitem_alloc(hi->hi_key);
5530 if (di == NULL)
5531 break;
5532 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005533 {
5534 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
5535 copyID) == FAIL)
5536 {
5537 vim_free(di);
5538 break;
5539 }
5540 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005541 else
5542 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5543 if (dict_add(copy, di) == FAIL)
5544 {
5545 dictitem_free(di);
5546 break;
5547 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005548 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005549 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005550
Bram Moolenaare9a41262005-01-15 22:18:47 +00005551 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005552 if (todo > 0)
5553 {
5554 dict_unref(copy);
5555 copy = NULL;
5556 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005557 }
5558
5559 return copy;
5560}
5561
5562/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005563 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005564 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005565 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005566 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005567dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005568 dict_T *d;
5569 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005570{
Bram Moolenaar33570922005-01-25 22:26:29 +00005571 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005572}
5573
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005575 * Add a number or string entry to dictionary "d".
5576 * When "str" is NULL use number "nr", otherwise use "str".
5577 * Returns FAIL when out of memory and when key already exists.
5578 */
5579 int
5580dict_add_nr_str(d, key, nr, str)
5581 dict_T *d;
5582 char *key;
5583 long nr;
5584 char_u *str;
5585{
5586 dictitem_T *item;
5587
5588 item = dictitem_alloc((char_u *)key);
5589 if (item == NULL)
5590 return FAIL;
5591 item->di_tv.v_lock = 0;
5592 if (str == NULL)
5593 {
5594 item->di_tv.v_type = VAR_NUMBER;
5595 item->di_tv.vval.v_number = nr;
5596 }
5597 else
5598 {
5599 item->di_tv.v_type = VAR_STRING;
5600 item->di_tv.vval.v_string = vim_strsave(str);
5601 }
5602 if (dict_add(d, item) == FAIL)
5603 {
5604 dictitem_free(item);
5605 return FAIL;
5606 }
5607 return OK;
5608}
5609
5610/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005611 * Get the number of items in a Dictionary.
5612 */
5613 static long
5614dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005615 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005616{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005617 if (d == NULL)
5618 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00005619 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005620}
5621
5622/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623 * Find item "key[len]" in Dictionary "d".
5624 * If "len" is negative use strlen(key).
5625 * Returns NULL when not found.
5626 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005627 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005628dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00005629 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005630 char_u *key;
5631 int len;
5632{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005633#define AKEYLEN 200
5634 char_u buf[AKEYLEN];
5635 char_u *akey;
5636 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00005637 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005638
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005639 if (len < 0)
5640 akey = key;
5641 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005642 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005643 tofree = akey = vim_strnsave(key, len);
5644 if (akey == NULL)
5645 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005646 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005647 else
5648 {
5649 /* Avoid a malloc/free by using buf[]. */
5650 STRNCPY(buf, key, len);
5651 buf[len] = NUL;
5652 akey = buf;
5653 }
5654
Bram Moolenaar33570922005-01-25 22:26:29 +00005655 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005656 vim_free(tofree);
5657 if (HASHITEM_EMPTY(hi))
5658 return NULL;
5659 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005660}
5661
5662/*
5663 * Return an allocated string with the string representation of a Dictionary.
5664 * May return NULL.
5665 */
5666 static char_u *
5667dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005668 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005669{
5670 garray_T ga;
5671 int first = TRUE;
5672 char_u *tofree;
5673 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005674 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00005676 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005677 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005679 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 return NULL;
5681 ga_init2(&ga, (int)sizeof(char), 80);
5682 ga_append(&ga, '{');
5683
Bram Moolenaar33570922005-01-25 22:26:29 +00005684 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005685 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005686 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005687 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005688 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005689 --todo;
5690
5691 if (first)
5692 first = FALSE;
5693 else
5694 ga_concat(&ga, (char_u *)", ");
5695
5696 tofree = string_quote(hi->hi_key, FALSE);
5697 if (tofree != NULL)
5698 {
5699 ga_concat(&ga, tofree);
5700 vim_free(tofree);
5701 }
5702 ga_concat(&ga, (char_u *)": ");
5703 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5704 if (s != NULL)
5705 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005707 if (s == NULL)
5708 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005711 if (todo > 0)
5712 {
5713 vim_free(ga.ga_data);
5714 return NULL;
5715 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716
5717 ga_append(&ga, '}');
5718 ga_append(&ga, NUL);
5719 return (char_u *)ga.ga_data;
5720}
5721
5722/*
5723 * Allocate a variable for a Dictionary and fill it from "*arg".
5724 * Return OK or FAIL. Returns NOTDONE for {expr}.
5725 */
5726 static int
5727get_dict_tv(arg, rettv, evaluate)
5728 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005729 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 int evaluate;
5731{
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 dict_T *d = NULL;
5733 typval_T tvkey;
5734 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00005736 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005737 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005738 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739
5740 /*
5741 * First check if it's not a curly-braces thing: {expr}.
5742 * Must do this without evaluating, otherwise a function may be called
5743 * twice. Unfortunately this means we need to call eval1() twice for the
5744 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005745 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005747 if (*start != '}')
5748 {
5749 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5750 return FAIL;
5751 if (*start == '}')
5752 return NOTDONE;
5753 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754
5755 if (evaluate)
5756 {
5757 d = dict_alloc();
5758 if (d == NULL)
5759 return FAIL;
5760 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005761 tvkey.v_type = VAR_UNKNOWN;
5762 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763
5764 *arg = skipwhite(*arg + 1);
5765 while (**arg != '}' && **arg != NUL)
5766 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005767 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005768 goto failret;
5769 if (**arg != ':')
5770 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005771 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005772 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 goto failret;
5774 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005775 key = get_tv_string_buf(&tvkey, buf);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005776 if (*key == NUL)
5777 {
5778 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005779 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 goto failret;
5781 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782
5783 *arg = skipwhite(*arg + 1);
5784 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5785 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005786 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 goto failret;
5788 }
5789 if (evaluate)
5790 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005791 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 if (item != NULL)
5793 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005794 EMSG(_("E721: Duplicate key in Dictionary"));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005795 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 clear_tv(&tv);
5797 goto failret;
5798 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005799 item = dictitem_alloc(key);
5800 clear_tv(&tvkey);
5801 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005804 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005805 if (dict_add(d, item) == FAIL)
5806 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 }
5808 }
5809
5810 if (**arg == '}')
5811 break;
5812 if (**arg != ',')
5813 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005814 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 goto failret;
5816 }
5817 *arg = skipwhite(*arg + 1);
5818 }
5819
5820 if (**arg != '}')
5821 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005822 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823failret:
5824 if (evaluate)
5825 dict_free(d);
5826 return FAIL;
5827 }
5828
5829 *arg = skipwhite(*arg + 1);
5830 if (evaluate)
5831 {
5832 rettv->v_type = VAR_DICT;
5833 rettv->vval.v_dict = d;
5834 ++d->dv_refcount;
5835 }
5836
5837 return OK;
5838}
5839
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005841 * Return a string with the string representation of a variable.
5842 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005843 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005845 * May return NULL;
5846 */
5847 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005849 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005851 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005853 static int recurse = 0;
5854 char_u *r = NULL;
5855
Bram Moolenaar33570922005-01-25 22:26:29 +00005856 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005857 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005858 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005859 *tofree = NULL;
5860 return NULL;
5861 }
5862 ++recurse;
5863
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 switch (tv->v_type)
5865 {
5866 case VAR_FUNC:
5867 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005868 r = tv->vval.v_string;
5869 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005870 case VAR_LIST:
5871 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005872 r = *tofree;
5873 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 case VAR_DICT:
5875 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005876 r = *tofree;
5877 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005878 case VAR_STRING:
5879 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005880 *tofree = NULL;
5881 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005883 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00005885 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005887
5888 --recurse;
5889 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890}
5891
5892/*
5893 * Return a string with the string representation of a variable.
5894 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5895 * "numbuf" is used for a number.
5896 * Puts quotes around strings, so that they can be parsed back by eval().
5897 * May return NULL;
5898 */
5899 static char_u *
5900tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005901 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 char_u **tofree;
5903 char_u *numbuf;
5904{
5905 switch (tv->v_type)
5906 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005907 case VAR_FUNC:
5908 *tofree = string_quote(tv->vval.v_string, TRUE);
5909 return *tofree;
5910 case VAR_STRING:
5911 *tofree = string_quote(tv->vval.v_string, FALSE);
5912 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005913 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005914 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005916 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005917 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005918 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005920 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921}
5922
5923/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005924 * Return string "str" in ' quotes, doubling ' characters.
5925 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005926 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005927 */
5928 static char_u *
5929string_quote(str, function)
5930 char_u *str;
5931 int function;
5932{
Bram Moolenaar33570922005-01-25 22:26:29 +00005933 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005934 char_u *p, *r, *s;
5935
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 len = (function ? 13 : 3);
5937 if (str != NULL)
5938 {
5939 len += STRLEN(str);
5940 for (p = str; *p != NUL; mb_ptr_adv(p))
5941 if (*p == '\'')
5942 ++len;
5943 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005944 s = r = alloc(len);
5945 if (r != NULL)
5946 {
5947 if (function)
5948 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005949 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005950 r += 10;
5951 }
5952 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005954 if (str != NULL)
5955 for (p = str; *p != NUL; )
5956 {
5957 if (*p == '\'')
5958 *r++ = '\'';
5959 MB_COPY_CHAR(p, r);
5960 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005961 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005962 if (function)
5963 *r++ = ')';
5964 *r++ = NUL;
5965 }
5966 return s;
5967}
5968
5969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 * Get the value of an environment variable.
5971 * "arg" is pointing to the '$'. It is advanced to after the name.
5972 * If the environment variable was not set, silently assume it is empty.
5973 * Always return OK.
5974 */
5975 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005976get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 int evaluate;
5980{
5981 char_u *string = NULL;
5982 int len;
5983 int cc;
5984 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005985 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
5987 ++*arg;
5988 name = *arg;
5989 len = get_env_len(arg);
5990 if (evaluate)
5991 {
5992 if (len != 0)
5993 {
5994 cc = name[len];
5995 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005996 /* first try vim_getenv(), fast for normal environment vars */
5997 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005999 {
6000 if (!mustfree)
6001 string = vim_strsave(string);
6002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 else
6004 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006005 if (mustfree)
6006 vim_free(string);
6007
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 /* next try expanding things like $VIM and ${HOME} */
6009 string = expand_env_save(name - 1);
6010 if (string != NULL && *string == '$')
6011 {
6012 vim_free(string);
6013 string = NULL;
6014 }
6015 }
6016 name[len] = cc;
6017 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006018 rettv->v_type = VAR_STRING;
6019 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 }
6021
6022 return OK;
6023}
6024
6025/*
6026 * Array with names and number of arguments of all internal functions
6027 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6028 */
6029static struct fst
6030{
6031 char *f_name; /* function name */
6032 char f_min_argc; /* minimal number of arguments */
6033 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036} functions[] =
6037{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006038 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 {"append", 2, 2, f_append},
6040 {"argc", 0, 0, f_argc},
6041 {"argidx", 0, 0, f_argidx},
6042 {"argv", 1, 1, f_argv},
6043 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006044 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 {"bufexists", 1, 1, f_bufexists},
6046 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6047 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6048 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6049 {"buflisted", 1, 1, f_buflisted},
6050 {"bufloaded", 1, 1, f_bufloaded},
6051 {"bufname", 1, 1, f_bufname},
6052 {"bufnr", 1, 1, f_bufnr},
6053 {"bufwinnr", 1, 1, f_bufwinnr},
6054 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006055 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006056 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 {"char2nr", 1, 1, f_char2nr},
6058 {"cindent", 1, 1, f_cindent},
6059 {"col", 1, 1, f_col},
6060 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006061 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006062 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 {"cscope_connection",0,3, f_cscope_connection},
6064 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006065 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 {"delete", 1, 1, f_delete},
6067 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006068 {"diff_filler", 1, 1, f_diff_filler},
6069 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006070 {"empty", 1, 1, f_empty},
Bram Moolenaar05159a02005-02-26 23:04:13 +00006071 {"errorlist", 0, 0, f_errorlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006073 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 {"eventhandler", 0, 0, f_eventhandler},
6075 {"executable", 1, 1, f_executable},
6076 {"exists", 1, 1, f_exists},
6077 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006078 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6080 {"filereadable", 1, 1, f_filereadable},
6081 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006082 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006083 {"finddir", 1, 3, f_finddir},
6084 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 {"fnamemodify", 2, 2, f_fnamemodify},
6086 {"foldclosed", 1, 1, f_foldclosed},
6087 {"foldclosedend", 1, 1, f_foldclosedend},
6088 {"foldlevel", 1, 1, f_foldlevel},
6089 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006090 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006093 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 {"getbufvar", 2, 2, f_getbufvar},
6095 {"getchar", 0, 1, f_getchar},
6096 {"getcharmod", 0, 0, f_getcharmod},
6097 {"getcmdline", 0, 0, f_getcmdline},
6098 {"getcmdpos", 0, 0, f_getcmdpos},
6099 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006100 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006101 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 {"getfsize", 1, 1, f_getfsize},
6103 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006104 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006105 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 {"getreg", 0, 1, f_getreg},
6107 {"getregtype", 0, 1, f_getregtype},
6108 {"getwinposx", 0, 0, f_getwinposx},
6109 {"getwinposy", 0, 0, f_getwinposy},
6110 {"getwinvar", 2, 2, f_getwinvar},
6111 {"glob", 1, 1, f_glob},
6112 {"globpath", 2, 2, f_globpath},
6113 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006114 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 {"hasmapto", 1, 2, f_hasmapto},
6116 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6117 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6118 {"histadd", 2, 2, f_histadd},
6119 {"histdel", 1, 2, f_histdel},
6120 {"histget", 1, 2, f_histget},
6121 {"histnr", 1, 1, f_histnr},
6122 {"hlID", 1, 1, f_hlID},
6123 {"hlexists", 1, 1, f_hlexists},
6124 {"hostname", 0, 0, f_hostname},
6125 {"iconv", 3, 3, f_iconv},
6126 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006127 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 {"input", 1, 2, f_input},
6129 {"inputdialog", 1, 3, f_inputdialog},
6130 {"inputrestore", 0, 0, f_inputrestore},
6131 {"inputsave", 0, 0, f_inputsave},
6132 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006133 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006135 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006136 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006137 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006138 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006140 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 {"libcall", 3, 3, f_libcall},
6142 {"libcallnr", 3, 3, f_libcallnr},
6143 {"line", 1, 1, f_line},
6144 {"line2byte", 1, 1, f_line2byte},
6145 {"lispindent", 1, 1, f_lispindent},
6146 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006147 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 {"maparg", 1, 2, f_maparg},
6149 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006150 {"match", 2, 4, f_match},
6151 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006152 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006153 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006154 {"max", 1, 1, f_max},
6155 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006156#ifdef vim_mkdir
6157 {"mkdir", 1, 3, f_mkdir},
6158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 {"mode", 0, 0, f_mode},
6160 {"nextnonblank", 1, 1, f_nextnonblank},
6161 {"nr2char", 1, 1, f_nr2char},
6162 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006163 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006164 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 {"remote_expr", 2, 3, f_remote_expr},
6166 {"remote_foreground", 1, 1, f_remote_foreground},
6167 {"remote_peek", 1, 2, f_remote_peek},
6168 {"remote_read", 1, 1, f_remote_read},
6169 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006170 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006172 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006174 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 {"search", 1, 2, f_search},
6176 {"searchpair", 3, 5, f_searchpair},
6177 {"server2client", 2, 2, f_server2client},
6178 {"serverlist", 0, 0, f_serverlist},
6179 {"setbufvar", 3, 3, f_setbufvar},
6180 {"setcmdpos", 1, 1, f_setcmdpos},
6181 {"setline", 2, 2, f_setline},
6182 {"setreg", 2, 3, f_setreg},
6183 {"setwinvar", 3, 3, f_setwinvar},
6184 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006185 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006186 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187#ifdef HAVE_STRFTIME
6188 {"strftime", 1, 2, f_strftime},
6189#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006190 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 {"strlen", 1, 1, f_strlen},
6193 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006194 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 {"strtrans", 1, 1, f_strtrans},
6196 {"submatch", 1, 1, f_submatch},
6197 {"substitute", 4, 4, f_substitute},
6198 {"synID", 3, 3, f_synID},
6199 {"synIDattr", 2, 3, f_synIDattr},
6200 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006201 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006202 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 {"tempname", 0, 0, f_tempname},
6204 {"tolower", 1, 1, f_tolower},
6205 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006206 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006208 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 {"virtcol", 1, 1, f_virtcol},
6210 {"visualmode", 0, 1, f_visualmode},
6211 {"winbufnr", 1, 1, f_winbufnr},
6212 {"wincol", 0, 0, f_wincol},
6213 {"winheight", 1, 1, f_winheight},
6214 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006215 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 {"winrestcmd", 0, 0, f_winrestcmd},
6217 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006218 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219};
6220
6221#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6222
6223/*
6224 * Function given to ExpandGeneric() to obtain the list of internal
6225 * or user defined function names.
6226 */
6227 char_u *
6228get_function_name(xp, idx)
6229 expand_T *xp;
6230 int idx;
6231{
6232 static int intidx = -1;
6233 char_u *name;
6234
6235 if (idx == 0)
6236 intidx = -1;
6237 if (intidx < 0)
6238 {
6239 name = get_user_func_name(xp, idx);
6240 if (name != NULL)
6241 return name;
6242 }
6243 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6244 {
6245 STRCPY(IObuff, functions[intidx].f_name);
6246 STRCAT(IObuff, "(");
6247 if (functions[intidx].f_max_argc == 0)
6248 STRCAT(IObuff, ")");
6249 return IObuff;
6250 }
6251
6252 return NULL;
6253}
6254
6255/*
6256 * Function given to ExpandGeneric() to obtain the list of internal or
6257 * user defined variable or function names.
6258 */
6259/*ARGSUSED*/
6260 char_u *
6261get_expr_name(xp, idx)
6262 expand_T *xp;
6263 int idx;
6264{
6265 static int intidx = -1;
6266 char_u *name;
6267
6268 if (idx == 0)
6269 intidx = -1;
6270 if (intidx < 0)
6271 {
6272 name = get_function_name(xp, idx);
6273 if (name != NULL)
6274 return name;
6275 }
6276 return get_user_var_name(xp, ++intidx);
6277}
6278
6279#endif /* FEAT_CMDL_COMPL */
6280
6281/*
6282 * Find internal function in table above.
6283 * Return index, or -1 if not found
6284 */
6285 static int
6286find_internal_func(name)
6287 char_u *name; /* name of the function */
6288{
6289 int first = 0;
6290 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6291 int cmp;
6292 int x;
6293
6294 /*
6295 * Find the function name in the table. Binary search.
6296 */
6297 while (first <= last)
6298 {
6299 x = first + ((unsigned)(last - first) >> 1);
6300 cmp = STRCMP(name, functions[x].f_name);
6301 if (cmp < 0)
6302 last = x - 1;
6303 else if (cmp > 0)
6304 first = x + 1;
6305 else
6306 return x;
6307 }
6308 return -1;
6309}
6310
6311/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6313 * name it contains, otherwise return "name".
6314 */
6315 static char_u *
6316deref_func_name(name, lenp)
6317 char_u *name;
6318 int *lenp;
6319{
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006321 int cc;
6322
6323 cc = name[*lenp];
6324 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006325 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006328 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006329 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006330 {
6331 *lenp = 0;
6332 return (char_u *)""; /* just in case */
6333 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006334 *lenp = STRLEN(v->di_tv.vval.v_string);
6335 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006336 }
6337
6338 return name;
6339}
6340
6341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 * Allocate a variable for the result of a function.
6343 * Return OK or FAIL.
6344 */
6345 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006346get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6347 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 char_u *name; /* name of the function */
6349 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 char_u **arg; /* argument, pointing to the '(' */
6352 linenr_T firstline; /* first line of range */
6353 linenr_T lastline; /* last line of range */
6354 int *doesrange; /* return: function handled range */
6355 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006356 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357{
6358 char_u *argp;
6359 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006360 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 int argcount = 0; /* number of arguments found */
6362
6363 /*
6364 * Get the arguments.
6365 */
6366 argp = *arg;
6367 while (argcount < MAX_FUNC_ARGS)
6368 {
6369 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6370 if (*argp == ')' || *argp == ',' || *argp == NUL)
6371 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6373 {
6374 ret = FAIL;
6375 break;
6376 }
6377 ++argcount;
6378 if (*argp != ',')
6379 break;
6380 }
6381 if (*argp == ')')
6382 ++argp;
6383 else
6384 ret = FAIL;
6385
6386 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006387 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006388 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006390 {
6391 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006392 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006393 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006394 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396
6397 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006398 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399
6400 *arg = skipwhite(argp);
6401 return ret;
6402}
6403
6404
6405/*
6406 * Call a function with its resolved parameters
6407 * Return OK or FAIL.
6408 */
6409 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006410call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006411 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 char_u *name; /* name of the function */
6413 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006414 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 linenr_T firstline; /* first line of range */
6418 linenr_T lastline; /* last line of range */
6419 int *doesrange; /* return: function handled range */
6420 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006421 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422{
6423 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424#define ERROR_UNKNOWN 0
6425#define ERROR_TOOMANY 1
6426#define ERROR_TOOFEW 2
6427#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006428#define ERROR_DICT 4
6429#define ERROR_NONE 5
6430#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 int error = ERROR_NONE;
6432 int i;
6433 int llen;
6434 ufunc_T *fp;
6435 int cc;
6436#define FLEN_FIXED 40
6437 char_u fname_buf[FLEN_FIXED + 1];
6438 char_u *fname;
6439
6440 /*
6441 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6442 * Change <SNR>123_name() to K_SNR 123_name().
6443 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6444 */
6445 cc = name[len];
6446 name[len] = NUL;
6447 llen = eval_fname_script(name);
6448 if (llen > 0)
6449 {
6450 fname_buf[0] = K_SPECIAL;
6451 fname_buf[1] = KS_EXTRA;
6452 fname_buf[2] = (int)KE_SNR;
6453 i = 3;
6454 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6455 {
6456 if (current_SID <= 0)
6457 error = ERROR_SCRIPT;
6458 else
6459 {
6460 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6461 i = (int)STRLEN(fname_buf);
6462 }
6463 }
6464 if (i + STRLEN(name + llen) < FLEN_FIXED)
6465 {
6466 STRCPY(fname_buf + i, name + llen);
6467 fname = fname_buf;
6468 }
6469 else
6470 {
6471 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
6472 if (fname == NULL)
6473 error = ERROR_OTHER;
6474 else
6475 {
6476 mch_memmove(fname, fname_buf, (size_t)i);
6477 STRCPY(fname + i, name + llen);
6478 }
6479 }
6480 }
6481 else
6482 fname = name;
6483
6484 *doesrange = FALSE;
6485
6486
6487 /* execute the function if no errors detected and executing */
6488 if (evaluate && error == ERROR_NONE)
6489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006490 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 error = ERROR_UNKNOWN;
6492
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006493 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 {
6495 /*
6496 * User defined function.
6497 */
6498 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006499
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006501 /* Trigger FuncUndefined event, may load the function. */
6502 if (fp == NULL
6503 && apply_autocmds(EVENT_FUNCUNDEFINED,
6504 fname, fname, TRUE, NULL)
6505 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006507 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 fp = find_func(fname);
6509 }
6510#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006511 /* Try loading a package. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006512 if (fp == NULL && script_autoload(fname) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006513 {
6514 /* loaded a package, search for the function again */
6515 fp = find_func(fname);
6516 }
6517
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 if (fp != NULL)
6519 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006520 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006522 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006524 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006526 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006527 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 else
6529 {
6530 /*
6531 * Call the user function.
6532 * Save and restore search patterns, script variables and
6533 * redo buffer.
6534 */
6535 save_search_patterns();
6536 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006537 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006538 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006539 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006540 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
6541 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
6542 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006543 /* Function was unreferenced while being used, free it
6544 * now. */
6545 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 restoreRedobuff();
6547 restore_search_patterns();
6548 error = ERROR_NONE;
6549 }
6550 }
6551 }
6552 else
6553 {
6554 /*
6555 * Find the function name in the table, call its implementation.
6556 */
6557 i = find_internal_func(fname);
6558 if (i >= 0)
6559 {
6560 if (argcount < functions[i].f_min_argc)
6561 error = ERROR_TOOFEW;
6562 else if (argcount > functions[i].f_max_argc)
6563 error = ERROR_TOOMANY;
6564 else
6565 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006566 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006567 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 error = ERROR_NONE;
6569 }
6570 }
6571 }
6572 /*
6573 * The function call (or "FuncUndefined" autocommand sequence) might
6574 * have been aborted by an error, an interrupt, or an explicitly thrown
6575 * exception that has not been caught so far. This situation can be
6576 * tested for by calling aborting(). For an error in an internal
6577 * function or for the "E132" error in call_user_func(), however, the
6578 * throw point at which the "force_abort" flag (temporarily reset by
6579 * emsg()) is normally updated has not been reached yet. We need to
6580 * update that flag first to make aborting() reliable.
6581 */
6582 update_force_abort();
6583 }
6584 if (error == ERROR_NONE)
6585 ret = OK;
6586
6587 /*
6588 * Report an error unless the argument evaluation or function call has been
6589 * cancelled due to an aborting error, an interrupt, or an exception.
6590 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006591 if (!aborting())
6592 {
6593 switch (error)
6594 {
6595 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006596 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006597 break;
6598 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006599 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006600 break;
6601 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006602 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006603 name);
6604 break;
6605 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006606 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006607 name);
6608 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006609 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006610 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00006611 name);
6612 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006613 }
6614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615
6616 name[len] = cc;
6617 if (fname != name && fname != fname_buf)
6618 vim_free(fname);
6619
6620 return ret;
6621}
6622
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006623/*
6624 * Give an error message with a function name. Handle <SNR> things.
6625 */
6626 static void
6627emsg_funcname(msg, name)
6628 char *msg;
6629 char_u *name;
6630{
6631 char_u *p;
6632
6633 if (*name == K_SPECIAL)
6634 p = concat_str((char_u *)"<SNR>", name + 3);
6635 else
6636 p = name;
6637 EMSG2(_(msg), p);
6638 if (p != name)
6639 vim_free(p);
6640}
6641
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642/*********************************************
6643 * Implementation of the built-in functions
6644 */
6645
6646/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006647 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 */
6649 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006650f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006651 typval_T *argvars;
6652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653{
Bram Moolenaar33570922005-01-25 22:26:29 +00006654 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006656 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006657 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006659 if ((l = argvars[0].vval.v_list) != NULL
6660 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
6661 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006662 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006663 }
6664 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006665 EMSG(_(e_listreq));
6666}
6667
6668/*
6669 * "append(lnum, string/list)" function
6670 */
6671 static void
6672f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006673 typval_T *argvars;
6674 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006675{
6676 long lnum;
Bram Moolenaar33570922005-01-25 22:26:29 +00006677 list_T *l = NULL;
6678 listitem_T *li = NULL;
6679 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006680 long added = 0;
6681
6682 rettv->vval.v_number = 1; /* Default: Failed */
6683 lnum = get_tv_lnum(argvars);
6684 if (lnum >= 0
6685 && lnum <= curbuf->b_ml.ml_line_count
6686 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006688 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006689 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006690 l = argvars[1].vval.v_list;
6691 if (l == NULL)
6692 return;
6693 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00006695 for (;;)
6696 {
6697 if (l == NULL)
6698 tv = &argvars[1]; /* append a string */
6699 else if (li == NULL)
6700 break; /* end of list */
6701 else
6702 tv = &li->li_tv; /* append item from list */
6703 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
6704 ++added;
6705 if (l == NULL)
6706 break;
6707 li = li->li_next;
6708 }
6709
6710 appended_lines_mark(lnum, added);
6711 if (curwin->w_cursor.lnum > lnum)
6712 curwin->w_cursor.lnum += added;
6713 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 }
6715}
6716
6717/*
6718 * "argc()" function
6719 */
6720/* ARGSUSED */
6721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006722f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006723 typval_T *argvars;
6724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006726 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727}
6728
6729/*
6730 * "argidx()" function
6731 */
6732/* ARGSUSED */
6733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006734f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006735 typval_T *argvars;
6736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006738 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739}
6740
6741/*
6742 * "argv(nr)" function
6743 */
6744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006745f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006746 typval_T *argvars;
6747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748{
6749 int idx;
6750
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006751 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006753 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006755 rettv->vval.v_string = NULL;
6756 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757}
6758
6759/*
6760 * "browse(save, title, initdir, default)" function
6761 */
6762/* ARGSUSED */
6763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006764f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006765 typval_T *argvars;
6766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767{
6768#ifdef FEAT_BROWSE
6769 int save;
6770 char_u *title;
6771 char_u *initdir;
6772 char_u *defname;
6773 char_u buf[NUMBUFLEN];
6774 char_u buf2[NUMBUFLEN];
6775
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006776 save = get_tv_number(&argvars[0]);
6777 title = get_tv_string(&argvars[1]);
6778 initdir = get_tv_string_buf(&argvars[2], buf);
6779 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006781 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006782 do_browse(save ? BROWSE_SAVE : 0,
6783 title, defname, NULL, initdir, NULL, curbuf);
6784#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006785 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006786#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006787 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006788}
6789
6790/*
6791 * "browsedir(title, initdir)" function
6792 */
6793/* ARGSUSED */
6794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006795f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006796 typval_T *argvars;
6797 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006798{
6799#ifdef FEAT_BROWSE
6800 char_u *title;
6801 char_u *initdir;
6802 char_u buf[NUMBUFLEN];
6803
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006804 title = get_tv_string(&argvars[0]);
6805 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006806
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006807 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006808 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006810 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006812 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813}
6814
Bram Moolenaar33570922005-01-25 22:26:29 +00006815static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006816
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817/*
6818 * Find a buffer by number or exact name.
6819 */
6820 static buf_T *
6821find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00006822 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823{
6824 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006826 if (avar->v_type == VAR_NUMBER)
6827 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006828 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006830 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006831 if (buf == NULL)
6832 {
6833 /* No full path name match, try a match with a URL or a "nofile"
6834 * buffer, these don't use the full path. */
6835 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6836 if (buf->b_fname != NULL
6837 && (path_with_url(buf->b_fname)
6838#ifdef FEAT_QUICKFIX
6839 || bt_nofile(buf)
6840#endif
6841 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006842 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006843 break;
6844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 }
6846 return buf;
6847}
6848
6849/*
6850 * "bufexists(expr)" function
6851 */
6852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006853f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006854 typval_T *argvars;
6855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006857 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858}
6859
6860/*
6861 * "buflisted(expr)" function
6862 */
6863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006864f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006865 typval_T *argvars;
6866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867{
6868 buf_T *buf;
6869
6870 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006871 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872}
6873
6874/*
6875 * "bufloaded(expr)" function
6876 */
6877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006878f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006879 typval_T *argvars;
6880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881{
6882 buf_T *buf;
6883
6884 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006885 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886}
6887
Bram Moolenaar33570922005-01-25 22:26:29 +00006888static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006889
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890/*
6891 * Get buffer by number or pattern.
6892 */
6893 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006894get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006895 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006897 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 int save_magic;
6899 char_u *save_cpo;
6900 buf_T *buf;
6901
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006902 if (tv->v_type == VAR_NUMBER)
6903 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006904 if (tv->v_type != VAR_STRING)
6905 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 if (name == NULL || *name == NUL)
6907 return curbuf;
6908 if (name[0] == '$' && name[1] == NUL)
6909 return lastbuf;
6910
6911 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
6912 save_magic = p_magic;
6913 p_magic = TRUE;
6914 save_cpo = p_cpo;
6915 p_cpo = (char_u *)"";
6916
6917 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
6918 TRUE, FALSE));
6919
6920 p_magic = save_magic;
6921 p_cpo = save_cpo;
6922
6923 /* If not found, try expanding the name, like done for bufexists(). */
6924 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006925 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926
6927 return buf;
6928}
6929
6930/*
6931 * "bufname(expr)" function
6932 */
6933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006934f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006935 typval_T *argvars;
6936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937{
6938 buf_T *buf;
6939
6940 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006941 buf = get_buf_tv(&argvars[0]);
6942 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006944 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006946 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 --emsg_off;
6948}
6949
6950/*
6951 * "bufnr(expr)" function
6952 */
6953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006954f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006955 typval_T *argvars;
6956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957{
6958 buf_T *buf;
6959
6960 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006961 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006963 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006965 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 --emsg_off;
6967}
6968
6969/*
6970 * "bufwinnr(nr)" function
6971 */
6972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006973f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006974 typval_T *argvars;
6975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976{
6977#ifdef FEAT_WINDOWS
6978 win_T *wp;
6979 int winnr = 0;
6980#endif
6981 buf_T *buf;
6982
6983 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006984 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985#ifdef FEAT_WINDOWS
6986 for (wp = firstwin; wp; wp = wp->w_next)
6987 {
6988 ++winnr;
6989 if (wp->w_buffer == buf)
6990 break;
6991 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006992 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006994 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995#endif
6996 --emsg_off;
6997}
6998
6999/*
7000 * "byte2line(byte)" function
7001 */
7002/*ARGSUSED*/
7003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007004f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007005 typval_T *argvars;
7006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007{
7008#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007009 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010#else
7011 long boff = 0;
7012
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007013 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007015 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007017 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 (linenr_T)0, &boff);
7019#endif
7020}
7021
7022/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007023 * "byteidx()" function
7024 */
7025/*ARGSUSED*/
7026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007027f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007028 typval_T *argvars;
7029 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007030{
7031#ifdef FEAT_MBYTE
7032 char_u *t;
7033#endif
7034 char_u *str;
7035 long idx;
7036
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007037 str = get_tv_string(&argvars[0]);
7038 idx = get_tv_number(&argvars[1]);
7039 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007040 if (idx < 0)
7041 return;
7042
7043#ifdef FEAT_MBYTE
7044 t = str;
7045 for ( ; idx > 0; idx--)
7046 {
7047 if (*t == NUL) /* EOL reached */
7048 return;
7049 t += mb_ptr2len_check(t);
7050 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007051 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007052#else
7053 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007054 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007055#endif
7056}
7057
7058/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007059 * "call(func, arglist)" function
7060 */
7061 static void
7062f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007063 typval_T *argvars;
7064 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007065{
7066 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007067 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007068 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007069 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007070 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007071 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007072
7073 rettv->vval.v_number = 0;
7074 if (argvars[1].v_type != VAR_LIST)
7075 {
7076 EMSG(_(e_listreq));
7077 return;
7078 }
7079 if (argvars[1].vval.v_list == NULL)
7080 return;
7081
7082 if (argvars[0].v_type == VAR_FUNC)
7083 func = argvars[0].vval.v_string;
7084 else
7085 func = get_tv_string(&argvars[0]);
7086
Bram Moolenaare9a41262005-01-15 22:18:47 +00007087 if (argvars[2].v_type != VAR_UNKNOWN)
7088 {
7089 if (argvars[2].v_type != VAR_DICT)
7090 {
7091 EMSG(_(e_dictreq));
7092 return;
7093 }
7094 selfdict = argvars[2].vval.v_dict;
7095 }
7096
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007097 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7098 item = item->li_next)
7099 {
7100 if (argc == MAX_FUNC_ARGS)
7101 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007102 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007103 break;
7104 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007105 /* Make a copy of each argument. This is needed to be able to set
7106 * v_lock to VAR_FIXED in the copy without changing the original list.
7107 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007108 copy_tv(&item->li_tv, &argv[argc++]);
7109 }
7110
7111 if (item == NULL)
7112 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007113 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7114 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007115
7116 /* Free the arguments. */
7117 while (argc > 0)
7118 clear_tv(&argv[--argc]);
7119}
7120
7121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 * "char2nr(string)" function
7123 */
7124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007125f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 typval_T *argvars;
7127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129#ifdef FEAT_MBYTE
7130 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007131 rettv->vval.v_number =
7132 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 else
7134#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007135 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136}
7137
7138/*
7139 * "cindent(lnum)" function
7140 */
7141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007142f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007143 typval_T *argvars;
7144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145{
7146#ifdef FEAT_CINDENT
7147 pos_T pos;
7148 linenr_T lnum;
7149
7150 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007151 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7153 {
7154 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007155 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 curwin->w_cursor = pos;
7157 }
7158 else
7159#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007160 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161}
7162
7163/*
7164 * "col(string)" function
7165 */
7166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007167f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007168 typval_T *argvars;
7169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
7171 colnr_T col = 0;
7172 pos_T *fp;
7173
7174 fp = var2fpos(&argvars[0], FALSE);
7175 if (fp != NULL)
7176 {
7177 if (fp->col == MAXCOL)
7178 {
7179 /* '> can be MAXCOL, get the length of the line then */
7180 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7181 col = STRLEN(ml_get(fp->lnum)) + 1;
7182 else
7183 col = MAXCOL;
7184 }
7185 else
7186 {
7187 col = fp->col + 1;
7188#ifdef FEAT_VIRTUALEDIT
7189 /* col(".") when the cursor is on the NUL at the end of the line
7190 * because of "coladd" can be seen as an extra column. */
7191 if (virtual_active() && fp == &curwin->w_cursor)
7192 {
7193 char_u *p = ml_get_cursor();
7194
7195 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7196 curwin->w_virtcol - curwin->w_cursor.coladd))
7197 {
7198# ifdef FEAT_MBYTE
7199 int l;
7200
7201 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7202 col += l;
7203# else
7204 if (*p != NUL && p[1] == NUL)
7205 ++col;
7206# endif
7207 }
7208 }
7209#endif
7210 }
7211 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007212 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213}
7214
7215/*
7216 * "confirm(message, buttons[, default [, type]])" function
7217 */
7218/*ARGSUSED*/
7219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007220f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007221 typval_T *argvars;
7222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223{
7224#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7225 char_u *message;
7226 char_u *buttons = NULL;
7227 char_u buf[NUMBUFLEN];
7228 char_u buf2[NUMBUFLEN];
7229 int def = 1;
7230 int type = VIM_GENERIC;
7231 int c;
7232
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007233 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007234 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007236 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007237 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007239 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007240 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007242 /* avoid that TOUPPER_ASC calls get_tv_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007243 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 switch (TOUPPER_ASC(c))
7245 {
7246 case 'E': type = VIM_ERROR; break;
7247 case 'Q': type = VIM_QUESTION; break;
7248 case 'I': type = VIM_INFO; break;
7249 case 'W': type = VIM_WARNING; break;
7250 case 'G': type = VIM_GENERIC; break;
7251 }
7252 }
7253 }
7254 }
7255
7256 if (buttons == NULL || *buttons == NUL)
7257 buttons = (char_u *)_("&Ok");
7258
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007259 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 def, NULL);
7261#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007262 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263#endif
7264}
7265
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007266/*
7267 * "copy()" function
7268 */
7269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007270f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007271 typval_T *argvars;
7272 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007273{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007274 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007275}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276
7277/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007278 * "count()" function
7279 */
7280 static void
7281f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 typval_T *argvars;
7283 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007284{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007285 long n = 0;
7286 int ic = FALSE;
7287
Bram Moolenaare9a41262005-01-15 22:18:47 +00007288 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007289 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007290 listitem_T *li;
7291 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007292 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007293
Bram Moolenaare9a41262005-01-15 22:18:47 +00007294 if ((l = argvars[0].vval.v_list) != NULL)
7295 {
7296 li = l->lv_first;
7297 if (argvars[2].v_type != VAR_UNKNOWN)
7298 {
7299 ic = get_tv_number(&argvars[2]);
7300 if (argvars[3].v_type != VAR_UNKNOWN)
7301 {
7302 idx = get_tv_number(&argvars[3]);
7303 li = list_find(l, idx);
7304 if (li == NULL)
7305 EMSGN(_(e_listidx), idx);
7306 }
7307 }
7308
7309 for ( ; li != NULL; li = li->li_next)
7310 if (tv_equal(&li->li_tv, &argvars[1], ic))
7311 ++n;
7312 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007313 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007314 else if (argvars[0].v_type == VAR_DICT)
7315 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007316 int todo;
7317 dict_T *d;
7318 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007319
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 if ((d = argvars[0].vval.v_dict) != NULL)
7321 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322 if (argvars[2].v_type != VAR_UNKNOWN)
7323 {
7324 ic = get_tv_number(&argvars[2]);
7325 if (argvars[3].v_type != VAR_UNKNOWN)
7326 EMSG(_(e_invarg));
7327 }
7328
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 todo = d->dv_hashtab.ht_used;
7330 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007331 {
7332 if (!HASHITEM_EMPTY(hi))
7333 {
7334 --todo;
7335 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7336 ++n;
7337 }
7338 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 }
7340 }
7341 else
7342 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007343 rettv->vval.v_number = n;
7344}
7345
7346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7348 *
7349 * Checks the existence of a cscope connection.
7350 */
7351/*ARGSUSED*/
7352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007353f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 typval_T *argvars;
7355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356{
7357#ifdef FEAT_CSCOPE
7358 int num = 0;
7359 char_u *dbpath = NULL;
7360 char_u *prepend = NULL;
7361 char_u buf[NUMBUFLEN];
7362
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007363 if (argvars[0].v_type != VAR_UNKNOWN
7364 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007366 num = (int)get_tv_number(&argvars[0]);
7367 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007368 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007369 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 }
7371
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007372 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007374 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375#endif
7376}
7377
7378/*
7379 * "cursor(lnum, col)" function
7380 *
7381 * Moves the cursor to the specified line and column
7382 */
7383/*ARGSUSED*/
7384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007385f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 typval_T *argvars;
7387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388{
7389 long line, col;
7390
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007391 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 if (line > 0)
7393 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007394 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 if (col > 0)
7396 curwin->w_cursor.col = col - 1;
7397#ifdef FEAT_VIRTUALEDIT
7398 curwin->w_cursor.coladd = 0;
7399#endif
7400
7401 /* Make sure the cursor is in a valid position. */
7402 check_cursor();
7403#ifdef FEAT_MBYTE
7404 /* Correct cursor for multi-byte character. */
7405 if (has_mbyte)
7406 mb_adjust_cursor();
7407#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007408
7409 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410}
7411
7412/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007413 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 */
7415 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007416f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007417 typval_T *argvars;
7418 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007420 static int copyID = 0;
7421 int noref = 0;
7422
7423 if (argvars[1].v_type != VAR_UNKNOWN)
7424 noref = get_tv_number(&argvars[1]);
7425 if (noref < 0 || noref > 1)
7426 EMSG(_(e_invarg));
7427 else
7428 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429}
7430
7431/*
7432 * "delete()" function
7433 */
7434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007435f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007436 typval_T *argvars;
7437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438{
7439 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007440 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007442 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443}
7444
7445/*
7446 * "did_filetype()" function
7447 */
7448/*ARGSUSED*/
7449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007450f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007451 typval_T *argvars;
7452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453{
7454#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007455 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007457 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458#endif
7459}
7460
7461/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00007462 * "diff_filler()" function
7463 */
7464/*ARGSUSED*/
7465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007466f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007467 typval_T *argvars;
7468 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007469{
7470#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007471 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00007472#endif
7473}
7474
7475/*
7476 * "diff_hlID()" function
7477 */
7478/*ARGSUSED*/
7479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007480f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007481 typval_T *argvars;
7482 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007483{
7484#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007485 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00007486 static linenr_T prev_lnum = 0;
7487 static int changedtick = 0;
7488 static int fnum = 0;
7489 static int change_start = 0;
7490 static int change_end = 0;
7491 static enum hlf_value hlID = 0;
7492 int filler_lines;
7493 int col;
7494
7495 if (lnum != prev_lnum
7496 || changedtick != curbuf->b_changedtick
7497 || fnum != curbuf->b_fnum)
7498 {
7499 /* New line, buffer, change: need to get the values. */
7500 filler_lines = diff_check(curwin, lnum);
7501 if (filler_lines < 0)
7502 {
7503 if (filler_lines == -1)
7504 {
7505 change_start = MAXCOL;
7506 change_end = -1;
7507 if (diff_find_change(curwin, lnum, &change_start, &change_end))
7508 hlID = HLF_ADD; /* added line */
7509 else
7510 hlID = HLF_CHD; /* changed line */
7511 }
7512 else
7513 hlID = HLF_ADD; /* added line */
7514 }
7515 else
7516 hlID = (enum hlf_value)0;
7517 prev_lnum = lnum;
7518 changedtick = curbuf->b_changedtick;
7519 fnum = curbuf->b_fnum;
7520 }
7521
7522 if (hlID == HLF_CHD || hlID == HLF_TXD)
7523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007524 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007525 if (col >= change_start && col <= change_end)
7526 hlID = HLF_TXD; /* changed text */
7527 else
7528 hlID = HLF_CHD; /* changed line */
7529 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007530 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007531#endif
7532}
7533
7534/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007535 * "empty({expr})" function
7536 */
7537 static void
7538f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007539 typval_T *argvars;
7540 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007541{
7542 int n;
7543
7544 switch (argvars[0].v_type)
7545 {
7546 case VAR_STRING:
7547 case VAR_FUNC:
7548 n = argvars[0].vval.v_string == NULL
7549 || *argvars[0].vval.v_string == NUL;
7550 break;
7551 case VAR_NUMBER:
7552 n = argvars[0].vval.v_number == 0;
7553 break;
7554 case VAR_LIST:
7555 n = argvars[0].vval.v_list == NULL
7556 || argvars[0].vval.v_list->lv_first == NULL;
7557 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007558 case VAR_DICT:
7559 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00007560 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007561 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007562 default:
7563 EMSG2(_(e_intern2), "f_empty()");
7564 n = 0;
7565 }
7566
7567 rettv->vval.v_number = n;
7568}
7569
7570/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007571 * "errorlist()" function
7572 */
7573/*ARGSUSED*/
7574 static void
7575f_errorlist(argvars, rettv)
7576 typval_T *argvars;
7577 typval_T *rettv;
7578{
7579#ifdef FEAT_QUICKFIX
7580 list_T *l;
7581#endif
7582
7583 rettv->vval.v_number = FALSE;
7584#ifdef FEAT_QUICKFIX
7585 l = list_alloc();
7586 if (l != NULL)
7587 {
7588 if (get_errorlist(l) != FAIL)
7589 {
7590 rettv->vval.v_list = l;
7591 rettv->v_type = VAR_LIST;
7592 ++l->lv_refcount;
7593 }
7594 else
7595 list_free(l);
7596 }
7597#endif
7598}
7599
7600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 * "escape({string}, {chars})" function
7602 */
7603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007604f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007605 typval_T *argvars;
7606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607{
7608 char_u buf[NUMBUFLEN];
7609
Bram Moolenaar758711c2005-02-02 23:11:38 +00007610 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
7611 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007612 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613}
7614
7615/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007616 * "eval()" function
7617 */
7618/*ARGSUSED*/
7619 static void
7620f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 typval_T *argvars;
7622 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007623{
7624 char_u *s;
7625
7626 s = get_tv_string(&argvars[0]);
7627 s = skipwhite(s);
7628
7629 if (eval1(&s, rettv, TRUE) == FAIL)
7630 rettv->vval.v_number = 0;
7631 else if (*s != NUL)
7632 EMSG(_(e_trailing));
7633}
7634
7635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 * "eventhandler()" function
7637 */
7638/*ARGSUSED*/
7639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007640f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007641 typval_T *argvars;
7642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007644 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645}
7646
7647/*
7648 * "executable()" function
7649 */
7650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007651f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007652 typval_T *argvars;
7653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007655 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656}
7657
7658/*
7659 * "exists()" function
7660 */
7661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007662f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007663 typval_T *argvars;
7664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665{
7666 char_u *p;
7667 char_u *name;
7668 int n = FALSE;
7669 int len = 0;
7670
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007671 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 if (*p == '$') /* environment variable */
7673 {
7674 /* first try "normal" environment variables (fast) */
7675 if (mch_getenv(p + 1) != NULL)
7676 n = TRUE;
7677 else
7678 {
7679 /* try expanding things like $VIM and ${HOME} */
7680 p = expand_env_save(p);
7681 if (p != NULL && *p != '$')
7682 n = TRUE;
7683 vim_free(p);
7684 }
7685 }
7686 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 else if (*p == '*') /* internal or user defined function */
7689 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007690 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 }
7692 else if (*p == ':')
7693 {
7694 n = cmd_exists(p + 1);
7695 }
7696 else if (*p == '#')
7697 {
7698#ifdef FEAT_AUTOCMD
7699 name = p + 1;
7700 p = vim_strchr(name, '#');
7701 if (p != NULL)
7702 n = au_exists(name, p, p + 1);
7703 else
7704 n = au_exists(name, name + STRLEN(name), NULL);
7705#endif
7706 }
7707 else /* internal variable */
7708 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007709 char_u *tofree;
7710 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007712 /* get_name_len() takes care of expanding curly braces */
7713 name = p;
7714 len = get_name_len(&p, &tofree, TRUE, FALSE);
7715 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007717 if (tofree != NULL)
7718 name = tofree;
7719 n = (get_var_tv(name, len, &tv, FALSE) == OK);
7720 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007722 /* handle d.key, l[idx], f(expr) */
7723 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
7724 if (n)
7725 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 }
7727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007729 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 }
7731
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007732 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733}
7734
7735/*
7736 * "expand()" function
7737 */
7738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007739f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007740 typval_T *argvars;
7741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742{
7743 char_u *s;
7744 int len;
7745 char_u *errormsg;
7746 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7747 expand_T xpc;
7748
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007749 rettv->v_type = VAR_STRING;
7750 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 if (*s == '%' || *s == '#' || *s == '<')
7752 {
7753 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007754 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 --emsg_off;
7756 }
7757 else
7758 {
7759 /* When the optional second argument is non-zero, don't remove matches
7760 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007761 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 flags |= WILD_KEEP_ALL;
7763 ExpandInit(&xpc);
7764 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 ExpandCleanup(&xpc);
7767 }
7768}
7769
7770/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007771 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00007772 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007773 */
7774 static void
7775f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007776 typval_T *argvars;
7777 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007778{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007779 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007780 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007781 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007782 list_T *l1, *l2;
7783 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007784 long before;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007785
Bram Moolenaare9a41262005-01-15 22:18:47 +00007786 l1 = argvars[0].vval.v_list;
7787 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007788 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
7789 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007790 {
7791 if (argvars[2].v_type != VAR_UNKNOWN)
7792 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007793 before = get_tv_number(&argvars[2]);
7794 if (before == l1->lv_len)
7795 item = NULL;
7796 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007797 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007798 item = list_find(l1, before);
7799 if (item == NULL)
7800 {
7801 EMSGN(_(e_listidx), before);
7802 return;
7803 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007804 }
7805 }
7806 else
7807 item = NULL;
7808 list_extend(l1, l2, item);
7809
7810 ++l1->lv_refcount;
7811 copy_tv(&argvars[0], rettv);
7812 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007813 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007814 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
7815 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007816 dict_T *d1, *d2;
7817 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 char_u *action;
7819 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007820 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007821 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007822
7823 d1 = argvars[0].vval.v_dict;
7824 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007825 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
7826 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007827 {
7828 /* Check the third argument. */
7829 if (argvars[2].v_type != VAR_UNKNOWN)
7830 {
7831 static char *(av[]) = {"keep", "force", "error"};
7832
7833 action = get_tv_string(&argvars[2]);
7834 for (i = 0; i < 3; ++i)
7835 if (STRCMP(action, av[i]) == 0)
7836 break;
7837 if (i == 3)
7838 {
7839 EMSGN(_(e_invarg2), action);
7840 return;
7841 }
7842 }
7843 else
7844 action = (char_u *)"force";
7845
7846 /* Go over all entries in the second dict and add them to the
7847 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007848 todo = d2->dv_hashtab.ht_used;
7849 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007850 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007851 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007852 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007853 --todo;
7854 di1 = dict_find(d1, hi2->hi_key, -1);
7855 if (di1 == NULL)
7856 {
7857 di1 = dictitem_copy(HI2DI(hi2));
7858 if (di1 != NULL && dict_add(d1, di1) == FAIL)
7859 dictitem_free(di1);
7860 }
7861 else if (*action == 'e')
7862 {
7863 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
7864 break;
7865 }
7866 else if (*action == 'f')
7867 {
7868 clear_tv(&di1->di_tv);
7869 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
7870 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007871 }
7872 }
7873
7874 ++d1->dv_refcount;
7875 copy_tv(&argvars[0], rettv);
7876 }
7877 }
7878 else
7879 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007880}
7881
7882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 * "filereadable()" function
7884 */
7885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007886f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007887 typval_T *argvars;
7888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889{
7890 FILE *fd;
7891 char_u *p;
7892 int n;
7893
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007894 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
7896 {
7897 n = TRUE;
7898 fclose(fd);
7899 }
7900 else
7901 n = FALSE;
7902
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007903 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904}
7905
7906/*
7907 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
7908 * rights to write into.
7909 */
7910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007911f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007912 typval_T *argvars;
7913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914{
7915 char_u *p;
7916 int retval = 0;
7917#if defined(UNIX) || defined(VMS)
7918 int perm = 0;
7919#endif
7920
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007921 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922#if defined(UNIX) || defined(VMS)
7923 perm = mch_getperm(p);
7924#endif
7925#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
7926 if (
7927# ifdef WIN3264
7928 mch_writable(p) &&
7929# else
7930# if defined(UNIX) || defined(VMS)
7931 (perm & 0222) &&
7932# endif
7933# endif
7934 mch_access((char *)p, W_OK) == 0
7935 )
7936#endif
7937 {
7938 ++retval;
7939 if (mch_isdir(p))
7940 ++retval;
7941 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007942 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943}
7944
Bram Moolenaar33570922005-01-25 22:26:29 +00007945static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007946
7947 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007948findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 typval_T *argvars;
7950 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007951 int dir;
7952{
7953#ifdef FEAT_SEARCHPATH
7954 char_u *fname;
7955 char_u *fresult = NULL;
7956 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
7957 char_u *p;
7958 char_u pathbuf[NUMBUFLEN];
7959 int count = 1;
7960 int first = TRUE;
7961
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007962 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007963
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007964 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007965 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007966 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007967 if (*p != NUL)
7968 path = p;
7969
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007970 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007971 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007972 }
7973
7974 do
7975 {
7976 vim_free(fresult);
7977 fresult = find_file_in_path_option(first ? fname : NULL,
7978 first ? (int)STRLEN(fname) : 0,
7979 0, first, path, dir, NULL);
7980 first = FALSE;
7981 } while (--count > 0 && fresult != NULL);
7982
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007983 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007984#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007985 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007986#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007987 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007988}
7989
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007990static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
7991static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00007992static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
7993static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007994
7995/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007996 * Prepare v: variable "idx" to be used.
7997 * Save the current typeval in "save_tv".
7998 * When not used yet add the variable to the v: hashtable.
7999 */
8000 static void
8001prepare_vimvar(idx, save_tv)
8002 int idx;
8003 typval_T *save_tv;
8004{
8005 *save_tv = vimvars[idx].vv_tv;
8006 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8007 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
8008}
8009
8010/*
8011 * Restore v: variable "idx" to typeval "save_tv".
8012 * When no longer defined, remove the variable from the v: hashtable.
8013 */
8014 static void
8015restore_vimvar(idx, save_tv)
8016 int idx;
8017 typval_T *save_tv;
8018{
8019 hashitem_T *hi;
8020
8021 clear_tv(&vimvars[idx].vv_tv);
8022 vimvars[idx].vv_tv = *save_tv;
8023 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8024 {
8025 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
8026 if (HASHITEM_EMPTY(hi))
8027 EMSG2(_(e_intern2), "restore_vimvar()");
8028 else
8029 hash_remove(&vimvarht, hi);
8030 }
8031}
8032
8033/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008034 * Implementation of map() and filter().
8035 */
8036 static void
8037filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008038 typval_T *argvars;
8039 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008040 int map;
8041{
8042 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008043 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 listitem_T *li, *nli;
8045 list_T *l = NULL;
8046 dictitem_T *di;
8047 hashtab_T *ht;
8048 hashitem_T *hi;
8049 dict_T *d = NULL;
8050 typval_T save_val;
8051 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008052 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008053 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008054 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8055
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008056
8057 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008058 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008059 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008060 if ((l = argvars[0].vval.v_list) == NULL
8061 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008062 return;
8063 }
8064 else if (argvars[0].v_type == VAR_DICT)
8065 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008066 if ((d = argvars[0].vval.v_dict) == NULL
8067 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008068 return;
8069 }
8070 else
8071 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008072 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008073 return;
8074 }
8075
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008076 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008077 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaare9a41262005-01-15 22:18:47 +00008078
8079 if (argvars[0].v_type == VAR_DICT)
8080 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008081 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar33570922005-01-25 22:26:29 +00008082 vimvars[VV_KEY].vv_type = VAR_STRING;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008083
Bram Moolenaar33570922005-01-25 22:26:29 +00008084 ht = &d->dv_hashtab;
Bram Moolenaara7043832005-01-21 11:56:39 +00008085 hash_lock(ht);
8086 todo = ht->ht_used;
8087 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008088 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008089 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008090 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008091 --todo;
8092 di = HI2DI(hi);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008093 if (tv_check_lock(di->di_tv.v_lock, msg))
8094 break;
Bram Moolenaar33570922005-01-25 22:26:29 +00008095 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008096 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8097 break;
8098 if (!map && rem)
8099 dictitem_remove(d, di);
Bram Moolenaar33570922005-01-25 22:26:29 +00008100 clear_tv(&vimvars[VV_KEY].vv_tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008101 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008102 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008103 hash_unlock(ht);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008104
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008105 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008106 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008107 else
8108 {
8109 for (li = l->lv_first; li != NULL; li = nli)
8110 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008111 if (tv_check_lock(li->li_tv.v_lock, msg))
8112 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008113 nli = li->li_next;
8114 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
8115 break;
8116 if (!map && rem)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00008117 listitem_remove(l, li);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008118 }
8119 }
8120
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008121 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008122
8123 copy_tv(&argvars[0], rettv);
8124}
8125
8126 static int
8127filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008128 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008129 char_u *expr;
8130 int map;
8131 int *remp;
8132{
Bram Moolenaar33570922005-01-25 22:26:29 +00008133 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008134 char_u *s;
8135
Bram Moolenaar33570922005-01-25 22:26:29 +00008136 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008137 s = expr;
8138 if (eval1(&s, &rettv, TRUE) == FAIL)
8139 return FAIL;
8140 if (*s != NUL) /* check for trailing chars after expr */
8141 {
8142 EMSG2(_(e_invexpr2), s);
8143 return FAIL;
8144 }
8145 if (map)
8146 {
8147 /* map(): replace the list item value */
8148 clear_tv(tv);
8149 *tv = rettv;
8150 }
8151 else
8152 {
8153 /* filter(): when expr is zero remove the item */
8154 *remp = (get_tv_number(&rettv) == 0);
8155 clear_tv(&rettv);
8156 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008157 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008158 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008159}
8160
8161/*
8162 * "filter()" function
8163 */
8164 static void
8165f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008166 typval_T *argvars;
8167 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008168{
8169 filter_map(argvars, rettv, FALSE);
8170}
8171
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008172/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008173 * "finddir({fname}[, {path}[, {count}]])" function
8174 */
8175 static void
8176f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008177 typval_T *argvars;
8178 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008179{
8180 findfilendir(argvars, rettv, TRUE);
8181}
8182
8183/*
8184 * "findfile({fname}[, {path}[, {count}]])" function
8185 */
8186 static void
8187f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008188 typval_T *argvars;
8189 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008190{
8191 findfilendir(argvars, rettv, FALSE);
8192}
8193
8194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 * "fnamemodify({fname}, {mods})" function
8196 */
8197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008198f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008199 typval_T *argvars;
8200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201{
8202 char_u *fname;
8203 char_u *mods;
8204 int usedlen = 0;
8205 int len;
8206 char_u *fbuf = NULL;
8207 char_u buf[NUMBUFLEN];
8208
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008209 fname = get_tv_string(&argvars[0]);
8210 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 len = (int)STRLEN(fname);
8212
8213 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8214
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008215 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008217 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008219 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 vim_free(fbuf);
8221}
8222
Bram Moolenaar33570922005-01-25 22:26:29 +00008223static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224
8225/*
8226 * "foldclosed()" function
8227 */
8228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008229foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008230 typval_T *argvars;
8231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 int end;
8233{
8234#ifdef FEAT_FOLDING
8235 linenr_T lnum;
8236 linenr_T first, last;
8237
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008238 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8240 {
8241 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8242 {
8243 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008244 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008246 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 return;
8248 }
8249 }
8250#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008251 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252}
8253
8254/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008255 * "foldclosed()" function
8256 */
8257 static void
8258f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008259 typval_T *argvars;
8260 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008261{
8262 foldclosed_both(argvars, rettv, FALSE);
8263}
8264
8265/*
8266 * "foldclosedend()" function
8267 */
8268 static void
8269f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008270 typval_T *argvars;
8271 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008272{
8273 foldclosed_both(argvars, rettv, TRUE);
8274}
8275
8276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 * "foldlevel()" function
8278 */
8279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008280f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008281 typval_T *argvars;
8282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283{
8284#ifdef FEAT_FOLDING
8285 linenr_T lnum;
8286
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008287 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008289 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 else
8291#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008292 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293}
8294
8295/*
8296 * "foldtext()" function
8297 */
8298/*ARGSUSED*/
8299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008300f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008301 typval_T *argvars;
8302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303{
8304#ifdef FEAT_FOLDING
8305 linenr_T lnum;
8306 char_u *s;
8307 char_u *r;
8308 int len;
8309 char *txt;
8310#endif
8311
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008312 rettv->v_type = VAR_STRING;
8313 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008315 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8316 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8317 <= curbuf->b_ml.ml_line_count
8318 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {
8320 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008321 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8322 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 {
8324 if (!linewhite(lnum))
8325 break;
8326 ++lnum;
8327 }
8328
8329 /* Find interesting text in this line. */
8330 s = skipwhite(ml_get(lnum));
8331 /* skip C comment-start */
8332 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008333 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008335 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008336 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008337 {
8338 s = skipwhite(ml_get(lnum + 1));
8339 if (*s == '*')
8340 s = skipwhite(s + 1);
8341 }
8342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 txt = _("+-%s%3ld lines: ");
8344 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008345 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 + 20 /* for %3ld */
8347 + STRLEN(s))); /* concatenated */
8348 if (r != NULL)
8349 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008350 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8351 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8352 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 len = (int)STRLEN(r);
8354 STRCAT(r, s);
8355 /* remove 'foldmarker' and 'commentstring' */
8356 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008357 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 }
8359 }
8360#endif
8361}
8362
8363/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008364 * "foldtextresult(lnum)" function
8365 */
8366/*ARGSUSED*/
8367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008368f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 typval_T *argvars;
8370 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008371{
8372#ifdef FEAT_FOLDING
8373 linenr_T lnum;
8374 char_u *text;
8375 char_u buf[51];
8376 foldinfo_T foldinfo;
8377 int fold_count;
8378#endif
8379
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008380 rettv->v_type = VAR_STRING;
8381 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008382#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008383 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008384 fold_count = foldedCount(curwin, lnum, &foldinfo);
8385 if (fold_count > 0)
8386 {
8387 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8388 &foldinfo, buf);
8389 if (text == buf)
8390 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008391 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008392 }
8393#endif
8394}
8395
8396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 * "foreground()" function
8398 */
8399/*ARGSUSED*/
8400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008401f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008402 typval_T *argvars;
8403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008405 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406#ifdef FEAT_GUI
8407 if (gui.in_use)
8408 gui_mch_set_foreground();
8409#else
8410# ifdef WIN32
8411 win32_set_foreground();
8412# endif
8413#endif
8414}
8415
8416/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008417 * "function()" function
8418 */
8419/*ARGSUSED*/
8420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008421f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008422 typval_T *argvars;
8423 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008424{
8425 char_u *s;
8426
Bram Moolenaara7043832005-01-21 11:56:39 +00008427 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008428 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008429 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008430 EMSG2(_(e_invarg2), s);
8431 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008432 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008433 else
8434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008435 rettv->vval.v_string = vim_strsave(s);
8436 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008437 }
8438}
8439
8440/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008441 * "get()" function
8442 */
8443 static void
8444f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008445 typval_T *argvars;
8446 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008447{
Bram Moolenaar33570922005-01-25 22:26:29 +00008448 listitem_T *li;
8449 list_T *l;
8450 dictitem_T *di;
8451 dict_T *d;
8452 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008453
Bram Moolenaare9a41262005-01-15 22:18:47 +00008454 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008455 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008456 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008457 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008458 li = list_find(l, get_tv_number(&argvars[1]));
8459 if (li != NULL)
8460 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008461 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008462 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008463 else if (argvars[0].v_type == VAR_DICT)
8464 {
8465 if ((d = argvars[0].vval.v_dict) != NULL)
8466 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008467 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008468 if (di != NULL)
8469 tv = &di->di_tv;
8470 }
8471 }
8472 else
8473 EMSG2(_(e_listdictarg), "get()");
8474
8475 if (tv == NULL)
8476 {
8477 if (argvars[2].v_type == VAR_UNKNOWN)
8478 rettv->vval.v_number = 0;
8479 else
8480 copy_tv(&argvars[2], rettv);
8481 }
8482 else
8483 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008484}
8485
8486/*
8487 * "getbufvar()" function
8488 */
8489 static void
8490f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008491 typval_T *argvars;
8492 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008493{
8494 buf_T *buf;
8495 buf_T *save_curbuf;
8496 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008497 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008498
8499 ++emsg_off;
8500 buf = get_buf_tv(&argvars[0]);
8501 varname = get_tv_string(&argvars[1]);
8502
8503 rettv->v_type = VAR_STRING;
8504 rettv->vval.v_string = NULL;
8505
8506 if (buf != NULL && varname != NULL)
8507 {
8508 if (*varname == '&') /* buffer-local-option */
8509 {
8510 /* set curbuf to be our buf, temporarily */
8511 save_curbuf = curbuf;
8512 curbuf = buf;
8513
8514 get_option_tv(&varname, rettv, TRUE);
8515
8516 /* restore previous notion of curbuf */
8517 curbuf = save_curbuf;
8518 }
8519 else
8520 {
8521 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008522 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008523 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008524 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008525 }
8526 }
8527
8528 --emsg_off;
8529}
8530
8531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 * "getchar()" function
8533 */
8534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008535f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008536 typval_T *argvars;
8537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538{
8539 varnumber_T n;
8540
8541 ++no_mapping;
8542 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008543 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 /* getchar(): blocking wait. */
8545 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008546 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 /* getchar(1): only check if char avail */
8548 n = vpeekc();
8549 else if (vpeekc() == NUL)
8550 /* getchar(0) and no char avail: return zero */
8551 n = 0;
8552 else
8553 /* getchar(0) and char avail: return char */
8554 n = safe_vgetc();
8555 --no_mapping;
8556 --allow_keys;
8557
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008558 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 if (IS_SPECIAL(n) || mod_mask != 0)
8560 {
8561 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
8562 int i = 0;
8563
8564 /* Turn a special key into three bytes, plus modifier. */
8565 if (mod_mask != 0)
8566 {
8567 temp[i++] = K_SPECIAL;
8568 temp[i++] = KS_MODIFIER;
8569 temp[i++] = mod_mask;
8570 }
8571 if (IS_SPECIAL(n))
8572 {
8573 temp[i++] = K_SPECIAL;
8574 temp[i++] = K_SECOND(n);
8575 temp[i++] = K_THIRD(n);
8576 }
8577#ifdef FEAT_MBYTE
8578 else if (has_mbyte)
8579 i += (*mb_char2bytes)(n, temp + i);
8580#endif
8581 else
8582 temp[i++] = n;
8583 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584 rettv->v_type = VAR_STRING;
8585 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 }
8587}
8588
8589/*
8590 * "getcharmod()" function
8591 */
8592/*ARGSUSED*/
8593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008595 typval_T *argvars;
8596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008598 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599}
8600
8601/*
8602 * "getcmdline()" function
8603 */
8604/*ARGSUSED*/
8605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008606f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008607 typval_T *argvars;
8608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008610 rettv->v_type = VAR_STRING;
8611 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612}
8613
8614/*
8615 * "getcmdpos()" function
8616 */
8617/*ARGSUSED*/
8618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008619f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008620 typval_T *argvars;
8621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008623 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624}
8625
8626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 * "getcwd()" function
8628 */
8629/*ARGSUSED*/
8630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008631f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008632 typval_T *argvars;
8633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634{
8635 char_u cwd[MAXPATHL];
8636
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008637 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008639 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 else
8641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008642 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008644 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645#endif
8646 }
8647}
8648
8649/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008650 * "getfontname()" function
8651 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008652/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008654f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008655 typval_T *argvars;
8656 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008657{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008658 rettv->v_type = VAR_STRING;
8659 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008660#ifdef FEAT_GUI
8661 if (gui.in_use)
8662 {
8663 GuiFont font;
8664 char_u *name = NULL;
8665
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008666 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008667 {
8668 /* Get the "Normal" font. Either the name saved by
8669 * hl_set_font_name() or from the font ID. */
8670 font = gui.norm_font;
8671 name = hl_get_font_name();
8672 }
8673 else
8674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008676 if (STRCMP(name, "*") == 0) /* don't use font dialog */
8677 return;
8678 font = gui_mch_get_font(name, FALSE);
8679 if (font == NOFONT)
8680 return; /* Invalid font name, return empty string. */
8681 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008682 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008683 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008684 gui_mch_free_font(font);
8685 }
8686#endif
8687}
8688
8689/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008690 * "getfperm({fname})" function
8691 */
8692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008693f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008694 typval_T *argvars;
8695 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008696{
8697 char_u *fname;
8698 struct stat st;
8699 char_u *perm = NULL;
8700 char_u flags[] = "rwx";
8701 int i;
8702
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008703 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008704
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008705 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008706 if (mch_stat((char *)fname, &st) >= 0)
8707 {
8708 perm = vim_strsave((char_u *)"---------");
8709 if (perm != NULL)
8710 {
8711 for (i = 0; i < 9; i++)
8712 {
8713 if (st.st_mode & (1 << (8 - i)))
8714 perm[i] = flags[i % 3];
8715 }
8716 }
8717 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008718 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008719}
8720
8721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 * "getfsize({fname})" function
8723 */
8724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008725f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008726 typval_T *argvars;
8727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728{
8729 char_u *fname;
8730 struct stat st;
8731
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008732 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008734 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735
8736 if (mch_stat((char *)fname, &st) >= 0)
8737 {
8738 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008739 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008741 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 }
8743 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008744 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745}
8746
8747/*
8748 * "getftime({fname})" function
8749 */
8750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008752 typval_T *argvars;
8753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754{
8755 char_u *fname;
8756 struct stat st;
8757
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008758 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759
8760 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008761 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008763 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764}
8765
8766/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008767 * "getftype({fname})" function
8768 */
8769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 typval_T *argvars;
8772 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008773{
8774 char_u *fname;
8775 struct stat st;
8776 char_u *type = NULL;
8777 char *t;
8778
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008779 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008781 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008782 if (mch_lstat((char *)fname, &st) >= 0)
8783 {
8784#ifdef S_ISREG
8785 if (S_ISREG(st.st_mode))
8786 t = "file";
8787 else if (S_ISDIR(st.st_mode))
8788 t = "dir";
8789# ifdef S_ISLNK
8790 else if (S_ISLNK(st.st_mode))
8791 t = "link";
8792# endif
8793# ifdef S_ISBLK
8794 else if (S_ISBLK(st.st_mode))
8795 t = "bdev";
8796# endif
8797# ifdef S_ISCHR
8798 else if (S_ISCHR(st.st_mode))
8799 t = "cdev";
8800# endif
8801# ifdef S_ISFIFO
8802 else if (S_ISFIFO(st.st_mode))
8803 t = "fifo";
8804# endif
8805# ifdef S_ISSOCK
8806 else if (S_ISSOCK(st.st_mode))
8807 t = "fifo";
8808# endif
8809 else
8810 t = "other";
8811#else
8812# ifdef S_IFMT
8813 switch (st.st_mode & S_IFMT)
8814 {
8815 case S_IFREG: t = "file"; break;
8816 case S_IFDIR: t = "dir"; break;
8817# ifdef S_IFLNK
8818 case S_IFLNK: t = "link"; break;
8819# endif
8820# ifdef S_IFBLK
8821 case S_IFBLK: t = "bdev"; break;
8822# endif
8823# ifdef S_IFCHR
8824 case S_IFCHR: t = "cdev"; break;
8825# endif
8826# ifdef S_IFIFO
8827 case S_IFIFO: t = "fifo"; break;
8828# endif
8829# ifdef S_IFSOCK
8830 case S_IFSOCK: t = "socket"; break;
8831# endif
8832 default: t = "other";
8833 }
8834# else
8835 if (mch_isdir(fname))
8836 t = "dir";
8837 else
8838 t = "file";
8839# endif
8840#endif
8841 type = vim_strsave((char_u *)t);
8842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008843 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008844}
8845
8846/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008847 * "getline(lnum)" function
8848 */
8849 static void
8850f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008851 typval_T *argvars;
8852 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008853{
8854 linenr_T lnum;
8855 linenr_T end;
8856 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00008857 list_T *l;
8858 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008859
8860 lnum = get_tv_lnum(argvars);
8861
8862 if (argvars[1].v_type == VAR_UNKNOWN)
8863 {
8864 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8865 p = ml_get(lnum);
8866 else
8867 p = (char_u *)"";
8868
8869 rettv->v_type = VAR_STRING;
8870 rettv->vval.v_string = vim_strsave(p);
8871 }
8872 else
8873 {
8874 end = get_tv_lnum(&argvars[1]);
8875 if (end < lnum)
8876 {
8877 EMSG(_(e_invrange));
8878 rettv->vval.v_number = 0;
8879 }
8880 else
8881 {
8882 l = list_alloc();
8883 if (l != NULL)
8884 {
8885 if (lnum < 1)
8886 lnum = 1;
8887 if (end > curbuf->b_ml.ml_line_count)
8888 end = curbuf->b_ml.ml_line_count;
8889 while (lnum <= end)
8890 {
8891 li = listitem_alloc();
8892 if (li == NULL)
8893 break;
8894 list_append(l, li);
8895 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008896 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008897 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
8898 }
8899 rettv->vval.v_list = l;
8900 rettv->v_type = VAR_LIST;
8901 ++l->lv_refcount;
8902 }
8903 }
8904 }
8905}
8906
8907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 * "getreg()" function
8909 */
8910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008911f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008912 typval_T *argvars;
8913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914{
8915 char_u *strregname;
8916 int regname;
8917
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008918 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008919 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008921 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 regname = (strregname == NULL ? '"' : *strregname);
8923 if (regname == 0)
8924 regname = '"';
8925
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008926 rettv->v_type = VAR_STRING;
8927 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928}
8929
8930/*
8931 * "getregtype()" function
8932 */
8933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008935 typval_T *argvars;
8936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937{
8938 char_u *strregname;
8939 int regname;
8940 char_u buf[NUMBUFLEN + 2];
8941 long reglen = 0;
8942
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008943 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 else
8946 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008947 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948
8949 regname = (strregname == NULL ? '"' : *strregname);
8950 if (regname == 0)
8951 regname = '"';
8952
8953 buf[0] = NUL;
8954 buf[1] = NUL;
8955 switch (get_reg_type(regname, &reglen))
8956 {
8957 case MLINE: buf[0] = 'V'; break;
8958 case MCHAR: buf[0] = 'v'; break;
8959#ifdef FEAT_VISUAL
8960 case MBLOCK:
8961 buf[0] = Ctrl_V;
8962 sprintf((char *)buf + 1, "%ld", reglen + 1);
8963 break;
8964#endif
8965 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 rettv->v_type = VAR_STRING;
8967 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968}
8969
8970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 * "getwinposx()" function
8972 */
8973/*ARGSUSED*/
8974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008976 typval_T *argvars;
8977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980#ifdef FEAT_GUI
8981 if (gui.in_use)
8982 {
8983 int x, y;
8984
8985 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 }
8988#endif
8989}
8990
8991/*
8992 * "getwinposy()" function
8993 */
8994/*ARGSUSED*/
8995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008997 typval_T *argvars;
8998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009000 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001#ifdef FEAT_GUI
9002 if (gui.in_use)
9003 {
9004 int x, y;
9005
9006 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009007 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 }
9009#endif
9010}
9011
9012/*
9013 * "getwinvar()" function
9014 */
9015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009016f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009017 typval_T *argvars;
9018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019{
9020 win_T *win, *oldcurwin;
9021 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009022 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023
9024 ++emsg_off;
9025 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009028 rettv->v_type = VAR_STRING;
9029 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030
9031 if (win != NULL && varname != NULL)
9032 {
9033 if (*varname == '&') /* window-local-option */
9034 {
9035 /* set curwin to be our win, temporarily */
9036 oldcurwin = curwin;
9037 curwin = win;
9038
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040
9041 /* restore previous notion of curwin */
9042 curwin = oldcurwin;
9043 }
9044 else
9045 {
9046 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009047 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009049 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 }
9051 }
9052
9053 --emsg_off;
9054}
9055
9056/*
9057 * "glob()" function
9058 */
9059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009060f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009061 typval_T *argvars;
9062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063{
9064 expand_T xpc;
9065
9066 ExpandInit(&xpc);
9067 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068 rettv->v_type = VAR_STRING;
9069 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9071 ExpandCleanup(&xpc);
9072}
9073
9074/*
9075 * "globpath()" function
9076 */
9077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009079 typval_T *argvars;
9080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081{
9082 char_u buf1[NUMBUFLEN];
9083
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084 rettv->v_type = VAR_STRING;
9085 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
9086 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087}
9088
9089/*
9090 * "has()" function
9091 */
9092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009093f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009094 typval_T *argvars;
9095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096{
9097 int i;
9098 char_u *name;
9099 int n = FALSE;
9100 static char *(has_list[]) =
9101 {
9102#ifdef AMIGA
9103 "amiga",
9104# ifdef FEAT_ARP
9105 "arp",
9106# endif
9107#endif
9108#ifdef __BEOS__
9109 "beos",
9110#endif
9111#ifdef MSDOS
9112# ifdef DJGPP
9113 "dos32",
9114# else
9115 "dos16",
9116# endif
9117#endif
9118#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9119 "mac",
9120#endif
9121#if defined(MACOS_X_UNIX)
9122 "macunix",
9123#endif
9124#ifdef OS2
9125 "os2",
9126#endif
9127#ifdef __QNX__
9128 "qnx",
9129#endif
9130#ifdef RISCOS
9131 "riscos",
9132#endif
9133#ifdef UNIX
9134 "unix",
9135#endif
9136#ifdef VMS
9137 "vms",
9138#endif
9139#ifdef WIN16
9140 "win16",
9141#endif
9142#ifdef WIN32
9143 "win32",
9144#endif
9145#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9146 "win32unix",
9147#endif
9148#ifdef WIN64
9149 "win64",
9150#endif
9151#ifdef EBCDIC
9152 "ebcdic",
9153#endif
9154#ifndef CASE_INSENSITIVE_FILENAME
9155 "fname_case",
9156#endif
9157#ifdef FEAT_ARABIC
9158 "arabic",
9159#endif
9160#ifdef FEAT_AUTOCMD
9161 "autocmd",
9162#endif
9163#ifdef FEAT_BEVAL
9164 "balloon_eval",
9165#endif
9166#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9167 "builtin_terms",
9168# ifdef ALL_BUILTIN_TCAPS
9169 "all_builtin_terms",
9170# endif
9171#endif
9172#ifdef FEAT_BYTEOFF
9173 "byte_offset",
9174#endif
9175#ifdef FEAT_CINDENT
9176 "cindent",
9177#endif
9178#ifdef FEAT_CLIENTSERVER
9179 "clientserver",
9180#endif
9181#ifdef FEAT_CLIPBOARD
9182 "clipboard",
9183#endif
9184#ifdef FEAT_CMDL_COMPL
9185 "cmdline_compl",
9186#endif
9187#ifdef FEAT_CMDHIST
9188 "cmdline_hist",
9189#endif
9190#ifdef FEAT_COMMENTS
9191 "comments",
9192#endif
9193#ifdef FEAT_CRYPT
9194 "cryptv",
9195#endif
9196#ifdef FEAT_CSCOPE
9197 "cscope",
9198#endif
9199#ifdef DEBUG
9200 "debug",
9201#endif
9202#ifdef FEAT_CON_DIALOG
9203 "dialog_con",
9204#endif
9205#ifdef FEAT_GUI_DIALOG
9206 "dialog_gui",
9207#endif
9208#ifdef FEAT_DIFF
9209 "diff",
9210#endif
9211#ifdef FEAT_DIGRAPHS
9212 "digraphs",
9213#endif
9214#ifdef FEAT_DND
9215 "dnd",
9216#endif
9217#ifdef FEAT_EMACS_TAGS
9218 "emacs_tags",
9219#endif
9220 "eval", /* always present, of course! */
9221#ifdef FEAT_EX_EXTRA
9222 "ex_extra",
9223#endif
9224#ifdef FEAT_SEARCH_EXTRA
9225 "extra_search",
9226#endif
9227#ifdef FEAT_FKMAP
9228 "farsi",
9229#endif
9230#ifdef FEAT_SEARCHPATH
9231 "file_in_path",
9232#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009233#if defined(UNIX) && !defined(USE_SYSTEM)
9234 "filterpipe",
9235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236#ifdef FEAT_FIND_ID
9237 "find_in_path",
9238#endif
9239#ifdef FEAT_FOLDING
9240 "folding",
9241#endif
9242#ifdef FEAT_FOOTER
9243 "footer",
9244#endif
9245#if !defined(USE_SYSTEM) && defined(UNIX)
9246 "fork",
9247#endif
9248#ifdef FEAT_GETTEXT
9249 "gettext",
9250#endif
9251#ifdef FEAT_GUI
9252 "gui",
9253#endif
9254#ifdef FEAT_GUI_ATHENA
9255# ifdef FEAT_GUI_NEXTAW
9256 "gui_neXtaw",
9257# else
9258 "gui_athena",
9259# endif
9260#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009261#ifdef FEAT_GUI_KDE
9262 "gui_kde",
9263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264#ifdef FEAT_GUI_GTK
9265 "gui_gtk",
9266# ifdef HAVE_GTK2
9267 "gui_gtk2",
9268# endif
9269#endif
9270#ifdef FEAT_GUI_MAC
9271 "gui_mac",
9272#endif
9273#ifdef FEAT_GUI_MOTIF
9274 "gui_motif",
9275#endif
9276#ifdef FEAT_GUI_PHOTON
9277 "gui_photon",
9278#endif
9279#ifdef FEAT_GUI_W16
9280 "gui_win16",
9281#endif
9282#ifdef FEAT_GUI_W32
9283 "gui_win32",
9284#endif
9285#ifdef FEAT_HANGULIN
9286 "hangul_input",
9287#endif
9288#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
9289 "iconv",
9290#endif
9291#ifdef FEAT_INS_EXPAND
9292 "insert_expand",
9293#endif
9294#ifdef FEAT_JUMPLIST
9295 "jumplist",
9296#endif
9297#ifdef FEAT_KEYMAP
9298 "keymap",
9299#endif
9300#ifdef FEAT_LANGMAP
9301 "langmap",
9302#endif
9303#ifdef FEAT_LIBCALL
9304 "libcall",
9305#endif
9306#ifdef FEAT_LINEBREAK
9307 "linebreak",
9308#endif
9309#ifdef FEAT_LISP
9310 "lispindent",
9311#endif
9312#ifdef FEAT_LISTCMDS
9313 "listcmds",
9314#endif
9315#ifdef FEAT_LOCALMAP
9316 "localmap",
9317#endif
9318#ifdef FEAT_MENU
9319 "menu",
9320#endif
9321#ifdef FEAT_SESSION
9322 "mksession",
9323#endif
9324#ifdef FEAT_MODIFY_FNAME
9325 "modify_fname",
9326#endif
9327#ifdef FEAT_MOUSE
9328 "mouse",
9329#endif
9330#ifdef FEAT_MOUSESHAPE
9331 "mouseshape",
9332#endif
9333#if defined(UNIX) || defined(VMS)
9334# ifdef FEAT_MOUSE_DEC
9335 "mouse_dec",
9336# endif
9337# ifdef FEAT_MOUSE_GPM
9338 "mouse_gpm",
9339# endif
9340# ifdef FEAT_MOUSE_JSB
9341 "mouse_jsbterm",
9342# endif
9343# ifdef FEAT_MOUSE_NET
9344 "mouse_netterm",
9345# endif
9346# ifdef FEAT_MOUSE_PTERM
9347 "mouse_pterm",
9348# endif
9349# ifdef FEAT_MOUSE_XTERM
9350 "mouse_xterm",
9351# endif
9352#endif
9353#ifdef FEAT_MBYTE
9354 "multi_byte",
9355#endif
9356#ifdef FEAT_MBYTE_IME
9357 "multi_byte_ime",
9358#endif
9359#ifdef FEAT_MULTI_LANG
9360 "multi_lang",
9361#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009362#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00009363#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009364 "mzscheme",
9365#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367#ifdef FEAT_OLE
9368 "ole",
9369#endif
9370#ifdef FEAT_OSFILETYPE
9371 "osfiletype",
9372#endif
9373#ifdef FEAT_PATH_EXTRA
9374 "path_extra",
9375#endif
9376#ifdef FEAT_PERL
9377#ifndef DYNAMIC_PERL
9378 "perl",
9379#endif
9380#endif
9381#ifdef FEAT_PYTHON
9382#ifndef DYNAMIC_PYTHON
9383 "python",
9384#endif
9385#endif
9386#ifdef FEAT_POSTSCRIPT
9387 "postscript",
9388#endif
9389#ifdef FEAT_PRINTER
9390 "printer",
9391#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00009392#ifdef FEAT_PROFILE
9393 "profile",
9394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395#ifdef FEAT_QUICKFIX
9396 "quickfix",
9397#endif
9398#ifdef FEAT_RIGHTLEFT
9399 "rightleft",
9400#endif
9401#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
9402 "ruby",
9403#endif
9404#ifdef FEAT_SCROLLBIND
9405 "scrollbind",
9406#endif
9407#ifdef FEAT_CMDL_INFO
9408 "showcmd",
9409 "cmdline_info",
9410#endif
9411#ifdef FEAT_SIGNS
9412 "signs",
9413#endif
9414#ifdef FEAT_SMARTINDENT
9415 "smartindent",
9416#endif
9417#ifdef FEAT_SNIFF
9418 "sniff",
9419#endif
9420#ifdef FEAT_STL_OPT
9421 "statusline",
9422#endif
9423#ifdef FEAT_SUN_WORKSHOP
9424 "sun_workshop",
9425#endif
9426#ifdef FEAT_NETBEANS_INTG
9427 "netbeans_intg",
9428#endif
9429#ifdef FEAT_SYN_HL
9430 "syntax",
9431#endif
9432#if defined(USE_SYSTEM) || !defined(UNIX)
9433 "system",
9434#endif
9435#ifdef FEAT_TAG_BINS
9436 "tag_binary",
9437#endif
9438#ifdef FEAT_TAG_OLDSTATIC
9439 "tag_old_static",
9440#endif
9441#ifdef FEAT_TAG_ANYWHITE
9442 "tag_any_white",
9443#endif
9444#ifdef FEAT_TCL
9445# ifndef DYNAMIC_TCL
9446 "tcl",
9447# endif
9448#endif
9449#ifdef TERMINFO
9450 "terminfo",
9451#endif
9452#ifdef FEAT_TERMRESPONSE
9453 "termresponse",
9454#endif
9455#ifdef FEAT_TEXTOBJ
9456 "textobjects",
9457#endif
9458#ifdef HAVE_TGETENT
9459 "tgetent",
9460#endif
9461#ifdef FEAT_TITLE
9462 "title",
9463#endif
9464#ifdef FEAT_TOOLBAR
9465 "toolbar",
9466#endif
9467#ifdef FEAT_USR_CMDS
9468 "user-commands", /* was accidentally included in 5.4 */
9469 "user_commands",
9470#endif
9471#ifdef FEAT_VIMINFO
9472 "viminfo",
9473#endif
9474#ifdef FEAT_VERTSPLIT
9475 "vertsplit",
9476#endif
9477#ifdef FEAT_VIRTUALEDIT
9478 "virtualedit",
9479#endif
9480#ifdef FEAT_VISUAL
9481 "visual",
9482#endif
9483#ifdef FEAT_VISUALEXTRA
9484 "visualextra",
9485#endif
9486#ifdef FEAT_VREPLACE
9487 "vreplace",
9488#endif
9489#ifdef FEAT_WILDIGN
9490 "wildignore",
9491#endif
9492#ifdef FEAT_WILDMENU
9493 "wildmenu",
9494#endif
9495#ifdef FEAT_WINDOWS
9496 "windows",
9497#endif
9498#ifdef FEAT_WAK
9499 "winaltkeys",
9500#endif
9501#ifdef FEAT_WRITEBACKUP
9502 "writebackup",
9503#endif
9504#ifdef FEAT_XIM
9505 "xim",
9506#endif
9507#ifdef FEAT_XFONTSET
9508 "xfontset",
9509#endif
9510#ifdef USE_XSMP
9511 "xsmp",
9512#endif
9513#ifdef USE_XSMP_INTERACT
9514 "xsmp_interact",
9515#endif
9516#ifdef FEAT_XCLIPBOARD
9517 "xterm_clipboard",
9518#endif
9519#ifdef FEAT_XTERM_SAVE
9520 "xterm_save",
9521#endif
9522#if defined(UNIX) && defined(FEAT_X11)
9523 "X11",
9524#endif
9525 NULL
9526 };
9527
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009528 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 for (i = 0; has_list[i] != NULL; ++i)
9530 if (STRICMP(name, has_list[i]) == 0)
9531 {
9532 n = TRUE;
9533 break;
9534 }
9535
9536 if (n == FALSE)
9537 {
9538 if (STRNICMP(name, "patch", 5) == 0)
9539 n = has_patch(atoi((char *)name + 5));
9540 else if (STRICMP(name, "vim_starting") == 0)
9541 n = (starting != 0);
9542#ifdef DYNAMIC_TCL
9543 else if (STRICMP(name, "tcl") == 0)
9544 n = tcl_enabled(FALSE);
9545#endif
9546#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
9547 else if (STRICMP(name, "iconv") == 0)
9548 n = iconv_enabled(FALSE);
9549#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009550#ifdef DYNAMIC_MZSCHEME
9551 else if (STRICMP(name, "mzscheme") == 0)
9552 n = mzscheme_enabled(FALSE);
9553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554#ifdef DYNAMIC_RUBY
9555 else if (STRICMP(name, "ruby") == 0)
9556 n = ruby_enabled(FALSE);
9557#endif
9558#ifdef DYNAMIC_PYTHON
9559 else if (STRICMP(name, "python") == 0)
9560 n = python_enabled(FALSE);
9561#endif
9562#ifdef DYNAMIC_PERL
9563 else if (STRICMP(name, "perl") == 0)
9564 n = perl_enabled(FALSE);
9565#endif
9566#ifdef FEAT_GUI
9567 else if (STRICMP(name, "gui_running") == 0)
9568 n = (gui.in_use || gui.starting);
9569# ifdef FEAT_GUI_W32
9570 else if (STRICMP(name, "gui_win32s") == 0)
9571 n = gui_is_win32s();
9572# endif
9573# ifdef FEAT_BROWSE
9574 else if (STRICMP(name, "browse") == 0)
9575 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
9576# endif
9577#endif
9578#ifdef FEAT_SYN_HL
9579 else if (STRICMP(name, "syntax_items") == 0)
9580 n = syntax_present(curbuf);
9581#endif
9582#if defined(WIN3264)
9583 else if (STRICMP(name, "win95") == 0)
9584 n = mch_windows95();
9585#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00009586#ifdef FEAT_NETBEANS_INTG
9587 else if (STRICMP(name, "netbeans_enabled") == 0)
9588 n = usingNetbeans;
9589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 }
9591
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009592 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593}
9594
9595/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00009596 * "has_key()" function
9597 */
9598 static void
9599f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009600 typval_T *argvars;
9601 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009602{
9603 rettv->vval.v_number = 0;
9604 if (argvars[0].v_type != VAR_DICT)
9605 {
9606 EMSG(_(e_dictreq));
9607 return;
9608 }
9609 if (argvars[0].vval.v_dict == NULL)
9610 return;
9611
9612 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009613 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009614}
9615
9616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 * "hasmapto()" function
9618 */
9619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009621 typval_T *argvars;
9622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
9624 char_u *name;
9625 char_u *mode;
9626 char_u buf[NUMBUFLEN];
9627
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009629 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 mode = (char_u *)"nvo";
9631 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009632 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633
9634 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009635 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009637 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638}
9639
9640/*
9641 * "histadd()" function
9642 */
9643/*ARGSUSED*/
9644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009645f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009646 typval_T *argvars;
9647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648{
9649#ifdef FEAT_CMDHIST
9650 int histype;
9651 char_u *str;
9652 char_u buf[NUMBUFLEN];
9653#endif
9654
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 if (check_restricted() || check_secure())
9657 return;
9658#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009659 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 if (histype >= 0)
9661 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009662 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 if (*str != NUL)
9664 {
9665 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009666 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 return;
9668 }
9669 }
9670#endif
9671}
9672
9673/*
9674 * "histdel()" function
9675 */
9676/*ARGSUSED*/
9677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009678f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009679 typval_T *argvars;
9680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681{
9682#ifdef FEAT_CMDHIST
9683 int n;
9684 char_u buf[NUMBUFLEN];
9685
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009686 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009689 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
9692 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 else
9694 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
9696 get_tv_string_buf(&argvars[1], buf));
9697 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700#endif
9701}
9702
9703/*
9704 * "histget()" function
9705 */
9706/*ARGSUSED*/
9707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009709 typval_T *argvars;
9710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711{
9712#ifdef FEAT_CMDHIST
9713 int type;
9714 int idx;
9715
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009717 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 idx = get_history_idx(type);
9719 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720 idx = (int)get_tv_number(&argvars[1]);
9721 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009723 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009725 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726}
9727
9728/*
9729 * "histnr()" function
9730 */
9731/*ARGSUSED*/
9732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009733f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009734 typval_T *argvars;
9735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736{
9737 int i;
9738
9739#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 if (i >= HIST_CMD && i < HIST_COUNT)
9742 i = get_history_idx(i);
9743 else
9744#endif
9745 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009746 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747}
9748
9749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 * "highlightID(name)" function
9751 */
9752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009753f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009754 typval_T *argvars;
9755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009757 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758}
9759
9760/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009761 * "highlight_exists()" function
9762 */
9763 static void
9764f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009765 typval_T *argvars;
9766 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009767{
9768 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
9769}
9770
9771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 * "hostname()" function
9773 */
9774/*ARGSUSED*/
9775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009777 typval_T *argvars;
9778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779{
9780 char_u hostname[256];
9781
9782 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009783 rettv->v_type = VAR_STRING;
9784 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785}
9786
9787/*
9788 * iconv() function
9789 */
9790/*ARGSUSED*/
9791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009793 typval_T *argvars;
9794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795{
9796#ifdef FEAT_MBYTE
9797 char_u buf1[NUMBUFLEN];
9798 char_u buf2[NUMBUFLEN];
9799 char_u *from, *to, *str;
9800 vimconv_T vimconv;
9801#endif
9802
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803 rettv->v_type = VAR_STRING;
9804 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805
9806#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009807 str = get_tv_string(&argvars[0]);
9808 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
9809 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 vimconv.vc_type = CONV_NONE;
9811 convert_setup(&vimconv, from, to);
9812
9813 /* If the encodings are equal, no conversion needed. */
9814 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818
9819 convert_setup(&vimconv, NULL, NULL);
9820 vim_free(from);
9821 vim_free(to);
9822#endif
9823}
9824
9825/*
9826 * "indent()" function
9827 */
9828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009829f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009830 typval_T *argvars;
9831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832{
9833 linenr_T lnum;
9834
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009837 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009839 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840}
9841
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009842/*
9843 * "index()" function
9844 */
9845 static void
9846f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009847 typval_T *argvars;
9848 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009849{
Bram Moolenaar33570922005-01-25 22:26:29 +00009850 list_T *l;
9851 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009852 long idx = 0;
9853 int ic = FALSE;
9854
9855 rettv->vval.v_number = -1;
9856 if (argvars[0].v_type != VAR_LIST)
9857 {
9858 EMSG(_(e_listreq));
9859 return;
9860 }
9861 l = argvars[0].vval.v_list;
9862 if (l != NULL)
9863 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009864 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009865 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009866 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009867 /* Start at specified item. Use the cached index that list_find()
9868 * sets, so that a negative number also works. */
9869 item = list_find(l, get_tv_number(&argvars[2]));
9870 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009871 if (argvars[3].v_type != VAR_UNKNOWN)
9872 ic = get_tv_number(&argvars[3]);
9873 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009874
Bram Moolenaar758711c2005-02-02 23:11:38 +00009875 for ( ; item != NULL; item = item->li_next, ++idx)
9876 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009877 {
9878 rettv->vval.v_number = idx;
9879 break;
9880 }
9881 }
9882}
9883
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884static int inputsecret_flag = 0;
9885
9886/*
9887 * "input()" function
9888 * Also handles inputsecret() when inputsecret is set.
9889 */
9890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009891f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009892 typval_T *argvars;
9893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009895 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 char_u *p = NULL;
9897 int c;
9898 char_u buf[NUMBUFLEN];
9899 int cmd_silent_save = cmd_silent;
9900
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902
9903#ifdef NO_CONSOLE_INPUT
9904 /* While starting up, there is no place to enter text. */
9905 if (no_console_input())
9906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009907 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 return;
9909 }
9910#endif
9911
9912 cmd_silent = FALSE; /* Want to see the prompt. */
9913 if (prompt != NULL)
9914 {
9915 /* Only the part of the message after the last NL is considered as
9916 * prompt for the command line */
9917 p = vim_strrchr(prompt, '\n');
9918 if (p == NULL)
9919 p = prompt;
9920 else
9921 {
9922 ++p;
9923 c = *p;
9924 *p = NUL;
9925 msg_start();
9926 msg_clr_eos();
9927 msg_puts_attr(prompt, echo_attr);
9928 msg_didout = FALSE;
9929 msg_starthere();
9930 *p = c;
9931 }
9932 cmdline_row = msg_row;
9933 }
9934
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009935 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009936 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009938 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
9940
9941 /* since the user typed this, no need to wait for return */
9942 need_wait_return = FALSE;
9943 msg_didout = FALSE;
9944 cmd_silent = cmd_silent_save;
9945}
9946
9947/*
9948 * "inputdialog()" function
9949 */
9950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009951f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009952 typval_T *argvars;
9953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954{
9955#if defined(FEAT_GUI_TEXTDIALOG)
9956 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
9957 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
9958 {
9959 char_u *message;
9960 char_u buf[NUMBUFLEN];
9961
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009962 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009963 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009965 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 IObuff[IOSIZE - 1] = NUL;
9967 }
9968 else
9969 IObuff[0] = NUL;
9970 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
9971 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 else
9974 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009975 if (argvars[1].v_type != VAR_UNKNOWN
9976 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977 rettv->vval.v_string = vim_strsave(
9978 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009980 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 }
9984 else
9985#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009986 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987}
9988
9989static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
9990
9991/*
9992 * "inputrestore()" function
9993 */
9994/*ARGSUSED*/
9995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009996f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009997 typval_T *argvars;
9998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999{
10000 if (ga_userinput.ga_len > 0)
10001 {
10002 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10004 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010005 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 }
10007 else if (p_verbose > 1)
10008 {
10009 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010010 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 }
10012}
10013
10014/*
10015 * "inputsave()" function
10016 */
10017/*ARGSUSED*/
10018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010019f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010020 typval_T *argvars;
10021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022{
10023 /* Add an entry to the stack of typehead storage. */
10024 if (ga_grow(&ga_userinput, 1) == OK)
10025 {
10026 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10027 + ga_userinput.ga_len);
10028 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010029 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 }
10031 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033}
10034
10035/*
10036 * "inputsecret()" function
10037 */
10038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010040 typval_T *argvars;
10041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042{
10043 ++cmdline_star;
10044 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 --cmdline_star;
10047 --inputsecret_flag;
10048}
10049
10050/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010051 * "insert()" function
10052 */
10053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010055 typval_T *argvars;
10056 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010057{
10058 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010059 listitem_T *item;
10060 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010061
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010062 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010063 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010064 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010065 else if ((l = argvars[0].vval.v_list) != NULL
10066 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010067 {
10068 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010069 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010070
Bram Moolenaar758711c2005-02-02 23:11:38 +000010071 if (before == l->lv_len)
10072 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010073 else
10074 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010075 item = list_find(l, before);
10076 if (item == NULL)
10077 {
10078 EMSGN(_(e_listidx), before);
10079 l = NULL;
10080 }
10081 }
10082 if (l != NULL)
10083 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010084 list_insert_tv(l, &argvars[1], item);
10085 ++l->lv_refcount;
10086 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010087 }
10088 }
10089}
10090
10091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 * "isdirectory()" function
10093 */
10094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010095f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010096 typval_T *argvars;
10097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010099 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100}
10101
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010102/*
10103 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
10104 * it refers to a List or Dictionary that is locked.
10105 */
10106 static int
10107tv_islocked(tv)
10108 typval_T *tv;
10109{
10110 return (tv->v_lock & VAR_LOCKED)
10111 || (tv->v_type == VAR_LIST
10112 && tv->vval.v_list != NULL
10113 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
10114 || (tv->v_type == VAR_DICT
10115 && tv->vval.v_dict != NULL
10116 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
10117}
10118
10119/*
10120 * "islocked()" function
10121 */
10122 static void
10123f_islocked(argvars, rettv)
10124 typval_T *argvars;
10125 typval_T *rettv;
10126{
10127 lval_T lv;
10128 char_u *end;
10129 dictitem_T *di;
10130
10131 rettv->vval.v_number = -1;
10132 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE);
10133 if (end != NULL && lv.ll_name != NULL)
10134 {
10135 if (*end != NUL)
10136 EMSG(_(e_trailing));
10137 else
10138 {
10139 if (lv.ll_tv == NULL)
10140 {
10141 if (check_changedtick(lv.ll_name))
10142 rettv->vval.v_number = 1; /* always locked */
10143 else
10144 {
10145 di = find_var(lv.ll_name, NULL);
10146 if (di != NULL)
10147 {
10148 /* Consider a variable locked when:
10149 * 1. the variable itself is locked
10150 * 2. the value of the variable is locked.
10151 * 3. the List or Dict value is locked.
10152 */
10153 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10154 || tv_islocked(&di->di_tv));
10155 }
10156 }
10157 }
10158 else if (lv.ll_range)
10159 EMSG(_("E745: Range not allowed"));
10160 else if (lv.ll_newkey != NULL)
10161 EMSG2(_(e_dictkey), lv.ll_newkey);
10162 else if (lv.ll_list != NULL)
10163 /* List item. */
10164 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10165 else
10166 /* Dictionary item. */
10167 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10168 }
10169 }
10170
10171 clear_lval(&lv);
10172}
10173
Bram Moolenaar33570922005-01-25 22:26:29 +000010174static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010175
10176/*
10177 * Turn a dict into a list:
10178 * "what" == 0: list of keys
10179 * "what" == 1: list of values
10180 * "what" == 2: list of items
10181 */
10182 static void
10183dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010184 typval_T *argvars;
10185 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010186 int what;
10187{
Bram Moolenaar33570922005-01-25 22:26:29 +000010188 list_T *l;
10189 list_T *l2;
10190 dictitem_T *di;
10191 hashitem_T *hi;
10192 listitem_T *li;
10193 listitem_T *li2;
10194 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010195 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010196
10197 rettv->vval.v_number = 0;
10198 if (argvars[0].v_type != VAR_DICT)
10199 {
10200 EMSG(_(e_dictreq));
10201 return;
10202 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010203 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010204 return;
10205
10206 l = list_alloc();
10207 if (l == NULL)
10208 return;
10209 rettv->v_type = VAR_LIST;
10210 rettv->vval.v_list = l;
10211 ++l->lv_refcount;
10212
Bram Moolenaar33570922005-01-25 22:26:29 +000010213 todo = d->dv_hashtab.ht_used;
10214 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010215 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010216 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010217 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010218 --todo;
10219 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010220
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010221 li = listitem_alloc();
10222 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010223 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010224 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010225
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010226 if (what == 0)
10227 {
10228 /* keys() */
10229 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010230 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010231 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10232 }
10233 else if (what == 1)
10234 {
10235 /* values() */
10236 copy_tv(&di->di_tv, &li->li_tv);
10237 }
10238 else
10239 {
10240 /* items() */
10241 l2 = list_alloc();
10242 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010243 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010244 li->li_tv.vval.v_list = l2;
10245 if (l2 == NULL)
10246 break;
10247 ++l2->lv_refcount;
10248
10249 li2 = listitem_alloc();
10250 if (li2 == NULL)
10251 break;
10252 list_append(l2, li2);
10253 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010254 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010255 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
10256
10257 li2 = listitem_alloc();
10258 if (li2 == NULL)
10259 break;
10260 list_append(l2, li2);
10261 copy_tv(&di->di_tv, &li2->li_tv);
10262 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010263 }
10264 }
10265}
10266
10267/*
10268 * "items(dict)" function
10269 */
10270 static void
10271f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010272 typval_T *argvars;
10273 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010274{
10275 dict_list(argvars, rettv, 2);
10276}
10277
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010279 * "join()" function
10280 */
10281 static void
10282f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010283 typval_T *argvars;
10284 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010285{
10286 garray_T ga;
10287 char_u *sep;
10288
10289 rettv->vval.v_number = 0;
10290 if (argvars[0].v_type != VAR_LIST)
10291 {
10292 EMSG(_(e_listreq));
10293 return;
10294 }
10295 if (argvars[0].vval.v_list == NULL)
10296 return;
10297 if (argvars[1].v_type == VAR_UNKNOWN)
10298 sep = (char_u *)" ";
10299 else
10300 sep = get_tv_string(&argvars[1]);
10301
10302 ga_init2(&ga, (int)sizeof(char), 80);
10303 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
10304 ga_append(&ga, NUL);
10305
10306 rettv->v_type = VAR_STRING;
10307 rettv->vval.v_string = (char_u *)ga.ga_data;
10308}
10309
10310/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010311 * "keys()" function
10312 */
10313 static void
10314f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010315 typval_T *argvars;
10316 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010317{
10318 dict_list(argvars, rettv, 0);
10319}
10320
10321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 * "last_buffer_nr()" function.
10323 */
10324/*ARGSUSED*/
10325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010326f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010327 typval_T *argvars;
10328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329{
10330 int n = 0;
10331 buf_T *buf;
10332
10333 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10334 if (n < buf->b_fnum)
10335 n = buf->b_fnum;
10336
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010337 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010338}
10339
10340/*
10341 * "len()" function
10342 */
10343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010344f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010345 typval_T *argvars;
10346 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010347{
10348 switch (argvars[0].v_type)
10349 {
10350 case VAR_STRING:
10351 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010352 rettv->vval.v_number = (varnumber_T)STRLEN(
10353 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010354 break;
10355 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010356 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010357 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010358 case VAR_DICT:
10359 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
10360 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010361 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010362 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010363 break;
10364 }
10365}
10366
Bram Moolenaar33570922005-01-25 22:26:29 +000010367static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010368
10369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010370libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010371 typval_T *argvars;
10372 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010373 int type;
10374{
10375#ifdef FEAT_LIBCALL
10376 char_u *string_in;
10377 char_u **string_result;
10378 int nr_result;
10379#endif
10380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010381 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010382 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010383 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010384 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010385 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010386
10387 if (check_restricted() || check_secure())
10388 return;
10389
10390#ifdef FEAT_LIBCALL
10391 /* The first two args must be strings, otherwise its meaningless */
10392 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
10393 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010394 string_in = NULL;
10395 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010396 string_in = argvars[2].vval.v_string;
10397 if (type == VAR_NUMBER)
10398 string_result = NULL;
10399 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010400 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010401 if (mch_libcall(argvars[0].vval.v_string,
10402 argvars[1].vval.v_string,
10403 string_in,
10404 argvars[2].vval.v_number,
10405 string_result,
10406 &nr_result) == OK
10407 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010408 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010409 }
10410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411}
10412
10413/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010414 * "libcall()" function
10415 */
10416 static void
10417f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010418 typval_T *argvars;
10419 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010420{
10421 libcall_common(argvars, rettv, VAR_STRING);
10422}
10423
10424/*
10425 * "libcallnr()" function
10426 */
10427 static void
10428f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010429 typval_T *argvars;
10430 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010431{
10432 libcall_common(argvars, rettv, VAR_NUMBER);
10433}
10434
10435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 * "line(string)" function
10437 */
10438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010439f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010440 typval_T *argvars;
10441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442{
10443 linenr_T lnum = 0;
10444 pos_T *fp;
10445
10446 fp = var2fpos(&argvars[0], TRUE);
10447 if (fp != NULL)
10448 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010449 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450}
10451
10452/*
10453 * "line2byte(lnum)" function
10454 */
10455/*ARGSUSED*/
10456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010457f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010458 typval_T *argvars;
10459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460{
10461#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010462 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463#else
10464 linenr_T lnum;
10465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010466 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010468 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
10471 if (rettv->vval.v_number >= 0)
10472 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473#endif
10474}
10475
10476/*
10477 * "lispindent(lnum)" function
10478 */
10479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010480f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010481 typval_T *argvars;
10482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483{
10484#ifdef FEAT_LISP
10485 pos_T pos;
10486 linenr_T lnum;
10487
10488 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010489 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10491 {
10492 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010493 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 curwin->w_cursor = pos;
10495 }
10496 else
10497#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010498 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499}
10500
10501/*
10502 * "localtime()" function
10503 */
10504/*ARGSUSED*/
10505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010506f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010507 typval_T *argvars;
10508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010510 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511}
10512
Bram Moolenaar33570922005-01-25 22:26:29 +000010513static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514
10515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010516get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000010517 typval_T *argvars;
10518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 int exact;
10520{
10521 char_u *keys;
10522 char_u *which;
10523 char_u buf[NUMBUFLEN];
10524 char_u *keys_buf = NULL;
10525 char_u *rhs;
10526 int mode;
10527 garray_T ga;
10528
10529 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010530 rettv->v_type = VAR_STRING;
10531 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010533 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 if (*keys == NUL)
10535 return;
10536
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010537 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010538 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 else
10540 which = (char_u *)"";
10541 mode = get_map_mode(&which, 0);
10542
10543 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
10544 rhs = check_map(keys, mode, exact);
10545 vim_free(keys_buf);
10546 if (rhs != NULL)
10547 {
10548 ga_init(&ga);
10549 ga.ga_itemsize = 1;
10550 ga.ga_growsize = 40;
10551
10552 while (*rhs != NUL)
10553 ga_concat(&ga, str2special(&rhs, FALSE));
10554
10555 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 }
10558}
10559
10560/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010561 * "map()" function
10562 */
10563 static void
10564f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010565 typval_T *argvars;
10566 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010567{
10568 filter_map(argvars, rettv, TRUE);
10569}
10570
10571/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010572 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 */
10574 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010575f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010576 typval_T *argvars;
10577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010579 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580}
10581
10582/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010583 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 */
10585 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010586f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010587 typval_T *argvars;
10588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010590 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591}
10592
Bram Moolenaar33570922005-01-25 22:26:29 +000010593static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594
10595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010596find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010597 typval_T *argvars;
10598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 int type;
10600{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010601 char_u *str = NULL;
10602 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 char_u *pat;
10604 regmatch_T regmatch;
10605 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010606 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607 char_u *save_cpo;
10608 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010609 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010610 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010611 list_T *l = NULL;
10612 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010613 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010614 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615
10616 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10617 save_cpo = p_cpo;
10618 p_cpo = (char_u *)"";
10619
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010620 rettv->vval.v_number = -1;
10621 if (type == 3)
10622 {
10623 /* return empty list when there are no matches */
10624 if ((rettv->vval.v_list = list_alloc()) == NULL)
10625 goto theend;
10626 rettv->v_type = VAR_LIST;
10627 ++rettv->vval.v_list->lv_refcount;
10628 }
10629 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010631 rettv->v_type = VAR_STRING;
10632 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010635 if (argvars[0].v_type == VAR_LIST)
10636 {
10637 if ((l = argvars[0].vval.v_list) == NULL)
10638 goto theend;
10639 li = l->lv_first;
10640 }
10641 else
10642 expr = str = get_tv_string(&argvars[0]);
10643
10644 pat = get_tv_string_buf(&argvars[1], patbuf);
10645
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010646 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010648 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010649 if (l != NULL)
10650 {
10651 li = list_find(l, start);
10652 if (li == NULL)
10653 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000010654 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010655 }
10656 else
10657 {
10658 if (start < 0)
10659 start = 0;
10660 if (start > (long)STRLEN(str))
10661 goto theend;
10662 str += start;
10663 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010664
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010665 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010666 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 }
10668
10669 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10670 if (regmatch.regprog != NULL)
10671 {
10672 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010673
10674 while (1)
10675 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010676 if (l != NULL)
10677 {
10678 if (li == NULL)
10679 {
10680 match = FALSE;
10681 break;
10682 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010683 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010684 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010685 if (str == NULL)
10686 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010687 }
10688
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010689 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010690
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010691 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010692 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010693 if (l == NULL && !match)
10694 break;
10695
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010696 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010697 if (l != NULL)
10698 {
10699 li = li->li_next;
10700 ++idx;
10701 }
10702 else
10703 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010704#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010705 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010706#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010707 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010708#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010709 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010710 }
10711
10712 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010714 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010715 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010716 int i;
10717
10718 /* return list with matched string and submatches */
10719 for (i = 0; i < NSUBEXP; ++i)
10720 {
10721 if (regmatch.endp[i] == NULL)
10722 break;
10723 li = listitem_alloc();
10724 if (li == NULL)
10725 break;
10726 li->li_tv.v_type = VAR_STRING;
10727 li->li_tv.v_lock = 0;
10728 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
10729 (int)(regmatch.endp[i] - regmatch.startp[i]));
10730 list_append(rettv->vval.v_list, li);
10731 }
10732 }
10733 else if (type == 2)
10734 {
10735 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010736 if (l != NULL)
10737 copy_tv(&li->li_tv, rettv);
10738 else
10739 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010741 }
10742 else if (l != NULL)
10743 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 else
10745 {
10746 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010747 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 (varnumber_T)(regmatch.startp[0] - str);
10749 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010750 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 }
10754 }
10755 vim_free(regmatch.regprog);
10756 }
10757
10758theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010759 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 p_cpo = save_cpo;
10761}
10762
10763/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010764 * "match()" function
10765 */
10766 static void
10767f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010768 typval_T *argvars;
10769 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010770{
10771 find_some_match(argvars, rettv, 1);
10772}
10773
10774/*
10775 * "matchend()" function
10776 */
10777 static void
10778f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010779 typval_T *argvars;
10780 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010781{
10782 find_some_match(argvars, rettv, 0);
10783}
10784
10785/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010786 * "matchlist()" function
10787 */
10788 static void
10789f_matchlist(argvars, rettv)
10790 typval_T *argvars;
10791 typval_T *rettv;
10792{
10793 find_some_match(argvars, rettv, 3);
10794}
10795
10796/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010797 * "matchstr()" function
10798 */
10799 static void
10800f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010801 typval_T *argvars;
10802 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010803{
10804 find_some_match(argvars, rettv, 2);
10805}
10806
Bram Moolenaar33570922005-01-25 22:26:29 +000010807static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010808
10809 static void
10810max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000010811 typval_T *argvars;
10812 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010813 int domax;
10814{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010815 long n = 0;
10816 long i;
10817
10818 if (argvars[0].v_type == VAR_LIST)
10819 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010820 list_T *l;
10821 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010822
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010823 l = argvars[0].vval.v_list;
10824 if (l != NULL)
10825 {
10826 li = l->lv_first;
10827 if (li != NULL)
10828 {
10829 n = get_tv_number(&li->li_tv);
10830 while (1)
10831 {
10832 li = li->li_next;
10833 if (li == NULL)
10834 break;
10835 i = get_tv_number(&li->li_tv);
10836 if (domax ? i > n : i < n)
10837 n = i;
10838 }
10839 }
10840 }
10841 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010842 else if (argvars[0].v_type == VAR_DICT)
10843 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010844 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010845 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000010846 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010847 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010848
10849 d = argvars[0].vval.v_dict;
10850 if (d != NULL)
10851 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010852 todo = d->dv_hashtab.ht_used;
10853 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010854 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010855 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010856 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010857 --todo;
10858 i = get_tv_number(&HI2DI(hi)->di_tv);
10859 if (first)
10860 {
10861 n = i;
10862 first = FALSE;
10863 }
10864 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010865 n = i;
10866 }
10867 }
10868 }
10869 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010870 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000010871 EMSG(_(e_listdictarg));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010872 rettv->vval.v_number = n;
10873}
10874
10875/*
10876 * "max()" function
10877 */
10878 static void
10879f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010880 typval_T *argvars;
10881 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010882{
10883 max_min(argvars, rettv, TRUE);
10884}
10885
10886/*
10887 * "min()" function
10888 */
10889 static void
10890f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010891 typval_T *argvars;
10892 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010893{
10894 max_min(argvars, rettv, FALSE);
10895}
10896
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010897static int mkdir_recurse __ARGS((char_u *dir, int prot));
10898
10899/*
10900 * Create the directory in which "dir" is located, and higher levels when
10901 * needed.
10902 */
10903 static int
10904mkdir_recurse(dir, prot)
10905 char_u *dir;
10906 int prot;
10907{
10908 char_u *p;
10909 char_u *updir;
10910 int r = FAIL;
10911
10912 /* Get end of directory name in "dir".
10913 * We're done when it's "/" or "c:/". */
10914 p = gettail_sep(dir);
10915 if (p <= get_past_head(dir))
10916 return OK;
10917
10918 /* If the directory exists we're done. Otherwise: create it.*/
10919 updir = vim_strnsave(dir, (int)(p - dir));
10920 if (updir == NULL)
10921 return FAIL;
10922 if (mch_isdir(updir))
10923 r = OK;
10924 else if (mkdir_recurse(updir, prot) == OK)
10925 r = vim_mkdir_emsg(updir, prot);
10926 vim_free(updir);
10927 return r;
10928}
10929
10930#ifdef vim_mkdir
10931/*
10932 * "mkdir()" function
10933 */
10934 static void
10935f_mkdir(argvars, rettv)
10936 typval_T *argvars;
10937 typval_T *rettv;
10938{
10939 char_u *dir;
10940 char_u buf[NUMBUFLEN];
10941 int prot = 0755;
10942
10943 rettv->vval.v_number = FAIL;
10944 if (check_restricted() || check_secure())
10945 return;
10946
10947 dir = get_tv_string_buf(&argvars[0], buf);
10948 if (argvars[1].v_type != VAR_UNKNOWN)
10949 {
10950 if (argvars[2].v_type != VAR_UNKNOWN)
10951 prot = get_tv_number(&argvars[2]);
10952 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
10953 mkdir_recurse(dir, prot);
10954 }
10955 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
10956}
10957#endif
10958
Bram Moolenaar0d660222005-01-07 21:51:51 +000010959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 * "mode()" function
10961 */
10962/*ARGSUSED*/
10963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010965 typval_T *argvars;
10966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967{
10968 char_u buf[2];
10969
10970#ifdef FEAT_VISUAL
10971 if (VIsual_active)
10972 {
10973 if (VIsual_select)
10974 buf[0] = VIsual_mode + 's' - 'v';
10975 else
10976 buf[0] = VIsual_mode;
10977 }
10978 else
10979#endif
10980 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
10981 buf[0] = 'r';
10982 else if (State & INSERT)
10983 {
10984 if (State & REPLACE_FLAG)
10985 buf[0] = 'R';
10986 else
10987 buf[0] = 'i';
10988 }
10989 else if (State & CMDLINE)
10990 buf[0] = 'c';
10991 else
10992 buf[0] = 'n';
10993
10994 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010995 rettv->vval.v_string = vim_strsave(buf);
10996 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997}
10998
10999/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011000 * "nextnonblank()" function
11001 */
11002 static void
11003f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011004 typval_T *argvars;
11005 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011006{
11007 linenr_T lnum;
11008
11009 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11010 {
11011 if (lnum > curbuf->b_ml.ml_line_count)
11012 {
11013 lnum = 0;
11014 break;
11015 }
11016 if (*skipwhite(ml_get(lnum)) != NUL)
11017 break;
11018 }
11019 rettv->vval.v_number = lnum;
11020}
11021
11022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 * "nr2char()" function
11024 */
11025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011026f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011027 typval_T *argvars;
11028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029{
11030 char_u buf[NUMBUFLEN];
11031
11032#ifdef FEAT_MBYTE
11033 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011034 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 else
11036#endif
11037 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011038 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 buf[1] = NUL;
11040 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041 rettv->v_type = VAR_STRING;
11042 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011043}
11044
11045/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011046 * "prevnonblank()" function
11047 */
11048 static void
11049f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011050 typval_T *argvars;
11051 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011052{
11053 linenr_T lnum;
11054
11055 lnum = get_tv_lnum(argvars);
11056 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11057 lnum = 0;
11058 else
11059 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11060 --lnum;
11061 rettv->vval.v_number = lnum;
11062}
11063
Bram Moolenaar8c711452005-01-14 21:53:12 +000011064/*
11065 * "range()" function
11066 */
11067 static void
11068f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011069 typval_T *argvars;
11070 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011071{
11072 long start;
11073 long end;
11074 long stride = 1;
11075 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011076 list_T *l;
11077 listitem_T *li;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011078
11079 start = get_tv_number(&argvars[0]);
11080 if (argvars[1].v_type == VAR_UNKNOWN)
11081 {
11082 end = start - 1;
11083 start = 0;
11084 }
11085 else
11086 {
11087 end = get_tv_number(&argvars[1]);
11088 if (argvars[2].v_type != VAR_UNKNOWN)
11089 stride = get_tv_number(&argvars[2]);
11090 }
11091
11092 rettv->vval.v_number = 0;
11093 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011094 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011095 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011096 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011097 else
11098 {
11099 l = list_alloc();
11100 if (l != NULL)
11101 {
11102 rettv->v_type = VAR_LIST;
11103 rettv->vval.v_list = l;
11104 ++l->lv_refcount;
11105
11106 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11107 {
11108 li = listitem_alloc();
11109 if (li == NULL)
11110 break;
11111 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011112 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011113 li->li_tv.vval.v_number = i;
11114 list_append(l, li);
11115 }
11116 }
11117 }
11118}
11119
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011120/*
11121 * "readfile()" function
11122 */
11123 static void
11124f_readfile(argvars, rettv)
11125 typval_T *argvars;
11126 typval_T *rettv;
11127{
11128 int binary = FALSE;
11129 char_u *fname;
11130 FILE *fd;
11131 list_T *l;
11132 listitem_T *li;
11133#define FREAD_SIZE 200 /* optimized for text lines */
11134 char_u buf[FREAD_SIZE];
11135 int readlen; /* size of last fread() */
11136 int buflen; /* nr of valid chars in buf[] */
11137 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11138 int tolist; /* first byte in buf[] still to be put in list */
11139 int chop; /* how many CR to chop off */
11140 char_u *prev = NULL; /* previously read bytes, if any */
11141 int prevlen = 0; /* length of "prev" if not NULL */
11142 char_u *s;
11143 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011144 long maxline = MAXLNUM;
11145 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011146
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011147 if (argvars[1].v_type != VAR_UNKNOWN)
11148 {
11149 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11150 binary = TRUE;
11151 if (argvars[2].v_type != VAR_UNKNOWN)
11152 maxline = get_tv_number(&argvars[2]);
11153 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011154
11155 l = list_alloc();
11156 if (l == NULL)
11157 return;
11158 rettv->v_type = VAR_LIST;
11159 rettv->vval.v_list = l;
11160 l->lv_refcount = 1;
11161
11162 /* Always open the file in binary mode, library functions have a mind of
11163 * their own about CR-LF conversion. */
11164 fname = get_tv_string(&argvars[0]);
11165 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11166 {
11167 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11168 return;
11169 }
11170
11171 filtd = 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011172 while (cnt < maxline)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011173 {
11174 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11175 buflen = filtd + readlen;
11176 tolist = 0;
11177 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11178 {
11179 if (buf[filtd] == '\n' || readlen <= 0)
11180 {
11181 /* Only when in binary mode add an empty list item when the
11182 * last line ends in a '\n'. */
11183 if (!binary && readlen == 0 && filtd == 0)
11184 break;
11185
11186 /* Found end-of-line or end-of-file: add a text line to the
11187 * list. */
11188 chop = 0;
11189 if (!binary)
11190 while (filtd - chop - 1 >= tolist
11191 && buf[filtd - chop - 1] == '\r')
11192 ++chop;
11193 len = filtd - tolist - chop;
11194 if (prev == NULL)
11195 s = vim_strnsave(buf + tolist, len);
11196 else
11197 {
11198 s = alloc((unsigned)(prevlen + len + 1));
11199 if (s != NULL)
11200 {
11201 mch_memmove(s, prev, prevlen);
11202 vim_free(prev);
11203 prev = NULL;
11204 mch_memmove(s + prevlen, buf + tolist, len);
11205 s[prevlen + len] = NUL;
11206 }
11207 }
11208 tolist = filtd + 1;
11209
11210 li = listitem_alloc();
11211 if (li == NULL)
11212 {
11213 vim_free(s);
11214 break;
11215 }
11216 li->li_tv.v_type = VAR_STRING;
11217 li->li_tv.v_lock = 0;
11218 li->li_tv.vval.v_string = s;
11219 list_append(l, li);
11220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011221 if (++cnt >= maxline)
11222 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011223 if (readlen <= 0)
11224 break;
11225 }
11226 else if (buf[filtd] == NUL)
11227 buf[filtd] = '\n';
11228 }
11229 if (readlen <= 0)
11230 break;
11231
11232 if (tolist == 0)
11233 {
11234 /* "buf" is full, need to move text to an allocated buffer */
11235 if (prev == NULL)
11236 {
11237 prev = vim_strnsave(buf, buflen);
11238 prevlen = buflen;
11239 }
11240 else
11241 {
11242 s = alloc((unsigned)(prevlen + buflen));
11243 if (s != NULL)
11244 {
11245 mch_memmove(s, prev, prevlen);
11246 mch_memmove(s + prevlen, buf, buflen);
11247 vim_free(prev);
11248 prev = s;
11249 prevlen += buflen;
11250 }
11251 }
11252 filtd = 0;
11253 }
11254 else
11255 {
11256 mch_memmove(buf, buf + tolist, buflen - tolist);
11257 filtd -= tolist;
11258 }
11259 }
11260
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011261 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011262 fclose(fd);
11263}
11264
11265
Bram Moolenaar0d660222005-01-07 21:51:51 +000011266#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
11267static void make_connection __ARGS((void));
11268static int check_connection __ARGS((void));
11269
11270 static void
11271make_connection()
11272{
11273 if (X_DISPLAY == NULL
11274# ifdef FEAT_GUI
11275 && !gui.in_use
11276# endif
11277 )
11278 {
11279 x_force_connect = TRUE;
11280 setup_term_clip();
11281 x_force_connect = FALSE;
11282 }
11283}
11284
11285 static int
11286check_connection()
11287{
11288 make_connection();
11289 if (X_DISPLAY == NULL)
11290 {
11291 EMSG(_("E240: No connection to Vim server"));
11292 return FAIL;
11293 }
11294 return OK;
11295}
11296#endif
11297
11298#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011299static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011300
11301 static void
11302remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000011303 typval_T *argvars;
11304 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011305 int expr;
11306{
11307 char_u *server_name;
11308 char_u *keys;
11309 char_u *r = NULL;
11310 char_u buf[NUMBUFLEN];
11311# ifdef WIN32
11312 HWND w;
11313# else
11314 Window w;
11315# endif
11316
11317 if (check_restricted() || check_secure())
11318 return;
11319
11320# ifdef FEAT_X11
11321 if (check_connection() == FAIL)
11322 return;
11323# endif
11324
11325 server_name = get_tv_string(&argvars[0]);
11326 keys = get_tv_string_buf(&argvars[1], buf);
11327# ifdef WIN32
11328 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
11329# else
11330 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
11331 < 0)
11332# endif
11333 {
11334 if (r != NULL)
11335 EMSG(r); /* sending worked but evaluation failed */
11336 else
11337 EMSG2(_("E241: Unable to send to %s"), server_name);
11338 return;
11339 }
11340
11341 rettv->vval.v_string = r;
11342
11343 if (argvars[2].v_type != VAR_UNKNOWN)
11344 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011345 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011346 char_u str[30];
11347
11348 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000011349 v.di_tv.v_type = VAR_STRING;
11350 v.di_tv.vval.v_string = vim_strsave(str);
11351 set_var(get_tv_string(&argvars[2]), &v.di_tv, FALSE);
11352 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011353 }
11354}
11355#endif
11356
11357/*
11358 * "remote_expr()" function
11359 */
11360/*ARGSUSED*/
11361 static void
11362f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011363 typval_T *argvars;
11364 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011365{
11366 rettv->v_type = VAR_STRING;
11367 rettv->vval.v_string = NULL;
11368#ifdef FEAT_CLIENTSERVER
11369 remote_common(argvars, rettv, TRUE);
11370#endif
11371}
11372
11373/*
11374 * "remote_foreground()" function
11375 */
11376/*ARGSUSED*/
11377 static void
11378f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011379 typval_T *argvars;
11380 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011381{
11382 rettv->vval.v_number = 0;
11383#ifdef FEAT_CLIENTSERVER
11384# ifdef WIN32
11385 /* On Win32 it's done in this application. */
11386 serverForeground(get_tv_string(&argvars[0]));
11387# else
11388 /* Send a foreground() expression to the server. */
11389 argvars[1].v_type = VAR_STRING;
11390 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
11391 argvars[2].v_type = VAR_UNKNOWN;
11392 remote_common(argvars, rettv, TRUE);
11393 vim_free(argvars[1].vval.v_string);
11394# endif
11395#endif
11396}
11397
11398/*ARGSUSED*/
11399 static void
11400f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011401 typval_T *argvars;
11402 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011403{
11404#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011405 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011406 char_u *s = NULL;
11407# ifdef WIN32
11408 int n = 0;
11409# endif
11410
11411 if (check_restricted() || check_secure())
11412 {
11413 rettv->vval.v_number = -1;
11414 return;
11415 }
11416# ifdef WIN32
11417 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11418 if (n == 0)
11419 rettv->vval.v_number = -1;
11420 else
11421 {
11422 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
11423 rettv->vval.v_number = (s != NULL);
11424 }
11425# else
11426 rettv->vval.v_number = 0;
11427 if (check_connection() == FAIL)
11428 return;
11429
11430 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
11431 serverStrToWin(get_tv_string(&argvars[0])), &s);
11432# endif
11433
11434 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
11435 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011436 v.di_tv.v_type = VAR_STRING;
11437 v.di_tv.vval.v_string = vim_strsave(s);
11438 set_var(get_tv_string(&argvars[1]), &v.di_tv, FALSE);
11439 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011440 }
11441#else
11442 rettv->vval.v_number = -1;
11443#endif
11444}
11445
11446/*ARGSUSED*/
11447 static void
11448f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011449 typval_T *argvars;
11450 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011451{
11452 char_u *r = NULL;
11453
11454#ifdef FEAT_CLIENTSERVER
11455 if (!check_restricted() && !check_secure())
11456 {
11457# ifdef WIN32
11458 /* The server's HWND is encoded in the 'id' parameter */
11459 int n = 0;
11460
11461 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11462 if (n != 0)
11463 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
11464 if (r == NULL)
11465# else
11466 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
11467 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
11468# endif
11469 EMSG(_("E277: Unable to read a server reply"));
11470 }
11471#endif
11472 rettv->v_type = VAR_STRING;
11473 rettv->vval.v_string = r;
11474}
11475
11476/*
11477 * "remote_send()" function
11478 */
11479/*ARGSUSED*/
11480 static void
11481f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011482 typval_T *argvars;
11483 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011484{
11485 rettv->v_type = VAR_STRING;
11486 rettv->vval.v_string = NULL;
11487#ifdef FEAT_CLIENTSERVER
11488 remote_common(argvars, rettv, FALSE);
11489#endif
11490}
11491
11492/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011493 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011494 */
11495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011496f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011497 typval_T *argvars;
11498 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011499{
Bram Moolenaar33570922005-01-25 22:26:29 +000011500 list_T *l;
11501 listitem_T *item, *item2;
11502 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011503 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011504 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011505 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000011506 dict_T *d;
11507 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011508
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011509 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011510 if (argvars[0].v_type == VAR_DICT)
11511 {
11512 if (argvars[2].v_type != VAR_UNKNOWN)
11513 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011514 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000011515 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011516 {
11517 key = get_tv_string(&argvars[1]);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011518 di = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011519 if (di == NULL)
11520 EMSG2(_(e_dictkey), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011521 else
11522 {
11523 *rettv = di->di_tv;
11524 init_tv(&di->di_tv);
11525 dictitem_remove(d, di);
11526 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011527 }
11528 }
11529 else if (argvars[0].v_type != VAR_LIST)
11530 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011531 else if ((l = argvars[0].vval.v_list) != NULL
11532 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011534 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011535 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011536 if (item == NULL)
11537 EMSGN(_(e_listidx), idx);
11538 else
11539 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011540 if (argvars[2].v_type == VAR_UNKNOWN)
11541 {
11542 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011543 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011544 *rettv = item->li_tv;
11545 vim_free(item);
11546 }
11547 else
11548 {
11549 /* Remove range of items, return list with values. */
11550 end = get_tv_number(&argvars[2]);
11551 item2 = list_find(l, end);
11552 if (item2 == NULL)
11553 EMSGN(_(e_listidx), end);
11554 else
11555 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011556 int cnt = 0;
11557
11558 for (li = item; li != NULL; li = li->li_next)
11559 {
11560 ++cnt;
11561 if (li == item2)
11562 break;
11563 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011564 if (li == NULL) /* didn't find "item2" after "item" */
11565 EMSG(_(e_invrange));
11566 else
11567 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011568 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011569 l = list_alloc();
11570 if (l != NULL)
11571 {
11572 rettv->v_type = VAR_LIST;
11573 rettv->vval.v_list = l;
11574 l->lv_first = item;
11575 l->lv_last = item2;
11576 l->lv_refcount = 1;
11577 item->li_prev = NULL;
11578 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011579 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011580 }
11581 }
11582 }
11583 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011584 }
11585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586}
11587
11588/*
11589 * "rename({from}, {to})" function
11590 */
11591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011592f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011593 typval_T *argvars;
11594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595{
11596 char_u buf[NUMBUFLEN];
11597
11598 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011599 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011601 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
11602 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603}
11604
11605/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011606 * "repeat()" function
11607 */
11608/*ARGSUSED*/
11609 static void
11610f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011611 typval_T *argvars;
11612 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011613{
11614 char_u *p;
11615 int n;
11616 int slen;
11617 int len;
11618 char_u *r;
11619 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011620 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011621
11622 n = get_tv_number(&argvars[1]);
11623 if (argvars[0].v_type == VAR_LIST)
11624 {
11625 l = list_alloc();
11626 if (l != NULL && argvars[0].vval.v_list != NULL)
11627 {
11628 l->lv_refcount = 1;
11629 while (n-- > 0)
11630 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
11631 break;
11632 }
11633 rettv->v_type = VAR_LIST;
11634 rettv->vval.v_list = l;
11635 }
11636 else
11637 {
11638 p = get_tv_string(&argvars[0]);
11639 rettv->v_type = VAR_STRING;
11640 rettv->vval.v_string = NULL;
11641
11642 slen = (int)STRLEN(p);
11643 len = slen * n;
11644 if (len <= 0)
11645 return;
11646
11647 r = alloc(len + 1);
11648 if (r != NULL)
11649 {
11650 for (i = 0; i < n; i++)
11651 mch_memmove(r + i * slen, p, (size_t)slen);
11652 r[len] = NUL;
11653 }
11654
11655 rettv->vval.v_string = r;
11656 }
11657}
11658
11659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 * "resolve()" function
11661 */
11662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011663f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011664 typval_T *argvars;
11665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666{
11667 char_u *p;
11668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011669 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670#ifdef FEAT_SHORTCUT
11671 {
11672 char_u *v = NULL;
11673
11674 v = mch_resolve_shortcut(p);
11675 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011676 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011678 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679 }
11680#else
11681# ifdef HAVE_READLINK
11682 {
11683 char_u buf[MAXPATHL + 1];
11684 char_u *cpy;
11685 int len;
11686 char_u *remain = NULL;
11687 char_u *q;
11688 int is_relative_to_current = FALSE;
11689 int has_trailing_pathsep = FALSE;
11690 int limit = 100;
11691
11692 p = vim_strsave(p);
11693
11694 if (p[0] == '.' && (vim_ispathsep(p[1])
11695 || (p[1] == '.' && (vim_ispathsep(p[2])))))
11696 is_relative_to_current = TRUE;
11697
11698 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011699 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 has_trailing_pathsep = TRUE;
11701
11702 q = getnextcomp(p);
11703 if (*q != NUL)
11704 {
11705 /* Separate the first path component in "p", and keep the
11706 * remainder (beginning with the path separator). */
11707 remain = vim_strsave(q - 1);
11708 q[-1] = NUL;
11709 }
11710
11711 for (;;)
11712 {
11713 for (;;)
11714 {
11715 len = readlink((char *)p, (char *)buf, MAXPATHL);
11716 if (len <= 0)
11717 break;
11718 buf[len] = NUL;
11719
11720 if (limit-- == 0)
11721 {
11722 vim_free(p);
11723 vim_free(remain);
11724 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011725 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726 goto fail;
11727 }
11728
11729 /* Ensure that the result will have a trailing path separator
11730 * if the argument has one. */
11731 if (remain == NULL && has_trailing_pathsep)
11732 add_pathsep(buf);
11733
11734 /* Separate the first path component in the link value and
11735 * concatenate the remainders. */
11736 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
11737 if (*q != NUL)
11738 {
11739 if (remain == NULL)
11740 remain = vim_strsave(q - 1);
11741 else
11742 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011743 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 if (cpy != NULL)
11745 {
11746 STRCAT(cpy, remain);
11747 vim_free(remain);
11748 remain = cpy;
11749 }
11750 }
11751 q[-1] = NUL;
11752 }
11753
11754 q = gettail(p);
11755 if (q > p && *q == NUL)
11756 {
11757 /* Ignore trailing path separator. */
11758 q[-1] = NUL;
11759 q = gettail(p);
11760 }
11761 if (q > p && !mch_isFullName(buf))
11762 {
11763 /* symlink is relative to directory of argument */
11764 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
11765 if (cpy != NULL)
11766 {
11767 STRCPY(cpy, p);
11768 STRCPY(gettail(cpy), buf);
11769 vim_free(p);
11770 p = cpy;
11771 }
11772 }
11773 else
11774 {
11775 vim_free(p);
11776 p = vim_strsave(buf);
11777 }
11778 }
11779
11780 if (remain == NULL)
11781 break;
11782
11783 /* Append the first path component of "remain" to "p". */
11784 q = getnextcomp(remain + 1);
11785 len = q - remain - (*q != NUL);
11786 cpy = vim_strnsave(p, STRLEN(p) + len);
11787 if (cpy != NULL)
11788 {
11789 STRNCAT(cpy, remain, len);
11790 vim_free(p);
11791 p = cpy;
11792 }
11793 /* Shorten "remain". */
11794 if (*q != NUL)
11795 STRCPY(remain, q - 1);
11796 else
11797 {
11798 vim_free(remain);
11799 remain = NULL;
11800 }
11801 }
11802
11803 /* If the result is a relative path name, make it explicitly relative to
11804 * the current directory if and only if the argument had this form. */
11805 if (!vim_ispathsep(*p))
11806 {
11807 if (is_relative_to_current
11808 && *p != NUL
11809 && !(p[0] == '.'
11810 && (p[1] == NUL
11811 || vim_ispathsep(p[1])
11812 || (p[1] == '.'
11813 && (p[2] == NUL
11814 || vim_ispathsep(p[2]))))))
11815 {
11816 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011817 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818 if (cpy != NULL)
11819 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820 vim_free(p);
11821 p = cpy;
11822 }
11823 }
11824 else if (!is_relative_to_current)
11825 {
11826 /* Strip leading "./". */
11827 q = p;
11828 while (q[0] == '.' && vim_ispathsep(q[1]))
11829 q += 2;
11830 if (q > p)
11831 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
11832 }
11833 }
11834
11835 /* Ensure that the result will have no trailing path separator
11836 * if the argument had none. But keep "/" or "//". */
11837 if (!has_trailing_pathsep)
11838 {
11839 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011840 if (after_pathsep(p, q))
11841 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 }
11843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011844 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 }
11846# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011847 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848# endif
11849#endif
11850
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011851 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852
11853#ifdef HAVE_READLINK
11854fail:
11855#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011856 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857}
11858
11859/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011860 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861 */
11862 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011863f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011864 typval_T *argvars;
11865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866{
Bram Moolenaar33570922005-01-25 22:26:29 +000011867 list_T *l;
11868 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869
Bram Moolenaar0d660222005-01-07 21:51:51 +000011870 rettv->vval.v_number = 0;
11871 if (argvars[0].v_type != VAR_LIST)
11872 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011873 else if ((l = argvars[0].vval.v_list) != NULL
11874 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000011875 {
11876 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011877 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011878 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011879 while (li != NULL)
11880 {
11881 ni = li->li_prev;
11882 list_append(l, li);
11883 li = ni;
11884 }
11885 rettv->vval.v_list = l;
11886 rettv->v_type = VAR_LIST;
11887 ++l->lv_refcount;
11888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889}
11890
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011891#define SP_NOMOVE 1 /* don't move cursor */
11892#define SP_REPEAT 2 /* repeat to find outer pair */
11893#define SP_RETCOUNT 4 /* return matchcount */
11894
Bram Moolenaar33570922005-01-25 22:26:29 +000011895static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011896
11897/*
11898 * Get flags for a search function.
11899 * Possibly sets "p_ws".
11900 * Returns BACKWARD, FORWARD or zero (for an error).
11901 */
11902 static int
11903get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011904 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011905 int *flagsp;
11906{
11907 int dir = FORWARD;
11908 char_u *flags;
11909 char_u nbuf[NUMBUFLEN];
11910 int mask;
11911
11912 if (varp->v_type != VAR_UNKNOWN)
11913 {
11914 flags = get_tv_string_buf(varp, nbuf);
11915 while (*flags != NUL)
11916 {
11917 switch (*flags)
11918 {
11919 case 'b': dir = BACKWARD; break;
11920 case 'w': p_ws = TRUE; break;
11921 case 'W': p_ws = FALSE; break;
11922 default: mask = 0;
11923 if (flagsp != NULL)
11924 switch (*flags)
11925 {
11926 case 'n': mask = SP_NOMOVE; break;
11927 case 'r': mask = SP_REPEAT; break;
11928 case 'm': mask = SP_RETCOUNT; break;
11929 }
11930 if (mask == 0)
11931 {
11932 EMSG2(_(e_invarg2), flags);
11933 dir = 0;
11934 }
11935 else
11936 *flagsp |= mask;
11937 }
11938 if (dir == 0)
11939 break;
11940 ++flags;
11941 }
11942 }
11943 return dir;
11944}
11945
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946/*
11947 * "search()" function
11948 */
11949 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011950f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011951 typval_T *argvars;
11952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953{
11954 char_u *pat;
11955 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011956 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957 int save_p_ws = p_ws;
11958 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011959 int flags = 0;
11960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011961 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011963 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011964 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
11965 if (dir == 0)
11966 goto theend;
11967 if ((flags & ~SP_NOMOVE) != 0)
11968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011969 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011970 goto theend;
11971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011973 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
11975 SEARCH_KEEP, RE_SEARCH) != FAIL)
11976 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011977 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978 curwin->w_cursor = pos;
11979 /* "/$" will put the cursor after the end of the line, may need to
11980 * correct that here */
11981 check_cursor();
11982 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011983
11984 /* If 'n' flag is used: restore cursor position. */
11985 if (flags & SP_NOMOVE)
11986 curwin->w_cursor = save_cursor;
11987theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 p_ws = save_p_ws;
11989}
11990
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991/*
11992 * "searchpair()" function
11993 */
11994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011995f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011996 typval_T *argvars;
11997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998{
11999 char_u *spat, *mpat, *epat;
12000 char_u *skip;
12001 char_u *pat, *pat2, *pat3;
12002 pos_T pos;
12003 pos_T firstpos;
12004 pos_T save_cursor;
12005 pos_T save_pos;
12006 int save_p_ws = p_ws;
12007 char_u *save_cpo;
12008 int dir;
12009 int flags = 0;
12010 char_u nbuf1[NUMBUFLEN];
12011 char_u nbuf2[NUMBUFLEN];
12012 char_u nbuf3[NUMBUFLEN];
12013 int n;
12014 int r;
12015 int nest = 1;
12016 int err;
12017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012018 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019
12020 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12021 save_cpo = p_cpo;
12022 p_cpo = (char_u *)"";
12023
12024 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012025 spat = get_tv_string(&argvars[0]);
12026 mpat = get_tv_string_buf(&argvars[1], nbuf1);
12027 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028
12029 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12030 * start/middle/end (pat3, for the top pair). */
12031 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12032 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12033 if (pat2 == NULL || pat3 == NULL)
12034 goto theend;
12035 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12036 if (*mpat == NUL)
12037 STRCPY(pat3, pat2);
12038 else
12039 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12040 spat, epat, mpat);
12041
12042 /* Handle the optional fourth argument: flags */
12043 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012044 if (dir == 0)
12045 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046
12047 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012048 if (argvars[3].v_type == VAR_UNKNOWN
12049 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 skip = (char_u *)"";
12051 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053
12054 save_cursor = curwin->w_cursor;
12055 pos = curwin->w_cursor;
12056 firstpos.lnum = 0;
12057 pat = pat3;
12058 for (;;)
12059 {
12060 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12061 SEARCH_KEEP, RE_SEARCH);
12062 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12063 /* didn't find it or found the first match again: FAIL */
12064 break;
12065
12066 if (firstpos.lnum == 0)
12067 firstpos = pos;
12068
12069 /* If the skip pattern matches, ignore this match. */
12070 if (*skip != NUL)
12071 {
12072 save_pos = curwin->w_cursor;
12073 curwin->w_cursor = pos;
12074 r = eval_to_bool(skip, &err, NULL, FALSE);
12075 curwin->w_cursor = save_pos;
12076 if (err)
12077 {
12078 /* Evaluating {skip} caused an error, break here. */
12079 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012080 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081 break;
12082 }
12083 if (r)
12084 continue;
12085 }
12086
12087 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12088 {
12089 /* Found end when searching backwards or start when searching
12090 * forward: nested pair. */
12091 ++nest;
12092 pat = pat2; /* nested, don't search for middle */
12093 }
12094 else
12095 {
12096 /* Found end when searching forward or start when searching
12097 * backward: end of (nested) pair; or found middle in outer pair. */
12098 if (--nest == 1)
12099 pat = pat3; /* outer level, search for middle */
12100 }
12101
12102 if (nest == 0)
12103 {
12104 /* Found the match: return matchcount or line number. */
12105 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012106 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012108 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109 curwin->w_cursor = pos;
12110 if (!(flags & SP_REPEAT))
12111 break;
12112 nest = 1; /* search for next unmatched */
12113 }
12114 }
12115
12116 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012117 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 curwin->w_cursor = save_cursor;
12119
12120theend:
12121 vim_free(pat2);
12122 vim_free(pat3);
12123 p_ws = save_p_ws;
12124 p_cpo = save_cpo;
12125}
12126
Bram Moolenaar0d660222005-01-07 21:51:51 +000012127/*ARGSUSED*/
12128 static void
12129f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012130 typval_T *argvars;
12131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012133#ifdef FEAT_CLIENTSERVER
12134 char_u buf[NUMBUFLEN];
12135 char_u *server = get_tv_string(&argvars[0]);
12136 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137
Bram Moolenaar0d660222005-01-07 21:51:51 +000012138 rettv->vval.v_number = -1;
12139 if (check_restricted() || check_secure())
12140 return;
12141# ifdef FEAT_X11
12142 if (check_connection() == FAIL)
12143 return;
12144# endif
12145
12146 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012148 EMSG(_("E258: Unable to send to client"));
12149 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012151 rettv->vval.v_number = 0;
12152#else
12153 rettv->vval.v_number = -1;
12154#endif
12155}
12156
12157/*ARGSUSED*/
12158 static void
12159f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012160 typval_T *argvars;
12161 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012162{
12163 char_u *r = NULL;
12164
12165#ifdef FEAT_CLIENTSERVER
12166# ifdef WIN32
12167 r = serverGetVimNames();
12168# else
12169 make_connection();
12170 if (X_DISPLAY != NULL)
12171 r = serverGetVimNames(X_DISPLAY);
12172# endif
12173#endif
12174 rettv->v_type = VAR_STRING;
12175 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176}
12177
12178/*
12179 * "setbufvar()" function
12180 */
12181/*ARGSUSED*/
12182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012183f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012184 typval_T *argvars;
12185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186{
12187 buf_T *buf;
12188#ifdef FEAT_AUTOCMD
12189 aco_save_T aco;
12190#else
12191 buf_T *save_curbuf;
12192#endif
12193 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012194 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195 char_u nbuf[NUMBUFLEN];
12196
12197 if (check_restricted() || check_secure())
12198 return;
12199 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012200 buf = get_buf_tv(&argvars[0]);
12201 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202 varp = &argvars[2];
12203
12204 if (buf != NULL && varname != NULL && varp != NULL)
12205 {
12206 /* set curbuf to be our buf, temporarily */
12207#ifdef FEAT_AUTOCMD
12208 aucmd_prepbuf(&aco, buf);
12209#else
12210 save_curbuf = curbuf;
12211 curbuf = buf;
12212#endif
12213
12214 if (*varname == '&')
12215 {
12216 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012217 set_option_value(varname, get_tv_number(varp),
12218 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219 }
12220 else
12221 {
12222 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
12223 if (bufvarname != NULL)
12224 {
12225 STRCPY(bufvarname, "b:");
12226 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012227 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228 vim_free(bufvarname);
12229 }
12230 }
12231
12232 /* reset notion of buffer */
12233#ifdef FEAT_AUTOCMD
12234 aucmd_restbuf(&aco);
12235#else
12236 curbuf = save_curbuf;
12237#endif
12238 }
12239 --emsg_off;
12240}
12241
12242/*
12243 * "setcmdpos()" function
12244 */
12245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012246f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012247 typval_T *argvars;
12248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012250 rettv->vval.v_number = set_cmdline_pos(
12251 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252}
12253
12254/*
12255 * "setline()" function
12256 */
12257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012259 typval_T *argvars;
12260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261{
12262 linenr_T lnum;
12263 char_u *line;
12264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012265 lnum = get_tv_lnum(argvars);
12266 line = get_tv_string(&argvars[1]);
12267 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268
12269 if (lnum >= 1
12270 && lnum <= curbuf->b_ml.ml_line_count
12271 && u_savesub(lnum) == OK
12272 && ml_replace(lnum, line, TRUE) == OK)
12273 {
12274 changed_bytes(lnum, 0);
12275 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012276 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277 }
12278}
12279
12280/*
12281 * "setreg()" function
12282 */
12283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012284f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012285 typval_T *argvars;
12286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287{
12288 int regname;
12289 char_u *strregname;
12290 char_u *stropt;
12291 int append;
12292 char_u yank_type;
12293 long block_len;
12294
12295 block_len = -1;
12296 yank_type = MAUTO;
12297 append = FALSE;
12298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012299 strregname = get_tv_string(argvars);
12300 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301
12302 regname = (strregname == NULL ? '"' : *strregname);
12303 if (regname == 0 || regname == '@')
12304 regname = '"';
12305 else if (regname == '=')
12306 return;
12307
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012308 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012310 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311 switch (*stropt)
12312 {
12313 case 'a': case 'A': /* append */
12314 append = TRUE;
12315 break;
12316 case 'v': case 'c': /* character-wise selection */
12317 yank_type = MCHAR;
12318 break;
12319 case 'V': case 'l': /* line-wise selection */
12320 yank_type = MLINE;
12321 break;
12322#ifdef FEAT_VISUAL
12323 case 'b': case Ctrl_V: /* block-wise selection */
12324 yank_type = MBLOCK;
12325 if (VIM_ISDIGIT(stropt[1]))
12326 {
12327 ++stropt;
12328 block_len = getdigits(&stropt) - 1;
12329 --stropt;
12330 }
12331 break;
12332#endif
12333 }
12334 }
12335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012336 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339}
12340
12341
12342/*
12343 * "setwinvar(expr)" function
12344 */
12345/*ARGSUSED*/
12346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012347f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012348 typval_T *argvars;
12349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350{
12351 win_T *win;
12352#ifdef FEAT_WINDOWS
12353 win_T *save_curwin;
12354#endif
12355 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012356 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357 char_u nbuf[NUMBUFLEN];
12358
12359 if (check_restricted() || check_secure())
12360 return;
12361 ++emsg_off;
12362 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012363 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364 varp = &argvars[2];
12365
12366 if (win != NULL && varname != NULL && varp != NULL)
12367 {
12368#ifdef FEAT_WINDOWS
12369 /* set curwin to be our win, temporarily */
12370 save_curwin = curwin;
12371 curwin = win;
12372 curbuf = curwin->w_buffer;
12373#endif
12374
12375 if (*varname == '&')
12376 {
12377 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012378 set_option_value(varname, get_tv_number(varp),
12379 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380 }
12381 else
12382 {
12383 winvarname = alloc((unsigned)STRLEN(varname) + 3);
12384 if (winvarname != NULL)
12385 {
12386 STRCPY(winvarname, "w:");
12387 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012388 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 vim_free(winvarname);
12390 }
12391 }
12392
12393#ifdef FEAT_WINDOWS
12394 /* Restore current window, if it's still valid (autocomands can make
12395 * it invalid). */
12396 if (win_valid(save_curwin))
12397 {
12398 curwin = save_curwin;
12399 curbuf = curwin->w_buffer;
12400 }
12401#endif
12402 }
12403 --emsg_off;
12404}
12405
12406/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012407 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 */
12409 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012410f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012411 typval_T *argvars;
12412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012414 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415
Bram Moolenaar0d660222005-01-07 21:51:51 +000012416 p = get_tv_string(&argvars[0]);
12417 rettv->vval.v_string = vim_strsave(p);
12418 simplify_filename(rettv->vval.v_string); /* simplify in place */
12419 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420}
12421
Bram Moolenaar0d660222005-01-07 21:51:51 +000012422static int
12423#ifdef __BORLANDC__
12424 _RTLENTRYF
12425#endif
12426 item_compare __ARGS((const void *s1, const void *s2));
12427static int
12428#ifdef __BORLANDC__
12429 _RTLENTRYF
12430#endif
12431 item_compare2 __ARGS((const void *s1, const void *s2));
12432
12433static int item_compare_ic;
12434static char_u *item_compare_func;
12435#define ITEM_COMPARE_FAIL 999
12436
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012438 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012440 static int
12441#ifdef __BORLANDC__
12442_RTLENTRYF
12443#endif
12444item_compare(s1, s2)
12445 const void *s1;
12446 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012448 char_u *p1, *p2;
12449 char_u *tofree1, *tofree2;
12450 int res;
12451 char_u numbuf1[NUMBUFLEN];
12452 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453
Bram Moolenaar33570922005-01-25 22:26:29 +000012454 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
12455 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012456 if (item_compare_ic)
12457 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012459 res = STRCMP(p1, p2);
12460 vim_free(tofree1);
12461 vim_free(tofree2);
12462 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463}
12464
12465 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000012466#ifdef __BORLANDC__
12467_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000012469item_compare2(s1, s2)
12470 const void *s1;
12471 const void *s2;
12472{
12473 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000012474 typval_T rettv;
12475 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000012476 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012478 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
12479 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012480 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
12481 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012482
12483 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12484 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000012485 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012486 clear_tv(&argv[0]);
12487 clear_tv(&argv[1]);
12488
12489 if (res == FAIL)
12490 res = ITEM_COMPARE_FAIL;
12491 else
12492 res = get_tv_number(&rettv);
12493 clear_tv(&rettv);
12494 return res;
12495}
12496
12497/*
12498 * "sort({list})" function
12499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012501f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012502 typval_T *argvars;
12503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504{
Bram Moolenaar33570922005-01-25 22:26:29 +000012505 list_T *l;
12506 listitem_T *li;
12507 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012508 long len;
12509 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510
Bram Moolenaar0d660222005-01-07 21:51:51 +000012511 rettv->vval.v_number = 0;
12512 if (argvars[0].v_type != VAR_LIST)
12513 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514 else
12515 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012516 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012517 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012518 return;
12519 rettv->vval.v_list = l;
12520 rettv->v_type = VAR_LIST;
12521 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522
Bram Moolenaar0d660222005-01-07 21:51:51 +000012523 len = list_len(l);
12524 if (len <= 1)
12525 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526
Bram Moolenaar0d660222005-01-07 21:51:51 +000012527 item_compare_ic = FALSE;
12528 item_compare_func = NULL;
12529 if (argvars[1].v_type != VAR_UNKNOWN)
12530 {
12531 if (argvars[1].v_type == VAR_FUNC)
12532 item_compare_func = argvars[0].vval.v_string;
12533 else
12534 {
12535 i = get_tv_number(&argvars[1]);
12536 if (i == 1)
12537 item_compare_ic = TRUE;
12538 else
12539 item_compare_func = get_tv_string(&argvars[1]);
12540 }
12541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542
Bram Moolenaar0d660222005-01-07 21:51:51 +000012543 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012544 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012545 if (ptrs == NULL)
12546 return;
12547 i = 0;
12548 for (li = l->lv_first; li != NULL; li = li->li_next)
12549 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550
Bram Moolenaar0d660222005-01-07 21:51:51 +000012551 /* test the compare function */
12552 if (item_compare_func != NULL
12553 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12554 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012555 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012557 {
12558 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012559 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000012560 item_compare_func == NULL ? item_compare : item_compare2);
12561
12562 /* Clear the List and append the items in the sorted order. */
12563 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012564 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012565 for (i = 0; i < len; ++i)
12566 list_append(l, ptrs[i]);
12567 }
12568
12569 vim_free(ptrs);
12570 }
12571}
12572
12573 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012574f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012575 typval_T *argvars;
12576 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012577{
12578 char_u *str;
12579 char_u *end;
12580 char_u *pat;
12581 regmatch_T regmatch;
12582 char_u patbuf[NUMBUFLEN];
12583 char_u *save_cpo;
12584 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000012585 listitem_T *ni;
12586 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012587 colnr_T col = 0;
12588
12589 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12590 save_cpo = p_cpo;
12591 p_cpo = (char_u *)"";
12592
12593 str = get_tv_string(&argvars[0]);
12594 if (argvars[1].v_type == VAR_UNKNOWN)
12595 pat = (char_u *)"[\\x01- ]\\+";
12596 else
12597 pat = get_tv_string_buf(&argvars[1], patbuf);
12598
12599 l = list_alloc();
12600 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012601 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012602 rettv->v_type = VAR_LIST;
12603 rettv->vval.v_list = l;
12604 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605
Bram Moolenaar0d660222005-01-07 21:51:51 +000012606 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12607 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012609 regmatch.rm_ic = FALSE;
12610 while (*str != NUL)
12611 {
12612 match = vim_regexec_nl(&regmatch, str, col);
12613 if (match)
12614 end = regmatch.startp[0];
12615 else
12616 end = str + STRLEN(str);
12617 if (end > str)
12618 {
12619 ni = listitem_alloc();
12620 if (ni == NULL)
12621 break;
12622 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012623 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012624 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
12625 list_append(l, ni);
12626 }
12627 if (!match)
12628 break;
12629 /* Advance to just after the match. */
12630 if (regmatch.endp[0] > str)
12631 col = 0;
12632 else
12633 {
12634 /* Don't get stuck at the same match. */
12635#ifdef FEAT_MBYTE
12636 col = mb_ptr2len_check(regmatch.endp[0]);
12637#else
12638 col = 1;
12639#endif
12640 }
12641 str = regmatch.endp[0];
12642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643
Bram Moolenaar0d660222005-01-07 21:51:51 +000012644 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646
Bram Moolenaar0d660222005-01-07 21:51:51 +000012647 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648}
12649
12650#ifdef HAVE_STRFTIME
12651/*
12652 * "strftime({format}[, {time}])" function
12653 */
12654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012655f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012656 typval_T *argvars;
12657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658{
12659 char_u result_buf[256];
12660 struct tm *curtime;
12661 time_t seconds;
12662 char_u *p;
12663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012664 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012666 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012667 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668 seconds = time(NULL);
12669 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012670 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671 curtime = localtime(&seconds);
12672 /* MSVC returns NULL for an invalid value of seconds. */
12673 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012674 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675 else
12676 {
12677# ifdef FEAT_MBYTE
12678 vimconv_T conv;
12679 char_u *enc;
12680
12681 conv.vc_type = CONV_NONE;
12682 enc = enc_locale();
12683 convert_setup(&conv, p_enc, enc);
12684 if (conv.vc_type != CONV_NONE)
12685 p = string_convert(&conv, p, NULL);
12686# endif
12687 if (p != NULL)
12688 (void)strftime((char *)result_buf, sizeof(result_buf),
12689 (char *)p, curtime);
12690 else
12691 result_buf[0] = NUL;
12692
12693# ifdef FEAT_MBYTE
12694 if (conv.vc_type != CONV_NONE)
12695 vim_free(p);
12696 convert_setup(&conv, enc, p_enc);
12697 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012698 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699 else
12700# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012701 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702
12703# ifdef FEAT_MBYTE
12704 /* Release conversion descriptors */
12705 convert_setup(&conv, NULL, NULL);
12706 vim_free(enc);
12707# endif
12708 }
12709}
12710#endif
12711
12712/*
12713 * "stridx()" function
12714 */
12715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012716f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012717 typval_T *argvars;
12718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719{
12720 char_u buf[NUMBUFLEN];
12721 char_u *needle;
12722 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000012723 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000012725 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012727 needle = get_tv_string(&argvars[1]);
Bram Moolenaar33570922005-01-25 22:26:29 +000012728 save_haystack = haystack = get_tv_string_buf(&argvars[0], buf);
12729 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730
Bram Moolenaar33570922005-01-25 22:26:29 +000012731 if (argvars[2].v_type != VAR_UNKNOWN)
12732 {
12733 start_idx = get_tv_number(&argvars[2]);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012734 if (start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000012735 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012736 if (start_idx >= 0)
12737 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000012738 }
12739
12740 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12741 if (pos != NULL)
12742 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743}
12744
12745/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012746 * "string()" function
12747 */
12748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012749f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012750 typval_T *argvars;
12751 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012752{
12753 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012754 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012756 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012757 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012758 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012759 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760}
12761
12762/*
12763 * "strlen()" function
12764 */
12765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012766f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012767 typval_T *argvars;
12768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770 rettv->vval.v_number = (varnumber_T)(STRLEN(
12771 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772}
12773
12774/*
12775 * "strpart()" function
12776 */
12777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012778f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012779 typval_T *argvars;
12780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781{
12782 char_u *p;
12783 int n;
12784 int len;
12785 int slen;
12786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012787 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788 slen = (int)STRLEN(p);
12789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012790 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012791 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012792 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793 else
12794 len = slen - n; /* default len: all bytes that are available. */
12795
12796 /*
12797 * Only return the overlap between the specified part and the actual
12798 * string.
12799 */
12800 if (n < 0)
12801 {
12802 len += n;
12803 n = 0;
12804 }
12805 else if (n > slen)
12806 n = slen;
12807 if (len < 0)
12808 len = 0;
12809 else if (n + len > slen)
12810 len = slen - n;
12811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012812 rettv->v_type = VAR_STRING;
12813 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814}
12815
12816/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012817 * "strridx()" function
12818 */
12819 static void
12820f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012821 typval_T *argvars;
12822 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012823{
12824 char_u buf[NUMBUFLEN];
12825 char_u *needle;
12826 char_u *haystack;
12827 char_u *rest;
12828 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012829 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012830
12831 needle = get_tv_string(&argvars[1]);
12832 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012833 haystack_len = STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000012834 if (argvars[2].v_type != VAR_UNKNOWN)
12835 {
12836 /* Third argument: upper limit for index */
12837 end_idx = get_tv_number(&argvars[2]);
12838 if (end_idx < 0)
12839 {
12840 /* can never find a match */
12841 rettv->vval.v_number = -1;
12842 return;
12843 }
12844 }
12845 else
12846 end_idx = haystack_len;
12847
Bram Moolenaar0d660222005-01-07 21:51:51 +000012848 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000012849 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012850 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000012851 lastmatch = haystack + end_idx;
12852 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012853 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000012854 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012855 for (rest = haystack; *rest != '\0'; ++rest)
12856 {
12857 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012858 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012859 break;
12860 lastmatch = rest;
12861 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000012862 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012863
12864 if (lastmatch == NULL)
12865 rettv->vval.v_number = -1;
12866 else
12867 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12868}
12869
12870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871 * "strtrans()" function
12872 */
12873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012874f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012875 typval_T *argvars;
12876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012878 rettv->v_type = VAR_STRING;
12879 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880}
12881
12882/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012883 * "submatch()" function
12884 */
12885 static void
12886f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012887 typval_T *argvars;
12888 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012889{
12890 rettv->v_type = VAR_STRING;
12891 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
12892}
12893
12894/*
12895 * "substitute()" function
12896 */
12897 static void
12898f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012899 typval_T *argvars;
12900 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012901{
12902 char_u patbuf[NUMBUFLEN];
12903 char_u subbuf[NUMBUFLEN];
12904 char_u flagsbuf[NUMBUFLEN];
12905
12906 rettv->v_type = VAR_STRING;
12907 rettv->vval.v_string = do_string_sub(
12908 get_tv_string(&argvars[0]),
12909 get_tv_string_buf(&argvars[1], patbuf),
12910 get_tv_string_buf(&argvars[2], subbuf),
12911 get_tv_string_buf(&argvars[3], flagsbuf));
12912}
12913
12914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915 * "synID(line, col, trans)" function
12916 */
12917/*ARGSUSED*/
12918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012919f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012920 typval_T *argvars;
12921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922{
12923 int id = 0;
12924#ifdef FEAT_SYN_HL
12925 long line;
12926 long col;
12927 int trans;
12928
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012929 line = get_tv_lnum(argvars);
12930 col = get_tv_number(&argvars[1]) - 1;
12931 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932
12933 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
12934 && col >= 0 && col < (long)STRLEN(ml_get(line)))
12935 id = syn_get_id(line, col, trans);
12936#endif
12937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012938 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939}
12940
12941/*
12942 * "synIDattr(id, what [, mode])" function
12943 */
12944/*ARGSUSED*/
12945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012947 typval_T *argvars;
12948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949{
12950 char_u *p = NULL;
12951#ifdef FEAT_SYN_HL
12952 int id;
12953 char_u *what;
12954 char_u *mode;
12955 char_u modebuf[NUMBUFLEN];
12956 int modec;
12957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012958 id = get_tv_number(&argvars[0]);
12959 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012960 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012962 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963 modec = TOLOWER_ASC(mode[0]);
12964 if (modec != 't' && modec != 'c'
12965#ifdef FEAT_GUI
12966 && modec != 'g'
12967#endif
12968 )
12969 modec = 0; /* replace invalid with current */
12970 }
12971 else
12972 {
12973#ifdef FEAT_GUI
12974 if (gui.in_use)
12975 modec = 'g';
12976 else
12977#endif
12978 if (t_colors > 1)
12979 modec = 'c';
12980 else
12981 modec = 't';
12982 }
12983
12984
12985 switch (TOLOWER_ASC(what[0]))
12986 {
12987 case 'b':
12988 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12989 p = highlight_color(id, what, modec);
12990 else /* bold */
12991 p = highlight_has_attr(id, HL_BOLD, modec);
12992 break;
12993
12994 case 'f': /* fg[#] */
12995 p = highlight_color(id, what, modec);
12996 break;
12997
12998 case 'i':
12999 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13000 p = highlight_has_attr(id, HL_INVERSE, modec);
13001 else /* italic */
13002 p = highlight_has_attr(id, HL_ITALIC, modec);
13003 break;
13004
13005 case 'n': /* name */
13006 p = get_highlight_name(NULL, id - 1);
13007 break;
13008
13009 case 'r': /* reverse */
13010 p = highlight_has_attr(id, HL_INVERSE, modec);
13011 break;
13012
13013 case 's': /* standout */
13014 p = highlight_has_attr(id, HL_STANDOUT, modec);
13015 break;
13016
13017 case 'u': /* underline */
13018 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13019 break;
13020 }
13021
13022 if (p != NULL)
13023 p = vim_strsave(p);
13024#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013025 rettv->v_type = VAR_STRING;
13026 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027}
13028
13029/*
13030 * "synIDtrans(id)" function
13031 */
13032/*ARGSUSED*/
13033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013034f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013035 typval_T *argvars;
13036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037{
13038 int id;
13039
13040#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013041 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042
13043 if (id > 0)
13044 id = syn_get_final_id(id);
13045 else
13046#endif
13047 id = 0;
13048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013049 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050}
13051
13052/*
13053 * "system()" function
13054 */
13055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013056f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013057 typval_T *argvars;
13058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013060 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013062 char_u *infile = NULL;
13063 char_u buf[NUMBUFLEN];
13064 int err = FALSE;
13065 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013067 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013068 {
13069 /*
13070 * Write the string to a temp file, to be used for input of the shell
13071 * command.
13072 */
13073 if ((infile = vim_tempname('i')) == NULL)
13074 {
13075 EMSG(_(e_notmp));
13076 return;
13077 }
13078
13079 fd = mch_fopen((char *)infile, WRITEBIN);
13080 if (fd == NULL)
13081 {
13082 EMSG2(_(e_notopen), infile);
13083 goto done;
13084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013085 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013086 if (fwrite(p, STRLEN(p), 1, fd) != 1)
13087 err = TRUE;
13088 if (fclose(fd) != 0)
13089 err = TRUE;
13090 if (err)
13091 {
13092 EMSG(_("E677: Error writing temp file"));
13093 goto done;
13094 }
13095 }
13096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013097 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013098
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099#ifdef USE_CR
13100 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013101 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102 {
13103 char_u *s;
13104
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013105 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106 {
13107 if (*s == CAR)
13108 *s = NL;
13109 }
13110 }
13111#else
13112# ifdef USE_CRNL
13113 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013114 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115 {
13116 char_u *s, *d;
13117
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013118 d = res;
13119 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120 {
13121 if (s[0] == CAR && s[1] == NL)
13122 ++s;
13123 *d++ = *s;
13124 }
13125 *d = NUL;
13126 }
13127# endif
13128#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013129
13130done:
13131 if (infile != NULL)
13132 {
13133 mch_remove(infile);
13134 vim_free(infile);
13135 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013136 rettv->v_type = VAR_STRING;
13137 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138}
13139
13140/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000013141 * "gettags()" function
13142 */
13143 static void
13144f_taglist(argvars, rettv)
13145 typval_T *argvars;
13146 typval_T *rettv;
13147{
13148 char_u *tag_pattern;
13149 list_T *l;
13150
13151 tag_pattern = get_tv_string(&argvars[0]);
13152
13153 rettv->vval.v_number = FALSE;
13154
13155 l = list_alloc();
13156 if (l != NULL)
13157 {
13158 if (get_tags(l, tag_pattern) != FAIL)
13159 {
13160 rettv->vval.v_list = l;
13161 rettv->v_type = VAR_LIST;
13162 ++l->lv_refcount;
13163 }
13164 else
13165 list_free(l);
13166 }
13167}
13168
13169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170 * "tempname()" function
13171 */
13172/*ARGSUSED*/
13173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013174f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013175 typval_T *argvars;
13176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177{
13178 static int x = 'A';
13179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013180 rettv->v_type = VAR_STRING;
13181 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182
13183 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13184 * names. Skip 'I' and 'O', they are used for shell redirection. */
13185 do
13186 {
13187 if (x == 'Z')
13188 x = '0';
13189 else if (x == '9')
13190 x = 'A';
13191 else
13192 {
13193#ifdef EBCDIC
13194 if (x == 'I')
13195 x = 'J';
13196 else if (x == 'R')
13197 x = 'S';
13198 else
13199#endif
13200 ++x;
13201 }
13202 } while (x == 'I' || x == 'O');
13203}
13204
13205/*
13206 * "tolower(string)" function
13207 */
13208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013209f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013210 typval_T *argvars;
13211 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212{
13213 char_u *p;
13214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013215 p = vim_strsave(get_tv_string(&argvars[0]));
13216 rettv->v_type = VAR_STRING;
13217 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013218
13219 if (p != NULL)
13220 while (*p != NUL)
13221 {
13222#ifdef FEAT_MBYTE
13223 int l;
13224
13225 if (enc_utf8)
13226 {
13227 int c, lc;
13228
13229 c = utf_ptr2char(p);
13230 lc = utf_tolower(c);
13231 l = utf_ptr2len_check(p);
13232 /* TODO: reallocate string when byte count changes. */
13233 if (utf_char2len(lc) == l)
13234 utf_char2bytes(lc, p);
13235 p += l;
13236 }
13237 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13238 p += l; /* skip multi-byte character */
13239 else
13240#endif
13241 {
13242 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
13243 ++p;
13244 }
13245 }
13246}
13247
13248/*
13249 * "toupper(string)" function
13250 */
13251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013252f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013253 typval_T *argvars;
13254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255{
13256 char_u *p;
13257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013258 p = vim_strsave(get_tv_string(&argvars[0]));
13259 rettv->v_type = VAR_STRING;
13260 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261
13262 if (p != NULL)
13263 while (*p != NUL)
13264 {
13265#ifdef FEAT_MBYTE
13266 int l;
13267
13268 if (enc_utf8)
13269 {
13270 int c, uc;
13271
13272 c = utf_ptr2char(p);
13273 uc = utf_toupper(c);
13274 l = utf_ptr2len_check(p);
13275 /* TODO: reallocate string when byte count changes. */
13276 if (utf_char2len(uc) == l)
13277 utf_char2bytes(uc, p);
13278 p += l;
13279 }
13280 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13281 p += l; /* skip multi-byte character */
13282 else
13283#endif
13284 {
13285 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
13286 p++;
13287 }
13288 }
13289}
13290
13291/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000013292 * "tr(string, fromstr, tostr)" function
13293 */
13294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013295f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013296 typval_T *argvars;
13297 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013298{
13299 char_u *instr;
13300 char_u *fromstr;
13301 char_u *tostr;
13302 char_u *p;
13303#ifdef FEAT_MBYTE
13304 int inlen;
13305 int fromlen;
13306 int tolen;
13307 int idx;
13308 char_u *cpstr;
13309 int cplen;
13310 int first = TRUE;
13311#endif
13312 char_u buf[NUMBUFLEN];
13313 char_u buf2[NUMBUFLEN];
13314 garray_T ga;
13315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013316 instr = get_tv_string(&argvars[0]);
13317 fromstr = get_tv_string_buf(&argvars[1], buf);
13318 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013319
13320 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013321 rettv->v_type = VAR_STRING;
13322 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013323 ga_init2(&ga, (int)sizeof(char), 80);
13324
13325#ifdef FEAT_MBYTE
13326 if (!has_mbyte)
13327#endif
13328 /* not multi-byte: fromstr and tostr must be the same length */
13329 if (STRLEN(fromstr) != STRLEN(tostr))
13330 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013331#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000013332error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013333#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000013334 EMSG2(_(e_invarg2), fromstr);
13335 ga_clear(&ga);
13336 return;
13337 }
13338
13339 /* fromstr and tostr have to contain the same number of chars */
13340 while (*instr != NUL)
13341 {
13342#ifdef FEAT_MBYTE
13343 if (has_mbyte)
13344 {
13345 inlen = mb_ptr2len_check(instr);
13346 cpstr = instr;
13347 cplen = inlen;
13348 idx = 0;
13349 for (p = fromstr; *p != NUL; p += fromlen)
13350 {
13351 fromlen = mb_ptr2len_check(p);
13352 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
13353 {
13354 for (p = tostr; *p != NUL; p += tolen)
13355 {
13356 tolen = mb_ptr2len_check(p);
13357 if (idx-- == 0)
13358 {
13359 cplen = tolen;
13360 cpstr = p;
13361 break;
13362 }
13363 }
13364 if (*p == NUL) /* tostr is shorter than fromstr */
13365 goto error;
13366 break;
13367 }
13368 ++idx;
13369 }
13370
13371 if (first && cpstr == instr)
13372 {
13373 /* Check that fromstr and tostr have the same number of
13374 * (multi-byte) characters. Done only once when a character
13375 * of instr doesn't appear in fromstr. */
13376 first = FALSE;
13377 for (p = tostr; *p != NUL; p += tolen)
13378 {
13379 tolen = mb_ptr2len_check(p);
13380 --idx;
13381 }
13382 if (idx != 0)
13383 goto error;
13384 }
13385
13386 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000013387 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013388 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013389
13390 instr += inlen;
13391 }
13392 else
13393#endif
13394 {
13395 /* When not using multi-byte chars we can do it faster. */
13396 p = vim_strchr(fromstr, *instr);
13397 if (p != NULL)
13398 ga_append(&ga, tostr[p - fromstr]);
13399 else
13400 ga_append(&ga, *instr);
13401 ++instr;
13402 }
13403 }
13404
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013405 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013406}
13407
13408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 * "type(expr)" function
13410 */
13411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013412f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013413 typval_T *argvars;
13414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013416 int n;
13417
13418 switch (argvars[0].v_type)
13419 {
13420 case VAR_NUMBER: n = 0; break;
13421 case VAR_STRING: n = 1; break;
13422 case VAR_FUNC: n = 2; break;
13423 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013424 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013425 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
13426 }
13427 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428}
13429
13430/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013431 * "values(dict)" function
13432 */
13433 static void
13434f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013435 typval_T *argvars;
13436 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013437{
13438 dict_list(argvars, rettv, 1);
13439}
13440
13441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 * "virtcol(string)" function
13443 */
13444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013445f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013446 typval_T *argvars;
13447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448{
13449 colnr_T vcol = 0;
13450 pos_T *fp;
13451
13452 fp = var2fpos(&argvars[0], FALSE);
13453 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
13454 {
13455 getvvcol(curwin, fp, NULL, NULL, &vcol);
13456 ++vcol;
13457 }
13458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013459 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460}
13461
13462/*
13463 * "visualmode()" function
13464 */
13465/*ARGSUSED*/
13466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013467f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013468 typval_T *argvars;
13469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470{
13471#ifdef FEAT_VISUAL
13472 char_u str[2];
13473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013474 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475 str[0] = curbuf->b_visual_mode_eval;
13476 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013477 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478
13479 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013480 if ((argvars[0].v_type == VAR_NUMBER
13481 && argvars[0].vval.v_number != 0)
13482 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013483 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484 curbuf->b_visual_mode_eval = NUL;
13485#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013486 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487#endif
13488}
13489
13490/*
13491 * "winbufnr(nr)" function
13492 */
13493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013494f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013495 typval_T *argvars;
13496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497{
13498 win_T *wp;
13499
13500 wp = find_win_by_nr(&argvars[0]);
13501 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013502 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013504 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505}
13506
13507/*
13508 * "wincol()" function
13509 */
13510/*ARGSUSED*/
13511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013512f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013513 typval_T *argvars;
13514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515{
13516 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013517 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518}
13519
13520/*
13521 * "winheight(nr)" function
13522 */
13523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013524f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013525 typval_T *argvars;
13526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527{
13528 win_T *wp;
13529
13530 wp = find_win_by_nr(&argvars[0]);
13531 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013532 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013534 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535}
13536
13537/*
13538 * "winline()" function
13539 */
13540/*ARGSUSED*/
13541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013542f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013543 typval_T *argvars;
13544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545{
13546 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013547 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548}
13549
13550/*
13551 * "winnr()" function
13552 */
13553/* ARGSUSED */
13554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013555f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013556 typval_T *argvars;
13557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558{
13559 int nr = 1;
13560#ifdef FEAT_WINDOWS
13561 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013562 win_T *twin = curwin;
13563 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013565 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013567 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013568 if (STRCMP(arg, "$") == 0)
13569 twin = lastwin;
13570 else if (STRCMP(arg, "#") == 0)
13571 {
13572 twin = prevwin;
13573 if (prevwin == NULL)
13574 nr = 0;
13575 }
13576 else
13577 {
13578 EMSG2(_(e_invexpr2), arg);
13579 nr = 0;
13580 }
13581 }
13582
13583 if (nr > 0)
13584 for (wp = firstwin; wp != twin; wp = wp->w_next)
13585 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013587 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588}
13589
13590/*
13591 * "winrestcmd()" function
13592 */
13593/* ARGSUSED */
13594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013595f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013596 typval_T *argvars;
13597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598{
13599#ifdef FEAT_WINDOWS
13600 win_T *wp;
13601 int winnr = 1;
13602 garray_T ga;
13603 char_u buf[50];
13604
13605 ga_init2(&ga, (int)sizeof(char), 70);
13606 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13607 {
13608 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13609 ga_concat(&ga, buf);
13610# ifdef FEAT_VERTSPLIT
13611 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13612 ga_concat(&ga, buf);
13613# endif
13614 ++winnr;
13615 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000013616 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013618 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013622 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623}
13624
13625/*
13626 * "winwidth(nr)" function
13627 */
13628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013629f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013630 typval_T *argvars;
13631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632{
13633 win_T *wp;
13634
13635 wp = find_win_by_nr(&argvars[0]);
13636 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 else
13639#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013642 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643#endif
13644}
13645
13646 static win_T *
13647find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013648 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649{
13650#ifdef FEAT_WINDOWS
13651 win_T *wp;
13652#endif
13653 int nr;
13654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013655 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656
13657#ifdef FEAT_WINDOWS
13658 if (nr == 0)
13659 return curwin;
13660
13661 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13662 if (--nr <= 0)
13663 break;
13664 return wp;
13665#else
13666 if (nr == 0 || nr == 1)
13667 return curwin;
13668 return NULL;
13669#endif
13670}
13671
13672/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013673 * "writefile()" function
13674 */
13675 static void
13676f_writefile(argvars, rettv)
13677 typval_T *argvars;
13678 typval_T *rettv;
13679{
13680 int binary = FALSE;
13681 char_u *fname;
13682 FILE *fd;
13683 listitem_T *li;
13684 char_u *s;
13685 int ret = 0;
13686 int c;
13687
13688 if (argvars[0].v_type != VAR_LIST)
13689 {
13690 EMSG2(_(e_listarg), "writefile()");
13691 return;
13692 }
13693 if (argvars[0].vval.v_list == NULL)
13694 return;
13695
13696 if (argvars[2].v_type != VAR_UNKNOWN
13697 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
13698 binary = TRUE;
13699
13700 /* Always open the file in binary mode, library functions have a mind of
13701 * their own about CR-LF conversion. */
13702 fname = get_tv_string(&argvars[1]);
13703 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
13704 {
13705 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13706 ret = -1;
13707 }
13708 else
13709 {
13710 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
13711 li = li->li_next)
13712 {
13713 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
13714 {
13715 if (*s == '\n')
13716 c = putc(NUL, fd);
13717 else
13718 c = putc(*s, fd);
13719 if (c == EOF)
13720 {
13721 ret = -1;
13722 break;
13723 }
13724 }
13725 if (!binary || li->li_next != NULL)
13726 if (putc('\n', fd) == EOF)
13727 {
13728 ret = -1;
13729 break;
13730 }
13731 if (ret < 0)
13732 {
13733 EMSG(_(e_write));
13734 break;
13735 }
13736 }
13737 fclose(fd);
13738 }
13739
13740 rettv->vval.v_number = ret;
13741}
13742
13743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744 * Translate a String variable into a position.
13745 */
13746 static pos_T *
13747var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000013748 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 int lnum; /* TRUE when $ is last line */
13750{
13751 char_u *name;
13752 static pos_T pos;
13753 pos_T *pp;
13754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013755 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 if (name[0] == '.') /* cursor */
13757 return &curwin->w_cursor;
13758 if (name[0] == '\'') /* mark */
13759 {
13760 pp = getmark(name[1], FALSE);
13761 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
13762 return NULL;
13763 return pp;
13764 }
13765 if (name[0] == '$') /* last column or line */
13766 {
13767 if (lnum)
13768 {
13769 pos.lnum = curbuf->b_ml.ml_line_count;
13770 pos.col = 0;
13771 }
13772 else
13773 {
13774 pos.lnum = curwin->w_cursor.lnum;
13775 pos.col = (colnr_T)STRLEN(ml_get_curline());
13776 }
13777 return &pos;
13778 }
13779 return NULL;
13780}
13781
13782/*
13783 * Get the length of an environment variable name.
13784 * Advance "arg" to the first character after the name.
13785 * Return 0 for error.
13786 */
13787 static int
13788get_env_len(arg)
13789 char_u **arg;
13790{
13791 char_u *p;
13792 int len;
13793
13794 for (p = *arg; vim_isIDc(*p); ++p)
13795 ;
13796 if (p == *arg) /* no name found */
13797 return 0;
13798
13799 len = (int)(p - *arg);
13800 *arg = p;
13801 return len;
13802}
13803
13804/*
13805 * Get the length of the name of a function or internal variable.
13806 * "arg" is advanced to the first non-white character after the name.
13807 * Return 0 if something is wrong.
13808 */
13809 static int
13810get_id_len(arg)
13811 char_u **arg;
13812{
13813 char_u *p;
13814 int len;
13815
13816 /* Find the end of the name. */
13817 for (p = *arg; eval_isnamec(*p); ++p)
13818 ;
13819 if (p == *arg) /* no name found */
13820 return 0;
13821
13822 len = (int)(p - *arg);
13823 *arg = skipwhite(p);
13824
13825 return len;
13826}
13827
13828/*
Bram Moolenaara7043832005-01-21 11:56:39 +000013829 * Get the length of the name of a variable or function.
13830 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013832 * Return -1 if curly braces expansion failed.
13833 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 * If the name contains 'magic' {}'s, expand them and return the
13835 * expanded name in an allocated string via 'alias' - caller must free.
13836 */
13837 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013838get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839 char_u **arg;
13840 char_u **alias;
13841 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013842 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843{
13844 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845 char_u *p;
13846 char_u *expr_start;
13847 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848
13849 *alias = NULL; /* default to no alias */
13850
13851 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
13852 && (*arg)[2] == (int)KE_SNR)
13853 {
13854 /* hard coded <SNR>, already translated */
13855 *arg += 3;
13856 return get_id_len(arg) + 3;
13857 }
13858 len = eval_fname_script(*arg);
13859 if (len > 0)
13860 {
13861 /* literal "<SID>", "s:" or "<SNR>" */
13862 *arg += len;
13863 }
13864
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013866 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013868 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869 if (expr_start != NULL)
13870 {
13871 char_u *temp_string;
13872
13873 if (!evaluate)
13874 {
13875 len += (int)(p - *arg);
13876 *arg = skipwhite(p);
13877 return len;
13878 }
13879
13880 /*
13881 * Include any <SID> etc in the expanded string:
13882 * Thus the -len here.
13883 */
13884 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
13885 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013886 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887 *alias = temp_string;
13888 *arg = skipwhite(p);
13889 return (int)STRLEN(temp_string);
13890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891
13892 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013893 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894 EMSG2(_(e_invexpr2), *arg);
13895
13896 return len;
13897}
13898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013899/*
13900 * Find the end of a variable or function name, taking care of magic braces.
13901 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
13902 * start and end of the first magic braces item.
13903 * Return a pointer to just after the name. Equal to "arg" if there is no
13904 * valid name.
13905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013907find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908 char_u *arg;
13909 char_u **expr_start;
13910 char_u **expr_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013911 int incl_br; /* Include [] indexes and .name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013913 int mb_nest = 0;
13914 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915 char_u *p;
13916
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013917 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013919 *expr_start = NULL;
13920 *expr_end = NULL;
13921 }
13922
13923 for (p = arg; *p != NUL
13924 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013925 || *p == '{'
Bram Moolenaar8c711452005-01-14 21:53:12 +000013926 || (incl_br && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013927 || mb_nest != 0
13928 || br_nest != 0); ++p)
13929 {
13930 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013932 if (*p == '[')
13933 ++br_nest;
13934 else if (*p == ']')
13935 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013937 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013939 if (*p == '{')
13940 {
13941 mb_nest++;
13942 if (expr_start != NULL && *expr_start == NULL)
13943 *expr_start = p;
13944 }
13945 else if (*p == '}')
13946 {
13947 mb_nest--;
13948 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
13949 *expr_end = p;
13950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952 }
13953
13954 return p;
13955}
13956
13957/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013958 * Expands out the 'magic' {}'s in a variable/function name.
13959 * Note that this can call itself recursively, to deal with
13960 * constructs like foo{bar}{baz}{bam}
13961 * The four pointer arguments point to "foo{expre}ss{ion}bar"
13962 * "in_start" ^
13963 * "expr_start" ^
13964 * "expr_end" ^
13965 * "in_end" ^
13966 *
13967 * Returns a new allocated string, which the caller must free.
13968 * Returns NULL for failure.
13969 */
13970 static char_u *
13971make_expanded_name(in_start, expr_start, expr_end, in_end)
13972 char_u *in_start;
13973 char_u *expr_start;
13974 char_u *expr_end;
13975 char_u *in_end;
13976{
13977 char_u c1;
13978 char_u *retval = NULL;
13979 char_u *temp_result;
13980 char_u *nextcmd = NULL;
13981
13982 if (expr_end == NULL || in_end == NULL)
13983 return NULL;
13984 *expr_start = NUL;
13985 *expr_end = NUL;
13986 c1 = *in_end;
13987 *in_end = NUL;
13988
13989 temp_result = eval_to_string(expr_start + 1, &nextcmd);
13990 if (temp_result != NULL && nextcmd == NULL)
13991 {
13992 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
13993 + (in_end - expr_end) + 1));
13994 if (retval != NULL)
13995 {
13996 STRCPY(retval, in_start);
13997 STRCAT(retval, temp_result);
13998 STRCAT(retval, expr_end + 1);
13999 }
14000 }
14001 vim_free(temp_result);
14002
14003 *in_end = c1; /* put char back for error messages */
14004 *expr_start = '{';
14005 *expr_end = '}';
14006
14007 if (retval != NULL)
14008 {
14009 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
14010 if (expr_start != NULL)
14011 {
14012 /* Further expansion! */
14013 temp_result = make_expanded_name(retval, expr_start,
14014 expr_end, temp_result);
14015 vim_free(retval);
14016 retval = temp_result;
14017 }
14018 }
14019
14020 return retval;
14021}
14022
14023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014025 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026 */
14027 static int
14028eval_isnamec(c)
14029 int c;
14030{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014031 return (ASCII_ISALNUM(c) || c == '_' || c == ':');
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032}
14033
14034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 * Set number v: variable to "val".
14036 */
14037 void
14038set_vim_var_nr(idx, val)
14039 int idx;
14040 long val;
14041{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014042 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043}
14044
14045/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014046 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047 */
14048 long
14049get_vim_var_nr(idx)
14050 int idx;
14051{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014052 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053}
14054
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014055#if defined(FEAT_AUTOCMD) || defined(PROTO)
14056/*
14057 * Get string v: variable value. Uses a static buffer, can only be used once.
14058 */
14059 char_u *
14060get_vim_var_str(idx)
14061 int idx;
14062{
14063 return get_tv_string(&vimvars[idx].vv_tv);
14064}
14065#endif
14066
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067/*
14068 * Set v:count, v:count1 and v:prevcount.
14069 */
14070 void
14071set_vcount(count, count1)
14072 long count;
14073 long count1;
14074{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014075 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
14076 vimvars[VV_COUNT].vv_nr = count;
14077 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078}
14079
14080/*
14081 * Set string v: variable to a copy of "val".
14082 */
14083 void
14084set_vim_var_string(idx, val, len)
14085 int idx;
14086 char_u *val;
14087 int len; /* length of "val" to use or -1 (whole string) */
14088{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014089 /* Need to do this (at least) once, since we can't initialize a union.
14090 * Will always be invoked when "v:progname" is set. */
14091 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
14092
Bram Moolenaare9a41262005-01-15 22:18:47 +000014093 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014095 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014097 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000014099 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100}
14101
14102/*
14103 * Set v:register if needed.
14104 */
14105 void
14106set_reg_var(c)
14107 int c;
14108{
14109 char_u regname;
14110
14111 if (c == 0 || c == ' ')
14112 regname = '"';
14113 else
14114 regname = c;
14115 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014116 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014117 set_vim_var_string(VV_REG, &regname, 1);
14118}
14119
14120/*
14121 * Get or set v:exception. If "oldval" == NULL, return the current value.
14122 * Otherwise, restore the value to "oldval" and return NULL.
14123 * Must always be called in pairs to save and restore v:exception! Does not
14124 * take care of memory allocations.
14125 */
14126 char_u *
14127v_exception(oldval)
14128 char_u *oldval;
14129{
14130 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014131 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132
Bram Moolenaare9a41262005-01-15 22:18:47 +000014133 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134 return NULL;
14135}
14136
14137/*
14138 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
14139 * Otherwise, restore the value to "oldval" and return NULL.
14140 * Must always be called in pairs to save and restore v:throwpoint! Does not
14141 * take care of memory allocations.
14142 */
14143 char_u *
14144v_throwpoint(oldval)
14145 char_u *oldval;
14146{
14147 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014148 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149
Bram Moolenaare9a41262005-01-15 22:18:47 +000014150 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151 return NULL;
14152}
14153
14154#if defined(FEAT_AUTOCMD) || defined(PROTO)
14155/*
14156 * Set v:cmdarg.
14157 * If "eap" != NULL, use "eap" to generate the value and return the old value.
14158 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
14159 * Must always be called in pairs!
14160 */
14161 char_u *
14162set_cmdarg(eap, oldarg)
14163 exarg_T *eap;
14164 char_u *oldarg;
14165{
14166 char_u *oldval;
14167 char_u *newval;
14168 unsigned len;
14169
Bram Moolenaare9a41262005-01-15 22:18:47 +000014170 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014171 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014173 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000014174 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014175 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176 }
14177
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014178 if (eap->force_bin == FORCE_BIN)
14179 len = 6;
14180 else if (eap->force_bin == FORCE_NOBIN)
14181 len = 8;
14182 else
14183 len = 0;
14184 if (eap->force_ff != 0)
14185 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
14186# ifdef FEAT_MBYTE
14187 if (eap->force_enc != 0)
14188 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
14189# endif
14190
14191 newval = alloc(len + 1);
14192 if (newval == NULL)
14193 return NULL;
14194
14195 if (eap->force_bin == FORCE_BIN)
14196 sprintf((char *)newval, " ++bin");
14197 else if (eap->force_bin == FORCE_NOBIN)
14198 sprintf((char *)newval, " ++nobin");
14199 else
14200 *newval = NUL;
14201 if (eap->force_ff != 0)
14202 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
14203 eap->cmd + eap->force_ff);
14204# ifdef FEAT_MBYTE
14205 if (eap->force_enc != 0)
14206 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
14207 eap->cmd + eap->force_enc);
14208# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000014209 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014210 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211}
14212#endif
14213
14214/*
14215 * Get the value of internal variable "name".
14216 * Return OK or FAIL.
14217 */
14218 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014219get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220 char_u *name;
14221 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000014222 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014223 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224{
14225 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000014226 typval_T *tv = NULL;
14227 typval_T atv;
14228 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230
14231 /* truncate the name, so that we can use strcmp() */
14232 cc = name[len];
14233 name[len] = NUL;
14234
14235 /*
14236 * Check for "b:changedtick".
14237 */
14238 if (STRCMP(name, "b:changedtick") == 0)
14239 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000014240 atv.v_type = VAR_NUMBER;
14241 atv.vval.v_number = curbuf->b_changedtick;
14242 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243 }
14244
14245 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246 * Check for user-defined variables.
14247 */
14248 else
14249 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014250 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014252 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253 }
14254
Bram Moolenaare9a41262005-01-15 22:18:47 +000014255 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014257 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014258 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259 ret = FAIL;
14260 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014261 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014262 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263
14264 name[len] = cc;
14265
14266 return ret;
14267}
14268
14269/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014270 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
14271 * Also handle function call with Funcref variable: func(expr)
14272 * Can all be combined: dict.func(expr)[idx]['func'](expr)
14273 */
14274 static int
14275handle_subscript(arg, rettv, evaluate, verbose)
14276 char_u **arg;
14277 typval_T *rettv;
14278 int evaluate; /* do more than finding the end */
14279 int verbose; /* give error messages */
14280{
14281 int ret = OK;
14282 dict_T *selfdict = NULL;
14283 char_u *s;
14284 int len;
14285
14286 while (ret == OK
14287 && (**arg == '['
14288 || (**arg == '.' && rettv->v_type == VAR_DICT)
14289 || (**arg == '(' && rettv->v_type == VAR_FUNC))
14290 && !vim_iswhite(*(*arg - 1)))
14291 {
14292 if (**arg == '(')
14293 {
14294 s = rettv->vval.v_string;
14295
14296 /* Invoke the function. Recursive! */
14297 ret = get_func_tv(s, STRLEN(s), rettv, arg,
14298 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
14299 &len, evaluate, selfdict);
14300
14301 /* Stop the expression evaluation when immediately aborting on
14302 * error, or when an interrupt occurred or an exception was thrown
14303 * but not caught. */
14304 if (aborting())
14305 {
14306 if (ret == OK)
14307 clear_tv(rettv);
14308 ret = FAIL;
14309 }
14310 dict_unref(selfdict);
14311 selfdict = NULL;
14312 }
14313 else /* **arg == '[' || **arg == '.' */
14314 {
14315 dict_unref(selfdict);
14316 if (rettv->v_type == VAR_DICT)
14317 {
14318 selfdict = rettv->vval.v_dict;
14319 if (selfdict != NULL)
14320 ++selfdict->dv_refcount;
14321 }
14322 else
14323 selfdict = NULL;
14324 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
14325 {
14326 clear_tv(rettv);
14327 ret = FAIL;
14328 }
14329 }
14330 }
14331 dict_unref(selfdict);
14332 return ret;
14333}
14334
14335/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014336 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
14337 * value).
14338 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014339 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014340alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014341{
Bram Moolenaar33570922005-01-25 22:26:29 +000014342 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014343}
14344
14345/*
14346 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347 * The string "s" must have been allocated, it is consumed.
14348 * Return NULL for out of memory, the variable otherwise.
14349 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014350 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014351alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352 char_u *s;
14353{
Bram Moolenaar33570922005-01-25 22:26:29 +000014354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014356 rettv = alloc_tv();
14357 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014359 rettv->v_type = VAR_STRING;
14360 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361 }
14362 else
14363 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014364 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365}
14366
14367/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014368 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 */
14370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014371free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014372 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373{
14374 if (varp != NULL)
14375 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014376 switch (varp->v_type)
14377 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014378 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014379 func_unref(varp->vval.v_string);
14380 /*FALLTHROUGH*/
14381 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014382 vim_free(varp->vval.v_string);
14383 break;
14384 case VAR_LIST:
14385 list_unref(varp->vval.v_list);
14386 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014387 case VAR_DICT:
14388 dict_unref(varp->vval.v_dict);
14389 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014390 case VAR_NUMBER:
14391 case VAR_UNKNOWN:
14392 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014393 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014394 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014395 break;
14396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397 vim_free(varp);
14398 }
14399}
14400
14401/*
14402 * Free the memory for a variable value and set the value to NULL or 0.
14403 */
14404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014405clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014406 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407{
14408 if (varp != NULL)
14409 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014410 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014412 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014413 func_unref(varp->vval.v_string);
14414 /*FALLTHROUGH*/
14415 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014416 vim_free(varp->vval.v_string);
14417 varp->vval.v_string = NULL;
14418 break;
14419 case VAR_LIST:
14420 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014421 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014422 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014423 case VAR_DICT:
14424 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014425 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014426 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014427 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014428 varp->vval.v_number = 0;
14429 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014430 case VAR_UNKNOWN:
14431 break;
14432 default:
14433 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014435 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 }
14437}
14438
14439/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014440 * Set the value of a variable to NULL without freeing items.
14441 */
14442 static void
14443init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014444 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014445{
14446 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014447 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014448}
14449
14450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 * Get the number value of a variable.
14452 * If it is a String variable, uses vim_str2nr().
14453 */
14454 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014455get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014456 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014458 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014460 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014462 case VAR_NUMBER:
14463 n = (long)(varp->vval.v_number);
14464 break;
14465 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014466 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014467 break;
14468 case VAR_STRING:
14469 if (varp->vval.v_string != NULL)
14470 vim_str2nr(varp->vval.v_string, NULL, NULL,
14471 TRUE, TRUE, &n, NULL);
14472 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014473 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014474 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014475 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014476 case VAR_DICT:
14477 EMSG(_("E728: Using a Dictionary as a number"));
14478 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014479 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014480 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014481 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014483 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484}
14485
14486/*
14487 * Get the lnum from the first argument. Also accepts ".", "$", etc.
14488 */
14489 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014490get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000014491 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492{
Bram Moolenaar33570922005-01-25 22:26:29 +000014493 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494 linenr_T lnum;
14495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014496 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497 if (lnum == 0) /* no valid number, try using line() */
14498 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014499 rettv.v_type = VAR_NUMBER;
14500 f_line(argvars, &rettv);
14501 lnum = rettv.vval.v_number;
14502 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503 }
14504 return lnum;
14505}
14506
14507/*
14508 * Get the string value of a variable.
14509 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000014510 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
14511 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 * If the String variable has never been set, return an empty string.
14513 * Never returns NULL;
14514 */
14515 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014516get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014517 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518{
14519 static char_u mybuf[NUMBUFLEN];
14520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014521 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522}
14523
14524 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014525get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000014526 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014527 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014529 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014531 case VAR_NUMBER:
14532 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
14533 return buf;
14534 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014535 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014536 break;
14537 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014538 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014539 break;
14540 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014541 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014542 break;
14543 case VAR_STRING:
14544 if (varp->vval.v_string != NULL)
14545 return varp->vval.v_string;
14546 break;
14547 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014548 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014549 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014551 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552}
14553
14554/*
14555 * Find variable "name" in the list of variables.
14556 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014557 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014558 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000014559 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014561 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014562find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014564 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014567 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568
Bram Moolenaara7043832005-01-21 11:56:39 +000014569 ht = find_var_ht(name, &varname);
14570 if (htp != NULL)
14571 *htp = ht;
14572 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014574 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575}
14576
14577/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014578 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000014579 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014581 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014582find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000014583 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000014584 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014585 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000014586{
Bram Moolenaar33570922005-01-25 22:26:29 +000014587 hashitem_T *hi;
14588
14589 if (*varname == NUL)
14590 {
14591 /* Must be something like "s:", otherwise "ht" would be NULL. */
14592 switch (varname[-2])
14593 {
14594 case 's': return &SCRIPT_SV(current_SID).sv_var;
14595 case 'g': return &globvars_var;
14596 case 'v': return &vimvars_var;
14597 case 'b': return &curbuf->b_bufvar;
14598 case 'w': return &curwin->w_winvar;
14599 case 'l': return &current_funccal->l_vars_var;
14600 case 'a': return &current_funccal->l_avars_var;
14601 }
14602 return NULL;
14603 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014604
14605 hi = hash_find(ht, varname);
14606 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014607 {
14608 /* For global variables we may try auto-loading the script. If it
14609 * worked find the variable again. */
14610 if (ht == &globvarht && !writing
14611 && script_autoload(varname) && !aborting())
14612 hi = hash_find(ht, varname);
14613 if (HASHITEM_EMPTY(hi))
14614 return NULL;
14615 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014616 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014617}
14618
14619/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014620 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014621 * Set "varname" to the start of name without ':'.
14622 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014623 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014624find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625 char_u *name;
14626 char_u **varname;
14627{
14628 if (name[1] != ':')
14629 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014630 /* The name must not start with a colon. */
14631 if (name[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632 return NULL;
14633 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014634
14635 /* "version" is "v:version" in all scopes */
14636 if (!HASHITEM_EMPTY(hash_find(&compat_hashtab, name)))
14637 return &compat_hashtab;
14638
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014640 return &globvarht; /* global variable */
14641 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642 }
14643 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014644 if (*name == 'g') /* global variable */
14645 return &globvarht;
14646 /* There must be no ':' in the rest of the name, unless g: is used */
14647 if (vim_strchr(name + 2, ':') != NULL)
14648 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014650 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014651 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014652 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000014653 if (*name == 'v') /* v: variable */
14654 return &vimvarht;
14655 if (*name == 'a' && current_funccal != NULL) /* function argument */
14656 return &current_funccal->l_avars.dv_hashtab;
14657 if (*name == 'l' && current_funccal != NULL) /* local function variable */
14658 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659 if (*name == 's' /* script variable */
14660 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
14661 return &SCRIPT_VARS(current_SID);
14662 return NULL;
14663}
14664
14665/*
14666 * Get the string value of a (global/local) variable.
14667 * Returns NULL when it doesn't exist.
14668 */
14669 char_u *
14670get_var_value(name)
14671 char_u *name;
14672{
Bram Moolenaar33570922005-01-25 22:26:29 +000014673 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674
Bram Moolenaara7043832005-01-21 11:56:39 +000014675 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676 if (v == NULL)
14677 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014678 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679}
14680
14681/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014682 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683 * sourcing this script and when executing functions defined in the script.
14684 */
14685 void
14686new_script_vars(id)
14687 scid_T id;
14688{
Bram Moolenaara7043832005-01-21 11:56:39 +000014689 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000014690 hashtab_T *ht;
14691 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000014692
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
14694 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014695 /* Re-allocating ga_data means that an ht_array pointing to
14696 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000014697 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000014698 for (i = 1; i <= ga_scripts.ga_len; ++i)
14699 {
14700 ht = &SCRIPT_VARS(i);
14701 if (ht->ht_mask == HT_INIT_SIZE - 1)
14702 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000014703 sv = &SCRIPT_SV(i);
14704 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000014705 }
14706
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707 while (ga_scripts.ga_len < id)
14708 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014709 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
14710 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712 }
14713 }
14714}
14715
14716/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014717 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
14718 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719 */
14720 void
Bram Moolenaar33570922005-01-25 22:26:29 +000014721init_var_dict(dict, dict_var)
14722 dict_T *dict;
14723 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724{
Bram Moolenaar33570922005-01-25 22:26:29 +000014725 hash_init(&dict->dv_hashtab);
14726 dict->dv_refcount = 99999;
14727 dict_var->di_tv.vval.v_dict = dict;
14728 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014729 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014730 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
14731 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014732}
14733
14734/*
14735 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000014736 * Frees all allocated variables and the value they contain.
14737 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738 */
14739 void
Bram Moolenaara7043832005-01-21 11:56:39 +000014740vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000014741 hashtab_T *ht;
14742{
14743 vars_clear_ext(ht, TRUE);
14744}
14745
14746/*
14747 * Like vars_clear(), but only free the value if "free_val" is TRUE.
14748 */
14749 static void
14750vars_clear_ext(ht, free_val)
14751 hashtab_T *ht;
14752 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753{
Bram Moolenaara7043832005-01-21 11:56:39 +000014754 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000014755 hashitem_T *hi;
14756 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757
Bram Moolenaar33570922005-01-25 22:26:29 +000014758 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000014759 todo = ht->ht_used;
14760 for (hi = ht->ht_array; todo > 0; ++hi)
14761 {
14762 if (!HASHITEM_EMPTY(hi))
14763 {
14764 --todo;
14765
Bram Moolenaar33570922005-01-25 22:26:29 +000014766 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000014767 * ht_array might change then. hash_clear() takes care of it
14768 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014769 v = HI2DI(hi);
14770 if (free_val)
14771 clear_tv(&v->di_tv);
14772 if ((v->di_flags & DI_FLAGS_FIX) == 0)
14773 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000014774 }
14775 }
14776 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777}
14778
Bram Moolenaara7043832005-01-21 11:56:39 +000014779/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014780 * Delete a variable from hashtab "ht" at item "hi".
14781 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000014782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014783 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000014784delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000014785 hashtab_T *ht;
14786 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787{
Bram Moolenaar33570922005-01-25 22:26:29 +000014788 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014789
14790 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000014791 clear_tv(&di->di_tv);
14792 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793}
14794
14795/*
14796 * List the value of one internal variable.
14797 */
14798 static void
14799list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000014800 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014801 char_u *prefix;
14802{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014803 char_u *tofree;
14804 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014805 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014806
Bram Moolenaar33570922005-01-25 22:26:29 +000014807 s = echo_string(&v->di_tv, &tofree, numbuf);
14808 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014809 s == NULL ? (char_u *)"" : s);
14810 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811}
14812
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813 static void
14814list_one_var_a(prefix, name, type, string)
14815 char_u *prefix;
14816 char_u *name;
14817 int type;
14818 char_u *string;
14819{
14820 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
14821 if (name != NULL) /* "a:" vars don't have a name stored */
14822 msg_puts(name);
14823 msg_putchar(' ');
14824 msg_advance(22);
14825 if (type == VAR_NUMBER)
14826 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014827 else if (type == VAR_FUNC)
14828 msg_putchar('*');
14829 else if (type == VAR_LIST)
14830 {
14831 msg_putchar('[');
14832 if (*string == '[')
14833 ++string;
14834 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014835 else if (type == VAR_DICT)
14836 {
14837 msg_putchar('{');
14838 if (*string == '{')
14839 ++string;
14840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841 else
14842 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014843
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014845
14846 if (type == VAR_FUNC)
14847 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848}
14849
14850/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014851 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 * If the variable already exists, the value is updated.
14853 * Otherwise the variable is created.
14854 */
14855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014856set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014858 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014859 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860{
Bram Moolenaar33570922005-01-25 22:26:29 +000014861 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014863 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014865 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014866 {
14867 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
14868 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
14869 ? name[2] : name[0]))
14870 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014871 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014872 return;
14873 }
14874 if (function_exists(name))
14875 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014876 EMSG2(_("705: Variable name conflicts with existing function: %s"),
14877 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014878 return;
14879 }
14880 }
14881
Bram Moolenaara7043832005-01-21 11:56:39 +000014882 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014883 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000014884 {
14885 EMSG2(_("E461: Illegal variable name: %s"), name);
14886 return;
14887 }
14888
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014889 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014890 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014892 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014893 if (var_check_ro(v->di_flags, name)
14894 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000014895 return;
14896 if (v->di_tv.v_type != tv->v_type
14897 && !((v->di_tv.v_type == VAR_STRING
14898 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014899 && (tv->v_type == VAR_STRING
14900 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014901 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014902 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014903 return;
14904 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014905
14906 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000014907 * Handle setting internal v: variables separately: we don't change
14908 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000014909 */
14910 if (ht == &vimvarht)
14911 {
14912 if (v->di_tv.v_type == VAR_STRING)
14913 {
14914 vim_free(v->di_tv.vval.v_string);
14915 if (copy || tv->v_type != VAR_STRING)
14916 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
14917 else
14918 {
14919 /* Take over the string to avoid an extra alloc/free. */
14920 v->di_tv.vval.v_string = tv->vval.v_string;
14921 tv->vval.v_string = NULL;
14922 }
14923 }
14924 else if (v->di_tv.v_type != VAR_NUMBER)
14925 EMSG2(_(e_intern2), "set_var()");
14926 else
14927 v->di_tv.vval.v_number = get_tv_number(tv);
14928 return;
14929 }
14930
14931 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014932 }
14933 else /* add a new variable */
14934 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014935 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
14936 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000014937 if (v == NULL)
14938 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000014939 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014940 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014942 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943 return;
14944 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014945 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014947
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014948 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000014949 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014950 else
14951 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014952 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014953 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014954 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956}
14957
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014958/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014959 * Return TRUE if di_flags "flags" indicate read-only variable "name".
14960 * Also give an error message.
14961 */
14962 static int
14963var_check_ro(flags, name)
14964 int flags;
14965 char_u *name;
14966{
14967 if (flags & DI_FLAGS_RO)
14968 {
14969 EMSG2(_(e_readonlyvar), name);
14970 return TRUE;
14971 }
14972 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
14973 {
14974 EMSG2(_(e_readonlysbx), name);
14975 return TRUE;
14976 }
14977 return FALSE;
14978}
14979
14980/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014981 * Return TRUE if typeval "tv" is set to be locked (immutable).
14982 * Also give an error message, using "name".
14983 */
14984 static int
14985tv_check_lock(lock, name)
14986 int lock;
14987 char_u *name;
14988{
14989 if (lock & VAR_LOCKED)
14990 {
14991 EMSG2(_("E741: Value is locked: %s"),
14992 name == NULL ? (char_u *)_("Unknown") : name);
14993 return TRUE;
14994 }
14995 if (lock & VAR_FIXED)
14996 {
14997 EMSG2(_("E742: Cannot change value of %s"),
14998 name == NULL ? (char_u *)_("Unknown") : name);
14999 return TRUE;
15000 }
15001 return FALSE;
15002}
15003
15004/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015005 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015006 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015007 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015010copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000015011 typval_T *from;
15012 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015014 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015015 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015016 switch (from->v_type)
15017 {
15018 case VAR_NUMBER:
15019 to->vval.v_number = from->vval.v_number;
15020 break;
15021 case VAR_STRING:
15022 case VAR_FUNC:
15023 if (from->vval.v_string == NULL)
15024 to->vval.v_string = NULL;
15025 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015026 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015027 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015028 if (from->v_type == VAR_FUNC)
15029 func_ref(to->vval.v_string);
15030 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015031 break;
15032 case VAR_LIST:
15033 if (from->vval.v_list == NULL)
15034 to->vval.v_list = NULL;
15035 else
15036 {
15037 to->vval.v_list = from->vval.v_list;
15038 ++to->vval.v_list->lv_refcount;
15039 }
15040 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015041 case VAR_DICT:
15042 if (from->vval.v_dict == NULL)
15043 to->vval.v_dict = NULL;
15044 else
15045 {
15046 to->vval.v_dict = from->vval.v_dict;
15047 ++to->vval.v_dict->dv_refcount;
15048 }
15049 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015050 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015051 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015052 break;
15053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054}
15055
15056/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000015057 * Make a copy of an item.
15058 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015059 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
15060 * reference to an already copied list/dict can be used.
15061 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015062 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015063 static int
15064item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000015065 typval_T *from;
15066 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015067 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015068 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015069{
15070 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015071 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015072
Bram Moolenaar33570922005-01-25 22:26:29 +000015073 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015074 {
15075 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015076 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015077 }
15078 ++recurse;
15079
15080 switch (from->v_type)
15081 {
15082 case VAR_NUMBER:
15083 case VAR_STRING:
15084 case VAR_FUNC:
15085 copy_tv(from, to);
15086 break;
15087 case VAR_LIST:
15088 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015089 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015090 if (from->vval.v_list == NULL)
15091 to->vval.v_list = NULL;
15092 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
15093 {
15094 /* use the copy made earlier */
15095 to->vval.v_list = from->vval.v_list->lv_copylist;
15096 ++to->vval.v_list->lv_refcount;
15097 }
15098 else
15099 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
15100 if (to->vval.v_list == NULL)
15101 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015102 break;
15103 case VAR_DICT:
15104 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015105 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015106 if (from->vval.v_dict == NULL)
15107 to->vval.v_dict = NULL;
15108 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
15109 {
15110 /* use the copy made earlier */
15111 to->vval.v_dict = from->vval.v_dict->dv_copydict;
15112 ++to->vval.v_dict->dv_refcount;
15113 }
15114 else
15115 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
15116 if (to->vval.v_dict == NULL)
15117 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015118 break;
15119 default:
15120 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015121 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015122 }
15123 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015124 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015125}
15126
15127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128 * ":echo expr1 ..." print each argument separated with a space, add a
15129 * newline at the end.
15130 * ":echon expr1 ..." print each argument plain.
15131 */
15132 void
15133ex_echo(eap)
15134 exarg_T *eap;
15135{
15136 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015137 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015138 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015139 char_u *p;
15140 int needclr = TRUE;
15141 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015142 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143
15144 if (eap->skip)
15145 ++emsg_skip;
15146 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
15147 {
15148 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015149 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150 {
15151 /*
15152 * Report the invalid expression unless the expression evaluation
15153 * has been cancelled due to an aborting error, an interrupt, or an
15154 * exception.
15155 */
15156 if (!aborting())
15157 EMSG2(_(e_invexpr2), p);
15158 break;
15159 }
15160 if (!eap->skip)
15161 {
15162 if (atstart)
15163 {
15164 atstart = FALSE;
15165 /* Call msg_start() after eval1(), evaluating the expression
15166 * may cause a message to appear. */
15167 if (eap->cmdidx == CMD_echo)
15168 msg_start();
15169 }
15170 else if (eap->cmdidx == CMD_echo)
15171 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015172 p = echo_string(&rettv, &tofree, numbuf);
15173 if (p != NULL)
15174 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015175 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015176 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015178 if (*p != TAB && needclr)
15179 {
15180 /* remove any text still there from the command */
15181 msg_clr_eos();
15182 needclr = FALSE;
15183 }
15184 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185 }
15186 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015187 {
15188#ifdef FEAT_MBYTE
15189 if (has_mbyte)
15190 {
15191 int i = (*mb_ptr2len_check)(p);
15192
15193 (void)msg_outtrans_len_attr(p, i, echo_attr);
15194 p += i - 1;
15195 }
15196 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015198 (void)msg_outtrans_len_attr(p, 1, echo_attr);
15199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015200 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015201 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015203 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015204 arg = skipwhite(arg);
15205 }
15206 eap->nextcmd = check_nextcmd(arg);
15207
15208 if (eap->skip)
15209 --emsg_skip;
15210 else
15211 {
15212 /* remove text that may still be there from the command */
15213 if (needclr)
15214 msg_clr_eos();
15215 if (eap->cmdidx == CMD_echo)
15216 msg_end();
15217 }
15218}
15219
15220/*
15221 * ":echohl {name}".
15222 */
15223 void
15224ex_echohl(eap)
15225 exarg_T *eap;
15226{
15227 int id;
15228
15229 id = syn_name2id(eap->arg);
15230 if (id == 0)
15231 echo_attr = 0;
15232 else
15233 echo_attr = syn_id2attr(id);
15234}
15235
15236/*
15237 * ":execute expr1 ..." execute the result of an expression.
15238 * ":echomsg expr1 ..." Print a message
15239 * ":echoerr expr1 ..." Print an error
15240 * Each gets spaces around each argument and a newline at the end for
15241 * echo commands
15242 */
15243 void
15244ex_execute(eap)
15245 exarg_T *eap;
15246{
15247 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015248 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015249 int ret = OK;
15250 char_u *p;
15251 garray_T ga;
15252 int len;
15253 int save_did_emsg;
15254
15255 ga_init2(&ga, 1, 80);
15256
15257 if (eap->skip)
15258 ++emsg_skip;
15259 while (*arg != NUL && *arg != '|' && *arg != '\n')
15260 {
15261 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015262 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263 {
15264 /*
15265 * Report the invalid expression unless the expression evaluation
15266 * has been cancelled due to an aborting error, an interrupt, or an
15267 * exception.
15268 */
15269 if (!aborting())
15270 EMSG2(_(e_invexpr2), p);
15271 ret = FAIL;
15272 break;
15273 }
15274
15275 if (!eap->skip)
15276 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015277 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278 len = (int)STRLEN(p);
15279 if (ga_grow(&ga, len + 2) == FAIL)
15280 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015281 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282 ret = FAIL;
15283 break;
15284 }
15285 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015286 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015288 ga.ga_len += len;
15289 }
15290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015291 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015292 arg = skipwhite(arg);
15293 }
15294
15295 if (ret != FAIL && ga.ga_data != NULL)
15296 {
15297 if (eap->cmdidx == CMD_echomsg)
15298 MSG_ATTR(ga.ga_data, echo_attr);
15299 else if (eap->cmdidx == CMD_echoerr)
15300 {
15301 /* We don't want to abort following commands, restore did_emsg. */
15302 save_did_emsg = did_emsg;
15303 EMSG((char_u *)ga.ga_data);
15304 if (!force_abort)
15305 did_emsg = save_did_emsg;
15306 }
15307 else if (eap->cmdidx == CMD_execute)
15308 do_cmdline((char_u *)ga.ga_data,
15309 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
15310 }
15311
15312 ga_clear(&ga);
15313
15314 if (eap->skip)
15315 --emsg_skip;
15316
15317 eap->nextcmd = check_nextcmd(arg);
15318}
15319
15320/*
15321 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
15322 * "arg" points to the "&" or '+' when called, to "option" when returning.
15323 * Returns NULL when no option name found. Otherwise pointer to the char
15324 * after the option name.
15325 */
15326 static char_u *
15327find_option_end(arg, opt_flags)
15328 char_u **arg;
15329 int *opt_flags;
15330{
15331 char_u *p = *arg;
15332
15333 ++p;
15334 if (*p == 'g' && p[1] == ':')
15335 {
15336 *opt_flags = OPT_GLOBAL;
15337 p += 2;
15338 }
15339 else if (*p == 'l' && p[1] == ':')
15340 {
15341 *opt_flags = OPT_LOCAL;
15342 p += 2;
15343 }
15344 else
15345 *opt_flags = 0;
15346
15347 if (!ASCII_ISALPHA(*p))
15348 return NULL;
15349 *arg = p;
15350
15351 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
15352 p += 4; /* termcap option */
15353 else
15354 while (ASCII_ISALPHA(*p))
15355 ++p;
15356 return p;
15357}
15358
15359/*
15360 * ":function"
15361 */
15362 void
15363ex_function(eap)
15364 exarg_T *eap;
15365{
15366 char_u *theline;
15367 int j;
15368 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370 char_u *name = NULL;
15371 char_u *p;
15372 char_u *arg;
15373 garray_T newargs;
15374 garray_T newlines;
15375 int varargs = FALSE;
15376 int mustend = FALSE;
15377 int flags = 0;
15378 ufunc_T *fp;
15379 int indent;
15380 int nesting;
15381 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015382 dictitem_T *v;
15383 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015384 static int func_nr = 0; /* number for nameless function */
15385 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015386 hashtab_T *ht;
15387 int todo;
15388 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389
15390 /*
15391 * ":function" without argument: list functions.
15392 */
15393 if (ends_excmd(*eap->arg))
15394 {
15395 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015396 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000015397 todo = func_hashtab.ht_used;
15398 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015399 {
15400 if (!HASHITEM_EMPTY(hi))
15401 {
15402 --todo;
15403 fp = HI2UF(hi);
15404 if (!isdigit(*fp->uf_name))
15405 list_func_head(fp, FALSE);
15406 }
15407 }
15408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409 eap->nextcmd = check_nextcmd(eap->arg);
15410 return;
15411 }
15412
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015413 /*
15414 * Get the function name. There are these situations:
15415 * func normal function name
15416 * "name" == func, "fudi.fd_dict" == NULL
15417 * dict.func new dictionary entry
15418 * "name" == NULL, "fudi.fd_dict" set,
15419 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
15420 * dict.func existing dict entry with a Funcref
15421 * "name" == fname, "fudi.fd_dict" set,
15422 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15423 * dict.func existing dict entry that's not a Funcref
15424 * "name" == NULL, "fudi.fd_dict" set,
15425 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015428 name = trans_function_name(&p, eap->skip, 0, &fudi);
15429 paren = (vim_strchr(p, '(') != NULL);
15430 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431 {
15432 /*
15433 * Return on an invalid expression in braces, unless the expression
15434 * evaluation has been cancelled due to an aborting error, an
15435 * interrupt, or an exception.
15436 */
15437 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015438 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015439 if (!eap->skip && fudi.fd_newkey != NULL)
15440 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015441 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444 else
15445 eap->skip = TRUE;
15446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447 /* An error in a function call during evaluation of an expression in magic
15448 * braces should not cause the function not to be defined. */
15449 saved_did_emsg = did_emsg;
15450 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451
15452 /*
15453 * ":function func" with only function name: list function.
15454 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015455 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015456 {
15457 if (!ends_excmd(*skipwhite(p)))
15458 {
15459 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015460 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461 }
15462 eap->nextcmd = check_nextcmd(p);
15463 if (eap->nextcmd != NULL)
15464 *p = NUL;
15465 if (!eap->skip && !got_int)
15466 {
15467 fp = find_func(name);
15468 if (fp != NULL)
15469 {
15470 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015471 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472 {
15473 msg_putchar('\n');
15474 msg_outnum((long)(j + 1));
15475 if (j < 9)
15476 msg_putchar(' ');
15477 if (j < 99)
15478 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015479 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480 out_flush(); /* show a line at a time */
15481 ui_breakcheck();
15482 }
15483 if (!got_int)
15484 {
15485 msg_putchar('\n');
15486 msg_puts((char_u *)" endfunction");
15487 }
15488 }
15489 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015490 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015492 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015493 }
15494
15495 /*
15496 * ":function name(arg1, arg2)" Define function.
15497 */
15498 p = skipwhite(p);
15499 if (*p != '(')
15500 {
15501 if (!eap->skip)
15502 {
15503 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015504 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505 }
15506 /* attempt to continue by skipping some text */
15507 if (vim_strchr(p, '(') != NULL)
15508 p = vim_strchr(p, '(');
15509 }
15510 p = skipwhite(p + 1);
15511
15512 ga_init2(&newargs, (int)sizeof(char_u *), 3);
15513 ga_init2(&newlines, (int)sizeof(char_u *), 3);
15514
15515 /*
15516 * Isolate the arguments: "arg1, arg2, ...)"
15517 */
15518 while (*p != ')')
15519 {
15520 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
15521 {
15522 varargs = TRUE;
15523 p += 3;
15524 mustend = TRUE;
15525 }
15526 else
15527 {
15528 arg = p;
15529 while (ASCII_ISALNUM(*p) || *p == '_')
15530 ++p;
15531 if (arg == p || isdigit(*arg)
15532 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
15533 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
15534 {
15535 if (!eap->skip)
15536 EMSG2(_("E125: Illegal argument: %s"), arg);
15537 break;
15538 }
15539 if (ga_grow(&newargs, 1) == FAIL)
15540 goto erret;
15541 c = *p;
15542 *p = NUL;
15543 arg = vim_strsave(arg);
15544 if (arg == NULL)
15545 goto erret;
15546 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
15547 *p = c;
15548 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 if (*p == ',')
15550 ++p;
15551 else
15552 mustend = TRUE;
15553 }
15554 p = skipwhite(p);
15555 if (mustend && *p != ')')
15556 {
15557 if (!eap->skip)
15558 EMSG2(_(e_invarg2), eap->arg);
15559 break;
15560 }
15561 }
15562 ++p; /* skip the ')' */
15563
Bram Moolenaare9a41262005-01-15 22:18:47 +000015564 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565 for (;;)
15566 {
15567 p = skipwhite(p);
15568 if (STRNCMP(p, "range", 5) == 0)
15569 {
15570 flags |= FC_RANGE;
15571 p += 5;
15572 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015573 else if (STRNCMP(p, "dict", 4) == 0)
15574 {
15575 flags |= FC_DICT;
15576 p += 4;
15577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578 else if (STRNCMP(p, "abort", 5) == 0)
15579 {
15580 flags |= FC_ABORT;
15581 p += 5;
15582 }
15583 else
15584 break;
15585 }
15586
15587 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
15588 EMSG(_(e_trailing));
15589
15590 /*
15591 * Read the body of the function, until ":endfunction" is found.
15592 */
15593 if (KeyTyped)
15594 {
15595 /* Check if the function already exists, don't let the user type the
15596 * whole function before telling him it doesn't work! For a script we
15597 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015598 if (!eap->skip && !eap->forceit)
15599 {
15600 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
15601 EMSG(_(e_funcdict));
15602 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015603 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605
15606 msg_putchar('\n'); /* don't overwrite the function name */
15607 cmdline_row = msg_row;
15608 }
15609
15610 indent = 2;
15611 nesting = 0;
15612 for (;;)
15613 {
15614 msg_scroll = TRUE;
15615 need_wait_return = FALSE;
15616 if (eap->getline == NULL)
15617 theline = getcmdline(':', 0L, indent);
15618 else
15619 theline = eap->getline(':', eap->cookie, indent);
15620 if (KeyTyped)
15621 lines_left = Rows - 1;
15622 if (theline == NULL)
15623 {
15624 EMSG(_("E126: Missing :endfunction"));
15625 goto erret;
15626 }
15627
15628 if (skip_until != NULL)
15629 {
15630 /* between ":append" and "." and between ":python <<EOF" and "EOF"
15631 * don't check for ":endfunc". */
15632 if (STRCMP(theline, skip_until) == 0)
15633 {
15634 vim_free(skip_until);
15635 skip_until = NULL;
15636 }
15637 }
15638 else
15639 {
15640 /* skip ':' and blanks*/
15641 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
15642 ;
15643
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015644 /* Check for "endfunction". */
15645 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015646 {
15647 vim_free(theline);
15648 break;
15649 }
15650
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015651 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652 * at "end". */
15653 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
15654 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015655 else if (STRNCMP(p, "if", 2) == 0
15656 || STRNCMP(p, "wh", 2) == 0
15657 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658 || STRNCMP(p, "try", 3) == 0)
15659 indent += 2;
15660
15661 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015662 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015664 if (*p == '!')
15665 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666 p += eval_fname_script(p);
15667 if (ASCII_ISALPHA(*p))
15668 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015669 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670 if (*skipwhite(p) == '(')
15671 {
15672 ++nesting;
15673 indent += 2;
15674 }
15675 }
15676 }
15677
15678 /* Check for ":append" or ":insert". */
15679 p = skip_range(p, NULL);
15680 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
15681 || (p[0] == 'i'
15682 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
15683 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
15684 skip_until = vim_strsave((char_u *)".");
15685
15686 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
15687 arg = skipwhite(skiptowhite(p));
15688 if (arg[0] == '<' && arg[1] =='<'
15689 && ((p[0] == 'p' && p[1] == 'y'
15690 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
15691 || (p[0] == 'p' && p[1] == 'e'
15692 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
15693 || (p[0] == 't' && p[1] == 'c'
15694 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
15695 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
15696 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000015697 || (p[0] == 'm' && p[1] == 'z'
15698 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699 ))
15700 {
15701 /* ":python <<" continues until a dot, like ":append" */
15702 p = skipwhite(arg + 2);
15703 if (*p == NUL)
15704 skip_until = vim_strsave((char_u *)".");
15705 else
15706 skip_until = vim_strsave(p);
15707 }
15708 }
15709
15710 /* Add the line to the function. */
15711 if (ga_grow(&newlines, 1) == FAIL)
15712 goto erret;
15713 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
15714 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 }
15716
15717 /* Don't define the function when skipping commands or when an error was
15718 * detected. */
15719 if (eap->skip || did_emsg)
15720 goto erret;
15721
15722 /*
15723 * If there are no errors, add the function
15724 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015725 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015726 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015727 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000015728 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015729 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015730 emsg_funcname("E707: Function name conflicts with variable: %s",
15731 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015732 goto erret;
15733 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015734
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015735 fp = find_func(name);
15736 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015738 if (!eap->forceit)
15739 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015740 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015741 goto erret;
15742 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015743 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015744 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015745 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015746 name);
15747 goto erret;
15748 }
15749 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015750 ga_clear_strings(&(fp->uf_args));
15751 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015752 vim_free(name);
15753 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755 }
15756 else
15757 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015758 char numbuf[20];
15759
15760 fp = NULL;
15761 if (fudi.fd_newkey == NULL && !eap->forceit)
15762 {
15763 EMSG(_(e_funcdict));
15764 goto erret;
15765 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000015766 if (fudi.fd_di == NULL)
15767 {
15768 /* Can't add a function to a locked dictionary */
15769 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
15770 goto erret;
15771 }
15772 /* Can't change an existing function if it is locked */
15773 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
15774 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015775
15776 /* Give the function a sequential number. Can only be used with a
15777 * Funcref! */
15778 vim_free(name);
15779 sprintf(numbuf, "%d", ++func_nr);
15780 name = vim_strsave((char_u *)numbuf);
15781 if (name == NULL)
15782 goto erret;
15783 }
15784
15785 if (fp == NULL)
15786 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015787 if (fudi.fd_dict == NULL && vim_strchr(name, ':') != NULL)
15788 {
15789 int slen, plen;
15790 char_u *scriptname;
15791
15792 /* Check that the autoload name matches the script name. */
15793 j = FAIL;
15794 if (sourcing_name != NULL)
15795 {
15796 scriptname = autoload_name(name);
15797 if (scriptname != NULL)
15798 {
15799 p = vim_strchr(scriptname, '/');
15800 plen = STRLEN(p);
15801 slen = STRLEN(sourcing_name);
15802 if (slen > plen && fnamecmp(p,
15803 sourcing_name + slen - plen) == 0)
15804 j = OK;
15805 vim_free(scriptname);
15806 }
15807 }
15808 if (j == FAIL)
15809 {
15810 EMSG2(_("E746: Function name does not match script file name: %s"), name);
15811 goto erret;
15812 }
15813 }
15814
15815 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 if (fp == NULL)
15817 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015818
15819 if (fudi.fd_dict != NULL)
15820 {
15821 if (fudi.fd_di == NULL)
15822 {
15823 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015824 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015825 if (fudi.fd_di == NULL)
15826 {
15827 vim_free(fp);
15828 goto erret;
15829 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015830 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
15831 {
15832 vim_free(fudi.fd_di);
15833 goto erret;
15834 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015835 }
15836 else
15837 /* overwrite existing dict entry */
15838 clear_tv(&fudi.fd_di->di_tv);
15839 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015840 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015841 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015842 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015843 }
15844
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015846 STRCPY(fp->uf_name, name);
15847 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015849 fp->uf_args = newargs;
15850 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000015851#ifdef FEAT_PROFILE
15852 fp->uf_tml_count = NULL;
15853 fp->uf_tml_total = NULL;
15854 fp->uf_tml_self = NULL;
15855 fp->uf_profiling = FALSE;
15856 if (prof_def_func())
15857 func_do_profile(fp);
15858#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015859 fp->uf_varargs = varargs;
15860 fp->uf_flags = flags;
15861 fp->uf_calls = 0;
15862 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015863 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864
15865erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866 ga_clear_strings(&newargs);
15867 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015868ret_free:
15869 vim_free(skip_until);
15870 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873}
15874
15875/*
15876 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000015877 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015879 * flags:
15880 * TFN_INT: internal function name OK
15881 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882 * Advances "pp" to just after the function name (if no error).
15883 */
15884 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015885trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886 char_u **pp;
15887 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015888 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000015889 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015891 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892 char_u *start;
15893 char_u *end;
15894 int lead;
15895 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000015897 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015898
15899 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015900 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000015902
15903 /* Check for hard coded <SNR>: already translated function ID (from a user
15904 * command). */
15905 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
15906 && (*pp)[2] == (int)KE_SNR)
15907 {
15908 *pp += 3;
15909 len = get_id_len(pp) + 3;
15910 return vim_strnsave(start, len);
15911 }
15912
15913 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
15914 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015915 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000015916 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015917 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015918
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015919 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015920 if (end == start)
15921 {
15922 if (!skip)
15923 EMSG(_("E129: Function name required"));
15924 goto theend;
15925 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015926 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015927 {
15928 /*
15929 * Report an invalid expression in braces, unless the expression
15930 * evaluation has been cancelled due to an aborting error, an
15931 * interrupt, or an exception.
15932 */
15933 if (!aborting())
15934 {
15935 if (end != NULL)
15936 EMSG2(_(e_invarg2), start);
15937 }
15938 else
15939 *pp = find_name_end(start, NULL, NULL, TRUE);
15940 goto theend;
15941 }
15942
15943 if (lv.ll_tv != NULL)
15944 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015945 if (fdp != NULL)
15946 {
15947 fdp->fd_dict = lv.ll_dict;
15948 fdp->fd_newkey = lv.ll_newkey;
15949 lv.ll_newkey = NULL;
15950 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015951 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015952 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
15953 {
15954 name = vim_strsave(lv.ll_tv->vval.v_string);
15955 *pp = end;
15956 }
15957 else
15958 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015959 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
15960 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015961 EMSG(_(e_funcref));
15962 else
15963 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015964 name = NULL;
15965 }
15966 goto theend;
15967 }
15968
15969 if (lv.ll_name == NULL)
15970 {
15971 /* Error found, but continue after the function name. */
15972 *pp = end;
15973 goto theend;
15974 }
15975
15976 if (lv.ll_exp_name != NULL)
15977 len = STRLEN(lv.ll_exp_name);
15978 else
Bram Moolenaara7043832005-01-21 11:56:39 +000015979 {
15980 if (lead == 2) /* skip over "s:" */
15981 lv.ll_name += 2;
15982 len = (int)(end - lv.ll_name);
15983 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015984
15985 /*
15986 * Copy the function name to allocated memory.
15987 * Accept <SID>name() inside a script, translate into <SNR>123_name().
15988 * Accept <SNR>123_name() outside a script.
15989 */
15990 if (skip)
15991 lead = 0; /* do nothing */
15992 else if (lead > 0)
15993 {
15994 lead = 3;
15995 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
15996 {
15997 if (current_SID <= 0)
15998 {
15999 EMSG(_(e_usingsid));
16000 goto theend;
16001 }
16002 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
16003 lead += (int)STRLEN(sid_buf);
16004 }
16005 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016006 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016007 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016008 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016009 goto theend;
16010 }
16011 name = alloc((unsigned)(len + lead + 1));
16012 if (name != NULL)
16013 {
16014 if (lead > 0)
16015 {
16016 name[0] = K_SPECIAL;
16017 name[1] = KS_EXTRA;
16018 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000016019 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016020 STRCPY(name + 3, sid_buf);
16021 }
16022 mch_memmove(name + lead, lv.ll_name, (size_t)len);
16023 name[len + lead] = NUL;
16024 }
16025 *pp = end;
16026
16027theend:
16028 clear_lval(&lv);
16029 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030}
16031
16032/*
16033 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
16034 * Return 2 if "p" starts with "s:".
16035 * Return 0 otherwise.
16036 */
16037 static int
16038eval_fname_script(p)
16039 char_u *p;
16040{
16041 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
16042 || STRNICMP(p + 1, "SNR>", 4) == 0))
16043 return 5;
16044 if (p[0] == 's' && p[1] == ':')
16045 return 2;
16046 return 0;
16047}
16048
16049/*
16050 * Return TRUE if "p" starts with "<SID>" or "s:".
16051 * Only works if eval_fname_script() returned non-zero for "p"!
16052 */
16053 static int
16054eval_fname_sid(p)
16055 char_u *p;
16056{
16057 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
16058}
16059
16060/*
16061 * List the head of the function: "name(arg1, arg2)".
16062 */
16063 static void
16064list_func_head(fp, indent)
16065 ufunc_T *fp;
16066 int indent;
16067{
16068 int j;
16069
16070 msg_start();
16071 if (indent)
16072 MSG_PUTS(" ");
16073 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016074 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075 {
16076 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016077 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078 }
16079 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016080 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016082 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083 {
16084 if (j)
16085 MSG_PUTS(", ");
16086 msg_puts(FUNCARG(fp, j));
16087 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016088 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089 {
16090 if (j)
16091 MSG_PUTS(", ");
16092 MSG_PUTS("...");
16093 }
16094 msg_putchar(')');
16095}
16096
16097/*
16098 * Find a function by name, return pointer to it in ufuncs.
16099 * Return NULL for unknown function.
16100 */
16101 static ufunc_T *
16102find_func(name)
16103 char_u *name;
16104{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016105 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016107 hi = hash_find(&func_hashtab, name);
16108 if (!HASHITEM_EMPTY(hi))
16109 return HI2UF(hi);
16110 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111}
16112
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016113/*
16114 * Return TRUE if a function "name" exists.
16115 */
16116 static int
16117function_exists(name)
16118 char_u *name;
16119{
16120 char_u *p = name;
16121 int n = FALSE;
16122
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016123 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016124 if (p != NULL)
16125 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016126 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016127 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016128 else
16129 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016130 vim_free(p);
16131 }
16132 return n;
16133}
16134
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016135/*
16136 * Return TRUE if "name" looks like a builtin function name: starts with a
16137 * lower case letter and doesn't contain a ':'.
16138 */
16139 static int
16140builtin_function(name)
16141 char_u *name;
16142{
16143 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL;
16144}
16145
Bram Moolenaar05159a02005-02-26 23:04:13 +000016146#if defined(FEAT_PROFILE) || defined(PROTO)
16147/*
16148 * Start profiling function "fp".
16149 */
16150 static void
16151func_do_profile(fp)
16152 ufunc_T *fp;
16153{
16154 fp->uf_tm_count = 0;
16155 profile_zero(&fp->uf_tm_self);
16156 profile_zero(&fp->uf_tm_total);
16157 if (fp->uf_tml_count == NULL)
16158 fp->uf_tml_count = (int *)alloc_clear((unsigned)
16159 (sizeof(int) * fp->uf_lines.ga_len));
16160 if (fp->uf_tml_total == NULL)
16161 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
16162 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16163 if (fp->uf_tml_self == NULL)
16164 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
16165 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16166 fp->uf_tml_idx = -1;
16167 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
16168 || fp->uf_tml_self == NULL)
16169 return; /* out of memory */
16170
16171 fp->uf_profiling = TRUE;
16172}
16173
16174/*
16175 * Dump the profiling results for all functions in file "fd".
16176 */
16177 void
16178func_dump_profile(fd)
16179 FILE *fd;
16180{
16181 hashitem_T *hi;
16182 int todo;
16183 ufunc_T *fp;
16184 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000016185 ufunc_T **sorttab;
16186 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016187
16188 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000016189 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
16190
Bram Moolenaar05159a02005-02-26 23:04:13 +000016191 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
16192 {
16193 if (!HASHITEM_EMPTY(hi))
16194 {
16195 --todo;
16196 fp = HI2UF(hi);
16197 if (fp->uf_profiling)
16198 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016199 if (sorttab != NULL)
16200 sorttab[st_len++] = fp;
16201
Bram Moolenaar05159a02005-02-26 23:04:13 +000016202 if (fp->uf_name[0] == K_SPECIAL)
16203 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
16204 else
16205 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
16206 if (fp->uf_tm_count == 1)
16207 fprintf(fd, "Called 1 time\n");
16208 else
16209 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
16210 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
16211 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
16212 fprintf(fd, "\n");
16213 fprintf(fd, "count total (s) self (s)\n");
16214
16215 for (i = 0; i < fp->uf_lines.ga_len; ++i)
16216 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016217 prof_func_line(fd, fp->uf_tml_count[i],
16218 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016219 fprintf(fd, "%s\n", FUNCLINE(fp, i));
16220 }
16221 fprintf(fd, "\n");
16222 }
16223 }
16224 }
Bram Moolenaar73830342005-02-28 22:48:19 +000016225
16226 if (sorttab != NULL && st_len > 0)
16227 {
16228 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16229 prof_total_cmp);
16230 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
16231 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16232 prof_self_cmp);
16233 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
16234 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016235}
Bram Moolenaar73830342005-02-28 22:48:19 +000016236
16237 static void
16238prof_sort_list(fd, sorttab, st_len, title, prefer_self)
16239 FILE *fd;
16240 ufunc_T **sorttab;
16241 int st_len;
16242 char *title;
16243 int prefer_self; /* when equal print only self time */
16244{
16245 int i;
16246 ufunc_T *fp;
16247
16248 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
16249 fprintf(fd, "count total (s) self (s) function\n");
16250 for (i = 0; i < 20 && i < st_len; ++i)
16251 {
16252 fp = sorttab[i];
16253 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
16254 prefer_self);
16255 if (fp->uf_name[0] == K_SPECIAL)
16256 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
16257 else
16258 fprintf(fd, " %s()\n", fp->uf_name);
16259 }
16260 fprintf(fd, "\n");
16261}
16262
16263/*
16264 * Print the count and times for one function or function line.
16265 */
16266 static void
16267prof_func_line(fd, count, total, self, prefer_self)
16268 FILE *fd;
16269 int count;
16270 proftime_T *total;
16271 proftime_T *self;
16272 int prefer_self; /* when equal print only self time */
16273{
16274 if (count > 0)
16275 {
16276 fprintf(fd, "%5d ", count);
16277 if (prefer_self && profile_equal(total, self))
16278 fprintf(fd, " ");
16279 else
16280 fprintf(fd, "%s ", profile_msg(total));
16281 if (!prefer_self && profile_equal(total, self))
16282 fprintf(fd, " ");
16283 else
16284 fprintf(fd, "%s ", profile_msg(self));
16285 }
16286 else
16287 fprintf(fd, " ");
16288}
16289
16290/*
16291 * Compare function for total time sorting.
16292 */
16293 static int
16294#ifdef __BORLANDC__
16295_RTLENTRYF
16296#endif
16297prof_total_cmp(s1, s2)
16298 const void *s1;
16299 const void *s2;
16300{
16301 ufunc_T *p1, *p2;
16302
16303 p1 = *(ufunc_T **)s1;
16304 p2 = *(ufunc_T **)s2;
16305 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
16306}
16307
16308/*
16309 * Compare function for self time sorting.
16310 */
16311 static int
16312#ifdef __BORLANDC__
16313_RTLENTRYF
16314#endif
16315prof_self_cmp(s1, s2)
16316 const void *s1;
16317 const void *s2;
16318{
16319 ufunc_T *p1, *p2;
16320
16321 p1 = *(ufunc_T **)s1;
16322 p2 = *(ufunc_T **)s2;
16323 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
16324}
16325
Bram Moolenaar05159a02005-02-26 23:04:13 +000016326#endif
16327
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016328/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016329 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016330 * Return TRUE if a package was loaded.
16331 */
16332 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016333script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016334 char_u *name;
16335{
16336 char_u *p;
16337 char_u *scriptname;
16338 int ret = FALSE;
16339
16340 /* If there is no colon after name[1] there is no package name. */
16341 p = vim_strchr(name, ':');
16342 if (p == NULL || p <= name + 2)
16343 return FALSE;
16344
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016345 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
16346 scriptname = autoload_name(name);
16347 if (cmd_runtime(scriptname, FALSE) == OK)
16348 ret = TRUE;
16349
16350 vim_free(scriptname);
16351 return ret;
16352}
16353
16354/*
16355 * Return the autoload script name for a function or variable name.
16356 * Returns NULL when out of memory.
16357 */
16358 static char_u *
16359autoload_name(name)
16360 char_u *name;
16361{
16362 char_u *p;
16363 char_u *scriptname;
16364
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016365 /* Get the script file name: replace ':' with '/', append ".vim". */
16366 scriptname = alloc((unsigned)(STRLEN(name) + 14));
16367 if (scriptname == NULL)
16368 return FALSE;
16369 STRCPY(scriptname, "autoload/");
16370 STRCAT(scriptname, name);
16371 *vim_strrchr(scriptname, ':') = NUL;
16372 STRCAT(scriptname, ".vim");
16373 while ((p = vim_strchr(scriptname, ':')) != NULL)
16374 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016375 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016376}
16377
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
16379
16380/*
16381 * Function given to ExpandGeneric() to obtain the list of user defined
16382 * function names.
16383 */
16384 char_u *
16385get_user_func_name(xp, idx)
16386 expand_T *xp;
16387 int idx;
16388{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016389 static long_u done;
16390 static hashitem_T *hi;
16391 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392
16393 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016395 done = 0;
16396 hi = func_hashtab.ht_array;
16397 }
16398 if (done < func_hashtab.ht_used)
16399 {
16400 if (done++ > 0)
16401 ++hi;
16402 while (HASHITEM_EMPTY(hi))
16403 ++hi;
16404 fp = HI2UF(hi);
16405
16406 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
16407 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408
16409 cat_func_name(IObuff, fp);
16410 if (xp->xp_context != EXPAND_USER_FUNC)
16411 {
16412 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016413 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414 STRCAT(IObuff, ")");
16415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416 return IObuff;
16417 }
16418 return NULL;
16419}
16420
16421#endif /* FEAT_CMDL_COMPL */
16422
16423/*
16424 * Copy the function name of "fp" to buffer "buf".
16425 * "buf" must be able to hold the function name plus three bytes.
16426 * Takes care of script-local function names.
16427 */
16428 static void
16429cat_func_name(buf, fp)
16430 char_u *buf;
16431 ufunc_T *fp;
16432{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016433 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016434 {
16435 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016436 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437 }
16438 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016439 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440}
16441
16442/*
16443 * ":delfunction {name}"
16444 */
16445 void
16446ex_delfunction(eap)
16447 exarg_T *eap;
16448{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016449 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450 char_u *p;
16451 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016452 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453
16454 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016455 name = trans_function_name(&p, eap->skip, 0, &fudi);
16456 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016458 {
16459 if (fudi.fd_dict != NULL && !eap->skip)
16460 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016461 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463 if (!ends_excmd(*skipwhite(p)))
16464 {
16465 vim_free(name);
16466 EMSG(_(e_trailing));
16467 return;
16468 }
16469 eap->nextcmd = check_nextcmd(p);
16470 if (eap->nextcmd != NULL)
16471 *p = NUL;
16472
16473 if (!eap->skip)
16474 fp = find_func(name);
16475 vim_free(name);
16476
16477 if (!eap->skip)
16478 {
16479 if (fp == NULL)
16480 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000016481 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482 return;
16483 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016484 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485 {
16486 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
16487 return;
16488 }
16489
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016490 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016492 /* Delete the dict item that refers to the function, it will
16493 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016494 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016496 else
16497 func_free(fp);
16498 }
16499}
16500
16501/*
16502 * Free a function and remove it from the list of functions.
16503 */
16504 static void
16505func_free(fp)
16506 ufunc_T *fp;
16507{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016508 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016509
16510 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016511 ga_clear_strings(&(fp->uf_args));
16512 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000016513#ifdef FEAT_PROFILE
16514 vim_free(fp->uf_tml_count);
16515 vim_free(fp->uf_tml_total);
16516 vim_free(fp->uf_tml_self);
16517#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016518
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016519 /* remove the function from the function hashtable */
16520 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
16521 if (HASHITEM_EMPTY(hi))
16522 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016523 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016524 hash_remove(&func_hashtab, hi);
16525
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016526 vim_free(fp);
16527}
16528
16529/*
16530 * Unreference a Function: decrement the reference count and free it when it
16531 * becomes zero. Only for numbered functions.
16532 */
16533 static void
16534func_unref(name)
16535 char_u *name;
16536{
16537 ufunc_T *fp;
16538
16539 if (name != NULL && isdigit(*name))
16540 {
16541 fp = find_func(name);
16542 if (fp == NULL)
16543 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016544 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016545 {
16546 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016547 * when "uf_calls" becomes zero. */
16548 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016549 func_free(fp);
16550 }
16551 }
16552}
16553
16554/*
16555 * Count a reference to a Function.
16556 */
16557 static void
16558func_ref(name)
16559 char_u *name;
16560{
16561 ufunc_T *fp;
16562
16563 if (name != NULL && isdigit(*name))
16564 {
16565 fp = find_func(name);
16566 if (fp == NULL)
16567 EMSG2(_(e_intern2), "func_ref()");
16568 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016569 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570 }
16571}
16572
16573/*
16574 * Call a user function.
16575 */
16576 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000016577call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578 ufunc_T *fp; /* pointer to function */
16579 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000016580 typval_T *argvars; /* arguments */
16581 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582 linenr_T firstline; /* first line of range */
16583 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000016584 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585{
Bram Moolenaar33570922005-01-25 22:26:29 +000016586 char_u *save_sourcing_name;
16587 linenr_T save_sourcing_lnum;
16588 scid_T save_current_SID;
16589 funccall_T fc;
16590 funccall_T *save_fcp = current_funccal;
16591 int save_did_emsg;
16592 static int depth = 0;
16593 dictitem_T *v;
16594 int fixvar_idx = 0; /* index in fixvar[] */
16595 int i;
16596 int ai;
16597 char_u numbuf[NUMBUFLEN];
16598 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016599#ifdef FEAT_PROFILE
16600 proftime_T wait_start;
16601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602
16603 /* If depth of calling is getting too high, don't execute the function */
16604 if (depth >= p_mfd)
16605 {
16606 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016607 rettv->v_type = VAR_NUMBER;
16608 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609 return;
16610 }
16611 ++depth;
16612
16613 line_breakcheck(); /* check for CTRL-C hit */
16614
Bram Moolenaar33570922005-01-25 22:26:29 +000016615 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016616 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016617 fc.rettv = rettv;
16618 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619 fc.linenr = 0;
16620 fc.returned = FALSE;
16621 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016623 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624 fc.dbg_tick = debug_tick;
16625
Bram Moolenaar33570922005-01-25 22:26:29 +000016626 /*
16627 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
16628 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
16629 * each argument variable and saves a lot of time.
16630 */
16631 /*
16632 * Init l: variables.
16633 */
16634 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000016635 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016636 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016637 /* Set l:self to "selfdict". */
16638 v = &fc.fixvar[fixvar_idx++].var;
16639 STRCPY(v->di_key, "self");
16640 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
16641 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
16642 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016643 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000016644 v->di_tv.vval.v_dict = selfdict;
16645 ++selfdict->dv_refcount;
16646 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016647
Bram Moolenaar33570922005-01-25 22:26:29 +000016648 /*
16649 * Init a: variables.
16650 * Set a:0 to "argcount".
16651 * Set a:000 to a list with room for the "..." arguments.
16652 */
16653 init_var_dict(&fc.l_avars, &fc.l_avars_var);
16654 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016655 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000016656 v = &fc.fixvar[fixvar_idx++].var;
16657 STRCPY(v->di_key, "000");
16658 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16659 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16660 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016661 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016662 v->di_tv.vval.v_list = &fc.l_varlist;
16663 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
16664 fc.l_varlist.lv_refcount = 99999;
16665
16666 /*
16667 * Set a:firstline to "firstline" and a:lastline to "lastline".
16668 * Set a:name to named arguments.
16669 * Set a:N to the "..." arguments.
16670 */
16671 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
16672 (varnumber_T)firstline);
16673 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
16674 (varnumber_T)lastline);
16675 for (i = 0; i < argcount; ++i)
16676 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016677 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016678 if (ai < 0)
16679 /* named argument a:name */
16680 name = FUNCARG(fp, i);
16681 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016682 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016683 /* "..." argument a:1, a:2, etc. */
16684 sprintf((char *)numbuf, "%d", ai + 1);
16685 name = numbuf;
16686 }
16687 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
16688 {
16689 v = &fc.fixvar[fixvar_idx++].var;
16690 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16691 }
16692 else
16693 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016694 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16695 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000016696 if (v == NULL)
16697 break;
16698 v->di_flags = DI_FLAGS_RO;
16699 }
16700 STRCPY(v->di_key, name);
16701 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16702
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016703 /* Note: the values are copied directly to avoid alloc/free.
16704 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016705 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016706 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016707
16708 if (ai >= 0 && ai < MAX_FUNC_ARGS)
16709 {
16710 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
16711 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016712 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016713 }
16714 }
16715
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 /* Don't redraw while executing the function. */
16717 ++RedrawingDisabled;
16718 save_sourcing_name = sourcing_name;
16719 save_sourcing_lnum = sourcing_lnum;
16720 sourcing_lnum = 1;
16721 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016722 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 if (sourcing_name != NULL)
16724 {
16725 if (save_sourcing_name != NULL
16726 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
16727 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
16728 else
16729 STRCPY(sourcing_name, "function ");
16730 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
16731
16732 if (p_verbose >= 12)
16733 {
16734 ++no_wait_return;
16735 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16736 msg_str((char_u *)_("calling %s"), sourcing_name);
16737 if (p_verbose >= 14)
16738 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000016740 char_u numbuf[NUMBUFLEN];
16741 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016742
16743 msg_puts((char_u *)"(");
16744 for (i = 0; i < argcount; ++i)
16745 {
16746 if (i > 0)
16747 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016748 if (argvars[i].v_type == VAR_NUMBER)
16749 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750 else
16751 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016752 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753 buf, MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016755 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 }
16757 }
16758 msg_puts((char_u *)")");
16759 }
16760 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16761 cmdline_row = msg_row;
16762 --no_wait_return;
16763 }
16764 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016765#ifdef FEAT_PROFILE
16766 if (do_profiling)
16767 {
16768 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
16769 func_do_profile(fp);
16770 if (fp->uf_profiling
16771 || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16772 {
16773 ++fp->uf_tm_count;
16774 profile_start(&fp->uf_tm_start);
16775 profile_zero(&fp->uf_tm_children);
16776 }
16777 script_prof_save(&wait_start);
16778 }
16779#endif
16780
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016782 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783 save_did_emsg = did_emsg;
16784 did_emsg = FALSE;
16785
16786 /* call do_cmdline() to execute the lines */
16787 do_cmdline(NULL, get_func_line, (void *)&fc,
16788 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
16789
16790 --RedrawingDisabled;
16791
16792 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016793 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016795 clear_tv(rettv);
16796 rettv->v_type = VAR_NUMBER;
16797 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 }
16799
Bram Moolenaar05159a02005-02-26 23:04:13 +000016800#ifdef FEAT_PROFILE
16801 if (fp->uf_profiling || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16802 {
16803 profile_end(&fp->uf_tm_start);
16804 profile_sub_wait(&wait_start, &fp->uf_tm_start);
16805 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
16806 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
16807 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
16808 if (save_fcp != NULL && &save_fcp->func->uf_profiling)
16809 {
16810 profile_add(&save_fcp->func->uf_tm_children, &fp->uf_tm_start);
16811 profile_add(&save_fcp->func->uf_tml_children, &fp->uf_tm_start);
16812 }
16813 }
16814#endif
16815
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816 /* when being verbose, mention the return value */
16817 if (p_verbose >= 12)
16818 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016819 char_u *sn;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820
16821 ++no_wait_return;
16822 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16823
16824 /* Make sure the output fits in IObuff. */
16825 sn = sourcing_name;
16826 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
16827 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
16828
16829 if (aborting())
16830 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016831 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016833 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016834 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016836 char_u buf[MSG_BUF_LEN];
16837 char_u numbuf[NUMBUFLEN];
16838 char_u *tofree;
16839
16840 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
16841 buf, MSG_BUF_LEN);
16842 smsg((char_u *)_("%s returning %s"), sn, buf);
16843 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 }
16845 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16846 cmdline_row = msg_row;
16847 --no_wait_return;
16848 }
16849
16850 vim_free(sourcing_name);
16851 sourcing_name = save_sourcing_name;
16852 sourcing_lnum = save_sourcing_lnum;
16853 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016854#ifdef FEAT_PROFILE
16855 if (do_profiling)
16856 script_prof_restore(&wait_start);
16857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858
16859 if (p_verbose >= 12 && sourcing_name != NULL)
16860 {
16861 ++no_wait_return;
16862 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16863 msg_str((char_u *)_("continuing in %s"), sourcing_name);
16864 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16865 cmdline_row = msg_row;
16866 --no_wait_return;
16867 }
16868
16869 did_emsg |= save_did_emsg;
16870 current_funccal = save_fcp;
16871
Bram Moolenaar33570922005-01-25 22:26:29 +000016872 /* The a: variables typevals were not alloced, only free the allocated
16873 * variables. */
16874 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
16875
16876 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877 --depth;
16878}
16879
16880/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016881 * Add a number variable "name" to dict "dp" with value "nr".
16882 */
16883 static void
16884add_nr_var(dp, v, name, nr)
16885 dict_T *dp;
16886 dictitem_T *v;
16887 char *name;
16888 varnumber_T nr;
16889{
16890 STRCPY(v->di_key, name);
16891 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16892 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
16893 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016894 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016895 v->di_tv.vval.v_number = nr;
16896}
16897
16898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899 * ":return [expr]"
16900 */
16901 void
16902ex_return(eap)
16903 exarg_T *eap;
16904{
16905 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016906 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 int returning = FALSE;
16908
16909 if (current_funccal == NULL)
16910 {
16911 EMSG(_("E133: :return not inside a function"));
16912 return;
16913 }
16914
16915 if (eap->skip)
16916 ++emsg_skip;
16917
16918 eap->nextcmd = NULL;
16919 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016920 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921 {
16922 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016923 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016925 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926 }
16927 /* It's safer to return also on error. */
16928 else if (!eap->skip)
16929 {
16930 /*
16931 * Return unless the expression evaluation has been cancelled due to an
16932 * aborting error, an interrupt, or an exception.
16933 */
16934 if (!aborting())
16935 returning = do_return(eap, FALSE, TRUE, NULL);
16936 }
16937
16938 /* When skipping or the return gets pending, advance to the next command
16939 * in this line (!returning). Otherwise, ignore the rest of the line.
16940 * Following lines will be ignored by get_func_line(). */
16941 if (returning)
16942 eap->nextcmd = NULL;
16943 else if (eap->nextcmd == NULL) /* no argument */
16944 eap->nextcmd = check_nextcmd(arg);
16945
16946 if (eap->skip)
16947 --emsg_skip;
16948}
16949
16950/*
16951 * Return from a function. Possibly makes the return pending. Also called
16952 * for a pending return at the ":endtry" or after returning from an extra
16953 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000016954 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016955 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 * FALSE when the return gets pending.
16957 */
16958 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016959do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960 exarg_T *eap;
16961 int reanimate;
16962 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016963 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964{
16965 int idx;
16966 struct condstack *cstack = eap->cstack;
16967
16968 if (reanimate)
16969 /* Undo the return. */
16970 current_funccal->returned = FALSE;
16971
16972 /*
16973 * Cleanup (and inactivate) conditionals, but stop when a try conditional
16974 * not in its finally clause (which then is to be executed next) is found.
16975 * In this case, make the ":return" pending for execution at the ":endtry".
16976 * Otherwise, return normally.
16977 */
16978 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
16979 if (idx >= 0)
16980 {
16981 cstack->cs_pending[idx] = CSTP_RETURN;
16982
16983 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016984 /* A pending return again gets pending. "rettv" points to an
16985 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016987 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 else
16989 {
16990 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016991 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016993 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016995 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996 {
16997 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016998 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016999 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000 else
17001 EMSG(_(e_outofmem));
17002 }
17003 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017004 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005
17006 if (reanimate)
17007 {
17008 /* The pending return value could be overwritten by a ":return"
17009 * without argument in a finally clause; reset the default
17010 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017011 current_funccal->rettv->v_type = VAR_NUMBER;
17012 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 }
17014 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017015 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 }
17017 else
17018 {
17019 current_funccal->returned = TRUE;
17020
17021 /* If the return is carried out now, store the return value. For
17022 * a return immediately after reanimation, the value is already
17023 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017024 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017026 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000017027 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017029 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 }
17031 }
17032
17033 return idx < 0;
17034}
17035
17036/*
17037 * Free the variable with a pending return value.
17038 */
17039 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017040discard_pending_return(rettv)
17041 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042{
Bram Moolenaar33570922005-01-25 22:26:29 +000017043 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044}
17045
17046/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017047 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 * is an allocated string. Used by report_pending() for verbose messages.
17049 */
17050 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017051get_return_cmd(rettv)
17052 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017054 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017055 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017056 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017058 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017059 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017060 if (s == NULL)
17061 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017062
17063 STRCPY(IObuff, ":return ");
17064 STRNCPY(IObuff + 8, s, IOSIZE - 8);
17065 if (STRLEN(s) + 8 >= IOSIZE)
17066 STRCPY(IObuff + IOSIZE - 4, "...");
17067 vim_free(tofree);
17068 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017069}
17070
17071/*
17072 * Get next function line.
17073 * Called by do_cmdline() to get the next line.
17074 * Returns allocated string, or NULL for end of function.
17075 */
17076/* ARGSUSED */
17077 char_u *
17078get_func_line(c, cookie, indent)
17079 int c; /* not used */
17080 void *cookie;
17081 int indent; /* not used */
17082{
Bram Moolenaar33570922005-01-25 22:26:29 +000017083 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017084 ufunc_T *fp = fcp->func;
17085 char_u *retval;
17086 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087
17088 /* If breakpoints have been added/deleted need to check for it. */
17089 if (fcp->dbg_tick != debug_tick)
17090 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017091 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092 sourcing_lnum);
17093 fcp->dbg_tick = debug_tick;
17094 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017095#ifdef FEAT_PROFILE
17096 if (do_profiling)
17097 func_line_end(cookie);
17098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017099
Bram Moolenaar05159a02005-02-26 23:04:13 +000017100 gap = &fp->uf_lines;
17101 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102 retval = NULL;
17103 else if (fcp->returned || fcp->linenr >= gap->ga_len)
17104 retval = NULL;
17105 else
17106 {
17107 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
17108 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017109#ifdef FEAT_PROFILE
17110 if (do_profiling)
17111 func_line_start(cookie);
17112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113 }
17114
17115 /* Did we encounter a breakpoint? */
17116 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
17117 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017118 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017120 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121 sourcing_lnum);
17122 fcp->dbg_tick = debug_tick;
17123 }
17124
17125 return retval;
17126}
17127
Bram Moolenaar05159a02005-02-26 23:04:13 +000017128#if defined(FEAT_PROFILE) || defined(PROTO)
17129/*
17130 * Called when starting to read a function line.
17131 * "sourcing_lnum" must be correct!
17132 * When skipping lines it may not actually be executed, but we won't find out
17133 * until later and we need to store the time now.
17134 */
17135 void
17136func_line_start(cookie)
17137 void *cookie;
17138{
17139 funccall_T *fcp = (funccall_T *)cookie;
17140 ufunc_T *fp = fcp->func;
17141
17142 if (fp->uf_profiling && sourcing_lnum >= 1
17143 && sourcing_lnum <= fp->uf_lines.ga_len)
17144 {
17145 fp->uf_tml_idx = sourcing_lnum - 1;
17146 fp->uf_tml_execed = FALSE;
17147 profile_start(&fp->uf_tml_start);
17148 profile_zero(&fp->uf_tml_children);
17149 profile_get_wait(&fp->uf_tml_wait);
17150 }
17151}
17152
17153/*
17154 * Called when actually executing a function line.
17155 */
17156 void
17157func_line_exec(cookie)
17158 void *cookie;
17159{
17160 funccall_T *fcp = (funccall_T *)cookie;
17161 ufunc_T *fp = fcp->func;
17162
17163 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17164 fp->uf_tml_execed = TRUE;
17165}
17166
17167/*
17168 * Called when done with a function line.
17169 */
17170 void
17171func_line_end(cookie)
17172 void *cookie;
17173{
17174 funccall_T *fcp = (funccall_T *)cookie;
17175 ufunc_T *fp = fcp->func;
17176
17177 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17178 {
17179 if (fp->uf_tml_execed)
17180 {
17181 ++fp->uf_tml_count[fp->uf_tml_idx];
17182 profile_end(&fp->uf_tml_start);
17183 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
17184 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
17185 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
17186 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
17187 }
17188 fp->uf_tml_idx = -1;
17189 }
17190}
17191#endif
17192
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193/*
17194 * Return TRUE if the currently active function should be ended, because a
17195 * return was encountered or an error occured. Used inside a ":while".
17196 */
17197 int
17198func_has_ended(cookie)
17199 void *cookie;
17200{
Bram Moolenaar33570922005-01-25 22:26:29 +000017201 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202
17203 /* Ignore the "abort" flag if the abortion behavior has been changed due to
17204 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017205 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206 || fcp->returned);
17207}
17208
17209/*
17210 * return TRUE if cookie indicates a function which "abort"s on errors.
17211 */
17212 int
17213func_has_abort(cookie)
17214 void *cookie;
17215{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017216 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217}
17218
17219#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
17220typedef enum
17221{
17222 VAR_FLAVOUR_DEFAULT,
17223 VAR_FLAVOUR_SESSION,
17224 VAR_FLAVOUR_VIMINFO
17225} var_flavour_T;
17226
17227static var_flavour_T var_flavour __ARGS((char_u *varname));
17228
17229 static var_flavour_T
17230var_flavour(varname)
17231 char_u *varname;
17232{
17233 char_u *p = varname;
17234
17235 if (ASCII_ISUPPER(*p))
17236 {
17237 while (*(++p))
17238 if (ASCII_ISLOWER(*p))
17239 return VAR_FLAVOUR_SESSION;
17240 return VAR_FLAVOUR_VIMINFO;
17241 }
17242 else
17243 return VAR_FLAVOUR_DEFAULT;
17244}
17245#endif
17246
17247#if defined(FEAT_VIMINFO) || defined(PROTO)
17248/*
17249 * Restore global vars that start with a capital from the viminfo file
17250 */
17251 int
17252read_viminfo_varlist(virp, writing)
17253 vir_T *virp;
17254 int writing;
17255{
17256 char_u *tab;
17257 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000017258 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259
17260 if (!writing && (find_viminfo_parameter('!') != NULL))
17261 {
17262 tab = vim_strchr(virp->vir_line + 1, '\t');
17263 if (tab != NULL)
17264 {
17265 *tab++ = '\0'; /* isolate the variable name */
17266 if (*tab == 'S') /* string var */
17267 is_string = TRUE;
17268
17269 tab = vim_strchr(tab, '\t');
17270 if (tab != NULL)
17271 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017272 if (is_string)
17273 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017274 tv.v_type = VAR_STRING;
17275 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277 }
17278 else
17279 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017280 tv.v_type = VAR_NUMBER;
17281 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017283 set_var(virp->vir_line + 1, &tv, FALSE);
17284 if (is_string)
17285 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 }
17287 }
17288 }
17289
17290 return viminfo_readline(virp);
17291}
17292
17293/*
17294 * Write global vars that start with a capital to the viminfo file
17295 */
17296 void
17297write_viminfo_varlist(fp)
17298 FILE *fp;
17299{
Bram Moolenaar33570922005-01-25 22:26:29 +000017300 hashitem_T *hi;
17301 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017302 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017303 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017304 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017305 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017306 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307
17308 if (find_viminfo_parameter('!') == NULL)
17309 return;
17310
17311 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000017312
Bram Moolenaar33570922005-01-25 22:26:29 +000017313 todo = globvarht.ht_used;
17314 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017316 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017318 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017319 this_var = HI2DI(hi);
17320 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017321 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017322 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000017323 {
17324 case VAR_STRING: s = "STR"; break;
17325 case VAR_NUMBER: s = "NUM"; break;
17326 default: continue;
17327 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017328 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017329 p = echo_string(&this_var->di_tv, &tofree, numbuf);
17330 if (p != NULL)
17331 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000017332 vim_free(tofree);
17333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334 }
17335 }
17336}
17337#endif
17338
17339#if defined(FEAT_SESSION) || defined(PROTO)
17340 int
17341store_session_globals(fd)
17342 FILE *fd;
17343{
Bram Moolenaar33570922005-01-25 22:26:29 +000017344 hashitem_T *hi;
17345 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017346 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347 char_u *p, *t;
17348
Bram Moolenaar33570922005-01-25 22:26:29 +000017349 todo = globvarht.ht_used;
17350 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017352 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017354 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017355 this_var = HI2DI(hi);
17356 if ((this_var->di_tv.v_type == VAR_NUMBER
17357 || this_var->di_tv.v_type == VAR_STRING)
17358 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017359 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017360 /* Escape special characters with a backslash. Turn a LF and
17361 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017362 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000017363 (char_u *)"\\\"\n\r");
17364 if (p == NULL) /* out of memory */
17365 break;
17366 for (t = p; *t != NUL; ++t)
17367 if (*t == '\n')
17368 *t = 'n';
17369 else if (*t == '\r')
17370 *t = 'r';
17371 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000017372 this_var->di_key,
17373 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17374 : ' ',
17375 p,
17376 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17377 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000017378 || put_eol(fd) == FAIL)
17379 {
17380 vim_free(p);
17381 return FAIL;
17382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 vim_free(p);
17384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 }
17386 }
17387 return OK;
17388}
17389#endif
17390
17391#endif /* FEAT_EVAL */
17392
17393#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
17394
17395
17396#ifdef WIN3264
17397/*
17398 * Functions for ":8" filename modifier: get 8.3 version of a filename.
17399 */
17400static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17401static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
17402static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17403
17404/*
17405 * Get the short pathname of a file.
17406 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
17407 */
17408 static int
17409get_short_pathname(fnamep, bufp, fnamelen)
17410 char_u **fnamep;
17411 char_u **bufp;
17412 int *fnamelen;
17413{
17414 int l,len;
17415 char_u *newbuf;
17416
17417 len = *fnamelen;
17418
17419 l = GetShortPathName(*fnamep, *fnamep, len);
17420 if (l > len - 1)
17421 {
17422 /* If that doesn't work (not enough space), then save the string
17423 * and try again with a new buffer big enough
17424 */
17425 newbuf = vim_strnsave(*fnamep, l);
17426 if (newbuf == NULL)
17427 return 0;
17428
17429 vim_free(*bufp);
17430 *fnamep = *bufp = newbuf;
17431
17432 l = GetShortPathName(*fnamep,*fnamep,l+1);
17433
17434 /* Really should always succeed, as the buffer is big enough */
17435 }
17436
17437 *fnamelen = l;
17438 return 1;
17439}
17440
17441/*
17442 * Create a short path name. Returns the length of the buffer it needs.
17443 * Doesn't copy over the end of the buffer passed in.
17444 */
17445 static int
17446shortpath_for_invalid_fname(fname, bufp, fnamelen)
17447 char_u **fname;
17448 char_u **bufp;
17449 int *fnamelen;
17450{
17451 char_u *s, *p, *pbuf2, *pbuf3;
17452 char_u ch;
17453 int l,len,len2,plen,slen;
17454
17455 /* Make a copy */
17456 len2 = *fnamelen;
17457 pbuf2 = vim_strnsave(*fname, len2);
17458 pbuf3 = NULL;
17459
17460 s = pbuf2 + len2 - 1; /* Find the end */
17461 slen = 1;
17462 plen = len2;
17463
17464 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017465 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017466 {
17467 --s;
17468 ++slen;
17469 --plen;
17470 }
17471
17472 do
17473 {
17474 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017475 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476 {
17477 --s;
17478 ++slen;
17479 --plen;
17480 }
17481 if (s <= pbuf2)
17482 break;
17483
17484 /* Remeber the character that is about to be blatted */
17485 ch = *s;
17486 *s = 0; /* get_short_pathname requires a null-terminated string */
17487
17488 /* Try it in situ */
17489 p = pbuf2;
17490 if (!get_short_pathname(&p, &pbuf3, &plen))
17491 {
17492 vim_free(pbuf2);
17493 return -1;
17494 }
17495 *s = ch; /* Preserve the string */
17496 } while (plen == 0);
17497
17498 if (plen > 0)
17499 {
17500 /* Remeber the length of the new string. */
17501 *fnamelen = len = plen + slen;
17502 vim_free(*bufp);
17503 if (len > len2)
17504 {
17505 /* If there's not enough space in the currently allocated string,
17506 * then copy it to a buffer big enough.
17507 */
17508 *fname= *bufp = vim_strnsave(p, len);
17509 if (*fname == NULL)
17510 return -1;
17511 }
17512 else
17513 {
17514 /* Transfer pbuf2 to being the main buffer (it's big enough) */
17515 *fname = *bufp = pbuf2;
17516 if (p != pbuf2)
17517 strncpy(*fname, p, plen);
17518 pbuf2 = NULL;
17519 }
17520 /* Concat the next bit */
17521 strncpy(*fname + plen, s, slen);
17522 (*fname)[len] = '\0';
17523 }
17524 vim_free(pbuf3);
17525 vim_free(pbuf2);
17526 return 0;
17527}
17528
17529/*
17530 * Get a pathname for a partial path.
17531 */
17532 static int
17533shortpath_for_partial(fnamep, bufp, fnamelen)
17534 char_u **fnamep;
17535 char_u **bufp;
17536 int *fnamelen;
17537{
17538 int sepcount, len, tflen;
17539 char_u *p;
17540 char_u *pbuf, *tfname;
17541 int hasTilde;
17542
17543 /* Count up the path seperators from the RHS.. so we know which part
17544 * of the path to return.
17545 */
17546 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017547 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548 if (vim_ispathsep(*p))
17549 ++sepcount;
17550
17551 /* Need full path first (use expand_env() to remove a "~/") */
17552 hasTilde = (**fnamep == '~');
17553 if (hasTilde)
17554 pbuf = tfname = expand_env_save(*fnamep);
17555 else
17556 pbuf = tfname = FullName_save(*fnamep, FALSE);
17557
17558 len = tflen = STRLEN(tfname);
17559
17560 if (!get_short_pathname(&tfname, &pbuf, &len))
17561 return -1;
17562
17563 if (len == 0)
17564 {
17565 /* Don't have a valid filename, so shorten the rest of the
17566 * path if we can. This CAN give us invalid 8.3 filenames, but
17567 * there's not a lot of point in guessing what it might be.
17568 */
17569 len = tflen;
17570 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
17571 return -1;
17572 }
17573
17574 /* Count the paths backward to find the beginning of the desired string. */
17575 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017576 {
17577#ifdef FEAT_MBYTE
17578 if (has_mbyte)
17579 p -= mb_head_off(tfname, p);
17580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581 if (vim_ispathsep(*p))
17582 {
17583 if (sepcount == 0 || (hasTilde && sepcount == 1))
17584 break;
17585 else
17586 sepcount --;
17587 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 if (hasTilde)
17590 {
17591 --p;
17592 if (p >= tfname)
17593 *p = '~';
17594 else
17595 return -1;
17596 }
17597 else
17598 ++p;
17599
17600 /* Copy in the string - p indexes into tfname - allocated at pbuf */
17601 vim_free(*bufp);
17602 *fnamelen = (int)STRLEN(p);
17603 *bufp = pbuf;
17604 *fnamep = p;
17605
17606 return 0;
17607}
17608#endif /* WIN3264 */
17609
17610/*
17611 * Adjust a filename, according to a string of modifiers.
17612 * *fnamep must be NUL terminated when called. When returning, the length is
17613 * determined by *fnamelen.
17614 * Returns valid flags.
17615 * When there is an error, *fnamep is set to NULL.
17616 */
17617 int
17618modify_fname(src, usedlen, fnamep, bufp, fnamelen)
17619 char_u *src; /* string with modifiers */
17620 int *usedlen; /* characters after src that are used */
17621 char_u **fnamep; /* file name so far */
17622 char_u **bufp; /* buffer for allocated file name or NULL */
17623 int *fnamelen; /* length of fnamep */
17624{
17625 int valid = 0;
17626 char_u *tail;
17627 char_u *s, *p, *pbuf;
17628 char_u dirname[MAXPATHL];
17629 int c;
17630 int has_fullname = 0;
17631#ifdef WIN3264
17632 int has_shortname = 0;
17633#endif
17634
17635repeat:
17636 /* ":p" - full path/file_name */
17637 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
17638 {
17639 has_fullname = 1;
17640
17641 valid |= VALID_PATH;
17642 *usedlen += 2;
17643
17644 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
17645 if ((*fnamep)[0] == '~'
17646#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
17647 && ((*fnamep)[1] == '/'
17648# ifdef BACKSLASH_IN_FILENAME
17649 || (*fnamep)[1] == '\\'
17650# endif
17651 || (*fnamep)[1] == NUL)
17652
17653#endif
17654 )
17655 {
17656 *fnamep = expand_env_save(*fnamep);
17657 vim_free(*bufp); /* free any allocated file name */
17658 *bufp = *fnamep;
17659 if (*fnamep == NULL)
17660 return -1;
17661 }
17662
17663 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017664 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 {
17666 if (vim_ispathsep(*p)
17667 && p[1] == '.'
17668 && (p[2] == NUL
17669 || vim_ispathsep(p[2])
17670 || (p[2] == '.'
17671 && (p[3] == NUL || vim_ispathsep(p[3])))))
17672 break;
17673 }
17674
17675 /* FullName_save() is slow, don't use it when not needed. */
17676 if (*p != NUL || !vim_isAbsName(*fnamep))
17677 {
17678 *fnamep = FullName_save(*fnamep, *p != NUL);
17679 vim_free(*bufp); /* free any allocated file name */
17680 *bufp = *fnamep;
17681 if (*fnamep == NULL)
17682 return -1;
17683 }
17684
17685 /* Append a path separator to a directory. */
17686 if (mch_isdir(*fnamep))
17687 {
17688 /* Make room for one or two extra characters. */
17689 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
17690 vim_free(*bufp); /* free any allocated file name */
17691 *bufp = *fnamep;
17692 if (*fnamep == NULL)
17693 return -1;
17694 add_pathsep(*fnamep);
17695 }
17696 }
17697
17698 /* ":." - path relative to the current directory */
17699 /* ":~" - path relative to the home directory */
17700 /* ":8" - shortname path - postponed till after */
17701 while (src[*usedlen] == ':'
17702 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
17703 {
17704 *usedlen += 2;
17705 if (c == '8')
17706 {
17707#ifdef WIN3264
17708 has_shortname = 1; /* Postpone this. */
17709#endif
17710 continue;
17711 }
17712 pbuf = NULL;
17713 /* Need full path first (use expand_env() to remove a "~/") */
17714 if (!has_fullname)
17715 {
17716 if (c == '.' && **fnamep == '~')
17717 p = pbuf = expand_env_save(*fnamep);
17718 else
17719 p = pbuf = FullName_save(*fnamep, FALSE);
17720 }
17721 else
17722 p = *fnamep;
17723
17724 has_fullname = 0;
17725
17726 if (p != NULL)
17727 {
17728 if (c == '.')
17729 {
17730 mch_dirname(dirname, MAXPATHL);
17731 s = shorten_fname(p, dirname);
17732 if (s != NULL)
17733 {
17734 *fnamep = s;
17735 if (pbuf != NULL)
17736 {
17737 vim_free(*bufp); /* free any allocated file name */
17738 *bufp = pbuf;
17739 pbuf = NULL;
17740 }
17741 }
17742 }
17743 else
17744 {
17745 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
17746 /* Only replace it when it starts with '~' */
17747 if (*dirname == '~')
17748 {
17749 s = vim_strsave(dirname);
17750 if (s != NULL)
17751 {
17752 *fnamep = s;
17753 vim_free(*bufp);
17754 *bufp = s;
17755 }
17756 }
17757 }
17758 vim_free(pbuf);
17759 }
17760 }
17761
17762 tail = gettail(*fnamep);
17763 *fnamelen = (int)STRLEN(*fnamep);
17764
17765 /* ":h" - head, remove "/file_name", can be repeated */
17766 /* Don't remove the first "/" or "c:\" */
17767 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
17768 {
17769 valid |= VALID_HEAD;
17770 *usedlen += 2;
17771 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017772 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 --tail;
17774 *fnamelen = (int)(tail - *fnamep);
17775#ifdef VMS
17776 if (*fnamelen > 0)
17777 *fnamelen += 1; /* the path separator is part of the path */
17778#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017779 while (tail > s && !after_pathsep(s, tail))
17780 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781 }
17782
17783 /* ":8" - shortname */
17784 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
17785 {
17786 *usedlen += 2;
17787#ifdef WIN3264
17788 has_shortname = 1;
17789#endif
17790 }
17791
17792#ifdef WIN3264
17793 /* Check shortname after we have done 'heads' and before we do 'tails'
17794 */
17795 if (has_shortname)
17796 {
17797 pbuf = NULL;
17798 /* Copy the string if it is shortened by :h */
17799 if (*fnamelen < (int)STRLEN(*fnamep))
17800 {
17801 p = vim_strnsave(*fnamep, *fnamelen);
17802 if (p == 0)
17803 return -1;
17804 vim_free(*bufp);
17805 *bufp = *fnamep = p;
17806 }
17807
17808 /* Split into two implementations - makes it easier. First is where
17809 * there isn't a full name already, second is where there is.
17810 */
17811 if (!has_fullname && !vim_isAbsName(*fnamep))
17812 {
17813 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
17814 return -1;
17815 }
17816 else
17817 {
17818 int l;
17819
17820 /* Simple case, already have the full-name
17821 * Nearly always shorter, so try first time. */
17822 l = *fnamelen;
17823 if (!get_short_pathname(fnamep, bufp, &l))
17824 return -1;
17825
17826 if (l == 0)
17827 {
17828 /* Couldn't find the filename.. search the paths.
17829 */
17830 l = *fnamelen;
17831 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
17832 return -1;
17833 }
17834 *fnamelen = l;
17835 }
17836 }
17837#endif /* WIN3264 */
17838
17839 /* ":t" - tail, just the basename */
17840 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
17841 {
17842 *usedlen += 2;
17843 *fnamelen -= (int)(tail - *fnamep);
17844 *fnamep = tail;
17845 }
17846
17847 /* ":e" - extension, can be repeated */
17848 /* ":r" - root, without extension, can be repeated */
17849 while (src[*usedlen] == ':'
17850 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
17851 {
17852 /* find a '.' in the tail:
17853 * - for second :e: before the current fname
17854 * - otherwise: The last '.'
17855 */
17856 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
17857 s = *fnamep - 2;
17858 else
17859 s = *fnamep + *fnamelen - 1;
17860 for ( ; s > tail; --s)
17861 if (s[0] == '.')
17862 break;
17863 if (src[*usedlen + 1] == 'e') /* :e */
17864 {
17865 if (s > tail)
17866 {
17867 *fnamelen += (int)(*fnamep - (s + 1));
17868 *fnamep = s + 1;
17869#ifdef VMS
17870 /* cut version from the extension */
17871 s = *fnamep + *fnamelen - 1;
17872 for ( ; s > *fnamep; --s)
17873 if (s[0] == ';')
17874 break;
17875 if (s > *fnamep)
17876 *fnamelen = s - *fnamep;
17877#endif
17878 }
17879 else if (*fnamep <= tail)
17880 *fnamelen = 0;
17881 }
17882 else /* :r */
17883 {
17884 if (s > tail) /* remove one extension */
17885 *fnamelen = (int)(s - *fnamep);
17886 }
17887 *usedlen += 2;
17888 }
17889
17890 /* ":s?pat?foo?" - substitute */
17891 /* ":gs?pat?foo?" - global substitute */
17892 if (src[*usedlen] == ':'
17893 && (src[*usedlen + 1] == 's'
17894 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
17895 {
17896 char_u *str;
17897 char_u *pat;
17898 char_u *sub;
17899 int sep;
17900 char_u *flags;
17901 int didit = FALSE;
17902
17903 flags = (char_u *)"";
17904 s = src + *usedlen + 2;
17905 if (src[*usedlen + 1] == 'g')
17906 {
17907 flags = (char_u *)"g";
17908 ++s;
17909 }
17910
17911 sep = *s++;
17912 if (sep)
17913 {
17914 /* find end of pattern */
17915 p = vim_strchr(s, sep);
17916 if (p != NULL)
17917 {
17918 pat = vim_strnsave(s, (int)(p - s));
17919 if (pat != NULL)
17920 {
17921 s = p + 1;
17922 /* find end of substitution */
17923 p = vim_strchr(s, sep);
17924 if (p != NULL)
17925 {
17926 sub = vim_strnsave(s, (int)(p - s));
17927 str = vim_strnsave(*fnamep, *fnamelen);
17928 if (sub != NULL && str != NULL)
17929 {
17930 *usedlen = (int)(p + 1 - src);
17931 s = do_string_sub(str, pat, sub, flags);
17932 if (s != NULL)
17933 {
17934 *fnamep = s;
17935 *fnamelen = (int)STRLEN(s);
17936 vim_free(*bufp);
17937 *bufp = s;
17938 didit = TRUE;
17939 }
17940 }
17941 vim_free(sub);
17942 vim_free(str);
17943 }
17944 vim_free(pat);
17945 }
17946 }
17947 /* after using ":s", repeat all the modifiers */
17948 if (didit)
17949 goto repeat;
17950 }
17951 }
17952
17953 return valid;
17954}
17955
17956/*
17957 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
17958 * "flags" can be "g" to do a global substitute.
17959 * Returns an allocated string, NULL for error.
17960 */
17961 char_u *
17962do_string_sub(str, pat, sub, flags)
17963 char_u *str;
17964 char_u *pat;
17965 char_u *sub;
17966 char_u *flags;
17967{
17968 int sublen;
17969 regmatch_T regmatch;
17970 int i;
17971 int do_all;
17972 char_u *tail;
17973 garray_T ga;
17974 char_u *ret;
17975 char_u *save_cpo;
17976
17977 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
17978 save_cpo = p_cpo;
17979 p_cpo = (char_u *)"";
17980
17981 ga_init2(&ga, 1, 200);
17982
17983 do_all = (flags[0] == 'g');
17984
17985 regmatch.rm_ic = p_ic;
17986 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17987 if (regmatch.regprog != NULL)
17988 {
17989 tail = str;
17990 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
17991 {
17992 /*
17993 * Get some space for a temporary buffer to do the substitution
17994 * into. It will contain:
17995 * - The text up to where the match is.
17996 * - The substituted text.
17997 * - The text after the match.
17998 */
17999 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
18000 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
18001 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
18002 {
18003 ga_clear(&ga);
18004 break;
18005 }
18006
18007 /* copy the text up to where the match is */
18008 i = (int)(regmatch.startp[0] - tail);
18009 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
18010 /* add the substituted text */
18011 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
18012 + ga.ga_len + i, TRUE, TRUE, FALSE);
18013 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014 /* avoid getting stuck on a match with an empty string */
18015 if (tail == regmatch.endp[0])
18016 {
18017 if (*tail == NUL)
18018 break;
18019 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
18020 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021 }
18022 else
18023 {
18024 tail = regmatch.endp[0];
18025 if (*tail == NUL)
18026 break;
18027 }
18028 if (!do_all)
18029 break;
18030 }
18031
18032 if (ga.ga_data != NULL)
18033 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
18034
18035 vim_free(regmatch.regprog);
18036 }
18037
18038 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
18039 ga_clear(&ga);
18040 p_cpo = save_cpo;
18041
18042 return ret;
18043}
18044
18045#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */