blob: f3e31e2812ebfbaa880bdd2bd9722123e73bb660 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000126 * Array to hold the hashtab with variables local to each sourced script.
127 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000129typedef struct
130{
131 dictitem_T sv_var;
132 dict_T sv_dict;
133} scriptvar_T;
134
135static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
136#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
137#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
139static int echo_attr = 0; /* attributes used for ":echo" */
140
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000141/* Values for trans_function_name() argument: */
142#define TFN_INT 1 /* internal function name OK */
143#define TFN_QUIET 2 /* no error messages */
144
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145/*
146 * Structure to hold info for a user function.
147 */
148typedef struct ufunc ufunc_T;
149
150struct ufunc
151{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000152 int uf_varargs; /* variable nr of arguments */
153 int uf_flags;
154 int uf_calls; /* nr of active calls */
155 garray_T uf_args; /* arguments */
156 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000157#ifdef FEAT_PROFILE
158 int uf_profiling; /* TRUE when func is being profiled */
159 /* profiling the function as a whole */
160 int uf_tm_count; /* nr of calls */
161 proftime_T uf_tm_total; /* time spend in function + children */
162 proftime_T uf_tm_self; /* time spend in function itself */
163 proftime_T uf_tm_start; /* time at function call */
164 proftime_T uf_tm_children; /* time spent in children this call */
165 /* profiling the function per line */
166 int *uf_tml_count; /* nr of times line was executed */
167 proftime_T *uf_tml_total; /* time spend in a line + children */
168 proftime_T *uf_tml_self; /* time spend in a line itself */
169 proftime_T uf_tml_start; /* start time for current line */
170 proftime_T uf_tml_children; /* time spent in children for this line */
171 proftime_T uf_tml_wait; /* start wait time for current line */
172 int uf_tml_idx; /* index of line being timed; -1 if none */
173 int uf_tml_execed; /* line being timed was executed */
174#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000175 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000177 int uf_refcount; /* for numbered function: reference count */
178 char_u uf_name[1]; /* name of function (actually longer); can
179 start with <SNR>123_ (<SNR> is K_SPECIAL
180 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181};
182
183/* function flags */
184#define FC_ABORT 1 /* abort function on error */
185#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000186#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
188/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000189 * All user-defined functions are found in this hash table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000191static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193/* From user function to hashitem and back. */
194static ufunc_T dumuf;
195#define UF2HIKEY(fp) ((fp)->uf_name)
196#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
197#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
198
199#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
200#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
Bram Moolenaar33570922005-01-25 22:26:29 +0000202#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
203#define VAR_SHORT_LEN 20 /* short variable name length */
204#define FIXVAR_CNT 12 /* number of fixed variables */
205
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar33570922005-01-25 22:26:29 +0000207typedef struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208{
209 ufunc_T *func; /* function being called */
210 int linenr; /* next line to be executed */
211 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212 struct /* fixed variables for arguments */
213 {
214 dictitem_T var; /* variable (without room for name) */
215 char_u room[VAR_SHORT_LEN]; /* room for the name */
216 } fixvar[FIXVAR_CNT];
217 dict_T l_vars; /* l: local function variables */
218 dictitem_T l_vars_var; /* variable for l: scope */
219 dict_T l_avars; /* a: argument variables */
220 dictitem_T l_avars_var; /* variable for a: scope */
221 list_T l_varlist; /* list for a:000 */
222 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
223 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 linenr_T breakpoint; /* next line with breakpoint or zero */
225 int dbg_tick; /* debug_tick when breakpoint was set */
226 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000227#ifdef FEAT_PROFILE
228 proftime_T prof_child; /* time spent in a child */
229#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000230} funccall_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
232/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000233 * Info used by a ":for" loop.
234 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000235typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000236{
237 int fi_semicolon; /* TRUE if ending in '; var]' */
238 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 listwatch_T fi_lw; /* keep an eye on the item used. */
240 list_T *fi_list; /* list being used */
241} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000242
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000243/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000244 * Struct used by trans_function_name()
245 */
246typedef struct
247{
Bram Moolenaar33570922005-01-25 22:26:29 +0000248 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000249 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250 dictitem_T *fd_di; /* Dictionary item used */
251} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000252
Bram Moolenaara7043832005-01-21 11:56:39 +0000253
254/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000255 * Array to hold the value of v: variables.
256 * The value is in a dictitem, so that it can also be used in the v: scope.
257 * The reason to use this table anyway is for very quick access to the
258 * variables with the VV_ defines.
259 */
260#include "version.h"
261
262/* values for vv_flags: */
263#define VV_COMPAT 1 /* compatible, also used without "v:" */
264#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000265#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000267#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000268
269static struct vimvar
270{
271 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000272 dictitem_T vv_di; /* value and name for key */
273 char vv_filler[16]; /* space for LONGEST name below!!! */
274 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
275} vimvars[VV_LEN] =
276{
277 /*
278 * The order here must match the VV_ defines in vim.h!
279 * Initializing a union does not work, leave tv.vval empty to get zero's.
280 */
281 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
282 {VV_NAME("count1", VAR_NUMBER), VV_RO},
283 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
284 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
285 {VV_NAME("warningmsg", VAR_STRING), 0},
286 {VV_NAME("statusmsg", VAR_STRING), 0},
287 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
288 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
289 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
290 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
291 {VV_NAME("termresponse", VAR_STRING), VV_RO},
292 {VV_NAME("fname", VAR_STRING), VV_RO},
293 {VV_NAME("lang", VAR_STRING), VV_RO},
294 {VV_NAME("lc_time", VAR_STRING), VV_RO},
295 {VV_NAME("ctype", VAR_STRING), VV_RO},
296 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
297 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
298 {VV_NAME("fname_in", VAR_STRING), VV_RO},
299 {VV_NAME("fname_out", VAR_STRING), VV_RO},
300 {VV_NAME("fname_new", VAR_STRING), VV_RO},
301 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
302 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
303 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
304 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
305 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
306 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
307 {VV_NAME("progname", VAR_STRING), VV_RO},
308 {VV_NAME("servername", VAR_STRING), VV_RO},
309 {VV_NAME("dying", VAR_NUMBER), VV_RO},
310 {VV_NAME("exception", VAR_STRING), VV_RO},
311 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
312 {VV_NAME("register", VAR_STRING), VV_RO},
313 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
314 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000315 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
316 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000317 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000318 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
319 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000320 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
321 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
322 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
323 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
324 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000325};
326
327/* shorthand */
328#define vv_type vv_di.di_tv.v_type
329#define vv_nr vv_di.di_tv.vval.v_number
330#define vv_str vv_di.di_tv.vval.v_string
331#define vv_tv vv_di.di_tv
332
333/*
334 * The v: variables are stored in dictionary "vimvardict".
335 * "vimvars_var" is the variable that is used for the "l:" scope.
336 */
337static dict_T vimvardict;
338static dictitem_T vimvars_var;
339#define vimvarht vimvardict.dv_hashtab
340
341static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
342static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
343static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
344static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
345static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
346static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
347static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
348static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000349static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000350static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
351static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
352static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
353static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
354static list_T *list_alloc __ARGS((void));
355static void list_unref __ARGS((list_T *l));
356static void list_free __ARGS((list_T *l));
357static listitem_T *listitem_alloc __ARGS((void));
358static void listitem_free __ARGS((listitem_T *item));
359static void listitem_remove __ARGS((list_T *l, listitem_T *item));
360static long list_len __ARGS((list_T *l));
361static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
362static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
363static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
364static int string_isa_number __ARGS((char_u *s));
365static listitem_T *list_find __ARGS((list_T *l, long n));
366static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000367static void list_append __ARGS((list_T *l, listitem_T *item));
368static int list_append_tv __ARGS((list_T *l, typval_T *tv));
369static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
370static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
371static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000372static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000373static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
374static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000375static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar33570922005-01-25 22:26:29 +0000376
Bram Moolenaar33570922005-01-25 22:26:29 +0000377static void dict_unref __ARGS((dict_T *d));
378static void dict_free __ARGS((dict_T *d));
379static dictitem_T *dictitem_alloc __ARGS((char_u *key));
380static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
381static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
382static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000383static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000384static int dict_add __ARGS((dict_T *d, dictitem_T *item));
385static long dict_len __ARGS((dict_T *d));
386static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
387static char_u *dict2string __ARGS((typval_T *tv));
388static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
389
390static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
391static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
392static char_u *string_quote __ARGS((char_u *str, int function));
393static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
394static int find_internal_func __ARGS((char_u *name));
395static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
396static 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));
397static 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 +0000398static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000399
400static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
401static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
402static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
403static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
404static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
405static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
406static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
407static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
408static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
409static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
410static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
411static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
412static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
413static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
414static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
415static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
416static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
417static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
418static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
419static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
420static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
421static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
422static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
423static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
424static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
425static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
426static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
427static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
428static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
429static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
430static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
431static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
432static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
433static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
434static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
435static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
436static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
437static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
438static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
439static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
440static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
441static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
442static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
443static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
444static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000463static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000491static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000508static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000512#ifdef vim_mkdir
513static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
514#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000520static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000538static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000543static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
546#ifdef HAVE_STRFTIME
547static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
548#endif
549static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000561static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000577static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578
579static win_T *find_win_by_nr __ARGS((typval_T *vp));
580static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
581static int get_env_len __ARGS((char_u **arg));
582static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000583static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000584static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
585#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
586#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
587 valid character */
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000589static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000590static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
591static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static typval_T *alloc_tv __ARGS((void));
593static typval_T *alloc_string_tv __ARGS((char_u *string));
594static void free_tv __ARGS((typval_T *varp));
595static void clear_tv __ARGS((typval_T *varp));
596static void init_tv __ARGS((typval_T *varp));
597static long get_tv_number __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000598static long get_tv_number_chk __ARGS((typval_T *varp, int *denote));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
600static char_u *get_tv_string __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000601static char_u *get_tv_string_chk __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000603static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000604static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000605static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
607static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
608static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
609static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
610static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
611static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
612static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000613static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000615static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
617static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
618static int eval_fname_script __ARGS((char_u *p));
619static int eval_fname_sid __ARGS((char_u *p));
620static void list_func_head __ARGS((ufunc_T *fp, int indent));
621static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
622static ufunc_T *find_func __ARGS((char_u *name));
623static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000624static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000625#ifdef FEAT_PROFILE
626static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000627static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
628static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
629static int
630# ifdef __BORLANDC__
631 _RTLENTRYF
632# endif
633 prof_total_cmp __ARGS((const void *s1, const void *s2));
634static int
635# ifdef __BORLANDC__
636 _RTLENTRYF
637# endif
638 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000639#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000640static int script_autoload __ARGS((char_u *name));
641static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000642static void func_free __ARGS((ufunc_T *fp));
643static void func_unref __ARGS((char_u *name));
644static void func_ref __ARGS((char_u *name));
645static 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));
646static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
647
648static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
649
650static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
651static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
652static char_u *skip_var_one __ARGS((char_u *arg));
653static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
654static void list_glob_vars __ARGS((void));
655static void list_buf_vars __ARGS((void));
656static void list_win_vars __ARGS((void));
657static void list_vim_vars __ARGS((void));
658static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
659static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
660static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000661static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static void clear_lval __ARGS((lval_T *lp));
663static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
664static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
665static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
666static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
667static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000668static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000670static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
671static void item_lock __ARGS((typval_T *tv, int deep, int lock));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000672static int tv_islocked __ARGS((typval_T *tv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000674/* Character used as separated in autoload function/variable names. */
675#define AUTOLOAD_CHAR '#'
676
Bram Moolenaar33570922005-01-25 22:26:29 +0000677/*
678 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000679 */
680 void
681eval_init()
682{
Bram Moolenaar33570922005-01-25 22:26:29 +0000683 int i;
684 struct vimvar *p;
685
686 init_var_dict(&globvardict, &globvars_var);
687 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000688 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000689 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000690
691 for (i = 0; i < VV_LEN; ++i)
692 {
693 p = &vimvars[i];
694 STRCPY(p->vv_di.di_key, p->vv_name);
695 if (p->vv_flags & VV_RO)
696 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
697 else if (p->vv_flags & VV_RO_SBX)
698 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
699 else
700 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000701
702 /* add to v: scope dict, unless the value is not always available */
703 if (p->vv_type != VAR_UNKNOWN)
704 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000705 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000706 /* add to compat scope dict */
707 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000708 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000709}
710
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000711#if defined(EXITFREE) || defined(PROTO)
712 void
713eval_clear()
714{
715 int i;
716 struct vimvar *p;
717
718 for (i = 0; i < VV_LEN; ++i)
719 {
720 p = &vimvars[i];
721 if (p->vv_di.di_tv.v_type == VAR_STRING)
722 vim_free(p->vv_di.di_tv.vval.v_string);
723 }
724 hash_clear(&vimvarht);
725 hash_clear(&compat_hashtab);
726
727 /* script-local variables */
728 for (i = 1; i <= ga_scripts.ga_len; ++i)
729 vars_clear(&SCRIPT_VARS(i));
730 ga_clear(&ga_scripts);
731
732 /* global variables */
733 vars_clear(&globvarht);
734}
735#endif
736
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 * Return the name of the executed function.
739 */
740 char_u *
741func_name(cookie)
742 void *cookie;
743{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000744 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745}
746
747/*
748 * Return the address holding the next breakpoint line for a funccall cookie.
749 */
750 linenr_T *
751func_breakpoint(cookie)
752 void *cookie;
753{
Bram Moolenaar33570922005-01-25 22:26:29 +0000754 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755}
756
757/*
758 * Return the address holding the debug tick for a funccall cookie.
759 */
760 int *
761func_dbg_tick(cookie)
762 void *cookie;
763{
Bram Moolenaar33570922005-01-25 22:26:29 +0000764 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765}
766
767/*
768 * Return the nesting level for a funccall cookie.
769 */
770 int
771func_level(cookie)
772 void *cookie;
773{
Bram Moolenaar33570922005-01-25 22:26:29 +0000774 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775}
776
777/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000778funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
780/*
781 * Return TRUE when a function was ended by a ":return" command.
782 */
783 int
784current_func_returned()
785{
786 return current_funccal->returned;
787}
788
789
790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 * Set an internal variable to a string value. Creates the variable if it does
792 * not already exist.
793 */
794 void
795set_internal_string_var(name, value)
796 char_u *name;
797 char_u *value;
798{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000799 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000800 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
802 val = vim_strsave(value);
803 if (val != NULL)
804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000805 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000806 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000808 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000809 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
811 }
812}
813
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000814static lval_T *redir_lval = NULL;
815static char_u *redir_endp = NULL;
816static char_u *redir_varname = NULL;
817
818/*
819 * Start recording command output to a variable
820 * Returns OK if successfully completed the setup. FAIL otherwise.
821 */
822 int
823var_redir_start(name, append)
824 char_u *name;
825 int append; /* append to an existing variable */
826{
827 int save_emsg;
828 int err;
829 typval_T tv;
830
831 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000832 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000833 {
834 EMSG(_(e_invarg));
835 return FAIL;
836 }
837
838 redir_varname = vim_strsave(name);
839 if (redir_varname == NULL)
840 return FAIL;
841
842 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
843 if (redir_lval == NULL)
844 {
845 var_redir_stop();
846 return FAIL;
847 }
848
849 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000850 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
851 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000852 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
853 {
854 if (redir_endp != NULL && *redir_endp != NUL)
855 /* Trailing characters are present after the variable name */
856 EMSG(_(e_trailing));
857 else
858 EMSG(_(e_invarg));
859 var_redir_stop();
860 return FAIL;
861 }
862
863 /* check if we can write to the variable: set it to or append an empty
864 * string */
865 save_emsg = did_emsg;
866 did_emsg = FALSE;
867 tv.v_type = VAR_STRING;
868 tv.vval.v_string = (char_u *)"";
869 if (append)
870 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
871 else
872 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
873 err = did_emsg;
874 did_emsg += save_emsg;
875 if (err)
876 {
877 var_redir_stop();
878 return FAIL;
879 }
880 if (redir_lval->ll_newkey != NULL)
881 {
882 /* Dictionary item was created, don't do it again. */
883 vim_free(redir_lval->ll_newkey);
884 redir_lval->ll_newkey = NULL;
885 }
886
887 return OK;
888}
889
890/*
891 * Append "value[len]" to the variable set by var_redir_start().
892 */
893 void
894var_redir_str(value, len)
895 char_u *value;
896 int len;
897{
898 char_u *val;
899 typval_T tv;
900 int save_emsg;
901 int err;
902
903 if (redir_lval == NULL)
904 return;
905
906 if (len == -1)
907 /* Append the entire string */
908 val = vim_strsave(value);
909 else
910 /* Append only the specified number of characters */
911 val = vim_strnsave(value, len);
912 if (val == NULL)
913 return;
914
915 tv.v_type = VAR_STRING;
916 tv.vval.v_string = val;
917
918 save_emsg = did_emsg;
919 did_emsg = FALSE;
920 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
921 err = did_emsg;
922 did_emsg += save_emsg;
923 if (err)
924 var_redir_stop();
925
926 vim_free(tv.vval.v_string);
927}
928
929/*
930 * Stop redirecting command output to a variable.
931 */
932 void
933var_redir_stop()
934{
935 if (redir_lval != NULL)
936 {
937 clear_lval(redir_lval);
938 vim_free(redir_lval);
939 redir_lval = NULL;
940 }
941 vim_free(redir_varname);
942 redir_varname = NULL;
943}
944
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945# if defined(FEAT_MBYTE) || defined(PROTO)
946 int
947eval_charconvert(enc_from, enc_to, fname_from, fname_to)
948 char_u *enc_from;
949 char_u *enc_to;
950 char_u *fname_from;
951 char_u *fname_to;
952{
953 int err = FALSE;
954
955 set_vim_var_string(VV_CC_FROM, enc_from, -1);
956 set_vim_var_string(VV_CC_TO, enc_to, -1);
957 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
958 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
959 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
960 err = TRUE;
961 set_vim_var_string(VV_CC_FROM, NULL, -1);
962 set_vim_var_string(VV_CC_TO, NULL, -1);
963 set_vim_var_string(VV_FNAME_IN, NULL, -1);
964 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
965
966 if (err)
967 return FAIL;
968 return OK;
969}
970# endif
971
972# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
973 int
974eval_printexpr(fname, args)
975 char_u *fname;
976 char_u *args;
977{
978 int err = FALSE;
979
980 set_vim_var_string(VV_FNAME_IN, fname, -1);
981 set_vim_var_string(VV_CMDARG, args, -1);
982 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
983 err = TRUE;
984 set_vim_var_string(VV_FNAME_IN, NULL, -1);
985 set_vim_var_string(VV_CMDARG, NULL, -1);
986
987 if (err)
988 {
989 mch_remove(fname);
990 return FAIL;
991 }
992 return OK;
993}
994# endif
995
996# if defined(FEAT_DIFF) || defined(PROTO)
997 void
998eval_diff(origfile, newfile, outfile)
999 char_u *origfile;
1000 char_u *newfile;
1001 char_u *outfile;
1002{
1003 int err = FALSE;
1004
1005 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1006 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1007 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1008 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1009 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1010 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1011 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1012}
1013
1014 void
1015eval_patch(origfile, difffile, outfile)
1016 char_u *origfile;
1017 char_u *difffile;
1018 char_u *outfile;
1019{
1020 int err;
1021
1022 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1023 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1024 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1025 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1026 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1027 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1028 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1029}
1030# endif
1031
1032/*
1033 * Top level evaluation function, returning a boolean.
1034 * Sets "error" to TRUE if there was an error.
1035 * Return TRUE or FALSE.
1036 */
1037 int
1038eval_to_bool(arg, error, nextcmd, skip)
1039 char_u *arg;
1040 int *error;
1041 char_u **nextcmd;
1042 int skip; /* only parse, don't execute */
1043{
Bram Moolenaar33570922005-01-25 22:26:29 +00001044 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 int retval = FALSE;
1046
1047 if (skip)
1048 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001049 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 else
1052 {
1053 *error = FALSE;
1054 if (!skip)
1055 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001056 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001057 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
1059 }
1060 if (skip)
1061 --emsg_skip;
1062
1063 return retval;
1064}
1065
1066/*
1067 * Top level evaluation function, returning a string. If "skip" is TRUE,
1068 * only parsing to "nextcmd" is done, without reporting errors. Return
1069 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1070 */
1071 char_u *
1072eval_to_string_skip(arg, nextcmd, skip)
1073 char_u *arg;
1074 char_u **nextcmd;
1075 int skip; /* only parse, don't execute */
1076{
Bram Moolenaar33570922005-01-25 22:26:29 +00001077 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 char_u *retval;
1079
1080 if (skip)
1081 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001082 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 retval = NULL;
1084 else
1085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001086 retval = vim_strsave(get_tv_string(&tv));
1087 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 }
1089 if (skip)
1090 --emsg_skip;
1091
1092 return retval;
1093}
1094
1095/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001096 * Skip over an expression at "*pp".
1097 * Return FAIL for an error, OK otherwise.
1098 */
1099 int
1100skip_expr(pp)
1101 char_u **pp;
1102{
Bram Moolenaar33570922005-01-25 22:26:29 +00001103 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001104
1105 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001106 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001107}
1108
1109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 * Top level evaluation function, returning a string.
1111 * Return pointer to allocated memory, or NULL for failure.
1112 */
1113 char_u *
1114eval_to_string(arg, nextcmd)
1115 char_u *arg;
1116 char_u **nextcmd;
1117{
Bram Moolenaar33570922005-01-25 22:26:29 +00001118 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 char_u *retval;
1120
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001121 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 retval = NULL;
1123 else
1124 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001125 retval = vim_strsave(get_tv_string(&tv));
1126 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 }
1128
1129 return retval;
1130}
1131
1132/*
1133 * Call eval_to_string() with "sandbox" set and not using local variables.
1134 */
1135 char_u *
1136eval_to_string_safe(arg, nextcmd)
1137 char_u *arg;
1138 char_u **nextcmd;
1139{
1140 char_u *retval;
1141 void *save_funccalp;
1142
1143 save_funccalp = save_funccal();
1144 ++sandbox;
1145 retval = eval_to_string(arg, nextcmd);
1146 --sandbox;
1147 restore_funccal(save_funccalp);
1148 return retval;
1149}
1150
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151/*
1152 * Top level evaluation function, returning a number.
1153 * Evaluates "expr" silently.
1154 * Returns -1 for an error.
1155 */
1156 int
1157eval_to_number(expr)
1158 char_u *expr;
1159{
Bram Moolenaar33570922005-01-25 22:26:29 +00001160 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001162 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
1164 ++emsg_off;
1165
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 retval = -1;
1168 else
1169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001170 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001171 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 }
1173 --emsg_off;
1174
1175 return retval;
1176}
1177
1178#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1179/*
1180 * Call some vimL function and return the result as a string
1181 * Uses argv[argc] for the function arguments.
1182 */
1183 char_u *
1184call_vim_function(func, argc, argv, safe)
1185 char_u *func;
1186 int argc;
1187 char_u **argv;
1188 int safe; /* use the sandbox */
1189{
1190 char_u *retval = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001191 typval_T rettv;
1192 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 long n;
1194 int len;
1195 int i;
1196 int doesrange;
1197 void *save_funccalp = NULL;
1198
Bram Moolenaar33570922005-01-25 22:26:29 +00001199 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 if (argvars == NULL)
1201 return NULL;
1202
1203 for (i = 0; i < argc; i++)
1204 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001205 /* Pass a NULL or empty argument as an empty string */
1206 if (argv[i] == NULL || *argv[i] == NUL)
1207 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001208 argvars[i].v_type = VAR_STRING;
1209 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001210 continue;
1211 }
1212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 /* Recognize a number argument, the others must be strings. */
1214 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1215 if (len != 0 && len == (int)STRLEN(argv[i]))
1216 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001217 argvars[i].v_type = VAR_NUMBER;
1218 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 }
1220 else
1221 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001222 argvars[i].v_type = VAR_STRING;
1223 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
1225 }
1226
1227 if (safe)
1228 {
1229 save_funccalp = save_funccal();
1230 ++sandbox;
1231 }
1232
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001233 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1234 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001236 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001237 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001239 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 vim_free(argvars);
1241
1242 if (safe)
1243 {
1244 --sandbox;
1245 restore_funccal(save_funccalp);
1246 }
1247 return retval;
1248}
1249#endif
1250
1251/*
1252 * Save the current function call pointer, and set it to NULL.
1253 * Used when executing autocommands and for ":source".
1254 */
1255 void *
1256save_funccal()
1257{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001258 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 current_funccal = NULL;
1261 return (void *)fc;
1262}
1263
1264 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001265restore_funccal(vfc)
1266 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001268 funccall_T *fc = (funccall_T *)vfc;
1269
1270 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271}
1272
Bram Moolenaar05159a02005-02-26 23:04:13 +00001273#if defined(FEAT_PROFILE) || defined(PROTO)
1274/*
1275 * Prepare profiling for entering a child or something else that is not
1276 * counted for the script/function itself.
1277 * Should always be called in pair with prof_child_exit().
1278 */
1279 void
1280prof_child_enter(tm)
1281 proftime_T *tm; /* place to store waittime */
1282{
1283 funccall_T *fc = current_funccal;
1284
1285 if (fc != NULL && fc->func->uf_profiling)
1286 profile_start(&fc->prof_child);
1287 script_prof_save(tm);
1288}
1289
1290/*
1291 * Take care of time spent in a child.
1292 * Should always be called after prof_child_enter().
1293 */
1294 void
1295prof_child_exit(tm)
1296 proftime_T *tm; /* where waittime was stored */
1297{
1298 funccall_T *fc = current_funccal;
1299
1300 if (fc != NULL && fc->func->uf_profiling)
1301 {
1302 profile_end(&fc->prof_child);
1303 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1304 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1305 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1306 }
1307 script_prof_restore(tm);
1308}
1309#endif
1310
1311
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312#ifdef FEAT_FOLDING
1313/*
1314 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1315 * it in "*cp". Doesn't give error messages.
1316 */
1317 int
1318eval_foldexpr(arg, cp)
1319 char_u *arg;
1320 int *cp;
1321{
Bram Moolenaar33570922005-01-25 22:26:29 +00001322 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 int retval;
1324 char_u *s;
1325
1326 ++emsg_off;
1327 ++sandbox;
1328 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 retval = 0;
1331 else
1332 {
1333 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 if (tv.v_type == VAR_NUMBER)
1335 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001336 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 retval = 0;
1338 else
1339 {
1340 /* If the result is a string, check if there is a non-digit before
1341 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001342 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 if (!VIM_ISDIGIT(*s) && *s != '-')
1344 *cp = *s++;
1345 retval = atol((char *)s);
1346 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001347 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
1349 --emsg_off;
1350 --sandbox;
1351
1352 return retval;
1353}
1354#endif
1355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001357 * ":let" list all variable values
1358 * ":let var1 var2" list variable values
1359 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001360 * ":let var += expr" assignment command.
1361 * ":let var -= expr" assignment command.
1362 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001363 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 */
1365 void
1366ex_let(eap)
1367 exarg_T *eap;
1368{
1369 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001370 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001371 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 int var_count = 0;
1374 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001375 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001377 expr = skip_var_list(arg, &var_count, &semicolon);
1378 if (expr == NULL)
1379 return;
1380 expr = vim_strchr(expr, '=');
1381 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001383 /*
1384 * ":let" without "=": list variables
1385 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001386 if (*arg == '[')
1387 EMSG(_(e_invarg));
1388 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001389 /* ":let var1 var2" */
1390 arg = list_arg_vars(eap, arg);
1391 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001392 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001393 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001394 list_glob_vars();
1395 list_buf_vars();
1396 list_win_vars();
1397 list_vim_vars();
1398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 eap->nextcmd = check_nextcmd(arg);
1400 }
1401 else
1402 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001403 op[0] = '=';
1404 op[1] = NUL;
1405 if (expr > arg)
1406 {
1407 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1408 op[0] = expr[-1]; /* +=, -= or .= */
1409 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001410 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001411
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 if (eap->skip)
1413 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001414 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (eap->skip)
1416 {
1417 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 --emsg_skip;
1420 }
1421 else if (i != FAIL)
1422 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001423 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001424 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001425 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 }
1427 }
1428}
1429
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001430/*
1431 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1432 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001433 * When "nextchars" is not NULL it points to a string with characters that
1434 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1435 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001436 * Returns OK or FAIL;
1437 */
1438 static int
1439ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1440 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001441 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001442 int copy; /* copy values from "tv", don't move */
1443 int semicolon; /* from skip_var_list() */
1444 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001445 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001446{
1447 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001448 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001449 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001450 listitem_T *item;
1451 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001452
1453 if (*arg != '[')
1454 {
1455 /*
1456 * ":let var = expr" or ":for var in list"
1457 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001458 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001459 return FAIL;
1460 return OK;
1461 }
1462
1463 /*
1464 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1465 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001466 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001467 {
1468 EMSG(_(e_listreq));
1469 return FAIL;
1470 }
1471
1472 i = list_len(l);
1473 if (semicolon == 0 && var_count < i)
1474 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001475 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001476 return FAIL;
1477 }
1478 if (var_count - semicolon > i)
1479 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001480 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001481 return FAIL;
1482 }
1483
1484 item = l->lv_first;
1485 while (*arg != ']')
1486 {
1487 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001488 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001489 item = item->li_next;
1490 if (arg == NULL)
1491 return FAIL;
1492
1493 arg = skipwhite(arg);
1494 if (*arg == ';')
1495 {
1496 /* Put the rest of the list (may be empty) in the var after ';'.
1497 * Create a new list for this. */
1498 l = list_alloc();
1499 if (l == NULL)
1500 return FAIL;
1501 while (item != NULL)
1502 {
1503 list_append_tv(l, &item->li_tv);
1504 item = item->li_next;
1505 }
1506
1507 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001508 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001509 ltv.vval.v_list = l;
1510 l->lv_refcount = 1;
1511
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001512 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1513 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001514 clear_tv(&ltv);
1515 if (arg == NULL)
1516 return FAIL;
1517 break;
1518 }
1519 else if (*arg != ',' && *arg != ']')
1520 {
1521 EMSG2(_(e_intern2), "ex_let_vars()");
1522 return FAIL;
1523 }
1524 }
1525
1526 return OK;
1527}
1528
1529/*
1530 * Skip over assignable variable "var" or list of variables "[var, var]".
1531 * Used for ":let varvar = expr" and ":for varvar in expr".
1532 * For "[var, var]" increment "*var_count" for each variable.
1533 * for "[var, var; var]" set "semicolon".
1534 * Return NULL for an error.
1535 */
1536 static char_u *
1537skip_var_list(arg, var_count, semicolon)
1538 char_u *arg;
1539 int *var_count;
1540 int *semicolon;
1541{
1542 char_u *p, *s;
1543
1544 if (*arg == '[')
1545 {
1546 /* "[var, var]": find the matching ']'. */
1547 p = arg;
1548 while (1)
1549 {
1550 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1551 s = skip_var_one(p);
1552 if (s == p)
1553 {
1554 EMSG2(_(e_invarg2), p);
1555 return NULL;
1556 }
1557 ++*var_count;
1558
1559 p = skipwhite(s);
1560 if (*p == ']')
1561 break;
1562 else if (*p == ';')
1563 {
1564 if (*semicolon == 1)
1565 {
1566 EMSG(_("Double ; in list of variables"));
1567 return NULL;
1568 }
1569 *semicolon = 1;
1570 }
1571 else if (*p != ',')
1572 {
1573 EMSG2(_(e_invarg2), p);
1574 return NULL;
1575 }
1576 }
1577 return p + 1;
1578 }
1579 else
1580 return skip_var_one(arg);
1581}
1582
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001583/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001584 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1585 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001586 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001587 static char_u *
1588skip_var_one(arg)
1589 char_u *arg;
1590{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001591 if (*arg == '@' && arg[1] != NUL)
1592 return arg + 2;
1593 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1594 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001595}
1596
Bram Moolenaara7043832005-01-21 11:56:39 +00001597/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001598 * List variables for hashtab "ht" with prefix "prefix".
1599 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001600 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001601 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001602list_hashtable_vars(ht, prefix, empty)
1603 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001604 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001605 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001606{
Bram Moolenaar33570922005-01-25 22:26:29 +00001607 hashitem_T *hi;
1608 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001609 int todo;
1610
1611 todo = ht->ht_used;
1612 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1613 {
1614 if (!HASHITEM_EMPTY(hi))
1615 {
1616 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001617 di = HI2DI(hi);
1618 if (empty || di->di_tv.v_type != VAR_STRING
1619 || di->di_tv.vval.v_string != NULL)
1620 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001621 }
1622 }
1623}
1624
1625/*
1626 * List global variables.
1627 */
1628 static void
1629list_glob_vars()
1630{
Bram Moolenaar33570922005-01-25 22:26:29 +00001631 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001632}
1633
1634/*
1635 * List buffer variables.
1636 */
1637 static void
1638list_buf_vars()
1639{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001640 char_u numbuf[NUMBUFLEN];
1641
Bram Moolenaar33570922005-01-25 22:26:29 +00001642 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001643
1644 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1645 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001646}
1647
1648/*
1649 * List window variables.
1650 */
1651 static void
1652list_win_vars()
1653{
Bram Moolenaar33570922005-01-25 22:26:29 +00001654 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001655}
1656
1657/*
1658 * List Vim variables.
1659 */
1660 static void
1661list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662{
Bram Moolenaar33570922005-01-25 22:26:29 +00001663 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664}
1665
1666/*
1667 * List variables in "arg".
1668 */
1669 static char_u *
1670list_arg_vars(eap, arg)
1671 exarg_T *eap;
1672 char_u *arg;
1673{
1674 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001675 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001676 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001677 char_u *name_start;
1678 char_u *arg_subsc;
1679 char_u *tofree;
1680 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001681
1682 while (!ends_excmd(*arg) && !got_int)
1683 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001684 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001685 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001686 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001687 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1688 {
1689 emsg_severe = TRUE;
1690 EMSG(_(e_trailing));
1691 break;
1692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001693 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001694 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001695 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001696 /* get_name_len() takes care of expanding curly braces */
1697 name_start = name = arg;
1698 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1699 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001700 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001701 /* This is mainly to keep test 49 working: when expanding
1702 * curly braces fails overrule the exception error message. */
1703 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001704 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001705 emsg_severe = TRUE;
1706 EMSG2(_(e_invarg2), arg);
1707 break;
1708 }
1709 error = TRUE;
1710 }
1711 else
1712 {
1713 if (tofree != NULL)
1714 name = tofree;
1715 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001716 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001717 else
1718 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001719 /* handle d.key, l[idx], f(expr) */
1720 arg_subsc = arg;
1721 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001722 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001723 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001724 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001725 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001726 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001727 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001728 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001729 case 'g': list_glob_vars(); break;
1730 case 'b': list_buf_vars(); break;
1731 case 'w': list_win_vars(); break;
1732 case 'v': list_vim_vars(); break;
1733 default:
1734 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001735 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001736 }
1737 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001738 {
1739 char_u numbuf[NUMBUFLEN];
1740 char_u *tf;
1741 int c;
1742 char_u *s;
1743
1744 s = echo_string(&tv, &tf, numbuf);
1745 c = *arg;
1746 *arg = NUL;
1747 list_one_var_a((char_u *)"",
1748 arg == arg_subsc ? name : name_start,
1749 tv.v_type, s == NULL ? (char_u *)"" : s);
1750 *arg = c;
1751 vim_free(tf);
1752 }
1753 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001754 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001755 }
1756 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001757
1758 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001760
1761 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001762 }
1763
1764 return arg;
1765}
1766
1767/*
1768 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1769 * Returns a pointer to the char just after the var name.
1770 * Returns NULL if there is an error.
1771 */
1772 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001773ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001774 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001775 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001776 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001778 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779{
1780 int c1;
1781 char_u *name;
1782 char_u *p;
1783 char_u *arg_end = NULL;
1784 int len;
1785 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001786 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787
1788 /*
1789 * ":let $VAR = expr": Set environment variable.
1790 */
1791 if (*arg == '$')
1792 {
1793 /* Find the end of the name. */
1794 ++arg;
1795 name = arg;
1796 len = get_env_len(&arg);
1797 if (len == 0)
1798 EMSG2(_(e_invarg2), name - 1);
1799 else
1800 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001801 if (op != NULL && (*op == '+' || *op == '-'))
1802 EMSG2(_(e_letwrong), op);
1803 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 EMSG(_(e_letunexp));
1806 else
1807 {
1808 c1 = name[len];
1809 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001810 p = get_tv_string_chk(tv);
1811 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001812 {
1813 int mustfree = FALSE;
1814 char_u *s = vim_getenv(name, &mustfree);
1815
1816 if (s != NULL)
1817 {
1818 p = tofree = concat_str(s, p);
1819 if (mustfree)
1820 vim_free(s);
1821 }
1822 }
1823 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001824 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001825 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001826 if (STRICMP(name, "HOME") == 0)
1827 init_homedir();
1828 else if (didset_vim && STRICMP(name, "VIM") == 0)
1829 didset_vim = FALSE;
1830 else if (didset_vimruntime
1831 && STRICMP(name, "VIMRUNTIME") == 0)
1832 didset_vimruntime = FALSE;
1833 arg_end = arg;
1834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001836 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 }
1838 }
1839 }
1840
1841 /*
1842 * ":let &option = expr": Set option value.
1843 * ":let &l:option = expr": Set local option value.
1844 * ":let &g:option = expr": Set global option value.
1845 */
1846 else if (*arg == '&')
1847 {
1848 /* Find the end of the name. */
1849 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850 if (p == NULL || (endchars != NULL
1851 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 EMSG(_(e_letunexp));
1853 else
1854 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 long n;
1856 int opt_type;
1857 long numval;
1858 char_u *stringval = NULL;
1859 char_u *s;
1860
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 c1 = *p;
1862 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001863
1864 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001865 s = get_tv_string_chk(tv); /* != NULL if number or string */
1866 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001867 {
1868 opt_type = get_option_value(arg, &numval,
1869 &stringval, opt_flags);
1870 if ((opt_type == 1 && *op == '.')
1871 || (opt_type == 0 && *op != '.'))
1872 EMSG2(_(e_letwrong), op);
1873 else
1874 {
1875 if (opt_type == 1) /* number */
1876 {
1877 if (*op == '+')
1878 n = numval + n;
1879 else
1880 n = numval - n;
1881 }
1882 else if (opt_type == 0 && stringval != NULL) /* string */
1883 {
1884 s = concat_str(stringval, s);
1885 vim_free(stringval);
1886 stringval = s;
1887 }
1888 }
1889 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001890 if (s != NULL)
1891 {
1892 set_option_value(arg, n, s, opt_flags);
1893 arg_end = p;
1894 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001896 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 }
1898 }
1899
1900 /*
1901 * ":let @r = expr": Set register contents.
1902 */
1903 else if (*arg == '@')
1904 {
1905 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001906 if (op != NULL && (*op == '+' || *op == '-'))
1907 EMSG2(_(e_letwrong), op);
1908 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 EMSG(_(e_letunexp));
1911 else
1912 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 char_u *tofree = NULL;
1914 char_u *s;
1915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001916 p = get_tv_string_chk(tv);
1917 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001918 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00001919 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001920 if (s != NULL)
1921 {
1922 p = tofree = concat_str(s, p);
1923 vim_free(s);
1924 }
1925 }
1926 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001927 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001928 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001929 arg_end = arg + 1;
1930 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001931 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 }
1933 }
1934
1935 /*
1936 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001937 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001939 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001941 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001943 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001944 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001946 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1947 EMSG(_(e_letunexp));
1948 else
1949 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001950 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001951 arg_end = p;
1952 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001953 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001954 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 }
1956
1957 else
1958 EMSG2(_(e_invarg2), arg);
1959
1960 return arg_end;
1961}
1962
1963/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001964 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1965 */
1966 static int
1967check_changedtick(arg)
1968 char_u *arg;
1969{
1970 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1971 {
1972 EMSG2(_(e_readonlyvar), arg);
1973 return TRUE;
1974 }
1975 return FALSE;
1976}
1977
1978/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001979 * Get an lval: variable, Dict item or List item that can be assigned a value
1980 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1981 * "name.key", "name.key[expr]" etc.
1982 * Indexing only works if "name" is an existing List or Dictionary.
1983 * "name" points to the start of the name.
1984 * If "rettv" is not NULL it points to the value to be assigned.
1985 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1986 * wrong; must end in space or cmd separator.
1987 *
1988 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001989 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001991 */
1992 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001993get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001994 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00001995 typval_T *rettv;
1996 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 int unlet;
1998 int skip;
1999 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002000 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002001{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002002 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002003 char_u *expr_start, *expr_end;
2004 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002005 dictitem_T *v;
2006 typval_T var1;
2007 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002008 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002009 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002010 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002011 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002012 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002013
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002014 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002015 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002016
2017 if (skip)
2018 {
2019 /* When skipping just find the end of the name. */
2020 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002021 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002022 }
2023
2024 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002025 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002026 if (expr_start != NULL)
2027 {
2028 /* Don't expand the name when we already know there is an error. */
2029 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2030 && *p != '[' && *p != '.')
2031 {
2032 EMSG(_(e_trailing));
2033 return NULL;
2034 }
2035
2036 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2037 if (lp->ll_exp_name == NULL)
2038 {
2039 /* Report an invalid expression in braces, unless the
2040 * expression evaluation has been cancelled due to an
2041 * aborting error, an interrupt, or an exception. */
2042 if (!aborting() && !quiet)
2043 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002044 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 EMSG2(_(e_invarg2), name);
2046 return NULL;
2047 }
2048 }
2049 lp->ll_name = lp->ll_exp_name;
2050 }
2051 else
2052 lp->ll_name = name;
2053
2054 /* Without [idx] or .key we are done. */
2055 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2056 return p;
2057
2058 cc = *p;
2059 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002060 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002061 if (v == NULL && !quiet)
2062 EMSG2(_(e_undefvar), lp->ll_name);
2063 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002064 if (v == NULL)
2065 return NULL;
2066
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002067 /*
2068 * Loop until no more [idx] or .key is following.
2069 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002070 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2074 && !(lp->ll_tv->v_type == VAR_DICT
2075 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002076 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002077 if (!quiet)
2078 EMSG(_("E689: Can only index a List or Dictionary"));
2079 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002080 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002081 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002082 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002083 if (!quiet)
2084 EMSG(_("E708: [:] must come last"));
2085 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002086 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002087
Bram Moolenaar8c711452005-01-14 21:53:12 +00002088 len = -1;
2089 if (*p == '.')
2090 {
2091 key = p + 1;
2092 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2093 ;
2094 if (len == 0)
2095 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002096 if (!quiet)
2097 EMSG(_(e_emptykey));
2098 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002099 }
2100 p = key + len;
2101 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002102 else
2103 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002104 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002105 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002106 if (*p == ':')
2107 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002108 else
2109 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002110 empty1 = FALSE;
2111 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002112 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002113 if (get_tv_string_chk(&var1) == NULL)
2114 {
2115 /* not a number or string */
2116 clear_tv(&var1);
2117 return NULL;
2118 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002119 }
2120
2121 /* Optionally get the second index [ :expr]. */
2122 if (*p == ':')
2123 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002124 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002125 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002126 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002127 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002128 if (!empty1)
2129 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002130 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002131 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 if (rettv != NULL && (rettv->v_type != VAR_LIST
2133 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002134 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 if (!quiet)
2136 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002137 if (!empty1)
2138 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002140 }
2141 p = skipwhite(p + 1);
2142 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002143 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002144 else
2145 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002146 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002147 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2148 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002149 if (!empty1)
2150 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002152 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002153 if (get_tv_string_chk(&var2) == NULL)
2154 {
2155 /* not a number or string */
2156 if (!empty1)
2157 clear_tv(&var1);
2158 clear_tv(&var2);
2159 return NULL;
2160 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002161 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002163 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002164 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002165 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002166
Bram Moolenaar8c711452005-01-14 21:53:12 +00002167 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002168 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002169 if (!quiet)
2170 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002171 if (!empty1)
2172 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002173 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002174 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002175 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002176 }
2177
2178 /* Skip to past ']'. */
2179 ++p;
2180 }
2181
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002183 {
2184 if (len == -1)
2185 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002187 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002188 if (*key == NUL)
2189 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002190 if (!quiet)
2191 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002192 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002193 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002194 }
2195 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002196 lp->ll_list = NULL;
2197 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002198 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002199 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002200 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002201 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002202 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002203 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002204 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002205 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002206 if (len == -1)
2207 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002208 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002209 }
2210 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002212 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002213 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002214 if (len == -1)
2215 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002217 p = NULL;
2218 break;
2219 }
2220 if (len == -1)
2221 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002222 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002223 }
2224 else
2225 {
2226 /*
2227 * Get the number and item for the only or first index of the List.
2228 */
2229 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002230 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002231 else
2232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002233 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002234 clear_tv(&var1);
2235 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002236 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002237 lp->ll_list = lp->ll_tv->vval.v_list;
2238 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2239 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002240 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002241 if (!quiet)
2242 EMSGN(_(e_listidx), lp->ll_n1);
2243 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002244 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002245 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002246 }
2247
2248 /*
2249 * May need to find the item or absolute index for the second
2250 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251 * When no index given: "lp->ll_empty2" is TRUE.
2252 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002253 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002256 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002257 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002259 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002261 if (ni == NULL)
2262 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002263 if (!quiet)
2264 EMSGN(_(e_listidx), lp->ll_n2);
2265 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002266 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002267 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002268 }
2269
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002270 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2271 if (lp->ll_n1 < 0)
2272 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2273 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002274 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002275 if (!quiet)
2276 EMSGN(_(e_listidx), lp->ll_n2);
2277 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002278 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002279 }
2280
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002282 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 }
2284
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002285 return p;
2286}
2287
2288/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002289 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002290 */
2291 static void
2292clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002293 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002294{
2295 vim_free(lp->ll_exp_name);
2296 vim_free(lp->ll_newkey);
2297}
2298
2299/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002300 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002301 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002303 */
2304 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002305set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002306 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002308 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002309 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002311{
2312 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002313 listitem_T *ni;
2314 listitem_T *ri;
2315 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316
2317 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002321 cc = *endp;
2322 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002323 if (op != NULL && *op != '=')
2324 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002325 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002326
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002327 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002328 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2329 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 {
2331 if (tv_op(&tv, rettv, op) == OK)
2332 set_var(lp->ll_name, &tv, FALSE);
2333 clear_tv(&tv);
2334 }
2335 }
2336 else
2337 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002341 else if (tv_check_lock(lp->ll_newkey == NULL
2342 ? lp->ll_tv->v_lock
2343 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2344 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002345 else if (lp->ll_range)
2346 {
2347 /*
2348 * Assign the List values to the list items.
2349 */
2350 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002351 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002352 if (op != NULL && *op != '=')
2353 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2354 else
2355 {
2356 clear_tv(&lp->ll_li->li_tv);
2357 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2358 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 ri = ri->li_next;
2360 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2361 break;
2362 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002363 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 /* Need to add an empty item. */
2365 ni = listitem_alloc();
2366 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002367 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002368 ri = NULL;
2369 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002370 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002371 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002372 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 ni->li_tv.vval.v_number = 0;
2374 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002375 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 lp->ll_li = lp->ll_li->li_next;
2377 ++lp->ll_n1;
2378 }
2379 if (ri != NULL)
2380 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 else if (lp->ll_empty2
2382 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 : lp->ll_n1 != lp->ll_n2)
2384 EMSG(_("E711: List value has not enough items"));
2385 }
2386 else
2387 {
2388 /*
2389 * Assign to a List or Dictionary item.
2390 */
2391 if (lp->ll_newkey != NULL)
2392 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393 if (op != NULL && *op != '=')
2394 {
2395 EMSG2(_(e_letwrong), op);
2396 return;
2397 }
2398
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002400 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401 if (di == NULL)
2402 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002403 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2404 {
2405 vim_free(di);
2406 return;
2407 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002409 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002410 else if (op != NULL && *op != '=')
2411 {
2412 tv_op(lp->ll_tv, rettv, op);
2413 return;
2414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002415 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002416 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002417
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002418 /*
2419 * Assign the value to the variable or list item.
2420 */
2421 if (copy)
2422 copy_tv(rettv, lp->ll_tv);
2423 else
2424 {
2425 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002426 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002427 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002428 }
2429 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430}
2431
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002432/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002433 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2434 * Returns OK or FAIL.
2435 */
2436 static int
2437tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002438 typval_T *tv1;
2439 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 char_u *op;
2441{
2442 long n;
2443 char_u numbuf[NUMBUFLEN];
2444 char_u *s;
2445
2446 /* Can't do anything with a Funcref or a Dict on the right. */
2447 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2448 {
2449 switch (tv1->v_type)
2450 {
2451 case VAR_DICT:
2452 case VAR_FUNC:
2453 break;
2454
2455 case VAR_LIST:
2456 if (*op != '+' || tv2->v_type != VAR_LIST)
2457 break;
2458 /* List += List */
2459 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2460 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2461 return OK;
2462
2463 case VAR_NUMBER:
2464 case VAR_STRING:
2465 if (tv2->v_type == VAR_LIST)
2466 break;
2467 if (*op == '+' || *op == '-')
2468 {
2469 /* nr += nr or nr -= nr*/
2470 n = get_tv_number(tv1);
2471 if (*op == '+')
2472 n += get_tv_number(tv2);
2473 else
2474 n -= get_tv_number(tv2);
2475 clear_tv(tv1);
2476 tv1->v_type = VAR_NUMBER;
2477 tv1->vval.v_number = n;
2478 }
2479 else
2480 {
2481 /* str .= str */
2482 s = get_tv_string(tv1);
2483 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2484 clear_tv(tv1);
2485 tv1->v_type = VAR_STRING;
2486 tv1->vval.v_string = s;
2487 }
2488 return OK;
2489 }
2490 }
2491
2492 EMSG2(_(e_letwrong), op);
2493 return FAIL;
2494}
2495
2496/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002497 * Add a watcher to a list.
2498 */
2499 static void
2500list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002501 list_T *l;
2502 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002503{
2504 lw->lw_next = l->lv_watch;
2505 l->lv_watch = lw;
2506}
2507
2508/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002509 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002510 * No warning when it isn't found...
2511 */
2512 static void
2513list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002514 list_T *l;
2515 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002516{
Bram Moolenaar33570922005-01-25 22:26:29 +00002517 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002518
2519 lwp = &l->lv_watch;
2520 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2521 {
2522 if (lw == lwrem)
2523 {
2524 *lwp = lw->lw_next;
2525 break;
2526 }
2527 lwp = &lw->lw_next;
2528 }
2529}
2530
2531/*
2532 * Just before removing an item from a list: advance watchers to the next
2533 * item.
2534 */
2535 static void
2536list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 list_T *l;
2538 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002539{
Bram Moolenaar33570922005-01-25 22:26:29 +00002540 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002541
2542 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2543 if (lw->lw_item == item)
2544 lw->lw_item = item->li_next;
2545}
2546
2547/*
2548 * Evaluate the expression used in a ":for var in expr" command.
2549 * "arg" points to "var".
2550 * Set "*errp" to TRUE for an error, FALSE otherwise;
2551 * Return a pointer that holds the info. Null when there is an error.
2552 */
2553 void *
2554eval_for_line(arg, errp, nextcmdp, skip)
2555 char_u *arg;
2556 int *errp;
2557 char_u **nextcmdp;
2558 int skip;
2559{
Bram Moolenaar33570922005-01-25 22:26:29 +00002560 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002561 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002562 typval_T tv;
2563 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002564
2565 *errp = TRUE; /* default: there is an error */
2566
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002568 if (fi == NULL)
2569 return NULL;
2570
2571 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2572 if (expr == NULL)
2573 return fi;
2574
2575 expr = skipwhite(expr);
2576 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2577 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002578 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002579 return fi;
2580 }
2581
2582 if (skip)
2583 ++emsg_skip;
2584 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2585 {
2586 *errp = FALSE;
2587 if (!skip)
2588 {
2589 l = tv.vval.v_list;
2590 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002591 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002592 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002593 clear_tv(&tv);
2594 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002595 else
2596 {
2597 fi->fi_list = l;
2598 list_add_watch(l, &fi->fi_lw);
2599 fi->fi_lw.lw_item = l->lv_first;
2600 }
2601 }
2602 }
2603 if (skip)
2604 --emsg_skip;
2605
2606 return fi;
2607}
2608
2609/*
2610 * Use the first item in a ":for" list. Advance to the next.
2611 * Assign the values to the variable (list). "arg" points to the first one.
2612 * Return TRUE when a valid item was found, FALSE when at end of list or
2613 * something wrong.
2614 */
2615 int
2616next_for_item(fi_void, arg)
2617 void *fi_void;
2618 char_u *arg;
2619{
Bram Moolenaar33570922005-01-25 22:26:29 +00002620 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002621 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002622 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002623
2624 item = fi->fi_lw.lw_item;
2625 if (item == NULL)
2626 result = FALSE;
2627 else
2628 {
2629 fi->fi_lw.lw_item = item->li_next;
2630 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2631 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2632 }
2633 return result;
2634}
2635
2636/*
2637 * Free the structure used to store info used by ":for".
2638 */
2639 void
2640free_for_info(fi_void)
2641 void *fi_void;
2642{
Bram Moolenaar33570922005-01-25 22:26:29 +00002643 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002644
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002645 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002646 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002647 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002648 list_unref(fi->fi_list);
2649 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002650 vim_free(fi);
2651}
2652
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2654
2655 void
2656set_context_for_expression(xp, arg, cmdidx)
2657 expand_T *xp;
2658 char_u *arg;
2659 cmdidx_T cmdidx;
2660{
2661 int got_eq = FALSE;
2662 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002663 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002665 if (cmdidx == CMD_let)
2666 {
2667 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002668 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002669 {
2670 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002671 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002672 {
2673 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002674 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002675 if (vim_iswhite(*p))
2676 break;
2677 }
2678 return;
2679 }
2680 }
2681 else
2682 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2683 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 while ((xp->xp_pattern = vim_strpbrk(arg,
2685 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2686 {
2687 c = *xp->xp_pattern;
2688 if (c == '&')
2689 {
2690 c = xp->xp_pattern[1];
2691 if (c == '&')
2692 {
2693 ++xp->xp_pattern;
2694 xp->xp_context = cmdidx != CMD_let || got_eq
2695 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2696 }
2697 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002698 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002700 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2701 xp->xp_pattern += 2;
2702
2703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 }
2705 else if (c == '$')
2706 {
2707 /* environment variable */
2708 xp->xp_context = EXPAND_ENV_VARS;
2709 }
2710 else if (c == '=')
2711 {
2712 got_eq = TRUE;
2713 xp->xp_context = EXPAND_EXPRESSION;
2714 }
2715 else if (c == '<'
2716 && xp->xp_context == EXPAND_FUNCTIONS
2717 && vim_strchr(xp->xp_pattern, '(') == NULL)
2718 {
2719 /* Function name can start with "<SNR>" */
2720 break;
2721 }
2722 else if (cmdidx != CMD_let || got_eq)
2723 {
2724 if (c == '"') /* string */
2725 {
2726 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2727 if (c == '\\' && xp->xp_pattern[1] != NUL)
2728 ++xp->xp_pattern;
2729 xp->xp_context = EXPAND_NOTHING;
2730 }
2731 else if (c == '\'') /* literal string */
2732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2735 /* skip */ ;
2736 xp->xp_context = EXPAND_NOTHING;
2737 }
2738 else if (c == '|')
2739 {
2740 if (xp->xp_pattern[1] == '|')
2741 {
2742 ++xp->xp_pattern;
2743 xp->xp_context = EXPAND_EXPRESSION;
2744 }
2745 else
2746 xp->xp_context = EXPAND_COMMANDS;
2747 }
2748 else
2749 xp->xp_context = EXPAND_EXPRESSION;
2750 }
2751 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002752 /* Doesn't look like something valid, expand as an expression
2753 * anyway. */
2754 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 arg = xp->xp_pattern;
2756 if (*arg != NUL)
2757 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2758 /* skip */ ;
2759 }
2760 xp->xp_pattern = arg;
2761}
2762
2763#endif /* FEAT_CMDL_COMPL */
2764
2765/*
2766 * ":1,25call func(arg1, arg2)" function call.
2767 */
2768 void
2769ex_call(eap)
2770 exarg_T *eap;
2771{
2772 char_u *arg = eap->arg;
2773 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002775 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002777 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 linenr_T lnum;
2779 int doesrange;
2780 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002781 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002783 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2784 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002785 if (tofree == NULL)
2786 return;
2787
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002788 /* Increase refcount on dictionary, it could get deleted when evaluating
2789 * the arguments. */
2790 if (fudi.fd_dict != NULL)
2791 ++fudi.fd_dict->dv_refcount;
2792
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002793 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2794 len = STRLEN(tofree);
2795 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
Bram Moolenaar532c7802005-01-27 14:44:31 +00002797 /* Skip white space to allow ":call func ()". Not good, but required for
2798 * backward compatibility. */
2799 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002800 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801
2802 if (*startarg != '(')
2803 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00002804 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 goto end;
2806 }
2807
2808 /*
2809 * When skipping, evaluate the function once, to find the end of the
2810 * arguments.
2811 * When the function takes a range, this is discovered after the first
2812 * call, and the loop is broken.
2813 */
2814 if (eap->skip)
2815 {
2816 ++emsg_skip;
2817 lnum = eap->line2; /* do it once, also with an invalid range */
2818 }
2819 else
2820 lnum = eap->line1;
2821 for ( ; lnum <= eap->line2; ++lnum)
2822 {
2823 if (!eap->skip && eap->addr_count > 0)
2824 {
2825 curwin->w_cursor.lnum = lnum;
2826 curwin->w_cursor.col = 0;
2827 }
2828 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002829 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002830 eap->line1, eap->line2, &doesrange,
2831 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 {
2833 failed = TRUE;
2834 break;
2835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002836 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 if (doesrange || eap->skip)
2838 break;
2839 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002840 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002841 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002842 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 if (aborting())
2844 break;
2845 }
2846 if (eap->skip)
2847 --emsg_skip;
2848
2849 if (!failed)
2850 {
2851 /* Check for trailing illegal characters and a following command. */
2852 if (!ends_excmd(*arg))
2853 {
2854 emsg_severe = TRUE;
2855 EMSG(_(e_trailing));
2856 }
2857 else
2858 eap->nextcmd = check_nextcmd(arg);
2859 }
2860
2861end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002862 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002863 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864}
2865
2866/*
2867 * ":unlet[!] var1 ... " command.
2868 */
2869 void
2870ex_unlet(eap)
2871 exarg_T *eap;
2872{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002873 ex_unletlock(eap, eap->arg, 0);
2874}
2875
2876/*
2877 * ":lockvar" and ":unlockvar" commands
2878 */
2879 void
2880ex_lockvar(eap)
2881 exarg_T *eap;
2882{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002884 int deep = 2;
2885
2886 if (eap->forceit)
2887 deep = -1;
2888 else if (vim_isdigit(*arg))
2889 {
2890 deep = getdigits(&arg);
2891 arg = skipwhite(arg);
2892 }
2893
2894 ex_unletlock(eap, arg, deep);
2895}
2896
2897/*
2898 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2899 */
2900 static void
2901ex_unletlock(eap, argstart, deep)
2902 exarg_T *eap;
2903 char_u *argstart;
2904 int deep;
2905{
2906 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002909 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910
2911 do
2912 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002914 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
2915 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 if (lv.ll_name == NULL)
2917 error = TRUE; /* error but continue parsing */
2918 if (name_end == NULL || (!vim_iswhite(*name_end)
2919 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 if (name_end != NULL)
2922 {
2923 emsg_severe = TRUE;
2924 EMSG(_(e_trailing));
2925 }
2926 if (!(eap->skip || error))
2927 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 break;
2929 }
2930
2931 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002932 {
2933 if (eap->cmdidx == CMD_unlet)
2934 {
2935 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2936 error = TRUE;
2937 }
2938 else
2939 {
2940 if (do_lock_var(&lv, name_end, deep,
2941 eap->cmdidx == CMD_lockvar) == FAIL)
2942 error = TRUE;
2943 }
2944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 if (!eap->skip)
2947 clear_lval(&lv);
2948
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 arg = skipwhite(name_end);
2950 } while (!ends_excmd(*arg));
2951
2952 eap->nextcmd = check_nextcmd(arg);
2953}
2954
Bram Moolenaar8c711452005-01-14 21:53:12 +00002955 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00002957 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002959 int forceit;
2960{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 int ret = OK;
2962 int cc;
2963
2964 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002965 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 cc = *name_end;
2967 *name_end = NUL;
2968
2969 /* Normal name or expanded name. */
2970 if (check_changedtick(lp->ll_name))
2971 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002972 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002974 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002975 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002976 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
2977 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 else if (lp->ll_range)
2979 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002980 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981
2982 /* Delete a range of List items. */
2983 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2984 {
2985 li = lp->ll_li->li_next;
2986 listitem_remove(lp->ll_list, lp->ll_li);
2987 lp->ll_li = li;
2988 ++lp->ll_n1;
2989 }
2990 }
2991 else
2992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 /* unlet a List item. */
2995 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002998 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002999 }
3000
3001 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003002}
3003
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004/*
3005 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003006 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 */
3008 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003009do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003011 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012{
Bram Moolenaar33570922005-01-25 22:26:29 +00003013 hashtab_T *ht;
3014 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003015 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016
Bram Moolenaar33570922005-01-25 22:26:29 +00003017 ht = find_var_ht(name, &varname);
3018 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003020 hi = hash_find(ht, varname);
3021 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003022 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003023 if (var_check_ro(HI2DI(hi)->di_flags, name))
3024 return FAIL;
3025 delete_var(ht, hi);
3026 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003029 if (forceit)
3030 return OK;
3031 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 return FAIL;
3033}
3034
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003035/*
3036 * Lock or unlock variable indicated by "lp".
3037 * "deep" is the levels to go (-1 for unlimited);
3038 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3039 */
3040 static int
3041do_lock_var(lp, name_end, deep, lock)
3042 lval_T *lp;
3043 char_u *name_end;
3044 int deep;
3045 int lock;
3046{
3047 int ret = OK;
3048 int cc;
3049 dictitem_T *di;
3050
3051 if (deep == 0) /* nothing to do */
3052 return OK;
3053
3054 if (lp->ll_tv == NULL)
3055 {
3056 cc = *name_end;
3057 *name_end = NUL;
3058
3059 /* Normal name or expanded name. */
3060 if (check_changedtick(lp->ll_name))
3061 ret = FAIL;
3062 else
3063 {
3064 di = find_var(lp->ll_name, NULL);
3065 if (di == NULL)
3066 ret = FAIL;
3067 else
3068 {
3069 if (lock)
3070 di->di_flags |= DI_FLAGS_LOCK;
3071 else
3072 di->di_flags &= ~DI_FLAGS_LOCK;
3073 item_lock(&di->di_tv, deep, lock);
3074 }
3075 }
3076 *name_end = cc;
3077 }
3078 else if (lp->ll_range)
3079 {
3080 listitem_T *li = lp->ll_li;
3081
3082 /* (un)lock a range of List items. */
3083 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3084 {
3085 item_lock(&li->li_tv, deep, lock);
3086 li = li->li_next;
3087 ++lp->ll_n1;
3088 }
3089 }
3090 else if (lp->ll_list != NULL)
3091 /* (un)lock a List item. */
3092 item_lock(&lp->ll_li->li_tv, deep, lock);
3093 else
3094 /* un(lock) a Dictionary item. */
3095 item_lock(&lp->ll_di->di_tv, deep, lock);
3096
3097 return ret;
3098}
3099
3100/*
3101 * Lock or unlock an item. "deep" is nr of levels to go.
3102 */
3103 static void
3104item_lock(tv, deep, lock)
3105 typval_T *tv;
3106 int deep;
3107 int lock;
3108{
3109 static int recurse = 0;
3110 list_T *l;
3111 listitem_T *li;
3112 dict_T *d;
3113 hashitem_T *hi;
3114 int todo;
3115
3116 if (recurse >= DICT_MAXNEST)
3117 {
3118 EMSG(_("E743: variable nested too deep for (un)lock"));
3119 return;
3120 }
3121 if (deep == 0)
3122 return;
3123 ++recurse;
3124
3125 /* lock/unlock the item itself */
3126 if (lock)
3127 tv->v_lock |= VAR_LOCKED;
3128 else
3129 tv->v_lock &= ~VAR_LOCKED;
3130
3131 switch (tv->v_type)
3132 {
3133 case VAR_LIST:
3134 if ((l = tv->vval.v_list) != NULL)
3135 {
3136 if (lock)
3137 l->lv_lock |= VAR_LOCKED;
3138 else
3139 l->lv_lock &= ~VAR_LOCKED;
3140 if (deep < 0 || deep > 1)
3141 /* recursive: lock/unlock the items the List contains */
3142 for (li = l->lv_first; li != NULL; li = li->li_next)
3143 item_lock(&li->li_tv, deep - 1, lock);
3144 }
3145 break;
3146 case VAR_DICT:
3147 if ((d = tv->vval.v_dict) != NULL)
3148 {
3149 if (lock)
3150 d->dv_lock |= VAR_LOCKED;
3151 else
3152 d->dv_lock &= ~VAR_LOCKED;
3153 if (deep < 0 || deep > 1)
3154 {
3155 /* recursive: lock/unlock the items the List contains */
3156 todo = d->dv_hashtab.ht_used;
3157 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3158 {
3159 if (!HASHITEM_EMPTY(hi))
3160 {
3161 --todo;
3162 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3163 }
3164 }
3165 }
3166 }
3167 }
3168 --recurse;
3169}
3170
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3172/*
3173 * Delete all "menutrans_" variables.
3174 */
3175 void
3176del_menutrans_vars()
3177{
Bram Moolenaar33570922005-01-25 22:26:29 +00003178 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003179 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 hash_lock(&globvarht);
3182 todo = globvarht.ht_used;
3183 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003184 {
3185 if (!HASHITEM_EMPTY(hi))
3186 {
3187 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3189 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003190 }
3191 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193}
3194#endif
3195
3196#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3197
3198/*
3199 * Local string buffer for the next two functions to store a variable name
3200 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3201 * get_user_var_name().
3202 */
3203
3204static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3205
3206static char_u *varnamebuf = NULL;
3207static int varnamebuflen = 0;
3208
3209/*
3210 * Function to concatenate a prefix and a variable name.
3211 */
3212 static char_u *
3213cat_prefix_varname(prefix, name)
3214 int prefix;
3215 char_u *name;
3216{
3217 int len;
3218
3219 len = (int)STRLEN(name) + 3;
3220 if (len > varnamebuflen)
3221 {
3222 vim_free(varnamebuf);
3223 len += 10; /* some additional space */
3224 varnamebuf = alloc(len);
3225 if (varnamebuf == NULL)
3226 {
3227 varnamebuflen = 0;
3228 return NULL;
3229 }
3230 varnamebuflen = len;
3231 }
3232 *varnamebuf = prefix;
3233 varnamebuf[1] = ':';
3234 STRCPY(varnamebuf + 2, name);
3235 return varnamebuf;
3236}
3237
3238/*
3239 * Function given to ExpandGeneric() to obtain the list of user defined
3240 * (global/buffer/window/built-in) variable names.
3241 */
3242/*ARGSUSED*/
3243 char_u *
3244get_user_var_name(xp, idx)
3245 expand_T *xp;
3246 int idx;
3247{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003248 static long_u gdone;
3249 static long_u bdone;
3250 static long_u wdone;
3251 static int vidx;
3252 static hashitem_T *hi;
3253 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
3255 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003256 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003257
3258 /* Global variables */
3259 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003261 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003262 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003263 else
3264 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003265 while (HASHITEM_EMPTY(hi))
3266 ++hi;
3267 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3268 return cat_prefix_varname('g', hi->hi_key);
3269 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003271
3272 /* b: variables */
3273 ht = &curbuf->b_vars.dv_hashtab;
3274 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003276 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003277 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003278 else
3279 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003280 while (HASHITEM_EMPTY(hi))
3281 ++hi;
3282 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003284 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003286 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 return (char_u *)"b:changedtick";
3288 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003289
3290 /* w: variables */
3291 ht = &curwin->w_vars.dv_hashtab;
3292 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003294 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003295 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003296 else
3297 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003298 while (HASHITEM_EMPTY(hi))
3299 ++hi;
3300 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003302
3303 /* v: variables */
3304 if (vidx < VV_LEN)
3305 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306
3307 vim_free(varnamebuf);
3308 varnamebuf = NULL;
3309 varnamebuflen = 0;
3310 return NULL;
3311}
3312
3313#endif /* FEAT_CMDL_COMPL */
3314
3315/*
3316 * types for expressions.
3317 */
3318typedef enum
3319{
3320 TYPE_UNKNOWN = 0
3321 , TYPE_EQUAL /* == */
3322 , TYPE_NEQUAL /* != */
3323 , TYPE_GREATER /* > */
3324 , TYPE_GEQUAL /* >= */
3325 , TYPE_SMALLER /* < */
3326 , TYPE_SEQUAL /* <= */
3327 , TYPE_MATCH /* =~ */
3328 , TYPE_NOMATCH /* !~ */
3329} exptype_T;
3330
3331/*
3332 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003333 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3335 */
3336
3337/*
3338 * Handle zero level expression.
3339 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003340 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 * Return OK or FAIL.
3342 */
3343 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003344eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 char_u **nextcmd;
3348 int evaluate;
3349{
3350 int ret;
3351 char_u *p;
3352
3353 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003354 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if (ret == FAIL || !ends_excmd(*p))
3356 {
3357 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003358 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 /*
3360 * Report the invalid expression unless the expression evaluation has
3361 * been cancelled due to an aborting error, an interrupt, or an
3362 * exception.
3363 */
3364 if (!aborting())
3365 EMSG2(_(e_invexpr2), arg);
3366 ret = FAIL;
3367 }
3368 if (nextcmd != NULL)
3369 *nextcmd = check_nextcmd(p);
3370
3371 return ret;
3372}
3373
3374/*
3375 * Handle top level expression:
3376 * expr1 ? expr0 : expr0
3377 *
3378 * "arg" must point to the first non-white of the expression.
3379 * "arg" is advanced to the next non-white after the recognized expression.
3380 *
3381 * Return OK or FAIL.
3382 */
3383 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003384eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 int evaluate;
3388{
3389 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003390 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391
3392 /*
3393 * Get the first variable.
3394 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003395 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 return FAIL;
3397
3398 if ((*arg)[0] == '?')
3399 {
3400 result = FALSE;
3401 if (evaluate)
3402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003403 int error = FALSE;
3404
3405 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003407 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003408 if (error)
3409 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 }
3411
3412 /*
3413 * Get the second variable.
3414 */
3415 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003416 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 return FAIL;
3418
3419 /*
3420 * Check for the ":".
3421 */
3422 if ((*arg)[0] != ':')
3423 {
3424 EMSG(_("E109: Missing ':' after '?'"));
3425 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003426 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 return FAIL;
3428 }
3429
3430 /*
3431 * Get the third variable.
3432 */
3433 *arg = skipwhite(*arg + 1);
3434 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3435 {
3436 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003437 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 return FAIL;
3439 }
3440 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003441 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 }
3443
3444 return OK;
3445}
3446
3447/*
3448 * Handle first level expression:
3449 * expr2 || expr2 || expr2 logical OR
3450 *
3451 * "arg" must point to the first non-white of the expression.
3452 * "arg" is advanced to the next non-white after the recognized expression.
3453 *
3454 * Return OK or FAIL.
3455 */
3456 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003457eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 int evaluate;
3461{
Bram Moolenaar33570922005-01-25 22:26:29 +00003462 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 long result;
3464 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003465 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
3467 /*
3468 * Get the first variable.
3469 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003470 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 return FAIL;
3472
3473 /*
3474 * Repeat until there is no following "||".
3475 */
3476 first = TRUE;
3477 result = FALSE;
3478 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3479 {
3480 if (evaluate && first)
3481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003482 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003484 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003485 if (error)
3486 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 first = FALSE;
3488 }
3489
3490 /*
3491 * Get the second variable.
3492 */
3493 *arg = skipwhite(*arg + 2);
3494 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3495 return FAIL;
3496
3497 /*
3498 * Compute the result.
3499 */
3500 if (evaluate && !result)
3501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003502 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003504 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003505 if (error)
3506 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
3508 if (evaluate)
3509 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003510 rettv->v_type = VAR_NUMBER;
3511 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
3513 }
3514
3515 return OK;
3516}
3517
3518/*
3519 * Handle second level expression:
3520 * expr3 && expr3 && expr3 logical AND
3521 *
3522 * "arg" must point to the first non-white of the expression.
3523 * "arg" is advanced to the next non-white after the recognized expression.
3524 *
3525 * Return OK or FAIL.
3526 */
3527 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003528eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 int evaluate;
3532{
Bram Moolenaar33570922005-01-25 22:26:29 +00003533 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 long result;
3535 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003536 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
3538 /*
3539 * Get the first variable.
3540 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003541 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 return FAIL;
3543
3544 /*
3545 * Repeat until there is no following "&&".
3546 */
3547 first = TRUE;
3548 result = TRUE;
3549 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3550 {
3551 if (evaluate && first)
3552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003553 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003555 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003556 if (error)
3557 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 first = FALSE;
3559 }
3560
3561 /*
3562 * Get the second variable.
3563 */
3564 *arg = skipwhite(*arg + 2);
3565 if (eval4(arg, &var2, evaluate && result) == FAIL)
3566 return FAIL;
3567
3568 /*
3569 * Compute the result.
3570 */
3571 if (evaluate && result)
3572 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003573 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003575 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003576 if (error)
3577 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
3579 if (evaluate)
3580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003581 rettv->v_type = VAR_NUMBER;
3582 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
3584 }
3585
3586 return OK;
3587}
3588
3589/*
3590 * Handle third level expression:
3591 * var1 == var2
3592 * var1 =~ var2
3593 * var1 != var2
3594 * var1 !~ var2
3595 * var1 > var2
3596 * var1 >= var2
3597 * var1 < var2
3598 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003599 * var1 is var2
3600 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 *
3602 * "arg" must point to the first non-white of the expression.
3603 * "arg" is advanced to the next non-white after the recognized expression.
3604 *
3605 * Return OK or FAIL.
3606 */
3607 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003608eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003610 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 int evaluate;
3612{
Bram Moolenaar33570922005-01-25 22:26:29 +00003613 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 char_u *p;
3615 int i;
3616 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003617 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 int len = 2;
3619 long n1, n2;
3620 char_u *s1, *s2;
3621 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3622 regmatch_T regmatch;
3623 int ic;
3624 char_u *save_cpo;
3625
3626 /*
3627 * Get the first variable.
3628 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003629 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 return FAIL;
3631
3632 p = *arg;
3633 switch (p[0])
3634 {
3635 case '=': if (p[1] == '=')
3636 type = TYPE_EQUAL;
3637 else if (p[1] == '~')
3638 type = TYPE_MATCH;
3639 break;
3640 case '!': if (p[1] == '=')
3641 type = TYPE_NEQUAL;
3642 else if (p[1] == '~')
3643 type = TYPE_NOMATCH;
3644 break;
3645 case '>': if (p[1] != '=')
3646 {
3647 type = TYPE_GREATER;
3648 len = 1;
3649 }
3650 else
3651 type = TYPE_GEQUAL;
3652 break;
3653 case '<': if (p[1] != '=')
3654 {
3655 type = TYPE_SMALLER;
3656 len = 1;
3657 }
3658 else
3659 type = TYPE_SEQUAL;
3660 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003661 case 'i': if (p[1] == 's')
3662 {
3663 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3664 len = 5;
3665 if (!vim_isIDc(p[len]))
3666 {
3667 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3668 type_is = TRUE;
3669 }
3670 }
3671 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673
3674 /*
3675 * If there is a comparitive operator, use it.
3676 */
3677 if (type != TYPE_UNKNOWN)
3678 {
3679 /* extra question mark appended: ignore case */
3680 if (p[len] == '?')
3681 {
3682 ic = TRUE;
3683 ++len;
3684 }
3685 /* extra '#' appended: match case */
3686 else if (p[len] == '#')
3687 {
3688 ic = FALSE;
3689 ++len;
3690 }
3691 /* nothing appened: use 'ignorecase' */
3692 else
3693 ic = p_ic;
3694
3695 /*
3696 * Get the second variable.
3697 */
3698 *arg = skipwhite(p + len);
3699 if (eval5(arg, &var2, evaluate) == FAIL)
3700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003701 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 return FAIL;
3703 }
3704
3705 if (evaluate)
3706 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003707 if (type_is && rettv->v_type != var2.v_type)
3708 {
3709 /* For "is" a different type always means FALSE, for "notis"
3710 * it means TRUE. */
3711 n1 = (type == TYPE_NEQUAL);
3712 }
3713 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3714 {
3715 if (type_is)
3716 {
3717 n1 = (rettv->v_type == var2.v_type
3718 && rettv->vval.v_list == var2.vval.v_list);
3719 if (type == TYPE_NEQUAL)
3720 n1 = !n1;
3721 }
3722 else if (rettv->v_type != var2.v_type
3723 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3724 {
3725 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003726 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003727 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003728 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003729 clear_tv(rettv);
3730 clear_tv(&var2);
3731 return FAIL;
3732 }
3733 else
3734 {
3735 /* Compare two Lists for being equal or unequal. */
3736 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3737 if (type == TYPE_NEQUAL)
3738 n1 = !n1;
3739 }
3740 }
3741
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003742 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3743 {
3744 if (type_is)
3745 {
3746 n1 = (rettv->v_type == var2.v_type
3747 && rettv->vval.v_dict == var2.vval.v_dict);
3748 if (type == TYPE_NEQUAL)
3749 n1 = !n1;
3750 }
3751 else if (rettv->v_type != var2.v_type
3752 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3753 {
3754 if (rettv->v_type != var2.v_type)
3755 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3756 else
3757 EMSG(_("E736: Invalid operation for Dictionary"));
3758 clear_tv(rettv);
3759 clear_tv(&var2);
3760 return FAIL;
3761 }
3762 else
3763 {
3764 /* Compare two Dictionaries for being equal or unequal. */
3765 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3766 if (type == TYPE_NEQUAL)
3767 n1 = !n1;
3768 }
3769 }
3770
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003771 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3772 {
3773 if (rettv->v_type != var2.v_type
3774 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3775 {
3776 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003777 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003778 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003779 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003780 clear_tv(rettv);
3781 clear_tv(&var2);
3782 return FAIL;
3783 }
3784 else
3785 {
3786 /* Compare two Funcrefs for being equal or unequal. */
3787 if (rettv->vval.v_string == NULL
3788 || var2.vval.v_string == NULL)
3789 n1 = FALSE;
3790 else
3791 n1 = STRCMP(rettv->vval.v_string,
3792 var2.vval.v_string) == 0;
3793 if (type == TYPE_NEQUAL)
3794 n1 = !n1;
3795 }
3796 }
3797
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 /*
3799 * If one of the two variables is a number, compare as a number.
3800 * When using "=~" or "!~", always compare as string.
3801 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003802 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003805 n1 = get_tv_number(rettv);
3806 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 switch (type)
3808 {
3809 case TYPE_EQUAL: n1 = (n1 == n2); break;
3810 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3811 case TYPE_GREATER: n1 = (n1 > n2); break;
3812 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3813 case TYPE_SMALLER: n1 = (n1 < n2); break;
3814 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3815 case TYPE_UNKNOWN:
3816 case TYPE_MATCH:
3817 case TYPE_NOMATCH: break; /* avoid gcc warning */
3818 }
3819 }
3820 else
3821 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003822 s1 = get_tv_string_buf(rettv, buf1);
3823 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3825 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3826 else
3827 i = 0;
3828 n1 = FALSE;
3829 switch (type)
3830 {
3831 case TYPE_EQUAL: n1 = (i == 0); break;
3832 case TYPE_NEQUAL: n1 = (i != 0); break;
3833 case TYPE_GREATER: n1 = (i > 0); break;
3834 case TYPE_GEQUAL: n1 = (i >= 0); break;
3835 case TYPE_SMALLER: n1 = (i < 0); break;
3836 case TYPE_SEQUAL: n1 = (i <= 0); break;
3837
3838 case TYPE_MATCH:
3839 case TYPE_NOMATCH:
3840 /* avoid 'l' flag in 'cpoptions' */
3841 save_cpo = p_cpo;
3842 p_cpo = (char_u *)"";
3843 regmatch.regprog = vim_regcomp(s2,
3844 RE_MAGIC + RE_STRING);
3845 regmatch.rm_ic = ic;
3846 if (regmatch.regprog != NULL)
3847 {
3848 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3849 vim_free(regmatch.regprog);
3850 if (type == TYPE_NOMATCH)
3851 n1 = !n1;
3852 }
3853 p_cpo = save_cpo;
3854 break;
3855
3856 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3857 }
3858 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003859 clear_tv(rettv);
3860 clear_tv(&var2);
3861 rettv->v_type = VAR_NUMBER;
3862 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 }
3864 }
3865
3866 return OK;
3867}
3868
3869/*
3870 * Handle fourth level expression:
3871 * + number addition
3872 * - number subtraction
3873 * . string concatenation
3874 *
3875 * "arg" must point to the first non-white of the expression.
3876 * "arg" is advanced to the next non-white after the recognized expression.
3877 *
3878 * Return OK or FAIL.
3879 */
3880 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003881eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 int evaluate;
3885{
Bram Moolenaar33570922005-01-25 22:26:29 +00003886 typval_T var2;
3887 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 int op;
3889 long n1, n2;
3890 char_u *s1, *s2;
3891 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3892 char_u *p;
3893
3894 /*
3895 * Get the first variable.
3896 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003897 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 return FAIL;
3899
3900 /*
3901 * Repeat computing, until no '+', '-' or '.' is following.
3902 */
3903 for (;;)
3904 {
3905 op = **arg;
3906 if (op != '+' && op != '-' && op != '.')
3907 break;
3908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003909 if (op != '+' || rettv->v_type != VAR_LIST)
3910 {
3911 /* For "list + ...", an illegal use of the first operand as
3912 * a number cannot be determined before evaluating the 2nd
3913 * operand: if this is also a list, all is ok.
3914 * For "something . ...", "something - ..." or "non-list + ...",
3915 * we know that the first operand needs to be a string or number
3916 * without evaluating the 2nd operand. So check before to avoid
3917 * side effects after an error. */
3918 if (evaluate && get_tv_string_chk(rettv) == NULL)
3919 {
3920 clear_tv(rettv);
3921 return FAIL;
3922 }
3923 }
3924
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 /*
3926 * Get the second variable.
3927 */
3928 *arg = skipwhite(*arg + 1);
3929 if (eval6(arg, &var2, evaluate) == FAIL)
3930 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003931 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 return FAIL;
3933 }
3934
3935 if (evaluate)
3936 {
3937 /*
3938 * Compute the result.
3939 */
3940 if (op == '.')
3941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003942 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3943 s2 = get_tv_string_buf_chk(&var2, buf2);
3944 if (s2 == NULL) /* type error ? */
3945 {
3946 clear_tv(rettv);
3947 clear_tv(&var2);
3948 return FAIL;
3949 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003950 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003951 clear_tv(rettv);
3952 rettv->v_type = VAR_STRING;
3953 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003955 else if (op == '+' && rettv->v_type == VAR_LIST
3956 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003957 {
3958 /* concatenate Lists */
3959 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3960 &var3) == FAIL)
3961 {
3962 clear_tv(rettv);
3963 clear_tv(&var2);
3964 return FAIL;
3965 }
3966 clear_tv(rettv);
3967 *rettv = var3;
3968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 else
3970 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003971 int error = FALSE;
3972
3973 n1 = get_tv_number_chk(rettv, &error);
3974 if (error)
3975 {
3976 /* This can only happen for "list + non-list".
3977 * For "non-list + ..." or "something - ...", we returned
3978 * before evaluating the 2nd operand. */
3979 clear_tv(rettv);
3980 return FAIL;
3981 }
3982 n2 = get_tv_number_chk(&var2, &error);
3983 if (error)
3984 {
3985 clear_tv(rettv);
3986 clear_tv(&var2);
3987 return FAIL;
3988 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003989 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 if (op == '+')
3991 n1 = n1 + n2;
3992 else
3993 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003994 rettv->v_type = VAR_NUMBER;
3995 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003997 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
3999 }
4000 return OK;
4001}
4002
4003/*
4004 * Handle fifth level expression:
4005 * * number multiplication
4006 * / number division
4007 * % number modulo
4008 *
4009 * "arg" must point to the first non-white of the expression.
4010 * "arg" is advanced to the next non-white after the recognized expression.
4011 *
4012 * Return OK or FAIL.
4013 */
4014 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 int evaluate;
4019{
Bram Moolenaar33570922005-01-25 22:26:29 +00004020 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 int op;
4022 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004023 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024
4025 /*
4026 * Get the first variable.
4027 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004028 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 return FAIL;
4030
4031 /*
4032 * Repeat computing, until no '*', '/' or '%' is following.
4033 */
4034 for (;;)
4035 {
4036 op = **arg;
4037 if (op != '*' && op != '/' && op != '%')
4038 break;
4039
4040 if (evaluate)
4041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004042 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004044 if (error)
4045 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
4047 else
4048 n1 = 0;
4049
4050 /*
4051 * Get the second variable.
4052 */
4053 *arg = skipwhite(*arg + 1);
4054 if (eval7(arg, &var2, evaluate) == FAIL)
4055 return FAIL;
4056
4057 if (evaluate)
4058 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004059 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004060 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004061 if (error)
4062 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064 /*
4065 * Compute the result.
4066 */
4067 if (op == '*')
4068 n1 = n1 * n2;
4069 else if (op == '/')
4070 {
4071 if (n2 == 0) /* give an error message? */
4072 n1 = 0x7fffffffL;
4073 else
4074 n1 = n1 / n2;
4075 }
4076 else
4077 {
4078 if (n2 == 0) /* give an error message? */
4079 n1 = 0;
4080 else
4081 n1 = n1 % n2;
4082 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004083 rettv->v_type = VAR_NUMBER;
4084 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 }
4086 }
4087
4088 return OK;
4089}
4090
4091/*
4092 * Handle sixth level expression:
4093 * number number constant
4094 * "string" string contstant
4095 * 'string' literal string contstant
4096 * &option-name option value
4097 * @r register contents
4098 * identifier variable value
4099 * function() function call
4100 * $VAR environment variable
4101 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004102 * [expr, expr] List
4103 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 *
4105 * Also handle:
4106 * ! in front logical NOT
4107 * - in front unary minus
4108 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004109 * trailing [] subscript in String or List
4110 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 *
4112 * "arg" must point to the first non-white of the expression.
4113 * "arg" is advanced to the next non-white after the recognized expression.
4114 *
4115 * Return OK or FAIL.
4116 */
4117 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 int evaluate;
4122{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 long n;
4124 int len;
4125 char_u *s;
4126 int val;
4127 char_u *start_leader, *end_leader;
4128 int ret = OK;
4129 char_u *alias;
4130
4131 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004133 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136
4137 /*
4138 * Skip '!' and '-' characters. They are handled later.
4139 */
4140 start_leader = *arg;
4141 while (**arg == '!' || **arg == '-' || **arg == '+')
4142 *arg = skipwhite(*arg + 1);
4143 end_leader = *arg;
4144
4145 switch (**arg)
4146 {
4147 /*
4148 * Number constant.
4149 */
4150 case '0':
4151 case '1':
4152 case '2':
4153 case '3':
4154 case '4':
4155 case '5':
4156 case '6':
4157 case '7':
4158 case '8':
4159 case '9':
4160 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4161 *arg += len;
4162 if (evaluate)
4163 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 rettv->v_type = VAR_NUMBER;
4165 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
4167 break;
4168
4169 /*
4170 * String constant: "string".
4171 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004172 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 break;
4174
4175 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004176 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004179 break;
4180
4181 /*
4182 * List: [expr, expr]
4183 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004184 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 break;
4186
4187 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004188 * Dictionary: {key: val, key: val}
4189 */
4190 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4191 break;
4192
4193 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004194 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004196 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 break;
4198
4199 /*
4200 * Environment variable: $VAR.
4201 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 break;
4204
4205 /*
4206 * Register contents: @r.
4207 */
4208 case '@': ++*arg;
4209 if (evaluate)
4210 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004212 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
4214 if (**arg != NUL)
4215 ++*arg;
4216 break;
4217
4218 /*
4219 * nested expression: (expression).
4220 */
4221 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 if (**arg == ')')
4224 ++*arg;
4225 else if (ret == OK)
4226 {
4227 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004228 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 ret = FAIL;
4230 }
4231 break;
4232
Bram Moolenaar8c711452005-01-14 21:53:12 +00004233 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 break;
4235 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004236
4237 if (ret == NOTDONE)
4238 {
4239 /*
4240 * Must be a variable or function name.
4241 * Can also be a curly-braces kind of name: {expr}.
4242 */
4243 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004244 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004245 if (alias != NULL)
4246 s = alias;
4247
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004248 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004249 ret = FAIL;
4250 else
4251 {
4252 if (**arg == '(') /* recursive! */
4253 {
4254 /* If "s" is the name of a variable of type VAR_FUNC
4255 * use its contents. */
4256 s = deref_func_name(s, &len);
4257
4258 /* Invoke the function. */
4259 ret = get_func_tv(s, len, rettv, arg,
4260 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004261 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004262 /* Stop the expression evaluation when immediately
4263 * aborting on error, or when an interrupt occurred or
4264 * an exception was thrown but not caught. */
4265 if (aborting())
4266 {
4267 if (ret == OK)
4268 clear_tv(rettv);
4269 ret = FAIL;
4270 }
4271 }
4272 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004273 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004274 else
4275 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004276 }
4277
4278 if (alias != NULL)
4279 vim_free(alias);
4280 }
4281
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 *arg = skipwhite(*arg);
4283
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004284 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4285 * expr(expr). */
4286 if (ret == OK)
4287 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288
4289 /*
4290 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4291 */
4292 if (ret == OK && evaluate && end_leader > start_leader)
4293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004294 int error = FALSE;
4295
4296 val = get_tv_number_chk(rettv, &error);
4297 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004299 clear_tv(rettv);
4300 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004302 else
4303 {
4304 while (end_leader > start_leader)
4305 {
4306 --end_leader;
4307 if (*end_leader == '!')
4308 val = !val;
4309 else if (*end_leader == '-')
4310 val = -val;
4311 }
4312 clear_tv(rettv);
4313 rettv->v_type = VAR_NUMBER;
4314 rettv->vval.v_number = val;
4315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317
4318 return ret;
4319}
4320
4321/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004322 * Evaluate an "[expr]" or "[expr:expr]" index.
4323 * "*arg" points to the '['.
4324 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4325 */
4326 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004327eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004328 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004329 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004330 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004331 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004332{
4333 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004334 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004335 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004336 long len = -1;
4337 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004338 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004339 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004340
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004342 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004343 if (verbose)
4344 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004345 return FAIL;
4346 }
4347
Bram Moolenaar8c711452005-01-14 21:53:12 +00004348 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004349 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004350 /*
4351 * dict.name
4352 */
4353 key = *arg + 1;
4354 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4355 ;
4356 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004357 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004358 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004359 }
4360 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004361 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004362 /*
4363 * something[idx]
4364 *
4365 * Get the (first) variable from inside the [].
4366 */
4367 *arg = skipwhite(*arg + 1);
4368 if (**arg == ':')
4369 empty1 = TRUE;
4370 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4371 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004372 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4373 {
4374 /* not a number or string */
4375 clear_tv(&var1);
4376 return FAIL;
4377 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004378
4379 /*
4380 * Get the second variable from inside the [:].
4381 */
4382 if (**arg == ':')
4383 {
4384 range = TRUE;
4385 *arg = skipwhite(*arg + 1);
4386 if (**arg == ']')
4387 empty2 = TRUE;
4388 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004390 if (!empty1)
4391 clear_tv(&var1);
4392 return FAIL;
4393 }
4394 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4395 {
4396 /* not a number or string */
4397 if (!empty1)
4398 clear_tv(&var1);
4399 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004400 return FAIL;
4401 }
4402 }
4403
4404 /* Check for the ']'. */
4405 if (**arg != ']')
4406 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004407 if (verbose)
4408 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004409 clear_tv(&var1);
4410 if (range)
4411 clear_tv(&var2);
4412 return FAIL;
4413 }
4414 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004415 }
4416
4417 if (evaluate)
4418 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004419 n1 = 0;
4420 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004421 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004422 n1 = get_tv_number(&var1);
4423 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004424 }
4425 if (range)
4426 {
4427 if (empty2)
4428 n2 = -1;
4429 else
4430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004431 n2 = get_tv_number(&var2);
4432 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004433 }
4434 }
4435
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004436 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004437 {
4438 case VAR_NUMBER:
4439 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004440 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004441 len = (long)STRLEN(s);
4442 if (range)
4443 {
4444 /* The resulting variable is a substring. If the indexes
4445 * are out of range the result is empty. */
4446 if (n1 < 0)
4447 {
4448 n1 = len + n1;
4449 if (n1 < 0)
4450 n1 = 0;
4451 }
4452 if (n2 < 0)
4453 n2 = len + n2;
4454 else if (n2 >= len)
4455 n2 = len;
4456 if (n1 >= len || n2 < 0 || n1 > n2)
4457 s = NULL;
4458 else
4459 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4460 }
4461 else
4462 {
4463 /* The resulting variable is a string of a single
4464 * character. If the index is too big or negative the
4465 * result is empty. */
4466 if (n1 >= len || n1 < 0)
4467 s = NULL;
4468 else
4469 s = vim_strnsave(s + n1, 1);
4470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004471 clear_tv(rettv);
4472 rettv->v_type = VAR_STRING;
4473 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004474 break;
4475
4476 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004477 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004478 if (n1 < 0)
4479 n1 = len + n1;
4480 if (!empty1 && (n1 < 0 || n1 >= len))
4481 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004482 if (verbose)
4483 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004484 return FAIL;
4485 }
4486 if (range)
4487 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004488 list_T *l;
4489 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004490
4491 if (n2 < 0)
4492 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004493 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004494 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004495 if (verbose)
4496 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004497 return FAIL;
4498 }
4499 l = list_alloc();
4500 if (l == NULL)
4501 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004502 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004503 n1 <= n2; ++n1)
4504 {
4505 if (list_append_tv(l, &item->li_tv) == FAIL)
4506 {
4507 list_free(l);
4508 return FAIL;
4509 }
4510 item = item->li_next;
4511 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004512 clear_tv(rettv);
4513 rettv->v_type = VAR_LIST;
4514 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004515 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004516 }
4517 else
4518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004519 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004520 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004521 clear_tv(rettv);
4522 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004523 }
4524 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004525
4526 case VAR_DICT:
4527 if (range)
4528 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004529 if (verbose)
4530 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004531 if (len == -1)
4532 clear_tv(&var1);
4533 return FAIL;
4534 }
4535 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004536 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004537
4538 if (len == -1)
4539 {
4540 key = get_tv_string(&var1);
4541 if (*key == NUL)
4542 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004543 if (verbose)
4544 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004545 clear_tv(&var1);
4546 return FAIL;
4547 }
4548 }
4549
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004550 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004551
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004552 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004553 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004554 if (len == -1)
4555 clear_tv(&var1);
4556 if (item == NULL)
4557 return FAIL;
4558
4559 copy_tv(&item->di_tv, &var1);
4560 clear_tv(rettv);
4561 *rettv = var1;
4562 }
4563 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004564 }
4565 }
4566
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004567 return OK;
4568}
4569
4570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 * Get an option value.
4572 * "arg" points to the '&' or '+' before the option name.
4573 * "arg" is advanced to character after the option name.
4574 * Return OK or FAIL.
4575 */
4576 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004577get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004579 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 int evaluate;
4581{
4582 char_u *option_end;
4583 long numval;
4584 char_u *stringval;
4585 int opt_type;
4586 int c;
4587 int working = (**arg == '+'); /* has("+option") */
4588 int ret = OK;
4589 int opt_flags;
4590
4591 /*
4592 * Isolate the option name and find its value.
4593 */
4594 option_end = find_option_end(arg, &opt_flags);
4595 if (option_end == NULL)
4596 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004597 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 EMSG2(_("E112: Option name missing: %s"), *arg);
4599 return FAIL;
4600 }
4601
4602 if (!evaluate)
4603 {
4604 *arg = option_end;
4605 return OK;
4606 }
4607
4608 c = *option_end;
4609 *option_end = NUL;
4610 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004611 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612
4613 if (opt_type == -3) /* invalid name */
4614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004615 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 EMSG2(_("E113: Unknown option: %s"), *arg);
4617 ret = FAIL;
4618 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004619 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
4621 if (opt_type == -2) /* hidden string option */
4622 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004623 rettv->v_type = VAR_STRING;
4624 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
4626 else if (opt_type == -1) /* hidden number option */
4627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 rettv->v_type = VAR_NUMBER;
4629 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 }
4631 else if (opt_type == 1) /* number option */
4632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004633 rettv->v_type = VAR_NUMBER;
4634 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 }
4636 else /* string option */
4637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004638 rettv->v_type = VAR_STRING;
4639 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
4641 }
4642 else if (working && (opt_type == -2 || opt_type == -1))
4643 ret = FAIL;
4644
4645 *option_end = c; /* put back for error messages */
4646 *arg = option_end;
4647
4648 return ret;
4649}
4650
4651/*
4652 * Allocate a variable for a string constant.
4653 * Return OK or FAIL.
4654 */
4655 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004656get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 int evaluate;
4660{
4661 char_u *p;
4662 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 int extra = 0;
4664
4665 /*
4666 * Find the end of the string, skipping backslashed characters.
4667 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004668 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
4670 if (*p == '\\' && p[1] != NUL)
4671 {
4672 ++p;
4673 /* A "\<x>" form occupies at least 4 characters, and produces up
4674 * to 6 characters: reserve space for 2 extra */
4675 if (*p == '<')
4676 extra += 2;
4677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
4679
4680 if (*p != '"')
4681 {
4682 EMSG2(_("E114: Missing quote: %s"), *arg);
4683 return FAIL;
4684 }
4685
4686 /* If only parsing, set *arg and return here */
4687 if (!evaluate)
4688 {
4689 *arg = p + 1;
4690 return OK;
4691 }
4692
4693 /*
4694 * Copy the string into allocated memory, handling backslashed
4695 * characters.
4696 */
4697 name = alloc((unsigned)(p - *arg + extra));
4698 if (name == NULL)
4699 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004700 rettv->v_type = VAR_STRING;
4701 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
Bram Moolenaar8c711452005-01-14 21:53:12 +00004703 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 {
4705 if (*p == '\\')
4706 {
4707 switch (*++p)
4708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004709 case 'b': *name++ = BS; ++p; break;
4710 case 'e': *name++ = ESC; ++p; break;
4711 case 'f': *name++ = FF; ++p; break;
4712 case 'n': *name++ = NL; ++p; break;
4713 case 'r': *name++ = CAR; ++p; break;
4714 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715
4716 case 'X': /* hex: "\x1", "\x12" */
4717 case 'x':
4718 case 'u': /* Unicode: "\u0023" */
4719 case 'U':
4720 if (vim_isxdigit(p[1]))
4721 {
4722 int n, nr;
4723 int c = toupper(*p);
4724
4725 if (c == 'X')
4726 n = 2;
4727 else
4728 n = 4;
4729 nr = 0;
4730 while (--n >= 0 && vim_isxdigit(p[1]))
4731 {
4732 ++p;
4733 nr = (nr << 4) + hex2nr(*p);
4734 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004735 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736#ifdef FEAT_MBYTE
4737 /* For "\u" store the number according to
4738 * 'encoding'. */
4739 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004740 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 else
4742#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004743 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 break;
4746
4747 /* octal: "\1", "\12", "\123" */
4748 case '0':
4749 case '1':
4750 case '2':
4751 case '3':
4752 case '4':
4753 case '5':
4754 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004755 case '7': *name = *p++ - '0';
4756 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004758 *name = (*name << 3) + *p++ - '0';
4759 if (*p >= '0' && *p <= '7')
4760 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004762 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 break;
4764
4765 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004766 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 if (extra != 0)
4768 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004769 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 break;
4771 }
4772 /* FALLTHROUGH */
4773
Bram Moolenaar8c711452005-01-14 21:53:12 +00004774 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 break;
4776 }
4777 }
4778 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004779 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004782 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 *arg = p + 1;
4784
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 return OK;
4786}
4787
4788/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004789 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 * Return OK or FAIL.
4791 */
4792 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004793get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 int evaluate;
4797{
4798 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004799 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004800 int reduce = 0;
4801
4802 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004803 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004804 */
4805 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4806 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004807 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004809 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004810 break;
4811 ++reduce;
4812 ++p;
4813 }
4814 }
4815
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004817 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004818 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004819 return FAIL;
4820 }
4821
Bram Moolenaar8c711452005-01-14 21:53:12 +00004822 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004823 if (!evaluate)
4824 {
4825 *arg = p + 1;
4826 return OK;
4827 }
4828
4829 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004830 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004831 */
4832 str = alloc((unsigned)((p - *arg) - reduce));
4833 if (str == NULL)
4834 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004835 rettv->v_type = VAR_STRING;
4836 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004837
Bram Moolenaar8c711452005-01-14 21:53:12 +00004838 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004839 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004840 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004841 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004842 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004843 break;
4844 ++p;
4845 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004846 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004847 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004848 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004849 *arg = p + 1;
4850
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004851 return OK;
4852}
4853
4854/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855 * Allocate a variable for a List and fill it from "*arg".
4856 * Return OK or FAIL.
4857 */
4858 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004859get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004860 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004861 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004862 int evaluate;
4863{
Bram Moolenaar33570922005-01-25 22:26:29 +00004864 list_T *l = NULL;
4865 typval_T tv;
4866 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004867
4868 if (evaluate)
4869 {
4870 l = list_alloc();
4871 if (l == NULL)
4872 return FAIL;
4873 }
4874
4875 *arg = skipwhite(*arg + 1);
4876 while (**arg != ']' && **arg != NUL)
4877 {
4878 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4879 goto failret;
4880 if (evaluate)
4881 {
4882 item = listitem_alloc();
4883 if (item != NULL)
4884 {
4885 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004886 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004887 list_append(l, item);
4888 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004889 else
4890 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004891 }
4892
4893 if (**arg == ']')
4894 break;
4895 if (**arg != ',')
4896 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004897 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004898 goto failret;
4899 }
4900 *arg = skipwhite(*arg + 1);
4901 }
4902
4903 if (**arg != ']')
4904 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004905 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004906failret:
4907 if (evaluate)
4908 list_free(l);
4909 return FAIL;
4910 }
4911
4912 *arg = skipwhite(*arg + 1);
4913 if (evaluate)
4914 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 rettv->v_type = VAR_LIST;
4916 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004917 ++l->lv_refcount;
4918 }
4919
4920 return OK;
4921}
4922
4923/*
4924 * Allocate an empty header for a list.
4925 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004926 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004927list_alloc()
4928{
Bram Moolenaar33570922005-01-25 22:26:29 +00004929 return (list_T *)alloc_clear(sizeof(list_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004930}
4931
4932/*
4933 * Unreference a list: decrement the reference count and free it when it
4934 * becomes zero.
4935 */
4936 static void
4937list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004938 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004939{
4940 if (l != NULL && --l->lv_refcount <= 0)
4941 list_free(l);
4942}
4943
4944/*
4945 * Free a list, including all items it points to.
4946 * Ignores the reference count.
4947 */
4948 static void
4949list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004950 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004951{
Bram Moolenaar33570922005-01-25 22:26:29 +00004952 listitem_T *item;
4953 listitem_T *next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004954
4955 for (item = l->lv_first; item != NULL; item = next)
4956 {
4957 next = item->li_next;
4958 listitem_free(item);
4959 }
4960 vim_free(l);
4961}
4962
4963/*
4964 * Allocate a list item.
4965 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004966 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004967listitem_alloc()
4968{
Bram Moolenaar33570922005-01-25 22:26:29 +00004969 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004970}
4971
4972/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004973 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004974 */
4975 static void
4976listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004977 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004979 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004980 vim_free(item);
4981}
4982
4983/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004984 * Remove a list item from a List and free it. Also clears the value.
4985 */
4986 static void
4987listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004988 list_T *l;
4989 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004990{
4991 list_remove(l, item, item);
4992 listitem_free(item);
4993}
4994
4995/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004996 * Get the number of items in a list.
4997 */
4998 static long
4999list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005000 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005001{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005002 if (l == NULL)
5003 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005004 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005005}
5006
5007/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005008 * Return TRUE when two lists have exactly the same values.
5009 */
5010 static int
5011list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005012 list_T *l1;
5013 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005014 int ic; /* ignore case for strings */
5015{
Bram Moolenaar33570922005-01-25 22:26:29 +00005016 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005017
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005018 if (list_len(l1) != list_len(l2))
5019 return FALSE;
5020
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005021 for (item1 = l1->lv_first, item2 = l2->lv_first;
5022 item1 != NULL && item2 != NULL;
5023 item1 = item1->li_next, item2 = item2->li_next)
5024 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5025 return FALSE;
5026 return item1 == NULL && item2 == NULL;
5027}
5028
5029/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005030 * Return TRUE when two dictionaries have exactly the same key/values.
5031 */
5032 static int
5033dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005034 dict_T *d1;
5035 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005036 int ic; /* ignore case for strings */
5037{
Bram Moolenaar33570922005-01-25 22:26:29 +00005038 hashitem_T *hi;
5039 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005040 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005041
5042 if (dict_len(d1) != dict_len(d2))
5043 return FALSE;
5044
Bram Moolenaar33570922005-01-25 22:26:29 +00005045 todo = d1->dv_hashtab.ht_used;
5046 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005047 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005048 if (!HASHITEM_EMPTY(hi))
5049 {
5050 item2 = dict_find(d2, hi->hi_key, -1);
5051 if (item2 == NULL)
5052 return FALSE;
5053 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5054 return FALSE;
5055 --todo;
5056 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005057 }
5058 return TRUE;
5059}
5060
5061/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005062 * Return TRUE if "tv1" and "tv2" have the same value.
5063 * Compares the items just like "==" would compare them.
5064 */
5065 static int
5066tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005067 typval_T *tv1;
5068 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005069 int ic; /* ignore case */
5070{
5071 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005072 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005073
5074 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
5075 {
5076 /* recursive! */
5077 if (tv1->v_type != tv2->v_type
5078 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
5079 return FALSE;
5080 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005081 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
5082 {
5083 /* recursive! */
5084 if (tv1->v_type != tv2->v_type
5085 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
5086 return FALSE;
5087 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005088 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
5089 {
5090 if (tv1->v_type != tv2->v_type
5091 || tv1->vval.v_string == NULL
5092 || tv2->vval.v_string == NULL
5093 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
5094 return FALSE;
5095 }
5096 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
5097 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005098 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
5099 * Don't consider "4x" to be equal to 4. */
5100 if ((tv1->v_type == VAR_STRING
5101 && !string_isa_number(tv1->vval.v_string))
5102 || (tv2->v_type == VAR_STRING
5103 && !string_isa_number(tv2->vval.v_string)))
5104 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005105 if (get_tv_number(tv1) != get_tv_number(tv2))
5106 return FALSE;
5107 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005108 else
5109 {
5110 s1 = get_tv_string_buf(tv1, buf1);
5111 s2 = get_tv_string_buf(tv2, buf2);
5112 if ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) != 0)
5113 return FALSE;
5114 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005115 return TRUE;
5116}
5117
5118/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005119 * Return TRUE if "tv" is a number without other non-white characters.
5120 */
5121 static int
5122string_isa_number(s)
5123 char_u *s;
5124{
5125 int len;
5126
5127 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
5128 return len > 0 && *skipwhite(s + len) == NUL;
5129}
5130
5131/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005132 * Locate item with index "n" in list "l" and return it.
5133 * A negative index is counted from the end; -1 is the last item.
5134 * Returns NULL when "n" is out of range.
5135 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005136 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005137list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005138 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005139 long n;
5140{
Bram Moolenaar33570922005-01-25 22:26:29 +00005141 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005142 long idx;
5143
5144 if (l == NULL)
5145 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005146
5147 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005148 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005149 n = l->lv_len + n;
5150
5151 /* Check for index out of range. */
5152 if (n < 0 || n >= l->lv_len)
5153 return NULL;
5154
5155 /* When there is a cached index may start search from there. */
5156 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005158 if (n < l->lv_idx / 2)
5159 {
5160 /* closest to the start of the list */
5161 item = l->lv_first;
5162 idx = 0;
5163 }
5164 else if (n > (l->lv_idx + l->lv_len) / 2)
5165 {
5166 /* closest to the end of the list */
5167 item = l->lv_last;
5168 idx = l->lv_len - 1;
5169 }
5170 else
5171 {
5172 /* closest to the cached index */
5173 item = l->lv_idx_item;
5174 idx = l->lv_idx;
5175 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005176 }
5177 else
5178 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005179 if (n < l->lv_len / 2)
5180 {
5181 /* closest to the start of the list */
5182 item = l->lv_first;
5183 idx = 0;
5184 }
5185 else
5186 {
5187 /* closest to the end of the list */
5188 item = l->lv_last;
5189 idx = l->lv_len - 1;
5190 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005191 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005192
5193 while (n > idx)
5194 {
5195 /* search forward */
5196 item = item->li_next;
5197 ++idx;
5198 }
5199 while (n < idx)
5200 {
5201 /* search backward */
5202 item = item->li_prev;
5203 --idx;
5204 }
5205
5206 /* cache the used index */
5207 l->lv_idx = idx;
5208 l->lv_idx_item = item;
5209
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005210 return item;
5211}
5212
5213/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005214 * Locate "item" list "l" and return its index.
5215 * Returns -1 when "item" is not in the list.
5216 */
5217 static long
5218list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005219 list_T *l;
5220 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005221{
5222 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005223 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005224
5225 if (l == NULL)
5226 return -1;
5227 idx = 0;
5228 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5229 ++idx;
5230 if (li == NULL)
5231 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005232 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005233}
5234
5235/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005236 * Append item "item" to the end of list "l".
5237 */
5238 static void
5239list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005240 list_T *l;
5241 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005242{
5243 if (l->lv_last == NULL)
5244 {
5245 /* empty list */
5246 l->lv_first = item;
5247 l->lv_last = item;
5248 item->li_prev = NULL;
5249 }
5250 else
5251 {
5252 l->lv_last->li_next = item;
5253 item->li_prev = l->lv_last;
5254 l->lv_last = item;
5255 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005256 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 item->li_next = NULL;
5258}
5259
5260/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005261 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005262 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 */
5264 static int
5265list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005266 list_T *l;
5267 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005269 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270
Bram Moolenaar05159a02005-02-26 23:04:13 +00005271 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005273 copy_tv(tv, &li->li_tv);
5274 list_append(l, li);
5275 return OK;
5276}
5277
5278/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005279 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005280 * Return FAIL when out of memory.
5281 */
5282 int
5283list_append_dict(list, dict)
5284 list_T *list;
5285 dict_T *dict;
5286{
5287 listitem_T *li = listitem_alloc();
5288
5289 if (li == NULL)
5290 return FAIL;
5291 li->li_tv.v_type = VAR_DICT;
5292 li->li_tv.v_lock = 0;
5293 li->li_tv.vval.v_dict = dict;
5294 list_append(list, li);
5295 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 return OK;
5297}
5298
5299/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005300 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005301 * If "item" is NULL append at the end.
5302 * Return FAIL when out of memory.
5303 */
5304 static int
5305list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005306 list_T *l;
5307 typval_T *tv;
5308 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005309{
Bram Moolenaar33570922005-01-25 22:26:29 +00005310 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005311
5312 if (ni == NULL)
5313 return FAIL;
5314 copy_tv(tv, &ni->li_tv);
5315 if (item == NULL)
5316 /* Append new item at end of list. */
5317 list_append(l, ni);
5318 else
5319 {
5320 /* Insert new item before existing item. */
5321 ni->li_prev = item->li_prev;
5322 ni->li_next = item;
5323 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005324 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005325 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005326 ++l->lv_idx;
5327 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005328 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005329 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005330 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005331 l->lv_idx_item = NULL;
5332 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005333 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005334 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005335 }
5336 return OK;
5337}
5338
5339/*
5340 * Extend "l1" with "l2".
5341 * If "bef" is NULL append at the end, otherwise insert before this item.
5342 * Returns FAIL when out of memory.
5343 */
5344 static int
5345list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005346 list_T *l1;
5347 list_T *l2;
5348 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005349{
Bram Moolenaar33570922005-01-25 22:26:29 +00005350 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005351
5352 for (item = l2->lv_first; item != NULL; item = item->li_next)
5353 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5354 return FAIL;
5355 return OK;
5356}
5357
5358/*
5359 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5360 * Return FAIL when out of memory.
5361 */
5362 static int
5363list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005364 list_T *l1;
5365 list_T *l2;
5366 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005367{
Bram Moolenaar33570922005-01-25 22:26:29 +00005368 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005369
5370 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005371 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005372 if (l == NULL)
5373 return FAIL;
5374 tv->v_type = VAR_LIST;
5375 tv->vval.v_list = l;
5376
5377 /* append all items from the second list */
5378 return list_extend(l, l2, NULL);
5379}
5380
5381/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005382 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005384 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 * Returns NULL when out of memory.
5386 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005387 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005388list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005389 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005391 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392{
Bram Moolenaar33570922005-01-25 22:26:29 +00005393 list_T *copy;
5394 listitem_T *item;
5395 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396
5397 if (orig == NULL)
5398 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399
5400 copy = list_alloc();
5401 if (copy != NULL)
5402 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005403 if (copyID != 0)
5404 {
5405 /* Do this before adding the items, because one of the items may
5406 * refer back to this list. */
5407 orig->lv_copyID = copyID;
5408 orig->lv_copylist = copy;
5409 }
5410 for (item = orig->lv_first; item != NULL && !got_int;
5411 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412 {
5413 ni = listitem_alloc();
5414 if (ni == NULL)
5415 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005416 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005417 {
5418 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5419 {
5420 vim_free(ni);
5421 break;
5422 }
5423 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005425 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 list_append(copy, ni);
5427 }
5428 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005429 if (item != NULL)
5430 {
5431 list_unref(copy);
5432 copy = NULL;
5433 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 }
5435
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005436 return copy;
5437}
5438
5439/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005440 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005441 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005443 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005444list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005445 list_T *l;
5446 listitem_T *item;
5447 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005448{
Bram Moolenaar33570922005-01-25 22:26:29 +00005449 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005451 /* notify watchers */
5452 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005454 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005455 list_fix_watch(l, ip);
5456 if (ip == item2)
5457 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005459
5460 if (item2->li_next == NULL)
5461 l->lv_last = item->li_prev;
5462 else
5463 item2->li_next->li_prev = item->li_prev;
5464 if (item->li_prev == NULL)
5465 l->lv_first = item2->li_next;
5466 else
5467 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005468 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469}
5470
5471/*
5472 * Return an allocated string with the string representation of a list.
5473 * May return NULL.
5474 */
5475 static char_u *
5476list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005477 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478{
5479 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005480
5481 if (tv->vval.v_list == NULL)
5482 return NULL;
5483 ga_init2(&ga, (int)sizeof(char), 80);
5484 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005485 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5486 {
5487 vim_free(ga.ga_data);
5488 return NULL;
5489 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 ga_append(&ga, ']');
5491 ga_append(&ga, NUL);
5492 return (char_u *)ga.ga_data;
5493}
5494
5495/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005496 * Join list "l" into a string in "*gap", using separator "sep".
5497 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005498 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005499 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005500 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005501list_join(gap, l, sep, echo)
5502 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005503 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005504 char_u *sep;
5505 int echo;
5506{
5507 int first = TRUE;
5508 char_u *tofree;
5509 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005510 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005511 char_u *s;
5512
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005513 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005514 {
5515 if (first)
5516 first = FALSE;
5517 else
5518 ga_concat(gap, sep);
5519
5520 if (echo)
5521 s = echo_string(&item->li_tv, &tofree, numbuf);
5522 else
5523 s = tv2string(&item->li_tv, &tofree, numbuf);
5524 if (s != NULL)
5525 ga_concat(gap, s);
5526 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005527 if (s == NULL)
5528 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005529 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005530 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005531}
5532
5533/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005534 * Allocate an empty header for a dictionary.
5535 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005536 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005537dict_alloc()
5538{
Bram Moolenaar33570922005-01-25 22:26:29 +00005539 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005540
Bram Moolenaar33570922005-01-25 22:26:29 +00005541 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005542 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005543 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005544 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005545 d->dv_lock = 0;
5546 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005547 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005548 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005549 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005550}
5551
5552/*
5553 * Unreference a Dictionary: decrement the reference count and free it when it
5554 * becomes zero.
5555 */
5556 static void
5557dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005558 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005559{
5560 if (d != NULL && --d->dv_refcount <= 0)
5561 dict_free(d);
5562}
5563
5564/*
5565 * Free a Dictionary, including all items it contains.
5566 * Ignores the reference count.
5567 */
5568 static void
5569dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005570 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005571{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005572 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005573 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005575 /* Careful: we free the dictitems while they still appear in the
Bram Moolenaar33570922005-01-25 22:26:29 +00005576 * hashtab. Must not try to resize the hashtab! */
5577 todo = d->dv_hashtab.ht_used;
5578 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005580 if (!HASHITEM_EMPTY(hi))
5581 {
5582 dictitem_free(HI2DI(hi));
5583 --todo;
5584 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005586 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587 vim_free(d);
5588}
5589
5590/*
5591 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005592 * The "key" is copied to the new item.
5593 * Note that the value of the item "di_tv" still needs to be initialized!
5594 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005596 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005597dictitem_alloc(key)
5598 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599{
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005601
Bram Moolenaar33570922005-01-25 22:26:29 +00005602 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005603 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005604 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005605 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005606 di->di_flags = 0;
5607 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005608 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005609}
5610
5611/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005612 * Make a copy of a Dictionary item.
5613 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005614 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005615dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005616 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005617{
Bram Moolenaar33570922005-01-25 22:26:29 +00005618 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005619
Bram Moolenaar33570922005-01-25 22:26:29 +00005620 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005621 if (di != NULL)
5622 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005623 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005624 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005625 copy_tv(&org->di_tv, &di->di_tv);
5626 }
5627 return di;
5628}
5629
5630/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005631 * Remove item "item" from Dictionary "dict" and free it.
5632 */
5633 static void
5634dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005635 dict_T *dict;
5636 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005637{
Bram Moolenaar33570922005-01-25 22:26:29 +00005638 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005639
Bram Moolenaar33570922005-01-25 22:26:29 +00005640 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005641 if (HASHITEM_EMPTY(hi))
5642 EMSG2(_(e_intern2), "dictitem_remove()");
5643 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005644 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005645 dictitem_free(item);
5646}
5647
5648/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005649 * Free a dict item. Also clears the value.
5650 */
5651 static void
5652dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005653 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005654{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005655 clear_tv(&item->di_tv);
5656 vim_free(item);
5657}
5658
5659/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005660 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5661 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005662 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00005663 * Returns NULL when out of memory.
5664 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005665 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005666dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005667 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005668 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005669 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005670{
Bram Moolenaar33570922005-01-25 22:26:29 +00005671 dict_T *copy;
5672 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005673 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005674 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005675
5676 if (orig == NULL)
5677 return NULL;
5678
5679 copy = dict_alloc();
5680 if (copy != NULL)
5681 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005682 if (copyID != 0)
5683 {
5684 orig->dv_copyID = copyID;
5685 orig->dv_copydict = copy;
5686 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005687 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005688 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005689 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005690 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005691 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005692 --todo;
5693
5694 di = dictitem_alloc(hi->hi_key);
5695 if (di == NULL)
5696 break;
5697 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005698 {
5699 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
5700 copyID) == FAIL)
5701 {
5702 vim_free(di);
5703 break;
5704 }
5705 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005706 else
5707 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5708 if (dict_add(copy, di) == FAIL)
5709 {
5710 dictitem_free(di);
5711 break;
5712 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005713 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005714 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005715
Bram Moolenaare9a41262005-01-15 22:18:47 +00005716 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005717 if (todo > 0)
5718 {
5719 dict_unref(copy);
5720 copy = NULL;
5721 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005722 }
5723
5724 return copy;
5725}
5726
5727/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005728 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005729 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005731 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005733 dict_T *d;
5734 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735{
Bram Moolenaar33570922005-01-25 22:26:29 +00005736 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005737}
5738
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005740 * Add a number or string entry to dictionary "d".
5741 * When "str" is NULL use number "nr", otherwise use "str".
5742 * Returns FAIL when out of memory and when key already exists.
5743 */
5744 int
5745dict_add_nr_str(d, key, nr, str)
5746 dict_T *d;
5747 char *key;
5748 long nr;
5749 char_u *str;
5750{
5751 dictitem_T *item;
5752
5753 item = dictitem_alloc((char_u *)key);
5754 if (item == NULL)
5755 return FAIL;
5756 item->di_tv.v_lock = 0;
5757 if (str == NULL)
5758 {
5759 item->di_tv.v_type = VAR_NUMBER;
5760 item->di_tv.vval.v_number = nr;
5761 }
5762 else
5763 {
5764 item->di_tv.v_type = VAR_STRING;
5765 item->di_tv.vval.v_string = vim_strsave(str);
5766 }
5767 if (dict_add(d, item) == FAIL)
5768 {
5769 dictitem_free(item);
5770 return FAIL;
5771 }
5772 return OK;
5773}
5774
5775/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005776 * Get the number of items in a Dictionary.
5777 */
5778 static long
5779dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005780 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005781{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005782 if (d == NULL)
5783 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00005784 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005785}
5786
5787/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 * Find item "key[len]" in Dictionary "d".
5789 * If "len" is negative use strlen(key).
5790 * Returns NULL when not found.
5791 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005792 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005793dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00005794 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 char_u *key;
5796 int len;
5797{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005798#define AKEYLEN 200
5799 char_u buf[AKEYLEN];
5800 char_u *akey;
5801 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00005802 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005804 if (len < 0)
5805 akey = key;
5806 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005807 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005808 tofree = akey = vim_strnsave(key, len);
5809 if (akey == NULL)
5810 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005811 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005812 else
5813 {
5814 /* Avoid a malloc/free by using buf[]. */
5815 STRNCPY(buf, key, len);
5816 buf[len] = NUL;
5817 akey = buf;
5818 }
5819
Bram Moolenaar33570922005-01-25 22:26:29 +00005820 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005821 vim_free(tofree);
5822 if (HASHITEM_EMPTY(hi))
5823 return NULL;
5824 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825}
5826
5827/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005828 * Get a string item from a dictionary in allocated memory.
5829 * Returns NULL if the entry doesn't exist or out of memory.
5830 */
5831 char_u *
5832get_dict_string(d, key)
5833 dict_T *d;
5834 char_u *key;
5835{
5836 dictitem_T *di;
5837
5838 di = dict_find(d, key, -1);
5839 if (di == NULL)
5840 return NULL;
5841 return vim_strsave(get_tv_string(&di->di_tv));
5842}
5843
5844/*
5845 * Get a number item from a dictionary.
5846 * Returns 0 if the entry doesn't exist or out of memory.
5847 */
5848 long
5849get_dict_number(d, key)
5850 dict_T *d;
5851 char_u *key;
5852{
5853 dictitem_T *di;
5854
5855 di = dict_find(d, key, -1);
5856 if (di == NULL)
5857 return 0;
5858 return get_tv_number(&di->di_tv);
5859}
5860
5861/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 * Return an allocated string with the string representation of a Dictionary.
5863 * May return NULL.
5864 */
5865 static char_u *
5866dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005867 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868{
5869 garray_T ga;
5870 int first = TRUE;
5871 char_u *tofree;
5872 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005873 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00005875 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005876 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005877
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005878 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 return NULL;
5880 ga_init2(&ga, (int)sizeof(char), 80);
5881 ga_append(&ga, '{');
5882
Bram Moolenaar33570922005-01-25 22:26:29 +00005883 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005884 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005886 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005888 --todo;
5889
5890 if (first)
5891 first = FALSE;
5892 else
5893 ga_concat(&ga, (char_u *)", ");
5894
5895 tofree = string_quote(hi->hi_key, FALSE);
5896 if (tofree != NULL)
5897 {
5898 ga_concat(&ga, tofree);
5899 vim_free(tofree);
5900 }
5901 ga_concat(&ga, (char_u *)": ");
5902 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5903 if (s != NULL)
5904 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005905 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005906 if (s == NULL)
5907 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005908 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005909 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005910 if (todo > 0)
5911 {
5912 vim_free(ga.ga_data);
5913 return NULL;
5914 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915
5916 ga_append(&ga, '}');
5917 ga_append(&ga, NUL);
5918 return (char_u *)ga.ga_data;
5919}
5920
5921/*
5922 * Allocate a variable for a Dictionary and fill it from "*arg".
5923 * Return OK or FAIL. Returns NOTDONE for {expr}.
5924 */
5925 static int
5926get_dict_tv(arg, rettv, evaluate)
5927 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005928 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005929 int evaluate;
5930{
Bram Moolenaar33570922005-01-25 22:26:29 +00005931 dict_T *d = NULL;
5932 typval_T tvkey;
5933 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005934 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00005935 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005936 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005937 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00005938
5939 /*
5940 * First check if it's not a curly-braces thing: {expr}.
5941 * Must do this without evaluating, otherwise a function may be called
5942 * twice. Unfortunately this means we need to call eval1() twice for the
5943 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005944 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005946 if (*start != '}')
5947 {
5948 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5949 return FAIL;
5950 if (*start == '}')
5951 return NOTDONE;
5952 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953
5954 if (evaluate)
5955 {
5956 d = dict_alloc();
5957 if (d == NULL)
5958 return FAIL;
5959 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005960 tvkey.v_type = VAR_UNKNOWN;
5961 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005962
5963 *arg = skipwhite(*arg + 1);
5964 while (**arg != '}' && **arg != NUL)
5965 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005966 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005967 goto failret;
5968 if (**arg != ':')
5969 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005970 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005971 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005972 goto failret;
5973 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005974 key = get_tv_string_buf_chk(&tvkey, buf);
5975 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005976 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005977 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
5978 if (key != NULL)
5979 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005980 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005981 goto failret;
5982 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005983
5984 *arg = skipwhite(*arg + 1);
5985 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5986 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005987 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005988 goto failret;
5989 }
5990 if (evaluate)
5991 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005992 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005993 if (item != NULL)
5994 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00005995 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005996 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005997 clear_tv(&tv);
5998 goto failret;
5999 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006000 item = dictitem_alloc(key);
6001 clear_tv(&tvkey);
6002 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006003 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006004 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006005 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006006 if (dict_add(d, item) == FAIL)
6007 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006008 }
6009 }
6010
6011 if (**arg == '}')
6012 break;
6013 if (**arg != ',')
6014 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006015 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006016 goto failret;
6017 }
6018 *arg = skipwhite(*arg + 1);
6019 }
6020
6021 if (**arg != '}')
6022 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006023 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006024failret:
6025 if (evaluate)
6026 dict_free(d);
6027 return FAIL;
6028 }
6029
6030 *arg = skipwhite(*arg + 1);
6031 if (evaluate)
6032 {
6033 rettv->v_type = VAR_DICT;
6034 rettv->vval.v_dict = d;
6035 ++d->dv_refcount;
6036 }
6037
6038 return OK;
6039}
6040
Bram Moolenaar8c711452005-01-14 21:53:12 +00006041/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 * Return a string with the string representation of a variable.
6043 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006044 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006045 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 * May return NULL;
6047 */
6048 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006049echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006050 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006052 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006054 static int recurse = 0;
6055 char_u *r = NULL;
6056
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006058 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006059 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006060 *tofree = NULL;
6061 return NULL;
6062 }
6063 ++recurse;
6064
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 switch (tv->v_type)
6066 {
6067 case VAR_FUNC:
6068 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006069 r = tv->vval.v_string;
6070 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071 case VAR_LIST:
6072 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006073 r = *tofree;
6074 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006075 case VAR_DICT:
6076 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006077 r = *tofree;
6078 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006079 case VAR_STRING:
6080 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006081 *tofree = NULL;
6082 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006084 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006085 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006086 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006087 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006088
6089 --recurse;
6090 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006091}
6092
6093/*
6094 * Return a string with the string representation of a variable.
6095 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6096 * "numbuf" is used for a number.
6097 * Puts quotes around strings, so that they can be parsed back by eval().
6098 * May return NULL;
6099 */
6100 static char_u *
6101tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006103 char_u **tofree;
6104 char_u *numbuf;
6105{
6106 switch (tv->v_type)
6107 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006108 case VAR_FUNC:
6109 *tofree = string_quote(tv->vval.v_string, TRUE);
6110 return *tofree;
6111 case VAR_STRING:
6112 *tofree = string_quote(tv->vval.v_string, FALSE);
6113 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006114 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006115 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006116 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006117 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006118 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006119 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006120 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006121 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006122}
6123
6124/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006125 * Return string "str" in ' quotes, doubling ' characters.
6126 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006127 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006128 */
6129 static char_u *
6130string_quote(str, function)
6131 char_u *str;
6132 int function;
6133{
Bram Moolenaar33570922005-01-25 22:26:29 +00006134 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006135 char_u *p, *r, *s;
6136
Bram Moolenaar33570922005-01-25 22:26:29 +00006137 len = (function ? 13 : 3);
6138 if (str != NULL)
6139 {
6140 len += STRLEN(str);
6141 for (p = str; *p != NUL; mb_ptr_adv(p))
6142 if (*p == '\'')
6143 ++len;
6144 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006145 s = r = alloc(len);
6146 if (r != NULL)
6147 {
6148 if (function)
6149 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006150 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006151 r += 10;
6152 }
6153 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006154 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006155 if (str != NULL)
6156 for (p = str; *p != NUL; )
6157 {
6158 if (*p == '\'')
6159 *r++ = '\'';
6160 MB_COPY_CHAR(p, r);
6161 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006162 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006163 if (function)
6164 *r++ = ')';
6165 *r++ = NUL;
6166 }
6167 return s;
6168}
6169
6170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 * Get the value of an environment variable.
6172 * "arg" is pointing to the '$'. It is advanced to after the name.
6173 * If the environment variable was not set, silently assume it is empty.
6174 * Always return OK.
6175 */
6176 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006177get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 int evaluate;
6181{
6182 char_u *string = NULL;
6183 int len;
6184 int cc;
6185 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006186 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187
6188 ++*arg;
6189 name = *arg;
6190 len = get_env_len(arg);
6191 if (evaluate)
6192 {
6193 if (len != 0)
6194 {
6195 cc = name[len];
6196 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006197 /* first try vim_getenv(), fast for normal environment vars */
6198 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006200 {
6201 if (!mustfree)
6202 string = vim_strsave(string);
6203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 else
6205 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006206 if (mustfree)
6207 vim_free(string);
6208
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 /* next try expanding things like $VIM and ${HOME} */
6210 string = expand_env_save(name - 1);
6211 if (string != NULL && *string == '$')
6212 {
6213 vim_free(string);
6214 string = NULL;
6215 }
6216 }
6217 name[len] = cc;
6218 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006219 rettv->v_type = VAR_STRING;
6220 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 }
6222
6223 return OK;
6224}
6225
6226/*
6227 * Array with names and number of arguments of all internal functions
6228 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6229 */
6230static struct fst
6231{
6232 char *f_name; /* function name */
6233 char f_min_argc; /* minimal number of arguments */
6234 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006235 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006236 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237} functions[] =
6238{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006239 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 {"append", 2, 2, f_append},
6241 {"argc", 0, 0, f_argc},
6242 {"argidx", 0, 0, f_argidx},
6243 {"argv", 1, 1, f_argv},
6244 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006245 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 {"bufexists", 1, 1, f_bufexists},
6247 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6248 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6249 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6250 {"buflisted", 1, 1, f_buflisted},
6251 {"bufloaded", 1, 1, f_bufloaded},
6252 {"bufname", 1, 1, f_bufname},
6253 {"bufnr", 1, 1, f_bufnr},
6254 {"bufwinnr", 1, 1, f_bufwinnr},
6255 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006256 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006257 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 {"char2nr", 1, 1, f_char2nr},
6259 {"cindent", 1, 1, f_cindent},
6260 {"col", 1, 1, f_col},
6261 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006263 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 {"cscope_connection",0,3, f_cscope_connection},
6265 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006266 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 {"delete", 1, 1, f_delete},
6268 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006269 {"diff_filler", 1, 1, f_diff_filler},
6270 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006271 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006273 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 {"eventhandler", 0, 0, f_eventhandler},
6275 {"executable", 1, 1, f_executable},
6276 {"exists", 1, 1, f_exists},
6277 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006278 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6280 {"filereadable", 1, 1, f_filereadable},
6281 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006282 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006283 {"finddir", 1, 3, f_finddir},
6284 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 {"fnamemodify", 2, 2, f_fnamemodify},
6286 {"foldclosed", 1, 1, f_foldclosed},
6287 {"foldclosedend", 1, 1, f_foldclosedend},
6288 {"foldlevel", 1, 1, f_foldlevel},
6289 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006290 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006292 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006293 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 {"getbufvar", 2, 2, f_getbufvar},
6295 {"getchar", 0, 1, f_getchar},
6296 {"getcharmod", 0, 0, f_getcharmod},
6297 {"getcmdline", 0, 0, f_getcmdline},
6298 {"getcmdpos", 0, 0, f_getcmdpos},
6299 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006300 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006301 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 {"getfsize", 1, 1, f_getfsize},
6303 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006304 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006305 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006306 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006307 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 {"getregtype", 0, 1, f_getregtype},
6309 {"getwinposx", 0, 0, f_getwinposx},
6310 {"getwinposy", 0, 0, f_getwinposy},
6311 {"getwinvar", 2, 2, f_getwinvar},
6312 {"glob", 1, 1, f_glob},
6313 {"globpath", 2, 2, f_globpath},
6314 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006315 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 {"hasmapto", 1, 2, f_hasmapto},
6317 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6318 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6319 {"histadd", 2, 2, f_histadd},
6320 {"histdel", 1, 2, f_histdel},
6321 {"histget", 1, 2, f_histget},
6322 {"histnr", 1, 1, f_histnr},
6323 {"hlID", 1, 1, f_hlID},
6324 {"hlexists", 1, 1, f_hlexists},
6325 {"hostname", 0, 0, f_hostname},
6326 {"iconv", 3, 3, f_iconv},
6327 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006328 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 {"input", 1, 2, f_input},
6330 {"inputdialog", 1, 3, f_inputdialog},
6331 {"inputrestore", 0, 0, f_inputrestore},
6332 {"inputsave", 0, 0, f_inputsave},
6333 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006334 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006336 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006337 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006338 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006339 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006341 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 {"libcall", 3, 3, f_libcall},
6343 {"libcallnr", 3, 3, f_libcallnr},
6344 {"line", 1, 1, f_line},
6345 {"line2byte", 1, 1, f_line2byte},
6346 {"lispindent", 1, 1, f_lispindent},
6347 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006348 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 {"maparg", 1, 2, f_maparg},
6350 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006351 {"match", 2, 4, f_match},
6352 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006353 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006354 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006355 {"max", 1, 1, f_max},
6356 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006357#ifdef vim_mkdir
6358 {"mkdir", 1, 3, f_mkdir},
6359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 {"mode", 0, 0, f_mode},
6361 {"nextnonblank", 1, 1, f_nextnonblank},
6362 {"nr2char", 1, 1, f_nr2char},
6363 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006364 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006365 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 {"remote_expr", 2, 3, f_remote_expr},
6367 {"remote_foreground", 1, 1, f_remote_foreground},
6368 {"remote_peek", 1, 2, f_remote_peek},
6369 {"remote_read", 1, 1, f_remote_read},
6370 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006371 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006373 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006375 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 {"search", 1, 2, f_search},
6377 {"searchpair", 3, 5, f_searchpair},
6378 {"server2client", 2, 2, f_server2client},
6379 {"serverlist", 0, 0, f_serverlist},
6380 {"setbufvar", 3, 3, f_setbufvar},
6381 {"setcmdpos", 1, 1, f_setcmdpos},
6382 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006383 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 {"setreg", 2, 3, f_setreg},
6385 {"setwinvar", 3, 3, f_setwinvar},
6386 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006387 {"sort", 1, 2, f_sort},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006388 {"spellbadword", 0, 0, f_spellbadword},
6389 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006390 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391#ifdef HAVE_STRFTIME
6392 {"strftime", 1, 2, f_strftime},
6393#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006395 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 {"strlen", 1, 1, f_strlen},
6397 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006398 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 {"strtrans", 1, 1, f_strtrans},
6400 {"submatch", 1, 1, f_submatch},
6401 {"substitute", 4, 4, f_substitute},
6402 {"synID", 3, 3, f_synID},
6403 {"synIDattr", 2, 3, f_synIDattr},
6404 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006405 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006406 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 {"tempname", 0, 0, f_tempname},
6408 {"tolower", 1, 1, f_tolower},
6409 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006410 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006412 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 {"virtcol", 1, 1, f_virtcol},
6414 {"visualmode", 0, 1, f_visualmode},
6415 {"winbufnr", 1, 1, f_winbufnr},
6416 {"wincol", 0, 0, f_wincol},
6417 {"winheight", 1, 1, f_winheight},
6418 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006419 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 {"winrestcmd", 0, 0, f_winrestcmd},
6421 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006422 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423};
6424
6425#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6426
6427/*
6428 * Function given to ExpandGeneric() to obtain the list of internal
6429 * or user defined function names.
6430 */
6431 char_u *
6432get_function_name(xp, idx)
6433 expand_T *xp;
6434 int idx;
6435{
6436 static int intidx = -1;
6437 char_u *name;
6438
6439 if (idx == 0)
6440 intidx = -1;
6441 if (intidx < 0)
6442 {
6443 name = get_user_func_name(xp, idx);
6444 if (name != NULL)
6445 return name;
6446 }
6447 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6448 {
6449 STRCPY(IObuff, functions[intidx].f_name);
6450 STRCAT(IObuff, "(");
6451 if (functions[intidx].f_max_argc == 0)
6452 STRCAT(IObuff, ")");
6453 return IObuff;
6454 }
6455
6456 return NULL;
6457}
6458
6459/*
6460 * Function given to ExpandGeneric() to obtain the list of internal or
6461 * user defined variable or function names.
6462 */
6463/*ARGSUSED*/
6464 char_u *
6465get_expr_name(xp, idx)
6466 expand_T *xp;
6467 int idx;
6468{
6469 static int intidx = -1;
6470 char_u *name;
6471
6472 if (idx == 0)
6473 intidx = -1;
6474 if (intidx < 0)
6475 {
6476 name = get_function_name(xp, idx);
6477 if (name != NULL)
6478 return name;
6479 }
6480 return get_user_var_name(xp, ++intidx);
6481}
6482
6483#endif /* FEAT_CMDL_COMPL */
6484
6485/*
6486 * Find internal function in table above.
6487 * Return index, or -1 if not found
6488 */
6489 static int
6490find_internal_func(name)
6491 char_u *name; /* name of the function */
6492{
6493 int first = 0;
6494 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6495 int cmp;
6496 int x;
6497
6498 /*
6499 * Find the function name in the table. Binary search.
6500 */
6501 while (first <= last)
6502 {
6503 x = first + ((unsigned)(last - first) >> 1);
6504 cmp = STRCMP(name, functions[x].f_name);
6505 if (cmp < 0)
6506 last = x - 1;
6507 else if (cmp > 0)
6508 first = x + 1;
6509 else
6510 return x;
6511 }
6512 return -1;
6513}
6514
6515/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006516 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6517 * name it contains, otherwise return "name".
6518 */
6519 static char_u *
6520deref_func_name(name, lenp)
6521 char_u *name;
6522 int *lenp;
6523{
Bram Moolenaar33570922005-01-25 22:26:29 +00006524 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006525 int cc;
6526
6527 cc = name[*lenp];
6528 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006529 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006531 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006533 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534 {
6535 *lenp = 0;
6536 return (char_u *)""; /* just in case */
6537 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006538 *lenp = STRLEN(v->di_tv.vval.v_string);
6539 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 }
6541
6542 return name;
6543}
6544
6545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 * Allocate a variable for the result of a function.
6547 * Return OK or FAIL.
6548 */
6549 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006550get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6551 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 char_u *name; /* name of the function */
6553 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 char_u **arg; /* argument, pointing to the '(' */
6556 linenr_T firstline; /* first line of range */
6557 linenr_T lastline; /* last line of range */
6558 int *doesrange; /* return: function handled range */
6559 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006560 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561{
6562 char_u *argp;
6563 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006564 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 int argcount = 0; /* number of arguments found */
6566
6567 /*
6568 * Get the arguments.
6569 */
6570 argp = *arg;
6571 while (argcount < MAX_FUNC_ARGS)
6572 {
6573 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6574 if (*argp == ')' || *argp == ',' || *argp == NUL)
6575 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6577 {
6578 ret = FAIL;
6579 break;
6580 }
6581 ++argcount;
6582 if (*argp != ',')
6583 break;
6584 }
6585 if (*argp == ')')
6586 ++argp;
6587 else
6588 ret = FAIL;
6589
6590 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006591 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006592 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006594 {
6595 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006596 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006597 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006598 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600
6601 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006602 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603
6604 *arg = skipwhite(argp);
6605 return ret;
6606}
6607
6608
6609/*
6610 * Call a function with its resolved parameters
6611 * Return OK or FAIL.
6612 */
6613 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006614call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006615 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 char_u *name; /* name of the function */
6617 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006618 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006620 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 linenr_T firstline; /* first line of range */
6622 linenr_T lastline; /* last line of range */
6623 int *doesrange; /* return: function handled range */
6624 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006625 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626{
6627 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628#define ERROR_UNKNOWN 0
6629#define ERROR_TOOMANY 1
6630#define ERROR_TOOFEW 2
6631#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006632#define ERROR_DICT 4
6633#define ERROR_NONE 5
6634#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 int error = ERROR_NONE;
6636 int i;
6637 int llen;
6638 ufunc_T *fp;
6639 int cc;
6640#define FLEN_FIXED 40
6641 char_u fname_buf[FLEN_FIXED + 1];
6642 char_u *fname;
6643
6644 /*
6645 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6646 * Change <SNR>123_name() to K_SNR 123_name().
6647 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6648 */
6649 cc = name[len];
6650 name[len] = NUL;
6651 llen = eval_fname_script(name);
6652 if (llen > 0)
6653 {
6654 fname_buf[0] = K_SPECIAL;
6655 fname_buf[1] = KS_EXTRA;
6656 fname_buf[2] = (int)KE_SNR;
6657 i = 3;
6658 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6659 {
6660 if (current_SID <= 0)
6661 error = ERROR_SCRIPT;
6662 else
6663 {
6664 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6665 i = (int)STRLEN(fname_buf);
6666 }
6667 }
6668 if (i + STRLEN(name + llen) < FLEN_FIXED)
6669 {
6670 STRCPY(fname_buf + i, name + llen);
6671 fname = fname_buf;
6672 }
6673 else
6674 {
6675 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
6676 if (fname == NULL)
6677 error = ERROR_OTHER;
6678 else
6679 {
6680 mch_memmove(fname, fname_buf, (size_t)i);
6681 STRCPY(fname + i, name + llen);
6682 }
6683 }
6684 }
6685 else
6686 fname = name;
6687
6688 *doesrange = FALSE;
6689
6690
6691 /* execute the function if no errors detected and executing */
6692 if (evaluate && error == ERROR_NONE)
6693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006694 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 error = ERROR_UNKNOWN;
6696
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006697 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 {
6699 /*
6700 * User defined function.
6701 */
6702 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006703
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006705 /* Trigger FuncUndefined event, may load the function. */
6706 if (fp == NULL
6707 && apply_autocmds(EVENT_FUNCUNDEFINED,
6708 fname, fname, TRUE, NULL)
6709 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006711 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 fp = find_func(fname);
6713 }
6714#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006715 /* Try loading a package. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006716 if (fp == NULL && script_autoload(fname) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006717 {
6718 /* loaded a package, search for the function again */
6719 fp = find_func(fname);
6720 }
6721
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 if (fp != NULL)
6723 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006724 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006726 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006728 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006730 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006731 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 else
6733 {
6734 /*
6735 * Call the user function.
6736 * Save and restore search patterns, script variables and
6737 * redo buffer.
6738 */
6739 save_search_patterns();
6740 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006741 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006742 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006743 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006744 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
6745 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
6746 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006747 /* Function was unreferenced while being used, free it
6748 * now. */
6749 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 restoreRedobuff();
6751 restore_search_patterns();
6752 error = ERROR_NONE;
6753 }
6754 }
6755 }
6756 else
6757 {
6758 /*
6759 * Find the function name in the table, call its implementation.
6760 */
6761 i = find_internal_func(fname);
6762 if (i >= 0)
6763 {
6764 if (argcount < functions[i].f_min_argc)
6765 error = ERROR_TOOFEW;
6766 else if (argcount > functions[i].f_max_argc)
6767 error = ERROR_TOOMANY;
6768 else
6769 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006770 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006771 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 error = ERROR_NONE;
6773 }
6774 }
6775 }
6776 /*
6777 * The function call (or "FuncUndefined" autocommand sequence) might
6778 * have been aborted by an error, an interrupt, or an explicitly thrown
6779 * exception that has not been caught so far. This situation can be
6780 * tested for by calling aborting(). For an error in an internal
6781 * function or for the "E132" error in call_user_func(), however, the
6782 * throw point at which the "force_abort" flag (temporarily reset by
6783 * emsg()) is normally updated has not been reached yet. We need to
6784 * update that flag first to make aborting() reliable.
6785 */
6786 update_force_abort();
6787 }
6788 if (error == ERROR_NONE)
6789 ret = OK;
6790
6791 /*
6792 * Report an error unless the argument evaluation or function call has been
6793 * cancelled due to an aborting error, an interrupt, or an exception.
6794 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006795 if (!aborting())
6796 {
6797 switch (error)
6798 {
6799 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006800 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006801 break;
6802 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006803 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006804 break;
6805 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006806 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006807 name);
6808 break;
6809 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006810 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006811 name);
6812 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006813 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006814 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00006815 name);
6816 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006817 }
6818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819
6820 name[len] = cc;
6821 if (fname != name && fname != fname_buf)
6822 vim_free(fname);
6823
6824 return ret;
6825}
6826
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006827/*
6828 * Give an error message with a function name. Handle <SNR> things.
6829 */
6830 static void
6831emsg_funcname(msg, name)
6832 char *msg;
6833 char_u *name;
6834{
6835 char_u *p;
6836
6837 if (*name == K_SPECIAL)
6838 p = concat_str((char_u *)"<SNR>", name + 3);
6839 else
6840 p = name;
6841 EMSG2(_(msg), p);
6842 if (p != name)
6843 vim_free(p);
6844}
6845
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846/*********************************************
6847 * Implementation of the built-in functions
6848 */
6849
6850/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006851 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 */
6853 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006854f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006855 typval_T *argvars;
6856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857{
Bram Moolenaar33570922005-01-25 22:26:29 +00006858 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006860 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006861 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006863 if ((l = argvars[0].vval.v_list) != NULL
6864 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
6865 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006866 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006867 }
6868 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006869 EMSG(_(e_listreq));
6870}
6871
6872/*
6873 * "append(lnum, string/list)" function
6874 */
6875 static void
6876f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006877 typval_T *argvars;
6878 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006879{
6880 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006881 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00006882 list_T *l = NULL;
6883 listitem_T *li = NULL;
6884 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006885 long added = 0;
6886
Bram Moolenaar0d660222005-01-07 21:51:51 +00006887 lnum = get_tv_lnum(argvars);
6888 if (lnum >= 0
6889 && lnum <= curbuf->b_ml.ml_line_count
6890 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006891 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006892 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006893 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006894 l = argvars[1].vval.v_list;
6895 if (l == NULL)
6896 return;
6897 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006898 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006899 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00006900 for (;;)
6901 {
6902 if (l == NULL)
6903 tv = &argvars[1]; /* append a string */
6904 else if (li == NULL)
6905 break; /* end of list */
6906 else
6907 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006908 line = get_tv_string_chk(tv);
6909 if (line == NULL) /* type error */
6910 {
6911 rettv->vval.v_number = 1; /* Failed */
6912 break;
6913 }
6914 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00006915 ++added;
6916 if (l == NULL)
6917 break;
6918 li = li->li_next;
6919 }
6920
6921 appended_lines_mark(lnum, added);
6922 if (curwin->w_cursor.lnum > lnum)
6923 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006925 else
6926 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927}
6928
6929/*
6930 * "argc()" function
6931 */
6932/* ARGSUSED */
6933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006934f_argc(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006938 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939}
6940
6941/*
6942 * "argidx()" function
6943 */
6944/* ARGSUSED */
6945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006946f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006947 typval_T *argvars;
6948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006950 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951}
6952
6953/*
6954 * "argv(nr)" function
6955 */
6956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006957f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006958 typval_T *argvars;
6959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960{
6961 int idx;
6962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006963 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006965 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006967 rettv->vval.v_string = NULL;
6968 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969}
6970
6971/*
6972 * "browse(save, title, initdir, default)" function
6973 */
6974/* ARGSUSED */
6975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006976f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 typval_T *argvars;
6978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979{
6980#ifdef FEAT_BROWSE
6981 int save;
6982 char_u *title;
6983 char_u *initdir;
6984 char_u *defname;
6985 char_u buf[NUMBUFLEN];
6986 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006987 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006989 save = get_tv_number_chk(&argvars[0], &error);
6990 title = get_tv_string_chk(&argvars[1]);
6991 initdir = get_tv_string_buf_chk(&argvars[2], buf);
6992 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006994 if (error || title == NULL || initdir == NULL || defname == NULL)
6995 rettv->vval.v_string = NULL;
6996 else
6997 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006998 do_browse(save ? BROWSE_SAVE : 0,
6999 title, defname, NULL, initdir, NULL, curbuf);
7000#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007001 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007002#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007003 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007004}
7005
7006/*
7007 * "browsedir(title, initdir)" function
7008 */
7009/* ARGSUSED */
7010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007011f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007012 typval_T *argvars;
7013 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007014{
7015#ifdef FEAT_BROWSE
7016 char_u *title;
7017 char_u *initdir;
7018 char_u buf[NUMBUFLEN];
7019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007020 title = get_tv_string_chk(&argvars[0]);
7021 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007022
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007023 if (title == NULL || initdir == NULL)
7024 rettv->vval.v_string = NULL;
7025 else
7026 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007027 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007029 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007031 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032}
7033
Bram Moolenaar33570922005-01-25 22:26:29 +00007034static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007035
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036/*
7037 * Find a buffer by number or exact name.
7038 */
7039 static buf_T *
7040find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007041 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
7043 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007045 if (avar->v_type == VAR_NUMBER)
7046 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007047 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007049 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007050 if (buf == NULL)
7051 {
7052 /* No full path name match, try a match with a URL or a "nofile"
7053 * buffer, these don't use the full path. */
7054 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7055 if (buf->b_fname != NULL
7056 && (path_with_url(buf->b_fname)
7057#ifdef FEAT_QUICKFIX
7058 || bt_nofile(buf)
7059#endif
7060 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007061 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007062 break;
7063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 }
7065 return buf;
7066}
7067
7068/*
7069 * "bufexists(expr)" function
7070 */
7071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007072f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007073 typval_T *argvars;
7074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007076 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077}
7078
7079/*
7080 * "buflisted(expr)" function
7081 */
7082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007083f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007084 typval_T *argvars;
7085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086{
7087 buf_T *buf;
7088
7089 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007090 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091}
7092
7093/*
7094 * "bufloaded(expr)" function
7095 */
7096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007097f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007098 typval_T *argvars;
7099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100{
7101 buf_T *buf;
7102
7103 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007104 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105}
7106
Bram Moolenaar33570922005-01-25 22:26:29 +00007107static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007108
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109/*
7110 * Get buffer by number or pattern.
7111 */
7112 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007113get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007114 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007116 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 int save_magic;
7118 char_u *save_cpo;
7119 buf_T *buf;
7120
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007121 if (tv->v_type == VAR_NUMBER)
7122 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007123 if (tv->v_type != VAR_STRING)
7124 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 if (name == NULL || *name == NUL)
7126 return curbuf;
7127 if (name[0] == '$' && name[1] == NUL)
7128 return lastbuf;
7129
7130 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7131 save_magic = p_magic;
7132 p_magic = TRUE;
7133 save_cpo = p_cpo;
7134 p_cpo = (char_u *)"";
7135
7136 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7137 TRUE, FALSE));
7138
7139 p_magic = save_magic;
7140 p_cpo = save_cpo;
7141
7142 /* If not found, try expanding the name, like done for bufexists(). */
7143 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007144 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
7146 return buf;
7147}
7148
7149/*
7150 * "bufname(expr)" function
7151 */
7152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007153f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 typval_T *argvars;
7155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156{
7157 buf_T *buf;
7158
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007159 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007161 buf = get_buf_tv(&argvars[0]);
7162 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007164 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007166 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 --emsg_off;
7168}
7169
7170/*
7171 * "bufnr(expr)" function
7172 */
7173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007174f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 typval_T *argvars;
7176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177{
7178 buf_T *buf;
7179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007180 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007182 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007184 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007186 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 --emsg_off;
7188}
7189
7190/*
7191 * "bufwinnr(nr)" function
7192 */
7193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007194f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007195 typval_T *argvars;
7196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
7198#ifdef FEAT_WINDOWS
7199 win_T *wp;
7200 int winnr = 0;
7201#endif
7202 buf_T *buf;
7203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007204 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007206 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207#ifdef FEAT_WINDOWS
7208 for (wp = firstwin; wp; wp = wp->w_next)
7209 {
7210 ++winnr;
7211 if (wp->w_buffer == buf)
7212 break;
7213 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007214 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007216 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217#endif
7218 --emsg_off;
7219}
7220
7221/*
7222 * "byte2line(byte)" function
7223 */
7224/*ARGSUSED*/
7225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007226f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007227 typval_T *argvars;
7228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
7230#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007231 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232#else
7233 long boff = 0;
7234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007235 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007237 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007239 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 (linenr_T)0, &boff);
7241#endif
7242}
7243
7244/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007245 * "byteidx()" function
7246 */
7247/*ARGSUSED*/
7248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007249f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007250 typval_T *argvars;
7251 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007252{
7253#ifdef FEAT_MBYTE
7254 char_u *t;
7255#endif
7256 char_u *str;
7257 long idx;
7258
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007259 str = get_tv_string_chk(&argvars[0]);
7260 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007261 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007262 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007263 return;
7264
7265#ifdef FEAT_MBYTE
7266 t = str;
7267 for ( ; idx > 0; idx--)
7268 {
7269 if (*t == NUL) /* EOL reached */
7270 return;
7271 t += mb_ptr2len_check(t);
7272 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007273 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007274#else
7275 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007276 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007277#endif
7278}
7279
7280/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007281 * "call(func, arglist)" function
7282 */
7283 static void
7284f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 typval_T *argvars;
7286 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007287{
7288 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007290 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007291 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007292 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007293 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007294
7295 rettv->vval.v_number = 0;
7296 if (argvars[1].v_type != VAR_LIST)
7297 {
7298 EMSG(_(e_listreq));
7299 return;
7300 }
7301 if (argvars[1].vval.v_list == NULL)
7302 return;
7303
7304 if (argvars[0].v_type == VAR_FUNC)
7305 func = argvars[0].vval.v_string;
7306 else
7307 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007308 if (*func == NUL)
7309 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007310
Bram Moolenaare9a41262005-01-15 22:18:47 +00007311 if (argvars[2].v_type != VAR_UNKNOWN)
7312 {
7313 if (argvars[2].v_type != VAR_DICT)
7314 {
7315 EMSG(_(e_dictreq));
7316 return;
7317 }
7318 selfdict = argvars[2].vval.v_dict;
7319 }
7320
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007321 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7322 item = item->li_next)
7323 {
7324 if (argc == MAX_FUNC_ARGS)
7325 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007326 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007327 break;
7328 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007329 /* Make a copy of each argument. This is needed to be able to set
7330 * v_lock to VAR_FIXED in the copy without changing the original list.
7331 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007332 copy_tv(&item->li_tv, &argv[argc++]);
7333 }
7334
7335 if (item == NULL)
7336 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007337 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7338 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007339
7340 /* Free the arguments. */
7341 while (argc > 0)
7342 clear_tv(&argv[--argc]);
7343}
7344
7345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 * "char2nr(string)" function
7347 */
7348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007349f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 typval_T *argvars;
7351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352{
7353#ifdef FEAT_MBYTE
7354 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007355 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 else
7357#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007358 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359}
7360
7361/*
7362 * "cindent(lnum)" function
7363 */
7364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007365f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007366 typval_T *argvars;
7367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368{
7369#ifdef FEAT_CINDENT
7370 pos_T pos;
7371 linenr_T lnum;
7372
7373 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007374 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7376 {
7377 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007378 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 curwin->w_cursor = pos;
7380 }
7381 else
7382#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007383 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384}
7385
7386/*
7387 * "col(string)" function
7388 */
7389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007390f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 typval_T *argvars;
7392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393{
7394 colnr_T col = 0;
7395 pos_T *fp;
7396
7397 fp = var2fpos(&argvars[0], FALSE);
7398 if (fp != NULL)
7399 {
7400 if (fp->col == MAXCOL)
7401 {
7402 /* '> can be MAXCOL, get the length of the line then */
7403 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7404 col = STRLEN(ml_get(fp->lnum)) + 1;
7405 else
7406 col = MAXCOL;
7407 }
7408 else
7409 {
7410 col = fp->col + 1;
7411#ifdef FEAT_VIRTUALEDIT
7412 /* col(".") when the cursor is on the NUL at the end of the line
7413 * because of "coladd" can be seen as an extra column. */
7414 if (virtual_active() && fp == &curwin->w_cursor)
7415 {
7416 char_u *p = ml_get_cursor();
7417
7418 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7419 curwin->w_virtcol - curwin->w_cursor.coladd))
7420 {
7421# ifdef FEAT_MBYTE
7422 int l;
7423
7424 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7425 col += l;
7426# else
7427 if (*p != NUL && p[1] == NUL)
7428 ++col;
7429# endif
7430 }
7431 }
7432#endif
7433 }
7434 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007435 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436}
7437
7438/*
7439 * "confirm(message, buttons[, default [, type]])" function
7440 */
7441/*ARGSUSED*/
7442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007443f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007444 typval_T *argvars;
7445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446{
7447#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7448 char_u *message;
7449 char_u *buttons = NULL;
7450 char_u buf[NUMBUFLEN];
7451 char_u buf2[NUMBUFLEN];
7452 int def = 1;
7453 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007454 char_u *typestr;
7455 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007457 message = get_tv_string_chk(&argvars[0]);
7458 if (message == NULL)
7459 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007460 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007462 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7463 if (buttons == NULL)
7464 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007465 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007467 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007468 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007470 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7471 if (typestr == NULL)
7472 error = TRUE;
7473 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007475 switch (TOUPPER_ASC(*typestr))
7476 {
7477 case 'E': type = VIM_ERROR; break;
7478 case 'Q': type = VIM_QUESTION; break;
7479 case 'I': type = VIM_INFO; break;
7480 case 'W': type = VIM_WARNING; break;
7481 case 'G': type = VIM_GENERIC; break;
7482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 }
7484 }
7485 }
7486 }
7487
7488 if (buttons == NULL || *buttons == NUL)
7489 buttons = (char_u *)_("&Ok");
7490
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007491 if (error)
7492 rettv->vval.v_number = 0;
7493 else
7494 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 def, NULL);
7496#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007497 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498#endif
7499}
7500
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007501/*
7502 * "copy()" function
7503 */
7504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007505f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007506 typval_T *argvars;
7507 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007508{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007509 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007510}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511
7512/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007513 * "count()" function
7514 */
7515 static void
7516f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007517 typval_T *argvars;
7518 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007519{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007520 long n = 0;
7521 int ic = FALSE;
7522
Bram Moolenaare9a41262005-01-15 22:18:47 +00007523 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007524 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007525 listitem_T *li;
7526 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007527 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007528
Bram Moolenaare9a41262005-01-15 22:18:47 +00007529 if ((l = argvars[0].vval.v_list) != NULL)
7530 {
7531 li = l->lv_first;
7532 if (argvars[2].v_type != VAR_UNKNOWN)
7533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007534 int error = FALSE;
7535
7536 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007537 if (argvars[3].v_type != VAR_UNKNOWN)
7538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007539 idx = get_tv_number_chk(&argvars[3], &error);
7540 if (!error)
7541 {
7542 li = list_find(l, idx);
7543 if (li == NULL)
7544 EMSGN(_(e_listidx), idx);
7545 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007546 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007547 if (error)
7548 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007549 }
7550
7551 for ( ; li != NULL; li = li->li_next)
7552 if (tv_equal(&li->li_tv, &argvars[1], ic))
7553 ++n;
7554 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007555 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007556 else if (argvars[0].v_type == VAR_DICT)
7557 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007558 int todo;
7559 dict_T *d;
7560 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007561
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007562 if ((d = argvars[0].vval.v_dict) != NULL)
7563 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007564 int error = FALSE;
7565
Bram Moolenaare9a41262005-01-15 22:18:47 +00007566 if (argvars[2].v_type != VAR_UNKNOWN)
7567 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007568 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007569 if (argvars[3].v_type != VAR_UNKNOWN)
7570 EMSG(_(e_invarg));
7571 }
7572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007573 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007574 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007575 {
7576 if (!HASHITEM_EMPTY(hi))
7577 {
7578 --todo;
7579 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7580 ++n;
7581 }
7582 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007583 }
7584 }
7585 else
7586 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007587 rettv->vval.v_number = n;
7588}
7589
7590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7592 *
7593 * Checks the existence of a cscope connection.
7594 */
7595/*ARGSUSED*/
7596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007597f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 typval_T *argvars;
7599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600{
7601#ifdef FEAT_CSCOPE
7602 int num = 0;
7603 char_u *dbpath = NULL;
7604 char_u *prepend = NULL;
7605 char_u buf[NUMBUFLEN];
7606
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007607 if (argvars[0].v_type != VAR_UNKNOWN
7608 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007610 num = (int)get_tv_number(&argvars[0]);
7611 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007612 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007613 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 }
7615
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007616 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007618 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619#endif
7620}
7621
7622/*
7623 * "cursor(lnum, col)" function
7624 *
7625 * Moves the cursor to the specified line and column
7626 */
7627/*ARGSUSED*/
7628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007629f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007630 typval_T *argvars;
7631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632{
7633 long line, col;
7634
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007635 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007636 col = get_tv_number_chk(&argvars[1], NULL);
7637 if (line < 0 || col < 0)
7638 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 if (line > 0)
7640 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 if (col > 0)
7642 curwin->w_cursor.col = col - 1;
7643#ifdef FEAT_VIRTUALEDIT
7644 curwin->w_cursor.coladd = 0;
7645#endif
7646
7647 /* Make sure the cursor is in a valid position. */
7648 check_cursor();
7649#ifdef FEAT_MBYTE
7650 /* Correct cursor for multi-byte character. */
7651 if (has_mbyte)
7652 mb_adjust_cursor();
7653#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007654
7655 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656}
7657
7658/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007659 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 */
7661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007662f_deepcopy(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{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007666 static int copyID = 0;
7667 int noref = 0;
7668
7669 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007670 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007671 if (noref < 0 || noref > 1)
7672 EMSG(_(e_invarg));
7673 else
7674 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675}
7676
7677/*
7678 * "delete()" function
7679 */
7680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007681f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 typval_T *argvars;
7683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684{
7685 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007686 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007688 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689}
7690
7691/*
7692 * "did_filetype()" function
7693 */
7694/*ARGSUSED*/
7695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007696f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007697 typval_T *argvars;
7698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
7700#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007701 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007703 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704#endif
7705}
7706
7707/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00007708 * "diff_filler()" function
7709 */
7710/*ARGSUSED*/
7711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007712f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007713 typval_T *argvars;
7714 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007715{
7716#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007717 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00007718#endif
7719}
7720
7721/*
7722 * "diff_hlID()" function
7723 */
7724/*ARGSUSED*/
7725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007726f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007727 typval_T *argvars;
7728 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007729{
7730#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007731 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00007732 static linenr_T prev_lnum = 0;
7733 static int changedtick = 0;
7734 static int fnum = 0;
7735 static int change_start = 0;
7736 static int change_end = 0;
7737 static enum hlf_value hlID = 0;
7738 int filler_lines;
7739 int col;
7740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007741 if (lnum < 0) /* ignore type error in {lnum} arg */
7742 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007743 if (lnum != prev_lnum
7744 || changedtick != curbuf->b_changedtick
7745 || fnum != curbuf->b_fnum)
7746 {
7747 /* New line, buffer, change: need to get the values. */
7748 filler_lines = diff_check(curwin, lnum);
7749 if (filler_lines < 0)
7750 {
7751 if (filler_lines == -1)
7752 {
7753 change_start = MAXCOL;
7754 change_end = -1;
7755 if (diff_find_change(curwin, lnum, &change_start, &change_end))
7756 hlID = HLF_ADD; /* added line */
7757 else
7758 hlID = HLF_CHD; /* changed line */
7759 }
7760 else
7761 hlID = HLF_ADD; /* added line */
7762 }
7763 else
7764 hlID = (enum hlf_value)0;
7765 prev_lnum = lnum;
7766 changedtick = curbuf->b_changedtick;
7767 fnum = curbuf->b_fnum;
7768 }
7769
7770 if (hlID == HLF_CHD || hlID == HLF_TXD)
7771 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007772 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00007773 if (col >= change_start && col <= change_end)
7774 hlID = HLF_TXD; /* changed text */
7775 else
7776 hlID = HLF_CHD; /* changed line */
7777 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007778 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007779#endif
7780}
7781
7782/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007783 * "empty({expr})" function
7784 */
7785 static void
7786f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007787 typval_T *argvars;
7788 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007789{
7790 int n;
7791
7792 switch (argvars[0].v_type)
7793 {
7794 case VAR_STRING:
7795 case VAR_FUNC:
7796 n = argvars[0].vval.v_string == NULL
7797 || *argvars[0].vval.v_string == NUL;
7798 break;
7799 case VAR_NUMBER:
7800 n = argvars[0].vval.v_number == 0;
7801 break;
7802 case VAR_LIST:
7803 n = argvars[0].vval.v_list == NULL
7804 || argvars[0].vval.v_list->lv_first == NULL;
7805 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007806 case VAR_DICT:
7807 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00007808 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007809 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007810 default:
7811 EMSG2(_(e_intern2), "f_empty()");
7812 n = 0;
7813 }
7814
7815 rettv->vval.v_number = n;
7816}
7817
7818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 * "escape({string}, {chars})" function
7820 */
7821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007822f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007823 typval_T *argvars;
7824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825{
7826 char_u buf[NUMBUFLEN];
7827
Bram Moolenaar758711c2005-02-02 23:11:38 +00007828 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
7829 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007830 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831}
7832
7833/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007834 * "eval()" function
7835 */
7836/*ARGSUSED*/
7837 static void
7838f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007839 typval_T *argvars;
7840 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007841{
7842 char_u *s;
7843
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007844 s = get_tv_string_chk(&argvars[0]);
7845 if (s != NULL)
7846 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007847
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007848 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
7849 {
7850 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007851 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007852 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007853 else if (*s != NUL)
7854 EMSG(_(e_trailing));
7855}
7856
7857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 * "eventhandler()" function
7859 */
7860/*ARGSUSED*/
7861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007862f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007863 typval_T *argvars;
7864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007866 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867}
7868
7869/*
7870 * "executable()" function
7871 */
7872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007873f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007874 typval_T *argvars;
7875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007877 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878}
7879
7880/*
7881 * "exists()" function
7882 */
7883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007884f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007885 typval_T *argvars;
7886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887{
7888 char_u *p;
7889 char_u *name;
7890 int n = FALSE;
7891 int len = 0;
7892
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007893 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 if (*p == '$') /* environment variable */
7895 {
7896 /* first try "normal" environment variables (fast) */
7897 if (mch_getenv(p + 1) != NULL)
7898 n = TRUE;
7899 else
7900 {
7901 /* try expanding things like $VIM and ${HOME} */
7902 p = expand_env_save(p);
7903 if (p != NULL && *p != '$')
7904 n = TRUE;
7905 vim_free(p);
7906 }
7907 }
7908 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007909 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 else if (*p == '*') /* internal or user defined function */
7911 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007912 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 }
7914 else if (*p == ':')
7915 {
7916 n = cmd_exists(p + 1);
7917 }
7918 else if (*p == '#')
7919 {
7920#ifdef FEAT_AUTOCMD
7921 name = p + 1;
7922 p = vim_strchr(name, '#');
7923 if (p != NULL)
7924 n = au_exists(name, p, p + 1);
7925 else
7926 n = au_exists(name, name + STRLEN(name), NULL);
7927#endif
7928 }
7929 else /* internal variable */
7930 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007931 char_u *tofree;
7932 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007934 /* get_name_len() takes care of expanding curly braces */
7935 name = p;
7936 len = get_name_len(&p, &tofree, TRUE, FALSE);
7937 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007939 if (tofree != NULL)
7940 name = tofree;
7941 n = (get_var_tv(name, len, &tv, FALSE) == OK);
7942 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007944 /* handle d.key, l[idx], f(expr) */
7945 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
7946 if (n)
7947 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 }
7949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007951 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 }
7953
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007954 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955}
7956
7957/*
7958 * "expand()" function
7959 */
7960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007961f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007962 typval_T *argvars;
7963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964{
7965 char_u *s;
7966 int len;
7967 char_u *errormsg;
7968 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7969 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007970 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007972 rettv->v_type = VAR_STRING;
7973 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 if (*s == '%' || *s == '#' || *s == '<')
7975 {
7976 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007977 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 --emsg_off;
7979 }
7980 else
7981 {
7982 /* When the optional second argument is non-zero, don't remove matches
7983 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007984 if (argvars[1].v_type != VAR_UNKNOWN
7985 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007987 if (!error)
7988 {
7989 ExpandInit(&xpc);
7990 xpc.xp_context = EXPAND_FILES;
7991 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
7992 ExpandCleanup(&xpc);
7993 }
7994 else
7995 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 }
7997}
7998
7999/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008000 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008001 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008002 */
8003 static void
8004f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008005 typval_T *argvars;
8006 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008007{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008008 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008009 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008010 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008011 list_T *l1, *l2;
8012 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008013 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008014 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008015
Bram Moolenaare9a41262005-01-15 22:18:47 +00008016 l1 = argvars[0].vval.v_list;
8017 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008018 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8019 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008020 {
8021 if (argvars[2].v_type != VAR_UNKNOWN)
8022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008023 before = get_tv_number_chk(&argvars[2], &error);
8024 if (error)
8025 return; /* type error; errmsg already given */
8026
Bram Moolenaar758711c2005-02-02 23:11:38 +00008027 if (before == l1->lv_len)
8028 item = NULL;
8029 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008030 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008031 item = list_find(l1, before);
8032 if (item == NULL)
8033 {
8034 EMSGN(_(e_listidx), before);
8035 return;
8036 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008037 }
8038 }
8039 else
8040 item = NULL;
8041 list_extend(l1, l2, item);
8042
8043 ++l1->lv_refcount;
8044 copy_tv(&argvars[0], rettv);
8045 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008046 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008047 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8048 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008049 dict_T *d1, *d2;
8050 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008051 char_u *action;
8052 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008054 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008055
8056 d1 = argvars[0].vval.v_dict;
8057 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008058 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8059 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008060 {
8061 /* Check the third argument. */
8062 if (argvars[2].v_type != VAR_UNKNOWN)
8063 {
8064 static char *(av[]) = {"keep", "force", "error"};
8065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008066 action = get_tv_string_chk(&argvars[2]);
8067 if (action == NULL)
8068 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008069 for (i = 0; i < 3; ++i)
8070 if (STRCMP(action, av[i]) == 0)
8071 break;
8072 if (i == 3)
8073 {
8074 EMSGN(_(e_invarg2), action);
8075 return;
8076 }
8077 }
8078 else
8079 action = (char_u *)"force";
8080
8081 /* Go over all entries in the second dict and add them to the
8082 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008083 todo = d2->dv_hashtab.ht_used;
8084 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008085 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008086 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008087 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008088 --todo;
8089 di1 = dict_find(d1, hi2->hi_key, -1);
8090 if (di1 == NULL)
8091 {
8092 di1 = dictitem_copy(HI2DI(hi2));
8093 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8094 dictitem_free(di1);
8095 }
8096 else if (*action == 'e')
8097 {
8098 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8099 break;
8100 }
8101 else if (*action == 'f')
8102 {
8103 clear_tv(&di1->di_tv);
8104 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8105 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008106 }
8107 }
8108
8109 ++d1->dv_refcount;
8110 copy_tv(&argvars[0], rettv);
8111 }
8112 }
8113 else
8114 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008115}
8116
8117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 * "filereadable()" function
8119 */
8120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008121f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008122 typval_T *argvars;
8123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124{
8125 FILE *fd;
8126 char_u *p;
8127 int n;
8128
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008129 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8131 {
8132 n = TRUE;
8133 fclose(fd);
8134 }
8135 else
8136 n = FALSE;
8137
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008138 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139}
8140
8141/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008142 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 * rights to write into.
8144 */
8145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008146f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008147 typval_T *argvars;
8148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008150 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151}
8152
Bram Moolenaar33570922005-01-25 22:26:29 +00008153static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008154
8155 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008156findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008157 typval_T *argvars;
8158 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008159 int dir;
8160{
8161#ifdef FEAT_SEARCHPATH
8162 char_u *fname;
8163 char_u *fresult = NULL;
8164 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8165 char_u *p;
8166 char_u pathbuf[NUMBUFLEN];
8167 int count = 1;
8168 int first = TRUE;
8169
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008170 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008171
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008172 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008174 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8175 if (p == NULL)
8176 count = -1; /* error */
8177 else
8178 {
8179 if (*p != NUL)
8180 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008181
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008182 if (argvars[2].v_type != VAR_UNKNOWN)
8183 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8184 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008185 }
8186
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008187 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008189 do
8190 {
8191 vim_free(fresult);
8192 fresult = find_file_in_path_option(first ? fname : NULL,
8193 first ? (int)STRLEN(fname) : 0,
8194 0, first, path, dir, NULL);
8195 first = FALSE;
8196 } while (--count > 0 && fresult != NULL);
8197 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008198
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008199 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008200#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008201 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008202#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008203 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008204}
8205
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008206static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
8207static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00008208static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8209static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008210
8211/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008212 * Prepare v: variable "idx" to be used.
8213 * Save the current typeval in "save_tv".
8214 * When not used yet add the variable to the v: hashtable.
8215 */
8216 static void
8217prepare_vimvar(idx, save_tv)
8218 int idx;
8219 typval_T *save_tv;
8220{
8221 *save_tv = vimvars[idx].vv_tv;
8222 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8223 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
8224}
8225
8226/*
8227 * Restore v: variable "idx" to typeval "save_tv".
8228 * When no longer defined, remove the variable from the v: hashtable.
8229 */
8230 static void
8231restore_vimvar(idx, save_tv)
8232 int idx;
8233 typval_T *save_tv;
8234{
8235 hashitem_T *hi;
8236
8237 clear_tv(&vimvars[idx].vv_tv);
8238 vimvars[idx].vv_tv = *save_tv;
8239 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8240 {
8241 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
8242 if (HASHITEM_EMPTY(hi))
8243 EMSG2(_(e_intern2), "restore_vimvar()");
8244 else
8245 hash_remove(&vimvarht, hi);
8246 }
8247}
8248
8249/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008250 * Implementation of map() and filter().
8251 */
8252 static void
8253filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008254 typval_T *argvars;
8255 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008256 int map;
8257{
8258 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008259 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008260 listitem_T *li, *nli;
8261 list_T *l = NULL;
8262 dictitem_T *di;
8263 hashtab_T *ht;
8264 hashitem_T *hi;
8265 dict_T *d = NULL;
8266 typval_T save_val;
8267 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008268 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008269 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008270 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8271
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008272
8273 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008274 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008275 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008276 if ((l = argvars[0].vval.v_list) == NULL
8277 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008278 return;
8279 }
8280 else if (argvars[0].v_type == VAR_DICT)
8281 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008282 if ((d = argvars[0].vval.v_dict) == NULL
8283 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008284 return;
8285 }
8286 else
8287 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008288 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008289 return;
8290 }
8291
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008292 expr = get_tv_string_buf_chk(&argvars[1], buf);
8293 /* On type errors, the preceding call has already displayed an error
8294 * message. Avoid a misleading error message for an empty string that
8295 * was not passed as argument. */
8296 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008298 prepare_vimvar(VV_VAL, &save_val);
8299 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008301 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008303 prepare_vimvar(VV_KEY, &save_key);
8304 vimvars[VV_KEY].vv_type = VAR_STRING;
8305
8306 ht = &d->dv_hashtab;
8307 hash_lock(ht);
8308 todo = ht->ht_used;
8309 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008311 if (!HASHITEM_EMPTY(hi))
8312 {
8313 --todo;
8314 di = HI2DI(hi);
8315 if (tv_check_lock(di->di_tv.v_lock, msg))
8316 break;
8317 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8318 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8319 break;
8320 if (!map && rem)
8321 dictitem_remove(d, di);
8322 clear_tv(&vimvars[VV_KEY].vv_tv);
8323 }
8324 }
8325 hash_unlock(ht);
8326
8327 restore_vimvar(VV_KEY, &save_key);
8328 }
8329 else
8330 {
8331 for (li = l->lv_first; li != NULL; li = nli)
8332 {
8333 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008334 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008335 nli = li->li_next;
8336 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008337 break;
8338 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008339 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008340 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008341 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008342
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008343 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008344 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008345
8346 copy_tv(&argvars[0], rettv);
8347}
8348
8349 static int
8350filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008352 char_u *expr;
8353 int map;
8354 int *remp;
8355{
Bram Moolenaar33570922005-01-25 22:26:29 +00008356 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008357 char_u *s;
8358
Bram Moolenaar33570922005-01-25 22:26:29 +00008359 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008360 s = expr;
8361 if (eval1(&s, &rettv, TRUE) == FAIL)
8362 return FAIL;
8363 if (*s != NUL) /* check for trailing chars after expr */
8364 {
8365 EMSG2(_(e_invexpr2), s);
8366 return FAIL;
8367 }
8368 if (map)
8369 {
8370 /* map(): replace the list item value */
8371 clear_tv(tv);
8372 *tv = rettv;
8373 }
8374 else
8375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008376 int error = FALSE;
8377
Bram Moolenaare9a41262005-01-15 22:18:47 +00008378 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008379 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008380 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008381 /* On type error, nothing has been removed; return FAIL to stop the
8382 * loop. The error message was given by get_tv_number_chk(). */
8383 if (error)
8384 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008385 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008386 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008387 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008388}
8389
8390/*
8391 * "filter()" function
8392 */
8393 static void
8394f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008395 typval_T *argvars;
8396 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008397{
8398 filter_map(argvars, rettv, FALSE);
8399}
8400
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008401/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008402 * "finddir({fname}[, {path}[, {count}]])" function
8403 */
8404 static void
8405f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008406 typval_T *argvars;
8407 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008408{
8409 findfilendir(argvars, rettv, TRUE);
8410}
8411
8412/*
8413 * "findfile({fname}[, {path}[, {count}]])" function
8414 */
8415 static void
8416f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008417 typval_T *argvars;
8418 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008419{
8420 findfilendir(argvars, rettv, FALSE);
8421}
8422
8423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 * "fnamemodify({fname}, {mods})" function
8425 */
8426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008427f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008428 typval_T *argvars;
8429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430{
8431 char_u *fname;
8432 char_u *mods;
8433 int usedlen = 0;
8434 int len;
8435 char_u *fbuf = NULL;
8436 char_u buf[NUMBUFLEN];
8437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008438 fname = get_tv_string_chk(&argvars[0]);
8439 mods = get_tv_string_buf_chk(&argvars[1], buf);
8440 if (fname == NULL || mods == NULL)
8441 fname = NULL;
8442 else
8443 {
8444 len = (int)STRLEN(fname);
8445 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008448 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008450 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008452 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 vim_free(fbuf);
8454}
8455
Bram Moolenaar33570922005-01-25 22:26:29 +00008456static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457
8458/*
8459 * "foldclosed()" function
8460 */
8461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008462foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008463 typval_T *argvars;
8464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 int end;
8466{
8467#ifdef FEAT_FOLDING
8468 linenr_T lnum;
8469 linenr_T first, last;
8470
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008471 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8473 {
8474 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8475 {
8476 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008477 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008479 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 return;
8481 }
8482 }
8483#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008484 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485}
8486
8487/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008488 * "foldclosed()" function
8489 */
8490 static void
8491f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008492 typval_T *argvars;
8493 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008494{
8495 foldclosed_both(argvars, rettv, FALSE);
8496}
8497
8498/*
8499 * "foldclosedend()" function
8500 */
8501 static void
8502f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008503 typval_T *argvars;
8504 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008505{
8506 foldclosed_both(argvars, rettv, TRUE);
8507}
8508
8509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 * "foldlevel()" function
8511 */
8512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008513f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008514 typval_T *argvars;
8515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516{
8517#ifdef FEAT_FOLDING
8518 linenr_T lnum;
8519
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008520 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008522 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 else
8524#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008525 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526}
8527
8528/*
8529 * "foldtext()" function
8530 */
8531/*ARGSUSED*/
8532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008533f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008534 typval_T *argvars;
8535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536{
8537#ifdef FEAT_FOLDING
8538 linenr_T lnum;
8539 char_u *s;
8540 char_u *r;
8541 int len;
8542 char *txt;
8543#endif
8544
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008545 rettv->v_type = VAR_STRING;
8546 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008548 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8549 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8550 <= curbuf->b_ml.ml_line_count
8551 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 {
8553 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008554 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8555 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 {
8557 if (!linewhite(lnum))
8558 break;
8559 ++lnum;
8560 }
8561
8562 /* Find interesting text in this line. */
8563 s = skipwhite(ml_get(lnum));
8564 /* skip C comment-start */
8565 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008568 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008569 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008570 {
8571 s = skipwhite(ml_get(lnum + 1));
8572 if (*s == '*')
8573 s = skipwhite(s + 1);
8574 }
8575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 txt = _("+-%s%3ld lines: ");
8577 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008578 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 + 20 /* for %3ld */
8580 + STRLEN(s))); /* concatenated */
8581 if (r != NULL)
8582 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008583 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8584 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8585 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 len = (int)STRLEN(r);
8587 STRCAT(r, s);
8588 /* remove 'foldmarker' and 'commentstring' */
8589 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 }
8592 }
8593#endif
8594}
8595
8596/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008597 * "foldtextresult(lnum)" function
8598 */
8599/*ARGSUSED*/
8600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008601f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008602 typval_T *argvars;
8603 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008604{
8605#ifdef FEAT_FOLDING
8606 linenr_T lnum;
8607 char_u *text;
8608 char_u buf[51];
8609 foldinfo_T foldinfo;
8610 int fold_count;
8611#endif
8612
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008613 rettv->v_type = VAR_STRING;
8614 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008615#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008616 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008617 /* treat illegal types and illegal string values for {lnum} the same */
8618 if (lnum < 0)
8619 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008620 fold_count = foldedCount(curwin, lnum, &foldinfo);
8621 if (fold_count > 0)
8622 {
8623 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8624 &foldinfo, buf);
8625 if (text == buf)
8626 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008627 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008628 }
8629#endif
8630}
8631
8632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 * "foreground()" function
8634 */
8635/*ARGSUSED*/
8636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008637f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008638 typval_T *argvars;
8639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008641 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642#ifdef FEAT_GUI
8643 if (gui.in_use)
8644 gui_mch_set_foreground();
8645#else
8646# ifdef WIN32
8647 win32_set_foreground();
8648# endif
8649#endif
8650}
8651
8652/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008653 * "function()" function
8654 */
8655/*ARGSUSED*/
8656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008657f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 typval_T *argvars;
8659 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008660{
8661 char_u *s;
8662
Bram Moolenaara7043832005-01-21 11:56:39 +00008663 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008664 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008665 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008666 EMSG2(_(e_invarg2), s);
8667 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008668 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008669 else
8670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008671 rettv->vval.v_string = vim_strsave(s);
8672 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008673 }
8674}
8675
8676/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008677 * "get()" function
8678 */
8679 static void
8680f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008681 typval_T *argvars;
8682 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008683{
Bram Moolenaar33570922005-01-25 22:26:29 +00008684 listitem_T *li;
8685 list_T *l;
8686 dictitem_T *di;
8687 dict_T *d;
8688 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008689
Bram Moolenaare9a41262005-01-15 22:18:47 +00008690 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008691 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008692 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008694 int error = FALSE;
8695
8696 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
8697 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008698 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008699 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008700 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008701 else if (argvars[0].v_type == VAR_DICT)
8702 {
8703 if ((d = argvars[0].vval.v_dict) != NULL)
8704 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008705 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008706 if (di != NULL)
8707 tv = &di->di_tv;
8708 }
8709 }
8710 else
8711 EMSG2(_(e_listdictarg), "get()");
8712
8713 if (tv == NULL)
8714 {
8715 if (argvars[2].v_type == VAR_UNKNOWN)
8716 rettv->vval.v_number = 0;
8717 else
8718 copy_tv(&argvars[2], rettv);
8719 }
8720 else
8721 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008722}
8723
8724/*
8725 * "getbufvar()" function
8726 */
8727 static void
8728f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 typval_T *argvars;
8730 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008731{
8732 buf_T *buf;
8733 buf_T *save_curbuf;
8734 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008735 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008737 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
8738 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008739 ++emsg_off;
8740 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008741
8742 rettv->v_type = VAR_STRING;
8743 rettv->vval.v_string = NULL;
8744
8745 if (buf != NULL && varname != NULL)
8746 {
8747 if (*varname == '&') /* buffer-local-option */
8748 {
8749 /* set curbuf to be our buf, temporarily */
8750 save_curbuf = curbuf;
8751 curbuf = buf;
8752
8753 get_option_tv(&varname, rettv, TRUE);
8754
8755 /* restore previous notion of curbuf */
8756 curbuf = save_curbuf;
8757 }
8758 else
8759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008760 if (*varname == NUL)
8761 /* let getbufvar({nr}, "") return the "b:" dictionary. The
8762 * scope prefix before the NUL byte is required by
8763 * find_var_in_ht(). */
8764 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008765 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008766 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008767 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008768 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008769 }
8770 }
8771
8772 --emsg_off;
8773}
8774
8775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 * "getchar()" function
8777 */
8778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008779f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008780 typval_T *argvars;
8781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782{
8783 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008784 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785
8786 ++no_mapping;
8787 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008788 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 /* getchar(): blocking wait. */
8790 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008791 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 /* getchar(1): only check if char avail */
8793 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008794 else if (error || vpeekc() == NUL)
8795 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 n = 0;
8797 else
8798 /* getchar(0) and char avail: return char */
8799 n = safe_vgetc();
8800 --no_mapping;
8801 --allow_keys;
8802
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008803 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 if (IS_SPECIAL(n) || mod_mask != 0)
8805 {
8806 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
8807 int i = 0;
8808
8809 /* Turn a special key into three bytes, plus modifier. */
8810 if (mod_mask != 0)
8811 {
8812 temp[i++] = K_SPECIAL;
8813 temp[i++] = KS_MODIFIER;
8814 temp[i++] = mod_mask;
8815 }
8816 if (IS_SPECIAL(n))
8817 {
8818 temp[i++] = K_SPECIAL;
8819 temp[i++] = K_SECOND(n);
8820 temp[i++] = K_THIRD(n);
8821 }
8822#ifdef FEAT_MBYTE
8823 else if (has_mbyte)
8824 i += (*mb_char2bytes)(n, temp + i);
8825#endif
8826 else
8827 temp[i++] = n;
8828 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008829 rettv->v_type = VAR_STRING;
8830 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 }
8832}
8833
8834/*
8835 * "getcharmod()" function
8836 */
8837/*ARGSUSED*/
8838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008839f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008840 typval_T *argvars;
8841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008843 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844}
8845
8846/*
8847 * "getcmdline()" function
8848 */
8849/*ARGSUSED*/
8850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008851f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008852 typval_T *argvars;
8853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855 rettv->v_type = VAR_STRING;
8856 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857}
8858
8859/*
8860 * "getcmdpos()" function
8861 */
8862/*ARGSUSED*/
8863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008864f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008865 typval_T *argvars;
8866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008868 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869}
8870
8871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 * "getcwd()" function
8873 */
8874/*ARGSUSED*/
8875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008876f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008877 typval_T *argvars;
8878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879{
8880 char_u cwd[MAXPATHL];
8881
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008882 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008884 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 else
8886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008887 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008889 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890#endif
8891 }
8892}
8893
8894/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008895 * "getfontname()" function
8896 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008897/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008900 typval_T *argvars;
8901 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008902{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008903 rettv->v_type = VAR_STRING;
8904 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008905#ifdef FEAT_GUI
8906 if (gui.in_use)
8907 {
8908 GuiFont font;
8909 char_u *name = NULL;
8910
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008911 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008912 {
8913 /* Get the "Normal" font. Either the name saved by
8914 * hl_set_font_name() or from the font ID. */
8915 font = gui.norm_font;
8916 name = hl_get_font_name();
8917 }
8918 else
8919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008920 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008921 if (STRCMP(name, "*") == 0) /* don't use font dialog */
8922 return;
8923 font = gui_mch_get_font(name, FALSE);
8924 if (font == NOFONT)
8925 return; /* Invalid font name, return empty string. */
8926 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008927 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008928 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008929 gui_mch_free_font(font);
8930 }
8931#endif
8932}
8933
8934/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008935 * "getfperm({fname})" function
8936 */
8937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008938f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008939 typval_T *argvars;
8940 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008941{
8942 char_u *fname;
8943 struct stat st;
8944 char_u *perm = NULL;
8945 char_u flags[] = "rwx";
8946 int i;
8947
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008949
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008950 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008951 if (mch_stat((char *)fname, &st) >= 0)
8952 {
8953 perm = vim_strsave((char_u *)"---------");
8954 if (perm != NULL)
8955 {
8956 for (i = 0; i < 9; i++)
8957 {
8958 if (st.st_mode & (1 << (8 - i)))
8959 perm[i] = flags[i % 3];
8960 }
8961 }
8962 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008963 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008964}
8965
8966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 * "getfsize({fname})" function
8968 */
8969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008971 typval_T *argvars;
8972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973{
8974 char_u *fname;
8975 struct stat st;
8976
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980
8981 if (mch_stat((char *)fname, &st) >= 0)
8982 {
8983 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008984 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 }
8988 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990}
8991
8992/*
8993 * "getftime({fname})" function
8994 */
8995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008997 typval_T *argvars;
8998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999{
9000 char_u *fname;
9001 struct stat st;
9002
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009003 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004
9005 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009008 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009}
9010
9011/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009012 * "getftype({fname})" function
9013 */
9014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009015f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009016 typval_T *argvars;
9017 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009018{
9019 char_u *fname;
9020 struct stat st;
9021 char_u *type = NULL;
9022 char *t;
9023
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009024 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009025
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009027 if (mch_lstat((char *)fname, &st) >= 0)
9028 {
9029#ifdef S_ISREG
9030 if (S_ISREG(st.st_mode))
9031 t = "file";
9032 else if (S_ISDIR(st.st_mode))
9033 t = "dir";
9034# ifdef S_ISLNK
9035 else if (S_ISLNK(st.st_mode))
9036 t = "link";
9037# endif
9038# ifdef S_ISBLK
9039 else if (S_ISBLK(st.st_mode))
9040 t = "bdev";
9041# endif
9042# ifdef S_ISCHR
9043 else if (S_ISCHR(st.st_mode))
9044 t = "cdev";
9045# endif
9046# ifdef S_ISFIFO
9047 else if (S_ISFIFO(st.st_mode))
9048 t = "fifo";
9049# endif
9050# ifdef S_ISSOCK
9051 else if (S_ISSOCK(st.st_mode))
9052 t = "fifo";
9053# endif
9054 else
9055 t = "other";
9056#else
9057# ifdef S_IFMT
9058 switch (st.st_mode & S_IFMT)
9059 {
9060 case S_IFREG: t = "file"; break;
9061 case S_IFDIR: t = "dir"; break;
9062# ifdef S_IFLNK
9063 case S_IFLNK: t = "link"; break;
9064# endif
9065# ifdef S_IFBLK
9066 case S_IFBLK: t = "bdev"; break;
9067# endif
9068# ifdef S_IFCHR
9069 case S_IFCHR: t = "cdev"; break;
9070# endif
9071# ifdef S_IFIFO
9072 case S_IFIFO: t = "fifo"; break;
9073# endif
9074# ifdef S_IFSOCK
9075 case S_IFSOCK: t = "socket"; break;
9076# endif
9077 default: t = "other";
9078 }
9079# else
9080 if (mch_isdir(fname))
9081 t = "dir";
9082 else
9083 t = "file";
9084# endif
9085#endif
9086 type = vim_strsave((char_u *)t);
9087 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009089}
9090
9091/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009092 * "getline(lnum)" function
9093 */
9094 static void
9095f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009096 typval_T *argvars;
9097 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009098{
9099 linenr_T lnum;
9100 linenr_T end;
9101 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00009102 list_T *l;
9103 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009104
9105 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009106 if (lnum < 0)
9107 rettv->vval.v_number = 0; /* failure; error message already given */
9108 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009109 {
9110 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9111 p = ml_get(lnum);
9112 else
9113 p = (char_u *)"";
9114
9115 rettv->v_type = VAR_STRING;
9116 rettv->vval.v_string = vim_strsave(p);
9117 }
9118 else
9119 {
9120 end = get_tv_lnum(&argvars[1]);
9121 if (end < lnum)
9122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009123 if (end >= 0) /* else: error message already given */
9124 EMSG(_(e_invrange));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009125 rettv->vval.v_number = 0;
9126 }
9127 else
9128 {
9129 l = list_alloc();
9130 if (l != NULL)
9131 {
9132 if (lnum < 1)
9133 lnum = 1;
9134 if (end > curbuf->b_ml.ml_line_count)
9135 end = curbuf->b_ml.ml_line_count;
9136 while (lnum <= end)
9137 {
9138 li = listitem_alloc();
9139 if (li == NULL)
9140 break;
9141 list_append(l, li);
9142 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009143 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009144 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
9145 }
9146 rettv->vval.v_list = l;
9147 rettv->v_type = VAR_LIST;
9148 ++l->lv_refcount;
9149 }
9150 }
9151 }
9152}
9153
9154/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009155 * "getqflist()" function
9156 */
9157/*ARGSUSED*/
9158 static void
9159f_getqflist(argvars, rettv)
9160 typval_T *argvars;
9161 typval_T *rettv;
9162{
9163#ifdef FEAT_QUICKFIX
9164 list_T *l;
9165#endif
9166
9167 rettv->vval.v_number = FALSE;
9168#ifdef FEAT_QUICKFIX
9169 l = list_alloc();
9170 if (l != NULL)
9171 {
9172 if (get_errorlist(l) != FAIL)
9173 {
9174 rettv->vval.v_list = l;
9175 rettv->v_type = VAR_LIST;
9176 ++l->lv_refcount;
9177 }
9178 else
9179 list_free(l);
9180 }
9181#endif
9182}
9183
9184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 * "getreg()" function
9186 */
9187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009189 typval_T *argvars;
9190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191{
9192 char_u *strregname;
9193 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009194 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009195 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009197 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009199 strregname = get_tv_string_chk(&argvars[0]);
9200 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009201 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009202 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009205 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 regname = (strregname == NULL ? '"' : *strregname);
9207 if (regname == 0)
9208 regname = '"';
9209
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009210 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009211 rettv->vval.v_string = error ? NULL :
9212 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213}
9214
9215/*
9216 * "getregtype()" function
9217 */
9218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009220 typval_T *argvars;
9221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222{
9223 char_u *strregname;
9224 int regname;
9225 char_u buf[NUMBUFLEN + 2];
9226 long reglen = 0;
9227
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009228 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009229 {
9230 strregname = get_tv_string_chk(&argvars[0]);
9231 if (strregname == NULL) /* type error; errmsg already given */
9232 {
9233 rettv->v_type = VAR_STRING;
9234 rettv->vval.v_string = NULL;
9235 return;
9236 }
9237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 else
9239 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009240 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241
9242 regname = (strregname == NULL ? '"' : *strregname);
9243 if (regname == 0)
9244 regname = '"';
9245
9246 buf[0] = NUL;
9247 buf[1] = NUL;
9248 switch (get_reg_type(regname, &reglen))
9249 {
9250 case MLINE: buf[0] = 'V'; break;
9251 case MCHAR: buf[0] = 'v'; break;
9252#ifdef FEAT_VISUAL
9253 case MBLOCK:
9254 buf[0] = Ctrl_V;
9255 sprintf((char *)buf + 1, "%ld", reglen + 1);
9256 break;
9257#endif
9258 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009259 rettv->v_type = VAR_STRING;
9260 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261}
9262
9263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 * "getwinposx()" function
9265 */
9266/*ARGSUSED*/
9267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009268f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009269 typval_T *argvars;
9270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009272 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273#ifdef FEAT_GUI
9274 if (gui.in_use)
9275 {
9276 int x, y;
9277
9278 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009279 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 }
9281#endif
9282}
9283
9284/*
9285 * "getwinposy()" function
9286 */
9287/*ARGSUSED*/
9288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009289f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009290 typval_T *argvars;
9291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009293 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294#ifdef FEAT_GUI
9295 if (gui.in_use)
9296 {
9297 int x, y;
9298
9299 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009300 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 }
9302#endif
9303}
9304
9305/*
9306 * "getwinvar()" function
9307 */
9308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009309f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009310 typval_T *argvars;
9311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312{
9313 win_T *win, *oldcurwin;
9314 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009315 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009318 varname = get_tv_string_chk(&argvars[1]);
9319 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009321 rettv->v_type = VAR_STRING;
9322 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323
9324 if (win != NULL && varname != NULL)
9325 {
9326 if (*varname == '&') /* window-local-option */
9327 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009328 /* Set curwin to be our win, temporarily. Also set curbuf, so
9329 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 oldcurwin = curwin;
9331 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009332 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009334 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335
9336 /* restore previous notion of curwin */
9337 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009338 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 }
9340 else
9341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009342 if (*varname == NUL)
9343 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9344 * scope prefix before the NUL byte is required by
9345 * find_var_in_ht(). */
9346 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009348 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009350 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 }
9352 }
9353
9354 --emsg_off;
9355}
9356
9357/*
9358 * "glob()" function
9359 */
9360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009361f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009362 typval_T *argvars;
9363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364{
9365 expand_T xpc;
9366
9367 ExpandInit(&xpc);
9368 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009369 rettv->v_type = VAR_STRING;
9370 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9372 ExpandCleanup(&xpc);
9373}
9374
9375/*
9376 * "globpath()" function
9377 */
9378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009379f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009380 typval_T *argvars;
9381 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382{
9383 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009384 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009386 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009387 if (file == NULL)
9388 rettv->vval.v_string = NULL;
9389 else
9390 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391}
9392
9393/*
9394 * "has()" function
9395 */
9396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009398 typval_T *argvars;
9399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400{
9401 int i;
9402 char_u *name;
9403 int n = FALSE;
9404 static char *(has_list[]) =
9405 {
9406#ifdef AMIGA
9407 "amiga",
9408# ifdef FEAT_ARP
9409 "arp",
9410# endif
9411#endif
9412#ifdef __BEOS__
9413 "beos",
9414#endif
9415#ifdef MSDOS
9416# ifdef DJGPP
9417 "dos32",
9418# else
9419 "dos16",
9420# endif
9421#endif
9422#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9423 "mac",
9424#endif
9425#if defined(MACOS_X_UNIX)
9426 "macunix",
9427#endif
9428#ifdef OS2
9429 "os2",
9430#endif
9431#ifdef __QNX__
9432 "qnx",
9433#endif
9434#ifdef RISCOS
9435 "riscos",
9436#endif
9437#ifdef UNIX
9438 "unix",
9439#endif
9440#ifdef VMS
9441 "vms",
9442#endif
9443#ifdef WIN16
9444 "win16",
9445#endif
9446#ifdef WIN32
9447 "win32",
9448#endif
9449#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9450 "win32unix",
9451#endif
9452#ifdef WIN64
9453 "win64",
9454#endif
9455#ifdef EBCDIC
9456 "ebcdic",
9457#endif
9458#ifndef CASE_INSENSITIVE_FILENAME
9459 "fname_case",
9460#endif
9461#ifdef FEAT_ARABIC
9462 "arabic",
9463#endif
9464#ifdef FEAT_AUTOCMD
9465 "autocmd",
9466#endif
9467#ifdef FEAT_BEVAL
9468 "balloon_eval",
9469#endif
9470#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9471 "builtin_terms",
9472# ifdef ALL_BUILTIN_TCAPS
9473 "all_builtin_terms",
9474# endif
9475#endif
9476#ifdef FEAT_BYTEOFF
9477 "byte_offset",
9478#endif
9479#ifdef FEAT_CINDENT
9480 "cindent",
9481#endif
9482#ifdef FEAT_CLIENTSERVER
9483 "clientserver",
9484#endif
9485#ifdef FEAT_CLIPBOARD
9486 "clipboard",
9487#endif
9488#ifdef FEAT_CMDL_COMPL
9489 "cmdline_compl",
9490#endif
9491#ifdef FEAT_CMDHIST
9492 "cmdline_hist",
9493#endif
9494#ifdef FEAT_COMMENTS
9495 "comments",
9496#endif
9497#ifdef FEAT_CRYPT
9498 "cryptv",
9499#endif
9500#ifdef FEAT_CSCOPE
9501 "cscope",
9502#endif
9503#ifdef DEBUG
9504 "debug",
9505#endif
9506#ifdef FEAT_CON_DIALOG
9507 "dialog_con",
9508#endif
9509#ifdef FEAT_GUI_DIALOG
9510 "dialog_gui",
9511#endif
9512#ifdef FEAT_DIFF
9513 "diff",
9514#endif
9515#ifdef FEAT_DIGRAPHS
9516 "digraphs",
9517#endif
9518#ifdef FEAT_DND
9519 "dnd",
9520#endif
9521#ifdef FEAT_EMACS_TAGS
9522 "emacs_tags",
9523#endif
9524 "eval", /* always present, of course! */
9525#ifdef FEAT_EX_EXTRA
9526 "ex_extra",
9527#endif
9528#ifdef FEAT_SEARCH_EXTRA
9529 "extra_search",
9530#endif
9531#ifdef FEAT_FKMAP
9532 "farsi",
9533#endif
9534#ifdef FEAT_SEARCHPATH
9535 "file_in_path",
9536#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009537#if defined(UNIX) && !defined(USE_SYSTEM)
9538 "filterpipe",
9539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540#ifdef FEAT_FIND_ID
9541 "find_in_path",
9542#endif
9543#ifdef FEAT_FOLDING
9544 "folding",
9545#endif
9546#ifdef FEAT_FOOTER
9547 "footer",
9548#endif
9549#if !defined(USE_SYSTEM) && defined(UNIX)
9550 "fork",
9551#endif
9552#ifdef FEAT_GETTEXT
9553 "gettext",
9554#endif
9555#ifdef FEAT_GUI
9556 "gui",
9557#endif
9558#ifdef FEAT_GUI_ATHENA
9559# ifdef FEAT_GUI_NEXTAW
9560 "gui_neXtaw",
9561# else
9562 "gui_athena",
9563# endif
9564#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009565#ifdef FEAT_GUI_KDE
9566 "gui_kde",
9567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568#ifdef FEAT_GUI_GTK
9569 "gui_gtk",
9570# ifdef HAVE_GTK2
9571 "gui_gtk2",
9572# endif
9573#endif
9574#ifdef FEAT_GUI_MAC
9575 "gui_mac",
9576#endif
9577#ifdef FEAT_GUI_MOTIF
9578 "gui_motif",
9579#endif
9580#ifdef FEAT_GUI_PHOTON
9581 "gui_photon",
9582#endif
9583#ifdef FEAT_GUI_W16
9584 "gui_win16",
9585#endif
9586#ifdef FEAT_GUI_W32
9587 "gui_win32",
9588#endif
9589#ifdef FEAT_HANGULIN
9590 "hangul_input",
9591#endif
9592#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
9593 "iconv",
9594#endif
9595#ifdef FEAT_INS_EXPAND
9596 "insert_expand",
9597#endif
9598#ifdef FEAT_JUMPLIST
9599 "jumplist",
9600#endif
9601#ifdef FEAT_KEYMAP
9602 "keymap",
9603#endif
9604#ifdef FEAT_LANGMAP
9605 "langmap",
9606#endif
9607#ifdef FEAT_LIBCALL
9608 "libcall",
9609#endif
9610#ifdef FEAT_LINEBREAK
9611 "linebreak",
9612#endif
9613#ifdef FEAT_LISP
9614 "lispindent",
9615#endif
9616#ifdef FEAT_LISTCMDS
9617 "listcmds",
9618#endif
9619#ifdef FEAT_LOCALMAP
9620 "localmap",
9621#endif
9622#ifdef FEAT_MENU
9623 "menu",
9624#endif
9625#ifdef FEAT_SESSION
9626 "mksession",
9627#endif
9628#ifdef FEAT_MODIFY_FNAME
9629 "modify_fname",
9630#endif
9631#ifdef FEAT_MOUSE
9632 "mouse",
9633#endif
9634#ifdef FEAT_MOUSESHAPE
9635 "mouseshape",
9636#endif
9637#if defined(UNIX) || defined(VMS)
9638# ifdef FEAT_MOUSE_DEC
9639 "mouse_dec",
9640# endif
9641# ifdef FEAT_MOUSE_GPM
9642 "mouse_gpm",
9643# endif
9644# ifdef FEAT_MOUSE_JSB
9645 "mouse_jsbterm",
9646# endif
9647# ifdef FEAT_MOUSE_NET
9648 "mouse_netterm",
9649# endif
9650# ifdef FEAT_MOUSE_PTERM
9651 "mouse_pterm",
9652# endif
9653# ifdef FEAT_MOUSE_XTERM
9654 "mouse_xterm",
9655# endif
9656#endif
9657#ifdef FEAT_MBYTE
9658 "multi_byte",
9659#endif
9660#ifdef FEAT_MBYTE_IME
9661 "multi_byte_ime",
9662#endif
9663#ifdef FEAT_MULTI_LANG
9664 "multi_lang",
9665#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009666#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00009667#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009668 "mzscheme",
9669#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671#ifdef FEAT_OLE
9672 "ole",
9673#endif
9674#ifdef FEAT_OSFILETYPE
9675 "osfiletype",
9676#endif
9677#ifdef FEAT_PATH_EXTRA
9678 "path_extra",
9679#endif
9680#ifdef FEAT_PERL
9681#ifndef DYNAMIC_PERL
9682 "perl",
9683#endif
9684#endif
9685#ifdef FEAT_PYTHON
9686#ifndef DYNAMIC_PYTHON
9687 "python",
9688#endif
9689#endif
9690#ifdef FEAT_POSTSCRIPT
9691 "postscript",
9692#endif
9693#ifdef FEAT_PRINTER
9694 "printer",
9695#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00009696#ifdef FEAT_PROFILE
9697 "profile",
9698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699#ifdef FEAT_QUICKFIX
9700 "quickfix",
9701#endif
9702#ifdef FEAT_RIGHTLEFT
9703 "rightleft",
9704#endif
9705#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
9706 "ruby",
9707#endif
9708#ifdef FEAT_SCROLLBIND
9709 "scrollbind",
9710#endif
9711#ifdef FEAT_CMDL_INFO
9712 "showcmd",
9713 "cmdline_info",
9714#endif
9715#ifdef FEAT_SIGNS
9716 "signs",
9717#endif
9718#ifdef FEAT_SMARTINDENT
9719 "smartindent",
9720#endif
9721#ifdef FEAT_SNIFF
9722 "sniff",
9723#endif
9724#ifdef FEAT_STL_OPT
9725 "statusline",
9726#endif
9727#ifdef FEAT_SUN_WORKSHOP
9728 "sun_workshop",
9729#endif
9730#ifdef FEAT_NETBEANS_INTG
9731 "netbeans_intg",
9732#endif
9733#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009734 "spell",
9735#endif
9736#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 "syntax",
9738#endif
9739#if defined(USE_SYSTEM) || !defined(UNIX)
9740 "system",
9741#endif
9742#ifdef FEAT_TAG_BINS
9743 "tag_binary",
9744#endif
9745#ifdef FEAT_TAG_OLDSTATIC
9746 "tag_old_static",
9747#endif
9748#ifdef FEAT_TAG_ANYWHITE
9749 "tag_any_white",
9750#endif
9751#ifdef FEAT_TCL
9752# ifndef DYNAMIC_TCL
9753 "tcl",
9754# endif
9755#endif
9756#ifdef TERMINFO
9757 "terminfo",
9758#endif
9759#ifdef FEAT_TERMRESPONSE
9760 "termresponse",
9761#endif
9762#ifdef FEAT_TEXTOBJ
9763 "textobjects",
9764#endif
9765#ifdef HAVE_TGETENT
9766 "tgetent",
9767#endif
9768#ifdef FEAT_TITLE
9769 "title",
9770#endif
9771#ifdef FEAT_TOOLBAR
9772 "toolbar",
9773#endif
9774#ifdef FEAT_USR_CMDS
9775 "user-commands", /* was accidentally included in 5.4 */
9776 "user_commands",
9777#endif
9778#ifdef FEAT_VIMINFO
9779 "viminfo",
9780#endif
9781#ifdef FEAT_VERTSPLIT
9782 "vertsplit",
9783#endif
9784#ifdef FEAT_VIRTUALEDIT
9785 "virtualedit",
9786#endif
9787#ifdef FEAT_VISUAL
9788 "visual",
9789#endif
9790#ifdef FEAT_VISUALEXTRA
9791 "visualextra",
9792#endif
9793#ifdef FEAT_VREPLACE
9794 "vreplace",
9795#endif
9796#ifdef FEAT_WILDIGN
9797 "wildignore",
9798#endif
9799#ifdef FEAT_WILDMENU
9800 "wildmenu",
9801#endif
9802#ifdef FEAT_WINDOWS
9803 "windows",
9804#endif
9805#ifdef FEAT_WAK
9806 "winaltkeys",
9807#endif
9808#ifdef FEAT_WRITEBACKUP
9809 "writebackup",
9810#endif
9811#ifdef FEAT_XIM
9812 "xim",
9813#endif
9814#ifdef FEAT_XFONTSET
9815 "xfontset",
9816#endif
9817#ifdef USE_XSMP
9818 "xsmp",
9819#endif
9820#ifdef USE_XSMP_INTERACT
9821 "xsmp_interact",
9822#endif
9823#ifdef FEAT_XCLIPBOARD
9824 "xterm_clipboard",
9825#endif
9826#ifdef FEAT_XTERM_SAVE
9827 "xterm_save",
9828#endif
9829#if defined(UNIX) && defined(FEAT_X11)
9830 "X11",
9831#endif
9832 NULL
9833 };
9834
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 for (i = 0; has_list[i] != NULL; ++i)
9837 if (STRICMP(name, has_list[i]) == 0)
9838 {
9839 n = TRUE;
9840 break;
9841 }
9842
9843 if (n == FALSE)
9844 {
9845 if (STRNICMP(name, "patch", 5) == 0)
9846 n = has_patch(atoi((char *)name + 5));
9847 else if (STRICMP(name, "vim_starting") == 0)
9848 n = (starting != 0);
9849#ifdef DYNAMIC_TCL
9850 else if (STRICMP(name, "tcl") == 0)
9851 n = tcl_enabled(FALSE);
9852#endif
9853#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
9854 else if (STRICMP(name, "iconv") == 0)
9855 n = iconv_enabled(FALSE);
9856#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009857#ifdef DYNAMIC_MZSCHEME
9858 else if (STRICMP(name, "mzscheme") == 0)
9859 n = mzscheme_enabled(FALSE);
9860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861#ifdef DYNAMIC_RUBY
9862 else if (STRICMP(name, "ruby") == 0)
9863 n = ruby_enabled(FALSE);
9864#endif
9865#ifdef DYNAMIC_PYTHON
9866 else if (STRICMP(name, "python") == 0)
9867 n = python_enabled(FALSE);
9868#endif
9869#ifdef DYNAMIC_PERL
9870 else if (STRICMP(name, "perl") == 0)
9871 n = perl_enabled(FALSE);
9872#endif
9873#ifdef FEAT_GUI
9874 else if (STRICMP(name, "gui_running") == 0)
9875 n = (gui.in_use || gui.starting);
9876# ifdef FEAT_GUI_W32
9877 else if (STRICMP(name, "gui_win32s") == 0)
9878 n = gui_is_win32s();
9879# endif
9880# ifdef FEAT_BROWSE
9881 else if (STRICMP(name, "browse") == 0)
9882 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
9883# endif
9884#endif
9885#ifdef FEAT_SYN_HL
9886 else if (STRICMP(name, "syntax_items") == 0)
9887 n = syntax_present(curbuf);
9888#endif
9889#if defined(WIN3264)
9890 else if (STRICMP(name, "win95") == 0)
9891 n = mch_windows95();
9892#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00009893#ifdef FEAT_NETBEANS_INTG
9894 else if (STRICMP(name, "netbeans_enabled") == 0)
9895 n = usingNetbeans;
9896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 }
9898
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009899 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900}
9901
9902/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00009903 * "has_key()" function
9904 */
9905 static void
9906f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009907 typval_T *argvars;
9908 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009909{
9910 rettv->vval.v_number = 0;
9911 if (argvars[0].v_type != VAR_DICT)
9912 {
9913 EMSG(_(e_dictreq));
9914 return;
9915 }
9916 if (argvars[0].vval.v_dict == NULL)
9917 return;
9918
9919 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009920 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009921}
9922
9923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 * "hasmapto()" function
9925 */
9926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009927f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009928 typval_T *argvars;
9929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930{
9931 char_u *name;
9932 char_u *mode;
9933 char_u buf[NUMBUFLEN];
9934
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009935 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009936 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 mode = (char_u *)"nvo";
9938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009939 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940
9941 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009942 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945}
9946
9947/*
9948 * "histadd()" function
9949 */
9950/*ARGSUSED*/
9951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009952f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009953 typval_T *argvars;
9954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955{
9956#ifdef FEAT_CMDHIST
9957 int histype;
9958 char_u *str;
9959 char_u buf[NUMBUFLEN];
9960#endif
9961
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009962 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 if (check_restricted() || check_secure())
9964 return;
9965#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009966 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
9967 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 if (histype >= 0)
9969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009970 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 if (*str != NUL)
9972 {
9973 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 return;
9976 }
9977 }
9978#endif
9979}
9980
9981/*
9982 * "histdel()" function
9983 */
9984/*ARGSUSED*/
9985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009986f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009987 typval_T *argvars;
9988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989{
9990#ifdef FEAT_CMDHIST
9991 int n;
9992 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009993 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009995 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
9996 if (str == NULL)
9997 n = 0;
9998 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010000 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010001 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010003 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010004 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 else
10006 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010007 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010008 get_tv_string_buf(&argvars[1], buf));
10009 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010011 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012#endif
10013}
10014
10015/*
10016 * "histget()" function
10017 */
10018/*ARGSUSED*/
10019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010020f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010021 typval_T *argvars;
10022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023{
10024#ifdef FEAT_CMDHIST
10025 int type;
10026 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010027 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010029 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10030 if (str == NULL)
10031 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010033 {
10034 type = get_histtype(str);
10035 if (argvars[1].v_type == VAR_UNKNOWN)
10036 idx = get_history_idx(type);
10037 else
10038 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10039 /* -1 on type error */
10040 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046}
10047
10048/*
10049 * "histnr()" function
10050 */
10051/*ARGSUSED*/
10052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010053f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010054 typval_T *argvars;
10055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056{
10057 int i;
10058
10059#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010060 char_u *history = get_tv_string_chk(&argvars[0]);
10061
10062 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 if (i >= HIST_CMD && i < HIST_COUNT)
10064 i = get_history_idx(i);
10065 else
10066#endif
10067 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010068 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069}
10070
10071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 * "highlightID(name)" function
10073 */
10074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010075f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010076 typval_T *argvars;
10077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080}
10081
10082/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010083 * "highlight_exists()" function
10084 */
10085 static void
10086f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010087 typval_T *argvars;
10088 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010089{
10090 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10091}
10092
10093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 * "hostname()" function
10095 */
10096/*ARGSUSED*/
10097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010098f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010099 typval_T *argvars;
10100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101{
10102 char_u hostname[256];
10103
10104 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010105 rettv->v_type = VAR_STRING;
10106 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107}
10108
10109/*
10110 * iconv() function
10111 */
10112/*ARGSUSED*/
10113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010114f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010115 typval_T *argvars;
10116 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117{
10118#ifdef FEAT_MBYTE
10119 char_u buf1[NUMBUFLEN];
10120 char_u buf2[NUMBUFLEN];
10121 char_u *from, *to, *str;
10122 vimconv_T vimconv;
10123#endif
10124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010125 rettv->v_type = VAR_STRING;
10126 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127
10128#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010129 str = get_tv_string(&argvars[0]);
10130 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10131 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 vimconv.vc_type = CONV_NONE;
10133 convert_setup(&vimconv, from, to);
10134
10135 /* If the encodings are equal, no conversion needed. */
10136 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140
10141 convert_setup(&vimconv, NULL, NULL);
10142 vim_free(from);
10143 vim_free(to);
10144#endif
10145}
10146
10147/*
10148 * "indent()" function
10149 */
10150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010151f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010152 typval_T *argvars;
10153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154{
10155 linenr_T lnum;
10156
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010157 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010159 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162}
10163
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010164/*
10165 * "index()" function
10166 */
10167 static void
10168f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010169 typval_T *argvars;
10170 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010171{
Bram Moolenaar33570922005-01-25 22:26:29 +000010172 list_T *l;
10173 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010174 long idx = 0;
10175 int ic = FALSE;
10176
10177 rettv->vval.v_number = -1;
10178 if (argvars[0].v_type != VAR_LIST)
10179 {
10180 EMSG(_(e_listreq));
10181 return;
10182 }
10183 l = argvars[0].vval.v_list;
10184 if (l != NULL)
10185 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010186 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010187 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010189 int error = FALSE;
10190
Bram Moolenaar758711c2005-02-02 23:11:38 +000010191 /* Start at specified item. Use the cached index that list_find()
10192 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010193 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010194 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010195 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010196 ic = get_tv_number_chk(&argvars[3], &error);
10197 if (error)
10198 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010199 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010200
Bram Moolenaar758711c2005-02-02 23:11:38 +000010201 for ( ; item != NULL; item = item->li_next, ++idx)
10202 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010203 {
10204 rettv->vval.v_number = idx;
10205 break;
10206 }
10207 }
10208}
10209
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210static int inputsecret_flag = 0;
10211
10212/*
10213 * "input()" function
10214 * Also handles inputsecret() when inputsecret is set.
10215 */
10216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010217f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010218 typval_T *argvars;
10219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010221 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 char_u *p = NULL;
10223 int c;
10224 char_u buf[NUMBUFLEN];
10225 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010226 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010228 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229
10230#ifdef NO_CONSOLE_INPUT
10231 /* While starting up, there is no place to enter text. */
10232 if (no_console_input())
10233 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010234 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 return;
10236 }
10237#endif
10238
10239 cmd_silent = FALSE; /* Want to see the prompt. */
10240 if (prompt != NULL)
10241 {
10242 /* Only the part of the message after the last NL is considered as
10243 * prompt for the command line */
10244 p = vim_strrchr(prompt, '\n');
10245 if (p == NULL)
10246 p = prompt;
10247 else
10248 {
10249 ++p;
10250 c = *p;
10251 *p = NUL;
10252 msg_start();
10253 msg_clr_eos();
10254 msg_puts_attr(prompt, echo_attr);
10255 msg_didout = FALSE;
10256 msg_starthere();
10257 *p = c;
10258 }
10259 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010261 if (argvars[1].v_type != VAR_UNKNOWN)
10262 {
10263 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10264 if (defstr != NULL)
10265 stuffReadbuffSpec(defstr);
10266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010268 if (defstr != NULL)
10269 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10271
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010272 /* since the user typed this, no need to wait for return */
10273 need_wait_return = FALSE;
10274 msg_didout = FALSE;
10275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 cmd_silent = cmd_silent_save;
10277}
10278
10279/*
10280 * "inputdialog()" function
10281 */
10282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010283f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010284 typval_T *argvars;
10285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286{
10287#if defined(FEAT_GUI_TEXTDIALOG)
10288 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10289 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10290 {
10291 char_u *message;
10292 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010293 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010295 message = get_tv_string_chk(&argvars[0]);
10296 if (argvars[1].v_type != VAR_UNKNOWN
10297 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010299 STRNCPY(IObuff, defstr, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 IObuff[IOSIZE - 1] = NUL;
10301 }
10302 else
10303 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010304 if (message != NULL && defstr != NULL
10305 && do_dialog(VIM_QUESTION, NULL, message,
10306 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010307 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 else
10309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010310 if (message != NULL && defstr != NULL
10311 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010312 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010313 rettv->vval.v_string = vim_strsave(
10314 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010316 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010318 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 }
10320 else
10321#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010322 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323}
10324
10325static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10326
10327/*
10328 * "inputrestore()" function
10329 */
10330/*ARGSUSED*/
10331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010332f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010333 typval_T *argvars;
10334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335{
10336 if (ga_userinput.ga_len > 0)
10337 {
10338 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10340 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010341 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 }
10343 else if (p_verbose > 1)
10344 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010345 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010346 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 }
10348}
10349
10350/*
10351 * "inputsave()" function
10352 */
10353/*ARGSUSED*/
10354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010355f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010356 typval_T *argvars;
10357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358{
10359 /* Add an entry to the stack of typehead storage. */
10360 if (ga_grow(&ga_userinput, 1) == OK)
10361 {
10362 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10363 + ga_userinput.ga_len);
10364 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010365 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 }
10367 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010368 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369}
10370
10371/*
10372 * "inputsecret()" function
10373 */
10374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010375f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010376 typval_T *argvars;
10377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378{
10379 ++cmdline_star;
10380 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010381 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 --cmdline_star;
10383 --inputsecret_flag;
10384}
10385
10386/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010387 * "insert()" function
10388 */
10389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010390f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010391 typval_T *argvars;
10392 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010393{
10394 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010395 listitem_T *item;
10396 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010397 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010398
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010399 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010400 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010401 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010402 else if ((l = argvars[0].vval.v_list) != NULL
10403 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010404 {
10405 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010406 before = get_tv_number_chk(&argvars[2], &error);
10407 if (error)
10408 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010409
Bram Moolenaar758711c2005-02-02 23:11:38 +000010410 if (before == l->lv_len)
10411 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010412 else
10413 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010414 item = list_find(l, before);
10415 if (item == NULL)
10416 {
10417 EMSGN(_(e_listidx), before);
10418 l = NULL;
10419 }
10420 }
10421 if (l != NULL)
10422 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010423 list_insert_tv(l, &argvars[1], item);
10424 ++l->lv_refcount;
10425 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010426 }
10427 }
10428}
10429
10430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 * "isdirectory()" function
10432 */
10433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010434f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010435 typval_T *argvars;
10436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010438 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439}
10440
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010441/*
10442 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
10443 * it refers to a List or Dictionary that is locked.
10444 */
10445 static int
10446tv_islocked(tv)
10447 typval_T *tv;
10448{
10449 return (tv->v_lock & VAR_LOCKED)
10450 || (tv->v_type == VAR_LIST
10451 && tv->vval.v_list != NULL
10452 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
10453 || (tv->v_type == VAR_DICT
10454 && tv->vval.v_dict != NULL
10455 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
10456}
10457
10458/*
10459 * "islocked()" function
10460 */
10461 static void
10462f_islocked(argvars, rettv)
10463 typval_T *argvars;
10464 typval_T *rettv;
10465{
10466 lval_T lv;
10467 char_u *end;
10468 dictitem_T *di;
10469
10470 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010471 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10472 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010473 if (end != NULL && lv.ll_name != NULL)
10474 {
10475 if (*end != NUL)
10476 EMSG(_(e_trailing));
10477 else
10478 {
10479 if (lv.ll_tv == NULL)
10480 {
10481 if (check_changedtick(lv.ll_name))
10482 rettv->vval.v_number = 1; /* always locked */
10483 else
10484 {
10485 di = find_var(lv.ll_name, NULL);
10486 if (di != NULL)
10487 {
10488 /* Consider a variable locked when:
10489 * 1. the variable itself is locked
10490 * 2. the value of the variable is locked.
10491 * 3. the List or Dict value is locked.
10492 */
10493 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10494 || tv_islocked(&di->di_tv));
10495 }
10496 }
10497 }
10498 else if (lv.ll_range)
10499 EMSG(_("E745: Range not allowed"));
10500 else if (lv.ll_newkey != NULL)
10501 EMSG2(_(e_dictkey), lv.ll_newkey);
10502 else if (lv.ll_list != NULL)
10503 /* List item. */
10504 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10505 else
10506 /* Dictionary item. */
10507 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10508 }
10509 }
10510
10511 clear_lval(&lv);
10512}
10513
Bram Moolenaar33570922005-01-25 22:26:29 +000010514static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010515
10516/*
10517 * Turn a dict into a list:
10518 * "what" == 0: list of keys
10519 * "what" == 1: list of values
10520 * "what" == 2: list of items
10521 */
10522 static void
10523dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010524 typval_T *argvars;
10525 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010526 int what;
10527{
Bram Moolenaar33570922005-01-25 22:26:29 +000010528 list_T *l;
10529 list_T *l2;
10530 dictitem_T *di;
10531 hashitem_T *hi;
10532 listitem_T *li;
10533 listitem_T *li2;
10534 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010535 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010536
10537 rettv->vval.v_number = 0;
10538 if (argvars[0].v_type != VAR_DICT)
10539 {
10540 EMSG(_(e_dictreq));
10541 return;
10542 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010543 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010544 return;
10545
10546 l = list_alloc();
10547 if (l == NULL)
10548 return;
10549 rettv->v_type = VAR_LIST;
10550 rettv->vval.v_list = l;
10551 ++l->lv_refcount;
10552
Bram Moolenaar33570922005-01-25 22:26:29 +000010553 todo = d->dv_hashtab.ht_used;
10554 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010555 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010556 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010557 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010558 --todo;
10559 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010560
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010561 li = listitem_alloc();
10562 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010563 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010564 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010565
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010566 if (what == 0)
10567 {
10568 /* keys() */
10569 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010570 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010571 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10572 }
10573 else if (what == 1)
10574 {
10575 /* values() */
10576 copy_tv(&di->di_tv, &li->li_tv);
10577 }
10578 else
10579 {
10580 /* items() */
10581 l2 = list_alloc();
10582 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010583 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010584 li->li_tv.vval.v_list = l2;
10585 if (l2 == NULL)
10586 break;
10587 ++l2->lv_refcount;
10588
10589 li2 = listitem_alloc();
10590 if (li2 == NULL)
10591 break;
10592 list_append(l2, li2);
10593 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010594 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010595 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
10596
10597 li2 = listitem_alloc();
10598 if (li2 == NULL)
10599 break;
10600 list_append(l2, li2);
10601 copy_tv(&di->di_tv, &li2->li_tv);
10602 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010603 }
10604 }
10605}
10606
10607/*
10608 * "items(dict)" function
10609 */
10610 static void
10611f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010612 typval_T *argvars;
10613 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010614{
10615 dict_list(argvars, rettv, 2);
10616}
10617
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010619 * "join()" function
10620 */
10621 static void
10622f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010623 typval_T *argvars;
10624 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010625{
10626 garray_T ga;
10627 char_u *sep;
10628
10629 rettv->vval.v_number = 0;
10630 if (argvars[0].v_type != VAR_LIST)
10631 {
10632 EMSG(_(e_listreq));
10633 return;
10634 }
10635 if (argvars[0].vval.v_list == NULL)
10636 return;
10637 if (argvars[1].v_type == VAR_UNKNOWN)
10638 sep = (char_u *)" ";
10639 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010640 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010641
10642 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010643
10644 if (sep != NULL)
10645 {
10646 ga_init2(&ga, (int)sizeof(char), 80);
10647 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
10648 ga_append(&ga, NUL);
10649 rettv->vval.v_string = (char_u *)ga.ga_data;
10650 }
10651 else
10652 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010653}
10654
10655/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010656 * "keys()" function
10657 */
10658 static void
10659f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010660 typval_T *argvars;
10661 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010662{
10663 dict_list(argvars, rettv, 0);
10664}
10665
10666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 * "last_buffer_nr()" function.
10668 */
10669/*ARGSUSED*/
10670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010671f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010672 typval_T *argvars;
10673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674{
10675 int n = 0;
10676 buf_T *buf;
10677
10678 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10679 if (n < buf->b_fnum)
10680 n = buf->b_fnum;
10681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010682 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010683}
10684
10685/*
10686 * "len()" function
10687 */
10688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010689f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010690 typval_T *argvars;
10691 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010692{
10693 switch (argvars[0].v_type)
10694 {
10695 case VAR_STRING:
10696 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010697 rettv->vval.v_number = (varnumber_T)STRLEN(
10698 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010699 break;
10700 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010701 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010702 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010703 case VAR_DICT:
10704 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
10705 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010706 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010707 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010708 break;
10709 }
10710}
10711
Bram Moolenaar33570922005-01-25 22:26:29 +000010712static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010713
10714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010715libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010716 typval_T *argvars;
10717 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010718 int type;
10719{
10720#ifdef FEAT_LIBCALL
10721 char_u *string_in;
10722 char_u **string_result;
10723 int nr_result;
10724#endif
10725
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010726 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010727 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010728 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010729 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010730 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010731
10732 if (check_restricted() || check_secure())
10733 return;
10734
10735#ifdef FEAT_LIBCALL
10736 /* The first two args must be strings, otherwise its meaningless */
10737 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
10738 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010739 string_in = NULL;
10740 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010741 string_in = argvars[2].vval.v_string;
10742 if (type == VAR_NUMBER)
10743 string_result = NULL;
10744 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010745 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010746 if (mch_libcall(argvars[0].vval.v_string,
10747 argvars[1].vval.v_string,
10748 string_in,
10749 argvars[2].vval.v_number,
10750 string_result,
10751 &nr_result) == OK
10752 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010753 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010754 }
10755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756}
10757
10758/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010759 * "libcall()" function
10760 */
10761 static void
10762f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010763 typval_T *argvars;
10764 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010765{
10766 libcall_common(argvars, rettv, VAR_STRING);
10767}
10768
10769/*
10770 * "libcallnr()" function
10771 */
10772 static void
10773f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010774 typval_T *argvars;
10775 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010776{
10777 libcall_common(argvars, rettv, VAR_NUMBER);
10778}
10779
10780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 * "line(string)" function
10782 */
10783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010784f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010785 typval_T *argvars;
10786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787{
10788 linenr_T lnum = 0;
10789 pos_T *fp;
10790
10791 fp = var2fpos(&argvars[0], TRUE);
10792 if (fp != NULL)
10793 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010794 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795}
10796
10797/*
10798 * "line2byte(lnum)" function
10799 */
10800/*ARGSUSED*/
10801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010802f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010803 typval_T *argvars;
10804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805{
10806#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010807 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808#else
10809 linenr_T lnum;
10810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010811 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010813 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010815 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
10816 if (rettv->vval.v_number >= 0)
10817 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818#endif
10819}
10820
10821/*
10822 * "lispindent(lnum)" function
10823 */
10824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010825f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010826 typval_T *argvars;
10827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828{
10829#ifdef FEAT_LISP
10830 pos_T pos;
10831 linenr_T lnum;
10832
10833 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010834 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10836 {
10837 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 curwin->w_cursor = pos;
10840 }
10841 else
10842#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010843 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844}
10845
10846/*
10847 * "localtime()" function
10848 */
10849/*ARGSUSED*/
10850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010851f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010852 typval_T *argvars;
10853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010855 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856}
10857
Bram Moolenaar33570922005-01-25 22:26:29 +000010858static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859
10860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010861get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000010862 typval_T *argvars;
10863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 int exact;
10865{
10866 char_u *keys;
10867 char_u *which;
10868 char_u buf[NUMBUFLEN];
10869 char_u *keys_buf = NULL;
10870 char_u *rhs;
10871 int mode;
10872 garray_T ga;
10873
10874 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010875 rettv->v_type = VAR_STRING;
10876 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010878 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 if (*keys == NUL)
10880 return;
10881
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010882 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010883 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 else
10885 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010886 if (which == NULL)
10887 return;
10888
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 mode = get_map_mode(&which, 0);
10890
10891 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000010892 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 vim_free(keys_buf);
10894 if (rhs != NULL)
10895 {
10896 ga_init(&ga);
10897 ga.ga_itemsize = 1;
10898 ga.ga_growsize = 40;
10899
10900 while (*rhs != NUL)
10901 ga_concat(&ga, str2special(&rhs, FALSE));
10902
10903 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010904 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 }
10906}
10907
10908/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010909 * "map()" function
10910 */
10911 static void
10912f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010913 typval_T *argvars;
10914 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010915{
10916 filter_map(argvars, rettv, TRUE);
10917}
10918
10919/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010920 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 */
10922 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010923f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010924 typval_T *argvars;
10925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010927 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928}
10929
10930/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010931 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 */
10933 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010934f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010935 typval_T *argvars;
10936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010938 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939}
10940
Bram Moolenaar33570922005-01-25 22:26:29 +000010941static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942
10943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010944find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010945 typval_T *argvars;
10946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 int type;
10948{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010949 char_u *str = NULL;
10950 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 char_u *pat;
10952 regmatch_T regmatch;
10953 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010954 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 char_u *save_cpo;
10956 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010957 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010958 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010959 list_T *l = NULL;
10960 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010961 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010962 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963
10964 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10965 save_cpo = p_cpo;
10966 p_cpo = (char_u *)"";
10967
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010968 rettv->vval.v_number = -1;
10969 if (type == 3)
10970 {
10971 /* return empty list when there are no matches */
10972 if ((rettv->vval.v_list = list_alloc()) == NULL)
10973 goto theend;
10974 rettv->v_type = VAR_LIST;
10975 ++rettv->vval.v_list->lv_refcount;
10976 }
10977 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010979 rettv->v_type = VAR_STRING;
10980 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010983 if (argvars[0].v_type == VAR_LIST)
10984 {
10985 if ((l = argvars[0].vval.v_list) == NULL)
10986 goto theend;
10987 li = l->lv_first;
10988 }
10989 else
10990 expr = str = get_tv_string(&argvars[0]);
10991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010992 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
10993 if (pat == NULL)
10994 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010995
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010996 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010998 int error = FALSE;
10999
11000 start = get_tv_number_chk(&argvars[2], &error);
11001 if (error)
11002 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011003 if (l != NULL)
11004 {
11005 li = list_find(l, start);
11006 if (li == NULL)
11007 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011008 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011009 }
11010 else
11011 {
11012 if (start < 0)
11013 start = 0;
11014 if (start > (long)STRLEN(str))
11015 goto theend;
11016 str += start;
11017 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011018
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011019 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011020 nth = get_tv_number_chk(&argvars[3], &error);
11021 if (error)
11022 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 }
11024
11025 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11026 if (regmatch.regprog != NULL)
11027 {
11028 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011029
11030 while (1)
11031 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011032 if (l != NULL)
11033 {
11034 if (li == NULL)
11035 {
11036 match = FALSE;
11037 break;
11038 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011039 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011040 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011041 if (str == NULL)
11042 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011043 }
11044
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011045 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011046
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011047 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011048 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011049 if (l == NULL && !match)
11050 break;
11051
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011052 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011053 if (l != NULL)
11054 {
11055 li = li->li_next;
11056 ++idx;
11057 }
11058 else
11059 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011060#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011061 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011062#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011063 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011064#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011065 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011066 }
11067
11068 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011070 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011071 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011072 int i;
11073
11074 /* return list with matched string and submatches */
11075 for (i = 0; i < NSUBEXP; ++i)
11076 {
11077 if (regmatch.endp[i] == NULL)
11078 break;
11079 li = listitem_alloc();
11080 if (li == NULL)
11081 break;
11082 li->li_tv.v_type = VAR_STRING;
11083 li->li_tv.v_lock = 0;
11084 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11085 (int)(regmatch.endp[i] - regmatch.startp[i]));
11086 list_append(rettv->vval.v_list, li);
11087 }
11088 }
11089 else if (type == 2)
11090 {
11091 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011092 if (l != NULL)
11093 copy_tv(&li->li_tv, rettv);
11094 else
11095 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011097 }
11098 else if (l != NULL)
11099 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 else
11101 {
11102 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011103 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 (varnumber_T)(regmatch.startp[0] - str);
11105 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011106 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 }
11110 }
11111 vim_free(regmatch.regprog);
11112 }
11113
11114theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011115 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 p_cpo = save_cpo;
11117}
11118
11119/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011120 * "match()" function
11121 */
11122 static void
11123f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011124 typval_T *argvars;
11125 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011126{
11127 find_some_match(argvars, rettv, 1);
11128}
11129
11130/*
11131 * "matchend()" function
11132 */
11133 static void
11134f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011135 typval_T *argvars;
11136 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011137{
11138 find_some_match(argvars, rettv, 0);
11139}
11140
11141/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011142 * "matchlist()" function
11143 */
11144 static void
11145f_matchlist(argvars, rettv)
11146 typval_T *argvars;
11147 typval_T *rettv;
11148{
11149 find_some_match(argvars, rettv, 3);
11150}
11151
11152/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011153 * "matchstr()" function
11154 */
11155 static void
11156f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011157 typval_T *argvars;
11158 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011159{
11160 find_some_match(argvars, rettv, 2);
11161}
11162
Bram Moolenaar33570922005-01-25 22:26:29 +000011163static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011164
11165 static void
11166max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011167 typval_T *argvars;
11168 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011169 int domax;
11170{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011171 long n = 0;
11172 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011173 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011174
11175 if (argvars[0].v_type == VAR_LIST)
11176 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011177 list_T *l;
11178 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011179
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011180 l = argvars[0].vval.v_list;
11181 if (l != NULL)
11182 {
11183 li = l->lv_first;
11184 if (li != NULL)
11185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011186 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011187 while (1)
11188 {
11189 li = li->li_next;
11190 if (li == NULL)
11191 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011192 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011193 if (domax ? i > n : i < n)
11194 n = i;
11195 }
11196 }
11197 }
11198 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011199 else if (argvars[0].v_type == VAR_DICT)
11200 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011201 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011202 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011203 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011204 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011205
11206 d = argvars[0].vval.v_dict;
11207 if (d != NULL)
11208 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011209 todo = d->dv_hashtab.ht_used;
11210 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011211 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011212 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011213 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011214 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011215 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011216 if (first)
11217 {
11218 n = i;
11219 first = FALSE;
11220 }
11221 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011222 n = i;
11223 }
11224 }
11225 }
11226 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011227 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011228 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011229 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011230}
11231
11232/*
11233 * "max()" function
11234 */
11235 static void
11236f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011237 typval_T *argvars;
11238 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011239{
11240 max_min(argvars, rettv, TRUE);
11241}
11242
11243/*
11244 * "min()" function
11245 */
11246 static void
11247f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011248 typval_T *argvars;
11249 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011250{
11251 max_min(argvars, rettv, FALSE);
11252}
11253
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011254static int mkdir_recurse __ARGS((char_u *dir, int prot));
11255
11256/*
11257 * Create the directory in which "dir" is located, and higher levels when
11258 * needed.
11259 */
11260 static int
11261mkdir_recurse(dir, prot)
11262 char_u *dir;
11263 int prot;
11264{
11265 char_u *p;
11266 char_u *updir;
11267 int r = FAIL;
11268
11269 /* Get end of directory name in "dir".
11270 * We're done when it's "/" or "c:/". */
11271 p = gettail_sep(dir);
11272 if (p <= get_past_head(dir))
11273 return OK;
11274
11275 /* If the directory exists we're done. Otherwise: create it.*/
11276 updir = vim_strnsave(dir, (int)(p - dir));
11277 if (updir == NULL)
11278 return FAIL;
11279 if (mch_isdir(updir))
11280 r = OK;
11281 else if (mkdir_recurse(updir, prot) == OK)
11282 r = vim_mkdir_emsg(updir, prot);
11283 vim_free(updir);
11284 return r;
11285}
11286
11287#ifdef vim_mkdir
11288/*
11289 * "mkdir()" function
11290 */
11291 static void
11292f_mkdir(argvars, rettv)
11293 typval_T *argvars;
11294 typval_T *rettv;
11295{
11296 char_u *dir;
11297 char_u buf[NUMBUFLEN];
11298 int prot = 0755;
11299
11300 rettv->vval.v_number = FAIL;
11301 if (check_restricted() || check_secure())
11302 return;
11303
11304 dir = get_tv_string_buf(&argvars[0], buf);
11305 if (argvars[1].v_type != VAR_UNKNOWN)
11306 {
11307 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011308 prot = get_tv_number_chk(&argvars[2], NULL);
11309 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011310 mkdir_recurse(dir, prot);
11311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011312 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011313}
11314#endif
11315
Bram Moolenaar0d660222005-01-07 21:51:51 +000011316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 * "mode()" function
11318 */
11319/*ARGSUSED*/
11320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011322 typval_T *argvars;
11323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324{
11325 char_u buf[2];
11326
11327#ifdef FEAT_VISUAL
11328 if (VIsual_active)
11329 {
11330 if (VIsual_select)
11331 buf[0] = VIsual_mode + 's' - 'v';
11332 else
11333 buf[0] = VIsual_mode;
11334 }
11335 else
11336#endif
11337 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11338 buf[0] = 'r';
11339 else if (State & INSERT)
11340 {
11341 if (State & REPLACE_FLAG)
11342 buf[0] = 'R';
11343 else
11344 buf[0] = 'i';
11345 }
11346 else if (State & CMDLINE)
11347 buf[0] = 'c';
11348 else
11349 buf[0] = 'n';
11350
11351 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011352 rettv->vval.v_string = vim_strsave(buf);
11353 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354}
11355
11356/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011357 * "nextnonblank()" function
11358 */
11359 static void
11360f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011361 typval_T *argvars;
11362 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011363{
11364 linenr_T lnum;
11365
11366 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011368 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011369 {
11370 lnum = 0;
11371 break;
11372 }
11373 if (*skipwhite(ml_get(lnum)) != NUL)
11374 break;
11375 }
11376 rettv->vval.v_number = lnum;
11377}
11378
11379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 * "nr2char()" function
11381 */
11382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011383f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011384 typval_T *argvars;
11385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386{
11387 char_u buf[NUMBUFLEN];
11388
11389#ifdef FEAT_MBYTE
11390 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011391 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 else
11393#endif
11394 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011395 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396 buf[1] = NUL;
11397 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011398 rettv->v_type = VAR_STRING;
11399 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011400}
11401
11402/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011403 * "prevnonblank()" function
11404 */
11405 static void
11406f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011407 typval_T *argvars;
11408 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011409{
11410 linenr_T lnum;
11411
11412 lnum = get_tv_lnum(argvars);
11413 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11414 lnum = 0;
11415 else
11416 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11417 --lnum;
11418 rettv->vval.v_number = lnum;
11419}
11420
Bram Moolenaar8c711452005-01-14 21:53:12 +000011421/*
11422 * "range()" function
11423 */
11424 static void
11425f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011426 typval_T *argvars;
11427 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011428{
11429 long start;
11430 long end;
11431 long stride = 1;
11432 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011433 list_T *l;
11434 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011435 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011436
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011437 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011438 if (argvars[1].v_type == VAR_UNKNOWN)
11439 {
11440 end = start - 1;
11441 start = 0;
11442 }
11443 else
11444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011445 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011446 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011447 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011448 }
11449
11450 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011451 if (error)
11452 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000011453 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011454 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000011455 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011456 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011457 else
11458 {
11459 l = list_alloc();
11460 if (l != NULL)
11461 {
11462 rettv->v_type = VAR_LIST;
11463 rettv->vval.v_list = l;
11464 ++l->lv_refcount;
11465
11466 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11467 {
11468 li = listitem_alloc();
11469 if (li == NULL)
11470 break;
11471 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011472 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011473 li->li_tv.vval.v_number = i;
11474 list_append(l, li);
11475 }
11476 }
11477 }
11478}
11479
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011480/*
11481 * "readfile()" function
11482 */
11483 static void
11484f_readfile(argvars, rettv)
11485 typval_T *argvars;
11486 typval_T *rettv;
11487{
11488 int binary = FALSE;
11489 char_u *fname;
11490 FILE *fd;
11491 list_T *l;
11492 listitem_T *li;
11493#define FREAD_SIZE 200 /* optimized for text lines */
11494 char_u buf[FREAD_SIZE];
11495 int readlen; /* size of last fread() */
11496 int buflen; /* nr of valid chars in buf[] */
11497 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11498 int tolist; /* first byte in buf[] still to be put in list */
11499 int chop; /* how many CR to chop off */
11500 char_u *prev = NULL; /* previously read bytes, if any */
11501 int prevlen = 0; /* length of "prev" if not NULL */
11502 char_u *s;
11503 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011504 long maxline = MAXLNUM;
11505 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011506
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011507 if (argvars[1].v_type != VAR_UNKNOWN)
11508 {
11509 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11510 binary = TRUE;
11511 if (argvars[2].v_type != VAR_UNKNOWN)
11512 maxline = get_tv_number(&argvars[2]);
11513 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011514
11515 l = list_alloc();
11516 if (l == NULL)
11517 return;
11518 rettv->v_type = VAR_LIST;
11519 rettv->vval.v_list = l;
11520 l->lv_refcount = 1;
11521
11522 /* Always open the file in binary mode, library functions have a mind of
11523 * their own about CR-LF conversion. */
11524 fname = get_tv_string(&argvars[0]);
11525 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11526 {
11527 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11528 return;
11529 }
11530
11531 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011532 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011533 {
11534 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11535 buflen = filtd + readlen;
11536 tolist = 0;
11537 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11538 {
11539 if (buf[filtd] == '\n' || readlen <= 0)
11540 {
11541 /* Only when in binary mode add an empty list item when the
11542 * last line ends in a '\n'. */
11543 if (!binary && readlen == 0 && filtd == 0)
11544 break;
11545
11546 /* Found end-of-line or end-of-file: add a text line to the
11547 * list. */
11548 chop = 0;
11549 if (!binary)
11550 while (filtd - chop - 1 >= tolist
11551 && buf[filtd - chop - 1] == '\r')
11552 ++chop;
11553 len = filtd - tolist - chop;
11554 if (prev == NULL)
11555 s = vim_strnsave(buf + tolist, len);
11556 else
11557 {
11558 s = alloc((unsigned)(prevlen + len + 1));
11559 if (s != NULL)
11560 {
11561 mch_memmove(s, prev, prevlen);
11562 vim_free(prev);
11563 prev = NULL;
11564 mch_memmove(s + prevlen, buf + tolist, len);
11565 s[prevlen + len] = NUL;
11566 }
11567 }
11568 tolist = filtd + 1;
11569
11570 li = listitem_alloc();
11571 if (li == NULL)
11572 {
11573 vim_free(s);
11574 break;
11575 }
11576 li->li_tv.v_type = VAR_STRING;
11577 li->li_tv.v_lock = 0;
11578 li->li_tv.vval.v_string = s;
11579 list_append(l, li);
11580
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011581 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011582 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011583 if (readlen <= 0)
11584 break;
11585 }
11586 else if (buf[filtd] == NUL)
11587 buf[filtd] = '\n';
11588 }
11589 if (readlen <= 0)
11590 break;
11591
11592 if (tolist == 0)
11593 {
11594 /* "buf" is full, need to move text to an allocated buffer */
11595 if (prev == NULL)
11596 {
11597 prev = vim_strnsave(buf, buflen);
11598 prevlen = buflen;
11599 }
11600 else
11601 {
11602 s = alloc((unsigned)(prevlen + buflen));
11603 if (s != NULL)
11604 {
11605 mch_memmove(s, prev, prevlen);
11606 mch_memmove(s + prevlen, buf, buflen);
11607 vim_free(prev);
11608 prev = s;
11609 prevlen += buflen;
11610 }
11611 }
11612 filtd = 0;
11613 }
11614 else
11615 {
11616 mch_memmove(buf, buf + tolist, buflen - tolist);
11617 filtd -= tolist;
11618 }
11619 }
11620
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011621 /*
11622 * For a negative line count use only the lines at the end of the file,
11623 * free the rest.
11624 */
11625 if (maxline < 0)
11626 while (cnt > -maxline)
11627 {
11628 listitem_remove(l, l->lv_first);
11629 --cnt;
11630 }
11631
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011632 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011633 fclose(fd);
11634}
11635
11636
Bram Moolenaar0d660222005-01-07 21:51:51 +000011637#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
11638static void make_connection __ARGS((void));
11639static int check_connection __ARGS((void));
11640
11641 static void
11642make_connection()
11643{
11644 if (X_DISPLAY == NULL
11645# ifdef FEAT_GUI
11646 && !gui.in_use
11647# endif
11648 )
11649 {
11650 x_force_connect = TRUE;
11651 setup_term_clip();
11652 x_force_connect = FALSE;
11653 }
11654}
11655
11656 static int
11657check_connection()
11658{
11659 make_connection();
11660 if (X_DISPLAY == NULL)
11661 {
11662 EMSG(_("E240: No connection to Vim server"));
11663 return FAIL;
11664 }
11665 return OK;
11666}
11667#endif
11668
11669#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011670static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011671
11672 static void
11673remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000011674 typval_T *argvars;
11675 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011676 int expr;
11677{
11678 char_u *server_name;
11679 char_u *keys;
11680 char_u *r = NULL;
11681 char_u buf[NUMBUFLEN];
11682# ifdef WIN32
11683 HWND w;
11684# else
11685 Window w;
11686# endif
11687
11688 if (check_restricted() || check_secure())
11689 return;
11690
11691# ifdef FEAT_X11
11692 if (check_connection() == FAIL)
11693 return;
11694# endif
11695
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011696 server_name = get_tv_string_chk(&argvars[0]);
11697 if (server_name == NULL)
11698 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011699 keys = get_tv_string_buf(&argvars[1], buf);
11700# ifdef WIN32
11701 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
11702# else
11703 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
11704 < 0)
11705# endif
11706 {
11707 if (r != NULL)
11708 EMSG(r); /* sending worked but evaluation failed */
11709 else
11710 EMSG2(_("E241: Unable to send to %s"), server_name);
11711 return;
11712 }
11713
11714 rettv->vval.v_string = r;
11715
11716 if (argvars[2].v_type != VAR_UNKNOWN)
11717 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011718 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000011719 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011720 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011721
11722 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000011723 v.di_tv.v_type = VAR_STRING;
11724 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011725 idvar = get_tv_string_chk(&argvars[2]);
11726 if (idvar != NULL)
11727 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000011728 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011729 }
11730}
11731#endif
11732
11733/*
11734 * "remote_expr()" function
11735 */
11736/*ARGSUSED*/
11737 static void
11738f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011739 typval_T *argvars;
11740 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011741{
11742 rettv->v_type = VAR_STRING;
11743 rettv->vval.v_string = NULL;
11744#ifdef FEAT_CLIENTSERVER
11745 remote_common(argvars, rettv, TRUE);
11746#endif
11747}
11748
11749/*
11750 * "remote_foreground()" function
11751 */
11752/*ARGSUSED*/
11753 static void
11754f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011755 typval_T *argvars;
11756 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011757{
11758 rettv->vval.v_number = 0;
11759#ifdef FEAT_CLIENTSERVER
11760# ifdef WIN32
11761 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011762 {
11763 char_u *server_name = get_tv_string_chk(&argvars[0]);
11764
11765 if (server_name != NULL)
11766 serverForeground(server_name);
11767 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011768# else
11769 /* Send a foreground() expression to the server. */
11770 argvars[1].v_type = VAR_STRING;
11771 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
11772 argvars[2].v_type = VAR_UNKNOWN;
11773 remote_common(argvars, rettv, TRUE);
11774 vim_free(argvars[1].vval.v_string);
11775# endif
11776#endif
11777}
11778
11779/*ARGSUSED*/
11780 static void
11781f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011782 typval_T *argvars;
11783 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011784{
11785#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011786 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011787 char_u *s = NULL;
11788# ifdef WIN32
11789 int n = 0;
11790# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011791 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011792
11793 if (check_restricted() || check_secure())
11794 {
11795 rettv->vval.v_number = -1;
11796 return;
11797 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011798 serverid = get_tv_string_chk(&argvars[0]);
11799 if (serverid == NULL)
11800 {
11801 rettv->vval.v_number = -1;
11802 return; /* type error; errmsg already given */
11803 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011804# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011805 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011806 if (n == 0)
11807 rettv->vval.v_number = -1;
11808 else
11809 {
11810 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
11811 rettv->vval.v_number = (s != NULL);
11812 }
11813# else
11814 rettv->vval.v_number = 0;
11815 if (check_connection() == FAIL)
11816 return;
11817
11818 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011819 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011820# endif
11821
11822 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
11823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011824 char_u *retvar;
11825
Bram Moolenaar33570922005-01-25 22:26:29 +000011826 v.di_tv.v_type = VAR_STRING;
11827 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011828 retvar = get_tv_string_chk(&argvars[1]);
11829 if (retvar != NULL)
11830 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000011831 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011832 }
11833#else
11834 rettv->vval.v_number = -1;
11835#endif
11836}
11837
11838/*ARGSUSED*/
11839 static void
11840f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011841 typval_T *argvars;
11842 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011843{
11844 char_u *r = NULL;
11845
11846#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011847 char_u *serverid = get_tv_string_chk(&argvars[0]);
11848
11849 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000011850 {
11851# ifdef WIN32
11852 /* The server's HWND is encoded in the 'id' parameter */
11853 int n = 0;
11854
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011855 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011856 if (n != 0)
11857 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
11858 if (r == NULL)
11859# else
11860 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011861 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011862# endif
11863 EMSG(_("E277: Unable to read a server reply"));
11864 }
11865#endif
11866 rettv->v_type = VAR_STRING;
11867 rettv->vval.v_string = r;
11868}
11869
11870/*
11871 * "remote_send()" function
11872 */
11873/*ARGSUSED*/
11874 static void
11875f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011876 typval_T *argvars;
11877 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011878{
11879 rettv->v_type = VAR_STRING;
11880 rettv->vval.v_string = NULL;
11881#ifdef FEAT_CLIENTSERVER
11882 remote_common(argvars, rettv, FALSE);
11883#endif
11884}
11885
11886/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011887 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011888 */
11889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011891 typval_T *argvars;
11892 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011893{
Bram Moolenaar33570922005-01-25 22:26:29 +000011894 list_T *l;
11895 listitem_T *item, *item2;
11896 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011897 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011898 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011899 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000011900 dict_T *d;
11901 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011902
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011903 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011904 if (argvars[0].v_type == VAR_DICT)
11905 {
11906 if (argvars[2].v_type != VAR_UNKNOWN)
11907 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011908 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000011909 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011910 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011911 key = get_tv_string_chk(&argvars[1]);
11912 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011913 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011914 di = dict_find(d, key, -1);
11915 if (di == NULL)
11916 EMSG2(_(e_dictkey), key);
11917 else
11918 {
11919 *rettv = di->di_tv;
11920 init_tv(&di->di_tv);
11921 dictitem_remove(d, di);
11922 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011923 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011924 }
11925 }
11926 else if (argvars[0].v_type != VAR_LIST)
11927 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011928 else if ((l = argvars[0].vval.v_list) != NULL
11929 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011930 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011931 int error = FALSE;
11932
11933 idx = get_tv_number_chk(&argvars[1], &error);
11934 if (error)
11935 ; /* type error: do nothing, errmsg already given */
11936 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011937 EMSGN(_(e_listidx), idx);
11938 else
11939 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011940 if (argvars[2].v_type == VAR_UNKNOWN)
11941 {
11942 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011943 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011944 *rettv = item->li_tv;
11945 vim_free(item);
11946 }
11947 else
11948 {
11949 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011950 end = get_tv_number_chk(&argvars[2], &error);
11951 if (error)
11952 ; /* type error: do nothing */
11953 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011954 EMSGN(_(e_listidx), end);
11955 else
11956 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011957 int cnt = 0;
11958
11959 for (li = item; li != NULL; li = li->li_next)
11960 {
11961 ++cnt;
11962 if (li == item2)
11963 break;
11964 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011965 if (li == NULL) /* didn't find "item2" after "item" */
11966 EMSG(_(e_invrange));
11967 else
11968 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011969 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011970 l = list_alloc();
11971 if (l != NULL)
11972 {
11973 rettv->v_type = VAR_LIST;
11974 rettv->vval.v_list = l;
11975 l->lv_first = item;
11976 l->lv_last = item2;
11977 l->lv_refcount = 1;
11978 item->li_prev = NULL;
11979 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011980 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011981 }
11982 }
11983 }
11984 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011985 }
11986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987}
11988
11989/*
11990 * "rename({from}, {to})" function
11991 */
11992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011994 typval_T *argvars;
11995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996{
11997 char_u buf[NUMBUFLEN];
11998
11999 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012000 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012002 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12003 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004}
12005
12006/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012007 * "repeat()" function
12008 */
12009/*ARGSUSED*/
12010 static void
12011f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012012 typval_T *argvars;
12013 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012014{
12015 char_u *p;
12016 int n;
12017 int slen;
12018 int len;
12019 char_u *r;
12020 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012021 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012022
12023 n = get_tv_number(&argvars[1]);
12024 if (argvars[0].v_type == VAR_LIST)
12025 {
12026 l = list_alloc();
12027 if (l != NULL && argvars[0].vval.v_list != NULL)
12028 {
12029 l->lv_refcount = 1;
12030 while (n-- > 0)
12031 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12032 break;
12033 }
12034 rettv->v_type = VAR_LIST;
12035 rettv->vval.v_list = l;
12036 }
12037 else
12038 {
12039 p = get_tv_string(&argvars[0]);
12040 rettv->v_type = VAR_STRING;
12041 rettv->vval.v_string = NULL;
12042
12043 slen = (int)STRLEN(p);
12044 len = slen * n;
12045 if (len <= 0)
12046 return;
12047
12048 r = alloc(len + 1);
12049 if (r != NULL)
12050 {
12051 for (i = 0; i < n; i++)
12052 mch_memmove(r + i * slen, p, (size_t)slen);
12053 r[len] = NUL;
12054 }
12055
12056 rettv->vval.v_string = r;
12057 }
12058}
12059
12060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 * "resolve()" function
12062 */
12063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012064f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012065 typval_T *argvars;
12066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067{
12068 char_u *p;
12069
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012070 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071#ifdef FEAT_SHORTCUT
12072 {
12073 char_u *v = NULL;
12074
12075 v = mch_resolve_shortcut(p);
12076 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012077 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012079 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 }
12081#else
12082# ifdef HAVE_READLINK
12083 {
12084 char_u buf[MAXPATHL + 1];
12085 char_u *cpy;
12086 int len;
12087 char_u *remain = NULL;
12088 char_u *q;
12089 int is_relative_to_current = FALSE;
12090 int has_trailing_pathsep = FALSE;
12091 int limit = 100;
12092
12093 p = vim_strsave(p);
12094
12095 if (p[0] == '.' && (vim_ispathsep(p[1])
12096 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12097 is_relative_to_current = TRUE;
12098
12099 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012100 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101 has_trailing_pathsep = TRUE;
12102
12103 q = getnextcomp(p);
12104 if (*q != NUL)
12105 {
12106 /* Separate the first path component in "p", and keep the
12107 * remainder (beginning with the path separator). */
12108 remain = vim_strsave(q - 1);
12109 q[-1] = NUL;
12110 }
12111
12112 for (;;)
12113 {
12114 for (;;)
12115 {
12116 len = readlink((char *)p, (char *)buf, MAXPATHL);
12117 if (len <= 0)
12118 break;
12119 buf[len] = NUL;
12120
12121 if (limit-- == 0)
12122 {
12123 vim_free(p);
12124 vim_free(remain);
12125 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012126 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127 goto fail;
12128 }
12129
12130 /* Ensure that the result will have a trailing path separator
12131 * if the argument has one. */
12132 if (remain == NULL && has_trailing_pathsep)
12133 add_pathsep(buf);
12134
12135 /* Separate the first path component in the link value and
12136 * concatenate the remainders. */
12137 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12138 if (*q != NUL)
12139 {
12140 if (remain == NULL)
12141 remain = vim_strsave(q - 1);
12142 else
12143 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012144 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145 if (cpy != NULL)
12146 {
12147 STRCAT(cpy, remain);
12148 vim_free(remain);
12149 remain = cpy;
12150 }
12151 }
12152 q[-1] = NUL;
12153 }
12154
12155 q = gettail(p);
12156 if (q > p && *q == NUL)
12157 {
12158 /* Ignore trailing path separator. */
12159 q[-1] = NUL;
12160 q = gettail(p);
12161 }
12162 if (q > p && !mch_isFullName(buf))
12163 {
12164 /* symlink is relative to directory of argument */
12165 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12166 if (cpy != NULL)
12167 {
12168 STRCPY(cpy, p);
12169 STRCPY(gettail(cpy), buf);
12170 vim_free(p);
12171 p = cpy;
12172 }
12173 }
12174 else
12175 {
12176 vim_free(p);
12177 p = vim_strsave(buf);
12178 }
12179 }
12180
12181 if (remain == NULL)
12182 break;
12183
12184 /* Append the first path component of "remain" to "p". */
12185 q = getnextcomp(remain + 1);
12186 len = q - remain - (*q != NUL);
12187 cpy = vim_strnsave(p, STRLEN(p) + len);
12188 if (cpy != NULL)
12189 {
12190 STRNCAT(cpy, remain, len);
12191 vim_free(p);
12192 p = cpy;
12193 }
12194 /* Shorten "remain". */
12195 if (*q != NUL)
12196 STRCPY(remain, q - 1);
12197 else
12198 {
12199 vim_free(remain);
12200 remain = NULL;
12201 }
12202 }
12203
12204 /* If the result is a relative path name, make it explicitly relative to
12205 * the current directory if and only if the argument had this form. */
12206 if (!vim_ispathsep(*p))
12207 {
12208 if (is_relative_to_current
12209 && *p != NUL
12210 && !(p[0] == '.'
12211 && (p[1] == NUL
12212 || vim_ispathsep(p[1])
12213 || (p[1] == '.'
12214 && (p[2] == NUL
12215 || vim_ispathsep(p[2]))))))
12216 {
12217 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012218 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219 if (cpy != NULL)
12220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221 vim_free(p);
12222 p = cpy;
12223 }
12224 }
12225 else if (!is_relative_to_current)
12226 {
12227 /* Strip leading "./". */
12228 q = p;
12229 while (q[0] == '.' && vim_ispathsep(q[1]))
12230 q += 2;
12231 if (q > p)
12232 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12233 }
12234 }
12235
12236 /* Ensure that the result will have no trailing path separator
12237 * if the argument had none. But keep "/" or "//". */
12238 if (!has_trailing_pathsep)
12239 {
12240 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012241 if (after_pathsep(p, q))
12242 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243 }
12244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012245 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246 }
12247# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012248 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249# endif
12250#endif
12251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012252 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253
12254#ifdef HAVE_READLINK
12255fail:
12256#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012257 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258}
12259
12260/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012261 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262 */
12263 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012264f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012265 typval_T *argvars;
12266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267{
Bram Moolenaar33570922005-01-25 22:26:29 +000012268 list_T *l;
12269 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270
Bram Moolenaar0d660222005-01-07 21:51:51 +000012271 rettv->vval.v_number = 0;
12272 if (argvars[0].v_type != VAR_LIST)
12273 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012274 else if ((l = argvars[0].vval.v_list) != NULL
12275 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012276 {
12277 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012278 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012279 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012280 while (li != NULL)
12281 {
12282 ni = li->li_prev;
12283 list_append(l, li);
12284 li = ni;
12285 }
12286 rettv->vval.v_list = l;
12287 rettv->v_type = VAR_LIST;
12288 ++l->lv_refcount;
12289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290}
12291
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012292#define SP_NOMOVE 1 /* don't move cursor */
12293#define SP_REPEAT 2 /* repeat to find outer pair */
12294#define SP_RETCOUNT 4 /* return matchcount */
12295
Bram Moolenaar33570922005-01-25 22:26:29 +000012296static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012297
12298/*
12299 * Get flags for a search function.
12300 * Possibly sets "p_ws".
12301 * Returns BACKWARD, FORWARD or zero (for an error).
12302 */
12303 static int
12304get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012305 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012306 int *flagsp;
12307{
12308 int dir = FORWARD;
12309 char_u *flags;
12310 char_u nbuf[NUMBUFLEN];
12311 int mask;
12312
12313 if (varp->v_type != VAR_UNKNOWN)
12314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012315 flags = get_tv_string_buf_chk(varp, nbuf);
12316 if (flags == NULL)
12317 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012318 while (*flags != NUL)
12319 {
12320 switch (*flags)
12321 {
12322 case 'b': dir = BACKWARD; break;
12323 case 'w': p_ws = TRUE; break;
12324 case 'W': p_ws = FALSE; break;
12325 default: mask = 0;
12326 if (flagsp != NULL)
12327 switch (*flags)
12328 {
12329 case 'n': mask = SP_NOMOVE; break;
12330 case 'r': mask = SP_REPEAT; break;
12331 case 'm': mask = SP_RETCOUNT; break;
12332 }
12333 if (mask == 0)
12334 {
12335 EMSG2(_(e_invarg2), flags);
12336 dir = 0;
12337 }
12338 else
12339 *flagsp |= mask;
12340 }
12341 if (dir == 0)
12342 break;
12343 ++flags;
12344 }
12345 }
12346 return dir;
12347}
12348
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349/*
12350 * "search()" function
12351 */
12352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012353f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012354 typval_T *argvars;
12355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356{
12357 char_u *pat;
12358 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012359 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 int save_p_ws = p_ws;
12361 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012362 int flags = 0;
12363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012364 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012366 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012367 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12368 if (dir == 0)
12369 goto theend;
12370 if ((flags & ~SP_NOMOVE) != 0)
12371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012372 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012373 goto theend;
12374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012376 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12378 SEARCH_KEEP, RE_SEARCH) != FAIL)
12379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012380 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 curwin->w_cursor = pos;
12382 /* "/$" will put the cursor after the end of the line, may need to
12383 * correct that here */
12384 check_cursor();
12385 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012386
12387 /* If 'n' flag is used: restore cursor position. */
12388 if (flags & SP_NOMOVE)
12389 curwin->w_cursor = save_cursor;
12390theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391 p_ws = save_p_ws;
12392}
12393
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394/*
12395 * "searchpair()" function
12396 */
12397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012398f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012399 typval_T *argvars;
12400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401{
12402 char_u *spat, *mpat, *epat;
12403 char_u *skip;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012404 char_u *pat, *pat2 = NULL, *pat3 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405 pos_T pos;
12406 pos_T firstpos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012407 pos_T foundpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 pos_T save_cursor;
12409 pos_T save_pos;
12410 int save_p_ws = p_ws;
12411 char_u *save_cpo;
12412 int dir;
12413 int flags = 0;
12414 char_u nbuf1[NUMBUFLEN];
12415 char_u nbuf2[NUMBUFLEN];
12416 char_u nbuf3[NUMBUFLEN];
12417 int n;
12418 int r;
12419 int nest = 1;
12420 int err;
12421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012422 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423
12424 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12425 save_cpo = p_cpo;
12426 p_cpo = (char_u *)"";
12427
12428 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012429 spat = get_tv_string_chk(&argvars[0]);
12430 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
12431 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
12432 if (spat == NULL || mpat == NULL || epat == NULL)
12433 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434
12435 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12436 * start/middle/end (pat3, for the top pair). */
12437 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12438 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12439 if (pat2 == NULL || pat3 == NULL)
12440 goto theend;
12441 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12442 if (*mpat == NUL)
12443 STRCPY(pat3, pat2);
12444 else
12445 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12446 spat, epat, mpat);
12447
12448 /* Handle the optional fourth argument: flags */
12449 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012450 if (dir == 0)
12451 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452
12453 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012454 if (argvars[3].v_type == VAR_UNKNOWN
12455 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012456 skip = (char_u *)"";
12457 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012458 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
12459 if (skip == NULL)
12460 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461
12462 save_cursor = curwin->w_cursor;
12463 pos = curwin->w_cursor;
12464 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012465 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466 pat = pat3;
12467 for (;;)
12468 {
12469 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12470 SEARCH_KEEP, RE_SEARCH);
12471 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12472 /* didn't find it or found the first match again: FAIL */
12473 break;
12474
12475 if (firstpos.lnum == 0)
12476 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012477 if (equalpos(pos, foundpos))
12478 {
12479 /* Found the same position again. Can happen with a pattern that
12480 * has "\zs" at the end and searching backwards. Advance one
12481 * character and try again. */
12482 if (dir == BACKWARD)
12483 decl(&pos);
12484 else
12485 incl(&pos);
12486 }
12487 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488
12489 /* If the skip pattern matches, ignore this match. */
12490 if (*skip != NUL)
12491 {
12492 save_pos = curwin->w_cursor;
12493 curwin->w_cursor = pos;
12494 r = eval_to_bool(skip, &err, NULL, FALSE);
12495 curwin->w_cursor = save_pos;
12496 if (err)
12497 {
12498 /* Evaluating {skip} caused an error, break here. */
12499 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012500 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501 break;
12502 }
12503 if (r)
12504 continue;
12505 }
12506
12507 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12508 {
12509 /* Found end when searching backwards or start when searching
12510 * forward: nested pair. */
12511 ++nest;
12512 pat = pat2; /* nested, don't search for middle */
12513 }
12514 else
12515 {
12516 /* Found end when searching forward or start when searching
12517 * backward: end of (nested) pair; or found middle in outer pair. */
12518 if (--nest == 1)
12519 pat = pat3; /* outer level, search for middle */
12520 }
12521
12522 if (nest == 0)
12523 {
12524 /* Found the match: return matchcount or line number. */
12525 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012526 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012528 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529 curwin->w_cursor = pos;
12530 if (!(flags & SP_REPEAT))
12531 break;
12532 nest = 1; /* search for next unmatched */
12533 }
12534 }
12535
12536 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012537 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012538 curwin->w_cursor = save_cursor;
12539
12540theend:
12541 vim_free(pat2);
12542 vim_free(pat3);
12543 p_ws = save_p_ws;
12544 p_cpo = save_cpo;
12545}
12546
Bram Moolenaar0d660222005-01-07 21:51:51 +000012547/*ARGSUSED*/
12548 static void
12549f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012550 typval_T *argvars;
12551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012553#ifdef FEAT_CLIENTSERVER
12554 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012555 char_u *server = get_tv_string_chk(&argvars[0]);
12556 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557
Bram Moolenaar0d660222005-01-07 21:51:51 +000012558 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012559 if (server == NULL || reply == NULL)
12560 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012561 if (check_restricted() || check_secure())
12562 return;
12563# ifdef FEAT_X11
12564 if (check_connection() == FAIL)
12565 return;
12566# endif
12567
12568 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012570 EMSG(_("E258: Unable to send to client"));
12571 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012573 rettv->vval.v_number = 0;
12574#else
12575 rettv->vval.v_number = -1;
12576#endif
12577}
12578
12579/*ARGSUSED*/
12580 static void
12581f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012582 typval_T *argvars;
12583 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012584{
12585 char_u *r = NULL;
12586
12587#ifdef FEAT_CLIENTSERVER
12588# ifdef WIN32
12589 r = serverGetVimNames();
12590# else
12591 make_connection();
12592 if (X_DISPLAY != NULL)
12593 r = serverGetVimNames(X_DISPLAY);
12594# endif
12595#endif
12596 rettv->v_type = VAR_STRING;
12597 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598}
12599
12600/*
12601 * "setbufvar()" function
12602 */
12603/*ARGSUSED*/
12604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012605f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012606 typval_T *argvars;
12607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608{
12609 buf_T *buf;
12610#ifdef FEAT_AUTOCMD
12611 aco_save_T aco;
12612#else
12613 buf_T *save_curbuf;
12614#endif
12615 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012616 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617 char_u nbuf[NUMBUFLEN];
12618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012619 rettv->vval.v_number = 0;
12620
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621 if (check_restricted() || check_secure())
12622 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012623 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12624 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012625 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626 varp = &argvars[2];
12627
12628 if (buf != NULL && varname != NULL && varp != NULL)
12629 {
12630 /* set curbuf to be our buf, temporarily */
12631#ifdef FEAT_AUTOCMD
12632 aucmd_prepbuf(&aco, buf);
12633#else
12634 save_curbuf = curbuf;
12635 curbuf = buf;
12636#endif
12637
12638 if (*varname == '&')
12639 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012640 long numval;
12641 char_u *strval;
12642 int error = FALSE;
12643
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012645 numval = get_tv_number_chk(varp, &error);
12646 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012647 if (!error && strval != NULL)
12648 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649 }
12650 else
12651 {
12652 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
12653 if (bufvarname != NULL)
12654 {
12655 STRCPY(bufvarname, "b:");
12656 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012657 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658 vim_free(bufvarname);
12659 }
12660 }
12661
12662 /* reset notion of buffer */
12663#ifdef FEAT_AUTOCMD
12664 aucmd_restbuf(&aco);
12665#else
12666 curbuf = save_curbuf;
12667#endif
12668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669}
12670
12671/*
12672 * "setcmdpos()" function
12673 */
12674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012675f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012676 typval_T *argvars;
12677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012679 int pos = (int)get_tv_number(&argvars[0]) - 1;
12680
12681 if (pos >= 0)
12682 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683}
12684
12685/*
12686 * "setline()" function
12687 */
12688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012689f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012690 typval_T *argvars;
12691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692{
12693 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000012694 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012695 list_T *l = NULL;
12696 listitem_T *li = NULL;
12697 long added = 0;
12698 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012700 lnum = get_tv_lnum(&argvars[0]);
12701 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012703 l = argvars[1].vval.v_list;
12704 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012706 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012707 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012708
12709 rettv->vval.v_number = 0; /* OK */
12710 for (;;)
12711 {
12712 if (l != NULL)
12713 {
12714 /* list argument, get next string */
12715 if (li == NULL)
12716 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012717 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012718 li = li->li_next;
12719 }
12720
12721 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012722 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012723 break;
12724 if (lnum <= curbuf->b_ml.ml_line_count)
12725 {
12726 /* existing line, replace it */
12727 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
12728 {
12729 changed_bytes(lnum, 0);
12730 check_cursor_col();
12731 rettv->vval.v_number = 0; /* OK */
12732 }
12733 }
12734 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
12735 {
12736 /* lnum is one past the last line, append the line */
12737 ++added;
12738 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
12739 rettv->vval.v_number = 0; /* OK */
12740 }
12741
12742 if (l == NULL) /* only one string argument */
12743 break;
12744 ++lnum;
12745 }
12746
12747 if (added > 0)
12748 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749}
12750
12751/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000012752 * "setqflist()" function
12753 */
12754/*ARGSUSED*/
12755 static void
12756f_setqflist(argvars, rettv)
12757 typval_T *argvars;
12758 typval_T *rettv;
12759{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000012760 char_u *act;
12761 int action = ' ';
12762
Bram Moolenaar2641f772005-03-25 21:58:17 +000012763 rettv->vval.v_number = -1;
12764
12765#ifdef FEAT_QUICKFIX
12766 if (argvars[0].v_type != VAR_LIST)
12767 EMSG(_(e_listreq));
12768 else
12769 {
12770 list_T *l = argvars[0].vval.v_list;
12771
Bram Moolenaarf4630b62005-05-20 21:31:17 +000012772 if (argvars[1].v_type == VAR_STRING)
12773 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012774 act = get_tv_string_chk(&argvars[1]);
12775 if (act == NULL)
12776 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000012777 if (*act == 'a' || *act == 'r')
12778 action = *act;
12779 }
12780
12781 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012782 rettv->vval.v_number = 0;
12783 }
12784#endif
12785}
12786
12787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788 * "setreg()" function
12789 */
12790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012792 typval_T *argvars;
12793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794{
12795 int regname;
12796 char_u *strregname;
12797 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012798 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799 int append;
12800 char_u yank_type;
12801 long block_len;
12802
12803 block_len = -1;
12804 yank_type = MAUTO;
12805 append = FALSE;
12806
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012807 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012810 if (strregname == NULL)
12811 return; /* type error; errmsg already given */
12812 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813 if (regname == 0 || regname == '@')
12814 regname = '"';
12815 else if (regname == '=')
12816 return;
12817
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012818 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012820 stropt = get_tv_string_chk(&argvars[2]);
12821 if (stropt == NULL)
12822 return; /* type error */
12823 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824 switch (*stropt)
12825 {
12826 case 'a': case 'A': /* append */
12827 append = TRUE;
12828 break;
12829 case 'v': case 'c': /* character-wise selection */
12830 yank_type = MCHAR;
12831 break;
12832 case 'V': case 'l': /* line-wise selection */
12833 yank_type = MLINE;
12834 break;
12835#ifdef FEAT_VISUAL
12836 case 'b': case Ctrl_V: /* block-wise selection */
12837 yank_type = MBLOCK;
12838 if (VIM_ISDIGIT(stropt[1]))
12839 {
12840 ++stropt;
12841 block_len = getdigits(&stropt) - 1;
12842 --stropt;
12843 }
12844 break;
12845#endif
12846 }
12847 }
12848
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012849 strval = get_tv_string_chk(&argvars[1]);
12850 if (strval != NULL)
12851 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854}
12855
12856
12857/*
12858 * "setwinvar(expr)" function
12859 */
12860/*ARGSUSED*/
12861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012862f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012863 typval_T *argvars;
12864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865{
12866 win_T *win;
12867#ifdef FEAT_WINDOWS
12868 win_T *save_curwin;
12869#endif
12870 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012871 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872 char_u nbuf[NUMBUFLEN];
12873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012874 rettv->vval.v_number = 0;
12875
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 if (check_restricted() || check_secure())
12877 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012879 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 varp = &argvars[2];
12881
12882 if (win != NULL && varname != NULL && varp != NULL)
12883 {
12884#ifdef FEAT_WINDOWS
12885 /* set curwin to be our win, temporarily */
12886 save_curwin = curwin;
12887 curwin = win;
12888 curbuf = curwin->w_buffer;
12889#endif
12890
12891 if (*varname == '&')
12892 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012893 long numval;
12894 char_u *strval;
12895 int error = FALSE;
12896
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012898 numval = get_tv_number_chk(varp, &error);
12899 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012900 if (!error && strval != NULL)
12901 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 }
12903 else
12904 {
12905 winvarname = alloc((unsigned)STRLEN(varname) + 3);
12906 if (winvarname != NULL)
12907 {
12908 STRCPY(winvarname, "w:");
12909 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012910 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911 vim_free(winvarname);
12912 }
12913 }
12914
12915#ifdef FEAT_WINDOWS
12916 /* Restore current window, if it's still valid (autocomands can make
12917 * it invalid). */
12918 if (win_valid(save_curwin))
12919 {
12920 curwin = save_curwin;
12921 curbuf = curwin->w_buffer;
12922 }
12923#endif
12924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925}
12926
12927/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012928 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 */
12930 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012931f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012932 typval_T *argvars;
12933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012935 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936
Bram Moolenaar0d660222005-01-07 21:51:51 +000012937 p = get_tv_string(&argvars[0]);
12938 rettv->vval.v_string = vim_strsave(p);
12939 simplify_filename(rettv->vval.v_string); /* simplify in place */
12940 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941}
12942
Bram Moolenaar0d660222005-01-07 21:51:51 +000012943static int
12944#ifdef __BORLANDC__
12945 _RTLENTRYF
12946#endif
12947 item_compare __ARGS((const void *s1, const void *s2));
12948static int
12949#ifdef __BORLANDC__
12950 _RTLENTRYF
12951#endif
12952 item_compare2 __ARGS((const void *s1, const void *s2));
12953
12954static int item_compare_ic;
12955static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012956static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012957#define ITEM_COMPARE_FAIL 999
12958
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012960 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012962 static int
12963#ifdef __BORLANDC__
12964_RTLENTRYF
12965#endif
12966item_compare(s1, s2)
12967 const void *s1;
12968 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012970 char_u *p1, *p2;
12971 char_u *tofree1, *tofree2;
12972 int res;
12973 char_u numbuf1[NUMBUFLEN];
12974 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975
Bram Moolenaar33570922005-01-25 22:26:29 +000012976 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
12977 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012978 if (item_compare_ic)
12979 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012981 res = STRCMP(p1, p2);
12982 vim_free(tofree1);
12983 vim_free(tofree2);
12984 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985}
12986
12987 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000012988#ifdef __BORLANDC__
12989_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000012990#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000012991item_compare2(s1, s2)
12992 const void *s1;
12993 const void *s2;
12994{
12995 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000012996 typval_T rettv;
12997 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000012998 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013000 /* shortcut after failure in previous call; compare all items equal */
13001 if (item_compare_func_err)
13002 return 0;
13003
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013004 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13005 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013006 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13007 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013008
13009 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13010 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013011 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013012 clear_tv(&argv[0]);
13013 clear_tv(&argv[1]);
13014
13015 if (res == FAIL)
13016 res = ITEM_COMPARE_FAIL;
13017 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013018 /* return value has wrong type */
13019 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13020 if (item_compare_func_err)
13021 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013022 clear_tv(&rettv);
13023 return res;
13024}
13025
13026/*
13027 * "sort({list})" function
13028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013030f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013031 typval_T *argvars;
13032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033{
Bram Moolenaar33570922005-01-25 22:26:29 +000013034 list_T *l;
13035 listitem_T *li;
13036 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013037 long len;
13038 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039
Bram Moolenaar0d660222005-01-07 21:51:51 +000013040 rettv->vval.v_number = 0;
13041 if (argvars[0].v_type != VAR_LIST)
13042 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043 else
13044 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013045 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013046 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013047 return;
13048 rettv->vval.v_list = l;
13049 rettv->v_type = VAR_LIST;
13050 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051
Bram Moolenaar0d660222005-01-07 21:51:51 +000013052 len = list_len(l);
13053 if (len <= 1)
13054 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055
Bram Moolenaar0d660222005-01-07 21:51:51 +000013056 item_compare_ic = FALSE;
13057 item_compare_func = NULL;
13058 if (argvars[1].v_type != VAR_UNKNOWN)
13059 {
13060 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013061 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013062 else
13063 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013064 int error = FALSE;
13065
13066 i = get_tv_number_chk(&argvars[1], &error);
13067 if (error)
13068 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013069 if (i == 1)
13070 item_compare_ic = TRUE;
13071 else
13072 item_compare_func = get_tv_string(&argvars[1]);
13073 }
13074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075
Bram Moolenaar0d660222005-01-07 21:51:51 +000013076 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013077 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013078 if (ptrs == NULL)
13079 return;
13080 i = 0;
13081 for (li = l->lv_first; li != NULL; li = li->li_next)
13082 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013084 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013085 /* test the compare function */
13086 if (item_compare_func != NULL
13087 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13088 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013089 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013091 {
13092 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013093 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013094 item_compare_func == NULL ? item_compare : item_compare2);
13095
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013096 if (!item_compare_func_err)
13097 {
13098 /* Clear the List and append the items in the sorted order. */
13099 l->lv_first = l->lv_last = NULL;
13100 l->lv_len = 0;
13101 for (i = 0; i < len; ++i)
13102 list_append(l, ptrs[i]);
13103 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013104 }
13105
13106 vim_free(ptrs);
13107 }
13108}
13109
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013110/*
13111 * "spellbadword()" function
13112 */
13113/* ARGSUSED */
13114 static void
13115f_spellbadword(argvars, rettv)
13116 typval_T *argvars;
13117 typval_T *rettv;
13118{
13119 int attr;
13120 char_u *ptr;
13121 int len;
13122
13123 rettv->vval.v_string = NULL;
13124 rettv->v_type = VAR_STRING;
13125
13126#ifdef FEAT_SYN_HL
13127 /* Find the start of the badly spelled word. */
13128 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
13129 return;
13130
13131 /* Get the length of the word and copy it. */
13132 ptr = ml_get_cursor();
13133 len = spell_check(curwin, ptr, &attr);
13134 rettv->vval.v_string = vim_strnsave(ptr, len);
13135#endif
13136}
13137
13138/*
13139 * "spellsuggest()" function
13140 */
13141 static void
13142f_spellsuggest(argvars, rettv)
13143 typval_T *argvars;
13144 typval_T *rettv;
13145{
13146 char_u *str;
13147 int maxcount;
13148 garray_T ga;
13149 list_T *l;
13150 listitem_T *li;
13151 int i;
13152
13153 l = list_alloc();
13154 if (l == NULL)
13155 return;
13156 rettv->v_type = VAR_LIST;
13157 rettv->vval.v_list = l;
13158 ++l->lv_refcount;
13159
13160#ifdef FEAT_SYN_HL
13161 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13162 {
13163 str = get_tv_string(&argvars[0]);
13164 if (argvars[1].v_type != VAR_UNKNOWN)
13165 {
13166 maxcount = get_tv_number(&argvars[1]);
13167 if (maxcount <= 0)
13168 return;
13169 }
13170 else
13171 maxcount = 25;
13172
13173 spell_suggest_list(&ga, str, maxcount);
13174
13175 for (i = 0; i < ga.ga_len; ++i)
13176 {
13177 str = ((char_u **)ga.ga_data)[i];
13178
13179 li = listitem_alloc();
13180 if (li == NULL)
13181 vim_free(str);
13182 else
13183 {
13184 li->li_tv.v_type = VAR_STRING;
13185 li->li_tv.v_lock = 0;
13186 li->li_tv.vval.v_string = str;
13187 list_append(l, li);
13188 }
13189 }
13190 ga_clear(&ga);
13191 }
13192#endif
13193}
13194
Bram Moolenaar0d660222005-01-07 21:51:51 +000013195 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013196f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013197 typval_T *argvars;
13198 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013199{
13200 char_u *str;
13201 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013202 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013203 regmatch_T regmatch;
13204 char_u patbuf[NUMBUFLEN];
13205 char_u *save_cpo;
13206 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013207 listitem_T *ni;
13208 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013209 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013210 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013211 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013212
13213 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13214 save_cpo = p_cpo;
13215 p_cpo = (char_u *)"";
13216
13217 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013218 if (argvars[1].v_type != VAR_UNKNOWN)
13219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013220 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13221 if (pat == NULL)
13222 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013223 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013224 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013225 }
13226 if (pat == NULL || *pat == NUL)
13227 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013228
13229 l = list_alloc();
13230 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013232 rettv->v_type = VAR_LIST;
13233 rettv->vval.v_list = l;
13234 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013235 if (typeerr)
13236 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237
Bram Moolenaar0d660222005-01-07 21:51:51 +000013238 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13239 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013241 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013242 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013243 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013244 if (*str == NUL)
13245 match = FALSE; /* empty item at the end */
13246 else
13247 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013248 if (match)
13249 end = regmatch.startp[0];
13250 else
13251 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013252 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13253 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013254 {
13255 ni = listitem_alloc();
13256 if (ni == NULL)
13257 break;
13258 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013259 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013260 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13261 list_append(l, ni);
13262 }
13263 if (!match)
13264 break;
13265 /* Advance to just after the match. */
13266 if (regmatch.endp[0] > str)
13267 col = 0;
13268 else
13269 {
13270 /* Don't get stuck at the same match. */
13271#ifdef FEAT_MBYTE
13272 col = mb_ptr2len_check(regmatch.endp[0]);
13273#else
13274 col = 1;
13275#endif
13276 }
13277 str = regmatch.endp[0];
13278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279
Bram Moolenaar0d660222005-01-07 21:51:51 +000013280 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282
Bram Moolenaar0d660222005-01-07 21:51:51 +000013283 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284}
13285
13286#ifdef HAVE_STRFTIME
13287/*
13288 * "strftime({format}[, {time}])" function
13289 */
13290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013291f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013292 typval_T *argvars;
13293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294{
13295 char_u result_buf[256];
13296 struct tm *curtime;
13297 time_t seconds;
13298 char_u *p;
13299
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013300 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013302 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013303 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 seconds = time(NULL);
13305 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013306 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307 curtime = localtime(&seconds);
13308 /* MSVC returns NULL for an invalid value of seconds. */
13309 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013310 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311 else
13312 {
13313# ifdef FEAT_MBYTE
13314 vimconv_T conv;
13315 char_u *enc;
13316
13317 conv.vc_type = CONV_NONE;
13318 enc = enc_locale();
13319 convert_setup(&conv, p_enc, enc);
13320 if (conv.vc_type != CONV_NONE)
13321 p = string_convert(&conv, p, NULL);
13322# endif
13323 if (p != NULL)
13324 (void)strftime((char *)result_buf, sizeof(result_buf),
13325 (char *)p, curtime);
13326 else
13327 result_buf[0] = NUL;
13328
13329# ifdef FEAT_MBYTE
13330 if (conv.vc_type != CONV_NONE)
13331 vim_free(p);
13332 convert_setup(&conv, enc, p_enc);
13333 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 else
13336# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013337 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338
13339# ifdef FEAT_MBYTE
13340 /* Release conversion descriptors */
13341 convert_setup(&conv, NULL, NULL);
13342 vim_free(enc);
13343# endif
13344 }
13345}
13346#endif
13347
13348/*
13349 * "stridx()" function
13350 */
13351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013352f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013353 typval_T *argvars;
13354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355{
13356 char_u buf[NUMBUFLEN];
13357 char_u *needle;
13358 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000013359 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000013361 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013363 needle = get_tv_string_chk(&argvars[1]);
13364 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000013365 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013366 if (needle == NULL || haystack == NULL)
13367 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368
Bram Moolenaar33570922005-01-25 22:26:29 +000013369 if (argvars[2].v_type != VAR_UNKNOWN)
13370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013371 int error = FALSE;
13372
13373 start_idx = get_tv_number_chk(&argvars[2], &error);
13374 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000013375 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013376 if (start_idx >= 0)
13377 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000013378 }
13379
13380 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13381 if (pos != NULL)
13382 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383}
13384
13385/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013386 * "string()" function
13387 */
13388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013389f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013390 typval_T *argvars;
13391 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013392{
13393 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013394 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013396 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013397 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013398 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013399 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400}
13401
13402/*
13403 * "strlen()" function
13404 */
13405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013406f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013407 typval_T *argvars;
13408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013410 rettv->vval.v_number = (varnumber_T)(STRLEN(
13411 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412}
13413
13414/*
13415 * "strpart()" function
13416 */
13417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013418f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013419 typval_T *argvars;
13420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421{
13422 char_u *p;
13423 int n;
13424 int len;
13425 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013426 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013428 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429 slen = (int)STRLEN(p);
13430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013431 n = get_tv_number_chk(&argvars[1], &error);
13432 if (error)
13433 len = 0;
13434 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013435 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436 else
13437 len = slen - n; /* default len: all bytes that are available. */
13438
13439 /*
13440 * Only return the overlap between the specified part and the actual
13441 * string.
13442 */
13443 if (n < 0)
13444 {
13445 len += n;
13446 n = 0;
13447 }
13448 else if (n > slen)
13449 n = slen;
13450 if (len < 0)
13451 len = 0;
13452 else if (n + len > slen)
13453 len = slen - n;
13454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013455 rettv->v_type = VAR_STRING;
13456 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457}
13458
13459/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013460 * "strridx()" function
13461 */
13462 static void
13463f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013464 typval_T *argvars;
13465 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013466{
13467 char_u buf[NUMBUFLEN];
13468 char_u *needle;
13469 char_u *haystack;
13470 char_u *rest;
13471 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013472 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013474 needle = get_tv_string_chk(&argvars[1]);
13475 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013476 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013477
13478 rettv->vval.v_number = -1;
13479 if (needle == NULL || haystack == NULL)
13480 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013481 if (argvars[2].v_type != VAR_UNKNOWN)
13482 {
13483 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013484 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000013485 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013486 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013487 }
13488 else
13489 end_idx = haystack_len;
13490
Bram Moolenaar0d660222005-01-07 21:51:51 +000013491 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000013492 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013493 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013494 lastmatch = haystack + end_idx;
13495 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013496 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000013497 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013498 for (rest = haystack; *rest != '\0'; ++rest)
13499 {
13500 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013501 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013502 break;
13503 lastmatch = rest;
13504 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000013505 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013506
13507 if (lastmatch == NULL)
13508 rettv->vval.v_number = -1;
13509 else
13510 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13511}
13512
13513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514 * "strtrans()" function
13515 */
13516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013517f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013518 typval_T *argvars;
13519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013521 rettv->v_type = VAR_STRING;
13522 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523}
13524
13525/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013526 * "submatch()" function
13527 */
13528 static void
13529f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013530 typval_T *argvars;
13531 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013532{
13533 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013534 rettv->vval.v_string =
13535 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013536}
13537
13538/*
13539 * "substitute()" function
13540 */
13541 static void
13542f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013543 typval_T *argvars;
13544 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013545{
13546 char_u patbuf[NUMBUFLEN];
13547 char_u subbuf[NUMBUFLEN];
13548 char_u flagsbuf[NUMBUFLEN];
13549
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013550 char_u *str = get_tv_string_chk(&argvars[0]);
13551 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13552 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
13553 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
13554
Bram Moolenaar0d660222005-01-07 21:51:51 +000013555 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013556 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
13557 rettv->vval.v_string = NULL;
13558 else
13559 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013560}
13561
13562/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013563 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 */
13565/*ARGSUSED*/
13566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013567f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013568 typval_T *argvars;
13569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570{
13571 int id = 0;
13572#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013573 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574 long col;
13575 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000013576 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013578 lnum = get_tv_lnum(argvars); /* -1 on type error */
13579 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
13580 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013582 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013583 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13584 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585#endif
13586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013587 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588}
13589
13590/*
13591 * "synIDattr(id, what [, mode])" function
13592 */
13593/*ARGSUSED*/
13594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013595f_synIDattr(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 char_u *p = NULL;
13600#ifdef FEAT_SYN_HL
13601 int id;
13602 char_u *what;
13603 char_u *mode;
13604 char_u modebuf[NUMBUFLEN];
13605 int modec;
13606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607 id = get_tv_number(&argvars[0]);
13608 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013609 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013611 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 modec = TOLOWER_ASC(mode[0]);
13613 if (modec != 't' && modec != 'c'
13614#ifdef FEAT_GUI
13615 && modec != 'g'
13616#endif
13617 )
13618 modec = 0; /* replace invalid with current */
13619 }
13620 else
13621 {
13622#ifdef FEAT_GUI
13623 if (gui.in_use)
13624 modec = 'g';
13625 else
13626#endif
13627 if (t_colors > 1)
13628 modec = 'c';
13629 else
13630 modec = 't';
13631 }
13632
13633
13634 switch (TOLOWER_ASC(what[0]))
13635 {
13636 case 'b':
13637 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13638 p = highlight_color(id, what, modec);
13639 else /* bold */
13640 p = highlight_has_attr(id, HL_BOLD, modec);
13641 break;
13642
13643 case 'f': /* fg[#] */
13644 p = highlight_color(id, what, modec);
13645 break;
13646
13647 case 'i':
13648 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13649 p = highlight_has_attr(id, HL_INVERSE, modec);
13650 else /* italic */
13651 p = highlight_has_attr(id, HL_ITALIC, modec);
13652 break;
13653
13654 case 'n': /* name */
13655 p = get_highlight_name(NULL, id - 1);
13656 break;
13657
13658 case 'r': /* reverse */
13659 p = highlight_has_attr(id, HL_INVERSE, modec);
13660 break;
13661
13662 case 's': /* standout */
13663 p = highlight_has_attr(id, HL_STANDOUT, modec);
13664 break;
13665
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000013666 case 'u':
13667 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13668 /* underline */
13669 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13670 else
13671 /* undercurl */
13672 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673 break;
13674 }
13675
13676 if (p != NULL)
13677 p = vim_strsave(p);
13678#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013679 rettv->v_type = VAR_STRING;
13680 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681}
13682
13683/*
13684 * "synIDtrans(id)" function
13685 */
13686/*ARGSUSED*/
13687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013688f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013689 typval_T *argvars;
13690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691{
13692 int id;
13693
13694#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013695 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696
13697 if (id > 0)
13698 id = syn_get_final_id(id);
13699 else
13700#endif
13701 id = 0;
13702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013703 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704}
13705
13706/*
13707 * "system()" function
13708 */
13709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013710f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013711 typval_T *argvars;
13712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013714 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013716 char_u *infile = NULL;
13717 char_u buf[NUMBUFLEN];
13718 int err = FALSE;
13719 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013721 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013722 {
13723 /*
13724 * Write the string to a temp file, to be used for input of the shell
13725 * command.
13726 */
13727 if ((infile = vim_tempname('i')) == NULL)
13728 {
13729 EMSG(_(e_notmp));
13730 return;
13731 }
13732
13733 fd = mch_fopen((char *)infile, WRITEBIN);
13734 if (fd == NULL)
13735 {
13736 EMSG2(_(e_notopen), infile);
13737 goto done;
13738 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013739 p = get_tv_string_buf_chk(&argvars[1], buf);
13740 if (p == NULL)
13741 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013742 if (fwrite(p, STRLEN(p), 1, fd) != 1)
13743 err = TRUE;
13744 if (fclose(fd) != 0)
13745 err = TRUE;
13746 if (err)
13747 {
13748 EMSG(_("E677: Error writing temp file"));
13749 goto done;
13750 }
13751 }
13752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013753 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013754
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755#ifdef USE_CR
13756 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013757 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 {
13759 char_u *s;
13760
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013761 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762 {
13763 if (*s == CAR)
13764 *s = NL;
13765 }
13766 }
13767#else
13768# ifdef USE_CRNL
13769 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013770 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771 {
13772 char_u *s, *d;
13773
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013774 d = res;
13775 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 {
13777 if (s[0] == CAR && s[1] == NL)
13778 ++s;
13779 *d++ = *s;
13780 }
13781 *d = NUL;
13782 }
13783# endif
13784#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013785
13786done:
13787 if (infile != NULL)
13788 {
13789 mch_remove(infile);
13790 vim_free(infile);
13791 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013792 rettv->v_type = VAR_STRING;
13793 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794}
13795
13796/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000013797 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000013798 */
13799 static void
13800f_taglist(argvars, rettv)
13801 typval_T *argvars;
13802 typval_T *rettv;
13803{
13804 char_u *tag_pattern;
13805 list_T *l;
13806
13807 tag_pattern = get_tv_string(&argvars[0]);
13808
13809 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013810 if (*tag_pattern == NUL)
13811 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000013812
13813 l = list_alloc();
13814 if (l != NULL)
13815 {
13816 if (get_tags(l, tag_pattern) != FAIL)
13817 {
13818 rettv->vval.v_list = l;
13819 rettv->v_type = VAR_LIST;
13820 ++l->lv_refcount;
13821 }
13822 else
13823 list_free(l);
13824 }
13825}
13826
13827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828 * "tempname()" function
13829 */
13830/*ARGSUSED*/
13831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013832f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013833 typval_T *argvars;
13834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835{
13836 static int x = 'A';
13837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013838 rettv->v_type = VAR_STRING;
13839 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840
13841 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13842 * names. Skip 'I' and 'O', they are used for shell redirection. */
13843 do
13844 {
13845 if (x == 'Z')
13846 x = '0';
13847 else if (x == '9')
13848 x = 'A';
13849 else
13850 {
13851#ifdef EBCDIC
13852 if (x == 'I')
13853 x = 'J';
13854 else if (x == 'R')
13855 x = 'S';
13856 else
13857#endif
13858 ++x;
13859 }
13860 } while (x == 'I' || x == 'O');
13861}
13862
13863/*
13864 * "tolower(string)" function
13865 */
13866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013867f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013868 typval_T *argvars;
13869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870{
13871 char_u *p;
13872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013873 p = vim_strsave(get_tv_string(&argvars[0]));
13874 rettv->v_type = VAR_STRING;
13875 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876
13877 if (p != NULL)
13878 while (*p != NUL)
13879 {
13880#ifdef FEAT_MBYTE
13881 int l;
13882
13883 if (enc_utf8)
13884 {
13885 int c, lc;
13886
13887 c = utf_ptr2char(p);
13888 lc = utf_tolower(c);
13889 l = utf_ptr2len_check(p);
13890 /* TODO: reallocate string when byte count changes. */
13891 if (utf_char2len(lc) == l)
13892 utf_char2bytes(lc, p);
13893 p += l;
13894 }
13895 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13896 p += l; /* skip multi-byte character */
13897 else
13898#endif
13899 {
13900 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
13901 ++p;
13902 }
13903 }
13904}
13905
13906/*
13907 * "toupper(string)" function
13908 */
13909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013910f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013911 typval_T *argvars;
13912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013914 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013915 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916}
13917
13918/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000013919 * "tr(string, fromstr, tostr)" function
13920 */
13921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013922f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013923 typval_T *argvars;
13924 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013925{
13926 char_u *instr;
13927 char_u *fromstr;
13928 char_u *tostr;
13929 char_u *p;
13930#ifdef FEAT_MBYTE
13931 int inlen;
13932 int fromlen;
13933 int tolen;
13934 int idx;
13935 char_u *cpstr;
13936 int cplen;
13937 int first = TRUE;
13938#endif
13939 char_u buf[NUMBUFLEN];
13940 char_u buf2[NUMBUFLEN];
13941 garray_T ga;
13942
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013943 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013944 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13945 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013946
13947 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013948 rettv->v_type = VAR_STRING;
13949 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013950 if (fromstr == NULL || tostr == NULL)
13951 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000013952 ga_init2(&ga, (int)sizeof(char), 80);
13953
13954#ifdef FEAT_MBYTE
13955 if (!has_mbyte)
13956#endif
13957 /* not multi-byte: fromstr and tostr must be the same length */
13958 if (STRLEN(fromstr) != STRLEN(tostr))
13959 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013960#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000013961error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013962#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000013963 EMSG2(_(e_invarg2), fromstr);
13964 ga_clear(&ga);
13965 return;
13966 }
13967
13968 /* fromstr and tostr have to contain the same number of chars */
13969 while (*instr != NUL)
13970 {
13971#ifdef FEAT_MBYTE
13972 if (has_mbyte)
13973 {
13974 inlen = mb_ptr2len_check(instr);
13975 cpstr = instr;
13976 cplen = inlen;
13977 idx = 0;
13978 for (p = fromstr; *p != NUL; p += fromlen)
13979 {
13980 fromlen = mb_ptr2len_check(p);
13981 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
13982 {
13983 for (p = tostr; *p != NUL; p += tolen)
13984 {
13985 tolen = mb_ptr2len_check(p);
13986 if (idx-- == 0)
13987 {
13988 cplen = tolen;
13989 cpstr = p;
13990 break;
13991 }
13992 }
13993 if (*p == NUL) /* tostr is shorter than fromstr */
13994 goto error;
13995 break;
13996 }
13997 ++idx;
13998 }
13999
14000 if (first && cpstr == instr)
14001 {
14002 /* Check that fromstr and tostr have the same number of
14003 * (multi-byte) characters. Done only once when a character
14004 * of instr doesn't appear in fromstr. */
14005 first = FALSE;
14006 for (p = tostr; *p != NUL; p += tolen)
14007 {
14008 tolen = mb_ptr2len_check(p);
14009 --idx;
14010 }
14011 if (idx != 0)
14012 goto error;
14013 }
14014
14015 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014016 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014017 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014018
14019 instr += inlen;
14020 }
14021 else
14022#endif
14023 {
14024 /* When not using multi-byte chars we can do it faster. */
14025 p = vim_strchr(fromstr, *instr);
14026 if (p != NULL)
14027 ga_append(&ga, tostr[p - fromstr]);
14028 else
14029 ga_append(&ga, *instr);
14030 ++instr;
14031 }
14032 }
14033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014034 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014035}
14036
14037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038 * "type(expr)" function
14039 */
14040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014041f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014042 typval_T *argvars;
14043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014045 int n;
14046
14047 switch (argvars[0].v_type)
14048 {
14049 case VAR_NUMBER: n = 0; break;
14050 case VAR_STRING: n = 1; break;
14051 case VAR_FUNC: n = 2; break;
14052 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014053 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014054 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14055 }
14056 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057}
14058
14059/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014060 * "values(dict)" function
14061 */
14062 static void
14063f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014064 typval_T *argvars;
14065 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014066{
14067 dict_list(argvars, rettv, 1);
14068}
14069
14070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071 * "virtcol(string)" function
14072 */
14073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014074f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014075 typval_T *argvars;
14076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077{
14078 colnr_T vcol = 0;
14079 pos_T *fp;
14080
14081 fp = var2fpos(&argvars[0], FALSE);
14082 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14083 {
14084 getvvcol(curwin, fp, NULL, NULL, &vcol);
14085 ++vcol;
14086 }
14087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014088 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089}
14090
14091/*
14092 * "visualmode()" function
14093 */
14094/*ARGSUSED*/
14095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014096f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014097 typval_T *argvars;
14098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099{
14100#ifdef FEAT_VISUAL
14101 char_u str[2];
14102
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014103 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104 str[0] = curbuf->b_visual_mode_eval;
14105 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014106 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107
14108 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014109 if ((argvars[0].v_type == VAR_NUMBER
14110 && argvars[0].vval.v_number != 0)
14111 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014112 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113 curbuf->b_visual_mode_eval = NUL;
14114#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014115 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116#endif
14117}
14118
14119/*
14120 * "winbufnr(nr)" function
14121 */
14122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014123f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014124 typval_T *argvars;
14125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126{
14127 win_T *wp;
14128
14129 wp = find_win_by_nr(&argvars[0]);
14130 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014131 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014133 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134}
14135
14136/*
14137 * "wincol()" function
14138 */
14139/*ARGSUSED*/
14140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014141f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014142 typval_T *argvars;
14143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144{
14145 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014146 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147}
14148
14149/*
14150 * "winheight(nr)" function
14151 */
14152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014153f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014154 typval_T *argvars;
14155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156{
14157 win_T *wp;
14158
14159 wp = find_win_by_nr(&argvars[0]);
14160 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014161 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014163 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164}
14165
14166/*
14167 * "winline()" function
14168 */
14169/*ARGSUSED*/
14170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014171f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014172 typval_T *argvars;
14173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174{
14175 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014176 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177}
14178
14179/*
14180 * "winnr()" function
14181 */
14182/* ARGSUSED */
14183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014184f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014185 typval_T *argvars;
14186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187{
14188 int nr = 1;
14189#ifdef FEAT_WINDOWS
14190 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014191 win_T *twin = curwin;
14192 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014194 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014196 arg = get_tv_string_chk(&argvars[0]);
14197 if (arg == NULL)
14198 nr = 0; /* type error; errmsg already given */
14199 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014200 twin = lastwin;
14201 else if (STRCMP(arg, "#") == 0)
14202 {
14203 twin = prevwin;
14204 if (prevwin == NULL)
14205 nr = 0;
14206 }
14207 else
14208 {
14209 EMSG2(_(e_invexpr2), arg);
14210 nr = 0;
14211 }
14212 }
14213
14214 if (nr > 0)
14215 for (wp = firstwin; wp != twin; wp = wp->w_next)
14216 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014218 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219}
14220
14221/*
14222 * "winrestcmd()" function
14223 */
14224/* ARGSUSED */
14225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014226f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014227 typval_T *argvars;
14228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229{
14230#ifdef FEAT_WINDOWS
14231 win_T *wp;
14232 int winnr = 1;
14233 garray_T ga;
14234 char_u buf[50];
14235
14236 ga_init2(&ga, (int)sizeof(char), 70);
14237 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14238 {
14239 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14240 ga_concat(&ga, buf);
14241# ifdef FEAT_VERTSPLIT
14242 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14243 ga_concat(&ga, buf);
14244# endif
14245 ++winnr;
14246 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014247 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014249 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014251 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014253 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254}
14255
14256/*
14257 * "winwidth(nr)" function
14258 */
14259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014260f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014261 typval_T *argvars;
14262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263{
14264 win_T *wp;
14265
14266 wp = find_win_by_nr(&argvars[0]);
14267 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014268 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269 else
14270#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014271 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014273 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274#endif
14275}
14276
14277 static win_T *
14278find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014279 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280{
14281#ifdef FEAT_WINDOWS
14282 win_T *wp;
14283#endif
14284 int nr;
14285
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014286 nr = get_tv_number_chk(vp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287
14288#ifdef FEAT_WINDOWS
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014289 if (nr < 0)
14290 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 if (nr == 0)
14292 return curwin;
14293
14294 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14295 if (--nr <= 0)
14296 break;
14297 return wp;
14298#else
14299 if (nr == 0 || nr == 1)
14300 return curwin;
14301 return NULL;
14302#endif
14303}
14304
14305/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014306 * "writefile()" function
14307 */
14308 static void
14309f_writefile(argvars, rettv)
14310 typval_T *argvars;
14311 typval_T *rettv;
14312{
14313 int binary = FALSE;
14314 char_u *fname;
14315 FILE *fd;
14316 listitem_T *li;
14317 char_u *s;
14318 int ret = 0;
14319 int c;
14320
14321 if (argvars[0].v_type != VAR_LIST)
14322 {
14323 EMSG2(_(e_listarg), "writefile()");
14324 return;
14325 }
14326 if (argvars[0].vval.v_list == NULL)
14327 return;
14328
14329 if (argvars[2].v_type != VAR_UNKNOWN
14330 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
14331 binary = TRUE;
14332
14333 /* Always open the file in binary mode, library functions have a mind of
14334 * their own about CR-LF conversion. */
14335 fname = get_tv_string(&argvars[1]);
14336 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
14337 {
14338 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14339 ret = -1;
14340 }
14341 else
14342 {
14343 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
14344 li = li->li_next)
14345 {
14346 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
14347 {
14348 if (*s == '\n')
14349 c = putc(NUL, fd);
14350 else
14351 c = putc(*s, fd);
14352 if (c == EOF)
14353 {
14354 ret = -1;
14355 break;
14356 }
14357 }
14358 if (!binary || li->li_next != NULL)
14359 if (putc('\n', fd) == EOF)
14360 {
14361 ret = -1;
14362 break;
14363 }
14364 if (ret < 0)
14365 {
14366 EMSG(_(e_write));
14367 break;
14368 }
14369 }
14370 fclose(fd);
14371 }
14372
14373 rettv->vval.v_number = ret;
14374}
14375
14376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 * Translate a String variable into a position.
14378 */
14379 static pos_T *
14380var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000014381 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382 int lnum; /* TRUE when $ is last line */
14383{
14384 char_u *name;
14385 static pos_T pos;
14386 pos_T *pp;
14387
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014388 name = get_tv_string_chk(varp);
14389 if (name == NULL)
14390 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 if (name[0] == '.') /* cursor */
14392 return &curwin->w_cursor;
14393 if (name[0] == '\'') /* mark */
14394 {
14395 pp = getmark(name[1], FALSE);
14396 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
14397 return NULL;
14398 return pp;
14399 }
14400 if (name[0] == '$') /* last column or line */
14401 {
14402 if (lnum)
14403 {
14404 pos.lnum = curbuf->b_ml.ml_line_count;
14405 pos.col = 0;
14406 }
14407 else
14408 {
14409 pos.lnum = curwin->w_cursor.lnum;
14410 pos.col = (colnr_T)STRLEN(ml_get_curline());
14411 }
14412 return &pos;
14413 }
14414 return NULL;
14415}
14416
14417/*
14418 * Get the length of an environment variable name.
14419 * Advance "arg" to the first character after the name.
14420 * Return 0 for error.
14421 */
14422 static int
14423get_env_len(arg)
14424 char_u **arg;
14425{
14426 char_u *p;
14427 int len;
14428
14429 for (p = *arg; vim_isIDc(*p); ++p)
14430 ;
14431 if (p == *arg) /* no name found */
14432 return 0;
14433
14434 len = (int)(p - *arg);
14435 *arg = p;
14436 return len;
14437}
14438
14439/*
14440 * Get the length of the name of a function or internal variable.
14441 * "arg" is advanced to the first non-white character after the name.
14442 * Return 0 if something is wrong.
14443 */
14444 static int
14445get_id_len(arg)
14446 char_u **arg;
14447{
14448 char_u *p;
14449 int len;
14450
14451 /* Find the end of the name. */
14452 for (p = *arg; eval_isnamec(*p); ++p)
14453 ;
14454 if (p == *arg) /* no name found */
14455 return 0;
14456
14457 len = (int)(p - *arg);
14458 *arg = skipwhite(p);
14459
14460 return len;
14461}
14462
14463/*
Bram Moolenaara7043832005-01-21 11:56:39 +000014464 * Get the length of the name of a variable or function.
14465 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014467 * Return -1 if curly braces expansion failed.
14468 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469 * If the name contains 'magic' {}'s, expand them and return the
14470 * expanded name in an allocated string via 'alias' - caller must free.
14471 */
14472 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014473get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474 char_u **arg;
14475 char_u **alias;
14476 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014477 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478{
14479 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 char_u *p;
14481 char_u *expr_start;
14482 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483
14484 *alias = NULL; /* default to no alias */
14485
14486 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
14487 && (*arg)[2] == (int)KE_SNR)
14488 {
14489 /* hard coded <SNR>, already translated */
14490 *arg += 3;
14491 return get_id_len(arg) + 3;
14492 }
14493 len = eval_fname_script(*arg);
14494 if (len > 0)
14495 {
14496 /* literal "<SID>", "s:" or "<SNR>" */
14497 *arg += len;
14498 }
14499
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014501 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014503 p = find_name_end(*arg, &expr_start, &expr_end,
14504 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505 if (expr_start != NULL)
14506 {
14507 char_u *temp_string;
14508
14509 if (!evaluate)
14510 {
14511 len += (int)(p - *arg);
14512 *arg = skipwhite(p);
14513 return len;
14514 }
14515
14516 /*
14517 * Include any <SID> etc in the expanded string:
14518 * Thus the -len here.
14519 */
14520 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
14521 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014522 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523 *alias = temp_string;
14524 *arg = skipwhite(p);
14525 return (int)STRLEN(temp_string);
14526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527
14528 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014529 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530 EMSG2(_(e_invexpr2), *arg);
14531
14532 return len;
14533}
14534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014535/*
14536 * Find the end of a variable or function name, taking care of magic braces.
14537 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
14538 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014539 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014540 * Return a pointer to just after the name. Equal to "arg" if there is no
14541 * valid name.
14542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014544find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 char_u *arg;
14546 char_u **expr_start;
14547 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014548 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014550 int mb_nest = 0;
14551 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 char_u *p;
14553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014554 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014556 *expr_start = NULL;
14557 *expr_end = NULL;
14558 }
14559
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014560 /* Quick check for valid starting character. */
14561 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
14562 return arg;
14563
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014564 for (p = arg; *p != NUL
14565 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014566 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014567 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014568 || mb_nest != 0
14569 || br_nest != 0); ++p)
14570 {
14571 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014572 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014573 if (*p == '[')
14574 ++br_nest;
14575 else if (*p == ']')
14576 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014578 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014580 if (*p == '{')
14581 {
14582 mb_nest++;
14583 if (expr_start != NULL && *expr_start == NULL)
14584 *expr_start = p;
14585 }
14586 else if (*p == '}')
14587 {
14588 mb_nest--;
14589 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
14590 *expr_end = p;
14591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593 }
14594
14595 return p;
14596}
14597
14598/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014599 * Expands out the 'magic' {}'s in a variable/function name.
14600 * Note that this can call itself recursively, to deal with
14601 * constructs like foo{bar}{baz}{bam}
14602 * The four pointer arguments point to "foo{expre}ss{ion}bar"
14603 * "in_start" ^
14604 * "expr_start" ^
14605 * "expr_end" ^
14606 * "in_end" ^
14607 *
14608 * Returns a new allocated string, which the caller must free.
14609 * Returns NULL for failure.
14610 */
14611 static char_u *
14612make_expanded_name(in_start, expr_start, expr_end, in_end)
14613 char_u *in_start;
14614 char_u *expr_start;
14615 char_u *expr_end;
14616 char_u *in_end;
14617{
14618 char_u c1;
14619 char_u *retval = NULL;
14620 char_u *temp_result;
14621 char_u *nextcmd = NULL;
14622
14623 if (expr_end == NULL || in_end == NULL)
14624 return NULL;
14625 *expr_start = NUL;
14626 *expr_end = NUL;
14627 c1 = *in_end;
14628 *in_end = NUL;
14629
14630 temp_result = eval_to_string(expr_start + 1, &nextcmd);
14631 if (temp_result != NULL && nextcmd == NULL)
14632 {
14633 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
14634 + (in_end - expr_end) + 1));
14635 if (retval != NULL)
14636 {
14637 STRCPY(retval, in_start);
14638 STRCAT(retval, temp_result);
14639 STRCAT(retval, expr_end + 1);
14640 }
14641 }
14642 vim_free(temp_result);
14643
14644 *in_end = c1; /* put char back for error messages */
14645 *expr_start = '{';
14646 *expr_end = '}';
14647
14648 if (retval != NULL)
14649 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014650 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014651 if (expr_start != NULL)
14652 {
14653 /* Further expansion! */
14654 temp_result = make_expanded_name(retval, expr_start,
14655 expr_end, temp_result);
14656 vim_free(retval);
14657 retval = temp_result;
14658 }
14659 }
14660
14661 return retval;
14662}
14663
14664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014666 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667 */
14668 static int
14669eval_isnamec(c)
14670 int c;
14671{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014672 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
14673}
14674
14675/*
14676 * Return TRUE if character "c" can be used as the first character in a
14677 * variable or function name (excluding '{' and '}').
14678 */
14679 static int
14680eval_isnamec1(c)
14681 int c;
14682{
14683 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684}
14685
14686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687 * Set number v: variable to "val".
14688 */
14689 void
14690set_vim_var_nr(idx, val)
14691 int idx;
14692 long val;
14693{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014694 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695}
14696
14697/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014698 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699 */
14700 long
14701get_vim_var_nr(idx)
14702 int idx;
14703{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014704 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014705}
14706
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014707#if defined(FEAT_AUTOCMD) || defined(PROTO)
14708/*
14709 * Get string v: variable value. Uses a static buffer, can only be used once.
14710 */
14711 char_u *
14712get_vim_var_str(idx)
14713 int idx;
14714{
14715 return get_tv_string(&vimvars[idx].vv_tv);
14716}
14717#endif
14718
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719/*
14720 * Set v:count, v:count1 and v:prevcount.
14721 */
14722 void
14723set_vcount(count, count1)
14724 long count;
14725 long count1;
14726{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014727 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
14728 vimvars[VV_COUNT].vv_nr = count;
14729 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730}
14731
14732/*
14733 * Set string v: variable to a copy of "val".
14734 */
14735 void
14736set_vim_var_string(idx, val, len)
14737 int idx;
14738 char_u *val;
14739 int len; /* length of "val" to use or -1 (whole string) */
14740{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014741 /* Need to do this (at least) once, since we can't initialize a union.
14742 * Will always be invoked when "v:progname" is set. */
14743 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
14744
Bram Moolenaare9a41262005-01-15 22:18:47 +000014745 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014747 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014749 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000014751 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752}
14753
14754/*
14755 * Set v:register if needed.
14756 */
14757 void
14758set_reg_var(c)
14759 int c;
14760{
14761 char_u regname;
14762
14763 if (c == 0 || c == ' ')
14764 regname = '"';
14765 else
14766 regname = c;
14767 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014768 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769 set_vim_var_string(VV_REG, &regname, 1);
14770}
14771
14772/*
14773 * Get or set v:exception. If "oldval" == NULL, return the current value.
14774 * Otherwise, restore the value to "oldval" and return NULL.
14775 * Must always be called in pairs to save and restore v:exception! Does not
14776 * take care of memory allocations.
14777 */
14778 char_u *
14779v_exception(oldval)
14780 char_u *oldval;
14781{
14782 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014783 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784
Bram Moolenaare9a41262005-01-15 22:18:47 +000014785 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014786 return NULL;
14787}
14788
14789/*
14790 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
14791 * Otherwise, restore the value to "oldval" and return NULL.
14792 * Must always be called in pairs to save and restore v:throwpoint! Does not
14793 * take care of memory allocations.
14794 */
14795 char_u *
14796v_throwpoint(oldval)
14797 char_u *oldval;
14798{
14799 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014800 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014801
Bram Moolenaare9a41262005-01-15 22:18:47 +000014802 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803 return NULL;
14804}
14805
14806#if defined(FEAT_AUTOCMD) || defined(PROTO)
14807/*
14808 * Set v:cmdarg.
14809 * If "eap" != NULL, use "eap" to generate the value and return the old value.
14810 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
14811 * Must always be called in pairs!
14812 */
14813 char_u *
14814set_cmdarg(eap, oldarg)
14815 exarg_T *eap;
14816 char_u *oldarg;
14817{
14818 char_u *oldval;
14819 char_u *newval;
14820 unsigned len;
14821
Bram Moolenaare9a41262005-01-15 22:18:47 +000014822 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014823 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014825 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000014826 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014827 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828 }
14829
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014830 if (eap->force_bin == FORCE_BIN)
14831 len = 6;
14832 else if (eap->force_bin == FORCE_NOBIN)
14833 len = 8;
14834 else
14835 len = 0;
14836 if (eap->force_ff != 0)
14837 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
14838# ifdef FEAT_MBYTE
14839 if (eap->force_enc != 0)
14840 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
14841# endif
14842
14843 newval = alloc(len + 1);
14844 if (newval == NULL)
14845 return NULL;
14846
14847 if (eap->force_bin == FORCE_BIN)
14848 sprintf((char *)newval, " ++bin");
14849 else if (eap->force_bin == FORCE_NOBIN)
14850 sprintf((char *)newval, " ++nobin");
14851 else
14852 *newval = NUL;
14853 if (eap->force_ff != 0)
14854 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
14855 eap->cmd + eap->force_ff);
14856# ifdef FEAT_MBYTE
14857 if (eap->force_enc != 0)
14858 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
14859 eap->cmd + eap->force_enc);
14860# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000014861 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014862 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863}
14864#endif
14865
14866/*
14867 * Get the value of internal variable "name".
14868 * Return OK or FAIL.
14869 */
14870 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014871get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872 char_u *name;
14873 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000014874 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014875 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876{
14877 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000014878 typval_T *tv = NULL;
14879 typval_T atv;
14880 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882
14883 /* truncate the name, so that we can use strcmp() */
14884 cc = name[len];
14885 name[len] = NUL;
14886
14887 /*
14888 * Check for "b:changedtick".
14889 */
14890 if (STRCMP(name, "b:changedtick") == 0)
14891 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000014892 atv.v_type = VAR_NUMBER;
14893 atv.vval.v_number = curbuf->b_changedtick;
14894 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895 }
14896
14897 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898 * Check for user-defined variables.
14899 */
14900 else
14901 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014902 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014903 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014904 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905 }
14906
Bram Moolenaare9a41262005-01-15 22:18:47 +000014907 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014909 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014910 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911 ret = FAIL;
14912 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014913 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014914 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915
14916 name[len] = cc;
14917
14918 return ret;
14919}
14920
14921/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014922 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
14923 * Also handle function call with Funcref variable: func(expr)
14924 * Can all be combined: dict.func(expr)[idx]['func'](expr)
14925 */
14926 static int
14927handle_subscript(arg, rettv, evaluate, verbose)
14928 char_u **arg;
14929 typval_T *rettv;
14930 int evaluate; /* do more than finding the end */
14931 int verbose; /* give error messages */
14932{
14933 int ret = OK;
14934 dict_T *selfdict = NULL;
14935 char_u *s;
14936 int len;
14937
14938 while (ret == OK
14939 && (**arg == '['
14940 || (**arg == '.' && rettv->v_type == VAR_DICT)
14941 || (**arg == '(' && rettv->v_type == VAR_FUNC))
14942 && !vim_iswhite(*(*arg - 1)))
14943 {
14944 if (**arg == '(')
14945 {
14946 s = rettv->vval.v_string;
14947
14948 /* Invoke the function. Recursive! */
14949 ret = get_func_tv(s, STRLEN(s), rettv, arg,
14950 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
14951 &len, evaluate, selfdict);
14952
14953 /* Stop the expression evaluation when immediately aborting on
14954 * error, or when an interrupt occurred or an exception was thrown
14955 * but not caught. */
14956 if (aborting())
14957 {
14958 if (ret == OK)
14959 clear_tv(rettv);
14960 ret = FAIL;
14961 }
14962 dict_unref(selfdict);
14963 selfdict = NULL;
14964 }
14965 else /* **arg == '[' || **arg == '.' */
14966 {
14967 dict_unref(selfdict);
14968 if (rettv->v_type == VAR_DICT)
14969 {
14970 selfdict = rettv->vval.v_dict;
14971 if (selfdict != NULL)
14972 ++selfdict->dv_refcount;
14973 }
14974 else
14975 selfdict = NULL;
14976 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
14977 {
14978 clear_tv(rettv);
14979 ret = FAIL;
14980 }
14981 }
14982 }
14983 dict_unref(selfdict);
14984 return ret;
14985}
14986
14987/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014988 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
14989 * value).
14990 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014991 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014992alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014993{
Bram Moolenaar33570922005-01-25 22:26:29 +000014994 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014995}
14996
14997/*
14998 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999 * The string "s" must have been allocated, it is consumed.
15000 * Return NULL for out of memory, the variable otherwise.
15001 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015002 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015003alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004 char_u *s;
15005{
Bram Moolenaar33570922005-01-25 22:26:29 +000015006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015008 rettv = alloc_tv();
15009 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015011 rettv->v_type = VAR_STRING;
15012 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013 }
15014 else
15015 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015016 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017}
15018
15019/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015020 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021 */
15022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015023free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015024 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025{
15026 if (varp != NULL)
15027 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015028 switch (varp->v_type)
15029 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015030 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015031 func_unref(varp->vval.v_string);
15032 /*FALLTHROUGH*/
15033 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015034 vim_free(varp->vval.v_string);
15035 break;
15036 case VAR_LIST:
15037 list_unref(varp->vval.v_list);
15038 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015039 case VAR_DICT:
15040 dict_unref(varp->vval.v_dict);
15041 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015042 case VAR_NUMBER:
15043 case VAR_UNKNOWN:
15044 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015045 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015046 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015047 break;
15048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015049 vim_free(varp);
15050 }
15051}
15052
15053/*
15054 * Free the memory for a variable value and set the value to NULL or 0.
15055 */
15056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015057clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015058 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059{
15060 if (varp != NULL)
15061 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015062 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015063 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015064 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015065 func_unref(varp->vval.v_string);
15066 /*FALLTHROUGH*/
15067 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015068 vim_free(varp->vval.v_string);
15069 varp->vval.v_string = NULL;
15070 break;
15071 case VAR_LIST:
15072 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015073 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015074 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015075 case VAR_DICT:
15076 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015077 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015078 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015079 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015080 varp->vval.v_number = 0;
15081 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015082 case VAR_UNKNOWN:
15083 break;
15084 default:
15085 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015087 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088 }
15089}
15090
15091/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015092 * Set the value of a variable to NULL without freeing items.
15093 */
15094 static void
15095init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015096 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015097{
15098 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015099 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015100}
15101
15102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103 * Get the number value of a variable.
15104 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015105 * For incompatible types, return 0.
15106 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15107 * caller of incompatible types: it sets *denote to TRUE if "denote"
15108 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109 */
15110 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015111get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015112 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015114 int error = FALSE;
15115
15116 return get_tv_number_chk(varp, &error); /* return 0L on error */
15117}
15118
15119 static long
15120get_tv_number_chk(varp, denote)
15121 typval_T *varp;
15122 int *denote;
15123{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015124 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015126 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015128 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015129 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015130 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015131 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015132 break;
15133 case VAR_STRING:
15134 if (varp->vval.v_string != NULL)
15135 vim_str2nr(varp->vval.v_string, NULL, NULL,
15136 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015137 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015138 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015139 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015140 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015141 case VAR_DICT:
15142 EMSG(_("E728: Using a Dictionary as a number"));
15143 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015144 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015145 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015146 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015148 if (denote == NULL) /* useful for values that must be unsigned */
15149 n = -1;
15150 else
15151 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015152 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153}
15154
15155/*
15156 * Get the lnum from the first argument. Also accepts ".", "$", etc.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015157 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158 */
15159 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015160get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015161 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162{
Bram Moolenaar33570922005-01-25 22:26:29 +000015163 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164 linenr_T lnum;
15165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015166 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167 if (lnum == 0) /* no valid number, try using line() */
15168 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015169 rettv.v_type = VAR_NUMBER;
15170 f_line(argvars, &rettv);
15171 lnum = rettv.vval.v_number;
15172 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173 }
15174 return lnum;
15175}
15176
15177/*
15178 * Get the string value of a variable.
15179 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015180 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15181 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182 * If the String variable has never been set, return an empty string.
15183 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015184 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15185 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186 */
15187 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015188get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015189 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190{
15191 static char_u mybuf[NUMBUFLEN];
15192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015193 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194}
15195
15196 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015197get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015198 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015199 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015200{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015201 char_u *res = get_tv_string_buf_chk(varp, buf);
15202
15203 return res != NULL ? res : (char_u *)"";
15204}
15205
15206 static char_u *
15207get_tv_string_chk(varp)
15208 typval_T *varp;
15209{
15210 static char_u mybuf[NUMBUFLEN];
15211
15212 return get_tv_string_buf_chk(varp, mybuf);
15213}
15214
15215 static char_u *
15216get_tv_string_buf_chk(varp, buf)
15217 typval_T *varp;
15218 char_u *buf;
15219{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015220 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015222 case VAR_NUMBER:
15223 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15224 return buf;
15225 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015226 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015227 break;
15228 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015229 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015230 break;
15231 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015232 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015233 break;
15234 case VAR_STRING:
15235 if (varp->vval.v_string != NULL)
15236 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015237 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015238 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015239 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015240 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015242 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015243}
15244
15245/*
15246 * Find variable "name" in the list of variables.
15247 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015248 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015249 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015250 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015251 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015252 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015253find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015254 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015255 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015258 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259
Bram Moolenaara7043832005-01-21 11:56:39 +000015260 ht = find_var_ht(name, &varname);
15261 if (htp != NULL)
15262 *htp = ht;
15263 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015265 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015266}
15267
15268/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015269 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000015270 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015271 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015272 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015273find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000015274 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000015275 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015276 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000015277{
Bram Moolenaar33570922005-01-25 22:26:29 +000015278 hashitem_T *hi;
15279
15280 if (*varname == NUL)
15281 {
15282 /* Must be something like "s:", otherwise "ht" would be NULL. */
15283 switch (varname[-2])
15284 {
15285 case 's': return &SCRIPT_SV(current_SID).sv_var;
15286 case 'g': return &globvars_var;
15287 case 'v': return &vimvars_var;
15288 case 'b': return &curbuf->b_bufvar;
15289 case 'w': return &curwin->w_winvar;
15290 case 'l': return &current_funccal->l_vars_var;
15291 case 'a': return &current_funccal->l_avars_var;
15292 }
15293 return NULL;
15294 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015295
15296 hi = hash_find(ht, varname);
15297 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015298 {
15299 /* For global variables we may try auto-loading the script. If it
15300 * worked find the variable again. */
15301 if (ht == &globvarht && !writing
15302 && script_autoload(varname) && !aborting())
15303 hi = hash_find(ht, varname);
15304 if (HASHITEM_EMPTY(hi))
15305 return NULL;
15306 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015307 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015308}
15309
15310/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015311 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015312 * Set "varname" to the start of name without ':'.
15313 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015314 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015315find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015316 char_u *name;
15317 char_u **varname;
15318{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015319 hashitem_T *hi;
15320
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321 if (name[1] != ':')
15322 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015323 /* The name must not start with a colon or #. */
15324 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325 return NULL;
15326 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015327
15328 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015329 hi = hash_find(&compat_hashtab, name);
15330 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000015331 return &compat_hashtab;
15332
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015334 return &globvarht; /* global variable */
15335 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336 }
15337 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015338 if (*name == 'g') /* global variable */
15339 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015340 /* There must be no ':' or '#' in the rest of the name, unless g: is used
15341 */
15342 if (vim_strchr(name + 2, ':') != NULL
15343 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015344 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015346 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015348 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000015349 if (*name == 'v') /* v: variable */
15350 return &vimvarht;
15351 if (*name == 'a' && current_funccal != NULL) /* function argument */
15352 return &current_funccal->l_avars.dv_hashtab;
15353 if (*name == 'l' && current_funccal != NULL) /* local function variable */
15354 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355 if (*name == 's' /* script variable */
15356 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
15357 return &SCRIPT_VARS(current_SID);
15358 return NULL;
15359}
15360
15361/*
15362 * Get the string value of a (global/local) variable.
15363 * Returns NULL when it doesn't exist.
15364 */
15365 char_u *
15366get_var_value(name)
15367 char_u *name;
15368{
Bram Moolenaar33570922005-01-25 22:26:29 +000015369 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370
Bram Moolenaara7043832005-01-21 11:56:39 +000015371 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372 if (v == NULL)
15373 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015374 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375}
15376
15377/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015378 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379 * sourcing this script and when executing functions defined in the script.
15380 */
15381 void
15382new_script_vars(id)
15383 scid_T id;
15384{
Bram Moolenaara7043832005-01-21 11:56:39 +000015385 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000015386 hashtab_T *ht;
15387 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000015388
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
15390 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015391 /* Re-allocating ga_data means that an ht_array pointing to
15392 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000015393 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000015394 for (i = 1; i <= ga_scripts.ga_len; ++i)
15395 {
15396 ht = &SCRIPT_VARS(i);
15397 if (ht->ht_mask == HT_INIT_SIZE - 1)
15398 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000015399 sv = &SCRIPT_SV(i);
15400 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000015401 }
15402
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 while (ga_scripts.ga_len < id)
15404 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015405 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
15406 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408 }
15409 }
15410}
15411
15412/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015413 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
15414 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415 */
15416 void
Bram Moolenaar33570922005-01-25 22:26:29 +000015417init_var_dict(dict, dict_var)
15418 dict_T *dict;
15419 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420{
Bram Moolenaar33570922005-01-25 22:26:29 +000015421 hash_init(&dict->dv_hashtab);
15422 dict->dv_refcount = 99999;
15423 dict_var->di_tv.vval.v_dict = dict;
15424 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015425 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015426 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15427 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428}
15429
15430/*
15431 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000015432 * Frees all allocated variables and the value they contain.
15433 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434 */
15435 void
Bram Moolenaara7043832005-01-21 11:56:39 +000015436vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000015437 hashtab_T *ht;
15438{
15439 vars_clear_ext(ht, TRUE);
15440}
15441
15442/*
15443 * Like vars_clear(), but only free the value if "free_val" is TRUE.
15444 */
15445 static void
15446vars_clear_ext(ht, free_val)
15447 hashtab_T *ht;
15448 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015449{
Bram Moolenaara7043832005-01-21 11:56:39 +000015450 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000015451 hashitem_T *hi;
15452 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453
Bram Moolenaar33570922005-01-25 22:26:29 +000015454 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000015455 todo = ht->ht_used;
15456 for (hi = ht->ht_array; todo > 0; ++hi)
15457 {
15458 if (!HASHITEM_EMPTY(hi))
15459 {
15460 --todo;
15461
Bram Moolenaar33570922005-01-25 22:26:29 +000015462 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000015463 * ht_array might change then. hash_clear() takes care of it
15464 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015465 v = HI2DI(hi);
15466 if (free_val)
15467 clear_tv(&v->di_tv);
15468 if ((v->di_flags & DI_FLAGS_FIX) == 0)
15469 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000015470 }
15471 }
15472 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473}
15474
Bram Moolenaara7043832005-01-21 11:56:39 +000015475/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015476 * Delete a variable from hashtab "ht" at item "hi".
15477 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000015478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000015480delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000015481 hashtab_T *ht;
15482 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483{
Bram Moolenaar33570922005-01-25 22:26:29 +000015484 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015485
15486 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000015487 clear_tv(&di->di_tv);
15488 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489}
15490
15491/*
15492 * List the value of one internal variable.
15493 */
15494 static void
15495list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000015496 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497 char_u *prefix;
15498{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015499 char_u *tofree;
15500 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015501 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015502
Bram Moolenaar33570922005-01-25 22:26:29 +000015503 s = echo_string(&v->di_tv, &tofree, numbuf);
15504 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015505 s == NULL ? (char_u *)"" : s);
15506 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507}
15508
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509 static void
15510list_one_var_a(prefix, name, type, string)
15511 char_u *prefix;
15512 char_u *name;
15513 int type;
15514 char_u *string;
15515{
15516 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
15517 if (name != NULL) /* "a:" vars don't have a name stored */
15518 msg_puts(name);
15519 msg_putchar(' ');
15520 msg_advance(22);
15521 if (type == VAR_NUMBER)
15522 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015523 else if (type == VAR_FUNC)
15524 msg_putchar('*');
15525 else if (type == VAR_LIST)
15526 {
15527 msg_putchar('[');
15528 if (*string == '[')
15529 ++string;
15530 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015531 else if (type == VAR_DICT)
15532 {
15533 msg_putchar('{');
15534 if (*string == '{')
15535 ++string;
15536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537 else
15538 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015539
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015541
15542 if (type == VAR_FUNC)
15543 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544}
15545
15546/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015547 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548 * If the variable already exists, the value is updated.
15549 * Otherwise the variable is created.
15550 */
15551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015552set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015554 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015555 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556{
Bram Moolenaar33570922005-01-25 22:26:29 +000015557 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015559 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015560 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015562 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015563 {
15564 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
15565 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
15566 ? name[2] : name[0]))
15567 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015568 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015569 return;
15570 }
15571 if (function_exists(name))
15572 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015573 EMSG2(_("705: Variable name conflicts with existing function: %s"),
15574 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015575 return;
15576 }
15577 }
15578
Bram Moolenaara7043832005-01-21 11:56:39 +000015579 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015580 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000015581 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000015582 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000015583 return;
15584 }
15585
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015586 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015587 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015589 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015590 if (var_check_ro(v->di_flags, name)
15591 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000015592 return;
15593 if (v->di_tv.v_type != tv->v_type
15594 && !((v->di_tv.v_type == VAR_STRING
15595 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015596 && (tv->v_type == VAR_STRING
15597 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015598 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015599 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015600 return;
15601 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015602
15603 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000015604 * Handle setting internal v: variables separately: we don't change
15605 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000015606 */
15607 if (ht == &vimvarht)
15608 {
15609 if (v->di_tv.v_type == VAR_STRING)
15610 {
15611 vim_free(v->di_tv.vval.v_string);
15612 if (copy || tv->v_type != VAR_STRING)
15613 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
15614 else
15615 {
15616 /* Take over the string to avoid an extra alloc/free. */
15617 v->di_tv.vval.v_string = tv->vval.v_string;
15618 tv->vval.v_string = NULL;
15619 }
15620 }
15621 else if (v->di_tv.v_type != VAR_NUMBER)
15622 EMSG2(_(e_intern2), "set_var()");
15623 else
15624 v->di_tv.vval.v_number = get_tv_number(tv);
15625 return;
15626 }
15627
15628 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629 }
15630 else /* add a new variable */
15631 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000015632 /* Make sure the variable name is valid. */
15633 for (p = varname; *p != NUL; ++p)
15634 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
15635 {
15636 EMSG2(_(e_illvar), varname);
15637 return;
15638 }
15639
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015640 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
15641 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000015642 if (v == NULL)
15643 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000015644 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015645 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015646 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015647 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 return;
15649 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015650 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015653 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000015654 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015655 else
15656 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015657 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015658 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015659 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661}
15662
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015663/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015664 * Return TRUE if di_flags "flags" indicate read-only variable "name".
15665 * Also give an error message.
15666 */
15667 static int
15668var_check_ro(flags, name)
15669 int flags;
15670 char_u *name;
15671{
15672 if (flags & DI_FLAGS_RO)
15673 {
15674 EMSG2(_(e_readonlyvar), name);
15675 return TRUE;
15676 }
15677 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
15678 {
15679 EMSG2(_(e_readonlysbx), name);
15680 return TRUE;
15681 }
15682 return FALSE;
15683}
15684
15685/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015686 * Return TRUE if typeval "tv" is set to be locked (immutable).
15687 * Also give an error message, using "name".
15688 */
15689 static int
15690tv_check_lock(lock, name)
15691 int lock;
15692 char_u *name;
15693{
15694 if (lock & VAR_LOCKED)
15695 {
15696 EMSG2(_("E741: Value is locked: %s"),
15697 name == NULL ? (char_u *)_("Unknown") : name);
15698 return TRUE;
15699 }
15700 if (lock & VAR_FIXED)
15701 {
15702 EMSG2(_("E742: Cannot change value of %s"),
15703 name == NULL ? (char_u *)_("Unknown") : name);
15704 return TRUE;
15705 }
15706 return FALSE;
15707}
15708
15709/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015710 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015711 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015712 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015713 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015715copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000015716 typval_T *from;
15717 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015719 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015720 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015721 switch (from->v_type)
15722 {
15723 case VAR_NUMBER:
15724 to->vval.v_number = from->vval.v_number;
15725 break;
15726 case VAR_STRING:
15727 case VAR_FUNC:
15728 if (from->vval.v_string == NULL)
15729 to->vval.v_string = NULL;
15730 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015731 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015732 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015733 if (from->v_type == VAR_FUNC)
15734 func_ref(to->vval.v_string);
15735 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015736 break;
15737 case VAR_LIST:
15738 if (from->vval.v_list == NULL)
15739 to->vval.v_list = NULL;
15740 else
15741 {
15742 to->vval.v_list = from->vval.v_list;
15743 ++to->vval.v_list->lv_refcount;
15744 }
15745 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015746 case VAR_DICT:
15747 if (from->vval.v_dict == NULL)
15748 to->vval.v_dict = NULL;
15749 else
15750 {
15751 to->vval.v_dict = from->vval.v_dict;
15752 ++to->vval.v_dict->dv_refcount;
15753 }
15754 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015755 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015756 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015757 break;
15758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759}
15760
15761/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000015762 * Make a copy of an item.
15763 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015764 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
15765 * reference to an already copied list/dict can be used.
15766 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015767 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015768 static int
15769item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000015770 typval_T *from;
15771 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015772 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015773 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015774{
15775 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015776 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015777
Bram Moolenaar33570922005-01-25 22:26:29 +000015778 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015779 {
15780 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015781 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015782 }
15783 ++recurse;
15784
15785 switch (from->v_type)
15786 {
15787 case VAR_NUMBER:
15788 case VAR_STRING:
15789 case VAR_FUNC:
15790 copy_tv(from, to);
15791 break;
15792 case VAR_LIST:
15793 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015794 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015795 if (from->vval.v_list == NULL)
15796 to->vval.v_list = NULL;
15797 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
15798 {
15799 /* use the copy made earlier */
15800 to->vval.v_list = from->vval.v_list->lv_copylist;
15801 ++to->vval.v_list->lv_refcount;
15802 }
15803 else
15804 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
15805 if (to->vval.v_list == NULL)
15806 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015807 break;
15808 case VAR_DICT:
15809 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015810 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015811 if (from->vval.v_dict == NULL)
15812 to->vval.v_dict = NULL;
15813 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
15814 {
15815 /* use the copy made earlier */
15816 to->vval.v_dict = from->vval.v_dict->dv_copydict;
15817 ++to->vval.v_dict->dv_refcount;
15818 }
15819 else
15820 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
15821 if (to->vval.v_dict == NULL)
15822 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015823 break;
15824 default:
15825 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015826 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015827 }
15828 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015829 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015830}
15831
15832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833 * ":echo expr1 ..." print each argument separated with a space, add a
15834 * newline at the end.
15835 * ":echon expr1 ..." print each argument plain.
15836 */
15837 void
15838ex_echo(eap)
15839 exarg_T *eap;
15840{
15841 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015842 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015843 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844 char_u *p;
15845 int needclr = TRUE;
15846 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015847 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848
15849 if (eap->skip)
15850 ++emsg_skip;
15851 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
15852 {
15853 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015854 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855 {
15856 /*
15857 * Report the invalid expression unless the expression evaluation
15858 * has been cancelled due to an aborting error, an interrupt, or an
15859 * exception.
15860 */
15861 if (!aborting())
15862 EMSG2(_(e_invexpr2), p);
15863 break;
15864 }
15865 if (!eap->skip)
15866 {
15867 if (atstart)
15868 {
15869 atstart = FALSE;
15870 /* Call msg_start() after eval1(), evaluating the expression
15871 * may cause a message to appear. */
15872 if (eap->cmdidx == CMD_echo)
15873 msg_start();
15874 }
15875 else if (eap->cmdidx == CMD_echo)
15876 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015877 p = echo_string(&rettv, &tofree, numbuf);
15878 if (p != NULL)
15879 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015881 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015883 if (*p != TAB && needclr)
15884 {
15885 /* remove any text still there from the command */
15886 msg_clr_eos();
15887 needclr = FALSE;
15888 }
15889 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890 }
15891 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015892 {
15893#ifdef FEAT_MBYTE
15894 if (has_mbyte)
15895 {
15896 int i = (*mb_ptr2len_check)(p);
15897
15898 (void)msg_outtrans_len_attr(p, i, echo_attr);
15899 p += i - 1;
15900 }
15901 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000015902#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015903 (void)msg_outtrans_len_attr(p, 1, echo_attr);
15904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015905 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015906 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015908 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015909 arg = skipwhite(arg);
15910 }
15911 eap->nextcmd = check_nextcmd(arg);
15912
15913 if (eap->skip)
15914 --emsg_skip;
15915 else
15916 {
15917 /* remove text that may still be there from the command */
15918 if (needclr)
15919 msg_clr_eos();
15920 if (eap->cmdidx == CMD_echo)
15921 msg_end();
15922 }
15923}
15924
15925/*
15926 * ":echohl {name}".
15927 */
15928 void
15929ex_echohl(eap)
15930 exarg_T *eap;
15931{
15932 int id;
15933
15934 id = syn_name2id(eap->arg);
15935 if (id == 0)
15936 echo_attr = 0;
15937 else
15938 echo_attr = syn_id2attr(id);
15939}
15940
15941/*
15942 * ":execute expr1 ..." execute the result of an expression.
15943 * ":echomsg expr1 ..." Print a message
15944 * ":echoerr expr1 ..." Print an error
15945 * Each gets spaces around each argument and a newline at the end for
15946 * echo commands
15947 */
15948 void
15949ex_execute(eap)
15950 exarg_T *eap;
15951{
15952 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015953 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015954 int ret = OK;
15955 char_u *p;
15956 garray_T ga;
15957 int len;
15958 int save_did_emsg;
15959
15960 ga_init2(&ga, 1, 80);
15961
15962 if (eap->skip)
15963 ++emsg_skip;
15964 while (*arg != NUL && *arg != '|' && *arg != '\n')
15965 {
15966 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015967 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015968 {
15969 /*
15970 * Report the invalid expression unless the expression evaluation
15971 * has been cancelled due to an aborting error, an interrupt, or an
15972 * exception.
15973 */
15974 if (!aborting())
15975 EMSG2(_(e_invexpr2), p);
15976 ret = FAIL;
15977 break;
15978 }
15979
15980 if (!eap->skip)
15981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015982 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015983 len = (int)STRLEN(p);
15984 if (ga_grow(&ga, len + 2) == FAIL)
15985 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015986 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987 ret = FAIL;
15988 break;
15989 }
15990 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015991 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015993 ga.ga_len += len;
15994 }
15995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015996 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997 arg = skipwhite(arg);
15998 }
15999
16000 if (ret != FAIL && ga.ga_data != NULL)
16001 {
16002 if (eap->cmdidx == CMD_echomsg)
16003 MSG_ATTR(ga.ga_data, echo_attr);
16004 else if (eap->cmdidx == CMD_echoerr)
16005 {
16006 /* We don't want to abort following commands, restore did_emsg. */
16007 save_did_emsg = did_emsg;
16008 EMSG((char_u *)ga.ga_data);
16009 if (!force_abort)
16010 did_emsg = save_did_emsg;
16011 }
16012 else if (eap->cmdidx == CMD_execute)
16013 do_cmdline((char_u *)ga.ga_data,
16014 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16015 }
16016
16017 ga_clear(&ga);
16018
16019 if (eap->skip)
16020 --emsg_skip;
16021
16022 eap->nextcmd = check_nextcmd(arg);
16023}
16024
16025/*
16026 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16027 * "arg" points to the "&" or '+' when called, to "option" when returning.
16028 * Returns NULL when no option name found. Otherwise pointer to the char
16029 * after the option name.
16030 */
16031 static char_u *
16032find_option_end(arg, opt_flags)
16033 char_u **arg;
16034 int *opt_flags;
16035{
16036 char_u *p = *arg;
16037
16038 ++p;
16039 if (*p == 'g' && p[1] == ':')
16040 {
16041 *opt_flags = OPT_GLOBAL;
16042 p += 2;
16043 }
16044 else if (*p == 'l' && p[1] == ':')
16045 {
16046 *opt_flags = OPT_LOCAL;
16047 p += 2;
16048 }
16049 else
16050 *opt_flags = 0;
16051
16052 if (!ASCII_ISALPHA(*p))
16053 return NULL;
16054 *arg = p;
16055
16056 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16057 p += 4; /* termcap option */
16058 else
16059 while (ASCII_ISALPHA(*p))
16060 ++p;
16061 return p;
16062}
16063
16064/*
16065 * ":function"
16066 */
16067 void
16068ex_function(eap)
16069 exarg_T *eap;
16070{
16071 char_u *theline;
16072 int j;
16073 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016074 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075 char_u *name = NULL;
16076 char_u *p;
16077 char_u *arg;
16078 garray_T newargs;
16079 garray_T newlines;
16080 int varargs = FALSE;
16081 int mustend = FALSE;
16082 int flags = 0;
16083 ufunc_T *fp;
16084 int indent;
16085 int nesting;
16086 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016087 dictitem_T *v;
16088 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016089 static int func_nr = 0; /* number for nameless function */
16090 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016091 hashtab_T *ht;
16092 int todo;
16093 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094
16095 /*
16096 * ":function" without argument: list functions.
16097 */
16098 if (ends_excmd(*eap->arg))
16099 {
16100 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016101 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016102 todo = func_hashtab.ht_used;
16103 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016104 {
16105 if (!HASHITEM_EMPTY(hi))
16106 {
16107 --todo;
16108 fp = HI2UF(hi);
16109 if (!isdigit(*fp->uf_name))
16110 list_func_head(fp, FALSE);
16111 }
16112 }
16113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114 eap->nextcmd = check_nextcmd(eap->arg);
16115 return;
16116 }
16117
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016118 /*
16119 * Get the function name. There are these situations:
16120 * func normal function name
16121 * "name" == func, "fudi.fd_dict" == NULL
16122 * dict.func new dictionary entry
16123 * "name" == NULL, "fudi.fd_dict" set,
16124 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16125 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016126 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016127 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16128 * dict.func existing dict entry that's not a Funcref
16129 * "name" == NULL, "fudi.fd_dict" set,
16130 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016133 name = trans_function_name(&p, eap->skip, 0, &fudi);
16134 paren = (vim_strchr(p, '(') != NULL);
16135 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136 {
16137 /*
16138 * Return on an invalid expression in braces, unless the expression
16139 * evaluation has been cancelled due to an aborting error, an
16140 * interrupt, or an exception.
16141 */
16142 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016143 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016144 if (!eap->skip && fudi.fd_newkey != NULL)
16145 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016146 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149 else
16150 eap->skip = TRUE;
16151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016152 /* An error in a function call during evaluation of an expression in magic
16153 * braces should not cause the function not to be defined. */
16154 saved_did_emsg = did_emsg;
16155 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156
16157 /*
16158 * ":function func" with only function name: list function.
16159 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016160 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161 {
16162 if (!ends_excmd(*skipwhite(p)))
16163 {
16164 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016165 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 }
16167 eap->nextcmd = check_nextcmd(p);
16168 if (eap->nextcmd != NULL)
16169 *p = NUL;
16170 if (!eap->skip && !got_int)
16171 {
16172 fp = find_func(name);
16173 if (fp != NULL)
16174 {
16175 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016176 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177 {
16178 msg_putchar('\n');
16179 msg_outnum((long)(j + 1));
16180 if (j < 9)
16181 msg_putchar(' ');
16182 if (j < 99)
16183 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016184 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185 out_flush(); /* show a line at a time */
16186 ui_breakcheck();
16187 }
16188 if (!got_int)
16189 {
16190 msg_putchar('\n');
16191 msg_puts((char_u *)" endfunction");
16192 }
16193 }
16194 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016195 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016197 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198 }
16199
16200 /*
16201 * ":function name(arg1, arg2)" Define function.
16202 */
16203 p = skipwhite(p);
16204 if (*p != '(')
16205 {
16206 if (!eap->skip)
16207 {
16208 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016209 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 }
16211 /* attempt to continue by skipping some text */
16212 if (vim_strchr(p, '(') != NULL)
16213 p = vim_strchr(p, '(');
16214 }
16215 p = skipwhite(p + 1);
16216
16217 ga_init2(&newargs, (int)sizeof(char_u *), 3);
16218 ga_init2(&newlines, (int)sizeof(char_u *), 3);
16219
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016220 if (!eap->skip)
16221 {
16222 /* Check the name of the function. */
16223 if (name != NULL)
16224 arg = name;
16225 else
16226 arg = fudi.fd_newkey;
16227 if (arg != NULL)
16228 {
16229 if (*arg == K_SPECIAL)
16230 j = 3;
16231 else
16232 j = 0;
16233 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
16234 : eval_isnamec(arg[j])))
16235 ++j;
16236 if (arg[j] != NUL)
16237 emsg_funcname(_(e_invarg2), arg);
16238 }
16239 }
16240
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241 /*
16242 * Isolate the arguments: "arg1, arg2, ...)"
16243 */
16244 while (*p != ')')
16245 {
16246 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
16247 {
16248 varargs = TRUE;
16249 p += 3;
16250 mustend = TRUE;
16251 }
16252 else
16253 {
16254 arg = p;
16255 while (ASCII_ISALNUM(*p) || *p == '_')
16256 ++p;
16257 if (arg == p || isdigit(*arg)
16258 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
16259 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
16260 {
16261 if (!eap->skip)
16262 EMSG2(_("E125: Illegal argument: %s"), arg);
16263 break;
16264 }
16265 if (ga_grow(&newargs, 1) == FAIL)
16266 goto erret;
16267 c = *p;
16268 *p = NUL;
16269 arg = vim_strsave(arg);
16270 if (arg == NULL)
16271 goto erret;
16272 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
16273 *p = c;
16274 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275 if (*p == ',')
16276 ++p;
16277 else
16278 mustend = TRUE;
16279 }
16280 p = skipwhite(p);
16281 if (mustend && *p != ')')
16282 {
16283 if (!eap->skip)
16284 EMSG2(_(e_invarg2), eap->arg);
16285 break;
16286 }
16287 }
16288 ++p; /* skip the ')' */
16289
Bram Moolenaare9a41262005-01-15 22:18:47 +000016290 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291 for (;;)
16292 {
16293 p = skipwhite(p);
16294 if (STRNCMP(p, "range", 5) == 0)
16295 {
16296 flags |= FC_RANGE;
16297 p += 5;
16298 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016299 else if (STRNCMP(p, "dict", 4) == 0)
16300 {
16301 flags |= FC_DICT;
16302 p += 4;
16303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 else if (STRNCMP(p, "abort", 5) == 0)
16305 {
16306 flags |= FC_ABORT;
16307 p += 5;
16308 }
16309 else
16310 break;
16311 }
16312
16313 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
16314 EMSG(_(e_trailing));
16315
16316 /*
16317 * Read the body of the function, until ":endfunction" is found.
16318 */
16319 if (KeyTyped)
16320 {
16321 /* Check if the function already exists, don't let the user type the
16322 * whole function before telling him it doesn't work! For a script we
16323 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016324 if (!eap->skip && !eap->forceit)
16325 {
16326 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
16327 EMSG(_(e_funcdict));
16328 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016329 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016332 if (!eap->skip && did_emsg)
16333 goto erret;
16334
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335 msg_putchar('\n'); /* don't overwrite the function name */
16336 cmdline_row = msg_row;
16337 }
16338
16339 indent = 2;
16340 nesting = 0;
16341 for (;;)
16342 {
16343 msg_scroll = TRUE;
16344 need_wait_return = FALSE;
16345 if (eap->getline == NULL)
16346 theline = getcmdline(':', 0L, indent);
16347 else
16348 theline = eap->getline(':', eap->cookie, indent);
16349 if (KeyTyped)
16350 lines_left = Rows - 1;
16351 if (theline == NULL)
16352 {
16353 EMSG(_("E126: Missing :endfunction"));
16354 goto erret;
16355 }
16356
16357 if (skip_until != NULL)
16358 {
16359 /* between ":append" and "." and between ":python <<EOF" and "EOF"
16360 * don't check for ":endfunc". */
16361 if (STRCMP(theline, skip_until) == 0)
16362 {
16363 vim_free(skip_until);
16364 skip_until = NULL;
16365 }
16366 }
16367 else
16368 {
16369 /* skip ':' and blanks*/
16370 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
16371 ;
16372
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016373 /* Check for "endfunction". */
16374 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 {
16376 vim_free(theline);
16377 break;
16378 }
16379
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016380 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 * at "end". */
16382 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
16383 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016384 else if (STRNCMP(p, "if", 2) == 0
16385 || STRNCMP(p, "wh", 2) == 0
16386 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387 || STRNCMP(p, "try", 3) == 0)
16388 indent += 2;
16389
16390 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016391 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016393 if (*p == '!')
16394 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395 p += eval_fname_script(p);
16396 if (ASCII_ISALPHA(*p))
16397 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016398 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399 if (*skipwhite(p) == '(')
16400 {
16401 ++nesting;
16402 indent += 2;
16403 }
16404 }
16405 }
16406
16407 /* Check for ":append" or ":insert". */
16408 p = skip_range(p, NULL);
16409 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
16410 || (p[0] == 'i'
16411 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
16412 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
16413 skip_until = vim_strsave((char_u *)".");
16414
16415 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
16416 arg = skipwhite(skiptowhite(p));
16417 if (arg[0] == '<' && arg[1] =='<'
16418 && ((p[0] == 'p' && p[1] == 'y'
16419 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
16420 || (p[0] == 'p' && p[1] == 'e'
16421 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
16422 || (p[0] == 't' && p[1] == 'c'
16423 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
16424 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
16425 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000016426 || (p[0] == 'm' && p[1] == 'z'
16427 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428 ))
16429 {
16430 /* ":python <<" continues until a dot, like ":append" */
16431 p = skipwhite(arg + 2);
16432 if (*p == NUL)
16433 skip_until = vim_strsave((char_u *)".");
16434 else
16435 skip_until = vim_strsave(p);
16436 }
16437 }
16438
16439 /* Add the line to the function. */
16440 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016441 {
16442 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016444 }
16445
16446 /* Copy the line to newly allocated memory. get_one_sourceline()
16447 * allocates 250 bytes per line, this saves 80% on average. The cost
16448 * is an extra alloc/free. */
16449 p = vim_strsave(theline);
16450 if (p != NULL)
16451 {
16452 vim_free(theline);
16453 theline = p;
16454 }
16455
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
16457 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458 }
16459
16460 /* Don't define the function when skipping commands or when an error was
16461 * detected. */
16462 if (eap->skip || did_emsg)
16463 goto erret;
16464
16465 /*
16466 * If there are no errors, add the function
16467 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016468 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016469 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016470 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000016471 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016472 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016473 emsg_funcname("E707: Function name conflicts with variable: %s",
16474 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016475 goto erret;
16476 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016477
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016478 fp = find_func(name);
16479 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016481 if (!eap->forceit)
16482 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016483 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016484 goto erret;
16485 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016486 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016487 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016488 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016489 name);
16490 goto erret;
16491 }
16492 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016493 ga_clear_strings(&(fp->uf_args));
16494 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016495 vim_free(name);
16496 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498 }
16499 else
16500 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016501 char numbuf[20];
16502
16503 fp = NULL;
16504 if (fudi.fd_newkey == NULL && !eap->forceit)
16505 {
16506 EMSG(_(e_funcdict));
16507 goto erret;
16508 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000016509 if (fudi.fd_di == NULL)
16510 {
16511 /* Can't add a function to a locked dictionary */
16512 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
16513 goto erret;
16514 }
16515 /* Can't change an existing function if it is locked */
16516 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
16517 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016518
16519 /* Give the function a sequential number. Can only be used with a
16520 * Funcref! */
16521 vim_free(name);
16522 sprintf(numbuf, "%d", ++func_nr);
16523 name = vim_strsave((char_u *)numbuf);
16524 if (name == NULL)
16525 goto erret;
16526 }
16527
16528 if (fp == NULL)
16529 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016530 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016531 {
16532 int slen, plen;
16533 char_u *scriptname;
16534
16535 /* Check that the autoload name matches the script name. */
16536 j = FAIL;
16537 if (sourcing_name != NULL)
16538 {
16539 scriptname = autoload_name(name);
16540 if (scriptname != NULL)
16541 {
16542 p = vim_strchr(scriptname, '/');
16543 plen = STRLEN(p);
16544 slen = STRLEN(sourcing_name);
16545 if (slen > plen && fnamecmp(p,
16546 sourcing_name + slen - plen) == 0)
16547 j = OK;
16548 vim_free(scriptname);
16549 }
16550 }
16551 if (j == FAIL)
16552 {
16553 EMSG2(_("E746: Function name does not match script file name: %s"), name);
16554 goto erret;
16555 }
16556 }
16557
16558 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 if (fp == NULL)
16560 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016561
16562 if (fudi.fd_dict != NULL)
16563 {
16564 if (fudi.fd_di == NULL)
16565 {
16566 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016567 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016568 if (fudi.fd_di == NULL)
16569 {
16570 vim_free(fp);
16571 goto erret;
16572 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016573 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
16574 {
16575 vim_free(fudi.fd_di);
16576 goto erret;
16577 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016578 }
16579 else
16580 /* overwrite existing dict entry */
16581 clear_tv(&fudi.fd_di->di_tv);
16582 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016583 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016584 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016585 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016586 }
16587
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016589 STRCPY(fp->uf_name, name);
16590 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016592 fp->uf_args = newargs;
16593 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016594#ifdef FEAT_PROFILE
16595 fp->uf_tml_count = NULL;
16596 fp->uf_tml_total = NULL;
16597 fp->uf_tml_self = NULL;
16598 fp->uf_profiling = FALSE;
16599 if (prof_def_func())
16600 func_do_profile(fp);
16601#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016602 fp->uf_varargs = varargs;
16603 fp->uf_flags = flags;
16604 fp->uf_calls = 0;
16605 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016606 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016607
16608erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609 ga_clear_strings(&newargs);
16610 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016611ret_free:
16612 vim_free(skip_until);
16613 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016616}
16617
16618/*
16619 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000016620 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016622 * flags:
16623 * TFN_INT: internal function name OK
16624 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625 * Advances "pp" to just after the function name (if no error).
16626 */
16627 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016628trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629 char_u **pp;
16630 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016631 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000016632 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016633{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016634 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635 char_u *start;
16636 char_u *end;
16637 int lead;
16638 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016640 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016641
16642 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016643 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000016645
16646 /* Check for hard coded <SNR>: already translated function ID (from a user
16647 * command). */
16648 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
16649 && (*pp)[2] == (int)KE_SNR)
16650 {
16651 *pp += 3;
16652 len = get_id_len(pp) + 3;
16653 return vim_strnsave(start, len);
16654 }
16655
16656 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
16657 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000016659 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016661
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016662 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
16663 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016664 if (end == start)
16665 {
16666 if (!skip)
16667 EMSG(_("E129: Function name required"));
16668 goto theend;
16669 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016670 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016671 {
16672 /*
16673 * Report an invalid expression in braces, unless the expression
16674 * evaluation has been cancelled due to an aborting error, an
16675 * interrupt, or an exception.
16676 */
16677 if (!aborting())
16678 {
16679 if (end != NULL)
16680 EMSG2(_(e_invarg2), start);
16681 }
16682 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016683 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016684 goto theend;
16685 }
16686
16687 if (lv.ll_tv != NULL)
16688 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016689 if (fdp != NULL)
16690 {
16691 fdp->fd_dict = lv.ll_dict;
16692 fdp->fd_newkey = lv.ll_newkey;
16693 lv.ll_newkey = NULL;
16694 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016695 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016696 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
16697 {
16698 name = vim_strsave(lv.ll_tv->vval.v_string);
16699 *pp = end;
16700 }
16701 else
16702 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016703 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
16704 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016705 EMSG(_(e_funcref));
16706 else
16707 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016708 name = NULL;
16709 }
16710 goto theend;
16711 }
16712
16713 if (lv.ll_name == NULL)
16714 {
16715 /* Error found, but continue after the function name. */
16716 *pp = end;
16717 goto theend;
16718 }
16719
16720 if (lv.ll_exp_name != NULL)
16721 len = STRLEN(lv.ll_exp_name);
16722 else
Bram Moolenaara7043832005-01-21 11:56:39 +000016723 {
16724 if (lead == 2) /* skip over "s:" */
16725 lv.ll_name += 2;
16726 len = (int)(end - lv.ll_name);
16727 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016728
16729 /*
16730 * Copy the function name to allocated memory.
16731 * Accept <SID>name() inside a script, translate into <SNR>123_name().
16732 * Accept <SNR>123_name() outside a script.
16733 */
16734 if (skip)
16735 lead = 0; /* do nothing */
16736 else if (lead > 0)
16737 {
16738 lead = 3;
16739 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
16740 {
16741 if (current_SID <= 0)
16742 {
16743 EMSG(_(e_usingsid));
16744 goto theend;
16745 }
16746 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
16747 lead += (int)STRLEN(sid_buf);
16748 }
16749 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016750 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016751 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016752 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016753 goto theend;
16754 }
16755 name = alloc((unsigned)(len + lead + 1));
16756 if (name != NULL)
16757 {
16758 if (lead > 0)
16759 {
16760 name[0] = K_SPECIAL;
16761 name[1] = KS_EXTRA;
16762 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000016763 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016764 STRCPY(name + 3, sid_buf);
16765 }
16766 mch_memmove(name + lead, lv.ll_name, (size_t)len);
16767 name[len + lead] = NUL;
16768 }
16769 *pp = end;
16770
16771theend:
16772 clear_lval(&lv);
16773 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774}
16775
16776/*
16777 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
16778 * Return 2 if "p" starts with "s:".
16779 * Return 0 otherwise.
16780 */
16781 static int
16782eval_fname_script(p)
16783 char_u *p;
16784{
16785 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
16786 || STRNICMP(p + 1, "SNR>", 4) == 0))
16787 return 5;
16788 if (p[0] == 's' && p[1] == ':')
16789 return 2;
16790 return 0;
16791}
16792
16793/*
16794 * Return TRUE if "p" starts with "<SID>" or "s:".
16795 * Only works if eval_fname_script() returned non-zero for "p"!
16796 */
16797 static int
16798eval_fname_sid(p)
16799 char_u *p;
16800{
16801 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
16802}
16803
16804/*
16805 * List the head of the function: "name(arg1, arg2)".
16806 */
16807 static void
16808list_func_head(fp, indent)
16809 ufunc_T *fp;
16810 int indent;
16811{
16812 int j;
16813
16814 msg_start();
16815 if (indent)
16816 MSG_PUTS(" ");
16817 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016818 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819 {
16820 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016821 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822 }
16823 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016824 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016826 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 {
16828 if (j)
16829 MSG_PUTS(", ");
16830 msg_puts(FUNCARG(fp, j));
16831 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016832 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833 {
16834 if (j)
16835 MSG_PUTS(", ");
16836 MSG_PUTS("...");
16837 }
16838 msg_putchar(')');
16839}
16840
16841/*
16842 * Find a function by name, return pointer to it in ufuncs.
16843 * Return NULL for unknown function.
16844 */
16845 static ufunc_T *
16846find_func(name)
16847 char_u *name;
16848{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016849 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016851 hi = hash_find(&func_hashtab, name);
16852 if (!HASHITEM_EMPTY(hi))
16853 return HI2UF(hi);
16854 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855}
16856
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016857#if defined(EXITFREE) || defined(PROTO)
16858 void
16859free_all_functions()
16860{
16861 hashitem_T *hi;
16862
16863 /* Need to start all over every time, because func_free() may change the
16864 * hash table. */
16865 while (func_hashtab.ht_used > 0)
16866 for (hi = func_hashtab.ht_array; ; ++hi)
16867 if (!HASHITEM_EMPTY(hi))
16868 {
16869 func_free(HI2UF(hi));
16870 break;
16871 }
16872}
16873#endif
16874
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016875/*
16876 * Return TRUE if a function "name" exists.
16877 */
16878 static int
16879function_exists(name)
16880 char_u *name;
16881{
16882 char_u *p = name;
16883 int n = FALSE;
16884
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016885 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016886 if (p != NULL)
16887 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016888 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016889 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016890 else
16891 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016892 vim_free(p);
16893 }
16894 return n;
16895}
16896
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016897/*
16898 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016899 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016900 */
16901 static int
16902builtin_function(name)
16903 char_u *name;
16904{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016905 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
16906 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016907}
16908
Bram Moolenaar05159a02005-02-26 23:04:13 +000016909#if defined(FEAT_PROFILE) || defined(PROTO)
16910/*
16911 * Start profiling function "fp".
16912 */
16913 static void
16914func_do_profile(fp)
16915 ufunc_T *fp;
16916{
16917 fp->uf_tm_count = 0;
16918 profile_zero(&fp->uf_tm_self);
16919 profile_zero(&fp->uf_tm_total);
16920 if (fp->uf_tml_count == NULL)
16921 fp->uf_tml_count = (int *)alloc_clear((unsigned)
16922 (sizeof(int) * fp->uf_lines.ga_len));
16923 if (fp->uf_tml_total == NULL)
16924 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
16925 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16926 if (fp->uf_tml_self == NULL)
16927 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
16928 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16929 fp->uf_tml_idx = -1;
16930 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
16931 || fp->uf_tml_self == NULL)
16932 return; /* out of memory */
16933
16934 fp->uf_profiling = TRUE;
16935}
16936
16937/*
16938 * Dump the profiling results for all functions in file "fd".
16939 */
16940 void
16941func_dump_profile(fd)
16942 FILE *fd;
16943{
16944 hashitem_T *hi;
16945 int todo;
16946 ufunc_T *fp;
16947 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000016948 ufunc_T **sorttab;
16949 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016950
16951 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000016952 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
16953
Bram Moolenaar05159a02005-02-26 23:04:13 +000016954 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
16955 {
16956 if (!HASHITEM_EMPTY(hi))
16957 {
16958 --todo;
16959 fp = HI2UF(hi);
16960 if (fp->uf_profiling)
16961 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016962 if (sorttab != NULL)
16963 sorttab[st_len++] = fp;
16964
Bram Moolenaar05159a02005-02-26 23:04:13 +000016965 if (fp->uf_name[0] == K_SPECIAL)
16966 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
16967 else
16968 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
16969 if (fp->uf_tm_count == 1)
16970 fprintf(fd, "Called 1 time\n");
16971 else
16972 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
16973 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
16974 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
16975 fprintf(fd, "\n");
16976 fprintf(fd, "count total (s) self (s)\n");
16977
16978 for (i = 0; i < fp->uf_lines.ga_len; ++i)
16979 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016980 prof_func_line(fd, fp->uf_tml_count[i],
16981 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016982 fprintf(fd, "%s\n", FUNCLINE(fp, i));
16983 }
16984 fprintf(fd, "\n");
16985 }
16986 }
16987 }
Bram Moolenaar73830342005-02-28 22:48:19 +000016988
16989 if (sorttab != NULL && st_len > 0)
16990 {
16991 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16992 prof_total_cmp);
16993 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
16994 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16995 prof_self_cmp);
16996 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
16997 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016998}
Bram Moolenaar73830342005-02-28 22:48:19 +000016999
17000 static void
17001prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17002 FILE *fd;
17003 ufunc_T **sorttab;
17004 int st_len;
17005 char *title;
17006 int prefer_self; /* when equal print only self time */
17007{
17008 int i;
17009 ufunc_T *fp;
17010
17011 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17012 fprintf(fd, "count total (s) self (s) function\n");
17013 for (i = 0; i < 20 && i < st_len; ++i)
17014 {
17015 fp = sorttab[i];
17016 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17017 prefer_self);
17018 if (fp->uf_name[0] == K_SPECIAL)
17019 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17020 else
17021 fprintf(fd, " %s()\n", fp->uf_name);
17022 }
17023 fprintf(fd, "\n");
17024}
17025
17026/*
17027 * Print the count and times for one function or function line.
17028 */
17029 static void
17030prof_func_line(fd, count, total, self, prefer_self)
17031 FILE *fd;
17032 int count;
17033 proftime_T *total;
17034 proftime_T *self;
17035 int prefer_self; /* when equal print only self time */
17036{
17037 if (count > 0)
17038 {
17039 fprintf(fd, "%5d ", count);
17040 if (prefer_self && profile_equal(total, self))
17041 fprintf(fd, " ");
17042 else
17043 fprintf(fd, "%s ", profile_msg(total));
17044 if (!prefer_self && profile_equal(total, self))
17045 fprintf(fd, " ");
17046 else
17047 fprintf(fd, "%s ", profile_msg(self));
17048 }
17049 else
17050 fprintf(fd, " ");
17051}
17052
17053/*
17054 * Compare function for total time sorting.
17055 */
17056 static int
17057#ifdef __BORLANDC__
17058_RTLENTRYF
17059#endif
17060prof_total_cmp(s1, s2)
17061 const void *s1;
17062 const void *s2;
17063{
17064 ufunc_T *p1, *p2;
17065
17066 p1 = *(ufunc_T **)s1;
17067 p2 = *(ufunc_T **)s2;
17068 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17069}
17070
17071/*
17072 * Compare function for self time sorting.
17073 */
17074 static int
17075#ifdef __BORLANDC__
17076_RTLENTRYF
17077#endif
17078prof_self_cmp(s1, s2)
17079 const void *s1;
17080 const void *s2;
17081{
17082 ufunc_T *p1, *p2;
17083
17084 p1 = *(ufunc_T **)s1;
17085 p2 = *(ufunc_T **)s2;
17086 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17087}
17088
Bram Moolenaar05159a02005-02-26 23:04:13 +000017089#endif
17090
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017091/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017092 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017093 * Return TRUE if a package was loaded.
17094 */
17095 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017096script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017097 char_u *name;
17098{
17099 char_u *p;
17100 char_u *scriptname;
17101 int ret = FALSE;
17102
17103 /* If there is no colon after name[1] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017104 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017105 if (p == NULL || p <= name + 2)
17106 return FALSE;
17107
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017108 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
17109 scriptname = autoload_name(name);
17110 if (cmd_runtime(scriptname, FALSE) == OK)
17111 ret = TRUE;
17112
17113 vim_free(scriptname);
17114 return ret;
17115}
17116
17117/*
17118 * Return the autoload script name for a function or variable name.
17119 * Returns NULL when out of memory.
17120 */
17121 static char_u *
17122autoload_name(name)
17123 char_u *name;
17124{
17125 char_u *p;
17126 char_u *scriptname;
17127
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017128 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017129 scriptname = alloc((unsigned)(STRLEN(name) + 14));
17130 if (scriptname == NULL)
17131 return FALSE;
17132 STRCPY(scriptname, "autoload/");
17133 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017134 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017135 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017136 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017137 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017138 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017139}
17140
Bram Moolenaar071d4272004-06-13 20:20:40 +000017141#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
17142
17143/*
17144 * Function given to ExpandGeneric() to obtain the list of user defined
17145 * function names.
17146 */
17147 char_u *
17148get_user_func_name(xp, idx)
17149 expand_T *xp;
17150 int idx;
17151{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017152 static long_u done;
17153 static hashitem_T *hi;
17154 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155
17156 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017158 done = 0;
17159 hi = func_hashtab.ht_array;
17160 }
17161 if (done < func_hashtab.ht_used)
17162 {
17163 if (done++ > 0)
17164 ++hi;
17165 while (HASHITEM_EMPTY(hi))
17166 ++hi;
17167 fp = HI2UF(hi);
17168
17169 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
17170 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171
17172 cat_func_name(IObuff, fp);
17173 if (xp->xp_context != EXPAND_USER_FUNC)
17174 {
17175 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017176 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177 STRCAT(IObuff, ")");
17178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179 return IObuff;
17180 }
17181 return NULL;
17182}
17183
17184#endif /* FEAT_CMDL_COMPL */
17185
17186/*
17187 * Copy the function name of "fp" to buffer "buf".
17188 * "buf" must be able to hold the function name plus three bytes.
17189 * Takes care of script-local function names.
17190 */
17191 static void
17192cat_func_name(buf, fp)
17193 char_u *buf;
17194 ufunc_T *fp;
17195{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017196 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197 {
17198 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017199 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 }
17201 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017202 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203}
17204
17205/*
17206 * ":delfunction {name}"
17207 */
17208 void
17209ex_delfunction(eap)
17210 exarg_T *eap;
17211{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017212 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 char_u *p;
17214 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017215 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017216
17217 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017218 name = trans_function_name(&p, eap->skip, 0, &fudi);
17219 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017221 {
17222 if (fudi.fd_dict != NULL && !eap->skip)
17223 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226 if (!ends_excmd(*skipwhite(p)))
17227 {
17228 vim_free(name);
17229 EMSG(_(e_trailing));
17230 return;
17231 }
17232 eap->nextcmd = check_nextcmd(p);
17233 if (eap->nextcmd != NULL)
17234 *p = NUL;
17235
17236 if (!eap->skip)
17237 fp = find_func(name);
17238 vim_free(name);
17239
17240 if (!eap->skip)
17241 {
17242 if (fp == NULL)
17243 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017244 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245 return;
17246 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017247 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248 {
17249 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
17250 return;
17251 }
17252
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017253 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017255 /* Delete the dict item that refers to the function, it will
17256 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017257 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017259 else
17260 func_free(fp);
17261 }
17262}
17263
17264/*
17265 * Free a function and remove it from the list of functions.
17266 */
17267 static void
17268func_free(fp)
17269 ufunc_T *fp;
17270{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017271 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017272
17273 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017274 ga_clear_strings(&(fp->uf_args));
17275 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000017276#ifdef FEAT_PROFILE
17277 vim_free(fp->uf_tml_count);
17278 vim_free(fp->uf_tml_total);
17279 vim_free(fp->uf_tml_self);
17280#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017281
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017282 /* remove the function from the function hashtable */
17283 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
17284 if (HASHITEM_EMPTY(hi))
17285 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017286 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017287 hash_remove(&func_hashtab, hi);
17288
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017289 vim_free(fp);
17290}
17291
17292/*
17293 * Unreference a Function: decrement the reference count and free it when it
17294 * becomes zero. Only for numbered functions.
17295 */
17296 static void
17297func_unref(name)
17298 char_u *name;
17299{
17300 ufunc_T *fp;
17301
17302 if (name != NULL && isdigit(*name))
17303 {
17304 fp = find_func(name);
17305 if (fp == NULL)
17306 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017307 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017308 {
17309 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017310 * when "uf_calls" becomes zero. */
17311 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017312 func_free(fp);
17313 }
17314 }
17315}
17316
17317/*
17318 * Count a reference to a Function.
17319 */
17320 static void
17321func_ref(name)
17322 char_u *name;
17323{
17324 ufunc_T *fp;
17325
17326 if (name != NULL && isdigit(*name))
17327 {
17328 fp = find_func(name);
17329 if (fp == NULL)
17330 EMSG2(_(e_intern2), "func_ref()");
17331 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017332 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 }
17334}
17335
17336/*
17337 * Call a user function.
17338 */
17339 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000017340call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017341 ufunc_T *fp; /* pointer to function */
17342 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000017343 typval_T *argvars; /* arguments */
17344 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345 linenr_T firstline; /* first line of range */
17346 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000017347 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348{
Bram Moolenaar33570922005-01-25 22:26:29 +000017349 char_u *save_sourcing_name;
17350 linenr_T save_sourcing_lnum;
17351 scid_T save_current_SID;
17352 funccall_T fc;
17353 funccall_T *save_fcp = current_funccal;
17354 int save_did_emsg;
17355 static int depth = 0;
17356 dictitem_T *v;
17357 int fixvar_idx = 0; /* index in fixvar[] */
17358 int i;
17359 int ai;
17360 char_u numbuf[NUMBUFLEN];
17361 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017362#ifdef FEAT_PROFILE
17363 proftime_T wait_start;
17364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365
17366 /* If depth of calling is getting too high, don't execute the function */
17367 if (depth >= p_mfd)
17368 {
17369 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017370 rettv->v_type = VAR_NUMBER;
17371 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 return;
17373 }
17374 ++depth;
17375
17376 line_breakcheck(); /* check for CTRL-C hit */
17377
Bram Moolenaar33570922005-01-25 22:26:29 +000017378 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017380 fc.rettv = rettv;
17381 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382 fc.linenr = 0;
17383 fc.returned = FALSE;
17384 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017386 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 fc.dbg_tick = debug_tick;
17388
Bram Moolenaar33570922005-01-25 22:26:29 +000017389 /*
17390 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
17391 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
17392 * each argument variable and saves a lot of time.
17393 */
17394 /*
17395 * Init l: variables.
17396 */
17397 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000017398 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017399 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017400 /* Set l:self to "selfdict". */
17401 v = &fc.fixvar[fixvar_idx++].var;
17402 STRCPY(v->di_key, "self");
17403 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
17404 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
17405 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017406 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000017407 v->di_tv.vval.v_dict = selfdict;
17408 ++selfdict->dv_refcount;
17409 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017410
Bram Moolenaar33570922005-01-25 22:26:29 +000017411 /*
17412 * Init a: variables.
17413 * Set a:0 to "argcount".
17414 * Set a:000 to a list with room for the "..." arguments.
17415 */
17416 init_var_dict(&fc.l_avars, &fc.l_avars_var);
17417 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017418 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000017419 v = &fc.fixvar[fixvar_idx++].var;
17420 STRCPY(v->di_key, "000");
17421 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17422 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17423 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017424 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017425 v->di_tv.vval.v_list = &fc.l_varlist;
17426 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
17427 fc.l_varlist.lv_refcount = 99999;
17428
17429 /*
17430 * Set a:firstline to "firstline" and a:lastline to "lastline".
17431 * Set a:name to named arguments.
17432 * Set a:N to the "..." arguments.
17433 */
17434 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
17435 (varnumber_T)firstline);
17436 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
17437 (varnumber_T)lastline);
17438 for (i = 0; i < argcount; ++i)
17439 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017440 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017441 if (ai < 0)
17442 /* named argument a:name */
17443 name = FUNCARG(fp, i);
17444 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017445 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017446 /* "..." argument a:1, a:2, etc. */
17447 sprintf((char *)numbuf, "%d", ai + 1);
17448 name = numbuf;
17449 }
17450 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
17451 {
17452 v = &fc.fixvar[fixvar_idx++].var;
17453 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17454 }
17455 else
17456 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017457 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17458 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000017459 if (v == NULL)
17460 break;
17461 v->di_flags = DI_FLAGS_RO;
17462 }
17463 STRCPY(v->di_key, name);
17464 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17465
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017466 /* Note: the values are copied directly to avoid alloc/free.
17467 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017468 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017469 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017470
17471 if (ai >= 0 && ai < MAX_FUNC_ARGS)
17472 {
17473 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
17474 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017475 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017476 }
17477 }
17478
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 /* Don't redraw while executing the function. */
17480 ++RedrawingDisabled;
17481 save_sourcing_name = sourcing_name;
17482 save_sourcing_lnum = sourcing_lnum;
17483 sourcing_lnum = 1;
17484 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017485 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486 if (sourcing_name != NULL)
17487 {
17488 if (save_sourcing_name != NULL
17489 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
17490 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
17491 else
17492 STRCPY(sourcing_name, "function ");
17493 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
17494
17495 if (p_verbose >= 12)
17496 {
17497 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017498 verbose_enter_scroll();
17499
Bram Moolenaar555b2802005-05-19 21:08:39 +000017500 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501 if (p_verbose >= 14)
17502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000017504 char_u numbuf[NUMBUFLEN];
17505 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506
17507 msg_puts((char_u *)"(");
17508 for (i = 0; i < argcount; ++i)
17509 {
17510 if (i > 0)
17511 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017512 if (argvars[i].v_type == VAR_NUMBER)
17513 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514 else
17515 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017516 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017517 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017519 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520 }
17521 }
17522 msg_puts((char_u *)")");
17523 }
17524 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017525
17526 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527 --no_wait_return;
17528 }
17529 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017530#ifdef FEAT_PROFILE
17531 if (do_profiling)
17532 {
17533 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
17534 func_do_profile(fp);
17535 if (fp->uf_profiling
17536 || (save_fcp != NULL && &save_fcp->func->uf_profiling))
17537 {
17538 ++fp->uf_tm_count;
17539 profile_start(&fp->uf_tm_start);
17540 profile_zero(&fp->uf_tm_children);
17541 }
17542 script_prof_save(&wait_start);
17543 }
17544#endif
17545
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017547 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548 save_did_emsg = did_emsg;
17549 did_emsg = FALSE;
17550
17551 /* call do_cmdline() to execute the lines */
17552 do_cmdline(NULL, get_func_line, (void *)&fc,
17553 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
17554
17555 --RedrawingDisabled;
17556
17557 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017558 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017560 clear_tv(rettv);
17561 rettv->v_type = VAR_NUMBER;
17562 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563 }
17564
Bram Moolenaar05159a02005-02-26 23:04:13 +000017565#ifdef FEAT_PROFILE
17566 if (fp->uf_profiling || (save_fcp != NULL && &save_fcp->func->uf_profiling))
17567 {
17568 profile_end(&fp->uf_tm_start);
17569 profile_sub_wait(&wait_start, &fp->uf_tm_start);
17570 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
17571 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
17572 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
17573 if (save_fcp != NULL && &save_fcp->func->uf_profiling)
17574 {
17575 profile_add(&save_fcp->func->uf_tm_children, &fp->uf_tm_start);
17576 profile_add(&save_fcp->func->uf_tml_children, &fp->uf_tm_start);
17577 }
17578 }
17579#endif
17580
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581 /* when being verbose, mention the return value */
17582 if (p_verbose >= 12)
17583 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017585 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000017588 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017589 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000017590 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
17591 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017592 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017594 char_u buf[MSG_BUF_LEN];
17595 char_u numbuf[NUMBUFLEN];
17596 char_u *tofree;
17597
Bram Moolenaar555b2802005-05-19 21:08:39 +000017598 /* The value may be very long. Skip the middle part, so that we
17599 * have some idea how it starts and ends. smsg() would always
17600 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000017601 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017602 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000017603 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017604 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 }
17606 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017607
17608 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609 --no_wait_return;
17610 }
17611
17612 vim_free(sourcing_name);
17613 sourcing_name = save_sourcing_name;
17614 sourcing_lnum = save_sourcing_lnum;
17615 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017616#ifdef FEAT_PROFILE
17617 if (do_profiling)
17618 script_prof_restore(&wait_start);
17619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620
17621 if (p_verbose >= 12 && sourcing_name != NULL)
17622 {
17623 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017624 verbose_enter_scroll();
17625
Bram Moolenaar555b2802005-05-19 21:08:39 +000017626 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017628
17629 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630 --no_wait_return;
17631 }
17632
17633 did_emsg |= save_did_emsg;
17634 current_funccal = save_fcp;
17635
Bram Moolenaar33570922005-01-25 22:26:29 +000017636 /* The a: variables typevals were not alloced, only free the allocated
17637 * variables. */
17638 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
17639
17640 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641 --depth;
17642}
17643
17644/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017645 * Add a number variable "name" to dict "dp" with value "nr".
17646 */
17647 static void
17648add_nr_var(dp, v, name, nr)
17649 dict_T *dp;
17650 dictitem_T *v;
17651 char *name;
17652 varnumber_T nr;
17653{
17654 STRCPY(v->di_key, name);
17655 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17656 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
17657 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017658 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017659 v->di_tv.vval.v_number = nr;
17660}
17661
17662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 * ":return [expr]"
17664 */
17665 void
17666ex_return(eap)
17667 exarg_T *eap;
17668{
17669 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017670 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671 int returning = FALSE;
17672
17673 if (current_funccal == NULL)
17674 {
17675 EMSG(_("E133: :return not inside a function"));
17676 return;
17677 }
17678
17679 if (eap->skip)
17680 ++emsg_skip;
17681
17682 eap->nextcmd = NULL;
17683 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017684 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685 {
17686 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017687 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017689 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690 }
17691 /* It's safer to return also on error. */
17692 else if (!eap->skip)
17693 {
17694 /*
17695 * Return unless the expression evaluation has been cancelled due to an
17696 * aborting error, an interrupt, or an exception.
17697 */
17698 if (!aborting())
17699 returning = do_return(eap, FALSE, TRUE, NULL);
17700 }
17701
17702 /* When skipping or the return gets pending, advance to the next command
17703 * in this line (!returning). Otherwise, ignore the rest of the line.
17704 * Following lines will be ignored by get_func_line(). */
17705 if (returning)
17706 eap->nextcmd = NULL;
17707 else if (eap->nextcmd == NULL) /* no argument */
17708 eap->nextcmd = check_nextcmd(arg);
17709
17710 if (eap->skip)
17711 --emsg_skip;
17712}
17713
17714/*
17715 * Return from a function. Possibly makes the return pending. Also called
17716 * for a pending return at the ":endtry" or after returning from an extra
17717 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000017718 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017719 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720 * FALSE when the return gets pending.
17721 */
17722 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017723do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724 exarg_T *eap;
17725 int reanimate;
17726 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017727 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728{
17729 int idx;
17730 struct condstack *cstack = eap->cstack;
17731
17732 if (reanimate)
17733 /* Undo the return. */
17734 current_funccal->returned = FALSE;
17735
17736 /*
17737 * Cleanup (and inactivate) conditionals, but stop when a try conditional
17738 * not in its finally clause (which then is to be executed next) is found.
17739 * In this case, make the ":return" pending for execution at the ":endtry".
17740 * Otherwise, return normally.
17741 */
17742 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
17743 if (idx >= 0)
17744 {
17745 cstack->cs_pending[idx] = CSTP_RETURN;
17746
17747 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017748 /* A pending return again gets pending. "rettv" points to an
17749 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017751 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752 else
17753 {
17754 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017755 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017757 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017759 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 {
17761 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017762 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017763 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764 else
17765 EMSG(_(e_outofmem));
17766 }
17767 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017768 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769
17770 if (reanimate)
17771 {
17772 /* The pending return value could be overwritten by a ":return"
17773 * without argument in a finally clause; reset the default
17774 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017775 current_funccal->rettv->v_type = VAR_NUMBER;
17776 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777 }
17778 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017779 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780 }
17781 else
17782 {
17783 current_funccal->returned = TRUE;
17784
17785 /* If the return is carried out now, store the return value. For
17786 * a return immediately after reanimation, the value is already
17787 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017788 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017790 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000017791 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017793 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017794 }
17795 }
17796
17797 return idx < 0;
17798}
17799
17800/*
17801 * Free the variable with a pending return value.
17802 */
17803 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017804discard_pending_return(rettv)
17805 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806{
Bram Moolenaar33570922005-01-25 22:26:29 +000017807 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808}
17809
17810/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017811 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812 * is an allocated string. Used by report_pending() for verbose messages.
17813 */
17814 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017815get_return_cmd(rettv)
17816 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017818 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017819 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017820 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017822 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017823 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017824 if (s == NULL)
17825 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017826
17827 STRCPY(IObuff, ":return ");
17828 STRNCPY(IObuff + 8, s, IOSIZE - 8);
17829 if (STRLEN(s) + 8 >= IOSIZE)
17830 STRCPY(IObuff + IOSIZE - 4, "...");
17831 vim_free(tofree);
17832 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833}
17834
17835/*
17836 * Get next function line.
17837 * Called by do_cmdline() to get the next line.
17838 * Returns allocated string, or NULL for end of function.
17839 */
17840/* ARGSUSED */
17841 char_u *
17842get_func_line(c, cookie, indent)
17843 int c; /* not used */
17844 void *cookie;
17845 int indent; /* not used */
17846{
Bram Moolenaar33570922005-01-25 22:26:29 +000017847 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017848 ufunc_T *fp = fcp->func;
17849 char_u *retval;
17850 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851
17852 /* If breakpoints have been added/deleted need to check for it. */
17853 if (fcp->dbg_tick != debug_tick)
17854 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017855 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856 sourcing_lnum);
17857 fcp->dbg_tick = debug_tick;
17858 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017859#ifdef FEAT_PROFILE
17860 if (do_profiling)
17861 func_line_end(cookie);
17862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863
Bram Moolenaar05159a02005-02-26 23:04:13 +000017864 gap = &fp->uf_lines;
17865 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866 retval = NULL;
17867 else if (fcp->returned || fcp->linenr >= gap->ga_len)
17868 retval = NULL;
17869 else
17870 {
17871 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
17872 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017873#ifdef FEAT_PROFILE
17874 if (do_profiling)
17875 func_line_start(cookie);
17876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877 }
17878
17879 /* Did we encounter a breakpoint? */
17880 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
17881 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017882 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017884 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885 sourcing_lnum);
17886 fcp->dbg_tick = debug_tick;
17887 }
17888
17889 return retval;
17890}
17891
Bram Moolenaar05159a02005-02-26 23:04:13 +000017892#if defined(FEAT_PROFILE) || defined(PROTO)
17893/*
17894 * Called when starting to read a function line.
17895 * "sourcing_lnum" must be correct!
17896 * When skipping lines it may not actually be executed, but we won't find out
17897 * until later and we need to store the time now.
17898 */
17899 void
17900func_line_start(cookie)
17901 void *cookie;
17902{
17903 funccall_T *fcp = (funccall_T *)cookie;
17904 ufunc_T *fp = fcp->func;
17905
17906 if (fp->uf_profiling && sourcing_lnum >= 1
17907 && sourcing_lnum <= fp->uf_lines.ga_len)
17908 {
17909 fp->uf_tml_idx = sourcing_lnum - 1;
17910 fp->uf_tml_execed = FALSE;
17911 profile_start(&fp->uf_tml_start);
17912 profile_zero(&fp->uf_tml_children);
17913 profile_get_wait(&fp->uf_tml_wait);
17914 }
17915}
17916
17917/*
17918 * Called when actually executing a function line.
17919 */
17920 void
17921func_line_exec(cookie)
17922 void *cookie;
17923{
17924 funccall_T *fcp = (funccall_T *)cookie;
17925 ufunc_T *fp = fcp->func;
17926
17927 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17928 fp->uf_tml_execed = TRUE;
17929}
17930
17931/*
17932 * Called when done with a function line.
17933 */
17934 void
17935func_line_end(cookie)
17936 void *cookie;
17937{
17938 funccall_T *fcp = (funccall_T *)cookie;
17939 ufunc_T *fp = fcp->func;
17940
17941 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17942 {
17943 if (fp->uf_tml_execed)
17944 {
17945 ++fp->uf_tml_count[fp->uf_tml_idx];
17946 profile_end(&fp->uf_tml_start);
17947 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
17948 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
17949 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
17950 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
17951 }
17952 fp->uf_tml_idx = -1;
17953 }
17954}
17955#endif
17956
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957/*
17958 * Return TRUE if the currently active function should be ended, because a
17959 * return was encountered or an error occured. Used inside a ":while".
17960 */
17961 int
17962func_has_ended(cookie)
17963 void *cookie;
17964{
Bram Moolenaar33570922005-01-25 22:26:29 +000017965 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017966
17967 /* Ignore the "abort" flag if the abortion behavior has been changed due to
17968 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017969 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970 || fcp->returned);
17971}
17972
17973/*
17974 * return TRUE if cookie indicates a function which "abort"s on errors.
17975 */
17976 int
17977func_has_abort(cookie)
17978 void *cookie;
17979{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017980 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981}
17982
17983#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
17984typedef enum
17985{
17986 VAR_FLAVOUR_DEFAULT,
17987 VAR_FLAVOUR_SESSION,
17988 VAR_FLAVOUR_VIMINFO
17989} var_flavour_T;
17990
17991static var_flavour_T var_flavour __ARGS((char_u *varname));
17992
17993 static var_flavour_T
17994var_flavour(varname)
17995 char_u *varname;
17996{
17997 char_u *p = varname;
17998
17999 if (ASCII_ISUPPER(*p))
18000 {
18001 while (*(++p))
18002 if (ASCII_ISLOWER(*p))
18003 return VAR_FLAVOUR_SESSION;
18004 return VAR_FLAVOUR_VIMINFO;
18005 }
18006 else
18007 return VAR_FLAVOUR_DEFAULT;
18008}
18009#endif
18010
18011#if defined(FEAT_VIMINFO) || defined(PROTO)
18012/*
18013 * Restore global vars that start with a capital from the viminfo file
18014 */
18015 int
18016read_viminfo_varlist(virp, writing)
18017 vir_T *virp;
18018 int writing;
18019{
18020 char_u *tab;
18021 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018022 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023
18024 if (!writing && (find_viminfo_parameter('!') != NULL))
18025 {
18026 tab = vim_strchr(virp->vir_line + 1, '\t');
18027 if (tab != NULL)
18028 {
18029 *tab++ = '\0'; /* isolate the variable name */
18030 if (*tab == 'S') /* string var */
18031 is_string = TRUE;
18032
18033 tab = vim_strchr(tab, '\t');
18034 if (tab != NULL)
18035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 if (is_string)
18037 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018038 tv.v_type = VAR_STRING;
18039 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 }
18042 else
18043 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018044 tv.v_type = VAR_NUMBER;
18045 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018047 set_var(virp->vir_line + 1, &tv, FALSE);
18048 if (is_string)
18049 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 }
18051 }
18052 }
18053
18054 return viminfo_readline(virp);
18055}
18056
18057/*
18058 * Write global vars that start with a capital to the viminfo file
18059 */
18060 void
18061write_viminfo_varlist(fp)
18062 FILE *fp;
18063{
Bram Moolenaar33570922005-01-25 22:26:29 +000018064 hashitem_T *hi;
18065 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018066 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018067 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018068 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018069 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018070 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071
18072 if (find_viminfo_parameter('!') == NULL)
18073 return;
18074
18075 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018076
Bram Moolenaar33570922005-01-25 22:26:29 +000018077 todo = globvarht.ht_used;
18078 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018080 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018082 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018083 this_var = HI2DI(hi);
18084 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018085 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018086 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018087 {
18088 case VAR_STRING: s = "STR"; break;
18089 case VAR_NUMBER: s = "NUM"; break;
18090 default: continue;
18091 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018092 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018093 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18094 if (p != NULL)
18095 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018096 vim_free(tofree);
18097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098 }
18099 }
18100}
18101#endif
18102
18103#if defined(FEAT_SESSION) || defined(PROTO)
18104 int
18105store_session_globals(fd)
18106 FILE *fd;
18107{
Bram Moolenaar33570922005-01-25 22:26:29 +000018108 hashitem_T *hi;
18109 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018110 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111 char_u *p, *t;
18112
Bram Moolenaar33570922005-01-25 22:26:29 +000018113 todo = globvarht.ht_used;
18114 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018116 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018118 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018119 this_var = HI2DI(hi);
18120 if ((this_var->di_tv.v_type == VAR_NUMBER
18121 || this_var->di_tv.v_type == VAR_STRING)
18122 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018123 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018124 /* Escape special characters with a backslash. Turn a LF and
18125 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018126 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018127 (char_u *)"\\\"\n\r");
18128 if (p == NULL) /* out of memory */
18129 break;
18130 for (t = p; *t != NUL; ++t)
18131 if (*t == '\n')
18132 *t = 'n';
18133 else if (*t == '\r')
18134 *t = 'r';
18135 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000018136 this_var->di_key,
18137 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18138 : ' ',
18139 p,
18140 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18141 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000018142 || put_eol(fd) == FAIL)
18143 {
18144 vim_free(p);
18145 return FAIL;
18146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 vim_free(p);
18148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 }
18150 }
18151 return OK;
18152}
18153#endif
18154
18155#endif /* FEAT_EVAL */
18156
18157#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
18158
18159
18160#ifdef WIN3264
18161/*
18162 * Functions for ":8" filename modifier: get 8.3 version of a filename.
18163 */
18164static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18165static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
18166static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18167
18168/*
18169 * Get the short pathname of a file.
18170 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
18171 */
18172 static int
18173get_short_pathname(fnamep, bufp, fnamelen)
18174 char_u **fnamep;
18175 char_u **bufp;
18176 int *fnamelen;
18177{
18178 int l,len;
18179 char_u *newbuf;
18180
18181 len = *fnamelen;
18182
18183 l = GetShortPathName(*fnamep, *fnamep, len);
18184 if (l > len - 1)
18185 {
18186 /* If that doesn't work (not enough space), then save the string
18187 * and try again with a new buffer big enough
18188 */
18189 newbuf = vim_strnsave(*fnamep, l);
18190 if (newbuf == NULL)
18191 return 0;
18192
18193 vim_free(*bufp);
18194 *fnamep = *bufp = newbuf;
18195
18196 l = GetShortPathName(*fnamep,*fnamep,l+1);
18197
18198 /* Really should always succeed, as the buffer is big enough */
18199 }
18200
18201 *fnamelen = l;
18202 return 1;
18203}
18204
18205/*
18206 * Create a short path name. Returns the length of the buffer it needs.
18207 * Doesn't copy over the end of the buffer passed in.
18208 */
18209 static int
18210shortpath_for_invalid_fname(fname, bufp, fnamelen)
18211 char_u **fname;
18212 char_u **bufp;
18213 int *fnamelen;
18214{
18215 char_u *s, *p, *pbuf2, *pbuf3;
18216 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018217 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218
18219 /* Make a copy */
18220 len2 = *fnamelen;
18221 pbuf2 = vim_strnsave(*fname, len2);
18222 pbuf3 = NULL;
18223
18224 s = pbuf2 + len2 - 1; /* Find the end */
18225 slen = 1;
18226 plen = len2;
18227
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018228 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 {
18230 --s;
18231 ++slen;
18232 --plen;
18233 }
18234
18235 do
18236 {
18237 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018238 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239 {
18240 --s;
18241 ++slen;
18242 --plen;
18243 }
18244 if (s <= pbuf2)
18245 break;
18246
18247 /* Remeber the character that is about to be blatted */
18248 ch = *s;
18249 *s = 0; /* get_short_pathname requires a null-terminated string */
18250
18251 /* Try it in situ */
18252 p = pbuf2;
18253 if (!get_short_pathname(&p, &pbuf3, &plen))
18254 {
18255 vim_free(pbuf2);
18256 return -1;
18257 }
18258 *s = ch; /* Preserve the string */
18259 } while (plen == 0);
18260
18261 if (plen > 0)
18262 {
18263 /* Remeber the length of the new string. */
18264 *fnamelen = len = plen + slen;
18265 vim_free(*bufp);
18266 if (len > len2)
18267 {
18268 /* If there's not enough space in the currently allocated string,
18269 * then copy it to a buffer big enough.
18270 */
18271 *fname= *bufp = vim_strnsave(p, len);
18272 if (*fname == NULL)
18273 return -1;
18274 }
18275 else
18276 {
18277 /* Transfer pbuf2 to being the main buffer (it's big enough) */
18278 *fname = *bufp = pbuf2;
18279 if (p != pbuf2)
18280 strncpy(*fname, p, plen);
18281 pbuf2 = NULL;
18282 }
18283 /* Concat the next bit */
18284 strncpy(*fname + plen, s, slen);
18285 (*fname)[len] = '\0';
18286 }
18287 vim_free(pbuf3);
18288 vim_free(pbuf2);
18289 return 0;
18290}
18291
18292/*
18293 * Get a pathname for a partial path.
18294 */
18295 static int
18296shortpath_for_partial(fnamep, bufp, fnamelen)
18297 char_u **fnamep;
18298 char_u **bufp;
18299 int *fnamelen;
18300{
18301 int sepcount, len, tflen;
18302 char_u *p;
18303 char_u *pbuf, *tfname;
18304 int hasTilde;
18305
18306 /* Count up the path seperators from the RHS.. so we know which part
18307 * of the path to return.
18308 */
18309 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018310 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311 if (vim_ispathsep(*p))
18312 ++sepcount;
18313
18314 /* Need full path first (use expand_env() to remove a "~/") */
18315 hasTilde = (**fnamep == '~');
18316 if (hasTilde)
18317 pbuf = tfname = expand_env_save(*fnamep);
18318 else
18319 pbuf = tfname = FullName_save(*fnamep, FALSE);
18320
18321 len = tflen = STRLEN(tfname);
18322
18323 if (!get_short_pathname(&tfname, &pbuf, &len))
18324 return -1;
18325
18326 if (len == 0)
18327 {
18328 /* Don't have a valid filename, so shorten the rest of the
18329 * path if we can. This CAN give us invalid 8.3 filenames, but
18330 * there's not a lot of point in guessing what it might be.
18331 */
18332 len = tflen;
18333 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
18334 return -1;
18335 }
18336
18337 /* Count the paths backward to find the beginning of the desired string. */
18338 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018339 {
18340#ifdef FEAT_MBYTE
18341 if (has_mbyte)
18342 p -= mb_head_off(tfname, p);
18343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018344 if (vim_ispathsep(*p))
18345 {
18346 if (sepcount == 0 || (hasTilde && sepcount == 1))
18347 break;
18348 else
18349 sepcount --;
18350 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352 if (hasTilde)
18353 {
18354 --p;
18355 if (p >= tfname)
18356 *p = '~';
18357 else
18358 return -1;
18359 }
18360 else
18361 ++p;
18362
18363 /* Copy in the string - p indexes into tfname - allocated at pbuf */
18364 vim_free(*bufp);
18365 *fnamelen = (int)STRLEN(p);
18366 *bufp = pbuf;
18367 *fnamep = p;
18368
18369 return 0;
18370}
18371#endif /* WIN3264 */
18372
18373/*
18374 * Adjust a filename, according to a string of modifiers.
18375 * *fnamep must be NUL terminated when called. When returning, the length is
18376 * determined by *fnamelen.
18377 * Returns valid flags.
18378 * When there is an error, *fnamep is set to NULL.
18379 */
18380 int
18381modify_fname(src, usedlen, fnamep, bufp, fnamelen)
18382 char_u *src; /* string with modifiers */
18383 int *usedlen; /* characters after src that are used */
18384 char_u **fnamep; /* file name so far */
18385 char_u **bufp; /* buffer for allocated file name or NULL */
18386 int *fnamelen; /* length of fnamep */
18387{
18388 int valid = 0;
18389 char_u *tail;
18390 char_u *s, *p, *pbuf;
18391 char_u dirname[MAXPATHL];
18392 int c;
18393 int has_fullname = 0;
18394#ifdef WIN3264
18395 int has_shortname = 0;
18396#endif
18397
18398repeat:
18399 /* ":p" - full path/file_name */
18400 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
18401 {
18402 has_fullname = 1;
18403
18404 valid |= VALID_PATH;
18405 *usedlen += 2;
18406
18407 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
18408 if ((*fnamep)[0] == '~'
18409#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
18410 && ((*fnamep)[1] == '/'
18411# ifdef BACKSLASH_IN_FILENAME
18412 || (*fnamep)[1] == '\\'
18413# endif
18414 || (*fnamep)[1] == NUL)
18415
18416#endif
18417 )
18418 {
18419 *fnamep = expand_env_save(*fnamep);
18420 vim_free(*bufp); /* free any allocated file name */
18421 *bufp = *fnamep;
18422 if (*fnamep == NULL)
18423 return -1;
18424 }
18425
18426 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018427 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018428 {
18429 if (vim_ispathsep(*p)
18430 && p[1] == '.'
18431 && (p[2] == NUL
18432 || vim_ispathsep(p[2])
18433 || (p[2] == '.'
18434 && (p[3] == NUL || vim_ispathsep(p[3])))))
18435 break;
18436 }
18437
18438 /* FullName_save() is slow, don't use it when not needed. */
18439 if (*p != NUL || !vim_isAbsName(*fnamep))
18440 {
18441 *fnamep = FullName_save(*fnamep, *p != NUL);
18442 vim_free(*bufp); /* free any allocated file name */
18443 *bufp = *fnamep;
18444 if (*fnamep == NULL)
18445 return -1;
18446 }
18447
18448 /* Append a path separator to a directory. */
18449 if (mch_isdir(*fnamep))
18450 {
18451 /* Make room for one or two extra characters. */
18452 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
18453 vim_free(*bufp); /* free any allocated file name */
18454 *bufp = *fnamep;
18455 if (*fnamep == NULL)
18456 return -1;
18457 add_pathsep(*fnamep);
18458 }
18459 }
18460
18461 /* ":." - path relative to the current directory */
18462 /* ":~" - path relative to the home directory */
18463 /* ":8" - shortname path - postponed till after */
18464 while (src[*usedlen] == ':'
18465 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
18466 {
18467 *usedlen += 2;
18468 if (c == '8')
18469 {
18470#ifdef WIN3264
18471 has_shortname = 1; /* Postpone this. */
18472#endif
18473 continue;
18474 }
18475 pbuf = NULL;
18476 /* Need full path first (use expand_env() to remove a "~/") */
18477 if (!has_fullname)
18478 {
18479 if (c == '.' && **fnamep == '~')
18480 p = pbuf = expand_env_save(*fnamep);
18481 else
18482 p = pbuf = FullName_save(*fnamep, FALSE);
18483 }
18484 else
18485 p = *fnamep;
18486
18487 has_fullname = 0;
18488
18489 if (p != NULL)
18490 {
18491 if (c == '.')
18492 {
18493 mch_dirname(dirname, MAXPATHL);
18494 s = shorten_fname(p, dirname);
18495 if (s != NULL)
18496 {
18497 *fnamep = s;
18498 if (pbuf != NULL)
18499 {
18500 vim_free(*bufp); /* free any allocated file name */
18501 *bufp = pbuf;
18502 pbuf = NULL;
18503 }
18504 }
18505 }
18506 else
18507 {
18508 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
18509 /* Only replace it when it starts with '~' */
18510 if (*dirname == '~')
18511 {
18512 s = vim_strsave(dirname);
18513 if (s != NULL)
18514 {
18515 *fnamep = s;
18516 vim_free(*bufp);
18517 *bufp = s;
18518 }
18519 }
18520 }
18521 vim_free(pbuf);
18522 }
18523 }
18524
18525 tail = gettail(*fnamep);
18526 *fnamelen = (int)STRLEN(*fnamep);
18527
18528 /* ":h" - head, remove "/file_name", can be repeated */
18529 /* Don't remove the first "/" or "c:\" */
18530 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
18531 {
18532 valid |= VALID_HEAD;
18533 *usedlen += 2;
18534 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018535 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 --tail;
18537 *fnamelen = (int)(tail - *fnamep);
18538#ifdef VMS
18539 if (*fnamelen > 0)
18540 *fnamelen += 1; /* the path separator is part of the path */
18541#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018542 while (tail > s && !after_pathsep(s, tail))
18543 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544 }
18545
18546 /* ":8" - shortname */
18547 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
18548 {
18549 *usedlen += 2;
18550#ifdef WIN3264
18551 has_shortname = 1;
18552#endif
18553 }
18554
18555#ifdef WIN3264
18556 /* Check shortname after we have done 'heads' and before we do 'tails'
18557 */
18558 if (has_shortname)
18559 {
18560 pbuf = NULL;
18561 /* Copy the string if it is shortened by :h */
18562 if (*fnamelen < (int)STRLEN(*fnamep))
18563 {
18564 p = vim_strnsave(*fnamep, *fnamelen);
18565 if (p == 0)
18566 return -1;
18567 vim_free(*bufp);
18568 *bufp = *fnamep = p;
18569 }
18570
18571 /* Split into two implementations - makes it easier. First is where
18572 * there isn't a full name already, second is where there is.
18573 */
18574 if (!has_fullname && !vim_isAbsName(*fnamep))
18575 {
18576 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
18577 return -1;
18578 }
18579 else
18580 {
18581 int l;
18582
18583 /* Simple case, already have the full-name
18584 * Nearly always shorter, so try first time. */
18585 l = *fnamelen;
18586 if (!get_short_pathname(fnamep, bufp, &l))
18587 return -1;
18588
18589 if (l == 0)
18590 {
18591 /* Couldn't find the filename.. search the paths.
18592 */
18593 l = *fnamelen;
18594 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
18595 return -1;
18596 }
18597 *fnamelen = l;
18598 }
18599 }
18600#endif /* WIN3264 */
18601
18602 /* ":t" - tail, just the basename */
18603 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
18604 {
18605 *usedlen += 2;
18606 *fnamelen -= (int)(tail - *fnamep);
18607 *fnamep = tail;
18608 }
18609
18610 /* ":e" - extension, can be repeated */
18611 /* ":r" - root, without extension, can be repeated */
18612 while (src[*usedlen] == ':'
18613 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
18614 {
18615 /* find a '.' in the tail:
18616 * - for second :e: before the current fname
18617 * - otherwise: The last '.'
18618 */
18619 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
18620 s = *fnamep - 2;
18621 else
18622 s = *fnamep + *fnamelen - 1;
18623 for ( ; s > tail; --s)
18624 if (s[0] == '.')
18625 break;
18626 if (src[*usedlen + 1] == 'e') /* :e */
18627 {
18628 if (s > tail)
18629 {
18630 *fnamelen += (int)(*fnamep - (s + 1));
18631 *fnamep = s + 1;
18632#ifdef VMS
18633 /* cut version from the extension */
18634 s = *fnamep + *fnamelen - 1;
18635 for ( ; s > *fnamep; --s)
18636 if (s[0] == ';')
18637 break;
18638 if (s > *fnamep)
18639 *fnamelen = s - *fnamep;
18640#endif
18641 }
18642 else if (*fnamep <= tail)
18643 *fnamelen = 0;
18644 }
18645 else /* :r */
18646 {
18647 if (s > tail) /* remove one extension */
18648 *fnamelen = (int)(s - *fnamep);
18649 }
18650 *usedlen += 2;
18651 }
18652
18653 /* ":s?pat?foo?" - substitute */
18654 /* ":gs?pat?foo?" - global substitute */
18655 if (src[*usedlen] == ':'
18656 && (src[*usedlen + 1] == 's'
18657 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
18658 {
18659 char_u *str;
18660 char_u *pat;
18661 char_u *sub;
18662 int sep;
18663 char_u *flags;
18664 int didit = FALSE;
18665
18666 flags = (char_u *)"";
18667 s = src + *usedlen + 2;
18668 if (src[*usedlen + 1] == 'g')
18669 {
18670 flags = (char_u *)"g";
18671 ++s;
18672 }
18673
18674 sep = *s++;
18675 if (sep)
18676 {
18677 /* find end of pattern */
18678 p = vim_strchr(s, sep);
18679 if (p != NULL)
18680 {
18681 pat = vim_strnsave(s, (int)(p - s));
18682 if (pat != NULL)
18683 {
18684 s = p + 1;
18685 /* find end of substitution */
18686 p = vim_strchr(s, sep);
18687 if (p != NULL)
18688 {
18689 sub = vim_strnsave(s, (int)(p - s));
18690 str = vim_strnsave(*fnamep, *fnamelen);
18691 if (sub != NULL && str != NULL)
18692 {
18693 *usedlen = (int)(p + 1 - src);
18694 s = do_string_sub(str, pat, sub, flags);
18695 if (s != NULL)
18696 {
18697 *fnamep = s;
18698 *fnamelen = (int)STRLEN(s);
18699 vim_free(*bufp);
18700 *bufp = s;
18701 didit = TRUE;
18702 }
18703 }
18704 vim_free(sub);
18705 vim_free(str);
18706 }
18707 vim_free(pat);
18708 }
18709 }
18710 /* after using ":s", repeat all the modifiers */
18711 if (didit)
18712 goto repeat;
18713 }
18714 }
18715
18716 return valid;
18717}
18718
18719/*
18720 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
18721 * "flags" can be "g" to do a global substitute.
18722 * Returns an allocated string, NULL for error.
18723 */
18724 char_u *
18725do_string_sub(str, pat, sub, flags)
18726 char_u *str;
18727 char_u *pat;
18728 char_u *sub;
18729 char_u *flags;
18730{
18731 int sublen;
18732 regmatch_T regmatch;
18733 int i;
18734 int do_all;
18735 char_u *tail;
18736 garray_T ga;
18737 char_u *ret;
18738 char_u *save_cpo;
18739
18740 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
18741 save_cpo = p_cpo;
18742 p_cpo = (char_u *)"";
18743
18744 ga_init2(&ga, 1, 200);
18745
18746 do_all = (flags[0] == 'g');
18747
18748 regmatch.rm_ic = p_ic;
18749 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18750 if (regmatch.regprog != NULL)
18751 {
18752 tail = str;
18753 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
18754 {
18755 /*
18756 * Get some space for a temporary buffer to do the substitution
18757 * into. It will contain:
18758 * - The text up to where the match is.
18759 * - The substituted text.
18760 * - The text after the match.
18761 */
18762 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
18763 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
18764 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
18765 {
18766 ga_clear(&ga);
18767 break;
18768 }
18769
18770 /* copy the text up to where the match is */
18771 i = (int)(regmatch.startp[0] - tail);
18772 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
18773 /* add the substituted text */
18774 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
18775 + ga.ga_len + i, TRUE, TRUE, FALSE);
18776 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777 /* avoid getting stuck on a match with an empty string */
18778 if (tail == regmatch.endp[0])
18779 {
18780 if (*tail == NUL)
18781 break;
18782 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
18783 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784 }
18785 else
18786 {
18787 tail = regmatch.endp[0];
18788 if (*tail == NUL)
18789 break;
18790 }
18791 if (!do_all)
18792 break;
18793 }
18794
18795 if (ga.ga_data != NULL)
18796 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
18797
18798 vim_free(regmatch.regprog);
18799 }
18800
18801 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
18802 ga_clear(&ga);
18803 p_cpo = save_cpo;
18804
18805 return ret;
18806}
18807
18808#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */