blob: 3737126de32576c1e0f1a158312bffb40ad3b6a0 [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 Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000340};
341
342/* shorthand */
343#define vv_type vv_di.di_tv.v_type
344#define vv_nr vv_di.di_tv.vval.v_number
345#define vv_str vv_di.di_tv.vval.v_string
346#define vv_tv vv_di.di_tv
347
348/*
349 * The v: variables are stored in dictionary "vimvardict".
350 * "vimvars_var" is the variable that is used for the "l:" scope.
351 */
352static dict_T vimvardict;
353static dictitem_T vimvars_var;
354#define vimvarht vimvardict.dv_hashtab
355
Bram Moolenaara40058a2005-07-11 22:42:07 +0000356static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
357static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
358#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
359static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
360#endif
361static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
362static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
363static char_u *skip_var_one __ARGS((char_u *arg));
364static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
365static void list_glob_vars __ARGS((void));
366static void list_buf_vars __ARGS((void));
367static void list_win_vars __ARGS((void));
368static void list_vim_vars __ARGS((void));
369static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
370static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
371static int check_changedtick __ARGS((char_u *arg));
372static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
373static void clear_lval __ARGS((lval_T *lp));
374static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
375static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
376static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
377static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
378static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
379static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
380static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
381static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
382static void item_lock __ARGS((typval_T *tv, int deep, int lock));
383static int tv_islocked __ARGS((typval_T *tv));
384
Bram Moolenaar33570922005-01-25 22:26:29 +0000385static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
386static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
387static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
388static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
389static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
390static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000394static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000395static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
397static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
399static list_T *list_alloc __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000400static void list_free __ARGS((list_T *l));
401static listitem_T *listitem_alloc __ARGS((void));
402static void listitem_free __ARGS((listitem_T *item));
403static void listitem_remove __ARGS((list_T *l, listitem_T *item));
404static long list_len __ARGS((list_T *l));
405static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
406static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
407static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000408static listitem_T *list_find __ARGS((list_T *l, long n));
409static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static void list_append __ARGS((list_T *l, listitem_T *item));
411static int list_append_tv __ARGS((list_T *l, typval_T *tv));
412static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
413static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
414static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000415static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
417static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000418static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000419static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
420static void set_ref_in_list __ARGS((list_T *l, int copyID));
421static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static void dict_unref __ARGS((dict_T *d));
423static void dict_free __ARGS((dict_T *d));
424static dictitem_T *dictitem_alloc __ARGS((char_u *key));
425static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
426static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
427static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000428static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static int dict_add __ARGS((dict_T *d, dictitem_T *item));
430static long dict_len __ARGS((dict_T *d));
431static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
432static char_u *dict2string __ARGS((typval_T *tv));
433static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
435static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
436static char_u *string_quote __ARGS((char_u *str, int function));
437static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
438static int find_internal_func __ARGS((char_u *name));
439static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
440static 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));
441static 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 +0000442static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443
444static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000463#if defined(FEAT_INS_EXPAND)
464static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
466#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000467static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
472static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000498static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000499static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000500static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000513static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000514static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000541static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000558static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000562#ifdef vim_mkdir
563static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
564#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000569static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000570static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000571static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000583static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000584static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000590static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000595static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000596static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000598static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
599#ifdef HAVE_STRFTIME
600static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
601#endif
602static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000614static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000616static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000617static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000631static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
634static int get_env_len __ARGS((char_u **arg));
635static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000636static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000637static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
638#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
639#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
640 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000641static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000642static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000643static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000644static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
645static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000646static typval_T *alloc_tv __ARGS((void));
647static typval_T *alloc_string_tv __ARGS((char_u *string));
648static void free_tv __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static void init_tv __ARGS((typval_T *varp));
650static long get_tv_number __ARGS((typval_T *varp));
651static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000652static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static char_u *get_tv_string __ARGS((typval_T *varp));
654static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000655static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000657static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
659static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
660static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
661static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
662static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
663static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
664static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000665static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000667static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
669static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
670static int eval_fname_script __ARGS((char_u *p));
671static int eval_fname_sid __ARGS((char_u *p));
672static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static ufunc_T *find_func __ARGS((char_u *name));
674static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000675static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000676#ifdef FEAT_PROFILE
677static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000678static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
679static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
680static int
681# ifdef __BORLANDC__
682 _RTLENTRYF
683# endif
684 prof_total_cmp __ARGS((const void *s1, const void *s2));
685static int
686# ifdef __BORLANDC__
687 _RTLENTRYF
688# endif
689 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000690#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000691static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000692static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000693static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void func_free __ARGS((ufunc_T *fp));
695static void func_unref __ARGS((char_u *name));
696static void func_ref __ARGS((char_u *name));
697static 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));
698static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
699
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000700/* Character used as separated in autoload function/variable names. */
701#define AUTOLOAD_CHAR '#'
702
Bram Moolenaar33570922005-01-25 22:26:29 +0000703/*
704 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000705 */
706 void
707eval_init()
708{
Bram Moolenaar33570922005-01-25 22:26:29 +0000709 int i;
710 struct vimvar *p;
711
712 init_var_dict(&globvardict, &globvars_var);
713 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000714 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000715 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000716
717 for (i = 0; i < VV_LEN; ++i)
718 {
719 p = &vimvars[i];
720 STRCPY(p->vv_di.di_key, p->vv_name);
721 if (p->vv_flags & VV_RO)
722 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
723 else if (p->vv_flags & VV_RO_SBX)
724 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
725 else
726 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000727
728 /* add to v: scope dict, unless the value is not always available */
729 if (p->vv_type != VAR_UNKNOWN)
730 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000731 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000732 /* add to compat scope dict */
733 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000734 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000735}
736
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000737#if defined(EXITFREE) || defined(PROTO)
738 void
739eval_clear()
740{
741 int i;
742 struct vimvar *p;
743
744 for (i = 0; i < VV_LEN; ++i)
745 {
746 p = &vimvars[i];
747 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000748 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000749 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000750 p->vv_di.di_tv.vval.v_string = NULL;
751 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000752 }
753 hash_clear(&vimvarht);
754 hash_clear(&compat_hashtab);
755
756 /* script-local variables */
757 for (i = 1; i <= ga_scripts.ga_len; ++i)
758 vars_clear(&SCRIPT_VARS(i));
759 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000760 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000761
762 /* global variables */
763 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000764
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000765 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000766 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000767 hash_clear(&func_hashtab);
768
769 /* unreferenced lists and dicts */
770 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000771}
772#endif
773
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 * Return the name of the executed function.
776 */
777 char_u *
778func_name(cookie)
779 void *cookie;
780{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000781 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782}
783
784/*
785 * Return the address holding the next breakpoint line for a funccall cookie.
786 */
787 linenr_T *
788func_breakpoint(cookie)
789 void *cookie;
790{
Bram Moolenaar33570922005-01-25 22:26:29 +0000791 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792}
793
794/*
795 * Return the address holding the debug tick for a funccall cookie.
796 */
797 int *
798func_dbg_tick(cookie)
799 void *cookie;
800{
Bram Moolenaar33570922005-01-25 22:26:29 +0000801 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802}
803
804/*
805 * Return the nesting level for a funccall cookie.
806 */
807 int
808func_level(cookie)
809 void *cookie;
810{
Bram Moolenaar33570922005-01-25 22:26:29 +0000811 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812}
813
814/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000815funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
817/*
818 * Return TRUE when a function was ended by a ":return" command.
819 */
820 int
821current_func_returned()
822{
823 return current_funccal->returned;
824}
825
826
827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 * Set an internal variable to a string value. Creates the variable if it does
829 * not already exist.
830 */
831 void
832set_internal_string_var(name, value)
833 char_u *name;
834 char_u *value;
835{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000836 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000837 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
839 val = vim_strsave(value);
840 if (val != NULL)
841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000842 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000843 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000845 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000846 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
848 }
849}
850
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000851static lval_T *redir_lval = NULL;
852static char_u *redir_endp = NULL;
853static char_u *redir_varname = NULL;
854
855/*
856 * Start recording command output to a variable
857 * Returns OK if successfully completed the setup. FAIL otherwise.
858 */
859 int
860var_redir_start(name, append)
861 char_u *name;
862 int append; /* append to an existing variable */
863{
864 int save_emsg;
865 int err;
866 typval_T tv;
867
868 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000869 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000870 {
871 EMSG(_(e_invarg));
872 return FAIL;
873 }
874
875 redir_varname = vim_strsave(name);
876 if (redir_varname == NULL)
877 return FAIL;
878
879 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
880 if (redir_lval == NULL)
881 {
882 var_redir_stop();
883 return FAIL;
884 }
885
886 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000887 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
888 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000889 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
890 {
891 if (redir_endp != NULL && *redir_endp != NUL)
892 /* Trailing characters are present after the variable name */
893 EMSG(_(e_trailing));
894 else
895 EMSG(_(e_invarg));
896 var_redir_stop();
897 return FAIL;
898 }
899
900 /* check if we can write to the variable: set it to or append an empty
901 * string */
902 save_emsg = did_emsg;
903 did_emsg = FALSE;
904 tv.v_type = VAR_STRING;
905 tv.vval.v_string = (char_u *)"";
906 if (append)
907 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
908 else
909 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
910 err = did_emsg;
911 did_emsg += save_emsg;
912 if (err)
913 {
914 var_redir_stop();
915 return FAIL;
916 }
917 if (redir_lval->ll_newkey != NULL)
918 {
919 /* Dictionary item was created, don't do it again. */
920 vim_free(redir_lval->ll_newkey);
921 redir_lval->ll_newkey = NULL;
922 }
923
924 return OK;
925}
926
927/*
928 * Append "value[len]" to the variable set by var_redir_start().
929 */
930 void
931var_redir_str(value, len)
932 char_u *value;
933 int len;
934{
935 char_u *val;
936 typval_T tv;
937 int save_emsg;
938 int err;
939
940 if (redir_lval == NULL)
941 return;
942
943 if (len == -1)
944 /* Append the entire string */
945 val = vim_strsave(value);
946 else
947 /* Append only the specified number of characters */
948 val = vim_strnsave(value, len);
949 if (val == NULL)
950 return;
951
952 tv.v_type = VAR_STRING;
953 tv.vval.v_string = val;
954
955 save_emsg = did_emsg;
956 did_emsg = FALSE;
957 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
958 err = did_emsg;
959 did_emsg += save_emsg;
960 if (err)
961 var_redir_stop();
962
963 vim_free(tv.vval.v_string);
964}
965
966/*
967 * Stop redirecting command output to a variable.
968 */
969 void
970var_redir_stop()
971{
972 if (redir_lval != NULL)
973 {
974 clear_lval(redir_lval);
975 vim_free(redir_lval);
976 redir_lval = NULL;
977 }
978 vim_free(redir_varname);
979 redir_varname = NULL;
980}
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982# if defined(FEAT_MBYTE) || defined(PROTO)
983 int
984eval_charconvert(enc_from, enc_to, fname_from, fname_to)
985 char_u *enc_from;
986 char_u *enc_to;
987 char_u *fname_from;
988 char_u *fname_to;
989{
990 int err = FALSE;
991
992 set_vim_var_string(VV_CC_FROM, enc_from, -1);
993 set_vim_var_string(VV_CC_TO, enc_to, -1);
994 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
995 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
996 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
997 err = TRUE;
998 set_vim_var_string(VV_CC_FROM, NULL, -1);
999 set_vim_var_string(VV_CC_TO, NULL, -1);
1000 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1001 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1002
1003 if (err)
1004 return FAIL;
1005 return OK;
1006}
1007# endif
1008
1009# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1010 int
1011eval_printexpr(fname, args)
1012 char_u *fname;
1013 char_u *args;
1014{
1015 int err = FALSE;
1016
1017 set_vim_var_string(VV_FNAME_IN, fname, -1);
1018 set_vim_var_string(VV_CMDARG, args, -1);
1019 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1020 err = TRUE;
1021 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1022 set_vim_var_string(VV_CMDARG, NULL, -1);
1023
1024 if (err)
1025 {
1026 mch_remove(fname);
1027 return FAIL;
1028 }
1029 return OK;
1030}
1031# endif
1032
1033# if defined(FEAT_DIFF) || defined(PROTO)
1034 void
1035eval_diff(origfile, newfile, outfile)
1036 char_u *origfile;
1037 char_u *newfile;
1038 char_u *outfile;
1039{
1040 int err = FALSE;
1041
1042 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1043 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1044 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1045 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1046 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1047 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1048 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1049}
1050
1051 void
1052eval_patch(origfile, difffile, outfile)
1053 char_u *origfile;
1054 char_u *difffile;
1055 char_u *outfile;
1056{
1057 int err;
1058
1059 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1060 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1061 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1062 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1063 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1064 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1065 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1066}
1067# endif
1068
1069/*
1070 * Top level evaluation function, returning a boolean.
1071 * Sets "error" to TRUE if there was an error.
1072 * Return TRUE or FALSE.
1073 */
1074 int
1075eval_to_bool(arg, error, nextcmd, skip)
1076 char_u *arg;
1077 int *error;
1078 char_u **nextcmd;
1079 int skip; /* only parse, don't execute */
1080{
Bram Moolenaar33570922005-01-25 22:26:29 +00001081 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 int retval = FALSE;
1083
1084 if (skip)
1085 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001086 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 else
1089 {
1090 *error = FALSE;
1091 if (!skip)
1092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001093 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001094 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 }
1096 }
1097 if (skip)
1098 --emsg_skip;
1099
1100 return retval;
1101}
1102
1103/*
1104 * Top level evaluation function, returning a string. If "skip" is TRUE,
1105 * only parsing to "nextcmd" is done, without reporting errors. Return
1106 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1107 */
1108 char_u *
1109eval_to_string_skip(arg, nextcmd, skip)
1110 char_u *arg;
1111 char_u **nextcmd;
1112 int skip; /* only parse, don't execute */
1113{
Bram Moolenaar33570922005-01-25 22:26:29 +00001114 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 char_u *retval;
1116
1117 if (skip)
1118 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001119 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 retval = NULL;
1121 else
1122 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001123 retval = vim_strsave(get_tv_string(&tv));
1124 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
1126 if (skip)
1127 --emsg_skip;
1128
1129 return retval;
1130}
1131
1132/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001133 * Skip over an expression at "*pp".
1134 * Return FAIL for an error, OK otherwise.
1135 */
1136 int
1137skip_expr(pp)
1138 char_u **pp;
1139{
Bram Moolenaar33570922005-01-25 22:26:29 +00001140 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001141
1142 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001143 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001144}
1145
1146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 * Top level evaluation function, returning a string.
1148 * Return pointer to allocated memory, or NULL for failure.
1149 */
1150 char_u *
1151eval_to_string(arg, nextcmd)
1152 char_u *arg;
1153 char_u **nextcmd;
1154{
Bram Moolenaar33570922005-01-25 22:26:29 +00001155 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 char_u *retval;
1157
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001158 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 retval = NULL;
1160 else
1161 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001162 retval = vim_strsave(get_tv_string(&tv));
1163 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 }
1165
1166 return retval;
1167}
1168
1169/*
1170 * Call eval_to_string() with "sandbox" set and not using local variables.
1171 */
1172 char_u *
1173eval_to_string_safe(arg, nextcmd)
1174 char_u *arg;
1175 char_u **nextcmd;
1176{
1177 char_u *retval;
1178 void *save_funccalp;
1179
1180 save_funccalp = save_funccal();
1181 ++sandbox;
1182 retval = eval_to_string(arg, nextcmd);
1183 --sandbox;
1184 restore_funccal(save_funccalp);
1185 return retval;
1186}
1187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188/*
1189 * Top level evaluation function, returning a number.
1190 * Evaluates "expr" silently.
1191 * Returns -1 for an error.
1192 */
1193 int
1194eval_to_number(expr)
1195 char_u *expr;
1196{
Bram Moolenaar33570922005-01-25 22:26:29 +00001197 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001199 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
1201 ++emsg_off;
1202
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001203 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 retval = -1;
1205 else
1206 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001207 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001208 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 }
1210 --emsg_off;
1211
1212 return retval;
1213}
1214
Bram Moolenaara40058a2005-07-11 22:42:07 +00001215/*
1216 * Prepare v: variable "idx" to be used.
1217 * Save the current typeval in "save_tv".
1218 * When not used yet add the variable to the v: hashtable.
1219 */
1220 static void
1221prepare_vimvar(idx, save_tv)
1222 int idx;
1223 typval_T *save_tv;
1224{
1225 *save_tv = vimvars[idx].vv_tv;
1226 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1227 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1228}
1229
1230/*
1231 * Restore v: variable "idx" to typeval "save_tv".
1232 * When no longer defined, remove the variable from the v: hashtable.
1233 */
1234 static void
1235restore_vimvar(idx, save_tv)
1236 int idx;
1237 typval_T *save_tv;
1238{
1239 hashitem_T *hi;
1240
1241 clear_tv(&vimvars[idx].vv_tv);
1242 vimvars[idx].vv_tv = *save_tv;
1243 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1244 {
1245 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1246 if (HASHITEM_EMPTY(hi))
1247 EMSG2(_(e_intern2), "restore_vimvar()");
1248 else
1249 hash_remove(&vimvarht, hi);
1250 }
1251}
1252
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001253#if defined(FEAT_SYN_HL) || defined(PROTO)
1254/*
1255 * Evaluate an expression to a list with suggestions.
1256 * For the "expr:" part of 'spellsuggest'.
1257 */
1258 list_T *
1259eval_spell_expr(badword, expr)
1260 char_u *badword;
1261 char_u *expr;
1262{
1263 typval_T save_val;
1264 typval_T rettv;
1265 list_T *list = NULL;
1266 char_u *p = skipwhite(expr);
1267
1268 /* Set "v:val" to the bad word. */
1269 prepare_vimvar(VV_VAL, &save_val);
1270 vimvars[VV_VAL].vv_type = VAR_STRING;
1271 vimvars[VV_VAL].vv_str = badword;
1272 if (p_verbose == 0)
1273 ++emsg_off;
1274
1275 if (eval1(&p, &rettv, TRUE) == OK)
1276 {
1277 if (rettv.v_type != VAR_LIST)
1278 clear_tv(&rettv);
1279 else
1280 list = rettv.vval.v_list;
1281 }
1282
1283 if (p_verbose == 0)
1284 --emsg_off;
1285 vimvars[VV_VAL].vv_str = NULL;
1286 restore_vimvar(VV_VAL, &save_val);
1287
1288 return list;
1289}
1290
1291/*
1292 * "list" is supposed to contain two items: a word and a number. Return the
1293 * word in "pp" and the number as the return value.
1294 * Return -1 if anything isn't right.
1295 * Used to get the good word and score from the eval_spell_expr() result.
1296 */
1297 int
1298get_spellword(list, pp)
1299 list_T *list;
1300 char_u **pp;
1301{
1302 listitem_T *li;
1303
1304 li = list->lv_first;
1305 if (li == NULL)
1306 return -1;
1307 *pp = get_tv_string(&li->li_tv);
1308
1309 li = li->li_next;
1310 if (li == NULL)
1311 return -1;
1312 return get_tv_number(&li->li_tv);
1313}
1314#endif
1315
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001316/*
1317 * Top level evaluation function,
1318 */
1319 typval_T *
1320eval_expr(arg, nextcmd)
1321 char_u *arg;
1322 char_u **nextcmd;
1323{
1324 typval_T *tv;
1325
1326 tv = (typval_T *)alloc(sizeof(typval_T));
1327 if (!tv)
1328 return NULL;
1329
1330 if (eval0(arg, tv, nextcmd, TRUE) == FAIL)
1331 {
1332 vim_free(tv);
1333 return NULL;
1334 }
1335
1336 return tv;
1337}
1338
1339
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1341/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001342 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001344 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001346 static int
1347call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 char_u *func;
1349 int argc;
1350 char_u **argv;
1351 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
Bram Moolenaar33570922005-01-25 22:26:29 +00001354 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 long n;
1356 int len;
1357 int i;
1358 int doesrange;
1359 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001360 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361
Bram Moolenaar33570922005-01-25 22:26:29 +00001362 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001364 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365
1366 for (i = 0; i < argc; i++)
1367 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001368 /* Pass a NULL or empty argument as an empty string */
1369 if (argv[i] == NULL || *argv[i] == NUL)
1370 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001371 argvars[i].v_type = VAR_STRING;
1372 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001373 continue;
1374 }
1375
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 /* Recognize a number argument, the others must be strings. */
1377 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1378 if (len != 0 && len == (int)STRLEN(argv[i]))
1379 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001380 argvars[i].v_type = VAR_NUMBER;
1381 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
1383 else
1384 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001385 argvars[i].v_type = VAR_STRING;
1386 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 }
1389
1390 if (safe)
1391 {
1392 save_funccalp = save_funccal();
1393 ++sandbox;
1394 }
1395
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001396 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1397 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001399 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 if (safe)
1401 {
1402 --sandbox;
1403 restore_funccal(save_funccalp);
1404 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001405 vim_free(argvars);
1406
1407 if (ret == FAIL)
1408 clear_tv(rettv);
1409
1410 return ret;
1411}
1412
1413/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001414 * Call vimL function "func" and return the result as a string.
1415 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001416 * Uses argv[argc] for the function arguments.
1417 */
1418 void *
1419call_func_retstr(func, argc, argv, safe)
1420 char_u *func;
1421 int argc;
1422 char_u **argv;
1423 int safe; /* use the sandbox */
1424{
1425 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001426 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001427
1428 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1429 return NULL;
1430
1431 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001432 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 return retval;
1434}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001435
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001436#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001437/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001438 * Call vimL function "func" and return the result as a number.
1439 * Returns -1 when calling the function fails.
1440 * Uses argv[argc] for the function arguments.
1441 */
1442 long
1443call_func_retnr(func, argc, argv, safe)
1444 char_u *func;
1445 int argc;
1446 char_u **argv;
1447 int safe; /* use the sandbox */
1448{
1449 typval_T rettv;
1450 long retval;
1451
1452 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1453 return -1;
1454
1455 retval = get_tv_number_chk(&rettv, NULL);
1456 clear_tv(&rettv);
1457 return retval;
1458}
1459#endif
1460
1461/*
1462 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001463 * Uses argv[argc] for the function arguments.
1464 */
1465 void *
1466call_func_retlist(func, argc, argv, safe)
1467 char_u *func;
1468 int argc;
1469 char_u **argv;
1470 int safe; /* use the sandbox */
1471{
1472 typval_T rettv;
1473
1474 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1475 return NULL;
1476
1477 if (rettv.v_type != VAR_LIST)
1478 {
1479 clear_tv(&rettv);
1480 return NULL;
1481 }
1482
1483 return rettv.vval.v_list;
1484}
1485
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486#endif
1487
1488/*
1489 * Save the current function call pointer, and set it to NULL.
1490 * Used when executing autocommands and for ":source".
1491 */
1492 void *
1493save_funccal()
1494{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001495 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 current_funccal = NULL;
1498 return (void *)fc;
1499}
1500
1501 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001502restore_funccal(vfc)
1503 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001505 funccall_T *fc = (funccall_T *)vfc;
1506
1507 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508}
1509
Bram Moolenaar05159a02005-02-26 23:04:13 +00001510#if defined(FEAT_PROFILE) || defined(PROTO)
1511/*
1512 * Prepare profiling for entering a child or something else that is not
1513 * counted for the script/function itself.
1514 * Should always be called in pair with prof_child_exit().
1515 */
1516 void
1517prof_child_enter(tm)
1518 proftime_T *tm; /* place to store waittime */
1519{
1520 funccall_T *fc = current_funccal;
1521
1522 if (fc != NULL && fc->func->uf_profiling)
1523 profile_start(&fc->prof_child);
1524 script_prof_save(tm);
1525}
1526
1527/*
1528 * Take care of time spent in a child.
1529 * Should always be called after prof_child_enter().
1530 */
1531 void
1532prof_child_exit(tm)
1533 proftime_T *tm; /* where waittime was stored */
1534{
1535 funccall_T *fc = current_funccal;
1536
1537 if (fc != NULL && fc->func->uf_profiling)
1538 {
1539 profile_end(&fc->prof_child);
1540 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1541 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1542 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1543 }
1544 script_prof_restore(tm);
1545}
1546#endif
1547
1548
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549#ifdef FEAT_FOLDING
1550/*
1551 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1552 * it in "*cp". Doesn't give error messages.
1553 */
1554 int
1555eval_foldexpr(arg, cp)
1556 char_u *arg;
1557 int *cp;
1558{
Bram Moolenaar33570922005-01-25 22:26:29 +00001559 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 int retval;
1561 char_u *s;
1562
1563 ++emsg_off;
1564 ++sandbox;
1565 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001566 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 retval = 0;
1568 else
1569 {
1570 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001571 if (tv.v_type == VAR_NUMBER)
1572 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001573 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 retval = 0;
1575 else
1576 {
1577 /* If the result is a string, check if there is a non-digit before
1578 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001579 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 if (!VIM_ISDIGIT(*s) && *s != '-')
1581 *cp = *s++;
1582 retval = atol((char *)s);
1583 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001584 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 }
1586 --emsg_off;
1587 --sandbox;
1588
1589 return retval;
1590}
1591#endif
1592
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594 * ":let" list all variable values
1595 * ":let var1 var2" list variable values
1596 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001597 * ":let var += expr" assignment command.
1598 * ":let var -= expr" assignment command.
1599 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001600 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 */
1602 void
1603ex_let(eap)
1604 exarg_T *eap;
1605{
1606 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001607 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001608 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001610 int var_count = 0;
1611 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001612 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001614 expr = skip_var_list(arg, &var_count, &semicolon);
1615 if (expr == NULL)
1616 return;
1617 expr = vim_strchr(expr, '=');
1618 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001620 /*
1621 * ":let" without "=": list variables
1622 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001623 if (*arg == '[')
1624 EMSG(_(e_invarg));
1625 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001626 /* ":let var1 var2" */
1627 arg = list_arg_vars(eap, arg);
1628 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001630 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001631 list_glob_vars();
1632 list_buf_vars();
1633 list_win_vars();
1634 list_vim_vars();
1635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 eap->nextcmd = check_nextcmd(arg);
1637 }
1638 else
1639 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001640 op[0] = '=';
1641 op[1] = NUL;
1642 if (expr > arg)
1643 {
1644 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1645 op[0] = expr[-1]; /* +=, -= or .= */
1646 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001647 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001648
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 if (eap->skip)
1650 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 if (eap->skip)
1653 {
1654 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001655 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 --emsg_skip;
1657 }
1658 else if (i != FAIL)
1659 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001661 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 }
1664 }
1665}
1666
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667/*
1668 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1669 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001670 * When "nextchars" is not NULL it points to a string with characters that
1671 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1672 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 * Returns OK or FAIL;
1674 */
1675 static int
1676ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1677 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001678 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679 int copy; /* copy values from "tv", don't move */
1680 int semicolon; /* from skip_var_list() */
1681 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001682 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001683{
1684 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001685 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001687 listitem_T *item;
1688 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689
1690 if (*arg != '[')
1691 {
1692 /*
1693 * ":let var = expr" or ":for var in list"
1694 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001695 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 return FAIL;
1697 return OK;
1698 }
1699
1700 /*
1701 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1702 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001703 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704 {
1705 EMSG(_(e_listreq));
1706 return FAIL;
1707 }
1708
1709 i = list_len(l);
1710 if (semicolon == 0 && var_count < i)
1711 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001712 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 return FAIL;
1714 }
1715 if (var_count - semicolon > i)
1716 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001717 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001718 return FAIL;
1719 }
1720
1721 item = l->lv_first;
1722 while (*arg != ']')
1723 {
1724 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001725 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001726 item = item->li_next;
1727 if (arg == NULL)
1728 return FAIL;
1729
1730 arg = skipwhite(arg);
1731 if (*arg == ';')
1732 {
1733 /* Put the rest of the list (may be empty) in the var after ';'.
1734 * Create a new list for this. */
1735 l = list_alloc();
1736 if (l == NULL)
1737 return FAIL;
1738 while (item != NULL)
1739 {
1740 list_append_tv(l, &item->li_tv);
1741 item = item->li_next;
1742 }
1743
1744 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001745 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001746 ltv.vval.v_list = l;
1747 l->lv_refcount = 1;
1748
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001749 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1750 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751 clear_tv(&ltv);
1752 if (arg == NULL)
1753 return FAIL;
1754 break;
1755 }
1756 else if (*arg != ',' && *arg != ']')
1757 {
1758 EMSG2(_(e_intern2), "ex_let_vars()");
1759 return FAIL;
1760 }
1761 }
1762
1763 return OK;
1764}
1765
1766/*
1767 * Skip over assignable variable "var" or list of variables "[var, var]".
1768 * Used for ":let varvar = expr" and ":for varvar in expr".
1769 * For "[var, var]" increment "*var_count" for each variable.
1770 * for "[var, var; var]" set "semicolon".
1771 * Return NULL for an error.
1772 */
1773 static char_u *
1774skip_var_list(arg, var_count, semicolon)
1775 char_u *arg;
1776 int *var_count;
1777 int *semicolon;
1778{
1779 char_u *p, *s;
1780
1781 if (*arg == '[')
1782 {
1783 /* "[var, var]": find the matching ']'. */
1784 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001785 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786 {
1787 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1788 s = skip_var_one(p);
1789 if (s == p)
1790 {
1791 EMSG2(_(e_invarg2), p);
1792 return NULL;
1793 }
1794 ++*var_count;
1795
1796 p = skipwhite(s);
1797 if (*p == ']')
1798 break;
1799 else if (*p == ';')
1800 {
1801 if (*semicolon == 1)
1802 {
1803 EMSG(_("Double ; in list of variables"));
1804 return NULL;
1805 }
1806 *semicolon = 1;
1807 }
1808 else if (*p != ',')
1809 {
1810 EMSG2(_(e_invarg2), p);
1811 return NULL;
1812 }
1813 }
1814 return p + 1;
1815 }
1816 else
1817 return skip_var_one(arg);
1818}
1819
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001820/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001821 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1822 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001823 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001824 static char_u *
1825skip_var_one(arg)
1826 char_u *arg;
1827{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001828 if (*arg == '@' && arg[1] != NUL)
1829 return arg + 2;
1830 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1831 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001832}
1833
Bram Moolenaara7043832005-01-21 11:56:39 +00001834/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001835 * List variables for hashtab "ht" with prefix "prefix".
1836 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001837 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001839list_hashtable_vars(ht, prefix, empty)
1840 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001841 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001842 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001843{
Bram Moolenaar33570922005-01-25 22:26:29 +00001844 hashitem_T *hi;
1845 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001846 int todo;
1847
1848 todo = ht->ht_used;
1849 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1850 {
1851 if (!HASHITEM_EMPTY(hi))
1852 {
1853 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001854 di = HI2DI(hi);
1855 if (empty || di->di_tv.v_type != VAR_STRING
1856 || di->di_tv.vval.v_string != NULL)
1857 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001858 }
1859 }
1860}
1861
1862/*
1863 * List global variables.
1864 */
1865 static void
1866list_glob_vars()
1867{
Bram Moolenaar33570922005-01-25 22:26:29 +00001868 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001869}
1870
1871/*
1872 * List buffer variables.
1873 */
1874 static void
1875list_buf_vars()
1876{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001877 char_u numbuf[NUMBUFLEN];
1878
Bram Moolenaar33570922005-01-25 22:26:29 +00001879 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001880
1881 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1882 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001883}
1884
1885/*
1886 * List window variables.
1887 */
1888 static void
1889list_win_vars()
1890{
Bram Moolenaar33570922005-01-25 22:26:29 +00001891 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001892}
1893
1894/*
1895 * List Vim variables.
1896 */
1897 static void
1898list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899{
Bram Moolenaar33570922005-01-25 22:26:29 +00001900 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901}
1902
1903/*
1904 * List variables in "arg".
1905 */
1906 static char_u *
1907list_arg_vars(eap, arg)
1908 exarg_T *eap;
1909 char_u *arg;
1910{
1911 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001912 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001914 char_u *name_start;
1915 char_u *arg_subsc;
1916 char_u *tofree;
1917 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918
1919 while (!ends_excmd(*arg) && !got_int)
1920 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001921 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001923 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001924 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1925 {
1926 emsg_severe = TRUE;
1927 EMSG(_(e_trailing));
1928 break;
1929 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001930 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001931 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001933 /* get_name_len() takes care of expanding curly braces */
1934 name_start = name = arg;
1935 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1936 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001938 /* This is mainly to keep test 49 working: when expanding
1939 * curly braces fails overrule the exception error message. */
1940 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001942 emsg_severe = TRUE;
1943 EMSG2(_(e_invarg2), arg);
1944 break;
1945 }
1946 error = TRUE;
1947 }
1948 else
1949 {
1950 if (tofree != NULL)
1951 name = tofree;
1952 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001953 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 else
1955 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001956 /* handle d.key, l[idx], f(expr) */
1957 arg_subsc = arg;
1958 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001959 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001961 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001962 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001963 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001964 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001965 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001966 case 'g': list_glob_vars(); break;
1967 case 'b': list_buf_vars(); break;
1968 case 'w': list_win_vars(); break;
1969 case 'v': list_vim_vars(); break;
1970 default:
1971 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001972 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001973 }
1974 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001975 {
1976 char_u numbuf[NUMBUFLEN];
1977 char_u *tf;
1978 int c;
1979 char_u *s;
1980
1981 s = echo_string(&tv, &tf, numbuf);
1982 c = *arg;
1983 *arg = NUL;
1984 list_one_var_a((char_u *)"",
1985 arg == arg_subsc ? name : name_start,
1986 tv.v_type, s == NULL ? (char_u *)"" : s);
1987 *arg = c;
1988 vim_free(tf);
1989 }
1990 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001991 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001992 }
1993 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001994
1995 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001996 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001997
1998 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001999 }
2000
2001 return arg;
2002}
2003
2004/*
2005 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2006 * Returns a pointer to the char just after the var name.
2007 * Returns NULL if there is an error.
2008 */
2009 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002010ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002011 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002012 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002013 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002014 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002015 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002016{
2017 int c1;
2018 char_u *name;
2019 char_u *p;
2020 char_u *arg_end = NULL;
2021 int len;
2022 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002023 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024
2025 /*
2026 * ":let $VAR = expr": Set environment variable.
2027 */
2028 if (*arg == '$')
2029 {
2030 /* Find the end of the name. */
2031 ++arg;
2032 name = arg;
2033 len = get_env_len(&arg);
2034 if (len == 0)
2035 EMSG2(_(e_invarg2), name - 1);
2036 else
2037 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002038 if (op != NULL && (*op == '+' || *op == '-'))
2039 EMSG2(_(e_letwrong), op);
2040 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042 EMSG(_(e_letunexp));
2043 else
2044 {
2045 c1 = name[len];
2046 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002047 p = get_tv_string_chk(tv);
2048 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002049 {
2050 int mustfree = FALSE;
2051 char_u *s = vim_getenv(name, &mustfree);
2052
2053 if (s != NULL)
2054 {
2055 p = tofree = concat_str(s, p);
2056 if (mustfree)
2057 vim_free(s);
2058 }
2059 }
2060 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002061 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002062 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002063 if (STRICMP(name, "HOME") == 0)
2064 init_homedir();
2065 else if (didset_vim && STRICMP(name, "VIM") == 0)
2066 didset_vim = FALSE;
2067 else if (didset_vimruntime
2068 && STRICMP(name, "VIMRUNTIME") == 0)
2069 didset_vimruntime = FALSE;
2070 arg_end = arg;
2071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002073 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002074 }
2075 }
2076 }
2077
2078 /*
2079 * ":let &option = expr": Set option value.
2080 * ":let &l:option = expr": Set local option value.
2081 * ":let &g:option = expr": Set global option value.
2082 */
2083 else if (*arg == '&')
2084 {
2085 /* Find the end of the name. */
2086 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002087 if (p == NULL || (endchars != NULL
2088 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002089 EMSG(_(e_letunexp));
2090 else
2091 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002092 long n;
2093 int opt_type;
2094 long numval;
2095 char_u *stringval = NULL;
2096 char_u *s;
2097
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098 c1 = *p;
2099 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002100
2101 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002102 s = get_tv_string_chk(tv); /* != NULL if number or string */
2103 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002104 {
2105 opt_type = get_option_value(arg, &numval,
2106 &stringval, opt_flags);
2107 if ((opt_type == 1 && *op == '.')
2108 || (opt_type == 0 && *op != '.'))
2109 EMSG2(_(e_letwrong), op);
2110 else
2111 {
2112 if (opt_type == 1) /* number */
2113 {
2114 if (*op == '+')
2115 n = numval + n;
2116 else
2117 n = numval - n;
2118 }
2119 else if (opt_type == 0 && stringval != NULL) /* string */
2120 {
2121 s = concat_str(stringval, s);
2122 vim_free(stringval);
2123 stringval = s;
2124 }
2125 }
2126 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002127 if (s != NULL)
2128 {
2129 set_option_value(arg, n, s, opt_flags);
2130 arg_end = p;
2131 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002132 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002133 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002134 }
2135 }
2136
2137 /*
2138 * ":let @r = expr": Set register contents.
2139 */
2140 else if (*arg == '@')
2141 {
2142 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002143 if (op != NULL && (*op == '+' || *op == '-'))
2144 EMSG2(_(e_letwrong), op);
2145 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002146 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147 EMSG(_(e_letunexp));
2148 else
2149 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002150 char_u *tofree = NULL;
2151 char_u *s;
2152
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002153 p = get_tv_string_chk(tv);
2154 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002155 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002156 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002157 if (s != NULL)
2158 {
2159 p = tofree = concat_str(s, p);
2160 vim_free(s);
2161 }
2162 }
2163 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002164 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002165 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002166 arg_end = arg + 1;
2167 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002168 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002169 }
2170 }
2171
2172 /*
2173 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002176 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002178 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002179
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002180 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002181 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002183 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2184 EMSG(_(e_letunexp));
2185 else
2186 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002187 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188 arg_end = p;
2189 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 }
2193
2194 else
2195 EMSG2(_(e_invarg2), arg);
2196
2197 return arg_end;
2198}
2199
2200/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002201 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2202 */
2203 static int
2204check_changedtick(arg)
2205 char_u *arg;
2206{
2207 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2208 {
2209 EMSG2(_(e_readonlyvar), arg);
2210 return TRUE;
2211 }
2212 return FALSE;
2213}
2214
2215/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 * Get an lval: variable, Dict item or List item that can be assigned a value
2217 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2218 * "name.key", "name.key[expr]" etc.
2219 * Indexing only works if "name" is an existing List or Dictionary.
2220 * "name" points to the start of the name.
2221 * If "rettv" is not NULL it points to the value to be assigned.
2222 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2223 * wrong; must end in space or cmd separator.
2224 *
2225 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002226 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002227 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 */
2229 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002230get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002232 typval_T *rettv;
2233 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 int unlet;
2235 int skip;
2236 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002237 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002240 char_u *expr_start, *expr_end;
2241 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002242 dictitem_T *v;
2243 typval_T var1;
2244 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002245 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002246 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002247 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002248 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002249 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002252 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002253
2254 if (skip)
2255 {
2256 /* When skipping just find the end of the name. */
2257 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002258 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259 }
2260
2261 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002262 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002263 if (expr_start != NULL)
2264 {
2265 /* Don't expand the name when we already know there is an error. */
2266 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2267 && *p != '[' && *p != '.')
2268 {
2269 EMSG(_(e_trailing));
2270 return NULL;
2271 }
2272
2273 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2274 if (lp->ll_exp_name == NULL)
2275 {
2276 /* Report an invalid expression in braces, unless the
2277 * expression evaluation has been cancelled due to an
2278 * aborting error, an interrupt, or an exception. */
2279 if (!aborting() && !quiet)
2280 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002281 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002282 EMSG2(_(e_invarg2), name);
2283 return NULL;
2284 }
2285 }
2286 lp->ll_name = lp->ll_exp_name;
2287 }
2288 else
2289 lp->ll_name = name;
2290
2291 /* Without [idx] or .key we are done. */
2292 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2293 return p;
2294
2295 cc = *p;
2296 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002298 if (v == NULL && !quiet)
2299 EMSG2(_(e_undefvar), lp->ll_name);
2300 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 if (v == NULL)
2302 return NULL;
2303
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 /*
2305 * Loop until no more [idx] or .key is following.
2306 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002307 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002310 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2311 && !(lp->ll_tv->v_type == VAR_DICT
2312 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 if (!quiet)
2315 EMSG(_("E689: Can only index a List or Dictionary"));
2316 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002318 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002320 if (!quiet)
2321 EMSG(_("E708: [:] must come last"));
2322 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002324
Bram Moolenaar8c711452005-01-14 21:53:12 +00002325 len = -1;
2326 if (*p == '.')
2327 {
2328 key = p + 1;
2329 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2330 ;
2331 if (len == 0)
2332 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002333 if (!quiet)
2334 EMSG(_(e_emptykey));
2335 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002336 }
2337 p = key + len;
2338 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002339 else
2340 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002341 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002342 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002343 if (*p == ':')
2344 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002345 else
2346 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002347 empty1 = FALSE;
2348 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002350 if (get_tv_string_chk(&var1) == NULL)
2351 {
2352 /* not a number or string */
2353 clear_tv(&var1);
2354 return NULL;
2355 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002356 }
2357
2358 /* Optionally get the second index [ :expr]. */
2359 if (*p == ':')
2360 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002361 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002362 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002363 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002365 if (!empty1)
2366 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002367 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002368 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 if (rettv != NULL && (rettv->v_type != VAR_LIST
2370 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002371 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002372 if (!quiet)
2373 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002374 if (!empty1)
2375 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002377 }
2378 p = skipwhite(p + 1);
2379 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002380 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002381 else
2382 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002384 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2385 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002386 if (!empty1)
2387 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002388 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002389 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002390 if (get_tv_string_chk(&var2) == NULL)
2391 {
2392 /* not a number or string */
2393 if (!empty1)
2394 clear_tv(&var1);
2395 clear_tv(&var2);
2396 return NULL;
2397 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002398 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002400 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002401 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002402 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002403
Bram Moolenaar8c711452005-01-14 21:53:12 +00002404 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002405 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 if (!quiet)
2407 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002408 if (!empty1)
2409 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002410 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002411 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002413 }
2414
2415 /* Skip to past ']'. */
2416 ++p;
2417 }
2418
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002419 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002420 {
2421 if (len == -1)
2422 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002424 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002425 if (*key == NUL)
2426 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002427 if (!quiet)
2428 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002429 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002431 }
2432 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002433 lp->ll_list = NULL;
2434 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002435 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002437 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002438 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443 if (len == -1)
2444 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002446 }
2447 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002451 if (len == -1)
2452 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 p = NULL;
2455 break;
2456 }
2457 if (len == -1)
2458 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 }
2461 else
2462 {
2463 /*
2464 * Get the number and item for the only or first index of the List.
2465 */
2466 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002468 else
2469 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002470 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002471 clear_tv(&var1);
2472 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 lp->ll_list = lp->ll_tv->vval.v_list;
2475 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2476 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 if (!quiet)
2479 EMSGN(_(e_listidx), lp->ll_n1);
2480 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002481 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002483 }
2484
2485 /*
2486 * May need to find the item or absolute index for the second
2487 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 * When no index given: "lp->ll_empty2" is TRUE.
2489 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002493 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 if (ni == NULL)
2499 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 if (!quiet)
2501 EMSGN(_(e_listidx), lp->ll_n2);
2502 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002503 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 }
2506
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2508 if (lp->ll_n1 < 0)
2509 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2510 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002511 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 if (!quiet)
2513 EMSGN(_(e_listidx), lp->ll_n2);
2514 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002515 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002516 }
2517
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002519 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 }
2521
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 return p;
2523}
2524
2525/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002526 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 */
2528 static void
2529clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002530 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531{
2532 vim_free(lp->ll_exp_name);
2533 vim_free(lp->ll_newkey);
2534}
2535
2536/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002537 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002539 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 */
2541 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002542set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002543 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002545 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002547 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548{
2549 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002550 listitem_T *ni;
2551 listitem_T *ri;
2552 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553
2554 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 cc = *endp;
2559 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002560 if (op != NULL && *op != '=')
2561 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002562 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002563
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002564 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002565 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2566 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002567 {
2568 if (tv_op(&tv, rettv, op) == OK)
2569 set_var(lp->ll_name, &tv, FALSE);
2570 clear_tv(&tv);
2571 }
2572 }
2573 else
2574 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002578 else if (tv_check_lock(lp->ll_newkey == NULL
2579 ? lp->ll_tv->v_lock
2580 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2581 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 else if (lp->ll_range)
2583 {
2584 /*
2585 * Assign the List values to the list items.
2586 */
2587 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002588 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002589 if (op != NULL && *op != '=')
2590 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2591 else
2592 {
2593 clear_tv(&lp->ll_li->li_tv);
2594 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2595 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 ri = ri->li_next;
2597 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2598 break;
2599 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 /* Need to add an empty item. */
2602 ni = listitem_alloc();
2603 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 ri = NULL;
2606 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002607 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002609 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 ni->li_tv.vval.v_number = 0;
2611 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002612 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 lp->ll_li = lp->ll_li->li_next;
2614 ++lp->ll_n1;
2615 }
2616 if (ri != NULL)
2617 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002618 else if (lp->ll_empty2
2619 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 : lp->ll_n1 != lp->ll_n2)
2621 EMSG(_("E711: List value has not enough items"));
2622 }
2623 else
2624 {
2625 /*
2626 * Assign to a List or Dictionary item.
2627 */
2628 if (lp->ll_newkey != NULL)
2629 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002630 if (op != NULL && *op != '=')
2631 {
2632 EMSG2(_(e_letwrong), op);
2633 return;
2634 }
2635
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002637 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (di == NULL)
2639 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002640 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2641 {
2642 vim_free(di);
2643 return;
2644 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002647 else if (op != NULL && *op != '=')
2648 {
2649 tv_op(lp->ll_tv, rettv, op);
2650 return;
2651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 /*
2656 * Assign the value to the variable or list item.
2657 */
2658 if (copy)
2659 copy_tv(rettv, lp->ll_tv);
2660 else
2661 {
2662 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002663 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665 }
2666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667}
2668
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002669/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002670 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2671 * Returns OK or FAIL.
2672 */
2673 static int
2674tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002675 typval_T *tv1;
2676 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002677 char_u *op;
2678{
2679 long n;
2680 char_u numbuf[NUMBUFLEN];
2681 char_u *s;
2682
2683 /* Can't do anything with a Funcref or a Dict on the right. */
2684 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2685 {
2686 switch (tv1->v_type)
2687 {
2688 case VAR_DICT:
2689 case VAR_FUNC:
2690 break;
2691
2692 case VAR_LIST:
2693 if (*op != '+' || tv2->v_type != VAR_LIST)
2694 break;
2695 /* List += List */
2696 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2697 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2698 return OK;
2699
2700 case VAR_NUMBER:
2701 case VAR_STRING:
2702 if (tv2->v_type == VAR_LIST)
2703 break;
2704 if (*op == '+' || *op == '-')
2705 {
2706 /* nr += nr or nr -= nr*/
2707 n = get_tv_number(tv1);
2708 if (*op == '+')
2709 n += get_tv_number(tv2);
2710 else
2711 n -= get_tv_number(tv2);
2712 clear_tv(tv1);
2713 tv1->v_type = VAR_NUMBER;
2714 tv1->vval.v_number = n;
2715 }
2716 else
2717 {
2718 /* str .= str */
2719 s = get_tv_string(tv1);
2720 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2721 clear_tv(tv1);
2722 tv1->v_type = VAR_STRING;
2723 tv1->vval.v_string = s;
2724 }
2725 return OK;
2726 }
2727 }
2728
2729 EMSG2(_(e_letwrong), op);
2730 return FAIL;
2731}
2732
2733/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002734 * Add a watcher to a list.
2735 */
2736 static void
2737list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002738 list_T *l;
2739 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002740{
2741 lw->lw_next = l->lv_watch;
2742 l->lv_watch = lw;
2743}
2744
2745/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002746 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002747 * No warning when it isn't found...
2748 */
2749 static void
2750list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002751 list_T *l;
2752 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002753{
Bram Moolenaar33570922005-01-25 22:26:29 +00002754 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002755
2756 lwp = &l->lv_watch;
2757 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2758 {
2759 if (lw == lwrem)
2760 {
2761 *lwp = lw->lw_next;
2762 break;
2763 }
2764 lwp = &lw->lw_next;
2765 }
2766}
2767
2768/*
2769 * Just before removing an item from a list: advance watchers to the next
2770 * item.
2771 */
2772 static void
2773list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002774 list_T *l;
2775 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002776{
Bram Moolenaar33570922005-01-25 22:26:29 +00002777 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002778
2779 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2780 if (lw->lw_item == item)
2781 lw->lw_item = item->li_next;
2782}
2783
2784/*
2785 * Evaluate the expression used in a ":for var in expr" command.
2786 * "arg" points to "var".
2787 * Set "*errp" to TRUE for an error, FALSE otherwise;
2788 * Return a pointer that holds the info. Null when there is an error.
2789 */
2790 void *
2791eval_for_line(arg, errp, nextcmdp, skip)
2792 char_u *arg;
2793 int *errp;
2794 char_u **nextcmdp;
2795 int skip;
2796{
Bram Moolenaar33570922005-01-25 22:26:29 +00002797 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002798 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002799 typval_T tv;
2800 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002801
2802 *errp = TRUE; /* default: there is an error */
2803
Bram Moolenaar33570922005-01-25 22:26:29 +00002804 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002805 if (fi == NULL)
2806 return NULL;
2807
2808 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2809 if (expr == NULL)
2810 return fi;
2811
2812 expr = skipwhite(expr);
2813 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2814 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002815 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002816 return fi;
2817 }
2818
2819 if (skip)
2820 ++emsg_skip;
2821 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2822 {
2823 *errp = FALSE;
2824 if (!skip)
2825 {
2826 l = tv.vval.v_list;
2827 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002828 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002829 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002830 clear_tv(&tv);
2831 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002832 else
2833 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002834 /* No need to increment the refcount, it's already set for the
2835 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002836 fi->fi_list = l;
2837 list_add_watch(l, &fi->fi_lw);
2838 fi->fi_lw.lw_item = l->lv_first;
2839 }
2840 }
2841 }
2842 if (skip)
2843 --emsg_skip;
2844
2845 return fi;
2846}
2847
2848/*
2849 * Use the first item in a ":for" list. Advance to the next.
2850 * Assign the values to the variable (list). "arg" points to the first one.
2851 * Return TRUE when a valid item was found, FALSE when at end of list or
2852 * something wrong.
2853 */
2854 int
2855next_for_item(fi_void, arg)
2856 void *fi_void;
2857 char_u *arg;
2858{
Bram Moolenaar33570922005-01-25 22:26:29 +00002859 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002860 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002861 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002862
2863 item = fi->fi_lw.lw_item;
2864 if (item == NULL)
2865 result = FALSE;
2866 else
2867 {
2868 fi->fi_lw.lw_item = item->li_next;
2869 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2870 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2871 }
2872 return result;
2873}
2874
2875/*
2876 * Free the structure used to store info used by ":for".
2877 */
2878 void
2879free_for_info(fi_void)
2880 void *fi_void;
2881{
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002884 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002885 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002886 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002887 list_unref(fi->fi_list);
2888 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002889 vim_free(fi);
2890}
2891
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2893
2894 void
2895set_context_for_expression(xp, arg, cmdidx)
2896 expand_T *xp;
2897 char_u *arg;
2898 cmdidx_T cmdidx;
2899{
2900 int got_eq = FALSE;
2901 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002902 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002904 if (cmdidx == CMD_let)
2905 {
2906 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002907 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002908 {
2909 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002910 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002911 {
2912 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002913 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002914 if (vim_iswhite(*p))
2915 break;
2916 }
2917 return;
2918 }
2919 }
2920 else
2921 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2922 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 while ((xp->xp_pattern = vim_strpbrk(arg,
2924 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2925 {
2926 c = *xp->xp_pattern;
2927 if (c == '&')
2928 {
2929 c = xp->xp_pattern[1];
2930 if (c == '&')
2931 {
2932 ++xp->xp_pattern;
2933 xp->xp_context = cmdidx != CMD_let || got_eq
2934 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2935 }
2936 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002939 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2940 xp->xp_pattern += 2;
2941
2942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 }
2944 else if (c == '$')
2945 {
2946 /* environment variable */
2947 xp->xp_context = EXPAND_ENV_VARS;
2948 }
2949 else if (c == '=')
2950 {
2951 got_eq = TRUE;
2952 xp->xp_context = EXPAND_EXPRESSION;
2953 }
2954 else if (c == '<'
2955 && xp->xp_context == EXPAND_FUNCTIONS
2956 && vim_strchr(xp->xp_pattern, '(') == NULL)
2957 {
2958 /* Function name can start with "<SNR>" */
2959 break;
2960 }
2961 else if (cmdidx != CMD_let || got_eq)
2962 {
2963 if (c == '"') /* string */
2964 {
2965 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2966 if (c == '\\' && xp->xp_pattern[1] != NUL)
2967 ++xp->xp_pattern;
2968 xp->xp_context = EXPAND_NOTHING;
2969 }
2970 else if (c == '\'') /* literal string */
2971 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002972 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2974 /* skip */ ;
2975 xp->xp_context = EXPAND_NOTHING;
2976 }
2977 else if (c == '|')
2978 {
2979 if (xp->xp_pattern[1] == '|')
2980 {
2981 ++xp->xp_pattern;
2982 xp->xp_context = EXPAND_EXPRESSION;
2983 }
2984 else
2985 xp->xp_context = EXPAND_COMMANDS;
2986 }
2987 else
2988 xp->xp_context = EXPAND_EXPRESSION;
2989 }
2990 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002991 /* Doesn't look like something valid, expand as an expression
2992 * anyway. */
2993 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 arg = xp->xp_pattern;
2995 if (*arg != NUL)
2996 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2997 /* skip */ ;
2998 }
2999 xp->xp_pattern = arg;
3000}
3001
3002#endif /* FEAT_CMDL_COMPL */
3003
3004/*
3005 * ":1,25call func(arg1, arg2)" function call.
3006 */
3007 void
3008ex_call(eap)
3009 exarg_T *eap;
3010{
3011 char_u *arg = eap->arg;
3012 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003014 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003016 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 linenr_T lnum;
3018 int doesrange;
3019 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003020 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003022 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3023 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003024 if (tofree == NULL)
3025 return;
3026
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003027 /* Increase refcount on dictionary, it could get deleted when evaluating
3028 * the arguments. */
3029 if (fudi.fd_dict != NULL)
3030 ++fudi.fd_dict->dv_refcount;
3031
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003032 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3033 len = STRLEN(tofree);
3034 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
Bram Moolenaar532c7802005-01-27 14:44:31 +00003036 /* Skip white space to allow ":call func ()". Not good, but required for
3037 * backward compatibility. */
3038 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003039 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040
3041 if (*startarg != '(')
3042 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003043 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 goto end;
3045 }
3046
3047 /*
3048 * When skipping, evaluate the function once, to find the end of the
3049 * arguments.
3050 * When the function takes a range, this is discovered after the first
3051 * call, and the loop is broken.
3052 */
3053 if (eap->skip)
3054 {
3055 ++emsg_skip;
3056 lnum = eap->line2; /* do it once, also with an invalid range */
3057 }
3058 else
3059 lnum = eap->line1;
3060 for ( ; lnum <= eap->line2; ++lnum)
3061 {
3062 if (!eap->skip && eap->addr_count > 0)
3063 {
3064 curwin->w_cursor.lnum = lnum;
3065 curwin->w_cursor.col = 0;
3066 }
3067 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003068 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003069 eap->line1, eap->line2, &doesrange,
3070 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 {
3072 failed = TRUE;
3073 break;
3074 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003075 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 if (doesrange || eap->skip)
3077 break;
3078 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003079 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003080 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003081 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 if (aborting())
3083 break;
3084 }
3085 if (eap->skip)
3086 --emsg_skip;
3087
3088 if (!failed)
3089 {
3090 /* Check for trailing illegal characters and a following command. */
3091 if (!ends_excmd(*arg))
3092 {
3093 emsg_severe = TRUE;
3094 EMSG(_(e_trailing));
3095 }
3096 else
3097 eap->nextcmd = check_nextcmd(arg);
3098 }
3099
3100end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003101 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003102 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103}
3104
3105/*
3106 * ":unlet[!] var1 ... " command.
3107 */
3108 void
3109ex_unlet(eap)
3110 exarg_T *eap;
3111{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003112 ex_unletlock(eap, eap->arg, 0);
3113}
3114
3115/*
3116 * ":lockvar" and ":unlockvar" commands
3117 */
3118 void
3119ex_lockvar(eap)
3120 exarg_T *eap;
3121{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003123 int deep = 2;
3124
3125 if (eap->forceit)
3126 deep = -1;
3127 else if (vim_isdigit(*arg))
3128 {
3129 deep = getdigits(&arg);
3130 arg = skipwhite(arg);
3131 }
3132
3133 ex_unletlock(eap, arg, deep);
3134}
3135
3136/*
3137 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3138 */
3139 static void
3140ex_unletlock(eap, argstart, deep)
3141 exarg_T *eap;
3142 char_u *argstart;
3143 int deep;
3144{
3145 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003148 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
3150 do
3151 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003152 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003153 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3154 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003155 if (lv.ll_name == NULL)
3156 error = TRUE; /* error but continue parsing */
3157 if (name_end == NULL || (!vim_iswhite(*name_end)
3158 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003160 if (name_end != NULL)
3161 {
3162 emsg_severe = TRUE;
3163 EMSG(_(e_trailing));
3164 }
3165 if (!(eap->skip || error))
3166 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 break;
3168 }
3169
3170 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003171 {
3172 if (eap->cmdidx == CMD_unlet)
3173 {
3174 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3175 error = TRUE;
3176 }
3177 else
3178 {
3179 if (do_lock_var(&lv, name_end, deep,
3180 eap->cmdidx == CMD_lockvar) == FAIL)
3181 error = TRUE;
3182 }
3183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003185 if (!eap->skip)
3186 clear_lval(&lv);
3187
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 arg = skipwhite(name_end);
3189 } while (!ends_excmd(*arg));
3190
3191 eap->nextcmd = check_nextcmd(arg);
3192}
3193
Bram Moolenaar8c711452005-01-14 21:53:12 +00003194 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003195do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003197 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003198 int forceit;
3199{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003200 int ret = OK;
3201 int cc;
3202
3203 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003204 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003205 cc = *name_end;
3206 *name_end = NUL;
3207
3208 /* Normal name or expanded name. */
3209 if (check_changedtick(lp->ll_name))
3210 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003211 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003212 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003213 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003214 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003215 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3216 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003217 else if (lp->ll_range)
3218 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003219 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003220
3221 /* Delete a range of List items. */
3222 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3223 {
3224 li = lp->ll_li->li_next;
3225 listitem_remove(lp->ll_list, lp->ll_li);
3226 lp->ll_li = li;
3227 ++lp->ll_n1;
3228 }
3229 }
3230 else
3231 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003232 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003233 /* unlet a List item. */
3234 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003235 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003236 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003237 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003238 }
3239
3240 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003241}
3242
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243/*
3244 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003245 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 */
3247 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003248do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003250 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251{
Bram Moolenaar33570922005-01-25 22:26:29 +00003252 hashtab_T *ht;
3253 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003254 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
Bram Moolenaar33570922005-01-25 22:26:29 +00003256 ht = find_var_ht(name, &varname);
3257 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 hi = hash_find(ht, varname);
3260 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003261 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003262 if (var_check_ro(HI2DI(hi)->di_flags, name))
3263 return FAIL;
3264 delete_var(ht, hi);
3265 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003268 if (forceit)
3269 return OK;
3270 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 return FAIL;
3272}
3273
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003274/*
3275 * Lock or unlock variable indicated by "lp".
3276 * "deep" is the levels to go (-1 for unlimited);
3277 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3278 */
3279 static int
3280do_lock_var(lp, name_end, deep, lock)
3281 lval_T *lp;
3282 char_u *name_end;
3283 int deep;
3284 int lock;
3285{
3286 int ret = OK;
3287 int cc;
3288 dictitem_T *di;
3289
3290 if (deep == 0) /* nothing to do */
3291 return OK;
3292
3293 if (lp->ll_tv == NULL)
3294 {
3295 cc = *name_end;
3296 *name_end = NUL;
3297
3298 /* Normal name or expanded name. */
3299 if (check_changedtick(lp->ll_name))
3300 ret = FAIL;
3301 else
3302 {
3303 di = find_var(lp->ll_name, NULL);
3304 if (di == NULL)
3305 ret = FAIL;
3306 else
3307 {
3308 if (lock)
3309 di->di_flags |= DI_FLAGS_LOCK;
3310 else
3311 di->di_flags &= ~DI_FLAGS_LOCK;
3312 item_lock(&di->di_tv, deep, lock);
3313 }
3314 }
3315 *name_end = cc;
3316 }
3317 else if (lp->ll_range)
3318 {
3319 listitem_T *li = lp->ll_li;
3320
3321 /* (un)lock a range of List items. */
3322 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3323 {
3324 item_lock(&li->li_tv, deep, lock);
3325 li = li->li_next;
3326 ++lp->ll_n1;
3327 }
3328 }
3329 else if (lp->ll_list != NULL)
3330 /* (un)lock a List item. */
3331 item_lock(&lp->ll_li->li_tv, deep, lock);
3332 else
3333 /* un(lock) a Dictionary item. */
3334 item_lock(&lp->ll_di->di_tv, deep, lock);
3335
3336 return ret;
3337}
3338
3339/*
3340 * Lock or unlock an item. "deep" is nr of levels to go.
3341 */
3342 static void
3343item_lock(tv, deep, lock)
3344 typval_T *tv;
3345 int deep;
3346 int lock;
3347{
3348 static int recurse = 0;
3349 list_T *l;
3350 listitem_T *li;
3351 dict_T *d;
3352 hashitem_T *hi;
3353 int todo;
3354
3355 if (recurse >= DICT_MAXNEST)
3356 {
3357 EMSG(_("E743: variable nested too deep for (un)lock"));
3358 return;
3359 }
3360 if (deep == 0)
3361 return;
3362 ++recurse;
3363
3364 /* lock/unlock the item itself */
3365 if (lock)
3366 tv->v_lock |= VAR_LOCKED;
3367 else
3368 tv->v_lock &= ~VAR_LOCKED;
3369
3370 switch (tv->v_type)
3371 {
3372 case VAR_LIST:
3373 if ((l = tv->vval.v_list) != NULL)
3374 {
3375 if (lock)
3376 l->lv_lock |= VAR_LOCKED;
3377 else
3378 l->lv_lock &= ~VAR_LOCKED;
3379 if (deep < 0 || deep > 1)
3380 /* recursive: lock/unlock the items the List contains */
3381 for (li = l->lv_first; li != NULL; li = li->li_next)
3382 item_lock(&li->li_tv, deep - 1, lock);
3383 }
3384 break;
3385 case VAR_DICT:
3386 if ((d = tv->vval.v_dict) != NULL)
3387 {
3388 if (lock)
3389 d->dv_lock |= VAR_LOCKED;
3390 else
3391 d->dv_lock &= ~VAR_LOCKED;
3392 if (deep < 0 || deep > 1)
3393 {
3394 /* recursive: lock/unlock the items the List contains */
3395 todo = d->dv_hashtab.ht_used;
3396 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3397 {
3398 if (!HASHITEM_EMPTY(hi))
3399 {
3400 --todo;
3401 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3402 }
3403 }
3404 }
3405 }
3406 }
3407 --recurse;
3408}
3409
Bram Moolenaara40058a2005-07-11 22:42:07 +00003410/*
3411 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3412 * it refers to a List or Dictionary that is locked.
3413 */
3414 static int
3415tv_islocked(tv)
3416 typval_T *tv;
3417{
3418 return (tv->v_lock & VAR_LOCKED)
3419 || (tv->v_type == VAR_LIST
3420 && tv->vval.v_list != NULL
3421 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3422 || (tv->v_type == VAR_DICT
3423 && tv->vval.v_dict != NULL
3424 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3425}
3426
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3428/*
3429 * Delete all "menutrans_" variables.
3430 */
3431 void
3432del_menutrans_vars()
3433{
Bram Moolenaar33570922005-01-25 22:26:29 +00003434 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003435 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar33570922005-01-25 22:26:29 +00003437 hash_lock(&globvarht);
3438 todo = globvarht.ht_used;
3439 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003440 {
3441 if (!HASHITEM_EMPTY(hi))
3442 {
3443 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003444 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3445 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003446 }
3447 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003448 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449}
3450#endif
3451
3452#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3453
3454/*
3455 * Local string buffer for the next two functions to store a variable name
3456 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3457 * get_user_var_name().
3458 */
3459
3460static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3461
3462static char_u *varnamebuf = NULL;
3463static int varnamebuflen = 0;
3464
3465/*
3466 * Function to concatenate a prefix and a variable name.
3467 */
3468 static char_u *
3469cat_prefix_varname(prefix, name)
3470 int prefix;
3471 char_u *name;
3472{
3473 int len;
3474
3475 len = (int)STRLEN(name) + 3;
3476 if (len > varnamebuflen)
3477 {
3478 vim_free(varnamebuf);
3479 len += 10; /* some additional space */
3480 varnamebuf = alloc(len);
3481 if (varnamebuf == NULL)
3482 {
3483 varnamebuflen = 0;
3484 return NULL;
3485 }
3486 varnamebuflen = len;
3487 }
3488 *varnamebuf = prefix;
3489 varnamebuf[1] = ':';
3490 STRCPY(varnamebuf + 2, name);
3491 return varnamebuf;
3492}
3493
3494/*
3495 * Function given to ExpandGeneric() to obtain the list of user defined
3496 * (global/buffer/window/built-in) variable names.
3497 */
3498/*ARGSUSED*/
3499 char_u *
3500get_user_var_name(xp, idx)
3501 expand_T *xp;
3502 int idx;
3503{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003504 static long_u gdone;
3505 static long_u bdone;
3506 static long_u wdone;
3507 static int vidx;
3508 static hashitem_T *hi;
3509 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
3511 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003512 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003513
3514 /* Global variables */
3515 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003517 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003518 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003519 else
3520 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003521 while (HASHITEM_EMPTY(hi))
3522 ++hi;
3523 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3524 return cat_prefix_varname('g', hi->hi_key);
3525 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003527
3528 /* b: variables */
3529 ht = &curbuf->b_vars.dv_hashtab;
3530 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003532 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003533 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003534 else
3535 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003536 while (HASHITEM_EMPTY(hi))
3537 ++hi;
3538 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003540 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003542 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 return (char_u *)"b:changedtick";
3544 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003545
3546 /* w: variables */
3547 ht = &curwin->w_vars.dv_hashtab;
3548 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003550 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003551 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003552 else
3553 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003554 while (HASHITEM_EMPTY(hi))
3555 ++hi;
3556 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003558
3559 /* v: variables */
3560 if (vidx < VV_LEN)
3561 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
3563 vim_free(varnamebuf);
3564 varnamebuf = NULL;
3565 varnamebuflen = 0;
3566 return NULL;
3567}
3568
3569#endif /* FEAT_CMDL_COMPL */
3570
3571/*
3572 * types for expressions.
3573 */
3574typedef enum
3575{
3576 TYPE_UNKNOWN = 0
3577 , TYPE_EQUAL /* == */
3578 , TYPE_NEQUAL /* != */
3579 , TYPE_GREATER /* > */
3580 , TYPE_GEQUAL /* >= */
3581 , TYPE_SMALLER /* < */
3582 , TYPE_SEQUAL /* <= */
3583 , TYPE_MATCH /* =~ */
3584 , TYPE_NOMATCH /* !~ */
3585} exptype_T;
3586
3587/*
3588 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003589 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3591 */
3592
3593/*
3594 * Handle zero level expression.
3595 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003596 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 * Return OK or FAIL.
3598 */
3599 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003600eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 char_u **nextcmd;
3604 int evaluate;
3605{
3606 int ret;
3607 char_u *p;
3608
3609 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003610 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 if (ret == FAIL || !ends_excmd(*p))
3612 {
3613 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003614 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 /*
3616 * Report the invalid expression unless the expression evaluation has
3617 * been cancelled due to an aborting error, an interrupt, or an
3618 * exception.
3619 */
3620 if (!aborting())
3621 EMSG2(_(e_invexpr2), arg);
3622 ret = FAIL;
3623 }
3624 if (nextcmd != NULL)
3625 *nextcmd = check_nextcmd(p);
3626
3627 return ret;
3628}
3629
3630/*
3631 * Handle top level expression:
3632 * expr1 ? expr0 : expr0
3633 *
3634 * "arg" must point to the first non-white of the expression.
3635 * "arg" is advanced to the next non-white after the recognized expression.
3636 *
3637 * Return OK or FAIL.
3638 */
3639 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003640eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 int evaluate;
3644{
3645 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003646 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
3648 /*
3649 * Get the first variable.
3650 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003651 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 return FAIL;
3653
3654 if ((*arg)[0] == '?')
3655 {
3656 result = FALSE;
3657 if (evaluate)
3658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003659 int error = FALSE;
3660
3661 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003663 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003664 if (error)
3665 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
3667
3668 /*
3669 * Get the second variable.
3670 */
3671 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003672 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 return FAIL;
3674
3675 /*
3676 * Check for the ":".
3677 */
3678 if ((*arg)[0] != ':')
3679 {
3680 EMSG(_("E109: Missing ':' after '?'"));
3681 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003682 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 return FAIL;
3684 }
3685
3686 /*
3687 * Get the third variable.
3688 */
3689 *arg = skipwhite(*arg + 1);
3690 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3691 {
3692 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003693 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 return FAIL;
3695 }
3696 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003697 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
3699
3700 return OK;
3701}
3702
3703/*
3704 * Handle first level expression:
3705 * expr2 || expr2 || expr2 logical OR
3706 *
3707 * "arg" must point to the first non-white of the expression.
3708 * "arg" is advanced to the next non-white after the recognized expression.
3709 *
3710 * Return OK or FAIL.
3711 */
3712 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003713eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 int evaluate;
3717{
Bram Moolenaar33570922005-01-25 22:26:29 +00003718 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 long result;
3720 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003721 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
3723 /*
3724 * Get the first variable.
3725 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003726 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 return FAIL;
3728
3729 /*
3730 * Repeat until there is no following "||".
3731 */
3732 first = TRUE;
3733 result = FALSE;
3734 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3735 {
3736 if (evaluate && first)
3737 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003738 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003740 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003741 if (error)
3742 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 first = FALSE;
3744 }
3745
3746 /*
3747 * Get the second variable.
3748 */
3749 *arg = skipwhite(*arg + 2);
3750 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3751 return FAIL;
3752
3753 /*
3754 * Compute the result.
3755 */
3756 if (evaluate && !result)
3757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003758 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003760 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003761 if (error)
3762 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
3764 if (evaluate)
3765 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003766 rettv->v_type = VAR_NUMBER;
3767 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
3769 }
3770
3771 return OK;
3772}
3773
3774/*
3775 * Handle second level expression:
3776 * expr3 && expr3 && expr3 logical AND
3777 *
3778 * "arg" must point to the first non-white of the expression.
3779 * "arg" is advanced to the next non-white after the recognized expression.
3780 *
3781 * Return OK or FAIL.
3782 */
3783 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003784eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 int evaluate;
3788{
Bram Moolenaar33570922005-01-25 22:26:29 +00003789 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 long result;
3791 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003792 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793
3794 /*
3795 * Get the first variable.
3796 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003797 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 return FAIL;
3799
3800 /*
3801 * Repeat until there is no following "&&".
3802 */
3803 first = TRUE;
3804 result = TRUE;
3805 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3806 {
3807 if (evaluate && first)
3808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003809 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003811 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003812 if (error)
3813 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 first = FALSE;
3815 }
3816
3817 /*
3818 * Get the second variable.
3819 */
3820 *arg = skipwhite(*arg + 2);
3821 if (eval4(arg, &var2, evaluate && result) == FAIL)
3822 return FAIL;
3823
3824 /*
3825 * Compute the result.
3826 */
3827 if (evaluate && result)
3828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003829 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003832 if (error)
3833 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
3835 if (evaluate)
3836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003837 rettv->v_type = VAR_NUMBER;
3838 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840 }
3841
3842 return OK;
3843}
3844
3845/*
3846 * Handle third level expression:
3847 * var1 == var2
3848 * var1 =~ var2
3849 * var1 != var2
3850 * var1 !~ var2
3851 * var1 > var2
3852 * var1 >= var2
3853 * var1 < var2
3854 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003855 * var1 is var2
3856 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 *
3858 * "arg" must point to the first non-white of the expression.
3859 * "arg" is advanced to the next non-white after the recognized expression.
3860 *
3861 * Return OK or FAIL.
3862 */
3863 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003864eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 int evaluate;
3868{
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 char_u *p;
3871 int i;
3872 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003873 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 int len = 2;
3875 long n1, n2;
3876 char_u *s1, *s2;
3877 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3878 regmatch_T regmatch;
3879 int ic;
3880 char_u *save_cpo;
3881
3882 /*
3883 * Get the first variable.
3884 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003885 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 return FAIL;
3887
3888 p = *arg;
3889 switch (p[0])
3890 {
3891 case '=': if (p[1] == '=')
3892 type = TYPE_EQUAL;
3893 else if (p[1] == '~')
3894 type = TYPE_MATCH;
3895 break;
3896 case '!': if (p[1] == '=')
3897 type = TYPE_NEQUAL;
3898 else if (p[1] == '~')
3899 type = TYPE_NOMATCH;
3900 break;
3901 case '>': if (p[1] != '=')
3902 {
3903 type = TYPE_GREATER;
3904 len = 1;
3905 }
3906 else
3907 type = TYPE_GEQUAL;
3908 break;
3909 case '<': if (p[1] != '=')
3910 {
3911 type = TYPE_SMALLER;
3912 len = 1;
3913 }
3914 else
3915 type = TYPE_SEQUAL;
3916 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003917 case 'i': if (p[1] == 's')
3918 {
3919 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3920 len = 5;
3921 if (!vim_isIDc(p[len]))
3922 {
3923 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3924 type_is = TRUE;
3925 }
3926 }
3927 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
3929
3930 /*
3931 * If there is a comparitive operator, use it.
3932 */
3933 if (type != TYPE_UNKNOWN)
3934 {
3935 /* extra question mark appended: ignore case */
3936 if (p[len] == '?')
3937 {
3938 ic = TRUE;
3939 ++len;
3940 }
3941 /* extra '#' appended: match case */
3942 else if (p[len] == '#')
3943 {
3944 ic = FALSE;
3945 ++len;
3946 }
3947 /* nothing appened: use 'ignorecase' */
3948 else
3949 ic = p_ic;
3950
3951 /*
3952 * Get the second variable.
3953 */
3954 *arg = skipwhite(p + len);
3955 if (eval5(arg, &var2, evaluate) == FAIL)
3956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003957 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 return FAIL;
3959 }
3960
3961 if (evaluate)
3962 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003963 if (type_is && rettv->v_type != var2.v_type)
3964 {
3965 /* For "is" a different type always means FALSE, for "notis"
3966 * it means TRUE. */
3967 n1 = (type == TYPE_NEQUAL);
3968 }
3969 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3970 {
3971 if (type_is)
3972 {
3973 n1 = (rettv->v_type == var2.v_type
3974 && rettv->vval.v_list == var2.vval.v_list);
3975 if (type == TYPE_NEQUAL)
3976 n1 = !n1;
3977 }
3978 else if (rettv->v_type != var2.v_type
3979 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3980 {
3981 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003982 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003983 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003984 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003985 clear_tv(rettv);
3986 clear_tv(&var2);
3987 return FAIL;
3988 }
3989 else
3990 {
3991 /* Compare two Lists for being equal or unequal. */
3992 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3993 if (type == TYPE_NEQUAL)
3994 n1 = !n1;
3995 }
3996 }
3997
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003998 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3999 {
4000 if (type_is)
4001 {
4002 n1 = (rettv->v_type == var2.v_type
4003 && rettv->vval.v_dict == var2.vval.v_dict);
4004 if (type == TYPE_NEQUAL)
4005 n1 = !n1;
4006 }
4007 else if (rettv->v_type != var2.v_type
4008 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4009 {
4010 if (rettv->v_type != var2.v_type)
4011 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4012 else
4013 EMSG(_("E736: Invalid operation for Dictionary"));
4014 clear_tv(rettv);
4015 clear_tv(&var2);
4016 return FAIL;
4017 }
4018 else
4019 {
4020 /* Compare two Dictionaries for being equal or unequal. */
4021 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4022 if (type == TYPE_NEQUAL)
4023 n1 = !n1;
4024 }
4025 }
4026
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004027 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4028 {
4029 if (rettv->v_type != var2.v_type
4030 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4031 {
4032 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004033 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004034 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004035 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004036 clear_tv(rettv);
4037 clear_tv(&var2);
4038 return FAIL;
4039 }
4040 else
4041 {
4042 /* Compare two Funcrefs for being equal or unequal. */
4043 if (rettv->vval.v_string == NULL
4044 || var2.vval.v_string == NULL)
4045 n1 = FALSE;
4046 else
4047 n1 = STRCMP(rettv->vval.v_string,
4048 var2.vval.v_string) == 0;
4049 if (type == TYPE_NEQUAL)
4050 n1 = !n1;
4051 }
4052 }
4053
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 /*
4055 * If one of the two variables is a number, compare as a number.
4056 * When using "=~" or "!~", always compare as string.
4057 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004058 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4060 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004061 n1 = get_tv_number(rettv);
4062 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 switch (type)
4064 {
4065 case TYPE_EQUAL: n1 = (n1 == n2); break;
4066 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4067 case TYPE_GREATER: n1 = (n1 > n2); break;
4068 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4069 case TYPE_SMALLER: n1 = (n1 < n2); break;
4070 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4071 case TYPE_UNKNOWN:
4072 case TYPE_MATCH:
4073 case TYPE_NOMATCH: break; /* avoid gcc warning */
4074 }
4075 }
4076 else
4077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004078 s1 = get_tv_string_buf(rettv, buf1);
4079 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4081 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4082 else
4083 i = 0;
4084 n1 = FALSE;
4085 switch (type)
4086 {
4087 case TYPE_EQUAL: n1 = (i == 0); break;
4088 case TYPE_NEQUAL: n1 = (i != 0); break;
4089 case TYPE_GREATER: n1 = (i > 0); break;
4090 case TYPE_GEQUAL: n1 = (i >= 0); break;
4091 case TYPE_SMALLER: n1 = (i < 0); break;
4092 case TYPE_SEQUAL: n1 = (i <= 0); break;
4093
4094 case TYPE_MATCH:
4095 case TYPE_NOMATCH:
4096 /* avoid 'l' flag in 'cpoptions' */
4097 save_cpo = p_cpo;
4098 p_cpo = (char_u *)"";
4099 regmatch.regprog = vim_regcomp(s2,
4100 RE_MAGIC + RE_STRING);
4101 regmatch.rm_ic = ic;
4102 if (regmatch.regprog != NULL)
4103 {
4104 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4105 vim_free(regmatch.regprog);
4106 if (type == TYPE_NOMATCH)
4107 n1 = !n1;
4108 }
4109 p_cpo = save_cpo;
4110 break;
4111
4112 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4113 }
4114 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 clear_tv(rettv);
4116 clear_tv(&var2);
4117 rettv->v_type = VAR_NUMBER;
4118 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120 }
4121
4122 return OK;
4123}
4124
4125/*
4126 * Handle fourth level expression:
4127 * + number addition
4128 * - number subtraction
4129 * . string concatenation
4130 *
4131 * "arg" must point to the first non-white of the expression.
4132 * "arg" is advanced to the next non-white after the recognized expression.
4133 *
4134 * Return OK or FAIL.
4135 */
4136 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 int evaluate;
4141{
Bram Moolenaar33570922005-01-25 22:26:29 +00004142 typval_T var2;
4143 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 int op;
4145 long n1, n2;
4146 char_u *s1, *s2;
4147 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4148 char_u *p;
4149
4150 /*
4151 * Get the first variable.
4152 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004153 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 return FAIL;
4155
4156 /*
4157 * Repeat computing, until no '+', '-' or '.' is following.
4158 */
4159 for (;;)
4160 {
4161 op = **arg;
4162 if (op != '+' && op != '-' && op != '.')
4163 break;
4164
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004165 if (op != '+' || rettv->v_type != VAR_LIST)
4166 {
4167 /* For "list + ...", an illegal use of the first operand as
4168 * a number cannot be determined before evaluating the 2nd
4169 * operand: if this is also a list, all is ok.
4170 * For "something . ...", "something - ..." or "non-list + ...",
4171 * we know that the first operand needs to be a string or number
4172 * without evaluating the 2nd operand. So check before to avoid
4173 * side effects after an error. */
4174 if (evaluate && get_tv_string_chk(rettv) == NULL)
4175 {
4176 clear_tv(rettv);
4177 return FAIL;
4178 }
4179 }
4180
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 /*
4182 * Get the second variable.
4183 */
4184 *arg = skipwhite(*arg + 1);
4185 if (eval6(arg, &var2, evaluate) == FAIL)
4186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 return FAIL;
4189 }
4190
4191 if (evaluate)
4192 {
4193 /*
4194 * Compute the result.
4195 */
4196 if (op == '.')
4197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4199 s2 = get_tv_string_buf_chk(&var2, buf2);
4200 if (s2 == NULL) /* type error ? */
4201 {
4202 clear_tv(rettv);
4203 clear_tv(&var2);
4204 return FAIL;
4205 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004206 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 clear_tv(rettv);
4208 rettv->v_type = VAR_STRING;
4209 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004211 else if (op == '+' && rettv->v_type == VAR_LIST
4212 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004213 {
4214 /* concatenate Lists */
4215 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4216 &var3) == FAIL)
4217 {
4218 clear_tv(rettv);
4219 clear_tv(&var2);
4220 return FAIL;
4221 }
4222 clear_tv(rettv);
4223 *rettv = var3;
4224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 else
4226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004227 int error = FALSE;
4228
4229 n1 = get_tv_number_chk(rettv, &error);
4230 if (error)
4231 {
4232 /* This can only happen for "list + non-list".
4233 * For "non-list + ..." or "something - ...", we returned
4234 * before evaluating the 2nd operand. */
4235 clear_tv(rettv);
4236 return FAIL;
4237 }
4238 n2 = get_tv_number_chk(&var2, &error);
4239 if (error)
4240 {
4241 clear_tv(rettv);
4242 clear_tv(&var2);
4243 return FAIL;
4244 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 if (op == '+')
4247 n1 = n1 + n2;
4248 else
4249 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004250 rettv->v_type = VAR_NUMBER;
4251 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 }
4255 }
4256 return OK;
4257}
4258
4259/*
4260 * Handle fifth level expression:
4261 * * number multiplication
4262 * / number division
4263 * % number modulo
4264 *
4265 * "arg" must point to the first non-white of the expression.
4266 * "arg" is advanced to the next non-white after the recognized expression.
4267 *
4268 * Return OK or FAIL.
4269 */
4270 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 int evaluate;
4275{
Bram Moolenaar33570922005-01-25 22:26:29 +00004276 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 int op;
4278 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
4281 /*
4282 * Get the first variable.
4283 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 return FAIL;
4286
4287 /*
4288 * Repeat computing, until no '*', '/' or '%' is following.
4289 */
4290 for (;;)
4291 {
4292 op = **arg;
4293 if (op != '*' && op != '/' && op != '%')
4294 break;
4295
4296 if (evaluate)
4297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004298 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004300 if (error)
4301 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303 else
4304 n1 = 0;
4305
4306 /*
4307 * Get the second variable.
4308 */
4309 *arg = skipwhite(*arg + 1);
4310 if (eval7(arg, &var2, evaluate) == FAIL)
4311 return FAIL;
4312
4313 if (evaluate)
4314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004315 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004317 if (error)
4318 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
4320 /*
4321 * Compute the result.
4322 */
4323 if (op == '*')
4324 n1 = n1 * n2;
4325 else if (op == '/')
4326 {
4327 if (n2 == 0) /* give an error message? */
4328 n1 = 0x7fffffffL;
4329 else
4330 n1 = n1 / n2;
4331 }
4332 else
4333 {
4334 if (n2 == 0) /* give an error message? */
4335 n1 = 0;
4336 else
4337 n1 = n1 % n2;
4338 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004339 rettv->v_type = VAR_NUMBER;
4340 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
4342 }
4343
4344 return OK;
4345}
4346
4347/*
4348 * Handle sixth level expression:
4349 * number number constant
4350 * "string" string contstant
4351 * 'string' literal string contstant
4352 * &option-name option value
4353 * @r register contents
4354 * identifier variable value
4355 * function() function call
4356 * $VAR environment variable
4357 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004358 * [expr, expr] List
4359 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 *
4361 * Also handle:
4362 * ! in front logical NOT
4363 * - in front unary minus
4364 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004365 * trailing [] subscript in String or List
4366 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 *
4368 * "arg" must point to the first non-white of the expression.
4369 * "arg" is advanced to the next non-white after the recognized expression.
4370 *
4371 * Return OK or FAIL.
4372 */
4373 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004374eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 int evaluate;
4378{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 long n;
4380 int len;
4381 char_u *s;
4382 int val;
4383 char_u *start_leader, *end_leader;
4384 int ret = OK;
4385 char_u *alias;
4386
4387 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004388 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004389 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393 /*
4394 * Skip '!' and '-' characters. They are handled later.
4395 */
4396 start_leader = *arg;
4397 while (**arg == '!' || **arg == '-' || **arg == '+')
4398 *arg = skipwhite(*arg + 1);
4399 end_leader = *arg;
4400
4401 switch (**arg)
4402 {
4403 /*
4404 * Number constant.
4405 */
4406 case '0':
4407 case '1':
4408 case '2':
4409 case '3':
4410 case '4':
4411 case '5':
4412 case '6':
4413 case '7':
4414 case '8':
4415 case '9':
4416 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4417 *arg += len;
4418 if (evaluate)
4419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004420 rettv->v_type = VAR_NUMBER;
4421 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 }
4423 break;
4424
4425 /*
4426 * String constant: "string".
4427 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004428 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 break;
4430
4431 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004432 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004434 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004435 break;
4436
4437 /*
4438 * List: [expr, expr]
4439 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004440 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 break;
4442
4443 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004444 * Dictionary: {key: val, key: val}
4445 */
4446 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4447 break;
4448
4449 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004450 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004452 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 break;
4454
4455 /*
4456 * Environment variable: $VAR.
4457 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004458 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 break;
4460
4461 /*
4462 * Register contents: @r.
4463 */
4464 case '@': ++*arg;
4465 if (evaluate)
4466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004467 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004468 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470 if (**arg != NUL)
4471 ++*arg;
4472 break;
4473
4474 /*
4475 * nested expression: (expression).
4476 */
4477 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004478 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 if (**arg == ')')
4480 ++*arg;
4481 else if (ret == OK)
4482 {
4483 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004484 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 ret = FAIL;
4486 }
4487 break;
4488
Bram Moolenaar8c711452005-01-14 21:53:12 +00004489 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 break;
4491 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004492
4493 if (ret == NOTDONE)
4494 {
4495 /*
4496 * Must be a variable or function name.
4497 * Can also be a curly-braces kind of name: {expr}.
4498 */
4499 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004500 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004501 if (alias != NULL)
4502 s = alias;
4503
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004504 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004505 ret = FAIL;
4506 else
4507 {
4508 if (**arg == '(') /* recursive! */
4509 {
4510 /* If "s" is the name of a variable of type VAR_FUNC
4511 * use its contents. */
4512 s = deref_func_name(s, &len);
4513
4514 /* Invoke the function. */
4515 ret = get_func_tv(s, len, rettv, arg,
4516 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004517 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004518 /* Stop the expression evaluation when immediately
4519 * aborting on error, or when an interrupt occurred or
4520 * an exception was thrown but not caught. */
4521 if (aborting())
4522 {
4523 if (ret == OK)
4524 clear_tv(rettv);
4525 ret = FAIL;
4526 }
4527 }
4528 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004529 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004530 else
4531 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004532 }
4533
4534 if (alias != NULL)
4535 vim_free(alias);
4536 }
4537
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 *arg = skipwhite(*arg);
4539
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004540 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4541 * expr(expr). */
4542 if (ret == OK)
4543 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
4545 /*
4546 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4547 */
4548 if (ret == OK && evaluate && end_leader > start_leader)
4549 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004550 int error = FALSE;
4551
4552 val = get_tv_number_chk(rettv, &error);
4553 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004555 clear_tv(rettv);
4556 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004558 else
4559 {
4560 while (end_leader > start_leader)
4561 {
4562 --end_leader;
4563 if (*end_leader == '!')
4564 val = !val;
4565 else if (*end_leader == '-')
4566 val = -val;
4567 }
4568 clear_tv(rettv);
4569 rettv->v_type = VAR_NUMBER;
4570 rettv->vval.v_number = val;
4571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 }
4573
4574 return ret;
4575}
4576
4577/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004578 * Evaluate an "[expr]" or "[expr:expr]" index.
4579 * "*arg" points to the '['.
4580 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4581 */
4582 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004583eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004584 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004585 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004586 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004587 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004588{
4589 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004590 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004591 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004592 long len = -1;
4593 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004594 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004595 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004596
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004597 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004598 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004599 if (verbose)
4600 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004601 return FAIL;
4602 }
4603
Bram Moolenaar8c711452005-01-14 21:53:12 +00004604 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004605 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004606 /*
4607 * dict.name
4608 */
4609 key = *arg + 1;
4610 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4611 ;
4612 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004613 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004614 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615 }
4616 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004618 /*
4619 * something[idx]
4620 *
4621 * Get the (first) variable from inside the [].
4622 */
4623 *arg = skipwhite(*arg + 1);
4624 if (**arg == ':')
4625 empty1 = TRUE;
4626 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4627 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004628 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4629 {
4630 /* not a number or string */
4631 clear_tv(&var1);
4632 return FAIL;
4633 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004634
4635 /*
4636 * Get the second variable from inside the [:].
4637 */
4638 if (**arg == ':')
4639 {
4640 range = TRUE;
4641 *arg = skipwhite(*arg + 1);
4642 if (**arg == ']')
4643 empty2 = TRUE;
4644 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4645 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004646 if (!empty1)
4647 clear_tv(&var1);
4648 return FAIL;
4649 }
4650 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4651 {
4652 /* not a number or string */
4653 if (!empty1)
4654 clear_tv(&var1);
4655 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004656 return FAIL;
4657 }
4658 }
4659
4660 /* Check for the ']'. */
4661 if (**arg != ']')
4662 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004663 if (verbose)
4664 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004665 clear_tv(&var1);
4666 if (range)
4667 clear_tv(&var2);
4668 return FAIL;
4669 }
4670 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004671 }
4672
4673 if (evaluate)
4674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004675 n1 = 0;
4676 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004678 n1 = get_tv_number(&var1);
4679 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004680 }
4681 if (range)
4682 {
4683 if (empty2)
4684 n2 = -1;
4685 else
4686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004687 n2 = get_tv_number(&var2);
4688 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004689 }
4690 }
4691
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004692 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004693 {
4694 case VAR_NUMBER:
4695 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004696 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004697 len = (long)STRLEN(s);
4698 if (range)
4699 {
4700 /* The resulting variable is a substring. If the indexes
4701 * are out of range the result is empty. */
4702 if (n1 < 0)
4703 {
4704 n1 = len + n1;
4705 if (n1 < 0)
4706 n1 = 0;
4707 }
4708 if (n2 < 0)
4709 n2 = len + n2;
4710 else if (n2 >= len)
4711 n2 = len;
4712 if (n1 >= len || n2 < 0 || n1 > n2)
4713 s = NULL;
4714 else
4715 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4716 }
4717 else
4718 {
4719 /* The resulting variable is a string of a single
4720 * character. If the index is too big or negative the
4721 * result is empty. */
4722 if (n1 >= len || n1 < 0)
4723 s = NULL;
4724 else
4725 s = vim_strnsave(s + n1, 1);
4726 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004727 clear_tv(rettv);
4728 rettv->v_type = VAR_STRING;
4729 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004730 break;
4731
4732 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004733 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004734 if (n1 < 0)
4735 n1 = len + n1;
4736 if (!empty1 && (n1 < 0 || n1 >= len))
4737 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004738 if (verbose)
4739 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004740 return FAIL;
4741 }
4742 if (range)
4743 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004744 list_T *l;
4745 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004746
4747 if (n2 < 0)
4748 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004749 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004751 if (verbose)
4752 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753 return FAIL;
4754 }
4755 l = list_alloc();
4756 if (l == NULL)
4757 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004759 n1 <= n2; ++n1)
4760 {
4761 if (list_append_tv(l, &item->li_tv) == FAIL)
4762 {
4763 list_free(l);
4764 return FAIL;
4765 }
4766 item = item->li_next;
4767 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004768 clear_tv(rettv);
4769 rettv->v_type = VAR_LIST;
4770 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004771 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772 }
4773 else
4774 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004775 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004777 clear_tv(rettv);
4778 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004779 }
4780 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004781
4782 case VAR_DICT:
4783 if (range)
4784 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004785 if (verbose)
4786 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004787 if (len == -1)
4788 clear_tv(&var1);
4789 return FAIL;
4790 }
4791 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004792 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004793
4794 if (len == -1)
4795 {
4796 key = get_tv_string(&var1);
4797 if (*key == NUL)
4798 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004799 if (verbose)
4800 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004801 clear_tv(&var1);
4802 return FAIL;
4803 }
4804 }
4805
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004806 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004807
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004808 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004809 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004810 if (len == -1)
4811 clear_tv(&var1);
4812 if (item == NULL)
4813 return FAIL;
4814
4815 copy_tv(&item->di_tv, &var1);
4816 clear_tv(rettv);
4817 *rettv = var1;
4818 }
4819 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004820 }
4821 }
4822
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823 return OK;
4824}
4825
4826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 * Get an option value.
4828 * "arg" points to the '&' or '+' before the option name.
4829 * "arg" is advanced to character after the option name.
4830 * Return OK or FAIL.
4831 */
4832 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004833get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004835 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 int evaluate;
4837{
4838 char_u *option_end;
4839 long numval;
4840 char_u *stringval;
4841 int opt_type;
4842 int c;
4843 int working = (**arg == '+'); /* has("+option") */
4844 int ret = OK;
4845 int opt_flags;
4846
4847 /*
4848 * Isolate the option name and find its value.
4849 */
4850 option_end = find_option_end(arg, &opt_flags);
4851 if (option_end == NULL)
4852 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004853 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 EMSG2(_("E112: Option name missing: %s"), *arg);
4855 return FAIL;
4856 }
4857
4858 if (!evaluate)
4859 {
4860 *arg = option_end;
4861 return OK;
4862 }
4863
4864 c = *option_end;
4865 *option_end = NUL;
4866 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004867 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
4869 if (opt_type == -3) /* invalid name */
4870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004871 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 EMSG2(_("E113: Unknown option: %s"), *arg);
4873 ret = FAIL;
4874 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004875 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
4877 if (opt_type == -2) /* hidden string option */
4878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004879 rettv->v_type = VAR_STRING;
4880 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 }
4882 else if (opt_type == -1) /* hidden number option */
4883 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004884 rettv->v_type = VAR_NUMBER;
4885 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 }
4887 else if (opt_type == 1) /* number option */
4888 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004889 rettv->v_type = VAR_NUMBER;
4890 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
4892 else /* string option */
4893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004894 rettv->v_type = VAR_STRING;
4895 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 }
4897 }
4898 else if (working && (opt_type == -2 || opt_type == -1))
4899 ret = FAIL;
4900
4901 *option_end = c; /* put back for error messages */
4902 *arg = option_end;
4903
4904 return ret;
4905}
4906
4907/*
4908 * Allocate a variable for a string constant.
4909 * Return OK or FAIL.
4910 */
4911 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004912get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 int evaluate;
4916{
4917 char_u *p;
4918 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 int extra = 0;
4920
4921 /*
4922 * Find the end of the string, skipping backslashed characters.
4923 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004924 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 {
4926 if (*p == '\\' && p[1] != NUL)
4927 {
4928 ++p;
4929 /* A "\<x>" form occupies at least 4 characters, and produces up
4930 * to 6 characters: reserve space for 2 extra */
4931 if (*p == '<')
4932 extra += 2;
4933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 }
4935
4936 if (*p != '"')
4937 {
4938 EMSG2(_("E114: Missing quote: %s"), *arg);
4939 return FAIL;
4940 }
4941
4942 /* If only parsing, set *arg and return here */
4943 if (!evaluate)
4944 {
4945 *arg = p + 1;
4946 return OK;
4947 }
4948
4949 /*
4950 * Copy the string into allocated memory, handling backslashed
4951 * characters.
4952 */
4953 name = alloc((unsigned)(p - *arg + extra));
4954 if (name == NULL)
4955 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004956 rettv->v_type = VAR_STRING;
4957 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958
Bram Moolenaar8c711452005-01-14 21:53:12 +00004959 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
4961 if (*p == '\\')
4962 {
4963 switch (*++p)
4964 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004965 case 'b': *name++ = BS; ++p; break;
4966 case 'e': *name++ = ESC; ++p; break;
4967 case 'f': *name++ = FF; ++p; break;
4968 case 'n': *name++ = NL; ++p; break;
4969 case 'r': *name++ = CAR; ++p; break;
4970 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971
4972 case 'X': /* hex: "\x1", "\x12" */
4973 case 'x':
4974 case 'u': /* Unicode: "\u0023" */
4975 case 'U':
4976 if (vim_isxdigit(p[1]))
4977 {
4978 int n, nr;
4979 int c = toupper(*p);
4980
4981 if (c == 'X')
4982 n = 2;
4983 else
4984 n = 4;
4985 nr = 0;
4986 while (--n >= 0 && vim_isxdigit(p[1]))
4987 {
4988 ++p;
4989 nr = (nr << 4) + hex2nr(*p);
4990 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004991 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992#ifdef FEAT_MBYTE
4993 /* For "\u" store the number according to
4994 * 'encoding'. */
4995 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004996 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 else
4998#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 break;
5002
5003 /* octal: "\1", "\12", "\123" */
5004 case '0':
5005 case '1':
5006 case '2':
5007 case '3':
5008 case '4':
5009 case '5':
5010 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005011 case '7': *name = *p++ - '0';
5012 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 *name = (*name << 3) + *p++ - '0';
5015 if (*p >= '0' && *p <= '7')
5016 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005018 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 break;
5020
5021 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 if (extra != 0)
5024 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005025 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 break;
5027 }
5028 /* FALLTHROUGH */
5029
Bram Moolenaar8c711452005-01-14 21:53:12 +00005030 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 break;
5032 }
5033 }
5034 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 *arg = p + 1;
5040
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 return OK;
5042}
5043
5044/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 * Return OK or FAIL.
5047 */
5048 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005049get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 int evaluate;
5053{
5054 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005055 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005056 int reduce = 0;
5057
5058 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005060 */
5061 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5062 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005063 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005064 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005066 break;
5067 ++reduce;
5068 ++p;
5069 }
5070 }
5071
Bram Moolenaar8c711452005-01-14 21:53:12 +00005072 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005073 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005074 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005075 return FAIL;
5076 }
5077
Bram Moolenaar8c711452005-01-14 21:53:12 +00005078 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005079 if (!evaluate)
5080 {
5081 *arg = p + 1;
5082 return OK;
5083 }
5084
5085 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005087 */
5088 str = alloc((unsigned)((p - *arg) - reduce));
5089 if (str == NULL)
5090 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005091 rettv->v_type = VAR_STRING;
5092 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005093
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005095 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005097 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005099 break;
5100 ++p;
5101 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005103 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005104 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005105 *arg = p + 1;
5106
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005107 return OK;
5108}
5109
5110/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005111 * Allocate a variable for a List and fill it from "*arg".
5112 * Return OK or FAIL.
5113 */
5114 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005115get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005116 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005117 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005118 int evaluate;
5119{
Bram Moolenaar33570922005-01-25 22:26:29 +00005120 list_T *l = NULL;
5121 typval_T tv;
5122 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005123
5124 if (evaluate)
5125 {
5126 l = list_alloc();
5127 if (l == NULL)
5128 return FAIL;
5129 }
5130
5131 *arg = skipwhite(*arg + 1);
5132 while (**arg != ']' && **arg != NUL)
5133 {
5134 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5135 goto failret;
5136 if (evaluate)
5137 {
5138 item = listitem_alloc();
5139 if (item != NULL)
5140 {
5141 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005142 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143 list_append(l, item);
5144 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005145 else
5146 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005147 }
5148
5149 if (**arg == ']')
5150 break;
5151 if (**arg != ',')
5152 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005154 goto failret;
5155 }
5156 *arg = skipwhite(*arg + 1);
5157 }
5158
5159 if (**arg != ']')
5160 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005162failret:
5163 if (evaluate)
5164 list_free(l);
5165 return FAIL;
5166 }
5167
5168 *arg = skipwhite(*arg + 1);
5169 if (evaluate)
5170 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005171 rettv->v_type = VAR_LIST;
5172 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005173 ++l->lv_refcount;
5174 }
5175
5176 return OK;
5177}
5178
5179/*
5180 * Allocate an empty header for a list.
5181 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005182 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005183list_alloc()
5184{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005185 list_T *l;
5186
5187 l = (list_T *)alloc_clear(sizeof(list_T));
5188 if (l != NULL)
5189 {
5190 /* Prepend the list to the list of lists for garbage collection. */
5191 if (first_list != NULL)
5192 first_list->lv_used_prev = l;
5193 l->lv_used_prev = NULL;
5194 l->lv_used_next = first_list;
5195 first_list = l;
5196 }
5197 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005198}
5199
5200/*
5201 * Unreference a list: decrement the reference count and free it when it
5202 * becomes zero.
5203 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005204 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005205list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005206 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005207{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005208 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5209 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005210}
5211
5212/*
5213 * Free a list, including all items it points to.
5214 * Ignores the reference count.
5215 */
5216 static void
5217list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005218 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005219{
Bram Moolenaar33570922005-01-25 22:26:29 +00005220 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005221
Bram Moolenaard9fba312005-06-26 22:34:35 +00005222 /* Avoid that recursive reference to the list frees us again. */
5223 l->lv_refcount = DEL_REFCOUNT;
5224
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005225 /* Remove the list from the list of lists for garbage collection. */
5226 if (l->lv_used_prev == NULL)
5227 first_list = l->lv_used_next;
5228 else
5229 l->lv_used_prev->lv_used_next = l->lv_used_next;
5230 if (l->lv_used_next != NULL)
5231 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5232
Bram Moolenaard9fba312005-06-26 22:34:35 +00005233 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005234 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005235 /* Remove the item before deleting it. */
5236 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237 listitem_free(item);
5238 }
5239 vim_free(l);
5240}
5241
5242/*
5243 * Allocate a list item.
5244 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005245 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246listitem_alloc()
5247{
Bram Moolenaar33570922005-01-25 22:26:29 +00005248 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249}
5250
5251/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005252 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 */
5254 static void
5255listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005256 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005258 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259 vim_free(item);
5260}
5261
5262/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005263 * Remove a list item from a List and free it. Also clears the value.
5264 */
5265 static void
5266listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005267 list_T *l;
5268 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005269{
5270 list_remove(l, item, item);
5271 listitem_free(item);
5272}
5273
5274/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 * Get the number of items in a list.
5276 */
5277 static long
5278list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005279 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 if (l == NULL)
5282 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005283 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284}
5285
5286/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005287 * Return TRUE when two lists have exactly the same values.
5288 */
5289 static int
5290list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005291 list_T *l1;
5292 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005293 int ic; /* ignore case for strings */
5294{
Bram Moolenaar33570922005-01-25 22:26:29 +00005295 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005296
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005297 if (list_len(l1) != list_len(l2))
5298 return FALSE;
5299
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005300 for (item1 = l1->lv_first, item2 = l2->lv_first;
5301 item1 != NULL && item2 != NULL;
5302 item1 = item1->li_next, item2 = item2->li_next)
5303 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5304 return FALSE;
5305 return item1 == NULL && item2 == NULL;
5306}
5307
5308/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005309 * Return TRUE when two dictionaries have exactly the same key/values.
5310 */
5311 static int
5312dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005313 dict_T *d1;
5314 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005315 int ic; /* ignore case for strings */
5316{
Bram Moolenaar33570922005-01-25 22:26:29 +00005317 hashitem_T *hi;
5318 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005319 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005320
5321 if (dict_len(d1) != dict_len(d2))
5322 return FALSE;
5323
Bram Moolenaar33570922005-01-25 22:26:29 +00005324 todo = d1->dv_hashtab.ht_used;
5325 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005326 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005327 if (!HASHITEM_EMPTY(hi))
5328 {
5329 item2 = dict_find(d2, hi->hi_key, -1);
5330 if (item2 == NULL)
5331 return FALSE;
5332 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5333 return FALSE;
5334 --todo;
5335 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005336 }
5337 return TRUE;
5338}
5339
5340/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005341 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005342 * Compares the items just like "==" would compare them, but strings and
5343 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005344 */
5345 static int
5346tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005347 typval_T *tv1;
5348 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005349 int ic; /* ignore case */
5350{
5351 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005352 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005353
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005354 if (tv1->v_type != tv2->v_type)
5355 return FALSE;
5356
5357 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005358 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005359 case VAR_LIST:
5360 /* recursive! */
5361 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5362
5363 case VAR_DICT:
5364 /* recursive! */
5365 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5366
5367 case VAR_FUNC:
5368 return (tv1->vval.v_string != NULL
5369 && tv2->vval.v_string != NULL
5370 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5371
5372 case VAR_NUMBER:
5373 return tv1->vval.v_number == tv2->vval.v_number;
5374
5375 case VAR_STRING:
5376 s1 = get_tv_string_buf(tv1, buf1);
5377 s2 = get_tv_string_buf(tv2, buf2);
5378 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005379 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005380
5381 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005382 return TRUE;
5383}
5384
5385/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 * Locate item with index "n" in list "l" and return it.
5387 * A negative index is counted from the end; -1 is the last item.
5388 * Returns NULL when "n" is out of range.
5389 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005390 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005392 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 long n;
5394{
Bram Moolenaar33570922005-01-25 22:26:29 +00005395 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 long idx;
5397
5398 if (l == NULL)
5399 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005400
5401 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005403 n = l->lv_len + n;
5404
5405 /* Check for index out of range. */
5406 if (n < 0 || n >= l->lv_len)
5407 return NULL;
5408
5409 /* When there is a cached index may start search from there. */
5410 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005412 if (n < l->lv_idx / 2)
5413 {
5414 /* closest to the start of the list */
5415 item = l->lv_first;
5416 idx = 0;
5417 }
5418 else if (n > (l->lv_idx + l->lv_len) / 2)
5419 {
5420 /* closest to the end of the list */
5421 item = l->lv_last;
5422 idx = l->lv_len - 1;
5423 }
5424 else
5425 {
5426 /* closest to the cached index */
5427 item = l->lv_idx_item;
5428 idx = l->lv_idx;
5429 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 }
5431 else
5432 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005433 if (n < l->lv_len / 2)
5434 {
5435 /* closest to the start of the list */
5436 item = l->lv_first;
5437 idx = 0;
5438 }
5439 else
5440 {
5441 /* closest to the end of the list */
5442 item = l->lv_last;
5443 idx = l->lv_len - 1;
5444 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005446
5447 while (n > idx)
5448 {
5449 /* search forward */
5450 item = item->li_next;
5451 ++idx;
5452 }
5453 while (n < idx)
5454 {
5455 /* search backward */
5456 item = item->li_prev;
5457 --idx;
5458 }
5459
5460 /* cache the used index */
5461 l->lv_idx = idx;
5462 l->lv_idx_item = item;
5463
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 return item;
5465}
5466
5467/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005468 * Locate "item" list "l" and return its index.
5469 * Returns -1 when "item" is not in the list.
5470 */
5471 static long
5472list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 list_T *l;
5474 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005475{
5476 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005477 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005478
5479 if (l == NULL)
5480 return -1;
5481 idx = 0;
5482 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5483 ++idx;
5484 if (li == NULL)
5485 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005486 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005487}
5488
5489/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 * Append item "item" to the end of list "l".
5491 */
5492 static void
5493list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005494 list_T *l;
5495 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496{
5497 if (l->lv_last == NULL)
5498 {
5499 /* empty list */
5500 l->lv_first = item;
5501 l->lv_last = item;
5502 item->li_prev = NULL;
5503 }
5504 else
5505 {
5506 l->lv_last->li_next = item;
5507 item->li_prev = l->lv_last;
5508 l->lv_last = item;
5509 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005510 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005511 item->li_next = NULL;
5512}
5513
5514/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005515 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005516 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005517 */
5518 static int
5519list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005520 list_T *l;
5521 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005523 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524
Bram Moolenaar05159a02005-02-26 23:04:13 +00005525 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005527 copy_tv(tv, &li->li_tv);
5528 list_append(l, li);
5529 return OK;
5530}
5531
5532/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005533 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005534 * Return FAIL when out of memory.
5535 */
5536 int
5537list_append_dict(list, dict)
5538 list_T *list;
5539 dict_T *dict;
5540{
5541 listitem_T *li = listitem_alloc();
5542
5543 if (li == NULL)
5544 return FAIL;
5545 li->li_tv.v_type = VAR_DICT;
5546 li->li_tv.v_lock = 0;
5547 li->li_tv.vval.v_dict = dict;
5548 list_append(list, li);
5549 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550 return OK;
5551}
5552
5553/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005554 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005555 * If "item" is NULL append at the end.
5556 * Return FAIL when out of memory.
5557 */
5558 static int
5559list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005560 list_T *l;
5561 typval_T *tv;
5562 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005563{
Bram Moolenaar33570922005-01-25 22:26:29 +00005564 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005565
5566 if (ni == NULL)
5567 return FAIL;
5568 copy_tv(tv, &ni->li_tv);
5569 if (item == NULL)
5570 /* Append new item at end of list. */
5571 list_append(l, ni);
5572 else
5573 {
5574 /* Insert new item before existing item. */
5575 ni->li_prev = item->li_prev;
5576 ni->li_next = item;
5577 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005578 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005579 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005580 ++l->lv_idx;
5581 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005582 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005583 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005584 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005585 l->lv_idx_item = NULL;
5586 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005587 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005588 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005589 }
5590 return OK;
5591}
5592
5593/*
5594 * Extend "l1" with "l2".
5595 * If "bef" is NULL append at the end, otherwise insert before this item.
5596 * Returns FAIL when out of memory.
5597 */
5598 static int
5599list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 list_T *l1;
5601 list_T *l2;
5602 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005603{
Bram Moolenaar33570922005-01-25 22:26:29 +00005604 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005605
5606 for (item = l2->lv_first; item != NULL; item = item->li_next)
5607 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5608 return FAIL;
5609 return OK;
5610}
5611
5612/*
5613 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5614 * Return FAIL when out of memory.
5615 */
5616 static int
5617list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005618 list_T *l1;
5619 list_T *l2;
5620 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005621{
Bram Moolenaar33570922005-01-25 22:26:29 +00005622 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005623
5624 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005625 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005626 if (l == NULL)
5627 return FAIL;
5628 tv->v_type = VAR_LIST;
5629 tv->vval.v_list = l;
5630
5631 /* append all items from the second list */
5632 return list_extend(l, l2, NULL);
5633}
5634
5635/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005636 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005637 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005638 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005639 * Returns NULL when out of memory.
5640 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005641 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005642list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005643 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005645 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646{
Bram Moolenaar33570922005-01-25 22:26:29 +00005647 list_T *copy;
5648 listitem_T *item;
5649 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005650
5651 if (orig == NULL)
5652 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005653
5654 copy = list_alloc();
5655 if (copy != NULL)
5656 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005657 if (copyID != 0)
5658 {
5659 /* Do this before adding the items, because one of the items may
5660 * refer back to this list. */
5661 orig->lv_copyID = copyID;
5662 orig->lv_copylist = copy;
5663 }
5664 for (item = orig->lv_first; item != NULL && !got_int;
5665 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005666 {
5667 ni = listitem_alloc();
5668 if (ni == NULL)
5669 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005670 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005671 {
5672 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5673 {
5674 vim_free(ni);
5675 break;
5676 }
5677 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005678 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005680 list_append(copy, ni);
5681 }
5682 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005683 if (item != NULL)
5684 {
5685 list_unref(copy);
5686 copy = NULL;
5687 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005688 }
5689
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005690 return copy;
5691}
5692
5693/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005694 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005695 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005697 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005698list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005699 list_T *l;
5700 listitem_T *item;
5701 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005702{
Bram Moolenaar33570922005-01-25 22:26:29 +00005703 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005704
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005705 /* notify watchers */
5706 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005707 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005708 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005709 list_fix_watch(l, ip);
5710 if (ip == item2)
5711 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005712 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005713
5714 if (item2->li_next == NULL)
5715 l->lv_last = item->li_prev;
5716 else
5717 item2->li_next->li_prev = item->li_prev;
5718 if (item->li_prev == NULL)
5719 l->lv_first = item2->li_next;
5720 else
5721 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005722 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005723}
5724
5725/*
5726 * Return an allocated string with the string representation of a list.
5727 * May return NULL.
5728 */
5729 static char_u *
5730list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005731 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732{
5733 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005734
5735 if (tv->vval.v_list == NULL)
5736 return NULL;
5737 ga_init2(&ga, (int)sizeof(char), 80);
5738 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005739 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5740 {
5741 vim_free(ga.ga_data);
5742 return NULL;
5743 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005744 ga_append(&ga, ']');
5745 ga_append(&ga, NUL);
5746 return (char_u *)ga.ga_data;
5747}
5748
5749/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005750 * Join list "l" into a string in "*gap", using separator "sep".
5751 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005752 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005753 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005754 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755list_join(gap, l, sep, echo)
5756 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005757 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005758 char_u *sep;
5759 int echo;
5760{
5761 int first = TRUE;
5762 char_u *tofree;
5763 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005764 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005765 char_u *s;
5766
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005767 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005768 {
5769 if (first)
5770 first = FALSE;
5771 else
5772 ga_concat(gap, sep);
5773
5774 if (echo)
5775 s = echo_string(&item->li_tv, &tofree, numbuf);
5776 else
5777 s = tv2string(&item->li_tv, &tofree, numbuf);
5778 if (s != NULL)
5779 ga_concat(gap, s);
5780 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005781 if (s == NULL)
5782 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005784 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005785}
5786
5787/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005788 * Garbage collection for lists and dictionaries.
5789 *
5790 * We use reference counts to be able to free most items right away when they
5791 * are no longer used. But for composite items it's possible that it becomes
5792 * unused while the reference count is > 0: When there is a recursive
5793 * reference. Example:
5794 * :let l = [1, 2, 3]
5795 * :let d = {9: l}
5796 * :let l[1] = d
5797 *
5798 * Since this is quite unusual we handle this with garbage collection: every
5799 * once in a while find out which lists and dicts are not referenced from any
5800 * variable.
5801 *
5802 * Here is a good reference text about garbage collection (refers to Python
5803 * but it applies to all reference-counting mechanisms):
5804 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005805 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005806
5807/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005808 * Do garbage collection for lists and dicts.
5809 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005810 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005811 int
5812garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005813{
5814 dict_T *dd;
5815 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005816 int copyID = ++current_copyID;
5817 buf_T *buf;
5818 win_T *wp;
5819 int i;
5820 funccall_T *fc;
5821 int did_free = FALSE;
5822
5823 /*
5824 * 1. Go through all accessible variables and mark all lists and dicts
5825 * with copyID.
5826 */
5827 /* script-local variables */
5828 for (i = 1; i <= ga_scripts.ga_len; ++i)
5829 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5830
5831 /* buffer-local variables */
5832 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5833 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5834
5835 /* window-local variables */
5836 FOR_ALL_WINDOWS(wp)
5837 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5838
5839 /* global variables */
5840 set_ref_in_ht(&globvarht, copyID);
5841
5842 /* function-local variables */
5843 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5844 {
5845 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5846 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5847 }
5848
5849 /*
5850 * 2. Go through the list of dicts and free items without the copyID.
5851 */
5852 for (dd = first_dict; dd != NULL; )
5853 if (dd->dv_copyID != copyID)
5854 {
5855 dict_free(dd);
5856 did_free = TRUE;
5857
5858 /* restart, next dict may also have been freed */
5859 dd = first_dict;
5860 }
5861 else
5862 dd = dd->dv_used_next;
5863
5864 /*
5865 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005866 * But don't free a list that has a watcher (used in a for loop), these
5867 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005868 */
5869 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005870 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005871 {
5872 list_free(ll);
5873 did_free = TRUE;
5874
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005875 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005876 ll = first_list;
5877 }
5878 else
5879 ll = ll->lv_used_next;
5880
5881 return did_free;
5882}
5883
5884/*
5885 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5886 */
5887 static void
5888set_ref_in_ht(ht, copyID)
5889 hashtab_T *ht;
5890 int copyID;
5891{
5892 int todo;
5893 hashitem_T *hi;
5894
5895 todo = ht->ht_used;
5896 for (hi = ht->ht_array; todo > 0; ++hi)
5897 if (!HASHITEM_EMPTY(hi))
5898 {
5899 --todo;
5900 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5901 }
5902}
5903
5904/*
5905 * Mark all lists and dicts referenced through list "l" with "copyID".
5906 */
5907 static void
5908set_ref_in_list(l, copyID)
5909 list_T *l;
5910 int copyID;
5911{
5912 listitem_T *li;
5913
5914 for (li = l->lv_first; li != NULL; li = li->li_next)
5915 set_ref_in_item(&li->li_tv, copyID);
5916}
5917
5918/*
5919 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5920 */
5921 static void
5922set_ref_in_item(tv, copyID)
5923 typval_T *tv;
5924 int copyID;
5925{
5926 dict_T *dd;
5927 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005928
5929 switch (tv->v_type)
5930 {
5931 case VAR_DICT:
5932 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005933 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005934 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005935 /* Didn't see this dict yet. */
5936 dd->dv_copyID = copyID;
5937 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005938 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005939 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005940
5941 case VAR_LIST:
5942 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005943 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005944 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005945 /* Didn't see this list yet. */
5946 ll->lv_copyID = copyID;
5947 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005948 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005949 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005950 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005951 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005952}
5953
5954/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005955 * Allocate an empty header for a dictionary.
5956 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005957 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005958dict_alloc()
5959{
Bram Moolenaar33570922005-01-25 22:26:29 +00005960 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005961
Bram Moolenaar33570922005-01-25 22:26:29 +00005962 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005963 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005964 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005965 /* Add the list to the hashtable for garbage collection. */
5966 if (first_dict != NULL)
5967 first_dict->dv_used_prev = d;
5968 d->dv_used_next = first_dict;
5969 d->dv_used_prev = NULL;
5970
Bram Moolenaar33570922005-01-25 22:26:29 +00005971 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005972 d->dv_lock = 0;
5973 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005974 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005975 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005976 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005977}
5978
5979/*
5980 * Unreference a Dictionary: decrement the reference count and free it when it
5981 * becomes zero.
5982 */
5983 static void
5984dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005985 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005986{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005987 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
5988 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005989}
5990
5991/*
5992 * Free a Dictionary, including all items it contains.
5993 * Ignores the reference count.
5994 */
5995 static void
5996dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005997 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005998{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005999 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006001 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006002
Bram Moolenaard9fba312005-06-26 22:34:35 +00006003 /* Avoid that recursive reference to the dict frees us again. */
6004 d->dv_refcount = DEL_REFCOUNT;
6005
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006006 /* Remove the dict from the list of dicts for garbage collection. */
6007 if (d->dv_used_prev == NULL)
6008 first_dict = d->dv_used_next;
6009 else
6010 d->dv_used_prev->dv_used_next = d->dv_used_next;
6011 if (d->dv_used_next != NULL)
6012 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6013
6014 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006015 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006016 todo = d->dv_hashtab.ht_used;
6017 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006018 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006019 if (!HASHITEM_EMPTY(hi))
6020 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006021 /* Remove the item before deleting it, just in case there is
6022 * something recursive causing trouble. */
6023 di = HI2DI(hi);
6024 hash_remove(&d->dv_hashtab, hi);
6025 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006026 --todo;
6027 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006028 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006030 vim_free(d);
6031}
6032
6033/*
6034 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006035 * The "key" is copied to the new item.
6036 * Note that the value of the item "di_tv" still needs to be initialized!
6037 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006038 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006039 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006040dictitem_alloc(key)
6041 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006042{
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006044
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006046 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006047 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006048 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006049 di->di_flags = 0;
6050 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006051 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006052}
6053
6054/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006055 * Make a copy of a Dictionary item.
6056 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006058dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006059 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006060{
Bram Moolenaar33570922005-01-25 22:26:29 +00006061 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006062
Bram Moolenaar33570922005-01-25 22:26:29 +00006063 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006064 if (di != NULL)
6065 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006066 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006067 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006068 copy_tv(&org->di_tv, &di->di_tv);
6069 }
6070 return di;
6071}
6072
6073/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006074 * Remove item "item" from Dictionary "dict" and free it.
6075 */
6076 static void
6077dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 dict_T *dict;
6079 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006080{
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006082
Bram Moolenaar33570922005-01-25 22:26:29 +00006083 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006084 if (HASHITEM_EMPTY(hi))
6085 EMSG2(_(e_intern2), "dictitem_remove()");
6086 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006087 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006088 dictitem_free(item);
6089}
6090
6091/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006092 * Free a dict item. Also clears the value.
6093 */
6094 static void
6095dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006096 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006097{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006098 clear_tv(&item->di_tv);
6099 vim_free(item);
6100}
6101
6102/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006103 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6104 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006105 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006106 * Returns NULL when out of memory.
6107 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006108 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006109dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006110 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006111 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006112 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006113{
Bram Moolenaar33570922005-01-25 22:26:29 +00006114 dict_T *copy;
6115 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006116 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006117 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006118
6119 if (orig == NULL)
6120 return NULL;
6121
6122 copy = dict_alloc();
6123 if (copy != NULL)
6124 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006125 if (copyID != 0)
6126 {
6127 orig->dv_copyID = copyID;
6128 orig->dv_copydict = copy;
6129 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006130 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006131 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006132 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006133 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006134 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006135 --todo;
6136
6137 di = dictitem_alloc(hi->hi_key);
6138 if (di == NULL)
6139 break;
6140 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006141 {
6142 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6143 copyID) == FAIL)
6144 {
6145 vim_free(di);
6146 break;
6147 }
6148 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006149 else
6150 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6151 if (dict_add(copy, di) == FAIL)
6152 {
6153 dictitem_free(di);
6154 break;
6155 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006156 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006157 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006158
Bram Moolenaare9a41262005-01-15 22:18:47 +00006159 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006160 if (todo > 0)
6161 {
6162 dict_unref(copy);
6163 copy = NULL;
6164 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006165 }
6166
6167 return copy;
6168}
6169
6170/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006171 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006172 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006173 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006174 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006175dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006176 dict_T *d;
6177 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006178{
Bram Moolenaar33570922005-01-25 22:26:29 +00006179 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006180}
6181
Bram Moolenaar8c711452005-01-14 21:53:12 +00006182/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006183 * Add a number or string entry to dictionary "d".
6184 * When "str" is NULL use number "nr", otherwise use "str".
6185 * Returns FAIL when out of memory and when key already exists.
6186 */
6187 int
6188dict_add_nr_str(d, key, nr, str)
6189 dict_T *d;
6190 char *key;
6191 long nr;
6192 char_u *str;
6193{
6194 dictitem_T *item;
6195
6196 item = dictitem_alloc((char_u *)key);
6197 if (item == NULL)
6198 return FAIL;
6199 item->di_tv.v_lock = 0;
6200 if (str == NULL)
6201 {
6202 item->di_tv.v_type = VAR_NUMBER;
6203 item->di_tv.vval.v_number = nr;
6204 }
6205 else
6206 {
6207 item->di_tv.v_type = VAR_STRING;
6208 item->di_tv.vval.v_string = vim_strsave(str);
6209 }
6210 if (dict_add(d, item) == FAIL)
6211 {
6212 dictitem_free(item);
6213 return FAIL;
6214 }
6215 return OK;
6216}
6217
6218/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006219 * Get the number of items in a Dictionary.
6220 */
6221 static long
6222dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006223 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006224{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006225 if (d == NULL)
6226 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006227 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006228}
6229
6230/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006231 * Find item "key[len]" in Dictionary "d".
6232 * If "len" is negative use strlen(key).
6233 * Returns NULL when not found.
6234 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006235 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006236dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006237 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006238 char_u *key;
6239 int len;
6240{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006241#define AKEYLEN 200
6242 char_u buf[AKEYLEN];
6243 char_u *akey;
6244 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006245 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006246
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006247 if (len < 0)
6248 akey = key;
6249 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006250 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006251 tofree = akey = vim_strnsave(key, len);
6252 if (akey == NULL)
6253 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006254 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006255 else
6256 {
6257 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006258 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006259 akey = buf;
6260 }
6261
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006263 vim_free(tofree);
6264 if (HASHITEM_EMPTY(hi))
6265 return NULL;
6266 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006267}
6268
6269/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006270 * Get a string item from a dictionary in allocated memory.
6271 * Returns NULL if the entry doesn't exist or out of memory.
6272 */
6273 char_u *
6274get_dict_string(d, key)
6275 dict_T *d;
6276 char_u *key;
6277{
6278 dictitem_T *di;
6279
6280 di = dict_find(d, key, -1);
6281 if (di == NULL)
6282 return NULL;
6283 return vim_strsave(get_tv_string(&di->di_tv));
6284}
6285
6286/*
6287 * Get a number item from a dictionary.
6288 * Returns 0 if the entry doesn't exist or out of memory.
6289 */
6290 long
6291get_dict_number(d, key)
6292 dict_T *d;
6293 char_u *key;
6294{
6295 dictitem_T *di;
6296
6297 di = dict_find(d, key, -1);
6298 if (di == NULL)
6299 return 0;
6300 return get_tv_number(&di->di_tv);
6301}
6302
6303/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006304 * Return an allocated string with the string representation of a Dictionary.
6305 * May return NULL.
6306 */
6307 static char_u *
6308dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006309 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006310{
6311 garray_T ga;
6312 int first = TRUE;
6313 char_u *tofree;
6314 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006315 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006316 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006317 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006318 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006319
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006320 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006321 return NULL;
6322 ga_init2(&ga, (int)sizeof(char), 80);
6323 ga_append(&ga, '{');
6324
Bram Moolenaar33570922005-01-25 22:26:29 +00006325 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006326 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006327 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006328 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006329 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006330 --todo;
6331
6332 if (first)
6333 first = FALSE;
6334 else
6335 ga_concat(&ga, (char_u *)", ");
6336
6337 tofree = string_quote(hi->hi_key, FALSE);
6338 if (tofree != NULL)
6339 {
6340 ga_concat(&ga, tofree);
6341 vim_free(tofree);
6342 }
6343 ga_concat(&ga, (char_u *)": ");
6344 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6345 if (s != NULL)
6346 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006347 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006348 if (s == NULL)
6349 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006350 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006351 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006352 if (todo > 0)
6353 {
6354 vim_free(ga.ga_data);
6355 return NULL;
6356 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006357
6358 ga_append(&ga, '}');
6359 ga_append(&ga, NUL);
6360 return (char_u *)ga.ga_data;
6361}
6362
6363/*
6364 * Allocate a variable for a Dictionary and fill it from "*arg".
6365 * Return OK or FAIL. Returns NOTDONE for {expr}.
6366 */
6367 static int
6368get_dict_tv(arg, rettv, evaluate)
6369 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006370 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006371 int evaluate;
6372{
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 dict_T *d = NULL;
6374 typval_T tvkey;
6375 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006376 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006378 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006379 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006380
6381 /*
6382 * First check if it's not a curly-braces thing: {expr}.
6383 * Must do this without evaluating, otherwise a function may be called
6384 * twice. Unfortunately this means we need to call eval1() twice for the
6385 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006386 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006387 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006388 if (*start != '}')
6389 {
6390 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6391 return FAIL;
6392 if (*start == '}')
6393 return NOTDONE;
6394 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006395
6396 if (evaluate)
6397 {
6398 d = dict_alloc();
6399 if (d == NULL)
6400 return FAIL;
6401 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006402 tvkey.v_type = VAR_UNKNOWN;
6403 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006404
6405 *arg = skipwhite(*arg + 1);
6406 while (**arg != '}' && **arg != NUL)
6407 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006408 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006409 goto failret;
6410 if (**arg != ':')
6411 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006412 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006413 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006414 goto failret;
6415 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006416 key = get_tv_string_buf_chk(&tvkey, buf);
6417 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006419 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6420 if (key != NULL)
6421 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006422 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006423 goto failret;
6424 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006425
6426 *arg = skipwhite(*arg + 1);
6427 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6428 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006429 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006430 goto failret;
6431 }
6432 if (evaluate)
6433 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006434 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006435 if (item != NULL)
6436 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006437 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006438 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006439 clear_tv(&tv);
6440 goto failret;
6441 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006442 item = dictitem_alloc(key);
6443 clear_tv(&tvkey);
6444 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006445 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006446 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006447 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006448 if (dict_add(d, item) == FAIL)
6449 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006450 }
6451 }
6452
6453 if (**arg == '}')
6454 break;
6455 if (**arg != ',')
6456 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006457 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006458 goto failret;
6459 }
6460 *arg = skipwhite(*arg + 1);
6461 }
6462
6463 if (**arg != '}')
6464 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006465 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006466failret:
6467 if (evaluate)
6468 dict_free(d);
6469 return FAIL;
6470 }
6471
6472 *arg = skipwhite(*arg + 1);
6473 if (evaluate)
6474 {
6475 rettv->v_type = VAR_DICT;
6476 rettv->vval.v_dict = d;
6477 ++d->dv_refcount;
6478 }
6479
6480 return OK;
6481}
6482
Bram Moolenaar8c711452005-01-14 21:53:12 +00006483/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006484 * Return a string with the string representation of a variable.
6485 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006486 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006487 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006488 * May return NULL;
6489 */
6490 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006491echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006492 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006493 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006495{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006496 static int recurse = 0;
6497 char_u *r = NULL;
6498
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006500 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006501 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006502 *tofree = NULL;
6503 return NULL;
6504 }
6505 ++recurse;
6506
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006507 switch (tv->v_type)
6508 {
6509 case VAR_FUNC:
6510 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006511 r = tv->vval.v_string;
6512 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513 case VAR_LIST:
6514 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006515 r = *tofree;
6516 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006517 case VAR_DICT:
6518 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006519 r = *tofree;
6520 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006521 case VAR_STRING:
6522 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006523 *tofree = NULL;
6524 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006525 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006526 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006527 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006528 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006529 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006530
6531 --recurse;
6532 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006533}
6534
6535/*
6536 * Return a string with the string representation of a variable.
6537 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6538 * "numbuf" is used for a number.
6539 * Puts quotes around strings, so that they can be parsed back by eval().
6540 * May return NULL;
6541 */
6542 static char_u *
6543tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006544 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006545 char_u **tofree;
6546 char_u *numbuf;
6547{
6548 switch (tv->v_type)
6549 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006550 case VAR_FUNC:
6551 *tofree = string_quote(tv->vval.v_string, TRUE);
6552 return *tofree;
6553 case VAR_STRING:
6554 *tofree = string_quote(tv->vval.v_string, FALSE);
6555 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006556 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006557 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006558 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006559 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006560 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006561 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006563 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564}
6565
6566/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006567 * Return string "str" in ' quotes, doubling ' characters.
6568 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006569 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006570 */
6571 static char_u *
6572string_quote(str, function)
6573 char_u *str;
6574 int function;
6575{
Bram Moolenaar33570922005-01-25 22:26:29 +00006576 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006577 char_u *p, *r, *s;
6578
Bram Moolenaar33570922005-01-25 22:26:29 +00006579 len = (function ? 13 : 3);
6580 if (str != NULL)
6581 {
6582 len += STRLEN(str);
6583 for (p = str; *p != NUL; mb_ptr_adv(p))
6584 if (*p == '\'')
6585 ++len;
6586 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006587 s = r = alloc(len);
6588 if (r != NULL)
6589 {
6590 if (function)
6591 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006592 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006593 r += 10;
6594 }
6595 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006596 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006597 if (str != NULL)
6598 for (p = str; *p != NUL; )
6599 {
6600 if (*p == '\'')
6601 *r++ = '\'';
6602 MB_COPY_CHAR(p, r);
6603 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006604 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006605 if (function)
6606 *r++ = ')';
6607 *r++ = NUL;
6608 }
6609 return s;
6610}
6611
6612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 * Get the value of an environment variable.
6614 * "arg" is pointing to the '$'. It is advanced to after the name.
6615 * If the environment variable was not set, silently assume it is empty.
6616 * Always return OK.
6617 */
6618 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006619get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 int evaluate;
6623{
6624 char_u *string = NULL;
6625 int len;
6626 int cc;
6627 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006628 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629
6630 ++*arg;
6631 name = *arg;
6632 len = get_env_len(arg);
6633 if (evaluate)
6634 {
6635 if (len != 0)
6636 {
6637 cc = name[len];
6638 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006639 /* first try vim_getenv(), fast for normal environment vars */
6640 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006642 {
6643 if (!mustfree)
6644 string = vim_strsave(string);
6645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 else
6647 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006648 if (mustfree)
6649 vim_free(string);
6650
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 /* next try expanding things like $VIM and ${HOME} */
6652 string = expand_env_save(name - 1);
6653 if (string != NULL && *string == '$')
6654 {
6655 vim_free(string);
6656 string = NULL;
6657 }
6658 }
6659 name[len] = cc;
6660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006661 rettv->v_type = VAR_STRING;
6662 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 }
6664
6665 return OK;
6666}
6667
6668/*
6669 * Array with names and number of arguments of all internal functions
6670 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6671 */
6672static struct fst
6673{
6674 char *f_name; /* function name */
6675 char f_min_argc; /* minimal number of arguments */
6676 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006677 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679} functions[] =
6680{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006681 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 {"append", 2, 2, f_append},
6683 {"argc", 0, 0, f_argc},
6684 {"argidx", 0, 0, f_argidx},
6685 {"argv", 1, 1, f_argv},
6686 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006687 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 {"bufexists", 1, 1, f_bufexists},
6689 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6690 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6691 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6692 {"buflisted", 1, 1, f_buflisted},
6693 {"bufloaded", 1, 1, f_bufloaded},
6694 {"bufname", 1, 1, f_bufname},
6695 {"bufnr", 1, 1, f_bufnr},
6696 {"bufwinnr", 1, 1, f_bufwinnr},
6697 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006698 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006699 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 {"char2nr", 1, 1, f_char2nr},
6701 {"cindent", 1, 1, f_cindent},
6702 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006703#if defined(FEAT_INS_EXPAND)
6704 {"complete_add", 1, 1, f_complete_add},
6705 {"complete_check", 0, 0, f_complete_check},
6706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006708 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006709 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 {"cscope_connection",0,3, f_cscope_connection},
6711 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006712 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 {"delete", 1, 1, f_delete},
6714 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006715 {"diff_filler", 1, 1, f_diff_filler},
6716 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006717 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006719 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 {"eventhandler", 0, 0, f_eventhandler},
6721 {"executable", 1, 1, f_executable},
6722 {"exists", 1, 1, f_exists},
6723 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006724 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6726 {"filereadable", 1, 1, f_filereadable},
6727 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006728 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006729 {"finddir", 1, 3, f_finddir},
6730 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 {"fnamemodify", 2, 2, f_fnamemodify},
6732 {"foldclosed", 1, 1, f_foldclosed},
6733 {"foldclosedend", 1, 1, f_foldclosedend},
6734 {"foldlevel", 1, 1, f_foldlevel},
6735 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006736 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006738 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006739 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006740 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006741 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 {"getbufvar", 2, 2, f_getbufvar},
6743 {"getchar", 0, 1, f_getchar},
6744 {"getcharmod", 0, 0, f_getcharmod},
6745 {"getcmdline", 0, 0, f_getcmdline},
6746 {"getcmdpos", 0, 0, f_getcmdpos},
6747 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006748 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006749 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {"getfsize", 1, 1, f_getfsize},
6751 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006752 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006753 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006754 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006755 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 {"getregtype", 0, 1, f_getregtype},
6757 {"getwinposx", 0, 0, f_getwinposx},
6758 {"getwinposy", 0, 0, f_getwinposy},
6759 {"getwinvar", 2, 2, f_getwinvar},
6760 {"glob", 1, 1, f_glob},
6761 {"globpath", 2, 2, f_globpath},
6762 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006763 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 {"hasmapto", 1, 2, f_hasmapto},
6765 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6766 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6767 {"histadd", 2, 2, f_histadd},
6768 {"histdel", 1, 2, f_histdel},
6769 {"histget", 1, 2, f_histget},
6770 {"histnr", 1, 1, f_histnr},
6771 {"hlID", 1, 1, f_hlID},
6772 {"hlexists", 1, 1, f_hlexists},
6773 {"hostname", 0, 0, f_hostname},
6774 {"iconv", 3, 3, f_iconv},
6775 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {"input", 1, 2, f_input},
6778 {"inputdialog", 1, 3, f_inputdialog},
6779 {"inputrestore", 0, 0, f_inputrestore},
6780 {"inputsave", 0, 0, f_inputsave},
6781 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006782 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006784 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006785 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006786 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006787 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006789 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 {"libcall", 3, 3, f_libcall},
6791 {"libcallnr", 3, 3, f_libcallnr},
6792 {"line", 1, 1, f_line},
6793 {"line2byte", 1, 1, f_line2byte},
6794 {"lispindent", 1, 1, f_lispindent},
6795 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006796 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 {"maparg", 1, 2, f_maparg},
6798 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006799 {"match", 2, 4, f_match},
6800 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006801 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006802 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006803 {"max", 1, 1, f_max},
6804 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006805#ifdef vim_mkdir
6806 {"mkdir", 1, 3, f_mkdir},
6807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {"mode", 0, 0, f_mode},
6809 {"nextnonblank", 1, 1, f_nextnonblank},
6810 {"nr2char", 1, 1, f_nr2char},
6811 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006812 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006813 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006814 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 {"remote_expr", 2, 3, f_remote_expr},
6816 {"remote_foreground", 1, 1, f_remote_foreground},
6817 {"remote_peek", 1, 2, f_remote_peek},
6818 {"remote_read", 1, 1, f_remote_read},
6819 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006820 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006822 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006824 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 {"search", 1, 2, f_search},
Bram Moolenaardd2436f2005-09-05 22:14:46 +00006826 {"searchdecl", 1, 2, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 {"searchpair", 3, 5, f_searchpair},
6828 {"server2client", 2, 2, f_server2client},
6829 {"serverlist", 0, 0, f_serverlist},
6830 {"setbufvar", 3, 3, f_setbufvar},
6831 {"setcmdpos", 1, 1, f_setcmdpos},
6832 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006833 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 {"setreg", 2, 3, f_setreg},
6835 {"setwinvar", 3, 3, f_setwinvar},
6836 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006837 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006838 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006839 {"spellbadword", 0, 0, f_spellbadword},
6840 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006841 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842#ifdef HAVE_STRFTIME
6843 {"strftime", 1, 2, f_strftime},
6844#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006845 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006846 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 {"strlen", 1, 1, f_strlen},
6848 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006849 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 {"strtrans", 1, 1, f_strtrans},
6851 {"submatch", 1, 1, f_submatch},
6852 {"substitute", 4, 4, f_substitute},
6853 {"synID", 3, 3, f_synID},
6854 {"synIDattr", 2, 3, f_synIDattr},
6855 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006856 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006857 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006859 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 {"tolower", 1, 1, f_tolower},
6861 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006862 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006864 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 {"virtcol", 1, 1, f_virtcol},
6866 {"visualmode", 0, 1, f_visualmode},
6867 {"winbufnr", 1, 1, f_winbufnr},
6868 {"wincol", 0, 0, f_wincol},
6869 {"winheight", 1, 1, f_winheight},
6870 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006871 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 {"winrestcmd", 0, 0, f_winrestcmd},
6873 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006874 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875};
6876
6877#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6878
6879/*
6880 * Function given to ExpandGeneric() to obtain the list of internal
6881 * or user defined function names.
6882 */
6883 char_u *
6884get_function_name(xp, idx)
6885 expand_T *xp;
6886 int idx;
6887{
6888 static int intidx = -1;
6889 char_u *name;
6890
6891 if (idx == 0)
6892 intidx = -1;
6893 if (intidx < 0)
6894 {
6895 name = get_user_func_name(xp, idx);
6896 if (name != NULL)
6897 return name;
6898 }
6899 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6900 {
6901 STRCPY(IObuff, functions[intidx].f_name);
6902 STRCAT(IObuff, "(");
6903 if (functions[intidx].f_max_argc == 0)
6904 STRCAT(IObuff, ")");
6905 return IObuff;
6906 }
6907
6908 return NULL;
6909}
6910
6911/*
6912 * Function given to ExpandGeneric() to obtain the list of internal or
6913 * user defined variable or function names.
6914 */
6915/*ARGSUSED*/
6916 char_u *
6917get_expr_name(xp, idx)
6918 expand_T *xp;
6919 int idx;
6920{
6921 static int intidx = -1;
6922 char_u *name;
6923
6924 if (idx == 0)
6925 intidx = -1;
6926 if (intidx < 0)
6927 {
6928 name = get_function_name(xp, idx);
6929 if (name != NULL)
6930 return name;
6931 }
6932 return get_user_var_name(xp, ++intidx);
6933}
6934
6935#endif /* FEAT_CMDL_COMPL */
6936
6937/*
6938 * Find internal function in table above.
6939 * Return index, or -1 if not found
6940 */
6941 static int
6942find_internal_func(name)
6943 char_u *name; /* name of the function */
6944{
6945 int first = 0;
6946 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6947 int cmp;
6948 int x;
6949
6950 /*
6951 * Find the function name in the table. Binary search.
6952 */
6953 while (first <= last)
6954 {
6955 x = first + ((unsigned)(last - first) >> 1);
6956 cmp = STRCMP(name, functions[x].f_name);
6957 if (cmp < 0)
6958 last = x - 1;
6959 else if (cmp > 0)
6960 first = x + 1;
6961 else
6962 return x;
6963 }
6964 return -1;
6965}
6966
6967/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006968 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6969 * name it contains, otherwise return "name".
6970 */
6971 static char_u *
6972deref_func_name(name, lenp)
6973 char_u *name;
6974 int *lenp;
6975{
Bram Moolenaar33570922005-01-25 22:26:29 +00006976 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006977 int cc;
6978
6979 cc = name[*lenp];
6980 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006981 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006982 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006983 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006984 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006985 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006986 {
6987 *lenp = 0;
6988 return (char_u *)""; /* just in case */
6989 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006990 *lenp = STRLEN(v->di_tv.vval.v_string);
6991 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006992 }
6993
6994 return name;
6995}
6996
6997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 * Allocate a variable for the result of a function.
6999 * Return OK or FAIL.
7000 */
7001 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007002get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7003 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 char_u *name; /* name of the function */
7005 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 char_u **arg; /* argument, pointing to the '(' */
7008 linenr_T firstline; /* first line of range */
7009 linenr_T lastline; /* last line of range */
7010 int *doesrange; /* return: function handled range */
7011 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007012 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013{
7014 char_u *argp;
7015 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007016 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 int argcount = 0; /* number of arguments found */
7018
7019 /*
7020 * Get the arguments.
7021 */
7022 argp = *arg;
7023 while (argcount < MAX_FUNC_ARGS)
7024 {
7025 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7026 if (*argp == ')' || *argp == ',' || *argp == NUL)
7027 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7029 {
7030 ret = FAIL;
7031 break;
7032 }
7033 ++argcount;
7034 if (*argp != ',')
7035 break;
7036 }
7037 if (*argp == ')')
7038 ++argp;
7039 else
7040 ret = FAIL;
7041
7042 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007043 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007044 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007046 {
7047 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007048 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007049 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007050 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052
7053 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007054 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
7056 *arg = skipwhite(argp);
7057 return ret;
7058}
7059
7060
7061/*
7062 * Call a function with its resolved parameters
7063 * Return OK or FAIL.
7064 */
7065 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007066call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007067 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 char_u *name; /* name of the function */
7069 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007070 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007072 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 linenr_T firstline; /* first line of range */
7074 linenr_T lastline; /* last line of range */
7075 int *doesrange; /* return: function handled range */
7076 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007077 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078{
7079 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080#define ERROR_UNKNOWN 0
7081#define ERROR_TOOMANY 1
7082#define ERROR_TOOFEW 2
7083#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007084#define ERROR_DICT 4
7085#define ERROR_NONE 5
7086#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 int error = ERROR_NONE;
7088 int i;
7089 int llen;
7090 ufunc_T *fp;
7091 int cc;
7092#define FLEN_FIXED 40
7093 char_u fname_buf[FLEN_FIXED + 1];
7094 char_u *fname;
7095
7096 /*
7097 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7098 * Change <SNR>123_name() to K_SNR 123_name().
7099 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7100 */
7101 cc = name[len];
7102 name[len] = NUL;
7103 llen = eval_fname_script(name);
7104 if (llen > 0)
7105 {
7106 fname_buf[0] = K_SPECIAL;
7107 fname_buf[1] = KS_EXTRA;
7108 fname_buf[2] = (int)KE_SNR;
7109 i = 3;
7110 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7111 {
7112 if (current_SID <= 0)
7113 error = ERROR_SCRIPT;
7114 else
7115 {
7116 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7117 i = (int)STRLEN(fname_buf);
7118 }
7119 }
7120 if (i + STRLEN(name + llen) < FLEN_FIXED)
7121 {
7122 STRCPY(fname_buf + i, name + llen);
7123 fname = fname_buf;
7124 }
7125 else
7126 {
7127 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7128 if (fname == NULL)
7129 error = ERROR_OTHER;
7130 else
7131 {
7132 mch_memmove(fname, fname_buf, (size_t)i);
7133 STRCPY(fname + i, name + llen);
7134 }
7135 }
7136 }
7137 else
7138 fname = name;
7139
7140 *doesrange = FALSE;
7141
7142
7143 /* execute the function if no errors detected and executing */
7144 if (evaluate && error == ERROR_NONE)
7145 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007146 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 error = ERROR_UNKNOWN;
7148
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007149 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 {
7151 /*
7152 * User defined function.
7153 */
7154 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007155
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007157 /* Trigger FuncUndefined event, may load the function. */
7158 if (fp == NULL
7159 && apply_autocmds(EVENT_FUNCUNDEFINED,
7160 fname, fname, TRUE, NULL)
7161 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007163 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 fp = find_func(fname);
7165 }
7166#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007167 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007168 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007169 {
7170 /* loaded a package, search for the function again */
7171 fp = find_func(fname);
7172 }
7173
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 if (fp != NULL)
7175 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007176 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007178 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007180 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007182 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007183 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 else
7185 {
7186 /*
7187 * Call the user function.
7188 * Save and restore search patterns, script variables and
7189 * redo buffer.
7190 */
7191 save_search_patterns();
7192 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007193 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007194 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007195 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007196 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7197 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7198 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007199 /* Function was unreferenced while being used, free it
7200 * now. */
7201 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 restoreRedobuff();
7203 restore_search_patterns();
7204 error = ERROR_NONE;
7205 }
7206 }
7207 }
7208 else
7209 {
7210 /*
7211 * Find the function name in the table, call its implementation.
7212 */
7213 i = find_internal_func(fname);
7214 if (i >= 0)
7215 {
7216 if (argcount < functions[i].f_min_argc)
7217 error = ERROR_TOOFEW;
7218 else if (argcount > functions[i].f_max_argc)
7219 error = ERROR_TOOMANY;
7220 else
7221 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007222 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007223 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 error = ERROR_NONE;
7225 }
7226 }
7227 }
7228 /*
7229 * The function call (or "FuncUndefined" autocommand sequence) might
7230 * have been aborted by an error, an interrupt, or an explicitly thrown
7231 * exception that has not been caught so far. This situation can be
7232 * tested for by calling aborting(). For an error in an internal
7233 * function or for the "E132" error in call_user_func(), however, the
7234 * throw point at which the "force_abort" flag (temporarily reset by
7235 * emsg()) is normally updated has not been reached yet. We need to
7236 * update that flag first to make aborting() reliable.
7237 */
7238 update_force_abort();
7239 }
7240 if (error == ERROR_NONE)
7241 ret = OK;
7242
7243 /*
7244 * Report an error unless the argument evaluation or function call has been
7245 * cancelled due to an aborting error, an interrupt, or an exception.
7246 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247 if (!aborting())
7248 {
7249 switch (error)
7250 {
7251 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007252 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253 break;
7254 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007255 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007256 break;
7257 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007258 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259 name);
7260 break;
7261 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007262 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263 name);
7264 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007265 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007266 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007267 name);
7268 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269 }
7270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
7272 name[len] = cc;
7273 if (fname != name && fname != fname_buf)
7274 vim_free(fname);
7275
7276 return ret;
7277}
7278
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007279/*
7280 * Give an error message with a function name. Handle <SNR> things.
7281 */
7282 static void
7283emsg_funcname(msg, name)
7284 char *msg;
7285 char_u *name;
7286{
7287 char_u *p;
7288
7289 if (*name == K_SPECIAL)
7290 p = concat_str((char_u *)"<SNR>", name + 3);
7291 else
7292 p = name;
7293 EMSG2(_(msg), p);
7294 if (p != name)
7295 vim_free(p);
7296}
7297
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298/*********************************************
7299 * Implementation of the built-in functions
7300 */
7301
7302/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007303 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 */
7305 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007306f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 typval_T *argvars;
7308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309{
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007312 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007313 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007315 if ((l = argvars[0].vval.v_list) != NULL
7316 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7317 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007318 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007319 }
7320 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007321 EMSG(_(e_listreq));
7322}
7323
7324/*
7325 * "append(lnum, string/list)" function
7326 */
7327 static void
7328f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 typval_T *argvars;
7330 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007331{
7332 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007333 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 list_T *l = NULL;
7335 listitem_T *li = NULL;
7336 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007337 long added = 0;
7338
Bram Moolenaar0d660222005-01-07 21:51:51 +00007339 lnum = get_tv_lnum(argvars);
7340 if (lnum >= 0
7341 && lnum <= curbuf->b_ml.ml_line_count
7342 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007343 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007344 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007345 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007346 l = argvars[1].vval.v_list;
7347 if (l == NULL)
7348 return;
7349 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007350 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007351 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007352 for (;;)
7353 {
7354 if (l == NULL)
7355 tv = &argvars[1]; /* append a string */
7356 else if (li == NULL)
7357 break; /* end of list */
7358 else
7359 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007360 line = get_tv_string_chk(tv);
7361 if (line == NULL) /* type error */
7362 {
7363 rettv->vval.v_number = 1; /* Failed */
7364 break;
7365 }
7366 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007367 ++added;
7368 if (l == NULL)
7369 break;
7370 li = li->li_next;
7371 }
7372
7373 appended_lines_mark(lnum, added);
7374 if (curwin->w_cursor.lnum > lnum)
7375 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007377 else
7378 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379}
7380
7381/*
7382 * "argc()" function
7383 */
7384/* ARGSUSED */
7385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007386f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007387 typval_T *argvars;
7388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007390 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391}
7392
7393/*
7394 * "argidx()" function
7395 */
7396/* ARGSUSED */
7397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007398f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007399 typval_T *argvars;
7400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007402 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403}
7404
7405/*
7406 * "argv(nr)" function
7407 */
7408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007409f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007410 typval_T *argvars;
7411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
7413 int idx;
7414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007415 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007417 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007419 rettv->vval.v_string = NULL;
7420 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421}
7422
7423/*
7424 * "browse(save, title, initdir, default)" function
7425 */
7426/* ARGSUSED */
7427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007428f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 typval_T *argvars;
7430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431{
7432#ifdef FEAT_BROWSE
7433 int save;
7434 char_u *title;
7435 char_u *initdir;
7436 char_u *defname;
7437 char_u buf[NUMBUFLEN];
7438 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007439 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007441 save = get_tv_number_chk(&argvars[0], &error);
7442 title = get_tv_string_chk(&argvars[1]);
7443 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7444 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007446 if (error || title == NULL || initdir == NULL || defname == NULL)
7447 rettv->vval.v_string = NULL;
7448 else
7449 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007450 do_browse(save ? BROWSE_SAVE : 0,
7451 title, defname, NULL, initdir, NULL, curbuf);
7452#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007453 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007454#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007455 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007456}
7457
7458/*
7459 * "browsedir(title, initdir)" function
7460 */
7461/* ARGSUSED */
7462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007463f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007464 typval_T *argvars;
7465 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007466{
7467#ifdef FEAT_BROWSE
7468 char_u *title;
7469 char_u *initdir;
7470 char_u buf[NUMBUFLEN];
7471
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007472 title = get_tv_string_chk(&argvars[0]);
7473 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007474
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007475 if (title == NULL || initdir == NULL)
7476 rettv->vval.v_string = NULL;
7477 else
7478 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007479 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007481 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007483 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484}
7485
Bram Moolenaar33570922005-01-25 22:26:29 +00007486static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007487
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488/*
7489 * Find a buffer by number or exact name.
7490 */
7491 static buf_T *
7492find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007493 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494{
7495 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007497 if (avar->v_type == VAR_NUMBER)
7498 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007499 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007501 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007502 if (buf == NULL)
7503 {
7504 /* No full path name match, try a match with a URL or a "nofile"
7505 * buffer, these don't use the full path. */
7506 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7507 if (buf->b_fname != NULL
7508 && (path_with_url(buf->b_fname)
7509#ifdef FEAT_QUICKFIX
7510 || bt_nofile(buf)
7511#endif
7512 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007513 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007514 break;
7515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 }
7517 return buf;
7518}
7519
7520/*
7521 * "bufexists(expr)" function
7522 */
7523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007524f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007525 typval_T *argvars;
7526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007528 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529}
7530
7531/*
7532 * "buflisted(expr)" function
7533 */
7534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007535f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007536 typval_T *argvars;
7537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538{
7539 buf_T *buf;
7540
7541 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007542 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543}
7544
7545/*
7546 * "bufloaded(expr)" function
7547 */
7548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007549f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007550 typval_T *argvars;
7551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552{
7553 buf_T *buf;
7554
7555 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007556 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557}
7558
Bram Moolenaar33570922005-01-25 22:26:29 +00007559static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007560
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561/*
7562 * Get buffer by number or pattern.
7563 */
7564 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007565get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007568 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 int save_magic;
7570 char_u *save_cpo;
7571 buf_T *buf;
7572
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007573 if (tv->v_type == VAR_NUMBER)
7574 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007575 if (tv->v_type != VAR_STRING)
7576 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 if (name == NULL || *name == NUL)
7578 return curbuf;
7579 if (name[0] == '$' && name[1] == NUL)
7580 return lastbuf;
7581
7582 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7583 save_magic = p_magic;
7584 p_magic = TRUE;
7585 save_cpo = p_cpo;
7586 p_cpo = (char_u *)"";
7587
7588 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7589 TRUE, FALSE));
7590
7591 p_magic = save_magic;
7592 p_cpo = save_cpo;
7593
7594 /* If not found, try expanding the name, like done for bufexists(). */
7595 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007596 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597
7598 return buf;
7599}
7600
7601/*
7602 * "bufname(expr)" function
7603 */
7604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007605f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 typval_T *argvars;
7607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608{
7609 buf_T *buf;
7610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007611 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007613 buf = get_buf_tv(&argvars[0]);
7614 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007616 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007618 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 --emsg_off;
7620}
7621
7622/*
7623 * "bufnr(expr)" function
7624 */
7625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007626f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 typval_T *argvars;
7628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629{
7630 buf_T *buf;
7631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007632 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007634 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007636 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007638 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 --emsg_off;
7640}
7641
7642/*
7643 * "bufwinnr(nr)" function
7644 */
7645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007646f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007647 typval_T *argvars;
7648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649{
7650#ifdef FEAT_WINDOWS
7651 win_T *wp;
7652 int winnr = 0;
7653#endif
7654 buf_T *buf;
7655
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007656 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007658 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659#ifdef FEAT_WINDOWS
7660 for (wp = firstwin; wp; wp = wp->w_next)
7661 {
7662 ++winnr;
7663 if (wp->w_buffer == buf)
7664 break;
7665 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007666 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007668 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669#endif
7670 --emsg_off;
7671}
7672
7673/*
7674 * "byte2line(byte)" function
7675 */
7676/*ARGSUSED*/
7677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007678f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 typval_T *argvars;
7680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681{
7682#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007683 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684#else
7685 long boff = 0;
7686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007687 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007689 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007691 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 (linenr_T)0, &boff);
7693#endif
7694}
7695
7696/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007697 * "byteidx()" function
7698 */
7699/*ARGSUSED*/
7700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007701f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007702 typval_T *argvars;
7703 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007704{
7705#ifdef FEAT_MBYTE
7706 char_u *t;
7707#endif
7708 char_u *str;
7709 long idx;
7710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007711 str = get_tv_string_chk(&argvars[0]);
7712 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007713 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007714 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007715 return;
7716
7717#ifdef FEAT_MBYTE
7718 t = str;
7719 for ( ; idx > 0; idx--)
7720 {
7721 if (*t == NUL) /* EOL reached */
7722 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007723 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007724 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007725 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007726#else
7727 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007728 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007729#endif
7730}
7731
7732/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007733 * "call(func, arglist)" function
7734 */
7735 static void
7736f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 typval_T *argvars;
7738 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007739{
7740 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007741 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007742 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007743 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007744 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007745 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007746
7747 rettv->vval.v_number = 0;
7748 if (argvars[1].v_type != VAR_LIST)
7749 {
7750 EMSG(_(e_listreq));
7751 return;
7752 }
7753 if (argvars[1].vval.v_list == NULL)
7754 return;
7755
7756 if (argvars[0].v_type == VAR_FUNC)
7757 func = argvars[0].vval.v_string;
7758 else
7759 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007760 if (*func == NUL)
7761 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007762
Bram Moolenaare9a41262005-01-15 22:18:47 +00007763 if (argvars[2].v_type != VAR_UNKNOWN)
7764 {
7765 if (argvars[2].v_type != VAR_DICT)
7766 {
7767 EMSG(_(e_dictreq));
7768 return;
7769 }
7770 selfdict = argvars[2].vval.v_dict;
7771 }
7772
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007773 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7774 item = item->li_next)
7775 {
7776 if (argc == MAX_FUNC_ARGS)
7777 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007778 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007779 break;
7780 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007781 /* Make a copy of each argument. This is needed to be able to set
7782 * v_lock to VAR_FIXED in the copy without changing the original list.
7783 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007784 copy_tv(&item->li_tv, &argv[argc++]);
7785 }
7786
7787 if (item == NULL)
7788 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007789 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7790 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007791
7792 /* Free the arguments. */
7793 while (argc > 0)
7794 clear_tv(&argv[--argc]);
7795}
7796
7797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 * "char2nr(string)" function
7799 */
7800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007801f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007802 typval_T *argvars;
7803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804{
7805#ifdef FEAT_MBYTE
7806 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007807 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 else
7809#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007810 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811}
7812
7813/*
7814 * "cindent(lnum)" function
7815 */
7816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007817f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007818 typval_T *argvars;
7819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820{
7821#ifdef FEAT_CINDENT
7822 pos_T pos;
7823 linenr_T lnum;
7824
7825 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007826 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7828 {
7829 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007830 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 curwin->w_cursor = pos;
7832 }
7833 else
7834#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007835 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836}
7837
7838/*
7839 * "col(string)" function
7840 */
7841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007842f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007843 typval_T *argvars;
7844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845{
7846 colnr_T col = 0;
7847 pos_T *fp;
7848
7849 fp = var2fpos(&argvars[0], FALSE);
7850 if (fp != NULL)
7851 {
7852 if (fp->col == MAXCOL)
7853 {
7854 /* '> can be MAXCOL, get the length of the line then */
7855 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7856 col = STRLEN(ml_get(fp->lnum)) + 1;
7857 else
7858 col = MAXCOL;
7859 }
7860 else
7861 {
7862 col = fp->col + 1;
7863#ifdef FEAT_VIRTUALEDIT
7864 /* col(".") when the cursor is on the NUL at the end of the line
7865 * because of "coladd" can be seen as an extra column. */
7866 if (virtual_active() && fp == &curwin->w_cursor)
7867 {
7868 char_u *p = ml_get_cursor();
7869
7870 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7871 curwin->w_virtcol - curwin->w_cursor.coladd))
7872 {
7873# ifdef FEAT_MBYTE
7874 int l;
7875
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007876 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 col += l;
7878# else
7879 if (*p != NUL && p[1] == NUL)
7880 ++col;
7881# endif
7882 }
7883 }
7884#endif
7885 }
7886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007887 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888}
7889
Bram Moolenaar572cb562005-08-05 21:35:02 +00007890#if defined(FEAT_INS_EXPAND)
7891/*
7892 * "complete_add()" function
7893 */
7894/*ARGSUSED*/
7895 static void
7896f_complete_add(argvars, rettv)
7897 typval_T *argvars;
7898 typval_T *rettv;
7899{
7900 char_u *s;
7901
7902 s = get_tv_string_chk(&argvars[0]);
7903 if (s != NULL)
7904 rettv->vval.v_number = ins_compl_add(s, -1, NULL, FORWARD, 0);
7905}
7906
7907/*
7908 * "complete_check()" function
7909 */
7910/*ARGSUSED*/
7911 static void
7912f_complete_check(argvars, rettv)
7913 typval_T *argvars;
7914 typval_T *rettv;
7915{
7916 int saved = RedrawingDisabled;
7917
7918 RedrawingDisabled = 0;
7919 ins_compl_check_keys(0);
7920 rettv->vval.v_number = compl_interrupted;
7921 RedrawingDisabled = saved;
7922}
7923#endif
7924
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925/*
7926 * "confirm(message, buttons[, default [, type]])" function
7927 */
7928/*ARGSUSED*/
7929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007930f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007931 typval_T *argvars;
7932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
7934#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7935 char_u *message;
7936 char_u *buttons = NULL;
7937 char_u buf[NUMBUFLEN];
7938 char_u buf2[NUMBUFLEN];
7939 int def = 1;
7940 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007941 char_u *typestr;
7942 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007944 message = get_tv_string_chk(&argvars[0]);
7945 if (message == NULL)
7946 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007947 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007949 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7950 if (buttons == NULL)
7951 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007952 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007954 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007955 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007957 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7958 if (typestr == NULL)
7959 error = TRUE;
7960 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007962 switch (TOUPPER_ASC(*typestr))
7963 {
7964 case 'E': type = VIM_ERROR; break;
7965 case 'Q': type = VIM_QUESTION; break;
7966 case 'I': type = VIM_INFO; break;
7967 case 'W': type = VIM_WARNING; break;
7968 case 'G': type = VIM_GENERIC; break;
7969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 }
7971 }
7972 }
7973 }
7974
7975 if (buttons == NULL || *buttons == NUL)
7976 buttons = (char_u *)_("&Ok");
7977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007978 if (error)
7979 rettv->vval.v_number = 0;
7980 else
7981 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 def, NULL);
7983#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985#endif
7986}
7987
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007988/*
7989 * "copy()" function
7990 */
7991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007992f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007993 typval_T *argvars;
7994 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007995{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007996 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007997}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998
7999/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008000 * "count()" function
8001 */
8002 static void
8003f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008004 typval_T *argvars;
8005 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008006{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008007 long n = 0;
8008 int ic = FALSE;
8009
Bram Moolenaare9a41262005-01-15 22:18:47 +00008010 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008011 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008012 listitem_T *li;
8013 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008014 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008015
Bram Moolenaare9a41262005-01-15 22:18:47 +00008016 if ((l = argvars[0].vval.v_list) != NULL)
8017 {
8018 li = l->lv_first;
8019 if (argvars[2].v_type != VAR_UNKNOWN)
8020 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008021 int error = FALSE;
8022
8023 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008024 if (argvars[3].v_type != VAR_UNKNOWN)
8025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008026 idx = get_tv_number_chk(&argvars[3], &error);
8027 if (!error)
8028 {
8029 li = list_find(l, idx);
8030 if (li == NULL)
8031 EMSGN(_(e_listidx), idx);
8032 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008033 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008034 if (error)
8035 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008036 }
8037
8038 for ( ; li != NULL; li = li->li_next)
8039 if (tv_equal(&li->li_tv, &argvars[1], ic))
8040 ++n;
8041 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008042 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008043 else if (argvars[0].v_type == VAR_DICT)
8044 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008045 int todo;
8046 dict_T *d;
8047 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008048
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008049 if ((d = argvars[0].vval.v_dict) != NULL)
8050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008051 int error = FALSE;
8052
Bram Moolenaare9a41262005-01-15 22:18:47 +00008053 if (argvars[2].v_type != VAR_UNKNOWN)
8054 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008055 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008056 if (argvars[3].v_type != VAR_UNKNOWN)
8057 EMSG(_(e_invarg));
8058 }
8059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008060 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008061 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008062 {
8063 if (!HASHITEM_EMPTY(hi))
8064 {
8065 --todo;
8066 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8067 ++n;
8068 }
8069 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008070 }
8071 }
8072 else
8073 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008074 rettv->vval.v_number = n;
8075}
8076
8077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8079 *
8080 * Checks the existence of a cscope connection.
8081 */
8082/*ARGSUSED*/
8083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008084f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008085 typval_T *argvars;
8086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087{
8088#ifdef FEAT_CSCOPE
8089 int num = 0;
8090 char_u *dbpath = NULL;
8091 char_u *prepend = NULL;
8092 char_u buf[NUMBUFLEN];
8093
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008094 if (argvars[0].v_type != VAR_UNKNOWN
8095 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008097 num = (int)get_tv_number(&argvars[0]);
8098 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008099 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008100 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 }
8102
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008103 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008105 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106#endif
8107}
8108
8109/*
8110 * "cursor(lnum, col)" function
8111 *
8112 * Moves the cursor to the specified line and column
8113 */
8114/*ARGSUSED*/
8115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008116f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008117 typval_T *argvars;
8118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119{
8120 long line, col;
8121
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008122 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008123 col = get_tv_number_chk(&argvars[1], NULL);
8124 if (line < 0 || col < 0)
8125 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 if (line > 0)
8127 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 if (col > 0)
8129 curwin->w_cursor.col = col - 1;
8130#ifdef FEAT_VIRTUALEDIT
8131 curwin->w_cursor.coladd = 0;
8132#endif
8133
8134 /* Make sure the cursor is in a valid position. */
8135 check_cursor();
8136#ifdef FEAT_MBYTE
8137 /* Correct cursor for multi-byte character. */
8138 if (has_mbyte)
8139 mb_adjust_cursor();
8140#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008141
8142 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143}
8144
8145/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008146 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 */
8148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008149f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008150 typval_T *argvars;
8151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008153 int noref = 0;
8154
8155 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008156 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008157 if (noref < 0 || noref > 1)
8158 EMSG(_(e_invarg));
8159 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008160 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161}
8162
8163/*
8164 * "delete()" function
8165 */
8166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008167f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008168 typval_T *argvars;
8169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170{
8171 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008172 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008174 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175}
8176
8177/*
8178 * "did_filetype()" function
8179 */
8180/*ARGSUSED*/
8181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008182f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008183 typval_T *argvars;
8184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185{
8186#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008187 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008189 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190#endif
8191}
8192
8193/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008194 * "diff_filler()" function
8195 */
8196/*ARGSUSED*/
8197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008198f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008199 typval_T *argvars;
8200 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008201{
8202#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008203 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008204#endif
8205}
8206
8207/*
8208 * "diff_hlID()" function
8209 */
8210/*ARGSUSED*/
8211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008212f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008213 typval_T *argvars;
8214 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008215{
8216#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008217 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008218 static linenr_T prev_lnum = 0;
8219 static int changedtick = 0;
8220 static int fnum = 0;
8221 static int change_start = 0;
8222 static int change_end = 0;
8223 static enum hlf_value hlID = 0;
8224 int filler_lines;
8225 int col;
8226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008227 if (lnum < 0) /* ignore type error in {lnum} arg */
8228 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008229 if (lnum != prev_lnum
8230 || changedtick != curbuf->b_changedtick
8231 || fnum != curbuf->b_fnum)
8232 {
8233 /* New line, buffer, change: need to get the values. */
8234 filler_lines = diff_check(curwin, lnum);
8235 if (filler_lines < 0)
8236 {
8237 if (filler_lines == -1)
8238 {
8239 change_start = MAXCOL;
8240 change_end = -1;
8241 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8242 hlID = HLF_ADD; /* added line */
8243 else
8244 hlID = HLF_CHD; /* changed line */
8245 }
8246 else
8247 hlID = HLF_ADD; /* added line */
8248 }
8249 else
8250 hlID = (enum hlf_value)0;
8251 prev_lnum = lnum;
8252 changedtick = curbuf->b_changedtick;
8253 fnum = curbuf->b_fnum;
8254 }
8255
8256 if (hlID == HLF_CHD || hlID == HLF_TXD)
8257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008258 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008259 if (col >= change_start && col <= change_end)
8260 hlID = HLF_TXD; /* changed text */
8261 else
8262 hlID = HLF_CHD; /* changed line */
8263 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008264 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008265#endif
8266}
8267
8268/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008269 * "empty({expr})" function
8270 */
8271 static void
8272f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008273 typval_T *argvars;
8274 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008275{
8276 int n;
8277
8278 switch (argvars[0].v_type)
8279 {
8280 case VAR_STRING:
8281 case VAR_FUNC:
8282 n = argvars[0].vval.v_string == NULL
8283 || *argvars[0].vval.v_string == NUL;
8284 break;
8285 case VAR_NUMBER:
8286 n = argvars[0].vval.v_number == 0;
8287 break;
8288 case VAR_LIST:
8289 n = argvars[0].vval.v_list == NULL
8290 || argvars[0].vval.v_list->lv_first == NULL;
8291 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008292 case VAR_DICT:
8293 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008294 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008295 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008296 default:
8297 EMSG2(_(e_intern2), "f_empty()");
8298 n = 0;
8299 }
8300
8301 rettv->vval.v_number = n;
8302}
8303
8304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 * "escape({string}, {chars})" function
8306 */
8307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008308f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008309 typval_T *argvars;
8310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311{
8312 char_u buf[NUMBUFLEN];
8313
Bram Moolenaar758711c2005-02-02 23:11:38 +00008314 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8315 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008316 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317}
8318
8319/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008320 * "eval()" function
8321 */
8322/*ARGSUSED*/
8323 static void
8324f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008325 typval_T *argvars;
8326 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008327{
8328 char_u *s;
8329
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008330 s = get_tv_string_chk(&argvars[0]);
8331 if (s != NULL)
8332 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008333
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008334 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8335 {
8336 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008337 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008338 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008339 else if (*s != NUL)
8340 EMSG(_(e_trailing));
8341}
8342
8343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 * "eventhandler()" function
8345 */
8346/*ARGSUSED*/
8347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008348f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008349 typval_T *argvars;
8350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008352 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353}
8354
8355/*
8356 * "executable()" function
8357 */
8358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008359f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008360 typval_T *argvars;
8361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008363 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364}
8365
8366/*
8367 * "exists()" function
8368 */
8369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008370f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008371 typval_T *argvars;
8372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373{
8374 char_u *p;
8375 char_u *name;
8376 int n = FALSE;
8377 int len = 0;
8378
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008379 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 if (*p == '$') /* environment variable */
8381 {
8382 /* first try "normal" environment variables (fast) */
8383 if (mch_getenv(p + 1) != NULL)
8384 n = TRUE;
8385 else
8386 {
8387 /* try expanding things like $VIM and ${HOME} */
8388 p = expand_env_save(p);
8389 if (p != NULL && *p != '$')
8390 n = TRUE;
8391 vim_free(p);
8392 }
8393 }
8394 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008395 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 else if (*p == '*') /* internal or user defined function */
8397 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008398 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 }
8400 else if (*p == ':')
8401 {
8402 n = cmd_exists(p + 1);
8403 }
8404 else if (*p == '#')
8405 {
8406#ifdef FEAT_AUTOCMD
8407 name = p + 1;
8408 p = vim_strchr(name, '#');
8409 if (p != NULL)
8410 n = au_exists(name, p, p + 1);
8411 else
8412 n = au_exists(name, name + STRLEN(name), NULL);
8413#endif
8414 }
8415 else /* internal variable */
8416 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008417 char_u *tofree;
8418 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008420 /* get_name_len() takes care of expanding curly braces */
8421 name = p;
8422 len = get_name_len(&p, &tofree, TRUE, FALSE);
8423 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008425 if (tofree != NULL)
8426 name = tofree;
8427 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8428 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008430 /* handle d.key, l[idx], f(expr) */
8431 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8432 if (n)
8433 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 }
8435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008437 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 }
8439
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008440 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441}
8442
8443/*
8444 * "expand()" function
8445 */
8446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008447f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008448 typval_T *argvars;
8449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450{
8451 char_u *s;
8452 int len;
8453 char_u *errormsg;
8454 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8455 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008456 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008458 rettv->v_type = VAR_STRING;
8459 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 if (*s == '%' || *s == '#' || *s == '<')
8461 {
8462 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008463 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 --emsg_off;
8465 }
8466 else
8467 {
8468 /* When the optional second argument is non-zero, don't remove matches
8469 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008470 if (argvars[1].v_type != VAR_UNKNOWN
8471 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008473 if (!error)
8474 {
8475 ExpandInit(&xpc);
8476 xpc.xp_context = EXPAND_FILES;
8477 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8478 ExpandCleanup(&xpc);
8479 }
8480 else
8481 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 }
8483}
8484
8485/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008486 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008487 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008488 */
8489 static void
8490f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008491 typval_T *argvars;
8492 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008493{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008494 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008495 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008496 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008497 list_T *l1, *l2;
8498 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008499 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008500 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008501
Bram Moolenaare9a41262005-01-15 22:18:47 +00008502 l1 = argvars[0].vval.v_list;
8503 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008504 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8505 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008506 {
8507 if (argvars[2].v_type != VAR_UNKNOWN)
8508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008509 before = get_tv_number_chk(&argvars[2], &error);
8510 if (error)
8511 return; /* type error; errmsg already given */
8512
Bram Moolenaar758711c2005-02-02 23:11:38 +00008513 if (before == l1->lv_len)
8514 item = NULL;
8515 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008516 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008517 item = list_find(l1, before);
8518 if (item == NULL)
8519 {
8520 EMSGN(_(e_listidx), before);
8521 return;
8522 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008523 }
8524 }
8525 else
8526 item = NULL;
8527 list_extend(l1, l2, item);
8528
Bram Moolenaare9a41262005-01-15 22:18:47 +00008529 copy_tv(&argvars[0], rettv);
8530 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008531 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008532 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8533 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008534 dict_T *d1, *d2;
8535 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008536 char_u *action;
8537 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008538 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008539 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008540
8541 d1 = argvars[0].vval.v_dict;
8542 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008543 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8544 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008545 {
8546 /* Check the third argument. */
8547 if (argvars[2].v_type != VAR_UNKNOWN)
8548 {
8549 static char *(av[]) = {"keep", "force", "error"};
8550
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008551 action = get_tv_string_chk(&argvars[2]);
8552 if (action == NULL)
8553 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008554 for (i = 0; i < 3; ++i)
8555 if (STRCMP(action, av[i]) == 0)
8556 break;
8557 if (i == 3)
8558 {
8559 EMSGN(_(e_invarg2), action);
8560 return;
8561 }
8562 }
8563 else
8564 action = (char_u *)"force";
8565
8566 /* Go over all entries in the second dict and add them to the
8567 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008568 todo = d2->dv_hashtab.ht_used;
8569 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008570 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008571 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008572 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008573 --todo;
8574 di1 = dict_find(d1, hi2->hi_key, -1);
8575 if (di1 == NULL)
8576 {
8577 di1 = dictitem_copy(HI2DI(hi2));
8578 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8579 dictitem_free(di1);
8580 }
8581 else if (*action == 'e')
8582 {
8583 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8584 break;
8585 }
8586 else if (*action == 'f')
8587 {
8588 clear_tv(&di1->di_tv);
8589 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8590 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008591 }
8592 }
8593
Bram Moolenaare9a41262005-01-15 22:18:47 +00008594 copy_tv(&argvars[0], rettv);
8595 }
8596 }
8597 else
8598 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008599}
8600
8601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 * "filereadable()" function
8603 */
8604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008605f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008606 typval_T *argvars;
8607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608{
8609 FILE *fd;
8610 char_u *p;
8611 int n;
8612
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008613 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8615 {
8616 n = TRUE;
8617 fclose(fd);
8618 }
8619 else
8620 n = FALSE;
8621
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008622 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623}
8624
8625/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008626 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 * rights to write into.
8628 */
8629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008630f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008631 typval_T *argvars;
8632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008634 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635}
8636
Bram Moolenaar33570922005-01-25 22:26:29 +00008637static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008638
8639 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008640findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008641 typval_T *argvars;
8642 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008643 int dir;
8644{
8645#ifdef FEAT_SEARCHPATH
8646 char_u *fname;
8647 char_u *fresult = NULL;
8648 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8649 char_u *p;
8650 char_u pathbuf[NUMBUFLEN];
8651 int count = 1;
8652 int first = TRUE;
8653
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008654 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008655
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008656 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008657 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008658 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8659 if (p == NULL)
8660 count = -1; /* error */
8661 else
8662 {
8663 if (*p != NUL)
8664 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008666 if (argvars[2].v_type != VAR_UNKNOWN)
8667 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8668 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008669 }
8670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008671 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008673 do
8674 {
8675 vim_free(fresult);
8676 fresult = find_file_in_path_option(first ? fname : NULL,
8677 first ? (int)STRLEN(fname) : 0,
8678 0, first, path, dir, NULL);
8679 first = FALSE;
8680 } while (--count > 0 && fresult != NULL);
8681 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008682
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008683 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008684#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008685 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008686#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008687 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008688}
8689
Bram Moolenaar33570922005-01-25 22:26:29 +00008690static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8691static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008692
8693/*
8694 * Implementation of map() and filter().
8695 */
8696 static void
8697filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008698 typval_T *argvars;
8699 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008700 int map;
8701{
8702 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008703 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008704 listitem_T *li, *nli;
8705 list_T *l = NULL;
8706 dictitem_T *di;
8707 hashtab_T *ht;
8708 hashitem_T *hi;
8709 dict_T *d = NULL;
8710 typval_T save_val;
8711 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008712 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008713 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008714 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8715
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008716
8717 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008718 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008719 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008720 if ((l = argvars[0].vval.v_list) == NULL
8721 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008722 return;
8723 }
8724 else if (argvars[0].v_type == VAR_DICT)
8725 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008726 if ((d = argvars[0].vval.v_dict) == NULL
8727 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008728 return;
8729 }
8730 else
8731 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008732 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008733 return;
8734 }
8735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008736 expr = get_tv_string_buf_chk(&argvars[1], buf);
8737 /* On type errors, the preceding call has already displayed an error
8738 * message. Avoid a misleading error message for an empty string that
8739 * was not passed as argument. */
8740 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008742 prepare_vimvar(VV_VAL, &save_val);
8743 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008744
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008745 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008747 prepare_vimvar(VV_KEY, &save_key);
8748 vimvars[VV_KEY].vv_type = VAR_STRING;
8749
8750 ht = &d->dv_hashtab;
8751 hash_lock(ht);
8752 todo = ht->ht_used;
8753 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008755 if (!HASHITEM_EMPTY(hi))
8756 {
8757 --todo;
8758 di = HI2DI(hi);
8759 if (tv_check_lock(di->di_tv.v_lock, msg))
8760 break;
8761 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8762 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8763 break;
8764 if (!map && rem)
8765 dictitem_remove(d, di);
8766 clear_tv(&vimvars[VV_KEY].vv_tv);
8767 }
8768 }
8769 hash_unlock(ht);
8770
8771 restore_vimvar(VV_KEY, &save_key);
8772 }
8773 else
8774 {
8775 for (li = l->lv_first; li != NULL; li = nli)
8776 {
8777 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008778 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008779 nli = li->li_next;
8780 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008781 break;
8782 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008783 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008784 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008785 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008787 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008788 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008789
8790 copy_tv(&argvars[0], rettv);
8791}
8792
8793 static int
8794filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008795 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008796 char_u *expr;
8797 int map;
8798 int *remp;
8799{
Bram Moolenaar33570922005-01-25 22:26:29 +00008800 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008801 char_u *s;
8802
Bram Moolenaar33570922005-01-25 22:26:29 +00008803 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008804 s = expr;
8805 if (eval1(&s, &rettv, TRUE) == FAIL)
8806 return FAIL;
8807 if (*s != NUL) /* check for trailing chars after expr */
8808 {
8809 EMSG2(_(e_invexpr2), s);
8810 return FAIL;
8811 }
8812 if (map)
8813 {
8814 /* map(): replace the list item value */
8815 clear_tv(tv);
8816 *tv = rettv;
8817 }
8818 else
8819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008820 int error = FALSE;
8821
Bram Moolenaare9a41262005-01-15 22:18:47 +00008822 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008823 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008824 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008825 /* On type error, nothing has been removed; return FAIL to stop the
8826 * loop. The error message was given by get_tv_number_chk(). */
8827 if (error)
8828 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008829 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008830 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008831 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008832}
8833
8834/*
8835 * "filter()" function
8836 */
8837 static void
8838f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008839 typval_T *argvars;
8840 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008841{
8842 filter_map(argvars, rettv, FALSE);
8843}
8844
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008845/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008846 * "finddir({fname}[, {path}[, {count}]])" function
8847 */
8848 static void
8849f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008850 typval_T *argvars;
8851 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008852{
8853 findfilendir(argvars, rettv, TRUE);
8854}
8855
8856/*
8857 * "findfile({fname}[, {path}[, {count}]])" function
8858 */
8859 static void
8860f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008861 typval_T *argvars;
8862 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008863{
8864 findfilendir(argvars, rettv, FALSE);
8865}
8866
8867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 * "fnamemodify({fname}, {mods})" function
8869 */
8870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 typval_T *argvars;
8873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874{
8875 char_u *fname;
8876 char_u *mods;
8877 int usedlen = 0;
8878 int len;
8879 char_u *fbuf = NULL;
8880 char_u buf[NUMBUFLEN];
8881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008882 fname = get_tv_string_chk(&argvars[0]);
8883 mods = get_tv_string_buf_chk(&argvars[1], buf);
8884 if (fname == NULL || mods == NULL)
8885 fname = NULL;
8886 else
8887 {
8888 len = (int)STRLEN(fname);
8889 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008892 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008894 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008896 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 vim_free(fbuf);
8898}
8899
Bram Moolenaar33570922005-01-25 22:26:29 +00008900static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901
8902/*
8903 * "foldclosed()" function
8904 */
8905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008906foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008907 typval_T *argvars;
8908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 int end;
8910{
8911#ifdef FEAT_FOLDING
8912 linenr_T lnum;
8913 linenr_T first, last;
8914
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8917 {
8918 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8919 {
8920 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 return;
8925 }
8926 }
8927#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008928 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929}
8930
8931/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008932 * "foldclosed()" function
8933 */
8934 static void
8935f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008936 typval_T *argvars;
8937 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008938{
8939 foldclosed_both(argvars, rettv, FALSE);
8940}
8941
8942/*
8943 * "foldclosedend()" function
8944 */
8945 static void
8946f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008947 typval_T *argvars;
8948 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008949{
8950 foldclosed_both(argvars, rettv, TRUE);
8951}
8952
8953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 * "foldlevel()" function
8955 */
8956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008958 typval_T *argvars;
8959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960{
8961#ifdef FEAT_FOLDING
8962 linenr_T lnum;
8963
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008964 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 else
8968#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970}
8971
8972/*
8973 * "foldtext()" function
8974 */
8975/*ARGSUSED*/
8976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008978 typval_T *argvars;
8979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980{
8981#ifdef FEAT_FOLDING
8982 linenr_T lnum;
8983 char_u *s;
8984 char_u *r;
8985 int len;
8986 char *txt;
8987#endif
8988
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989 rettv->v_type = VAR_STRING;
8990 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008992 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8993 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8994 <= curbuf->b_ml.ml_line_count
8995 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 {
8997 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008998 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8999 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 {
9001 if (!linewhite(lnum))
9002 break;
9003 ++lnum;
9004 }
9005
9006 /* Find interesting text in this line. */
9007 s = skipwhite(ml_get(lnum));
9008 /* skip C comment-start */
9009 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009012 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009013 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009014 {
9015 s = skipwhite(ml_get(lnum + 1));
9016 if (*s == '*')
9017 s = skipwhite(s + 1);
9018 }
9019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 txt = _("+-%s%3ld lines: ");
9021 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009022 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 + 20 /* for %3ld */
9024 + STRLEN(s))); /* concatenated */
9025 if (r != NULL)
9026 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009027 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9028 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9029 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 len = (int)STRLEN(r);
9031 STRCAT(r, s);
9032 /* remove 'foldmarker' and 'commentstring' */
9033 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009034 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 }
9036 }
9037#endif
9038}
9039
9040/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009041 * "foldtextresult(lnum)" function
9042 */
9043/*ARGSUSED*/
9044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009045f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009046 typval_T *argvars;
9047 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009048{
9049#ifdef FEAT_FOLDING
9050 linenr_T lnum;
9051 char_u *text;
9052 char_u buf[51];
9053 foldinfo_T foldinfo;
9054 int fold_count;
9055#endif
9056
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057 rettv->v_type = VAR_STRING;
9058 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009059#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009060 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009061 /* treat illegal types and illegal string values for {lnum} the same */
9062 if (lnum < 0)
9063 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009064 fold_count = foldedCount(curwin, lnum, &foldinfo);
9065 if (fold_count > 0)
9066 {
9067 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9068 &foldinfo, buf);
9069 if (text == buf)
9070 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009072 }
9073#endif
9074}
9075
9076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 * "foreground()" function
9078 */
9079/*ARGSUSED*/
9080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009082 typval_T *argvars;
9083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086#ifdef FEAT_GUI
9087 if (gui.in_use)
9088 gui_mch_set_foreground();
9089#else
9090# ifdef WIN32
9091 win32_set_foreground();
9092# endif
9093#endif
9094}
9095
9096/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009097 * "function()" function
9098 */
9099/*ARGSUSED*/
9100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009102 typval_T *argvars;
9103 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009104{
9105 char_u *s;
9106
Bram Moolenaara7043832005-01-21 11:56:39 +00009107 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009109 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009110 EMSG2(_(e_invarg2), s);
9111 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009112 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009113 else
9114 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 rettv->vval.v_string = vim_strsave(s);
9116 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009117 }
9118}
9119
9120/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009121 * "garbagecollect()" function
9122 */
9123/*ARGSUSED*/
9124 static void
9125f_garbagecollect(argvars, rettv)
9126 typval_T *argvars;
9127 typval_T *rettv;
9128{
9129 garbage_collect();
9130}
9131
9132/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009133 * "get()" function
9134 */
9135 static void
9136f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009137 typval_T *argvars;
9138 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009139{
Bram Moolenaar33570922005-01-25 22:26:29 +00009140 listitem_T *li;
9141 list_T *l;
9142 dictitem_T *di;
9143 dict_T *d;
9144 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009145
Bram Moolenaare9a41262005-01-15 22:18:47 +00009146 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009147 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009148 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009149 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009150 int error = FALSE;
9151
9152 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9153 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009154 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009155 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009156 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009157 else if (argvars[0].v_type == VAR_DICT)
9158 {
9159 if ((d = argvars[0].vval.v_dict) != NULL)
9160 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009161 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009162 if (di != NULL)
9163 tv = &di->di_tv;
9164 }
9165 }
9166 else
9167 EMSG2(_(e_listdictarg), "get()");
9168
9169 if (tv == NULL)
9170 {
9171 if (argvars[2].v_type == VAR_UNKNOWN)
9172 rettv->vval.v_number = 0;
9173 else
9174 copy_tv(&argvars[2], rettv);
9175 }
9176 else
9177 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009178}
9179
Bram Moolenaar342337a2005-07-21 21:11:17 +00009180static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009181
9182/*
9183 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009184 * Return a range (from start to end) of lines in rettv from the specified
9185 * buffer.
9186 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009187 */
9188 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009189get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009190 buf_T *buf;
9191 linenr_T start;
9192 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009193 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009194 typval_T *rettv;
9195{
9196 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009197 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009198 listitem_T *li;
9199
Bram Moolenaar342337a2005-07-21 21:11:17 +00009200 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009201 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009202 l = list_alloc();
9203 if (l == NULL)
9204 return;
9205
9206 rettv->vval.v_list = l;
9207 rettv->v_type = VAR_LIST;
9208 ++l->lv_refcount;
9209 }
9210 else
9211 rettv->vval.v_number = 0;
9212
9213 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9214 return;
9215
9216 if (!retlist)
9217 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009218 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9219 p = ml_get_buf(buf, start, FALSE);
9220 else
9221 p = (char_u *)"";
9222
9223 rettv->v_type = VAR_STRING;
9224 rettv->vval.v_string = vim_strsave(p);
9225 }
9226 else
9227 {
9228 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009229 return;
9230
9231 if (start < 1)
9232 start = 1;
9233 if (end > buf->b_ml.ml_line_count)
9234 end = buf->b_ml.ml_line_count;
9235 while (start <= end)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009236 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009237 li = listitem_alloc();
9238 if (li == NULL)
9239 break;
9240 list_append(l, li);
9241 li->li_tv.v_type = VAR_STRING;
9242 li->li_tv.v_lock = 0;
9243 li->li_tv.vval.v_string =
9244 vim_strsave(ml_get_buf(buf, start++, FALSE));
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009245 }
9246 }
9247}
9248
9249/*
9250 * "getbufline()" function
9251 */
9252 static void
9253f_getbufline(argvars, rettv)
9254 typval_T *argvars;
9255 typval_T *rettv;
9256{
9257 linenr_T lnum;
9258 linenr_T end;
9259 buf_T *buf;
9260
9261 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9262 ++emsg_off;
9263 buf = get_buf_tv(&argvars[0]);
9264 --emsg_off;
9265
Bram Moolenaar661b1822005-07-28 22:36:45 +00009266 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009267 if (argvars[2].v_type == VAR_UNKNOWN)
9268 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009269 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009270 end = get_tv_lnum_buf(&argvars[2], buf);
9271
Bram Moolenaar342337a2005-07-21 21:11:17 +00009272 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009273}
9274
Bram Moolenaar0d660222005-01-07 21:51:51 +00009275/*
9276 * "getbufvar()" function
9277 */
9278 static void
9279f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009280 typval_T *argvars;
9281 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009282{
9283 buf_T *buf;
9284 buf_T *save_curbuf;
9285 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009286 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009287
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009288 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9289 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009290 ++emsg_off;
9291 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009292
9293 rettv->v_type = VAR_STRING;
9294 rettv->vval.v_string = NULL;
9295
9296 if (buf != NULL && varname != NULL)
9297 {
9298 if (*varname == '&') /* buffer-local-option */
9299 {
9300 /* set curbuf to be our buf, temporarily */
9301 save_curbuf = curbuf;
9302 curbuf = buf;
9303
9304 get_option_tv(&varname, rettv, TRUE);
9305
9306 /* restore previous notion of curbuf */
9307 curbuf = save_curbuf;
9308 }
9309 else
9310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009311 if (*varname == NUL)
9312 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9313 * scope prefix before the NUL byte is required by
9314 * find_var_in_ht(). */
9315 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009316 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009317 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009318 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009319 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009320 }
9321 }
9322
9323 --emsg_off;
9324}
9325
9326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 * "getchar()" function
9328 */
9329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009330f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009331 typval_T *argvars;
9332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333{
9334 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009335 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336
9337 ++no_mapping;
9338 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009339 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 /* getchar(): blocking wait. */
9341 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009342 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 /* getchar(1): only check if char avail */
9344 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009345 else if (error || vpeekc() == NUL)
9346 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 n = 0;
9348 else
9349 /* getchar(0) and char avail: return char */
9350 n = safe_vgetc();
9351 --no_mapping;
9352 --allow_keys;
9353
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009354 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 if (IS_SPECIAL(n) || mod_mask != 0)
9356 {
9357 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9358 int i = 0;
9359
9360 /* Turn a special key into three bytes, plus modifier. */
9361 if (mod_mask != 0)
9362 {
9363 temp[i++] = K_SPECIAL;
9364 temp[i++] = KS_MODIFIER;
9365 temp[i++] = mod_mask;
9366 }
9367 if (IS_SPECIAL(n))
9368 {
9369 temp[i++] = K_SPECIAL;
9370 temp[i++] = K_SECOND(n);
9371 temp[i++] = K_THIRD(n);
9372 }
9373#ifdef FEAT_MBYTE
9374 else if (has_mbyte)
9375 i += (*mb_char2bytes)(n, temp + i);
9376#endif
9377 else
9378 temp[i++] = n;
9379 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009380 rettv->v_type = VAR_STRING;
9381 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 }
9383}
9384
9385/*
9386 * "getcharmod()" function
9387 */
9388/*ARGSUSED*/
9389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009390f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009391 typval_T *argvars;
9392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395}
9396
9397/*
9398 * "getcmdline()" function
9399 */
9400/*ARGSUSED*/
9401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009402f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009403 typval_T *argvars;
9404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406 rettv->v_type = VAR_STRING;
9407 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408}
9409
9410/*
9411 * "getcmdpos()" function
9412 */
9413/*ARGSUSED*/
9414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009415f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009416 typval_T *argvars;
9417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009419 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420}
9421
9422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 * "getcwd()" function
9424 */
9425/*ARGSUSED*/
9426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009428 typval_T *argvars;
9429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430{
9431 char_u cwd[MAXPATHL];
9432
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009433 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 else
9437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009440 if (rettv->vval.v_string != NULL)
9441 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442#endif
9443 }
9444}
9445
9446/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009447 * "getfontname()" function
9448 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009449/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009452 typval_T *argvars;
9453 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009454{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009455 rettv->v_type = VAR_STRING;
9456 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009457#ifdef FEAT_GUI
9458 if (gui.in_use)
9459 {
9460 GuiFont font;
9461 char_u *name = NULL;
9462
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009463 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009464 {
9465 /* Get the "Normal" font. Either the name saved by
9466 * hl_set_font_name() or from the font ID. */
9467 font = gui.norm_font;
9468 name = hl_get_font_name();
9469 }
9470 else
9471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009472 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009473 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9474 return;
9475 font = gui_mch_get_font(name, FALSE);
9476 if (font == NOFONT)
9477 return; /* Invalid font name, return empty string. */
9478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009479 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009480 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009481 gui_mch_free_font(font);
9482 }
9483#endif
9484}
9485
9486/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009487 * "getfperm({fname})" function
9488 */
9489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009491 typval_T *argvars;
9492 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009493{
9494 char_u *fname;
9495 struct stat st;
9496 char_u *perm = NULL;
9497 char_u flags[] = "rwx";
9498 int i;
9499
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009500 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009501
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009503 if (mch_stat((char *)fname, &st) >= 0)
9504 {
9505 perm = vim_strsave((char_u *)"---------");
9506 if (perm != NULL)
9507 {
9508 for (i = 0; i < 9; i++)
9509 {
9510 if (st.st_mode & (1 << (8 - i)))
9511 perm[i] = flags[i % 3];
9512 }
9513 }
9514 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009516}
9517
9518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 * "getfsize({fname})" function
9520 */
9521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009522f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009523 typval_T *argvars;
9524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525{
9526 char_u *fname;
9527 struct stat st;
9528
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009529 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009531 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532
9533 if (mch_stat((char *)fname, &st) >= 0)
9534 {
9535 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009536 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009538 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 }
9540 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542}
9543
9544/*
9545 * "getftime({fname})" function
9546 */
9547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009548f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009549 typval_T *argvars;
9550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551{
9552 char_u *fname;
9553 struct stat st;
9554
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556
9557 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561}
9562
9563/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009564 * "getftype({fname})" function
9565 */
9566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009568 typval_T *argvars;
9569 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009570{
9571 char_u *fname;
9572 struct stat st;
9573 char_u *type = NULL;
9574 char *t;
9575
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009577
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009578 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009579 if (mch_lstat((char *)fname, &st) >= 0)
9580 {
9581#ifdef S_ISREG
9582 if (S_ISREG(st.st_mode))
9583 t = "file";
9584 else if (S_ISDIR(st.st_mode))
9585 t = "dir";
9586# ifdef S_ISLNK
9587 else if (S_ISLNK(st.st_mode))
9588 t = "link";
9589# endif
9590# ifdef S_ISBLK
9591 else if (S_ISBLK(st.st_mode))
9592 t = "bdev";
9593# endif
9594# ifdef S_ISCHR
9595 else if (S_ISCHR(st.st_mode))
9596 t = "cdev";
9597# endif
9598# ifdef S_ISFIFO
9599 else if (S_ISFIFO(st.st_mode))
9600 t = "fifo";
9601# endif
9602# ifdef S_ISSOCK
9603 else if (S_ISSOCK(st.st_mode))
9604 t = "fifo";
9605# endif
9606 else
9607 t = "other";
9608#else
9609# ifdef S_IFMT
9610 switch (st.st_mode & S_IFMT)
9611 {
9612 case S_IFREG: t = "file"; break;
9613 case S_IFDIR: t = "dir"; break;
9614# ifdef S_IFLNK
9615 case S_IFLNK: t = "link"; break;
9616# endif
9617# ifdef S_IFBLK
9618 case S_IFBLK: t = "bdev"; break;
9619# endif
9620# ifdef S_IFCHR
9621 case S_IFCHR: t = "cdev"; break;
9622# endif
9623# ifdef S_IFIFO
9624 case S_IFIFO: t = "fifo"; break;
9625# endif
9626# ifdef S_IFSOCK
9627 case S_IFSOCK: t = "socket"; break;
9628# endif
9629 default: t = "other";
9630 }
9631# else
9632 if (mch_isdir(fname))
9633 t = "dir";
9634 else
9635 t = "file";
9636# endif
9637#endif
9638 type = vim_strsave((char_u *)t);
9639 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009641}
9642
9643/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009644 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009645 */
9646 static void
9647f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009648 typval_T *argvars;
9649 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009650{
9651 linenr_T lnum;
9652 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009653 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009654
9655 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009656 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009657 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009658 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009659 retlist = FALSE;
9660 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009661 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009662 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009663 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009664 retlist = TRUE;
9665 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009666
Bram Moolenaar342337a2005-07-21 21:11:17 +00009667 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009668}
9669
9670/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009671 * "getqflist()" function
9672 */
9673/*ARGSUSED*/
9674 static void
9675f_getqflist(argvars, rettv)
9676 typval_T *argvars;
9677 typval_T *rettv;
9678{
9679#ifdef FEAT_QUICKFIX
9680 list_T *l;
9681#endif
9682
9683 rettv->vval.v_number = FALSE;
9684#ifdef FEAT_QUICKFIX
9685 l = list_alloc();
9686 if (l != NULL)
9687 {
9688 if (get_errorlist(l) != FAIL)
9689 {
9690 rettv->vval.v_list = l;
9691 rettv->v_type = VAR_LIST;
9692 ++l->lv_refcount;
9693 }
9694 else
9695 list_free(l);
9696 }
9697#endif
9698}
9699
9700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 * "getreg()" function
9702 */
9703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009705 typval_T *argvars;
9706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707{
9708 char_u *strregname;
9709 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009710 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009711 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009713 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009715 strregname = get_tv_string_chk(&argvars[0]);
9716 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009717 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009718 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009721 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 regname = (strregname == NULL ? '"' : *strregname);
9723 if (regname == 0)
9724 regname = '"';
9725
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009726 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009727 rettv->vval.v_string = error ? NULL :
9728 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729}
9730
9731/*
9732 * "getregtype()" function
9733 */
9734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009735f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009736 typval_T *argvars;
9737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738{
9739 char_u *strregname;
9740 int regname;
9741 char_u buf[NUMBUFLEN + 2];
9742 long reglen = 0;
9743
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009744 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009745 {
9746 strregname = get_tv_string_chk(&argvars[0]);
9747 if (strregname == NULL) /* type error; errmsg already given */
9748 {
9749 rettv->v_type = VAR_STRING;
9750 rettv->vval.v_string = NULL;
9751 return;
9752 }
9753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 else
9755 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009756 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757
9758 regname = (strregname == NULL ? '"' : *strregname);
9759 if (regname == 0)
9760 regname = '"';
9761
9762 buf[0] = NUL;
9763 buf[1] = NUL;
9764 switch (get_reg_type(regname, &reglen))
9765 {
9766 case MLINE: buf[0] = 'V'; break;
9767 case MCHAR: buf[0] = 'v'; break;
9768#ifdef FEAT_VISUAL
9769 case MBLOCK:
9770 buf[0] = Ctrl_V;
9771 sprintf((char *)buf + 1, "%ld", reglen + 1);
9772 break;
9773#endif
9774 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775 rettv->v_type = VAR_STRING;
9776 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777}
9778
9779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 * "getwinposx()" function
9781 */
9782/*ARGSUSED*/
9783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009784f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009785 typval_T *argvars;
9786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009788 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789#ifdef FEAT_GUI
9790 if (gui.in_use)
9791 {
9792 int x, y;
9793
9794 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 }
9797#endif
9798}
9799
9800/*
9801 * "getwinposy()" function
9802 */
9803/*ARGSUSED*/
9804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009806 typval_T *argvars;
9807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810#ifdef FEAT_GUI
9811 if (gui.in_use)
9812 {
9813 int x, y;
9814
9815 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009816 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 }
9818#endif
9819}
9820
Bram Moolenaara40058a2005-07-11 22:42:07 +00009821static win_T *find_win_by_nr __ARGS((typval_T *vp));
9822
9823 static win_T *
9824find_win_by_nr(vp)
9825 typval_T *vp;
9826{
9827#ifdef FEAT_WINDOWS
9828 win_T *wp;
9829#endif
9830 int nr;
9831
9832 nr = get_tv_number_chk(vp, NULL);
9833
9834#ifdef FEAT_WINDOWS
9835 if (nr < 0)
9836 return NULL;
9837 if (nr == 0)
9838 return curwin;
9839
9840 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9841 if (--nr <= 0)
9842 break;
9843 return wp;
9844#else
9845 if (nr == 0 || nr == 1)
9846 return curwin;
9847 return NULL;
9848#endif
9849}
9850
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851/*
9852 * "getwinvar()" function
9853 */
9854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009855f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009856 typval_T *argvars;
9857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858{
9859 win_T *win, *oldcurwin;
9860 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009861 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009864 varname = get_tv_string_chk(&argvars[1]);
9865 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009867 rettv->v_type = VAR_STRING;
9868 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869
9870 if (win != NULL && varname != NULL)
9871 {
9872 if (*varname == '&') /* window-local-option */
9873 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009874 /* Set curwin to be our win, temporarily. Also set curbuf, so
9875 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 oldcurwin = curwin;
9877 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009878 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009880 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881
9882 /* restore previous notion of curwin */
9883 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009884 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 }
9886 else
9887 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009888 if (*varname == NUL)
9889 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9890 * scope prefix before the NUL byte is required by
9891 * find_var_in_ht(). */
9892 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009894 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009896 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 }
9898 }
9899
9900 --emsg_off;
9901}
9902
9903/*
9904 * "glob()" function
9905 */
9906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009907f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009908 typval_T *argvars;
9909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910{
9911 expand_T xpc;
9912
9913 ExpandInit(&xpc);
9914 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009915 rettv->v_type = VAR_STRING;
9916 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9918 ExpandCleanup(&xpc);
9919}
9920
9921/*
9922 * "globpath()" function
9923 */
9924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009925f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009926 typval_T *argvars;
9927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928{
9929 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009930 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009932 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009933 if (file == NULL)
9934 rettv->vval.v_string = NULL;
9935 else
9936 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937}
9938
9939/*
9940 * "has()" function
9941 */
9942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009943f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009944 typval_T *argvars;
9945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946{
9947 int i;
9948 char_u *name;
9949 int n = FALSE;
9950 static char *(has_list[]) =
9951 {
9952#ifdef AMIGA
9953 "amiga",
9954# ifdef FEAT_ARP
9955 "arp",
9956# endif
9957#endif
9958#ifdef __BEOS__
9959 "beos",
9960#endif
9961#ifdef MSDOS
9962# ifdef DJGPP
9963 "dos32",
9964# else
9965 "dos16",
9966# endif
9967#endif
9968#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9969 "mac",
9970#endif
9971#if defined(MACOS_X_UNIX)
9972 "macunix",
9973#endif
9974#ifdef OS2
9975 "os2",
9976#endif
9977#ifdef __QNX__
9978 "qnx",
9979#endif
9980#ifdef RISCOS
9981 "riscos",
9982#endif
9983#ifdef UNIX
9984 "unix",
9985#endif
9986#ifdef VMS
9987 "vms",
9988#endif
9989#ifdef WIN16
9990 "win16",
9991#endif
9992#ifdef WIN32
9993 "win32",
9994#endif
9995#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9996 "win32unix",
9997#endif
9998#ifdef WIN64
9999 "win64",
10000#endif
10001#ifdef EBCDIC
10002 "ebcdic",
10003#endif
10004#ifndef CASE_INSENSITIVE_FILENAME
10005 "fname_case",
10006#endif
10007#ifdef FEAT_ARABIC
10008 "arabic",
10009#endif
10010#ifdef FEAT_AUTOCMD
10011 "autocmd",
10012#endif
10013#ifdef FEAT_BEVAL
10014 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010015# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10016 "balloon_multiline",
10017# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018#endif
10019#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10020 "builtin_terms",
10021# ifdef ALL_BUILTIN_TCAPS
10022 "all_builtin_terms",
10023# endif
10024#endif
10025#ifdef FEAT_BYTEOFF
10026 "byte_offset",
10027#endif
10028#ifdef FEAT_CINDENT
10029 "cindent",
10030#endif
10031#ifdef FEAT_CLIENTSERVER
10032 "clientserver",
10033#endif
10034#ifdef FEAT_CLIPBOARD
10035 "clipboard",
10036#endif
10037#ifdef FEAT_CMDL_COMPL
10038 "cmdline_compl",
10039#endif
10040#ifdef FEAT_CMDHIST
10041 "cmdline_hist",
10042#endif
10043#ifdef FEAT_COMMENTS
10044 "comments",
10045#endif
10046#ifdef FEAT_CRYPT
10047 "cryptv",
10048#endif
10049#ifdef FEAT_CSCOPE
10050 "cscope",
10051#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010052#ifdef CURSOR_SHAPE
10053 "cursorshape",
10054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055#ifdef DEBUG
10056 "debug",
10057#endif
10058#ifdef FEAT_CON_DIALOG
10059 "dialog_con",
10060#endif
10061#ifdef FEAT_GUI_DIALOG
10062 "dialog_gui",
10063#endif
10064#ifdef FEAT_DIFF
10065 "diff",
10066#endif
10067#ifdef FEAT_DIGRAPHS
10068 "digraphs",
10069#endif
10070#ifdef FEAT_DND
10071 "dnd",
10072#endif
10073#ifdef FEAT_EMACS_TAGS
10074 "emacs_tags",
10075#endif
10076 "eval", /* always present, of course! */
10077#ifdef FEAT_EX_EXTRA
10078 "ex_extra",
10079#endif
10080#ifdef FEAT_SEARCH_EXTRA
10081 "extra_search",
10082#endif
10083#ifdef FEAT_FKMAP
10084 "farsi",
10085#endif
10086#ifdef FEAT_SEARCHPATH
10087 "file_in_path",
10088#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010089#if defined(UNIX) && !defined(USE_SYSTEM)
10090 "filterpipe",
10091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092#ifdef FEAT_FIND_ID
10093 "find_in_path",
10094#endif
10095#ifdef FEAT_FOLDING
10096 "folding",
10097#endif
10098#ifdef FEAT_FOOTER
10099 "footer",
10100#endif
10101#if !defined(USE_SYSTEM) && defined(UNIX)
10102 "fork",
10103#endif
10104#ifdef FEAT_GETTEXT
10105 "gettext",
10106#endif
10107#ifdef FEAT_GUI
10108 "gui",
10109#endif
10110#ifdef FEAT_GUI_ATHENA
10111# ifdef FEAT_GUI_NEXTAW
10112 "gui_neXtaw",
10113# else
10114 "gui_athena",
10115# endif
10116#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +000010117#ifdef FEAT_GUI_KDE
10118 "gui_kde",
10119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120#ifdef FEAT_GUI_GTK
10121 "gui_gtk",
10122# ifdef HAVE_GTK2
10123 "gui_gtk2",
10124# endif
10125#endif
10126#ifdef FEAT_GUI_MAC
10127 "gui_mac",
10128#endif
10129#ifdef FEAT_GUI_MOTIF
10130 "gui_motif",
10131#endif
10132#ifdef FEAT_GUI_PHOTON
10133 "gui_photon",
10134#endif
10135#ifdef FEAT_GUI_W16
10136 "gui_win16",
10137#endif
10138#ifdef FEAT_GUI_W32
10139 "gui_win32",
10140#endif
10141#ifdef FEAT_HANGULIN
10142 "hangul_input",
10143#endif
10144#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10145 "iconv",
10146#endif
10147#ifdef FEAT_INS_EXPAND
10148 "insert_expand",
10149#endif
10150#ifdef FEAT_JUMPLIST
10151 "jumplist",
10152#endif
10153#ifdef FEAT_KEYMAP
10154 "keymap",
10155#endif
10156#ifdef FEAT_LANGMAP
10157 "langmap",
10158#endif
10159#ifdef FEAT_LIBCALL
10160 "libcall",
10161#endif
10162#ifdef FEAT_LINEBREAK
10163 "linebreak",
10164#endif
10165#ifdef FEAT_LISP
10166 "lispindent",
10167#endif
10168#ifdef FEAT_LISTCMDS
10169 "listcmds",
10170#endif
10171#ifdef FEAT_LOCALMAP
10172 "localmap",
10173#endif
10174#ifdef FEAT_MENU
10175 "menu",
10176#endif
10177#ifdef FEAT_SESSION
10178 "mksession",
10179#endif
10180#ifdef FEAT_MODIFY_FNAME
10181 "modify_fname",
10182#endif
10183#ifdef FEAT_MOUSE
10184 "mouse",
10185#endif
10186#ifdef FEAT_MOUSESHAPE
10187 "mouseshape",
10188#endif
10189#if defined(UNIX) || defined(VMS)
10190# ifdef FEAT_MOUSE_DEC
10191 "mouse_dec",
10192# endif
10193# ifdef FEAT_MOUSE_GPM
10194 "mouse_gpm",
10195# endif
10196# ifdef FEAT_MOUSE_JSB
10197 "mouse_jsbterm",
10198# endif
10199# ifdef FEAT_MOUSE_NET
10200 "mouse_netterm",
10201# endif
10202# ifdef FEAT_MOUSE_PTERM
10203 "mouse_pterm",
10204# endif
10205# ifdef FEAT_MOUSE_XTERM
10206 "mouse_xterm",
10207# endif
10208#endif
10209#ifdef FEAT_MBYTE
10210 "multi_byte",
10211#endif
10212#ifdef FEAT_MBYTE_IME
10213 "multi_byte_ime",
10214#endif
10215#ifdef FEAT_MULTI_LANG
10216 "multi_lang",
10217#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010218#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010219#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010220 "mzscheme",
10221#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223#ifdef FEAT_OLE
10224 "ole",
10225#endif
10226#ifdef FEAT_OSFILETYPE
10227 "osfiletype",
10228#endif
10229#ifdef FEAT_PATH_EXTRA
10230 "path_extra",
10231#endif
10232#ifdef FEAT_PERL
10233#ifndef DYNAMIC_PERL
10234 "perl",
10235#endif
10236#endif
10237#ifdef FEAT_PYTHON
10238#ifndef DYNAMIC_PYTHON
10239 "python",
10240#endif
10241#endif
10242#ifdef FEAT_POSTSCRIPT
10243 "postscript",
10244#endif
10245#ifdef FEAT_PRINTER
10246 "printer",
10247#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010248#ifdef FEAT_PROFILE
10249 "profile",
10250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251#ifdef FEAT_QUICKFIX
10252 "quickfix",
10253#endif
10254#ifdef FEAT_RIGHTLEFT
10255 "rightleft",
10256#endif
10257#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10258 "ruby",
10259#endif
10260#ifdef FEAT_SCROLLBIND
10261 "scrollbind",
10262#endif
10263#ifdef FEAT_CMDL_INFO
10264 "showcmd",
10265 "cmdline_info",
10266#endif
10267#ifdef FEAT_SIGNS
10268 "signs",
10269#endif
10270#ifdef FEAT_SMARTINDENT
10271 "smartindent",
10272#endif
10273#ifdef FEAT_SNIFF
10274 "sniff",
10275#endif
10276#ifdef FEAT_STL_OPT
10277 "statusline",
10278#endif
10279#ifdef FEAT_SUN_WORKSHOP
10280 "sun_workshop",
10281#endif
10282#ifdef FEAT_NETBEANS_INTG
10283 "netbeans_intg",
10284#endif
10285#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010286 "spell",
10287#endif
10288#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 "syntax",
10290#endif
10291#if defined(USE_SYSTEM) || !defined(UNIX)
10292 "system",
10293#endif
10294#ifdef FEAT_TAG_BINS
10295 "tag_binary",
10296#endif
10297#ifdef FEAT_TAG_OLDSTATIC
10298 "tag_old_static",
10299#endif
10300#ifdef FEAT_TAG_ANYWHITE
10301 "tag_any_white",
10302#endif
10303#ifdef FEAT_TCL
10304# ifndef DYNAMIC_TCL
10305 "tcl",
10306# endif
10307#endif
10308#ifdef TERMINFO
10309 "terminfo",
10310#endif
10311#ifdef FEAT_TERMRESPONSE
10312 "termresponse",
10313#endif
10314#ifdef FEAT_TEXTOBJ
10315 "textobjects",
10316#endif
10317#ifdef HAVE_TGETENT
10318 "tgetent",
10319#endif
10320#ifdef FEAT_TITLE
10321 "title",
10322#endif
10323#ifdef FEAT_TOOLBAR
10324 "toolbar",
10325#endif
10326#ifdef FEAT_USR_CMDS
10327 "user-commands", /* was accidentally included in 5.4 */
10328 "user_commands",
10329#endif
10330#ifdef FEAT_VIMINFO
10331 "viminfo",
10332#endif
10333#ifdef FEAT_VERTSPLIT
10334 "vertsplit",
10335#endif
10336#ifdef FEAT_VIRTUALEDIT
10337 "virtualedit",
10338#endif
10339#ifdef FEAT_VISUAL
10340 "visual",
10341#endif
10342#ifdef FEAT_VISUALEXTRA
10343 "visualextra",
10344#endif
10345#ifdef FEAT_VREPLACE
10346 "vreplace",
10347#endif
10348#ifdef FEAT_WILDIGN
10349 "wildignore",
10350#endif
10351#ifdef FEAT_WILDMENU
10352 "wildmenu",
10353#endif
10354#ifdef FEAT_WINDOWS
10355 "windows",
10356#endif
10357#ifdef FEAT_WAK
10358 "winaltkeys",
10359#endif
10360#ifdef FEAT_WRITEBACKUP
10361 "writebackup",
10362#endif
10363#ifdef FEAT_XIM
10364 "xim",
10365#endif
10366#ifdef FEAT_XFONTSET
10367 "xfontset",
10368#endif
10369#ifdef USE_XSMP
10370 "xsmp",
10371#endif
10372#ifdef USE_XSMP_INTERACT
10373 "xsmp_interact",
10374#endif
10375#ifdef FEAT_XCLIPBOARD
10376 "xterm_clipboard",
10377#endif
10378#ifdef FEAT_XTERM_SAVE
10379 "xterm_save",
10380#endif
10381#if defined(UNIX) && defined(FEAT_X11)
10382 "X11",
10383#endif
10384 NULL
10385 };
10386
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010387 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 for (i = 0; has_list[i] != NULL; ++i)
10389 if (STRICMP(name, has_list[i]) == 0)
10390 {
10391 n = TRUE;
10392 break;
10393 }
10394
10395 if (n == FALSE)
10396 {
10397 if (STRNICMP(name, "patch", 5) == 0)
10398 n = has_patch(atoi((char *)name + 5));
10399 else if (STRICMP(name, "vim_starting") == 0)
10400 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010401#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10402 else if (STRICMP(name, "balloon_multiline") == 0)
10403 n = multiline_balloon_available();
10404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405#ifdef DYNAMIC_TCL
10406 else if (STRICMP(name, "tcl") == 0)
10407 n = tcl_enabled(FALSE);
10408#endif
10409#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10410 else if (STRICMP(name, "iconv") == 0)
10411 n = iconv_enabled(FALSE);
10412#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010413#ifdef DYNAMIC_MZSCHEME
10414 else if (STRICMP(name, "mzscheme") == 0)
10415 n = mzscheme_enabled(FALSE);
10416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417#ifdef DYNAMIC_RUBY
10418 else if (STRICMP(name, "ruby") == 0)
10419 n = ruby_enabled(FALSE);
10420#endif
10421#ifdef DYNAMIC_PYTHON
10422 else if (STRICMP(name, "python") == 0)
10423 n = python_enabled(FALSE);
10424#endif
10425#ifdef DYNAMIC_PERL
10426 else if (STRICMP(name, "perl") == 0)
10427 n = perl_enabled(FALSE);
10428#endif
10429#ifdef FEAT_GUI
10430 else if (STRICMP(name, "gui_running") == 0)
10431 n = (gui.in_use || gui.starting);
10432# ifdef FEAT_GUI_W32
10433 else if (STRICMP(name, "gui_win32s") == 0)
10434 n = gui_is_win32s();
10435# endif
10436# ifdef FEAT_BROWSE
10437 else if (STRICMP(name, "browse") == 0)
10438 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10439# endif
10440#endif
10441#ifdef FEAT_SYN_HL
10442 else if (STRICMP(name, "syntax_items") == 0)
10443 n = syntax_present(curbuf);
10444#endif
10445#if defined(WIN3264)
10446 else if (STRICMP(name, "win95") == 0)
10447 n = mch_windows95();
10448#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010449#ifdef FEAT_NETBEANS_INTG
10450 else if (STRICMP(name, "netbeans_enabled") == 0)
10451 n = usingNetbeans;
10452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 }
10454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010455 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456}
10457
10458/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010459 * "has_key()" function
10460 */
10461 static void
10462f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010463 typval_T *argvars;
10464 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010465{
10466 rettv->vval.v_number = 0;
10467 if (argvars[0].v_type != VAR_DICT)
10468 {
10469 EMSG(_(e_dictreq));
10470 return;
10471 }
10472 if (argvars[0].vval.v_dict == NULL)
10473 return;
10474
10475 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010476 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010477}
10478
10479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 * "hasmapto()" function
10481 */
10482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010483f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010484 typval_T *argvars;
10485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486{
10487 char_u *name;
10488 char_u *mode;
10489 char_u buf[NUMBUFLEN];
10490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010491 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010492 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 mode = (char_u *)"nvo";
10494 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010495 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496
10497 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010498 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010500 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501}
10502
10503/*
10504 * "histadd()" function
10505 */
10506/*ARGSUSED*/
10507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010508f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010509 typval_T *argvars;
10510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511{
10512#ifdef FEAT_CMDHIST
10513 int histype;
10514 char_u *str;
10515 char_u buf[NUMBUFLEN];
10516#endif
10517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010518 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 if (check_restricted() || check_secure())
10520 return;
10521#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010522 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10523 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 if (histype >= 0)
10525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010526 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 if (*str != NUL)
10528 {
10529 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010530 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 return;
10532 }
10533 }
10534#endif
10535}
10536
10537/*
10538 * "histdel()" function
10539 */
10540/*ARGSUSED*/
10541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010542f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010543 typval_T *argvars;
10544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545{
10546#ifdef FEAT_CMDHIST
10547 int n;
10548 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010549 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010551 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10552 if (str == NULL)
10553 n = 0;
10554 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010556 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010557 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010559 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010560 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 else
10562 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010563 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010564 get_tv_string_buf(&argvars[1], buf));
10565 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010567 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568#endif
10569}
10570
10571/*
10572 * "histget()" function
10573 */
10574/*ARGSUSED*/
10575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010576f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010577 typval_T *argvars;
10578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579{
10580#ifdef FEAT_CMDHIST
10581 int type;
10582 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010583 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010585 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10586 if (str == NULL)
10587 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010589 {
10590 type = get_histtype(str);
10591 if (argvars[1].v_type == VAR_UNKNOWN)
10592 idx = get_history_idx(type);
10593 else
10594 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10595 /* -1 on type error */
10596 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010599 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010601 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602}
10603
10604/*
10605 * "histnr()" function
10606 */
10607/*ARGSUSED*/
10608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010609f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010610 typval_T *argvars;
10611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612{
10613 int i;
10614
10615#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010616 char_u *history = get_tv_string_chk(&argvars[0]);
10617
10618 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619 if (i >= HIST_CMD && i < HIST_COUNT)
10620 i = get_history_idx(i);
10621 else
10622#endif
10623 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010624 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625}
10626
10627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 * "highlightID(name)" function
10629 */
10630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010631f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010632 typval_T *argvars;
10633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010635 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636}
10637
10638/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010639 * "highlight_exists()" function
10640 */
10641 static void
10642f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010643 typval_T *argvars;
10644 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010645{
10646 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10647}
10648
10649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 * "hostname()" function
10651 */
10652/*ARGSUSED*/
10653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010654f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010655 typval_T *argvars;
10656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657{
10658 char_u hostname[256];
10659
10660 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010661 rettv->v_type = VAR_STRING;
10662 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663}
10664
10665/*
10666 * iconv() function
10667 */
10668/*ARGSUSED*/
10669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010670f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 typval_T *argvars;
10672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673{
10674#ifdef FEAT_MBYTE
10675 char_u buf1[NUMBUFLEN];
10676 char_u buf2[NUMBUFLEN];
10677 char_u *from, *to, *str;
10678 vimconv_T vimconv;
10679#endif
10680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681 rettv->v_type = VAR_STRING;
10682 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683
10684#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010685 str = get_tv_string(&argvars[0]);
10686 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10687 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 vimconv.vc_type = CONV_NONE;
10689 convert_setup(&vimconv, from, to);
10690
10691 /* If the encodings are equal, no conversion needed. */
10692 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010693 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696
10697 convert_setup(&vimconv, NULL, NULL);
10698 vim_free(from);
10699 vim_free(to);
10700#endif
10701}
10702
10703/*
10704 * "indent()" function
10705 */
10706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010707f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010708 typval_T *argvars;
10709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710{
10711 linenr_T lnum;
10712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010713 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010715 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010717 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718}
10719
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010720/*
10721 * "index()" function
10722 */
10723 static void
10724f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010725 typval_T *argvars;
10726 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010727{
Bram Moolenaar33570922005-01-25 22:26:29 +000010728 list_T *l;
10729 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010730 long idx = 0;
10731 int ic = FALSE;
10732
10733 rettv->vval.v_number = -1;
10734 if (argvars[0].v_type != VAR_LIST)
10735 {
10736 EMSG(_(e_listreq));
10737 return;
10738 }
10739 l = argvars[0].vval.v_list;
10740 if (l != NULL)
10741 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010742 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010743 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010744 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010745 int error = FALSE;
10746
Bram Moolenaar758711c2005-02-02 23:11:38 +000010747 /* Start at specified item. Use the cached index that list_find()
10748 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010749 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010750 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010751 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010752 ic = get_tv_number_chk(&argvars[3], &error);
10753 if (error)
10754 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010755 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010756
Bram Moolenaar758711c2005-02-02 23:11:38 +000010757 for ( ; item != NULL; item = item->li_next, ++idx)
10758 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010759 {
10760 rettv->vval.v_number = idx;
10761 break;
10762 }
10763 }
10764}
10765
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766static int inputsecret_flag = 0;
10767
10768/*
10769 * "input()" function
10770 * Also handles inputsecret() when inputsecret is set.
10771 */
10772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010773f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010774 typval_T *argvars;
10775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010777 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 char_u *p = NULL;
10779 int c;
10780 char_u buf[NUMBUFLEN];
10781 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010782 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010784 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785
10786#ifdef NO_CONSOLE_INPUT
10787 /* While starting up, there is no place to enter text. */
10788 if (no_console_input())
10789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010790 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 return;
10792 }
10793#endif
10794
10795 cmd_silent = FALSE; /* Want to see the prompt. */
10796 if (prompt != NULL)
10797 {
10798 /* Only the part of the message after the last NL is considered as
10799 * prompt for the command line */
10800 p = vim_strrchr(prompt, '\n');
10801 if (p == NULL)
10802 p = prompt;
10803 else
10804 {
10805 ++p;
10806 c = *p;
10807 *p = NUL;
10808 msg_start();
10809 msg_clr_eos();
10810 msg_puts_attr(prompt, echo_attr);
10811 msg_didout = FALSE;
10812 msg_starthere();
10813 *p = c;
10814 }
10815 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010817 if (argvars[1].v_type != VAR_UNKNOWN)
10818 {
10819 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10820 if (defstr != NULL)
10821 stuffReadbuffSpec(defstr);
10822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010824 if (defstr != NULL)
10825 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010828 /* since the user typed this, no need to wait for return */
10829 need_wait_return = FALSE;
10830 msg_didout = FALSE;
10831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 cmd_silent = cmd_silent_save;
10833}
10834
10835/*
10836 * "inputdialog()" function
10837 */
10838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010839f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010840 typval_T *argvars;
10841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842{
10843#if defined(FEAT_GUI_TEXTDIALOG)
10844 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10845 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10846 {
10847 char_u *message;
10848 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010849 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010851 message = get_tv_string_chk(&argvars[0]);
10852 if (argvars[1].v_type != VAR_UNKNOWN
10853 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010854 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 else
10856 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010857 if (message != NULL && defstr != NULL
10858 && do_dialog(VIM_QUESTION, NULL, message,
10859 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010860 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 else
10862 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010863 if (message != NULL && defstr != NULL
10864 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010865 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010866 rettv->vval.v_string = vim_strsave(
10867 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010871 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 }
10873 else
10874#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010875 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876}
10877
10878static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10879
10880/*
10881 * "inputrestore()" function
10882 */
10883/*ARGSUSED*/
10884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010885f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010886 typval_T *argvars;
10887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888{
10889 if (ga_userinput.ga_len > 0)
10890 {
10891 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10893 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010894 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 }
10896 else if (p_verbose > 1)
10897 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010898 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010899 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 }
10901}
10902
10903/*
10904 * "inputsave()" function
10905 */
10906/*ARGSUSED*/
10907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010909 typval_T *argvars;
10910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911{
10912 /* Add an entry to the stack of typehead storage. */
10913 if (ga_grow(&ga_userinput, 1) == OK)
10914 {
10915 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10916 + ga_userinput.ga_len);
10917 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010918 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 }
10920 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010921 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922}
10923
10924/*
10925 * "inputsecret()" function
10926 */
10927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010928f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010929 typval_T *argvars;
10930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931{
10932 ++cmdline_star;
10933 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010934 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 --cmdline_star;
10936 --inputsecret_flag;
10937}
10938
10939/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010940 * "insert()" function
10941 */
10942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010943f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010944 typval_T *argvars;
10945 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010946{
10947 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010948 listitem_T *item;
10949 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010950 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010951
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010952 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010953 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010954 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010955 else if ((l = argvars[0].vval.v_list) != NULL
10956 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010957 {
10958 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010959 before = get_tv_number_chk(&argvars[2], &error);
10960 if (error)
10961 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010962
Bram Moolenaar758711c2005-02-02 23:11:38 +000010963 if (before == l->lv_len)
10964 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010965 else
10966 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010967 item = list_find(l, before);
10968 if (item == NULL)
10969 {
10970 EMSGN(_(e_listidx), before);
10971 l = NULL;
10972 }
10973 }
10974 if (l != NULL)
10975 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010976 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010977 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010978 }
10979 }
10980}
10981
10982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 * "isdirectory()" function
10984 */
10985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010986f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010987 typval_T *argvars;
10988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010990 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991}
10992
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010993/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010994 * "islocked()" function
10995 */
10996 static void
10997f_islocked(argvars, rettv)
10998 typval_T *argvars;
10999 typval_T *rettv;
11000{
11001 lval_T lv;
11002 char_u *end;
11003 dictitem_T *di;
11004
11005 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011006 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11007 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011008 if (end != NULL && lv.ll_name != NULL)
11009 {
11010 if (*end != NUL)
11011 EMSG(_(e_trailing));
11012 else
11013 {
11014 if (lv.ll_tv == NULL)
11015 {
11016 if (check_changedtick(lv.ll_name))
11017 rettv->vval.v_number = 1; /* always locked */
11018 else
11019 {
11020 di = find_var(lv.ll_name, NULL);
11021 if (di != NULL)
11022 {
11023 /* Consider a variable locked when:
11024 * 1. the variable itself is locked
11025 * 2. the value of the variable is locked.
11026 * 3. the List or Dict value is locked.
11027 */
11028 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11029 || tv_islocked(&di->di_tv));
11030 }
11031 }
11032 }
11033 else if (lv.ll_range)
11034 EMSG(_("E745: Range not allowed"));
11035 else if (lv.ll_newkey != NULL)
11036 EMSG2(_(e_dictkey), lv.ll_newkey);
11037 else if (lv.ll_list != NULL)
11038 /* List item. */
11039 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11040 else
11041 /* Dictionary item. */
11042 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11043 }
11044 }
11045
11046 clear_lval(&lv);
11047}
11048
Bram Moolenaar33570922005-01-25 22:26:29 +000011049static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011050
11051/*
11052 * Turn a dict into a list:
11053 * "what" == 0: list of keys
11054 * "what" == 1: list of values
11055 * "what" == 2: list of items
11056 */
11057 static void
11058dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011059 typval_T *argvars;
11060 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011061 int what;
11062{
Bram Moolenaar33570922005-01-25 22:26:29 +000011063 list_T *l;
11064 list_T *l2;
11065 dictitem_T *di;
11066 hashitem_T *hi;
11067 listitem_T *li;
11068 listitem_T *li2;
11069 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011070 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011071
11072 rettv->vval.v_number = 0;
11073 if (argvars[0].v_type != VAR_DICT)
11074 {
11075 EMSG(_(e_dictreq));
11076 return;
11077 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011078 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011079 return;
11080
11081 l = list_alloc();
11082 if (l == NULL)
11083 return;
11084 rettv->v_type = VAR_LIST;
11085 rettv->vval.v_list = l;
11086 ++l->lv_refcount;
11087
Bram Moolenaar33570922005-01-25 22:26:29 +000011088 todo = d->dv_hashtab.ht_used;
11089 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011090 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011091 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011092 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011093 --todo;
11094 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011095
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011096 li = listitem_alloc();
11097 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011098 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011099 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011100
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011101 if (what == 0)
11102 {
11103 /* keys() */
11104 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011105 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011106 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11107 }
11108 else if (what == 1)
11109 {
11110 /* values() */
11111 copy_tv(&di->di_tv, &li->li_tv);
11112 }
11113 else
11114 {
11115 /* items() */
11116 l2 = list_alloc();
11117 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011118 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011119 li->li_tv.vval.v_list = l2;
11120 if (l2 == NULL)
11121 break;
11122 ++l2->lv_refcount;
11123
11124 li2 = listitem_alloc();
11125 if (li2 == NULL)
11126 break;
11127 list_append(l2, li2);
11128 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011129 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011130 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11131
11132 li2 = listitem_alloc();
11133 if (li2 == NULL)
11134 break;
11135 list_append(l2, li2);
11136 copy_tv(&di->di_tv, &li2->li_tv);
11137 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011138 }
11139 }
11140}
11141
11142/*
11143 * "items(dict)" function
11144 */
11145 static void
11146f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011147 typval_T *argvars;
11148 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011149{
11150 dict_list(argvars, rettv, 2);
11151}
11152
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011154 * "join()" function
11155 */
11156 static void
11157f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011158 typval_T *argvars;
11159 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011160{
11161 garray_T ga;
11162 char_u *sep;
11163
11164 rettv->vval.v_number = 0;
11165 if (argvars[0].v_type != VAR_LIST)
11166 {
11167 EMSG(_(e_listreq));
11168 return;
11169 }
11170 if (argvars[0].vval.v_list == NULL)
11171 return;
11172 if (argvars[1].v_type == VAR_UNKNOWN)
11173 sep = (char_u *)" ";
11174 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011175 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011176
11177 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011178
11179 if (sep != NULL)
11180 {
11181 ga_init2(&ga, (int)sizeof(char), 80);
11182 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11183 ga_append(&ga, NUL);
11184 rettv->vval.v_string = (char_u *)ga.ga_data;
11185 }
11186 else
11187 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011188}
11189
11190/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011191 * "keys()" function
11192 */
11193 static void
11194f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011195 typval_T *argvars;
11196 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011197{
11198 dict_list(argvars, rettv, 0);
11199}
11200
11201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 * "last_buffer_nr()" function.
11203 */
11204/*ARGSUSED*/
11205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011206f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011207 typval_T *argvars;
11208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209{
11210 int n = 0;
11211 buf_T *buf;
11212
11213 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11214 if (n < buf->b_fnum)
11215 n = buf->b_fnum;
11216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011217 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011218}
11219
11220/*
11221 * "len()" function
11222 */
11223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011224f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011225 typval_T *argvars;
11226 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011227{
11228 switch (argvars[0].v_type)
11229 {
11230 case VAR_STRING:
11231 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011232 rettv->vval.v_number = (varnumber_T)STRLEN(
11233 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011234 break;
11235 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011237 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011238 case VAR_DICT:
11239 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11240 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011241 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011242 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011243 break;
11244 }
11245}
11246
Bram Moolenaar33570922005-01-25 22:26:29 +000011247static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011248
11249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011250libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011251 typval_T *argvars;
11252 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011253 int type;
11254{
11255#ifdef FEAT_LIBCALL
11256 char_u *string_in;
11257 char_u **string_result;
11258 int nr_result;
11259#endif
11260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011261 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011262 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011263 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011264 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011266
11267 if (check_restricted() || check_secure())
11268 return;
11269
11270#ifdef FEAT_LIBCALL
11271 /* The first two args must be strings, otherwise its meaningless */
11272 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11273 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011274 string_in = NULL;
11275 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011276 string_in = argvars[2].vval.v_string;
11277 if (type == VAR_NUMBER)
11278 string_result = NULL;
11279 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011281 if (mch_libcall(argvars[0].vval.v_string,
11282 argvars[1].vval.v_string,
11283 string_in,
11284 argvars[2].vval.v_number,
11285 string_result,
11286 &nr_result) == OK
11287 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011289 }
11290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291}
11292
11293/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011294 * "libcall()" function
11295 */
11296 static void
11297f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011298 typval_T *argvars;
11299 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011300{
11301 libcall_common(argvars, rettv, VAR_STRING);
11302}
11303
11304/*
11305 * "libcallnr()" function
11306 */
11307 static void
11308f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011309 typval_T *argvars;
11310 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011311{
11312 libcall_common(argvars, rettv, VAR_NUMBER);
11313}
11314
11315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 * "line(string)" function
11317 */
11318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011319f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011320 typval_T *argvars;
11321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322{
11323 linenr_T lnum = 0;
11324 pos_T *fp;
11325
11326 fp = var2fpos(&argvars[0], TRUE);
11327 if (fp != NULL)
11328 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011329 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330}
11331
11332/*
11333 * "line2byte(lnum)" function
11334 */
11335/*ARGSUSED*/
11336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011337f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011338 typval_T *argvars;
11339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340{
11341#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343#else
11344 linenr_T lnum;
11345
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011348 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011350 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11351 if (rettv->vval.v_number >= 0)
11352 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353#endif
11354}
11355
11356/*
11357 * "lispindent(lnum)" function
11358 */
11359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011360f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011361 typval_T *argvars;
11362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363{
11364#ifdef FEAT_LISP
11365 pos_T pos;
11366 linenr_T lnum;
11367
11368 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011369 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11371 {
11372 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011373 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 curwin->w_cursor = pos;
11375 }
11376 else
11377#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011378 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379}
11380
11381/*
11382 * "localtime()" function
11383 */
11384/*ARGSUSED*/
11385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011387 typval_T *argvars;
11388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391}
11392
Bram Moolenaar33570922005-01-25 22:26:29 +000011393static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394
11395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011397 typval_T *argvars;
11398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 int exact;
11400{
11401 char_u *keys;
11402 char_u *which;
11403 char_u buf[NUMBUFLEN];
11404 char_u *keys_buf = NULL;
11405 char_u *rhs;
11406 int mode;
11407 garray_T ga;
11408
11409 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410 rettv->v_type = VAR_STRING;
11411 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 if (*keys == NUL)
11415 return;
11416
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011417 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011418 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419 else
11420 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011421 if (which == NULL)
11422 return;
11423
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424 mode = get_map_mode(&which, 0);
11425
11426 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011427 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428 vim_free(keys_buf);
11429 if (rhs != NULL)
11430 {
11431 ga_init(&ga);
11432 ga.ga_itemsize = 1;
11433 ga.ga_growsize = 40;
11434
11435 while (*rhs != NUL)
11436 ga_concat(&ga, str2special(&rhs, FALSE));
11437
11438 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440 }
11441}
11442
11443/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011444 * "map()" function
11445 */
11446 static void
11447f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011448 typval_T *argvars;
11449 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011450{
11451 filter_map(argvars, rettv, TRUE);
11452}
11453
11454/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011455 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456 */
11457 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011458f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011459 typval_T *argvars;
11460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011462 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463}
11464
11465/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011466 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 */
11468 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011469f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011470 typval_T *argvars;
11471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011473 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474}
11475
Bram Moolenaar33570922005-01-25 22:26:29 +000011476static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477
11478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011479find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011480 typval_T *argvars;
11481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 int type;
11483{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011484 char_u *str = NULL;
11485 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486 char_u *pat;
11487 regmatch_T regmatch;
11488 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011489 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490 char_u *save_cpo;
11491 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011492 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011493 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011494 list_T *l = NULL;
11495 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011496 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011497 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498
11499 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11500 save_cpo = p_cpo;
11501 p_cpo = (char_u *)"";
11502
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011503 rettv->vval.v_number = -1;
11504 if (type == 3)
11505 {
11506 /* return empty list when there are no matches */
11507 if ((rettv->vval.v_list = list_alloc()) == NULL)
11508 goto theend;
11509 rettv->v_type = VAR_LIST;
11510 ++rettv->vval.v_list->lv_refcount;
11511 }
11512 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011514 rettv->v_type = VAR_STRING;
11515 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011518 if (argvars[0].v_type == VAR_LIST)
11519 {
11520 if ((l = argvars[0].vval.v_list) == NULL)
11521 goto theend;
11522 li = l->lv_first;
11523 }
11524 else
11525 expr = str = get_tv_string(&argvars[0]);
11526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011527 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11528 if (pat == NULL)
11529 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011530
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011531 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011533 int error = FALSE;
11534
11535 start = get_tv_number_chk(&argvars[2], &error);
11536 if (error)
11537 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011538 if (l != NULL)
11539 {
11540 li = list_find(l, start);
11541 if (li == NULL)
11542 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011543 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011544 }
11545 else
11546 {
11547 if (start < 0)
11548 start = 0;
11549 if (start > (long)STRLEN(str))
11550 goto theend;
11551 str += start;
11552 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011553
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011554 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011555 nth = get_tv_number_chk(&argvars[3], &error);
11556 if (error)
11557 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558 }
11559
11560 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11561 if (regmatch.regprog != NULL)
11562 {
11563 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011564
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011565 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011566 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011567 if (l != NULL)
11568 {
11569 if (li == NULL)
11570 {
11571 match = FALSE;
11572 break;
11573 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011574 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011575 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011576 if (str == NULL)
11577 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011578 }
11579
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011580 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011581
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011582 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011583 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011584 if (l == NULL && !match)
11585 break;
11586
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011587 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011588 if (l != NULL)
11589 {
11590 li = li->li_next;
11591 ++idx;
11592 }
11593 else
11594 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011595#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011596 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011597#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011598 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011599#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011600 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011601 }
11602
11603 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011605 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011606 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011607 int i;
11608
11609 /* return list with matched string and submatches */
11610 for (i = 0; i < NSUBEXP; ++i)
11611 {
11612 if (regmatch.endp[i] == NULL)
11613 break;
11614 li = listitem_alloc();
11615 if (li == NULL)
11616 break;
11617 li->li_tv.v_type = VAR_STRING;
11618 li->li_tv.v_lock = 0;
11619 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11620 (int)(regmatch.endp[i] - regmatch.startp[i]));
11621 list_append(rettv->vval.v_list, li);
11622 }
11623 }
11624 else if (type == 2)
11625 {
11626 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011627 if (l != NULL)
11628 copy_tv(&li->li_tv, rettv);
11629 else
11630 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011632 }
11633 else if (l != NULL)
11634 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635 else
11636 {
11637 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011638 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 (varnumber_T)(regmatch.startp[0] - str);
11640 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011641 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011643 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644 }
11645 }
11646 vim_free(regmatch.regprog);
11647 }
11648
11649theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011650 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651 p_cpo = save_cpo;
11652}
11653
11654/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011655 * "match()" function
11656 */
11657 static void
11658f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011659 typval_T *argvars;
11660 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011661{
11662 find_some_match(argvars, rettv, 1);
11663}
11664
11665/*
11666 * "matchend()" function
11667 */
11668 static void
11669f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011670 typval_T *argvars;
11671 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011672{
11673 find_some_match(argvars, rettv, 0);
11674}
11675
11676/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011677 * "matchlist()" function
11678 */
11679 static void
11680f_matchlist(argvars, rettv)
11681 typval_T *argvars;
11682 typval_T *rettv;
11683{
11684 find_some_match(argvars, rettv, 3);
11685}
11686
11687/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011688 * "matchstr()" function
11689 */
11690 static void
11691f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011692 typval_T *argvars;
11693 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011694{
11695 find_some_match(argvars, rettv, 2);
11696}
11697
Bram Moolenaar33570922005-01-25 22:26:29 +000011698static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011699
11700 static void
11701max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011702 typval_T *argvars;
11703 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011704 int domax;
11705{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011706 long n = 0;
11707 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011708 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011709
11710 if (argvars[0].v_type == VAR_LIST)
11711 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011712 list_T *l;
11713 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011714
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011715 l = argvars[0].vval.v_list;
11716 if (l != NULL)
11717 {
11718 li = l->lv_first;
11719 if (li != NULL)
11720 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011721 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011722 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011723 {
11724 li = li->li_next;
11725 if (li == NULL)
11726 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011727 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011728 if (domax ? i > n : i < n)
11729 n = i;
11730 }
11731 }
11732 }
11733 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011734 else if (argvars[0].v_type == VAR_DICT)
11735 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011736 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011737 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011738 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011739 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011740
11741 d = argvars[0].vval.v_dict;
11742 if (d != NULL)
11743 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011744 todo = d->dv_hashtab.ht_used;
11745 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011746 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011747 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011748 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011749 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011750 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011751 if (first)
11752 {
11753 n = i;
11754 first = FALSE;
11755 }
11756 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011757 n = i;
11758 }
11759 }
11760 }
11761 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011762 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011763 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011764 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011765}
11766
11767/*
11768 * "max()" function
11769 */
11770 static void
11771f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011772 typval_T *argvars;
11773 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011774{
11775 max_min(argvars, rettv, TRUE);
11776}
11777
11778/*
11779 * "min()" function
11780 */
11781 static void
11782f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011783 typval_T *argvars;
11784 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011785{
11786 max_min(argvars, rettv, FALSE);
11787}
11788
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011789static int mkdir_recurse __ARGS((char_u *dir, int prot));
11790
11791/*
11792 * Create the directory in which "dir" is located, and higher levels when
11793 * needed.
11794 */
11795 static int
11796mkdir_recurse(dir, prot)
11797 char_u *dir;
11798 int prot;
11799{
11800 char_u *p;
11801 char_u *updir;
11802 int r = FAIL;
11803
11804 /* Get end of directory name in "dir".
11805 * We're done when it's "/" or "c:/". */
11806 p = gettail_sep(dir);
11807 if (p <= get_past_head(dir))
11808 return OK;
11809
11810 /* If the directory exists we're done. Otherwise: create it.*/
11811 updir = vim_strnsave(dir, (int)(p - dir));
11812 if (updir == NULL)
11813 return FAIL;
11814 if (mch_isdir(updir))
11815 r = OK;
11816 else if (mkdir_recurse(updir, prot) == OK)
11817 r = vim_mkdir_emsg(updir, prot);
11818 vim_free(updir);
11819 return r;
11820}
11821
11822#ifdef vim_mkdir
11823/*
11824 * "mkdir()" function
11825 */
11826 static void
11827f_mkdir(argvars, rettv)
11828 typval_T *argvars;
11829 typval_T *rettv;
11830{
11831 char_u *dir;
11832 char_u buf[NUMBUFLEN];
11833 int prot = 0755;
11834
11835 rettv->vval.v_number = FAIL;
11836 if (check_restricted() || check_secure())
11837 return;
11838
11839 dir = get_tv_string_buf(&argvars[0], buf);
11840 if (argvars[1].v_type != VAR_UNKNOWN)
11841 {
11842 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011843 prot = get_tv_number_chk(&argvars[2], NULL);
11844 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011845 mkdir_recurse(dir, prot);
11846 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011847 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011848}
11849#endif
11850
Bram Moolenaar0d660222005-01-07 21:51:51 +000011851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852 * "mode()" function
11853 */
11854/*ARGSUSED*/
11855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011856f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011857 typval_T *argvars;
11858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859{
11860 char_u buf[2];
11861
11862#ifdef FEAT_VISUAL
11863 if (VIsual_active)
11864 {
11865 if (VIsual_select)
11866 buf[0] = VIsual_mode + 's' - 'v';
11867 else
11868 buf[0] = VIsual_mode;
11869 }
11870 else
11871#endif
11872 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11873 buf[0] = 'r';
11874 else if (State & INSERT)
11875 {
11876 if (State & REPLACE_FLAG)
11877 buf[0] = 'R';
11878 else
11879 buf[0] = 'i';
11880 }
11881 else if (State & CMDLINE)
11882 buf[0] = 'c';
11883 else
11884 buf[0] = 'n';
11885
11886 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011887 rettv->vval.v_string = vim_strsave(buf);
11888 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889}
11890
11891/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011892 * "nextnonblank()" function
11893 */
11894 static void
11895f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011896 typval_T *argvars;
11897 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011898{
11899 linenr_T lnum;
11900
11901 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11902 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011903 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011904 {
11905 lnum = 0;
11906 break;
11907 }
11908 if (*skipwhite(ml_get(lnum)) != NUL)
11909 break;
11910 }
11911 rettv->vval.v_number = lnum;
11912}
11913
11914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 * "nr2char()" function
11916 */
11917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011918f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011919 typval_T *argvars;
11920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921{
11922 char_u buf[NUMBUFLEN];
11923
11924#ifdef FEAT_MBYTE
11925 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011926 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 else
11928#endif
11929 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011930 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931 buf[1] = NUL;
11932 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011933 rettv->v_type = VAR_STRING;
11934 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011935}
11936
11937/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011938 * "prevnonblank()" function
11939 */
11940 static void
11941f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011942 typval_T *argvars;
11943 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011944{
11945 linenr_T lnum;
11946
11947 lnum = get_tv_lnum(argvars);
11948 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11949 lnum = 0;
11950 else
11951 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11952 --lnum;
11953 rettv->vval.v_number = lnum;
11954}
11955
Bram Moolenaara6c840d2005-08-22 22:59:46 +000011956#ifdef HAVE_STDARG_H
11957/* This dummy va_list is here because:
11958 * - passing a NULL pointer doesn't work when va_list isn't a pointer
11959 * - locally in the function results in a "used before set" warning
11960 * - using va_start() to initialize it gives "function with fixed args" error */
11961static va_list ap;
11962#endif
11963
Bram Moolenaar8c711452005-01-14 21:53:12 +000011964/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000011965 * "printf()" function
11966 */
11967 static void
11968f_printf(argvars, rettv)
11969 typval_T *argvars;
11970 typval_T *rettv;
11971{
11972 rettv->v_type = VAR_STRING;
11973 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000011974#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000011975 {
11976 char_u buf[NUMBUFLEN];
11977 int len;
11978 char_u *s;
11979 int saved_did_emsg = did_emsg;
11980 char *fmt;
11981
11982 /* Get the required length, allocate the buffer and do it for real. */
11983 did_emsg = FALSE;
11984 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011985 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000011986 if (!did_emsg)
11987 {
11988 s = alloc(len + 1);
11989 if (s != NULL)
11990 {
11991 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011992 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000011993 }
11994 }
11995 did_emsg |= saved_did_emsg;
11996 }
11997#endif
11998}
11999
12000/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012001 * "range()" function
12002 */
12003 static void
12004f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012005 typval_T *argvars;
12006 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012007{
12008 long start;
12009 long end;
12010 long stride = 1;
12011 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012012 list_T *l;
12013 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012014 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012016 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012017 if (argvars[1].v_type == VAR_UNKNOWN)
12018 {
12019 end = start - 1;
12020 start = 0;
12021 }
12022 else
12023 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012024 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012025 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012026 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012027 }
12028
12029 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012030 if (error)
12031 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012032 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012033 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012034 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012035 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012036 else
12037 {
12038 l = list_alloc();
12039 if (l != NULL)
12040 {
12041 rettv->v_type = VAR_LIST;
12042 rettv->vval.v_list = l;
12043 ++l->lv_refcount;
12044
12045 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
12046 {
12047 li = listitem_alloc();
12048 if (li == NULL)
12049 break;
12050 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012051 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012052 li->li_tv.vval.v_number = i;
12053 list_append(l, li);
12054 }
12055 }
12056 }
12057}
12058
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012059/*
12060 * "readfile()" function
12061 */
12062 static void
12063f_readfile(argvars, rettv)
12064 typval_T *argvars;
12065 typval_T *rettv;
12066{
12067 int binary = FALSE;
12068 char_u *fname;
12069 FILE *fd;
12070 list_T *l;
12071 listitem_T *li;
12072#define FREAD_SIZE 200 /* optimized for text lines */
12073 char_u buf[FREAD_SIZE];
12074 int readlen; /* size of last fread() */
12075 int buflen; /* nr of valid chars in buf[] */
12076 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12077 int tolist; /* first byte in buf[] still to be put in list */
12078 int chop; /* how many CR to chop off */
12079 char_u *prev = NULL; /* previously read bytes, if any */
12080 int prevlen = 0; /* length of "prev" if not NULL */
12081 char_u *s;
12082 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012083 long maxline = MAXLNUM;
12084 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012085
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012086 if (argvars[1].v_type != VAR_UNKNOWN)
12087 {
12088 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12089 binary = TRUE;
12090 if (argvars[2].v_type != VAR_UNKNOWN)
12091 maxline = get_tv_number(&argvars[2]);
12092 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012093
12094 l = list_alloc();
12095 if (l == NULL)
12096 return;
12097 rettv->v_type = VAR_LIST;
12098 rettv->vval.v_list = l;
12099 l->lv_refcount = 1;
12100
12101 /* Always open the file in binary mode, library functions have a mind of
12102 * their own about CR-LF conversion. */
12103 fname = get_tv_string(&argvars[0]);
12104 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12105 {
12106 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12107 return;
12108 }
12109
12110 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012111 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012112 {
12113 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12114 buflen = filtd + readlen;
12115 tolist = 0;
12116 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12117 {
12118 if (buf[filtd] == '\n' || readlen <= 0)
12119 {
12120 /* Only when in binary mode add an empty list item when the
12121 * last line ends in a '\n'. */
12122 if (!binary && readlen == 0 && filtd == 0)
12123 break;
12124
12125 /* Found end-of-line or end-of-file: add a text line to the
12126 * list. */
12127 chop = 0;
12128 if (!binary)
12129 while (filtd - chop - 1 >= tolist
12130 && buf[filtd - chop - 1] == '\r')
12131 ++chop;
12132 len = filtd - tolist - chop;
12133 if (prev == NULL)
12134 s = vim_strnsave(buf + tolist, len);
12135 else
12136 {
12137 s = alloc((unsigned)(prevlen + len + 1));
12138 if (s != NULL)
12139 {
12140 mch_memmove(s, prev, prevlen);
12141 vim_free(prev);
12142 prev = NULL;
12143 mch_memmove(s + prevlen, buf + tolist, len);
12144 s[prevlen + len] = NUL;
12145 }
12146 }
12147 tolist = filtd + 1;
12148
12149 li = listitem_alloc();
12150 if (li == NULL)
12151 {
12152 vim_free(s);
12153 break;
12154 }
12155 li->li_tv.v_type = VAR_STRING;
12156 li->li_tv.v_lock = 0;
12157 li->li_tv.vval.v_string = s;
12158 list_append(l, li);
12159
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012160 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012161 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012162 if (readlen <= 0)
12163 break;
12164 }
12165 else if (buf[filtd] == NUL)
12166 buf[filtd] = '\n';
12167 }
12168 if (readlen <= 0)
12169 break;
12170
12171 if (tolist == 0)
12172 {
12173 /* "buf" is full, need to move text to an allocated buffer */
12174 if (prev == NULL)
12175 {
12176 prev = vim_strnsave(buf, buflen);
12177 prevlen = buflen;
12178 }
12179 else
12180 {
12181 s = alloc((unsigned)(prevlen + buflen));
12182 if (s != NULL)
12183 {
12184 mch_memmove(s, prev, prevlen);
12185 mch_memmove(s + prevlen, buf, buflen);
12186 vim_free(prev);
12187 prev = s;
12188 prevlen += buflen;
12189 }
12190 }
12191 filtd = 0;
12192 }
12193 else
12194 {
12195 mch_memmove(buf, buf + tolist, buflen - tolist);
12196 filtd -= tolist;
12197 }
12198 }
12199
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012200 /*
12201 * For a negative line count use only the lines at the end of the file,
12202 * free the rest.
12203 */
12204 if (maxline < 0)
12205 while (cnt > -maxline)
12206 {
12207 listitem_remove(l, l->lv_first);
12208 --cnt;
12209 }
12210
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012211 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012212 fclose(fd);
12213}
12214
12215
Bram Moolenaar0d660222005-01-07 21:51:51 +000012216#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12217static void make_connection __ARGS((void));
12218static int check_connection __ARGS((void));
12219
12220 static void
12221make_connection()
12222{
12223 if (X_DISPLAY == NULL
12224# ifdef FEAT_GUI
12225 && !gui.in_use
12226# endif
12227 )
12228 {
12229 x_force_connect = TRUE;
12230 setup_term_clip();
12231 x_force_connect = FALSE;
12232 }
12233}
12234
12235 static int
12236check_connection()
12237{
12238 make_connection();
12239 if (X_DISPLAY == NULL)
12240 {
12241 EMSG(_("E240: No connection to Vim server"));
12242 return FAIL;
12243 }
12244 return OK;
12245}
12246#endif
12247
12248#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012249static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012250
12251 static void
12252remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012253 typval_T *argvars;
12254 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012255 int expr;
12256{
12257 char_u *server_name;
12258 char_u *keys;
12259 char_u *r = NULL;
12260 char_u buf[NUMBUFLEN];
12261# ifdef WIN32
12262 HWND w;
12263# else
12264 Window w;
12265# endif
12266
12267 if (check_restricted() || check_secure())
12268 return;
12269
12270# ifdef FEAT_X11
12271 if (check_connection() == FAIL)
12272 return;
12273# endif
12274
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012275 server_name = get_tv_string_chk(&argvars[0]);
12276 if (server_name == NULL)
12277 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012278 keys = get_tv_string_buf(&argvars[1], buf);
12279# ifdef WIN32
12280 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12281# else
12282 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12283 < 0)
12284# endif
12285 {
12286 if (r != NULL)
12287 EMSG(r); /* sending worked but evaluation failed */
12288 else
12289 EMSG2(_("E241: Unable to send to %s"), server_name);
12290 return;
12291 }
12292
12293 rettv->vval.v_string = r;
12294
12295 if (argvars[2].v_type != VAR_UNKNOWN)
12296 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012297 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012298 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012299 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012300
12301 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012302 v.di_tv.v_type = VAR_STRING;
12303 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012304 idvar = get_tv_string_chk(&argvars[2]);
12305 if (idvar != NULL)
12306 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012307 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012308 }
12309}
12310#endif
12311
12312/*
12313 * "remote_expr()" function
12314 */
12315/*ARGSUSED*/
12316 static void
12317f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012318 typval_T *argvars;
12319 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012320{
12321 rettv->v_type = VAR_STRING;
12322 rettv->vval.v_string = NULL;
12323#ifdef FEAT_CLIENTSERVER
12324 remote_common(argvars, rettv, TRUE);
12325#endif
12326}
12327
12328/*
12329 * "remote_foreground()" function
12330 */
12331/*ARGSUSED*/
12332 static void
12333f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012334 typval_T *argvars;
12335 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012336{
12337 rettv->vval.v_number = 0;
12338#ifdef FEAT_CLIENTSERVER
12339# ifdef WIN32
12340 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012341 {
12342 char_u *server_name = get_tv_string_chk(&argvars[0]);
12343
12344 if (server_name != NULL)
12345 serverForeground(server_name);
12346 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012347# else
12348 /* Send a foreground() expression to the server. */
12349 argvars[1].v_type = VAR_STRING;
12350 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12351 argvars[2].v_type = VAR_UNKNOWN;
12352 remote_common(argvars, rettv, TRUE);
12353 vim_free(argvars[1].vval.v_string);
12354# endif
12355#endif
12356}
12357
12358/*ARGSUSED*/
12359 static void
12360f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012361 typval_T *argvars;
12362 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012363{
12364#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012365 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012366 char_u *s = NULL;
12367# ifdef WIN32
12368 int n = 0;
12369# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012370 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012371
12372 if (check_restricted() || check_secure())
12373 {
12374 rettv->vval.v_number = -1;
12375 return;
12376 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012377 serverid = get_tv_string_chk(&argvars[0]);
12378 if (serverid == NULL)
12379 {
12380 rettv->vval.v_number = -1;
12381 return; /* type error; errmsg already given */
12382 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012383# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012384 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012385 if (n == 0)
12386 rettv->vval.v_number = -1;
12387 else
12388 {
12389 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12390 rettv->vval.v_number = (s != NULL);
12391 }
12392# else
12393 rettv->vval.v_number = 0;
12394 if (check_connection() == FAIL)
12395 return;
12396
12397 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012398 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012399# endif
12400
12401 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012403 char_u *retvar;
12404
Bram Moolenaar33570922005-01-25 22:26:29 +000012405 v.di_tv.v_type = VAR_STRING;
12406 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012407 retvar = get_tv_string_chk(&argvars[1]);
12408 if (retvar != NULL)
12409 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012410 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012411 }
12412#else
12413 rettv->vval.v_number = -1;
12414#endif
12415}
12416
12417/*ARGSUSED*/
12418 static void
12419f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012420 typval_T *argvars;
12421 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012422{
12423 char_u *r = NULL;
12424
12425#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012426 char_u *serverid = get_tv_string_chk(&argvars[0]);
12427
12428 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012429 {
12430# ifdef WIN32
12431 /* The server's HWND is encoded in the 'id' parameter */
12432 int n = 0;
12433
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012434 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012435 if (n != 0)
12436 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12437 if (r == NULL)
12438# else
12439 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012440 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012441# endif
12442 EMSG(_("E277: Unable to read a server reply"));
12443 }
12444#endif
12445 rettv->v_type = VAR_STRING;
12446 rettv->vval.v_string = r;
12447}
12448
12449/*
12450 * "remote_send()" function
12451 */
12452/*ARGSUSED*/
12453 static void
12454f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012455 typval_T *argvars;
12456 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012457{
12458 rettv->v_type = VAR_STRING;
12459 rettv->vval.v_string = NULL;
12460#ifdef FEAT_CLIENTSERVER
12461 remote_common(argvars, rettv, FALSE);
12462#endif
12463}
12464
12465/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012466 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012467 */
12468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012469f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012470 typval_T *argvars;
12471 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012472{
Bram Moolenaar33570922005-01-25 22:26:29 +000012473 list_T *l;
12474 listitem_T *item, *item2;
12475 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012476 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012477 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012478 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012479 dict_T *d;
12480 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012481
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012482 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012483 if (argvars[0].v_type == VAR_DICT)
12484 {
12485 if (argvars[2].v_type != VAR_UNKNOWN)
12486 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012487 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012488 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012489 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012490 key = get_tv_string_chk(&argvars[1]);
12491 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012492 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012493 di = dict_find(d, key, -1);
12494 if (di == NULL)
12495 EMSG2(_(e_dictkey), key);
12496 else
12497 {
12498 *rettv = di->di_tv;
12499 init_tv(&di->di_tv);
12500 dictitem_remove(d, di);
12501 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012502 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012503 }
12504 }
12505 else if (argvars[0].v_type != VAR_LIST)
12506 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012507 else if ((l = argvars[0].vval.v_list) != NULL
12508 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012510 int error = FALSE;
12511
12512 idx = get_tv_number_chk(&argvars[1], &error);
12513 if (error)
12514 ; /* type error: do nothing, errmsg already given */
12515 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012516 EMSGN(_(e_listidx), idx);
12517 else
12518 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012519 if (argvars[2].v_type == VAR_UNKNOWN)
12520 {
12521 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012522 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012523 *rettv = item->li_tv;
12524 vim_free(item);
12525 }
12526 else
12527 {
12528 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012529 end = get_tv_number_chk(&argvars[2], &error);
12530 if (error)
12531 ; /* type error: do nothing */
12532 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012533 EMSGN(_(e_listidx), end);
12534 else
12535 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012536 int cnt = 0;
12537
12538 for (li = item; li != NULL; li = li->li_next)
12539 {
12540 ++cnt;
12541 if (li == item2)
12542 break;
12543 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012544 if (li == NULL) /* didn't find "item2" after "item" */
12545 EMSG(_(e_invrange));
12546 else
12547 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012548 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012549 l = list_alloc();
12550 if (l != NULL)
12551 {
12552 rettv->v_type = VAR_LIST;
12553 rettv->vval.v_list = l;
12554 l->lv_first = item;
12555 l->lv_last = item2;
12556 l->lv_refcount = 1;
12557 item->li_prev = NULL;
12558 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012559 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012560 }
12561 }
12562 }
12563 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012564 }
12565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566}
12567
12568/*
12569 * "rename({from}, {to})" function
12570 */
12571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012572f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012573 typval_T *argvars;
12574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012575{
12576 char_u buf[NUMBUFLEN];
12577
12578 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012579 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012581 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12582 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583}
12584
12585/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012586 * "repeat()" function
12587 */
12588/*ARGSUSED*/
12589 static void
12590f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012591 typval_T *argvars;
12592 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012593{
12594 char_u *p;
12595 int n;
12596 int slen;
12597 int len;
12598 char_u *r;
12599 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012600 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012601
12602 n = get_tv_number(&argvars[1]);
12603 if (argvars[0].v_type == VAR_LIST)
12604 {
12605 l = list_alloc();
12606 if (l != NULL && argvars[0].vval.v_list != NULL)
12607 {
12608 l->lv_refcount = 1;
12609 while (n-- > 0)
12610 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12611 break;
12612 }
12613 rettv->v_type = VAR_LIST;
12614 rettv->vval.v_list = l;
12615 }
12616 else
12617 {
12618 p = get_tv_string(&argvars[0]);
12619 rettv->v_type = VAR_STRING;
12620 rettv->vval.v_string = NULL;
12621
12622 slen = (int)STRLEN(p);
12623 len = slen * n;
12624 if (len <= 0)
12625 return;
12626
12627 r = alloc(len + 1);
12628 if (r != NULL)
12629 {
12630 for (i = 0; i < n; i++)
12631 mch_memmove(r + i * slen, p, (size_t)slen);
12632 r[len] = NUL;
12633 }
12634
12635 rettv->vval.v_string = r;
12636 }
12637}
12638
12639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640 * "resolve()" function
12641 */
12642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012644 typval_T *argvars;
12645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646{
12647 char_u *p;
12648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012649 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650#ifdef FEAT_SHORTCUT
12651 {
12652 char_u *v = NULL;
12653
12654 v = mch_resolve_shortcut(p);
12655 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012656 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012658 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659 }
12660#else
12661# ifdef HAVE_READLINK
12662 {
12663 char_u buf[MAXPATHL + 1];
12664 char_u *cpy;
12665 int len;
12666 char_u *remain = NULL;
12667 char_u *q;
12668 int is_relative_to_current = FALSE;
12669 int has_trailing_pathsep = FALSE;
12670 int limit = 100;
12671
12672 p = vim_strsave(p);
12673
12674 if (p[0] == '.' && (vim_ispathsep(p[1])
12675 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12676 is_relative_to_current = TRUE;
12677
12678 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012679 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680 has_trailing_pathsep = TRUE;
12681
12682 q = getnextcomp(p);
12683 if (*q != NUL)
12684 {
12685 /* Separate the first path component in "p", and keep the
12686 * remainder (beginning with the path separator). */
12687 remain = vim_strsave(q - 1);
12688 q[-1] = NUL;
12689 }
12690
12691 for (;;)
12692 {
12693 for (;;)
12694 {
12695 len = readlink((char *)p, (char *)buf, MAXPATHL);
12696 if (len <= 0)
12697 break;
12698 buf[len] = NUL;
12699
12700 if (limit-- == 0)
12701 {
12702 vim_free(p);
12703 vim_free(remain);
12704 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012705 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706 goto fail;
12707 }
12708
12709 /* Ensure that the result will have a trailing path separator
12710 * if the argument has one. */
12711 if (remain == NULL && has_trailing_pathsep)
12712 add_pathsep(buf);
12713
12714 /* Separate the first path component in the link value and
12715 * concatenate the remainders. */
12716 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12717 if (*q != NUL)
12718 {
12719 if (remain == NULL)
12720 remain = vim_strsave(q - 1);
12721 else
12722 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012723 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724 if (cpy != NULL)
12725 {
12726 STRCAT(cpy, remain);
12727 vim_free(remain);
12728 remain = cpy;
12729 }
12730 }
12731 q[-1] = NUL;
12732 }
12733
12734 q = gettail(p);
12735 if (q > p && *q == NUL)
12736 {
12737 /* Ignore trailing path separator. */
12738 q[-1] = NUL;
12739 q = gettail(p);
12740 }
12741 if (q > p && !mch_isFullName(buf))
12742 {
12743 /* symlink is relative to directory of argument */
12744 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12745 if (cpy != NULL)
12746 {
12747 STRCPY(cpy, p);
12748 STRCPY(gettail(cpy), buf);
12749 vim_free(p);
12750 p = cpy;
12751 }
12752 }
12753 else
12754 {
12755 vim_free(p);
12756 p = vim_strsave(buf);
12757 }
12758 }
12759
12760 if (remain == NULL)
12761 break;
12762
12763 /* Append the first path component of "remain" to "p". */
12764 q = getnextcomp(remain + 1);
12765 len = q - remain - (*q != NUL);
12766 cpy = vim_strnsave(p, STRLEN(p) + len);
12767 if (cpy != NULL)
12768 {
12769 STRNCAT(cpy, remain, len);
12770 vim_free(p);
12771 p = cpy;
12772 }
12773 /* Shorten "remain". */
12774 if (*q != NUL)
12775 STRCPY(remain, q - 1);
12776 else
12777 {
12778 vim_free(remain);
12779 remain = NULL;
12780 }
12781 }
12782
12783 /* If the result is a relative path name, make it explicitly relative to
12784 * the current directory if and only if the argument had this form. */
12785 if (!vim_ispathsep(*p))
12786 {
12787 if (is_relative_to_current
12788 && *p != NUL
12789 && !(p[0] == '.'
12790 && (p[1] == NUL
12791 || vim_ispathsep(p[1])
12792 || (p[1] == '.'
12793 && (p[2] == NUL
12794 || vim_ispathsep(p[2]))))))
12795 {
12796 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012797 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798 if (cpy != NULL)
12799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800 vim_free(p);
12801 p = cpy;
12802 }
12803 }
12804 else if (!is_relative_to_current)
12805 {
12806 /* Strip leading "./". */
12807 q = p;
12808 while (q[0] == '.' && vim_ispathsep(q[1]))
12809 q += 2;
12810 if (q > p)
12811 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12812 }
12813 }
12814
12815 /* Ensure that the result will have no trailing path separator
12816 * if the argument had none. But keep "/" or "//". */
12817 if (!has_trailing_pathsep)
12818 {
12819 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012820 if (after_pathsep(p, q))
12821 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 }
12823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012824 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825 }
12826# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012827 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828# endif
12829#endif
12830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832
12833#ifdef HAVE_READLINK
12834fail:
12835#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012836 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837}
12838
12839/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012840 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841 */
12842 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012843f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012844 typval_T *argvars;
12845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846{
Bram Moolenaar33570922005-01-25 22:26:29 +000012847 list_T *l;
12848 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849
Bram Moolenaar0d660222005-01-07 21:51:51 +000012850 rettv->vval.v_number = 0;
12851 if (argvars[0].v_type != VAR_LIST)
12852 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012853 else if ((l = argvars[0].vval.v_list) != NULL
12854 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012855 {
12856 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012857 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012858 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012859 while (li != NULL)
12860 {
12861 ni = li->li_prev;
12862 list_append(l, li);
12863 li = ni;
12864 }
12865 rettv->vval.v_list = l;
12866 rettv->v_type = VAR_LIST;
12867 ++l->lv_refcount;
12868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869}
12870
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012871#define SP_NOMOVE 1 /* don't move cursor */
12872#define SP_REPEAT 2 /* repeat to find outer pair */
12873#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000012874#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012875
Bram Moolenaar33570922005-01-25 22:26:29 +000012876static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012877
12878/*
12879 * Get flags for a search function.
12880 * Possibly sets "p_ws".
12881 * Returns BACKWARD, FORWARD or zero (for an error).
12882 */
12883 static int
12884get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012885 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012886 int *flagsp;
12887{
12888 int dir = FORWARD;
12889 char_u *flags;
12890 char_u nbuf[NUMBUFLEN];
12891 int mask;
12892
12893 if (varp->v_type != VAR_UNKNOWN)
12894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012895 flags = get_tv_string_buf_chk(varp, nbuf);
12896 if (flags == NULL)
12897 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012898 while (*flags != NUL)
12899 {
12900 switch (*flags)
12901 {
12902 case 'b': dir = BACKWARD; break;
12903 case 'w': p_ws = TRUE; break;
12904 case 'W': p_ws = FALSE; break;
12905 default: mask = 0;
12906 if (flagsp != NULL)
12907 switch (*flags)
12908 {
12909 case 'n': mask = SP_NOMOVE; break;
12910 case 'r': mask = SP_REPEAT; break;
12911 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012912 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012913 }
12914 if (mask == 0)
12915 {
12916 EMSG2(_(e_invarg2), flags);
12917 dir = 0;
12918 }
12919 else
12920 *flagsp |= mask;
12921 }
12922 if (dir == 0)
12923 break;
12924 ++flags;
12925 }
12926 }
12927 return dir;
12928}
12929
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930/*
12931 * "search()" function
12932 */
12933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012934f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012935 typval_T *argvars;
12936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937{
12938 char_u *pat;
12939 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012940 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941 int save_p_ws = p_ws;
12942 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012943 int flags = 0;
12944
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012945 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012947 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012948 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12949 if (dir == 0)
12950 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012951 /*
12952 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
12953 * Check to make sure only those flags are set.
12954 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
12955 * flags cannot be set. Check for that condition also.
12956 */
12957 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
12958 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012960 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012961 goto theend;
12962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012964 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12966 SEARCH_KEEP, RE_SEARCH) != FAIL)
12967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012968 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012969 if (flags & SP_SETPCMARK)
12970 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971 curwin->w_cursor = pos;
12972 /* "/$" will put the cursor after the end of the line, may need to
12973 * correct that here */
12974 check_cursor();
12975 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012976
12977 /* If 'n' flag is used: restore cursor position. */
12978 if (flags & SP_NOMOVE)
12979 curwin->w_cursor = save_cursor;
12980theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981 p_ws = save_p_ws;
12982}
12983
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000012985 * "searchdecl()" function
12986 */
12987 static void
12988f_searchdecl(argvars, rettv)
12989 typval_T *argvars;
12990 typval_T *rettv;
12991{
12992 int locally = 1;
12993 int error = FALSE;
12994 char_u *name;
12995
12996 rettv->vval.v_number = 1; /* default: FAIL */
12997
12998 name = get_tv_string_chk(&argvars[0]);
12999 if (argvars[1].v_type != VAR_UNKNOWN)
13000 locally = get_tv_number_chk(&argvars[1], &error) == 0;
13001 if (!error && name != NULL)
13002 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
13003 locally, SEARCH_KEEP) == FAIL;
13004}
13005
13006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007 * "searchpair()" function
13008 */
13009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013010f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013011 typval_T *argvars;
13012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013{
13014 char_u *spat, *mpat, *epat;
13015 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017 int dir;
13018 int flags = 0;
13019 char_u nbuf1[NUMBUFLEN];
13020 char_u nbuf2[NUMBUFLEN];
13021 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013023 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013026 spat = get_tv_string_chk(&argvars[0]);
13027 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13028 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13029 if (spat == NULL || mpat == NULL || epat == NULL)
13030 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032 /* Handle the optional fourth argument: flags */
13033 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013034 if (dir == 0)
13035 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013036 /*
13037 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13038 */
13039 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13040 {
13041 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13042 goto theend;
13043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044
13045 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013046 if (argvars[3].v_type == VAR_UNKNOWN
13047 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048 skip = (char_u *)"";
13049 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013050 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13051 if (skip == NULL)
13052 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013054 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
13055
13056theend:
13057 p_ws = save_p_ws;
13058}
13059
13060/*
13061 * Search for a start/middle/end thing.
13062 * Used by searchpair(), see its documentation for the details.
13063 * Returns 0 or -1 for no match,
13064 */
13065 long
13066do_searchpair(spat, mpat, epat, dir, skip, flags)
13067 char_u *spat; /* start pattern */
13068 char_u *mpat; /* middle pattern */
13069 char_u *epat; /* end pattern */
13070 int dir; /* BACKWARD or FORWARD */
13071 char_u *skip; /* skip expression */
13072 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
13073{
13074 char_u *save_cpo;
13075 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13076 long retval = 0;
13077 pos_T pos;
13078 pos_T firstpos;
13079 pos_T foundpos;
13080 pos_T save_cursor;
13081 pos_T save_pos;
13082 int n;
13083 int r;
13084 int nest = 1;
13085 int err;
13086
13087 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13088 save_cpo = p_cpo;
13089 p_cpo = (char_u *)"";
13090
13091 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13092 * start/middle/end (pat3, for the top pair). */
13093 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13094 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13095 if (pat2 == NULL || pat3 == NULL)
13096 goto theend;
13097 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13098 if (*mpat == NUL)
13099 STRCPY(pat3, pat2);
13100 else
13101 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13102 spat, epat, mpat);
13103
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104 save_cursor = curwin->w_cursor;
13105 pos = curwin->w_cursor;
13106 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013107 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108 pat = pat3;
13109 for (;;)
13110 {
13111 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13112 SEARCH_KEEP, RE_SEARCH);
13113 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13114 /* didn't find it or found the first match again: FAIL */
13115 break;
13116
13117 if (firstpos.lnum == 0)
13118 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013119 if (equalpos(pos, foundpos))
13120 {
13121 /* Found the same position again. Can happen with a pattern that
13122 * has "\zs" at the end and searching backwards. Advance one
13123 * character and try again. */
13124 if (dir == BACKWARD)
13125 decl(&pos);
13126 else
13127 incl(&pos);
13128 }
13129 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130
13131 /* If the skip pattern matches, ignore this match. */
13132 if (*skip != NUL)
13133 {
13134 save_pos = curwin->w_cursor;
13135 curwin->w_cursor = pos;
13136 r = eval_to_bool(skip, &err, NULL, FALSE);
13137 curwin->w_cursor = save_pos;
13138 if (err)
13139 {
13140 /* Evaluating {skip} caused an error, break here. */
13141 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013142 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143 break;
13144 }
13145 if (r)
13146 continue;
13147 }
13148
13149 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13150 {
13151 /* Found end when searching backwards or start when searching
13152 * forward: nested pair. */
13153 ++nest;
13154 pat = pat2; /* nested, don't search for middle */
13155 }
13156 else
13157 {
13158 /* Found end when searching forward or start when searching
13159 * backward: end of (nested) pair; or found middle in outer pair. */
13160 if (--nest == 1)
13161 pat = pat3; /* outer level, search for middle */
13162 }
13163
13164 if (nest == 0)
13165 {
13166 /* Found the match: return matchcount or line number. */
13167 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013168 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013170 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013171 if (flags & SP_SETPCMARK)
13172 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173 curwin->w_cursor = pos;
13174 if (!(flags & SP_REPEAT))
13175 break;
13176 nest = 1; /* search for next unmatched */
13177 }
13178 }
13179
13180 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013181 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182 curwin->w_cursor = save_cursor;
13183
13184theend:
13185 vim_free(pat2);
13186 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013188
13189 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190}
13191
Bram Moolenaar0d660222005-01-07 21:51:51 +000013192/*ARGSUSED*/
13193 static void
13194f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013195 typval_T *argvars;
13196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013198#ifdef FEAT_CLIENTSERVER
13199 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013200 char_u *server = get_tv_string_chk(&argvars[0]);
13201 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202
Bram Moolenaar0d660222005-01-07 21:51:51 +000013203 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013204 if (server == NULL || reply == NULL)
13205 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013206 if (check_restricted() || check_secure())
13207 return;
13208# ifdef FEAT_X11
13209 if (check_connection() == FAIL)
13210 return;
13211# endif
13212
13213 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013215 EMSG(_("E258: Unable to send to client"));
13216 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013218 rettv->vval.v_number = 0;
13219#else
13220 rettv->vval.v_number = -1;
13221#endif
13222}
13223
13224/*ARGSUSED*/
13225 static void
13226f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013227 typval_T *argvars;
13228 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013229{
13230 char_u *r = NULL;
13231
13232#ifdef FEAT_CLIENTSERVER
13233# ifdef WIN32
13234 r = serverGetVimNames();
13235# else
13236 make_connection();
13237 if (X_DISPLAY != NULL)
13238 r = serverGetVimNames(X_DISPLAY);
13239# endif
13240#endif
13241 rettv->v_type = VAR_STRING;
13242 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243}
13244
13245/*
13246 * "setbufvar()" function
13247 */
13248/*ARGSUSED*/
13249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013250f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013251 typval_T *argvars;
13252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253{
13254 buf_T *buf;
13255#ifdef FEAT_AUTOCMD
13256 aco_save_T aco;
13257#else
13258 buf_T *save_curbuf;
13259#endif
13260 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013261 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 char_u nbuf[NUMBUFLEN];
13263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013264 rettv->vval.v_number = 0;
13265
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266 if (check_restricted() || check_secure())
13267 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013268 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13269 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013270 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271 varp = &argvars[2];
13272
13273 if (buf != NULL && varname != NULL && varp != NULL)
13274 {
13275 /* set curbuf to be our buf, temporarily */
13276#ifdef FEAT_AUTOCMD
13277 aucmd_prepbuf(&aco, buf);
13278#else
13279 save_curbuf = curbuf;
13280 curbuf = buf;
13281#endif
13282
13283 if (*varname == '&')
13284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013285 long numval;
13286 char_u *strval;
13287 int error = FALSE;
13288
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013290 numval = get_tv_number_chk(varp, &error);
13291 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013292 if (!error && strval != NULL)
13293 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294 }
13295 else
13296 {
13297 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13298 if (bufvarname != NULL)
13299 {
13300 STRCPY(bufvarname, "b:");
13301 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013302 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303 vim_free(bufvarname);
13304 }
13305 }
13306
13307 /* reset notion of buffer */
13308#ifdef FEAT_AUTOCMD
13309 aucmd_restbuf(&aco);
13310#else
13311 curbuf = save_curbuf;
13312#endif
13313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314}
13315
13316/*
13317 * "setcmdpos()" function
13318 */
13319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013320f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013321 typval_T *argvars;
13322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013324 int pos = (int)get_tv_number(&argvars[0]) - 1;
13325
13326 if (pos >= 0)
13327 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013328}
13329
13330/*
13331 * "setline()" function
13332 */
13333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013335 typval_T *argvars;
13336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337{
13338 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013339 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013340 list_T *l = NULL;
13341 listitem_T *li = NULL;
13342 long added = 0;
13343 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013345 lnum = get_tv_lnum(&argvars[0]);
13346 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013348 l = argvars[1].vval.v_list;
13349 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013351 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013352 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013353
13354 rettv->vval.v_number = 0; /* OK */
13355 for (;;)
13356 {
13357 if (l != NULL)
13358 {
13359 /* list argument, get next string */
13360 if (li == NULL)
13361 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013362 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013363 li = li->li_next;
13364 }
13365
13366 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013367 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013368 break;
13369 if (lnum <= curbuf->b_ml.ml_line_count)
13370 {
13371 /* existing line, replace it */
13372 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13373 {
13374 changed_bytes(lnum, 0);
13375 check_cursor_col();
13376 rettv->vval.v_number = 0; /* OK */
13377 }
13378 }
13379 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13380 {
13381 /* lnum is one past the last line, append the line */
13382 ++added;
13383 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13384 rettv->vval.v_number = 0; /* OK */
13385 }
13386
13387 if (l == NULL) /* only one string argument */
13388 break;
13389 ++lnum;
13390 }
13391
13392 if (added > 0)
13393 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394}
13395
13396/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013397 * "setqflist()" function
13398 */
13399/*ARGSUSED*/
13400 static void
13401f_setqflist(argvars, rettv)
13402 typval_T *argvars;
13403 typval_T *rettv;
13404{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013405 char_u *act;
13406 int action = ' ';
13407
Bram Moolenaar2641f772005-03-25 21:58:17 +000013408 rettv->vval.v_number = -1;
13409
13410#ifdef FEAT_QUICKFIX
13411 if (argvars[0].v_type != VAR_LIST)
13412 EMSG(_(e_listreq));
13413 else
13414 {
13415 list_T *l = argvars[0].vval.v_list;
13416
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013417 if (argvars[1].v_type == VAR_STRING)
13418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013419 act = get_tv_string_chk(&argvars[1]);
13420 if (act == NULL)
13421 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013422 if (*act == 'a' || *act == 'r')
13423 action = *act;
13424 }
13425
13426 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013427 rettv->vval.v_number = 0;
13428 }
13429#endif
13430}
13431
13432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433 * "setreg()" function
13434 */
13435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013436f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013437 typval_T *argvars;
13438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439{
13440 int regname;
13441 char_u *strregname;
13442 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013443 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444 int append;
13445 char_u yank_type;
13446 long block_len;
13447
13448 block_len = -1;
13449 yank_type = MAUTO;
13450 append = FALSE;
13451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013452 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013453 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013455 if (strregname == NULL)
13456 return; /* type error; errmsg already given */
13457 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 if (regname == 0 || regname == '@')
13459 regname = '"';
13460 else if (regname == '=')
13461 return;
13462
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013463 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013465 stropt = get_tv_string_chk(&argvars[2]);
13466 if (stropt == NULL)
13467 return; /* type error */
13468 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469 switch (*stropt)
13470 {
13471 case 'a': case 'A': /* append */
13472 append = TRUE;
13473 break;
13474 case 'v': case 'c': /* character-wise selection */
13475 yank_type = MCHAR;
13476 break;
13477 case 'V': case 'l': /* line-wise selection */
13478 yank_type = MLINE;
13479 break;
13480#ifdef FEAT_VISUAL
13481 case 'b': case Ctrl_V: /* block-wise selection */
13482 yank_type = MBLOCK;
13483 if (VIM_ISDIGIT(stropt[1]))
13484 {
13485 ++stropt;
13486 block_len = getdigits(&stropt) - 1;
13487 --stropt;
13488 }
13489 break;
13490#endif
13491 }
13492 }
13493
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013494 strval = get_tv_string_chk(&argvars[1]);
13495 if (strval != NULL)
13496 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013498 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499}
13500
13501
13502/*
13503 * "setwinvar(expr)" function
13504 */
13505/*ARGSUSED*/
13506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013507f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013508 typval_T *argvars;
13509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510{
13511 win_T *win;
13512#ifdef FEAT_WINDOWS
13513 win_T *save_curwin;
13514#endif
13515 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013516 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517 char_u nbuf[NUMBUFLEN];
13518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013519 rettv->vval.v_number = 0;
13520
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521 if (check_restricted() || check_secure())
13522 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013524 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525 varp = &argvars[2];
13526
13527 if (win != NULL && varname != NULL && varp != NULL)
13528 {
13529#ifdef FEAT_WINDOWS
13530 /* set curwin to be our win, temporarily */
13531 save_curwin = curwin;
13532 curwin = win;
13533 curbuf = curwin->w_buffer;
13534#endif
13535
13536 if (*varname == '&')
13537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013538 long numval;
13539 char_u *strval;
13540 int error = FALSE;
13541
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013543 numval = get_tv_number_chk(varp, &error);
13544 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013545 if (!error && strval != NULL)
13546 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 }
13548 else
13549 {
13550 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13551 if (winvarname != NULL)
13552 {
13553 STRCPY(winvarname, "w:");
13554 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013555 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556 vim_free(winvarname);
13557 }
13558 }
13559
13560#ifdef FEAT_WINDOWS
13561 /* Restore current window, if it's still valid (autocomands can make
13562 * it invalid). */
13563 if (win_valid(save_curwin))
13564 {
13565 curwin = save_curwin;
13566 curbuf = curwin->w_buffer;
13567 }
13568#endif
13569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570}
13571
13572/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013573 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574 */
13575 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013576f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013577 typval_T *argvars;
13578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013580 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581
Bram Moolenaar0d660222005-01-07 21:51:51 +000013582 p = get_tv_string(&argvars[0]);
13583 rettv->vval.v_string = vim_strsave(p);
13584 simplify_filename(rettv->vval.v_string); /* simplify in place */
13585 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586}
13587
Bram Moolenaar0d660222005-01-07 21:51:51 +000013588static int
13589#ifdef __BORLANDC__
13590 _RTLENTRYF
13591#endif
13592 item_compare __ARGS((const void *s1, const void *s2));
13593static int
13594#ifdef __BORLANDC__
13595 _RTLENTRYF
13596#endif
13597 item_compare2 __ARGS((const void *s1, const void *s2));
13598
13599static int item_compare_ic;
13600static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013601static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013602#define ITEM_COMPARE_FAIL 999
13603
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013605 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013607 static int
13608#ifdef __BORLANDC__
13609_RTLENTRYF
13610#endif
13611item_compare(s1, s2)
13612 const void *s1;
13613 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013615 char_u *p1, *p2;
13616 char_u *tofree1, *tofree2;
13617 int res;
13618 char_u numbuf1[NUMBUFLEN];
13619 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620
Bram Moolenaar33570922005-01-25 22:26:29 +000013621 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13622 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013623 if (item_compare_ic)
13624 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013626 res = STRCMP(p1, p2);
13627 vim_free(tofree1);
13628 vim_free(tofree2);
13629 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630}
13631
13632 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013633#ifdef __BORLANDC__
13634_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013636item_compare2(s1, s2)
13637 const void *s1;
13638 const void *s2;
13639{
13640 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013641 typval_T rettv;
13642 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013643 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013645 /* shortcut after failure in previous call; compare all items equal */
13646 if (item_compare_func_err)
13647 return 0;
13648
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013649 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13650 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013651 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13652 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013653
13654 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13655 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013656 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013657 clear_tv(&argv[0]);
13658 clear_tv(&argv[1]);
13659
13660 if (res == FAIL)
13661 res = ITEM_COMPARE_FAIL;
13662 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013663 /* return value has wrong type */
13664 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13665 if (item_compare_func_err)
13666 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013667 clear_tv(&rettv);
13668 return res;
13669}
13670
13671/*
13672 * "sort({list})" function
13673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013675f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013676 typval_T *argvars;
13677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678{
Bram Moolenaar33570922005-01-25 22:26:29 +000013679 list_T *l;
13680 listitem_T *li;
13681 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013682 long len;
13683 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684
Bram Moolenaar0d660222005-01-07 21:51:51 +000013685 rettv->vval.v_number = 0;
13686 if (argvars[0].v_type != VAR_LIST)
13687 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688 else
13689 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013690 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013691 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013692 return;
13693 rettv->vval.v_list = l;
13694 rettv->v_type = VAR_LIST;
13695 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696
Bram Moolenaar0d660222005-01-07 21:51:51 +000013697 len = list_len(l);
13698 if (len <= 1)
13699 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700
Bram Moolenaar0d660222005-01-07 21:51:51 +000013701 item_compare_ic = FALSE;
13702 item_compare_func = NULL;
13703 if (argvars[1].v_type != VAR_UNKNOWN)
13704 {
13705 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013706 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013707 else
13708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013709 int error = FALSE;
13710
13711 i = get_tv_number_chk(&argvars[1], &error);
13712 if (error)
13713 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013714 if (i == 1)
13715 item_compare_ic = TRUE;
13716 else
13717 item_compare_func = get_tv_string(&argvars[1]);
13718 }
13719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720
Bram Moolenaar0d660222005-01-07 21:51:51 +000013721 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013722 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013723 if (ptrs == NULL)
13724 return;
13725 i = 0;
13726 for (li = l->lv_first; li != NULL; li = li->li_next)
13727 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013729 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013730 /* test the compare function */
13731 if (item_compare_func != NULL
13732 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13733 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013734 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013736 {
13737 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013738 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013739 item_compare_func == NULL ? item_compare : item_compare2);
13740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013741 if (!item_compare_func_err)
13742 {
13743 /* Clear the List and append the items in the sorted order. */
13744 l->lv_first = l->lv_last = NULL;
13745 l->lv_len = 0;
13746 for (i = 0; i < len; ++i)
13747 list_append(l, ptrs[i]);
13748 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013749 }
13750
13751 vim_free(ptrs);
13752 }
13753}
13754
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013755/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013756 * "soundfold({word})" function
13757 */
13758 static void
13759f_soundfold(argvars, rettv)
13760 typval_T *argvars;
13761 typval_T *rettv;
13762{
13763 char_u *s;
13764
13765 rettv->v_type = VAR_STRING;
13766 s = get_tv_string(&argvars[0]);
13767#ifdef FEAT_SYN_HL
13768 rettv->vval.v_string = eval_soundfold(s);
13769#else
13770 rettv->vval.v_string = vim_strsave(s);
13771#endif
13772}
13773
13774/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013775 * "spellbadword()" function
13776 */
13777/* ARGSUSED */
13778 static void
13779f_spellbadword(argvars, rettv)
13780 typval_T *argvars;
13781 typval_T *rettv;
13782{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013783 int len;
13784
13785 rettv->vval.v_string = NULL;
13786 rettv->v_type = VAR_STRING;
13787
13788#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +000013789 /* Find the start and length of the badly spelled word. */
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000013790 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +000013791 if (len != 0)
13792 rettv->vval.v_string = vim_strnsave(ml_get_cursor(), len);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013793#endif
13794}
13795
13796/*
13797 * "spellsuggest()" function
13798 */
13799 static void
13800f_spellsuggest(argvars, rettv)
13801 typval_T *argvars;
13802 typval_T *rettv;
13803{
13804 char_u *str;
13805 int maxcount;
13806 garray_T ga;
13807 list_T *l;
13808 listitem_T *li;
13809 int i;
13810
13811 l = list_alloc();
13812 if (l == NULL)
13813 return;
13814 rettv->v_type = VAR_LIST;
13815 rettv->vval.v_list = l;
13816 ++l->lv_refcount;
13817
13818#ifdef FEAT_SYN_HL
13819 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13820 {
13821 str = get_tv_string(&argvars[0]);
13822 if (argvars[1].v_type != VAR_UNKNOWN)
13823 {
13824 maxcount = get_tv_number(&argvars[1]);
13825 if (maxcount <= 0)
13826 return;
13827 }
13828 else
13829 maxcount = 25;
13830
Bram Moolenaar8c45cdf2005-08-11 20:11:38 +000013831 spell_suggest_list(&ga, str, maxcount, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013832
13833 for (i = 0; i < ga.ga_len; ++i)
13834 {
13835 str = ((char_u **)ga.ga_data)[i];
13836
13837 li = listitem_alloc();
13838 if (li == NULL)
13839 vim_free(str);
13840 else
13841 {
13842 li->li_tv.v_type = VAR_STRING;
13843 li->li_tv.v_lock = 0;
13844 li->li_tv.vval.v_string = str;
13845 list_append(l, li);
13846 }
13847 }
13848 ga_clear(&ga);
13849 }
13850#endif
13851}
13852
Bram Moolenaar0d660222005-01-07 21:51:51 +000013853 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013854f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013855 typval_T *argvars;
13856 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013857{
13858 char_u *str;
13859 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013860 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013861 regmatch_T regmatch;
13862 char_u patbuf[NUMBUFLEN];
13863 char_u *save_cpo;
13864 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013865 listitem_T *ni;
13866 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013867 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013868 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013869 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013870
13871 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13872 save_cpo = p_cpo;
13873 p_cpo = (char_u *)"";
13874
13875 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013876 if (argvars[1].v_type != VAR_UNKNOWN)
13877 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013878 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13879 if (pat == NULL)
13880 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013881 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013882 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013883 }
13884 if (pat == NULL || *pat == NUL)
13885 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013886
13887 l = list_alloc();
13888 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013890 rettv->v_type = VAR_LIST;
13891 rettv->vval.v_list = l;
13892 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013893 if (typeerr)
13894 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895
Bram Moolenaar0d660222005-01-07 21:51:51 +000013896 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13897 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013899 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013900 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013901 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013902 if (*str == NUL)
13903 match = FALSE; /* empty item at the end */
13904 else
13905 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013906 if (match)
13907 end = regmatch.startp[0];
13908 else
13909 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013910 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13911 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013912 {
13913 ni = listitem_alloc();
13914 if (ni == NULL)
13915 break;
13916 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013917 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013918 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13919 list_append(l, ni);
13920 }
13921 if (!match)
13922 break;
13923 /* Advance to just after the match. */
13924 if (regmatch.endp[0] > str)
13925 col = 0;
13926 else
13927 {
13928 /* Don't get stuck at the same match. */
13929#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013930 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013931#else
13932 col = 1;
13933#endif
13934 }
13935 str = regmatch.endp[0];
13936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937
Bram Moolenaar0d660222005-01-07 21:51:51 +000013938 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940
Bram Moolenaar0d660222005-01-07 21:51:51 +000013941 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942}
13943
13944#ifdef HAVE_STRFTIME
13945/*
13946 * "strftime({format}[, {time}])" function
13947 */
13948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013949f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013950 typval_T *argvars;
13951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952{
13953 char_u result_buf[256];
13954 struct tm *curtime;
13955 time_t seconds;
13956 char_u *p;
13957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013958 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013960 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013961 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962 seconds = time(NULL);
13963 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013964 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 curtime = localtime(&seconds);
13966 /* MSVC returns NULL for an invalid value of seconds. */
13967 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013968 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 else
13970 {
13971# ifdef FEAT_MBYTE
13972 vimconv_T conv;
13973 char_u *enc;
13974
13975 conv.vc_type = CONV_NONE;
13976 enc = enc_locale();
13977 convert_setup(&conv, p_enc, enc);
13978 if (conv.vc_type != CONV_NONE)
13979 p = string_convert(&conv, p, NULL);
13980# endif
13981 if (p != NULL)
13982 (void)strftime((char *)result_buf, sizeof(result_buf),
13983 (char *)p, curtime);
13984 else
13985 result_buf[0] = NUL;
13986
13987# ifdef FEAT_MBYTE
13988 if (conv.vc_type != CONV_NONE)
13989 vim_free(p);
13990 convert_setup(&conv, enc, p_enc);
13991 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013992 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993 else
13994# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013995 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996
13997# ifdef FEAT_MBYTE
13998 /* Release conversion descriptors */
13999 convert_setup(&conv, NULL, NULL);
14000 vim_free(enc);
14001# endif
14002 }
14003}
14004#endif
14005
14006/*
14007 * "stridx()" function
14008 */
14009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014010f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014011 typval_T *argvars;
14012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013{
14014 char_u buf[NUMBUFLEN];
14015 char_u *needle;
14016 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014017 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014019 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014021 needle = get_tv_string_chk(&argvars[1]);
14022 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014023 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014024 if (needle == NULL || haystack == NULL)
14025 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026
Bram Moolenaar33570922005-01-25 22:26:29 +000014027 if (argvars[2].v_type != VAR_UNKNOWN)
14028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014029 int error = FALSE;
14030
14031 start_idx = get_tv_number_chk(&argvars[2], &error);
14032 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014033 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014034 if (start_idx >= 0)
14035 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014036 }
14037
14038 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14039 if (pos != NULL)
14040 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041}
14042
14043/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014044 * "string()" function
14045 */
14046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014047f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014048 typval_T *argvars;
14049 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014050{
14051 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014052 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014054 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014055 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014056 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014057 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058}
14059
14060/*
14061 * "strlen()" function
14062 */
14063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014064f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014065 typval_T *argvars;
14066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014068 rettv->vval.v_number = (varnumber_T)(STRLEN(
14069 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070}
14071
14072/*
14073 * "strpart()" function
14074 */
14075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014076f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014077 typval_T *argvars;
14078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079{
14080 char_u *p;
14081 int n;
14082 int len;
14083 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014084 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014086 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087 slen = (int)STRLEN(p);
14088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014089 n = get_tv_number_chk(&argvars[1], &error);
14090 if (error)
14091 len = 0;
14092 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014093 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094 else
14095 len = slen - n; /* default len: all bytes that are available. */
14096
14097 /*
14098 * Only return the overlap between the specified part and the actual
14099 * string.
14100 */
14101 if (n < 0)
14102 {
14103 len += n;
14104 n = 0;
14105 }
14106 else if (n > slen)
14107 n = slen;
14108 if (len < 0)
14109 len = 0;
14110 else if (n + len > slen)
14111 len = slen - n;
14112
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014113 rettv->v_type = VAR_STRING;
14114 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115}
14116
14117/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014118 * "strridx()" function
14119 */
14120 static void
14121f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014122 typval_T *argvars;
14123 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014124{
14125 char_u buf[NUMBUFLEN];
14126 char_u *needle;
14127 char_u *haystack;
14128 char_u *rest;
14129 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014130 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014132 needle = get_tv_string_chk(&argvars[1]);
14133 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014134 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014135
14136 rettv->vval.v_number = -1;
14137 if (needle == NULL || haystack == NULL)
14138 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014139 if (argvars[2].v_type != VAR_UNKNOWN)
14140 {
14141 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014142 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014143 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014144 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014145 }
14146 else
14147 end_idx = haystack_len;
14148
Bram Moolenaar0d660222005-01-07 21:51:51 +000014149 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014150 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014151 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014152 lastmatch = haystack + end_idx;
14153 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014154 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014155 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014156 for (rest = haystack; *rest != '\0'; ++rest)
14157 {
14158 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014159 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014160 break;
14161 lastmatch = rest;
14162 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014163 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014164
14165 if (lastmatch == NULL)
14166 rettv->vval.v_number = -1;
14167 else
14168 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14169}
14170
14171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172 * "strtrans()" function
14173 */
14174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014175f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014176 typval_T *argvars;
14177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014179 rettv->v_type = VAR_STRING;
14180 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181}
14182
14183/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014184 * "submatch()" function
14185 */
14186 static void
14187f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014188 typval_T *argvars;
14189 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014190{
14191 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014192 rettv->vval.v_string =
14193 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014194}
14195
14196/*
14197 * "substitute()" function
14198 */
14199 static void
14200f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014201 typval_T *argvars;
14202 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014203{
14204 char_u patbuf[NUMBUFLEN];
14205 char_u subbuf[NUMBUFLEN];
14206 char_u flagsbuf[NUMBUFLEN];
14207
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208 char_u *str = get_tv_string_chk(&argvars[0]);
14209 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14210 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14211 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14212
Bram Moolenaar0d660222005-01-07 21:51:51 +000014213 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014214 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14215 rettv->vval.v_string = NULL;
14216 else
14217 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014218}
14219
14220/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014221 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222 */
14223/*ARGSUSED*/
14224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014225f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014226 typval_T *argvars;
14227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228{
14229 int id = 0;
14230#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014231 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232 long col;
14233 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014234 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014236 lnum = get_tv_lnum(argvars); /* -1 on type error */
14237 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14238 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014240 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014241 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014242 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243#endif
14244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014245 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246}
14247
14248/*
14249 * "synIDattr(id, what [, mode])" function
14250 */
14251/*ARGSUSED*/
14252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014253f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014254 typval_T *argvars;
14255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256{
14257 char_u *p = NULL;
14258#ifdef FEAT_SYN_HL
14259 int id;
14260 char_u *what;
14261 char_u *mode;
14262 char_u modebuf[NUMBUFLEN];
14263 int modec;
14264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014265 id = get_tv_number(&argvars[0]);
14266 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014267 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014269 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270 modec = TOLOWER_ASC(mode[0]);
14271 if (modec != 't' && modec != 'c'
14272#ifdef FEAT_GUI
14273 && modec != 'g'
14274#endif
14275 )
14276 modec = 0; /* replace invalid with current */
14277 }
14278 else
14279 {
14280#ifdef FEAT_GUI
14281 if (gui.in_use)
14282 modec = 'g';
14283 else
14284#endif
14285 if (t_colors > 1)
14286 modec = 'c';
14287 else
14288 modec = 't';
14289 }
14290
14291
14292 switch (TOLOWER_ASC(what[0]))
14293 {
14294 case 'b':
14295 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14296 p = highlight_color(id, what, modec);
14297 else /* bold */
14298 p = highlight_has_attr(id, HL_BOLD, modec);
14299 break;
14300
14301 case 'f': /* fg[#] */
14302 p = highlight_color(id, what, modec);
14303 break;
14304
14305 case 'i':
14306 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14307 p = highlight_has_attr(id, HL_INVERSE, modec);
14308 else /* italic */
14309 p = highlight_has_attr(id, HL_ITALIC, modec);
14310 break;
14311
14312 case 'n': /* name */
14313 p = get_highlight_name(NULL, id - 1);
14314 break;
14315
14316 case 'r': /* reverse */
14317 p = highlight_has_attr(id, HL_INVERSE, modec);
14318 break;
14319
14320 case 's': /* standout */
14321 p = highlight_has_attr(id, HL_STANDOUT, modec);
14322 break;
14323
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014324 case 'u':
14325 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14326 /* underline */
14327 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14328 else
14329 /* undercurl */
14330 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 break;
14332 }
14333
14334 if (p != NULL)
14335 p = vim_strsave(p);
14336#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014337 rettv->v_type = VAR_STRING;
14338 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339}
14340
14341/*
14342 * "synIDtrans(id)" function
14343 */
14344/*ARGSUSED*/
14345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014346f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014347 typval_T *argvars;
14348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349{
14350 int id;
14351
14352#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014353 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354
14355 if (id > 0)
14356 id = syn_get_final_id(id);
14357 else
14358#endif
14359 id = 0;
14360
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014361 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362}
14363
14364/*
14365 * "system()" function
14366 */
14367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014368f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014369 typval_T *argvars;
14370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014372 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014374 char_u *infile = NULL;
14375 char_u buf[NUMBUFLEN];
14376 int err = FALSE;
14377 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014379 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014380 {
14381 /*
14382 * Write the string to a temp file, to be used for input of the shell
14383 * command.
14384 */
14385 if ((infile = vim_tempname('i')) == NULL)
14386 {
14387 EMSG(_(e_notmp));
14388 return;
14389 }
14390
14391 fd = mch_fopen((char *)infile, WRITEBIN);
14392 if (fd == NULL)
14393 {
14394 EMSG2(_(e_notopen), infile);
14395 goto done;
14396 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014397 p = get_tv_string_buf_chk(&argvars[1], buf);
14398 if (p == NULL)
14399 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014400 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14401 err = TRUE;
14402 if (fclose(fd) != 0)
14403 err = TRUE;
14404 if (err)
14405 {
14406 EMSG(_("E677: Error writing temp file"));
14407 goto done;
14408 }
14409 }
14410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014411 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014412
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413#ifdef USE_CR
14414 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014415 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416 {
14417 char_u *s;
14418
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014419 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420 {
14421 if (*s == CAR)
14422 *s = NL;
14423 }
14424 }
14425#else
14426# ifdef USE_CRNL
14427 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014428 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 {
14430 char_u *s, *d;
14431
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014432 d = res;
14433 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 {
14435 if (s[0] == CAR && s[1] == NL)
14436 ++s;
14437 *d++ = *s;
14438 }
14439 *d = NUL;
14440 }
14441# endif
14442#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014443
14444done:
14445 if (infile != NULL)
14446 {
14447 mch_remove(infile);
14448 vim_free(infile);
14449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014450 rettv->v_type = VAR_STRING;
14451 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452}
14453
14454/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014455 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014456 */
14457 static void
14458f_taglist(argvars, rettv)
14459 typval_T *argvars;
14460 typval_T *rettv;
14461{
14462 char_u *tag_pattern;
14463 list_T *l;
14464
14465 tag_pattern = get_tv_string(&argvars[0]);
14466
14467 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014468 if (*tag_pattern == NUL)
14469 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014470
14471 l = list_alloc();
14472 if (l != NULL)
14473 {
14474 if (get_tags(l, tag_pattern) != FAIL)
14475 {
14476 rettv->vval.v_list = l;
14477 rettv->v_type = VAR_LIST;
14478 ++l->lv_refcount;
14479 }
14480 else
14481 list_free(l);
14482 }
14483}
14484
14485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486 * "tempname()" function
14487 */
14488/*ARGSUSED*/
14489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014490f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014491 typval_T *argvars;
14492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493{
14494 static int x = 'A';
14495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014496 rettv->v_type = VAR_STRING;
14497 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498
14499 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14500 * names. Skip 'I' and 'O', they are used for shell redirection. */
14501 do
14502 {
14503 if (x == 'Z')
14504 x = '0';
14505 else if (x == '9')
14506 x = 'A';
14507 else
14508 {
14509#ifdef EBCDIC
14510 if (x == 'I')
14511 x = 'J';
14512 else if (x == 'R')
14513 x = 'S';
14514 else
14515#endif
14516 ++x;
14517 }
14518 } while (x == 'I' || x == 'O');
14519}
14520
14521/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000014522 * "test(list)" function: Just checking the walls...
14523 */
14524/*ARGSUSED*/
14525 static void
14526f_test(argvars, rettv)
14527 typval_T *argvars;
14528 typval_T *rettv;
14529{
14530 /* Used for unit testing. Change the code below to your liking. */
14531#if 0
14532 listitem_T *li;
14533 list_T *l;
14534 char_u *bad, *good;
14535
14536 if (argvars[0].v_type != VAR_LIST)
14537 return;
14538 l = argvars[0].vval.v_list;
14539 if (l == NULL)
14540 return;
14541 li = l->lv_first;
14542 if (li == NULL)
14543 return;
14544 bad = get_tv_string(&li->li_tv);
14545 li = li->li_next;
14546 if (li == NULL)
14547 return;
14548 good = get_tv_string(&li->li_tv);
14549 rettv->vval.v_number = test_edit_score(bad, good);
14550#endif
14551}
14552
14553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554 * "tolower(string)" function
14555 */
14556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014557f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014558 typval_T *argvars;
14559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560{
14561 char_u *p;
14562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014563 p = vim_strsave(get_tv_string(&argvars[0]));
14564 rettv->v_type = VAR_STRING;
14565 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566
14567 if (p != NULL)
14568 while (*p != NUL)
14569 {
14570#ifdef FEAT_MBYTE
14571 int l;
14572
14573 if (enc_utf8)
14574 {
14575 int c, lc;
14576
14577 c = utf_ptr2char(p);
14578 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014579 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 /* TODO: reallocate string when byte count changes. */
14581 if (utf_char2len(lc) == l)
14582 utf_char2bytes(lc, p);
14583 p += l;
14584 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014585 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586 p += l; /* skip multi-byte character */
14587 else
14588#endif
14589 {
14590 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14591 ++p;
14592 }
14593 }
14594}
14595
14596/*
14597 * "toupper(string)" function
14598 */
14599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014600f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014601 typval_T *argvars;
14602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014604 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014605 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606}
14607
14608/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014609 * "tr(string, fromstr, tostr)" function
14610 */
14611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014612f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014613 typval_T *argvars;
14614 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014615{
14616 char_u *instr;
14617 char_u *fromstr;
14618 char_u *tostr;
14619 char_u *p;
14620#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014621 int inlen;
14622 int fromlen;
14623 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014624 int idx;
14625 char_u *cpstr;
14626 int cplen;
14627 int first = TRUE;
14628#endif
14629 char_u buf[NUMBUFLEN];
14630 char_u buf2[NUMBUFLEN];
14631 garray_T ga;
14632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014633 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014634 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14635 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014636
14637 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014638 rettv->v_type = VAR_STRING;
14639 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014640 if (fromstr == NULL || tostr == NULL)
14641 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014642 ga_init2(&ga, (int)sizeof(char), 80);
14643
14644#ifdef FEAT_MBYTE
14645 if (!has_mbyte)
14646#endif
14647 /* not multi-byte: fromstr and tostr must be the same length */
14648 if (STRLEN(fromstr) != STRLEN(tostr))
14649 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014650#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014651error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014652#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014653 EMSG2(_(e_invarg2), fromstr);
14654 ga_clear(&ga);
14655 return;
14656 }
14657
14658 /* fromstr and tostr have to contain the same number of chars */
14659 while (*instr != NUL)
14660 {
14661#ifdef FEAT_MBYTE
14662 if (has_mbyte)
14663 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014664 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014665 cpstr = instr;
14666 cplen = inlen;
14667 idx = 0;
14668 for (p = fromstr; *p != NUL; p += fromlen)
14669 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014670 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014671 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14672 {
14673 for (p = tostr; *p != NUL; p += tolen)
14674 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014675 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014676 if (idx-- == 0)
14677 {
14678 cplen = tolen;
14679 cpstr = p;
14680 break;
14681 }
14682 }
14683 if (*p == NUL) /* tostr is shorter than fromstr */
14684 goto error;
14685 break;
14686 }
14687 ++idx;
14688 }
14689
14690 if (first && cpstr == instr)
14691 {
14692 /* Check that fromstr and tostr have the same number of
14693 * (multi-byte) characters. Done only once when a character
14694 * of instr doesn't appear in fromstr. */
14695 first = FALSE;
14696 for (p = tostr; *p != NUL; p += tolen)
14697 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014698 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014699 --idx;
14700 }
14701 if (idx != 0)
14702 goto error;
14703 }
14704
14705 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014706 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014707 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014708
14709 instr += inlen;
14710 }
14711 else
14712#endif
14713 {
14714 /* When not using multi-byte chars we can do it faster. */
14715 p = vim_strchr(fromstr, *instr);
14716 if (p != NULL)
14717 ga_append(&ga, tostr[p - fromstr]);
14718 else
14719 ga_append(&ga, *instr);
14720 ++instr;
14721 }
14722 }
14723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014724 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014725}
14726
14727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728 * "type(expr)" function
14729 */
14730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014731f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014732 typval_T *argvars;
14733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014735 int n;
14736
14737 switch (argvars[0].v_type)
14738 {
14739 case VAR_NUMBER: n = 0; break;
14740 case VAR_STRING: n = 1; break;
14741 case VAR_FUNC: n = 2; break;
14742 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014743 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014744 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14745 }
14746 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747}
14748
14749/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014750 * "values(dict)" function
14751 */
14752 static void
14753f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014754 typval_T *argvars;
14755 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014756{
14757 dict_list(argvars, rettv, 1);
14758}
14759
14760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 * "virtcol(string)" function
14762 */
14763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014764f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014765 typval_T *argvars;
14766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767{
14768 colnr_T vcol = 0;
14769 pos_T *fp;
14770
14771 fp = var2fpos(&argvars[0], FALSE);
14772 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14773 {
14774 getvvcol(curwin, fp, NULL, NULL, &vcol);
14775 ++vcol;
14776 }
14777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014778 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779}
14780
14781/*
14782 * "visualmode()" function
14783 */
14784/*ARGSUSED*/
14785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014786f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014787 typval_T *argvars;
14788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789{
14790#ifdef FEAT_VISUAL
14791 char_u str[2];
14792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014793 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794 str[0] = curbuf->b_visual_mode_eval;
14795 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014796 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014797
14798 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014799 if ((argvars[0].v_type == VAR_NUMBER
14800 && argvars[0].vval.v_number != 0)
14801 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014802 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803 curbuf->b_visual_mode_eval = NUL;
14804#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014805 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806#endif
14807}
14808
14809/*
14810 * "winbufnr(nr)" function
14811 */
14812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014813f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014814 typval_T *argvars;
14815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816{
14817 win_T *wp;
14818
14819 wp = find_win_by_nr(&argvars[0]);
14820 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014821 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014823 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824}
14825
14826/*
14827 * "wincol()" function
14828 */
14829/*ARGSUSED*/
14830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014831f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014832 typval_T *argvars;
14833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834{
14835 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014836 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837}
14838
14839/*
14840 * "winheight(nr)" function
14841 */
14842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014843f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014844 typval_T *argvars;
14845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014846{
14847 win_T *wp;
14848
14849 wp = find_win_by_nr(&argvars[0]);
14850 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014851 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014853 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854}
14855
14856/*
14857 * "winline()" function
14858 */
14859/*ARGSUSED*/
14860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014861f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014862 typval_T *argvars;
14863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864{
14865 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014866 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867}
14868
14869/*
14870 * "winnr()" function
14871 */
14872/* ARGSUSED */
14873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014874f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014875 typval_T *argvars;
14876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014877{
14878 int nr = 1;
14879#ifdef FEAT_WINDOWS
14880 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014881 win_T *twin = curwin;
14882 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014884 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014885 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014886 arg = get_tv_string_chk(&argvars[0]);
14887 if (arg == NULL)
14888 nr = 0; /* type error; errmsg already given */
14889 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014890 twin = lastwin;
14891 else if (STRCMP(arg, "#") == 0)
14892 {
14893 twin = prevwin;
14894 if (prevwin == NULL)
14895 nr = 0;
14896 }
14897 else
14898 {
14899 EMSG2(_(e_invexpr2), arg);
14900 nr = 0;
14901 }
14902 }
14903
14904 if (nr > 0)
14905 for (wp = firstwin; wp != twin; wp = wp->w_next)
14906 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014908 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909}
14910
14911/*
14912 * "winrestcmd()" function
14913 */
14914/* ARGSUSED */
14915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014916f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014917 typval_T *argvars;
14918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919{
14920#ifdef FEAT_WINDOWS
14921 win_T *wp;
14922 int winnr = 1;
14923 garray_T ga;
14924 char_u buf[50];
14925
14926 ga_init2(&ga, (int)sizeof(char), 70);
14927 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14928 {
14929 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14930 ga_concat(&ga, buf);
14931# ifdef FEAT_VERTSPLIT
14932 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14933 ga_concat(&ga, buf);
14934# endif
14935 ++winnr;
14936 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014937 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014939 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014941 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014943 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014944}
14945
14946/*
14947 * "winwidth(nr)" function
14948 */
14949 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014950f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014951 typval_T *argvars;
14952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953{
14954 win_T *wp;
14955
14956 wp = find_win_by_nr(&argvars[0]);
14957 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014958 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959 else
14960#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014961 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014963 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964#endif
14965}
14966
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014968 * "writefile()" function
14969 */
14970 static void
14971f_writefile(argvars, rettv)
14972 typval_T *argvars;
14973 typval_T *rettv;
14974{
14975 int binary = FALSE;
14976 char_u *fname;
14977 FILE *fd;
14978 listitem_T *li;
14979 char_u *s;
14980 int ret = 0;
14981 int c;
14982
14983 if (argvars[0].v_type != VAR_LIST)
14984 {
14985 EMSG2(_(e_listarg), "writefile()");
14986 return;
14987 }
14988 if (argvars[0].vval.v_list == NULL)
14989 return;
14990
14991 if (argvars[2].v_type != VAR_UNKNOWN
14992 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
14993 binary = TRUE;
14994
14995 /* Always open the file in binary mode, library functions have a mind of
14996 * their own about CR-LF conversion. */
14997 fname = get_tv_string(&argvars[1]);
14998 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
14999 {
15000 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15001 ret = -1;
15002 }
15003 else
15004 {
15005 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15006 li = li->li_next)
15007 {
15008 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15009 {
15010 if (*s == '\n')
15011 c = putc(NUL, fd);
15012 else
15013 c = putc(*s, fd);
15014 if (c == EOF)
15015 {
15016 ret = -1;
15017 break;
15018 }
15019 }
15020 if (!binary || li->li_next != NULL)
15021 if (putc('\n', fd) == EOF)
15022 {
15023 ret = -1;
15024 break;
15025 }
15026 if (ret < 0)
15027 {
15028 EMSG(_(e_write));
15029 break;
15030 }
15031 }
15032 fclose(fd);
15033 }
15034
15035 rettv->vval.v_number = ret;
15036}
15037
15038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039 * Translate a String variable into a position.
15040 */
15041 static pos_T *
15042var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015043 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044 int lnum; /* TRUE when $ is last line */
15045{
15046 char_u *name;
15047 static pos_T pos;
15048 pos_T *pp;
15049
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015050 name = get_tv_string_chk(varp);
15051 if (name == NULL)
15052 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015053 if (name[0] == '.') /* cursor */
15054 return &curwin->w_cursor;
15055 if (name[0] == '\'') /* mark */
15056 {
15057 pp = getmark(name[1], FALSE);
15058 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15059 return NULL;
15060 return pp;
15061 }
15062 if (name[0] == '$') /* last column or line */
15063 {
15064 if (lnum)
15065 {
15066 pos.lnum = curbuf->b_ml.ml_line_count;
15067 pos.col = 0;
15068 }
15069 else
15070 {
15071 pos.lnum = curwin->w_cursor.lnum;
15072 pos.col = (colnr_T)STRLEN(ml_get_curline());
15073 }
15074 return &pos;
15075 }
15076 return NULL;
15077}
15078
15079/*
15080 * Get the length of an environment variable name.
15081 * Advance "arg" to the first character after the name.
15082 * Return 0 for error.
15083 */
15084 static int
15085get_env_len(arg)
15086 char_u **arg;
15087{
15088 char_u *p;
15089 int len;
15090
15091 for (p = *arg; vim_isIDc(*p); ++p)
15092 ;
15093 if (p == *arg) /* no name found */
15094 return 0;
15095
15096 len = (int)(p - *arg);
15097 *arg = p;
15098 return len;
15099}
15100
15101/*
15102 * Get the length of the name of a function or internal variable.
15103 * "arg" is advanced to the first non-white character after the name.
15104 * Return 0 if something is wrong.
15105 */
15106 static int
15107get_id_len(arg)
15108 char_u **arg;
15109{
15110 char_u *p;
15111 int len;
15112
15113 /* Find the end of the name. */
15114 for (p = *arg; eval_isnamec(*p); ++p)
15115 ;
15116 if (p == *arg) /* no name found */
15117 return 0;
15118
15119 len = (int)(p - *arg);
15120 *arg = skipwhite(p);
15121
15122 return len;
15123}
15124
15125/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015126 * Get the length of the name of a variable or function.
15127 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015129 * Return -1 if curly braces expansion failed.
15130 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 * If the name contains 'magic' {}'s, expand them and return the
15132 * expanded name in an allocated string via 'alias' - caller must free.
15133 */
15134 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015135get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136 char_u **arg;
15137 char_u **alias;
15138 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015139 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140{
15141 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142 char_u *p;
15143 char_u *expr_start;
15144 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145
15146 *alias = NULL; /* default to no alias */
15147
15148 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15149 && (*arg)[2] == (int)KE_SNR)
15150 {
15151 /* hard coded <SNR>, already translated */
15152 *arg += 3;
15153 return get_id_len(arg) + 3;
15154 }
15155 len = eval_fname_script(*arg);
15156 if (len > 0)
15157 {
15158 /* literal "<SID>", "s:" or "<SNR>" */
15159 *arg += len;
15160 }
15161
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015163 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015165 p = find_name_end(*arg, &expr_start, &expr_end,
15166 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167 if (expr_start != NULL)
15168 {
15169 char_u *temp_string;
15170
15171 if (!evaluate)
15172 {
15173 len += (int)(p - *arg);
15174 *arg = skipwhite(p);
15175 return len;
15176 }
15177
15178 /*
15179 * Include any <SID> etc in the expanded string:
15180 * Thus the -len here.
15181 */
15182 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15183 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015184 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185 *alias = temp_string;
15186 *arg = skipwhite(p);
15187 return (int)STRLEN(temp_string);
15188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189
15190 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015191 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192 EMSG2(_(e_invexpr2), *arg);
15193
15194 return len;
15195}
15196
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015197/*
15198 * Find the end of a variable or function name, taking care of magic braces.
15199 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15200 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015201 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015202 * Return a pointer to just after the name. Equal to "arg" if there is no
15203 * valid name.
15204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015206find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015207 char_u *arg;
15208 char_u **expr_start;
15209 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015210 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015212 int mb_nest = 0;
15213 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015214 char_u *p;
15215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015216 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015217 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015218 *expr_start = NULL;
15219 *expr_end = NULL;
15220 }
15221
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015222 /* Quick check for valid starting character. */
15223 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15224 return arg;
15225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015226 for (p = arg; *p != NUL
15227 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015228 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015229 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015230 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015231 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015232 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015233 if (*p == '\'')
15234 {
15235 /* skip over 'string' to avoid counting [ and ] inside it. */
15236 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15237 ;
15238 if (*p == NUL)
15239 break;
15240 }
15241 else if (*p == '"')
15242 {
15243 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15244 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15245 if (*p == '\\' && p[1] != NUL)
15246 ++p;
15247 if (*p == NUL)
15248 break;
15249 }
15250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015251 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015252 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015253 if (*p == '[')
15254 ++br_nest;
15255 else if (*p == ']')
15256 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015259 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015261 if (*p == '{')
15262 {
15263 mb_nest++;
15264 if (expr_start != NULL && *expr_start == NULL)
15265 *expr_start = p;
15266 }
15267 else if (*p == '}')
15268 {
15269 mb_nest--;
15270 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15271 *expr_end = p;
15272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274 }
15275
15276 return p;
15277}
15278
15279/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015280 * Expands out the 'magic' {}'s in a variable/function name.
15281 * Note that this can call itself recursively, to deal with
15282 * constructs like foo{bar}{baz}{bam}
15283 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15284 * "in_start" ^
15285 * "expr_start" ^
15286 * "expr_end" ^
15287 * "in_end" ^
15288 *
15289 * Returns a new allocated string, which the caller must free.
15290 * Returns NULL for failure.
15291 */
15292 static char_u *
15293make_expanded_name(in_start, expr_start, expr_end, in_end)
15294 char_u *in_start;
15295 char_u *expr_start;
15296 char_u *expr_end;
15297 char_u *in_end;
15298{
15299 char_u c1;
15300 char_u *retval = NULL;
15301 char_u *temp_result;
15302 char_u *nextcmd = NULL;
15303
15304 if (expr_end == NULL || in_end == NULL)
15305 return NULL;
15306 *expr_start = NUL;
15307 *expr_end = NUL;
15308 c1 = *in_end;
15309 *in_end = NUL;
15310
15311 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15312 if (temp_result != NULL && nextcmd == NULL)
15313 {
15314 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15315 + (in_end - expr_end) + 1));
15316 if (retval != NULL)
15317 {
15318 STRCPY(retval, in_start);
15319 STRCAT(retval, temp_result);
15320 STRCAT(retval, expr_end + 1);
15321 }
15322 }
15323 vim_free(temp_result);
15324
15325 *in_end = c1; /* put char back for error messages */
15326 *expr_start = '{';
15327 *expr_end = '}';
15328
15329 if (retval != NULL)
15330 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015331 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015332 if (expr_start != NULL)
15333 {
15334 /* Further expansion! */
15335 temp_result = make_expanded_name(retval, expr_start,
15336 expr_end, temp_result);
15337 vim_free(retval);
15338 retval = temp_result;
15339 }
15340 }
15341
15342 return retval;
15343}
15344
15345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015347 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348 */
15349 static int
15350eval_isnamec(c)
15351 int c;
15352{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015353 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15354}
15355
15356/*
15357 * Return TRUE if character "c" can be used as the first character in a
15358 * variable or function name (excluding '{' and '}').
15359 */
15360 static int
15361eval_isnamec1(c)
15362 int c;
15363{
15364 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365}
15366
15367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368 * Set number v: variable to "val".
15369 */
15370 void
15371set_vim_var_nr(idx, val)
15372 int idx;
15373 long val;
15374{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015375 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376}
15377
15378/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015379 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 */
15381 long
15382get_vim_var_nr(idx)
15383 int idx;
15384{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015385 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386}
15387
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015388#if defined(FEAT_AUTOCMD) || defined(PROTO)
15389/*
15390 * Get string v: variable value. Uses a static buffer, can only be used once.
15391 */
15392 char_u *
15393get_vim_var_str(idx)
15394 int idx;
15395{
15396 return get_tv_string(&vimvars[idx].vv_tv);
15397}
15398#endif
15399
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400/*
15401 * Set v:count, v:count1 and v:prevcount.
15402 */
15403 void
15404set_vcount(count, count1)
15405 long count;
15406 long count1;
15407{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015408 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15409 vimvars[VV_COUNT].vv_nr = count;
15410 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411}
15412
15413/*
15414 * Set string v: variable to a copy of "val".
15415 */
15416 void
15417set_vim_var_string(idx, val, len)
15418 int idx;
15419 char_u *val;
15420 int len; /* length of "val" to use or -1 (whole string) */
15421{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015422 /* Need to do this (at least) once, since we can't initialize a union.
15423 * Will always be invoked when "v:progname" is set. */
15424 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15425
Bram Moolenaare9a41262005-01-15 22:18:47 +000015426 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015428 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015430 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015432 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015433}
15434
15435/*
15436 * Set v:register if needed.
15437 */
15438 void
15439set_reg_var(c)
15440 int c;
15441{
15442 char_u regname;
15443
15444 if (c == 0 || c == ' ')
15445 regname = '"';
15446 else
15447 regname = c;
15448 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015449 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450 set_vim_var_string(VV_REG, &regname, 1);
15451}
15452
15453/*
15454 * Get or set v:exception. If "oldval" == NULL, return the current value.
15455 * Otherwise, restore the value to "oldval" and return NULL.
15456 * Must always be called in pairs to save and restore v:exception! Does not
15457 * take care of memory allocations.
15458 */
15459 char_u *
15460v_exception(oldval)
15461 char_u *oldval;
15462{
15463 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015464 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465
Bram Moolenaare9a41262005-01-15 22:18:47 +000015466 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467 return NULL;
15468}
15469
15470/*
15471 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15472 * Otherwise, restore the value to "oldval" and return NULL.
15473 * Must always be called in pairs to save and restore v:throwpoint! Does not
15474 * take care of memory allocations.
15475 */
15476 char_u *
15477v_throwpoint(oldval)
15478 char_u *oldval;
15479{
15480 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015481 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482
Bram Moolenaare9a41262005-01-15 22:18:47 +000015483 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484 return NULL;
15485}
15486
15487#if defined(FEAT_AUTOCMD) || defined(PROTO)
15488/*
15489 * Set v:cmdarg.
15490 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15491 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15492 * Must always be called in pairs!
15493 */
15494 char_u *
15495set_cmdarg(eap, oldarg)
15496 exarg_T *eap;
15497 char_u *oldarg;
15498{
15499 char_u *oldval;
15500 char_u *newval;
15501 unsigned len;
15502
Bram Moolenaare9a41262005-01-15 22:18:47 +000015503 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015504 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015506 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015507 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015508 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509 }
15510
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015511 if (eap->force_bin == FORCE_BIN)
15512 len = 6;
15513 else if (eap->force_bin == FORCE_NOBIN)
15514 len = 8;
15515 else
15516 len = 0;
15517 if (eap->force_ff != 0)
15518 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15519# ifdef FEAT_MBYTE
15520 if (eap->force_enc != 0)
15521 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15522# endif
15523
15524 newval = alloc(len + 1);
15525 if (newval == NULL)
15526 return NULL;
15527
15528 if (eap->force_bin == FORCE_BIN)
15529 sprintf((char *)newval, " ++bin");
15530 else if (eap->force_bin == FORCE_NOBIN)
15531 sprintf((char *)newval, " ++nobin");
15532 else
15533 *newval = NUL;
15534 if (eap->force_ff != 0)
15535 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15536 eap->cmd + eap->force_ff);
15537# ifdef FEAT_MBYTE
15538 if (eap->force_enc != 0)
15539 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15540 eap->cmd + eap->force_enc);
15541# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015542 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015543 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544}
15545#endif
15546
15547/*
15548 * Get the value of internal variable "name".
15549 * Return OK or FAIL.
15550 */
15551 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015552get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 char_u *name;
15554 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015555 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015556 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557{
15558 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015559 typval_T *tv = NULL;
15560 typval_T atv;
15561 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563
15564 /* truncate the name, so that we can use strcmp() */
15565 cc = name[len];
15566 name[len] = NUL;
15567
15568 /*
15569 * Check for "b:changedtick".
15570 */
15571 if (STRCMP(name, "b:changedtick") == 0)
15572 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015573 atv.v_type = VAR_NUMBER;
15574 atv.vval.v_number = curbuf->b_changedtick;
15575 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576 }
15577
15578 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 * Check for user-defined variables.
15580 */
15581 else
15582 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015583 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015585 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586 }
15587
Bram Moolenaare9a41262005-01-15 22:18:47 +000015588 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015590 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015591 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 ret = FAIL;
15593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015594 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015595 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596
15597 name[len] = cc;
15598
15599 return ret;
15600}
15601
15602/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015603 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15604 * Also handle function call with Funcref variable: func(expr)
15605 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15606 */
15607 static int
15608handle_subscript(arg, rettv, evaluate, verbose)
15609 char_u **arg;
15610 typval_T *rettv;
15611 int evaluate; /* do more than finding the end */
15612 int verbose; /* give error messages */
15613{
15614 int ret = OK;
15615 dict_T *selfdict = NULL;
15616 char_u *s;
15617 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015618 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015619
15620 while (ret == OK
15621 && (**arg == '['
15622 || (**arg == '.' && rettv->v_type == VAR_DICT)
15623 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15624 && !vim_iswhite(*(*arg - 1)))
15625 {
15626 if (**arg == '(')
15627 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015628 /* need to copy the funcref so that we can clear rettv */
15629 functv = *rettv;
15630 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015631
15632 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015633 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015634 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015635 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15636 &len, evaluate, selfdict);
15637
15638 /* Clear the funcref afterwards, so that deleting it while
15639 * evaluating the arguments is possible (see test55). */
15640 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015641
15642 /* Stop the expression evaluation when immediately aborting on
15643 * error, or when an interrupt occurred or an exception was thrown
15644 * but not caught. */
15645 if (aborting())
15646 {
15647 if (ret == OK)
15648 clear_tv(rettv);
15649 ret = FAIL;
15650 }
15651 dict_unref(selfdict);
15652 selfdict = NULL;
15653 }
15654 else /* **arg == '[' || **arg == '.' */
15655 {
15656 dict_unref(selfdict);
15657 if (rettv->v_type == VAR_DICT)
15658 {
15659 selfdict = rettv->vval.v_dict;
15660 if (selfdict != NULL)
15661 ++selfdict->dv_refcount;
15662 }
15663 else
15664 selfdict = NULL;
15665 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15666 {
15667 clear_tv(rettv);
15668 ret = FAIL;
15669 }
15670 }
15671 }
15672 dict_unref(selfdict);
15673 return ret;
15674}
15675
15676/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015677 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15678 * value).
15679 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015680 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015681alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015682{
Bram Moolenaar33570922005-01-25 22:26:29 +000015683 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015684}
15685
15686/*
15687 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688 * The string "s" must have been allocated, it is consumed.
15689 * Return NULL for out of memory, the variable otherwise.
15690 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015691 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015692alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693 char_u *s;
15694{
Bram Moolenaar33570922005-01-25 22:26:29 +000015695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015697 rettv = alloc_tv();
15698 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015700 rettv->v_type = VAR_STRING;
15701 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702 }
15703 else
15704 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015705 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706}
15707
15708/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015709 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710 */
15711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015712free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015713 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714{
15715 if (varp != NULL)
15716 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015717 switch (varp->v_type)
15718 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015719 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015720 func_unref(varp->vval.v_string);
15721 /*FALLTHROUGH*/
15722 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015723 vim_free(varp->vval.v_string);
15724 break;
15725 case VAR_LIST:
15726 list_unref(varp->vval.v_list);
15727 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015728 case VAR_DICT:
15729 dict_unref(varp->vval.v_dict);
15730 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015731 case VAR_NUMBER:
15732 case VAR_UNKNOWN:
15733 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015734 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015735 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015736 break;
15737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738 vim_free(varp);
15739 }
15740}
15741
15742/*
15743 * Free the memory for a variable value and set the value to NULL or 0.
15744 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015745 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015746clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015747 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748{
15749 if (varp != NULL)
15750 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015751 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015753 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015754 func_unref(varp->vval.v_string);
15755 /*FALLTHROUGH*/
15756 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015757 vim_free(varp->vval.v_string);
15758 varp->vval.v_string = NULL;
15759 break;
15760 case VAR_LIST:
15761 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015762 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015763 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015764 case VAR_DICT:
15765 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015766 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015767 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015768 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015769 varp->vval.v_number = 0;
15770 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015771 case VAR_UNKNOWN:
15772 break;
15773 default:
15774 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015776 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 }
15778}
15779
15780/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015781 * Set the value of a variable to NULL without freeing items.
15782 */
15783 static void
15784init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015785 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015786{
15787 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015788 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015789}
15790
15791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792 * Get the number value of a variable.
15793 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015794 * For incompatible types, return 0.
15795 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15796 * caller of incompatible types: it sets *denote to TRUE if "denote"
15797 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798 */
15799 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015800get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015801 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015803 int error = FALSE;
15804
15805 return get_tv_number_chk(varp, &error); /* return 0L on error */
15806}
15807
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015808 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015809get_tv_number_chk(varp, denote)
15810 typval_T *varp;
15811 int *denote;
15812{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015813 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015815 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015817 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015818 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015819 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015820 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015821 break;
15822 case VAR_STRING:
15823 if (varp->vval.v_string != NULL)
15824 vim_str2nr(varp->vval.v_string, NULL, NULL,
15825 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015826 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015827 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015828 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015829 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015830 case VAR_DICT:
15831 EMSG(_("E728: Using a Dictionary as a number"));
15832 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015833 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015834 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015835 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015836 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015837 if (denote == NULL) /* useful for values that must be unsigned */
15838 n = -1;
15839 else
15840 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015841 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842}
15843
15844/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015845 * Get the lnum from the first argument.
15846 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015847 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848 */
15849 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015850get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015851 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852{
Bram Moolenaar33570922005-01-25 22:26:29 +000015853 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854 linenr_T lnum;
15855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015856 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 if (lnum == 0) /* no valid number, try using line() */
15858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015859 rettv.v_type = VAR_NUMBER;
15860 f_line(argvars, &rettv);
15861 lnum = rettv.vval.v_number;
15862 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863 }
15864 return lnum;
15865}
15866
15867/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015868 * Get the lnum from the first argument.
15869 * Also accepts "$", then "buf" is used.
15870 * Returns 0 on error.
15871 */
15872 static linenr_T
15873get_tv_lnum_buf(argvars, buf)
15874 typval_T *argvars;
15875 buf_T *buf;
15876{
15877 if (argvars[0].v_type == VAR_STRING
15878 && argvars[0].vval.v_string != NULL
15879 && argvars[0].vval.v_string[0] == '$'
15880 && buf != NULL)
15881 return buf->b_ml.ml_line_count;
15882 return get_tv_number_chk(&argvars[0], NULL);
15883}
15884
15885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886 * Get the string value of a variable.
15887 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015888 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15889 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890 * If the String variable has never been set, return an empty string.
15891 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015892 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15893 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015894 */
15895 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015896get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015897 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898{
15899 static char_u mybuf[NUMBUFLEN];
15900
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015901 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015902}
15903
15904 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015905get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015906 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015907 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015909 char_u *res = get_tv_string_buf_chk(varp, buf);
15910
15911 return res != NULL ? res : (char_u *)"";
15912}
15913
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015914 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015915get_tv_string_chk(varp)
15916 typval_T *varp;
15917{
15918 static char_u mybuf[NUMBUFLEN];
15919
15920 return get_tv_string_buf_chk(varp, mybuf);
15921}
15922
15923 static char_u *
15924get_tv_string_buf_chk(varp, buf)
15925 typval_T *varp;
15926 char_u *buf;
15927{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015928 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015930 case VAR_NUMBER:
15931 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15932 return buf;
15933 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015934 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015935 break;
15936 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015937 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015938 break;
15939 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015940 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015941 break;
15942 case VAR_STRING:
15943 if (varp->vval.v_string != NULL)
15944 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015945 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015946 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015947 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015948 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015950 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951}
15952
15953/*
15954 * Find variable "name" in the list of variables.
15955 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015956 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015957 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015958 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015959 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015960 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015961find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015962 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015963 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015965 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015966 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967
Bram Moolenaara7043832005-01-21 11:56:39 +000015968 ht = find_var_ht(name, &varname);
15969 if (htp != NULL)
15970 *htp = ht;
15971 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015973 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974}
15975
15976/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015977 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000015978 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015979 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015980 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015981find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000015982 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000015983 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015984 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000015985{
Bram Moolenaar33570922005-01-25 22:26:29 +000015986 hashitem_T *hi;
15987
15988 if (*varname == NUL)
15989 {
15990 /* Must be something like "s:", otherwise "ht" would be NULL. */
15991 switch (varname[-2])
15992 {
15993 case 's': return &SCRIPT_SV(current_SID).sv_var;
15994 case 'g': return &globvars_var;
15995 case 'v': return &vimvars_var;
15996 case 'b': return &curbuf->b_bufvar;
15997 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015998 case 'l': return current_funccal == NULL
15999 ? NULL : &current_funccal->l_vars_var;
16000 case 'a': return current_funccal == NULL
16001 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016002 }
16003 return NULL;
16004 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016005
16006 hi = hash_find(ht, varname);
16007 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016008 {
16009 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016010 * worked find the variable again. Don't auto-load a script if it was
16011 * loaded already, otherwise it would be loaded every time when
16012 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016013 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016014 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016015 hi = hash_find(ht, varname);
16016 if (HASHITEM_EMPTY(hi))
16017 return NULL;
16018 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016019 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016020}
16021
16022/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016023 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016024 * Set "varname" to the start of name without ':'.
16025 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016026 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016027find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028 char_u *name;
16029 char_u **varname;
16030{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016031 hashitem_T *hi;
16032
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033 if (name[1] != ':')
16034 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016035 /* The name must not start with a colon or #. */
16036 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037 return NULL;
16038 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016039
16040 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016041 hi = hash_find(&compat_hashtab, name);
16042 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016043 return &compat_hashtab;
16044
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016046 return &globvarht; /* global variable */
16047 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048 }
16049 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016050 if (*name == 'g') /* global variable */
16051 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016052 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16053 */
16054 if (vim_strchr(name + 2, ':') != NULL
16055 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016056 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016058 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016060 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016061 if (*name == 'v') /* v: variable */
16062 return &vimvarht;
16063 if (*name == 'a' && current_funccal != NULL) /* function argument */
16064 return &current_funccal->l_avars.dv_hashtab;
16065 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16066 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016067 if (*name == 's' /* script variable */
16068 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16069 return &SCRIPT_VARS(current_SID);
16070 return NULL;
16071}
16072
16073/*
16074 * Get the string value of a (global/local) variable.
16075 * Returns NULL when it doesn't exist.
16076 */
16077 char_u *
16078get_var_value(name)
16079 char_u *name;
16080{
Bram Moolenaar33570922005-01-25 22:26:29 +000016081 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082
Bram Moolenaara7043832005-01-21 11:56:39 +000016083 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084 if (v == NULL)
16085 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016086 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016087}
16088
16089/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016090 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091 * sourcing this script and when executing functions defined in the script.
16092 */
16093 void
16094new_script_vars(id)
16095 scid_T id;
16096{
Bram Moolenaara7043832005-01-21 11:56:39 +000016097 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016098 hashtab_T *ht;
16099 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016100
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16102 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016103 /* Re-allocating ga_data means that an ht_array pointing to
16104 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016105 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016106 for (i = 1; i <= ga_scripts.ga_len; ++i)
16107 {
16108 ht = &SCRIPT_VARS(i);
16109 if (ht->ht_mask == HT_INIT_SIZE - 1)
16110 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016111 sv = &SCRIPT_SV(i);
16112 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016113 }
16114
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115 while (ga_scripts.ga_len < id)
16116 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016117 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16118 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120 }
16121 }
16122}
16123
16124/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016125 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16126 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016127 */
16128 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016129init_var_dict(dict, dict_var)
16130 dict_T *dict;
16131 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132{
Bram Moolenaar33570922005-01-25 22:26:29 +000016133 hash_init(&dict->dv_hashtab);
16134 dict->dv_refcount = 99999;
16135 dict_var->di_tv.vval.v_dict = dict;
16136 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016137 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016138 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16139 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140}
16141
16142/*
16143 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016144 * Frees all allocated variables and the value they contain.
16145 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146 */
16147 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016148vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016149 hashtab_T *ht;
16150{
16151 vars_clear_ext(ht, TRUE);
16152}
16153
16154/*
16155 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16156 */
16157 static void
16158vars_clear_ext(ht, free_val)
16159 hashtab_T *ht;
16160 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161{
Bram Moolenaara7043832005-01-21 11:56:39 +000016162 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016163 hashitem_T *hi;
16164 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165
Bram Moolenaar33570922005-01-25 22:26:29 +000016166 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016167 todo = ht->ht_used;
16168 for (hi = ht->ht_array; todo > 0; ++hi)
16169 {
16170 if (!HASHITEM_EMPTY(hi))
16171 {
16172 --todo;
16173
Bram Moolenaar33570922005-01-25 22:26:29 +000016174 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016175 * ht_array might change then. hash_clear() takes care of it
16176 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016177 v = HI2DI(hi);
16178 if (free_val)
16179 clear_tv(&v->di_tv);
16180 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16181 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016182 }
16183 }
16184 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016185 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186}
16187
Bram Moolenaara7043832005-01-21 11:56:39 +000016188/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016189 * Delete a variable from hashtab "ht" at item "hi".
16190 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016193delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016194 hashtab_T *ht;
16195 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196{
Bram Moolenaar33570922005-01-25 22:26:29 +000016197 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016198
16199 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016200 clear_tv(&di->di_tv);
16201 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202}
16203
16204/*
16205 * List the value of one internal variable.
16206 */
16207 static void
16208list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016209 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 char_u *prefix;
16211{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016212 char_u *tofree;
16213 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016214 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016215
Bram Moolenaar33570922005-01-25 22:26:29 +000016216 s = echo_string(&v->di_tv, &tofree, numbuf);
16217 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016218 s == NULL ? (char_u *)"" : s);
16219 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220}
16221
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 static void
16223list_one_var_a(prefix, name, type, string)
16224 char_u *prefix;
16225 char_u *name;
16226 int type;
16227 char_u *string;
16228{
16229 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16230 if (name != NULL) /* "a:" vars don't have a name stored */
16231 msg_puts(name);
16232 msg_putchar(' ');
16233 msg_advance(22);
16234 if (type == VAR_NUMBER)
16235 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016236 else if (type == VAR_FUNC)
16237 msg_putchar('*');
16238 else if (type == VAR_LIST)
16239 {
16240 msg_putchar('[');
16241 if (*string == '[')
16242 ++string;
16243 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016244 else if (type == VAR_DICT)
16245 {
16246 msg_putchar('{');
16247 if (*string == '{')
16248 ++string;
16249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250 else
16251 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016252
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016254
16255 if (type == VAR_FUNC)
16256 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257}
16258
16259/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016260 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261 * If the variable already exists, the value is updated.
16262 * Otherwise the variable is created.
16263 */
16264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016265set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016267 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016268 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269{
Bram Moolenaar33570922005-01-25 22:26:29 +000016270 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016272 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016273 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016275 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016276 {
16277 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16278 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16279 ? name[2] : name[0]))
16280 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016281 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016282 return;
16283 }
16284 if (function_exists(name))
16285 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016286 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016287 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016288 return;
16289 }
16290 }
16291
Bram Moolenaara7043832005-01-21 11:56:39 +000016292 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016293 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016294 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016295 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016296 return;
16297 }
16298
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016299 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016300 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016302 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016303 if (var_check_ro(v->di_flags, name)
16304 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016305 return;
16306 if (v->di_tv.v_type != tv->v_type
16307 && !((v->di_tv.v_type == VAR_STRING
16308 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016309 && (tv->v_type == VAR_STRING
16310 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016311 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016312 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016313 return;
16314 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016315
16316 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016317 * Handle setting internal v: variables separately: we don't change
16318 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016319 */
16320 if (ht == &vimvarht)
16321 {
16322 if (v->di_tv.v_type == VAR_STRING)
16323 {
16324 vim_free(v->di_tv.vval.v_string);
16325 if (copy || tv->v_type != VAR_STRING)
16326 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16327 else
16328 {
16329 /* Take over the string to avoid an extra alloc/free. */
16330 v->di_tv.vval.v_string = tv->vval.v_string;
16331 tv->vval.v_string = NULL;
16332 }
16333 }
16334 else if (v->di_tv.v_type != VAR_NUMBER)
16335 EMSG2(_(e_intern2), "set_var()");
16336 else
16337 v->di_tv.vval.v_number = get_tv_number(tv);
16338 return;
16339 }
16340
16341 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342 }
16343 else /* add a new variable */
16344 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016345 /* Make sure the variable name is valid. */
16346 for (p = varname; *p != NUL; ++p)
16347 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16348 {
16349 EMSG2(_(e_illvar), varname);
16350 return;
16351 }
16352
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016353 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16354 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016355 if (v == NULL)
16356 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016357 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016358 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016360 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361 return;
16362 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016363 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016366 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016367 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016368 else
16369 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016370 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016371 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016372 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374}
16375
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016376/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016377 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16378 * Also give an error message.
16379 */
16380 static int
16381var_check_ro(flags, name)
16382 int flags;
16383 char_u *name;
16384{
16385 if (flags & DI_FLAGS_RO)
16386 {
16387 EMSG2(_(e_readonlyvar), name);
16388 return TRUE;
16389 }
16390 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16391 {
16392 EMSG2(_(e_readonlysbx), name);
16393 return TRUE;
16394 }
16395 return FALSE;
16396}
16397
16398/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016399 * Return TRUE if typeval "tv" is set to be locked (immutable).
16400 * Also give an error message, using "name".
16401 */
16402 static int
16403tv_check_lock(lock, name)
16404 int lock;
16405 char_u *name;
16406{
16407 if (lock & VAR_LOCKED)
16408 {
16409 EMSG2(_("E741: Value is locked: %s"),
16410 name == NULL ? (char_u *)_("Unknown") : name);
16411 return TRUE;
16412 }
16413 if (lock & VAR_FIXED)
16414 {
16415 EMSG2(_("E742: Cannot change value of %s"),
16416 name == NULL ? (char_u *)_("Unknown") : name);
16417 return TRUE;
16418 }
16419 return FALSE;
16420}
16421
16422/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016423 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016424 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016425 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016428copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016429 typval_T *from;
16430 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016432 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016433 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016434 switch (from->v_type)
16435 {
16436 case VAR_NUMBER:
16437 to->vval.v_number = from->vval.v_number;
16438 break;
16439 case VAR_STRING:
16440 case VAR_FUNC:
16441 if (from->vval.v_string == NULL)
16442 to->vval.v_string = NULL;
16443 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016444 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016445 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016446 if (from->v_type == VAR_FUNC)
16447 func_ref(to->vval.v_string);
16448 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016449 break;
16450 case VAR_LIST:
16451 if (from->vval.v_list == NULL)
16452 to->vval.v_list = NULL;
16453 else
16454 {
16455 to->vval.v_list = from->vval.v_list;
16456 ++to->vval.v_list->lv_refcount;
16457 }
16458 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016459 case VAR_DICT:
16460 if (from->vval.v_dict == NULL)
16461 to->vval.v_dict = NULL;
16462 else
16463 {
16464 to->vval.v_dict = from->vval.v_dict;
16465 ++to->vval.v_dict->dv_refcount;
16466 }
16467 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016468 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016469 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016470 break;
16471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472}
16473
16474/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016475 * Make a copy of an item.
16476 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016477 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16478 * reference to an already copied list/dict can be used.
16479 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016480 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016481 static int
16482item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016483 typval_T *from;
16484 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016485 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016486 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016487{
16488 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016489 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016490
Bram Moolenaar33570922005-01-25 22:26:29 +000016491 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016492 {
16493 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016494 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016495 }
16496 ++recurse;
16497
16498 switch (from->v_type)
16499 {
16500 case VAR_NUMBER:
16501 case VAR_STRING:
16502 case VAR_FUNC:
16503 copy_tv(from, to);
16504 break;
16505 case VAR_LIST:
16506 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016507 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016508 if (from->vval.v_list == NULL)
16509 to->vval.v_list = NULL;
16510 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16511 {
16512 /* use the copy made earlier */
16513 to->vval.v_list = from->vval.v_list->lv_copylist;
16514 ++to->vval.v_list->lv_refcount;
16515 }
16516 else
16517 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16518 if (to->vval.v_list == NULL)
16519 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016520 break;
16521 case VAR_DICT:
16522 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016523 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016524 if (from->vval.v_dict == NULL)
16525 to->vval.v_dict = NULL;
16526 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16527 {
16528 /* use the copy made earlier */
16529 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16530 ++to->vval.v_dict->dv_refcount;
16531 }
16532 else
16533 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16534 if (to->vval.v_dict == NULL)
16535 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016536 break;
16537 default:
16538 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016539 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016540 }
16541 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016542 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016543}
16544
16545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 * ":echo expr1 ..." print each argument separated with a space, add a
16547 * newline at the end.
16548 * ":echon expr1 ..." print each argument plain.
16549 */
16550 void
16551ex_echo(eap)
16552 exarg_T *eap;
16553{
16554 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016555 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016556 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557 char_u *p;
16558 int needclr = TRUE;
16559 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016560 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561
16562 if (eap->skip)
16563 ++emsg_skip;
16564 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16565 {
16566 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016567 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568 {
16569 /*
16570 * Report the invalid expression unless the expression evaluation
16571 * has been cancelled due to an aborting error, an interrupt, or an
16572 * exception.
16573 */
16574 if (!aborting())
16575 EMSG2(_(e_invexpr2), p);
16576 break;
16577 }
16578 if (!eap->skip)
16579 {
16580 if (atstart)
16581 {
16582 atstart = FALSE;
16583 /* Call msg_start() after eval1(), evaluating the expression
16584 * may cause a message to appear. */
16585 if (eap->cmdidx == CMD_echo)
16586 msg_start();
16587 }
16588 else if (eap->cmdidx == CMD_echo)
16589 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016590 p = echo_string(&rettv, &tofree, numbuf);
16591 if (p != NULL)
16592 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016594 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016596 if (*p != TAB && needclr)
16597 {
16598 /* remove any text still there from the command */
16599 msg_clr_eos();
16600 needclr = FALSE;
16601 }
16602 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603 }
16604 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016605 {
16606#ifdef FEAT_MBYTE
16607 if (has_mbyte)
16608 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016609 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016610
16611 (void)msg_outtrans_len_attr(p, i, echo_attr);
16612 p += i - 1;
16613 }
16614 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016616 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016619 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016621 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622 arg = skipwhite(arg);
16623 }
16624 eap->nextcmd = check_nextcmd(arg);
16625
16626 if (eap->skip)
16627 --emsg_skip;
16628 else
16629 {
16630 /* remove text that may still be there from the command */
16631 if (needclr)
16632 msg_clr_eos();
16633 if (eap->cmdidx == CMD_echo)
16634 msg_end();
16635 }
16636}
16637
16638/*
16639 * ":echohl {name}".
16640 */
16641 void
16642ex_echohl(eap)
16643 exarg_T *eap;
16644{
16645 int id;
16646
16647 id = syn_name2id(eap->arg);
16648 if (id == 0)
16649 echo_attr = 0;
16650 else
16651 echo_attr = syn_id2attr(id);
16652}
16653
16654/*
16655 * ":execute expr1 ..." execute the result of an expression.
16656 * ":echomsg expr1 ..." Print a message
16657 * ":echoerr expr1 ..." Print an error
16658 * Each gets spaces around each argument and a newline at the end for
16659 * echo commands
16660 */
16661 void
16662ex_execute(eap)
16663 exarg_T *eap;
16664{
16665 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016666 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667 int ret = OK;
16668 char_u *p;
16669 garray_T ga;
16670 int len;
16671 int save_did_emsg;
16672
16673 ga_init2(&ga, 1, 80);
16674
16675 if (eap->skip)
16676 ++emsg_skip;
16677 while (*arg != NUL && *arg != '|' && *arg != '\n')
16678 {
16679 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016680 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016681 {
16682 /*
16683 * Report the invalid expression unless the expression evaluation
16684 * has been cancelled due to an aborting error, an interrupt, or an
16685 * exception.
16686 */
16687 if (!aborting())
16688 EMSG2(_(e_invexpr2), p);
16689 ret = FAIL;
16690 break;
16691 }
16692
16693 if (!eap->skip)
16694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016695 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696 len = (int)STRLEN(p);
16697 if (ga_grow(&ga, len + 2) == FAIL)
16698 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016699 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016700 ret = FAIL;
16701 break;
16702 }
16703 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706 ga.ga_len += len;
16707 }
16708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016709 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710 arg = skipwhite(arg);
16711 }
16712
16713 if (ret != FAIL && ga.ga_data != NULL)
16714 {
16715 if (eap->cmdidx == CMD_echomsg)
16716 MSG_ATTR(ga.ga_data, echo_attr);
16717 else if (eap->cmdidx == CMD_echoerr)
16718 {
16719 /* We don't want to abort following commands, restore did_emsg. */
16720 save_did_emsg = did_emsg;
16721 EMSG((char_u *)ga.ga_data);
16722 if (!force_abort)
16723 did_emsg = save_did_emsg;
16724 }
16725 else if (eap->cmdidx == CMD_execute)
16726 do_cmdline((char_u *)ga.ga_data,
16727 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16728 }
16729
16730 ga_clear(&ga);
16731
16732 if (eap->skip)
16733 --emsg_skip;
16734
16735 eap->nextcmd = check_nextcmd(arg);
16736}
16737
16738/*
16739 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16740 * "arg" points to the "&" or '+' when called, to "option" when returning.
16741 * Returns NULL when no option name found. Otherwise pointer to the char
16742 * after the option name.
16743 */
16744 static char_u *
16745find_option_end(arg, opt_flags)
16746 char_u **arg;
16747 int *opt_flags;
16748{
16749 char_u *p = *arg;
16750
16751 ++p;
16752 if (*p == 'g' && p[1] == ':')
16753 {
16754 *opt_flags = OPT_GLOBAL;
16755 p += 2;
16756 }
16757 else if (*p == 'l' && p[1] == ':')
16758 {
16759 *opt_flags = OPT_LOCAL;
16760 p += 2;
16761 }
16762 else
16763 *opt_flags = 0;
16764
16765 if (!ASCII_ISALPHA(*p))
16766 return NULL;
16767 *arg = p;
16768
16769 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16770 p += 4; /* termcap option */
16771 else
16772 while (ASCII_ISALPHA(*p))
16773 ++p;
16774 return p;
16775}
16776
16777/*
16778 * ":function"
16779 */
16780 void
16781ex_function(eap)
16782 exarg_T *eap;
16783{
16784 char_u *theline;
16785 int j;
16786 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788 char_u *name = NULL;
16789 char_u *p;
16790 char_u *arg;
16791 garray_T newargs;
16792 garray_T newlines;
16793 int varargs = FALSE;
16794 int mustend = FALSE;
16795 int flags = 0;
16796 ufunc_T *fp;
16797 int indent;
16798 int nesting;
16799 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016800 dictitem_T *v;
16801 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016802 static int func_nr = 0; /* number for nameless function */
16803 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016804 hashtab_T *ht;
16805 int todo;
16806 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807
16808 /*
16809 * ":function" without argument: list functions.
16810 */
16811 if (ends_excmd(*eap->arg))
16812 {
16813 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016814 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016815 todo = func_hashtab.ht_used;
16816 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016817 {
16818 if (!HASHITEM_EMPTY(hi))
16819 {
16820 --todo;
16821 fp = HI2UF(hi);
16822 if (!isdigit(*fp->uf_name))
16823 list_func_head(fp, FALSE);
16824 }
16825 }
16826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 eap->nextcmd = check_nextcmd(eap->arg);
16828 return;
16829 }
16830
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016831 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016832 * ":function /pat": list functions matching pattern.
16833 */
16834 if (*eap->arg == '/')
16835 {
16836 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
16837 if (!eap->skip)
16838 {
16839 regmatch_T regmatch;
16840
16841 c = *p;
16842 *p = NUL;
16843 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
16844 *p = c;
16845 if (regmatch.regprog != NULL)
16846 {
16847 regmatch.rm_ic = p_ic;
16848
16849 todo = func_hashtab.ht_used;
16850 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
16851 {
16852 if (!HASHITEM_EMPTY(hi))
16853 {
16854 --todo;
16855 fp = HI2UF(hi);
16856 if (!isdigit(*fp->uf_name)
16857 && vim_regexec(&regmatch, fp->uf_name, 0))
16858 list_func_head(fp, FALSE);
16859 }
16860 }
16861 }
16862 }
16863 if (*p == '/')
16864 ++p;
16865 eap->nextcmd = check_nextcmd(p);
16866 return;
16867 }
16868
16869 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016870 * Get the function name. There are these situations:
16871 * func normal function name
16872 * "name" == func, "fudi.fd_dict" == NULL
16873 * dict.func new dictionary entry
16874 * "name" == NULL, "fudi.fd_dict" set,
16875 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16876 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016877 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016878 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16879 * dict.func existing dict entry that's not a Funcref
16880 * "name" == NULL, "fudi.fd_dict" set,
16881 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016884 name = trans_function_name(&p, eap->skip, 0, &fudi);
16885 paren = (vim_strchr(p, '(') != NULL);
16886 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887 {
16888 /*
16889 * Return on an invalid expression in braces, unless the expression
16890 * evaluation has been cancelled due to an aborting error, an
16891 * interrupt, or an exception.
16892 */
16893 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016894 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016895 if (!eap->skip && fudi.fd_newkey != NULL)
16896 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016897 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900 else
16901 eap->skip = TRUE;
16902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903 /* An error in a function call during evaluation of an expression in magic
16904 * braces should not cause the function not to be defined. */
16905 saved_did_emsg = did_emsg;
16906 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907
16908 /*
16909 * ":function func" with only function name: list function.
16910 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016911 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016912 {
16913 if (!ends_excmd(*skipwhite(p)))
16914 {
16915 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016916 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917 }
16918 eap->nextcmd = check_nextcmd(p);
16919 if (eap->nextcmd != NULL)
16920 *p = NUL;
16921 if (!eap->skip && !got_int)
16922 {
16923 fp = find_func(name);
16924 if (fp != NULL)
16925 {
16926 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016927 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 {
16929 msg_putchar('\n');
16930 msg_outnum((long)(j + 1));
16931 if (j < 9)
16932 msg_putchar(' ');
16933 if (j < 99)
16934 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016935 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936 out_flush(); /* show a line at a time */
16937 ui_breakcheck();
16938 }
16939 if (!got_int)
16940 {
16941 msg_putchar('\n');
16942 msg_puts((char_u *)" endfunction");
16943 }
16944 }
16945 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016946 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016948 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949 }
16950
16951 /*
16952 * ":function name(arg1, arg2)" Define function.
16953 */
16954 p = skipwhite(p);
16955 if (*p != '(')
16956 {
16957 if (!eap->skip)
16958 {
16959 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016960 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961 }
16962 /* attempt to continue by skipping some text */
16963 if (vim_strchr(p, '(') != NULL)
16964 p = vim_strchr(p, '(');
16965 }
16966 p = skipwhite(p + 1);
16967
16968 ga_init2(&newargs, (int)sizeof(char_u *), 3);
16969 ga_init2(&newlines, (int)sizeof(char_u *), 3);
16970
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016971 if (!eap->skip)
16972 {
16973 /* Check the name of the function. */
16974 if (name != NULL)
16975 arg = name;
16976 else
16977 arg = fudi.fd_newkey;
16978 if (arg != NULL)
16979 {
16980 if (*arg == K_SPECIAL)
16981 j = 3;
16982 else
16983 j = 0;
16984 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
16985 : eval_isnamec(arg[j])))
16986 ++j;
16987 if (arg[j] != NUL)
16988 emsg_funcname(_(e_invarg2), arg);
16989 }
16990 }
16991
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992 /*
16993 * Isolate the arguments: "arg1, arg2, ...)"
16994 */
16995 while (*p != ')')
16996 {
16997 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
16998 {
16999 varargs = TRUE;
17000 p += 3;
17001 mustend = TRUE;
17002 }
17003 else
17004 {
17005 arg = p;
17006 while (ASCII_ISALNUM(*p) || *p == '_')
17007 ++p;
17008 if (arg == p || isdigit(*arg)
17009 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17010 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17011 {
17012 if (!eap->skip)
17013 EMSG2(_("E125: Illegal argument: %s"), arg);
17014 break;
17015 }
17016 if (ga_grow(&newargs, 1) == FAIL)
17017 goto erret;
17018 c = *p;
17019 *p = NUL;
17020 arg = vim_strsave(arg);
17021 if (arg == NULL)
17022 goto erret;
17023 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17024 *p = c;
17025 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 if (*p == ',')
17027 ++p;
17028 else
17029 mustend = TRUE;
17030 }
17031 p = skipwhite(p);
17032 if (mustend && *p != ')')
17033 {
17034 if (!eap->skip)
17035 EMSG2(_(e_invarg2), eap->arg);
17036 break;
17037 }
17038 }
17039 ++p; /* skip the ')' */
17040
Bram Moolenaare9a41262005-01-15 22:18:47 +000017041 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 for (;;)
17043 {
17044 p = skipwhite(p);
17045 if (STRNCMP(p, "range", 5) == 0)
17046 {
17047 flags |= FC_RANGE;
17048 p += 5;
17049 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017050 else if (STRNCMP(p, "dict", 4) == 0)
17051 {
17052 flags |= FC_DICT;
17053 p += 4;
17054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055 else if (STRNCMP(p, "abort", 5) == 0)
17056 {
17057 flags |= FC_ABORT;
17058 p += 5;
17059 }
17060 else
17061 break;
17062 }
17063
17064 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
17065 EMSG(_(e_trailing));
17066
17067 /*
17068 * Read the body of the function, until ":endfunction" is found.
17069 */
17070 if (KeyTyped)
17071 {
17072 /* Check if the function already exists, don't let the user type the
17073 * whole function before telling him it doesn't work! For a script we
17074 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017075 if (!eap->skip && !eap->forceit)
17076 {
17077 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17078 EMSG(_(e_funcdict));
17079 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017080 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017083 if (!eap->skip && did_emsg)
17084 goto erret;
17085
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086 msg_putchar('\n'); /* don't overwrite the function name */
17087 cmdline_row = msg_row;
17088 }
17089
17090 indent = 2;
17091 nesting = 0;
17092 for (;;)
17093 {
17094 msg_scroll = TRUE;
17095 need_wait_return = FALSE;
17096 if (eap->getline == NULL)
17097 theline = getcmdline(':', 0L, indent);
17098 else
17099 theline = eap->getline(':', eap->cookie, indent);
17100 if (KeyTyped)
17101 lines_left = Rows - 1;
17102 if (theline == NULL)
17103 {
17104 EMSG(_("E126: Missing :endfunction"));
17105 goto erret;
17106 }
17107
17108 if (skip_until != NULL)
17109 {
17110 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17111 * don't check for ":endfunc". */
17112 if (STRCMP(theline, skip_until) == 0)
17113 {
17114 vim_free(skip_until);
17115 skip_until = NULL;
17116 }
17117 }
17118 else
17119 {
17120 /* skip ':' and blanks*/
17121 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17122 ;
17123
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017124 /* Check for "endfunction". */
17125 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126 {
17127 vim_free(theline);
17128 break;
17129 }
17130
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017131 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132 * at "end". */
17133 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17134 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017135 else if (STRNCMP(p, "if", 2) == 0
17136 || STRNCMP(p, "wh", 2) == 0
17137 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138 || STRNCMP(p, "try", 3) == 0)
17139 indent += 2;
17140
17141 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017142 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017144 if (*p == '!')
17145 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146 p += eval_fname_script(p);
17147 if (ASCII_ISALPHA(*p))
17148 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017149 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150 if (*skipwhite(p) == '(')
17151 {
17152 ++nesting;
17153 indent += 2;
17154 }
17155 }
17156 }
17157
17158 /* Check for ":append" or ":insert". */
17159 p = skip_range(p, NULL);
17160 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17161 || (p[0] == 'i'
17162 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17163 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17164 skip_until = vim_strsave((char_u *)".");
17165
17166 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17167 arg = skipwhite(skiptowhite(p));
17168 if (arg[0] == '<' && arg[1] =='<'
17169 && ((p[0] == 'p' && p[1] == 'y'
17170 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17171 || (p[0] == 'p' && p[1] == 'e'
17172 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17173 || (p[0] == 't' && p[1] == 'c'
17174 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17175 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17176 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017177 || (p[0] == 'm' && p[1] == 'z'
17178 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179 ))
17180 {
17181 /* ":python <<" continues until a dot, like ":append" */
17182 p = skipwhite(arg + 2);
17183 if (*p == NUL)
17184 skip_until = vim_strsave((char_u *)".");
17185 else
17186 skip_until = vim_strsave(p);
17187 }
17188 }
17189
17190 /* Add the line to the function. */
17191 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017192 {
17193 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017195 }
17196
17197 /* Copy the line to newly allocated memory. get_one_sourceline()
17198 * allocates 250 bytes per line, this saves 80% on average. The cost
17199 * is an extra alloc/free. */
17200 p = vim_strsave(theline);
17201 if (p != NULL)
17202 {
17203 vim_free(theline);
17204 theline = p;
17205 }
17206
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17208 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 }
17210
17211 /* Don't define the function when skipping commands or when an error was
17212 * detected. */
17213 if (eap->skip || did_emsg)
17214 goto erret;
17215
17216 /*
17217 * If there are no errors, add the function
17218 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017219 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017220 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017221 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017222 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017223 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017224 emsg_funcname("E707: Function name conflicts with variable: %s",
17225 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017226 goto erret;
17227 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017228
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017229 fp = find_func(name);
17230 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017232 if (!eap->forceit)
17233 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017234 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017235 goto erret;
17236 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017237 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017238 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017239 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017240 name);
17241 goto erret;
17242 }
17243 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017244 ga_clear_strings(&(fp->uf_args));
17245 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017246 vim_free(name);
17247 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 }
17250 else
17251 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017252 char numbuf[20];
17253
17254 fp = NULL;
17255 if (fudi.fd_newkey == NULL && !eap->forceit)
17256 {
17257 EMSG(_(e_funcdict));
17258 goto erret;
17259 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017260 if (fudi.fd_di == NULL)
17261 {
17262 /* Can't add a function to a locked dictionary */
17263 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17264 goto erret;
17265 }
17266 /* Can't change an existing function if it is locked */
17267 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17268 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017269
17270 /* Give the function a sequential number. Can only be used with a
17271 * Funcref! */
17272 vim_free(name);
17273 sprintf(numbuf, "%d", ++func_nr);
17274 name = vim_strsave((char_u *)numbuf);
17275 if (name == NULL)
17276 goto erret;
17277 }
17278
17279 if (fp == NULL)
17280 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017281 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017282 {
17283 int slen, plen;
17284 char_u *scriptname;
17285
17286 /* Check that the autoload name matches the script name. */
17287 j = FAIL;
17288 if (sourcing_name != NULL)
17289 {
17290 scriptname = autoload_name(name);
17291 if (scriptname != NULL)
17292 {
17293 p = vim_strchr(scriptname, '/');
17294 plen = STRLEN(p);
17295 slen = STRLEN(sourcing_name);
17296 if (slen > plen && fnamecmp(p,
17297 sourcing_name + slen - plen) == 0)
17298 j = OK;
17299 vim_free(scriptname);
17300 }
17301 }
17302 if (j == FAIL)
17303 {
17304 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17305 goto erret;
17306 }
17307 }
17308
17309 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310 if (fp == NULL)
17311 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017312
17313 if (fudi.fd_dict != NULL)
17314 {
17315 if (fudi.fd_di == NULL)
17316 {
17317 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017318 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017319 if (fudi.fd_di == NULL)
17320 {
17321 vim_free(fp);
17322 goto erret;
17323 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017324 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17325 {
17326 vim_free(fudi.fd_di);
17327 goto erret;
17328 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017329 }
17330 else
17331 /* overwrite existing dict entry */
17332 clear_tv(&fudi.fd_di->di_tv);
17333 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017334 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017335 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017336 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017337 }
17338
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017340 STRCPY(fp->uf_name, name);
17341 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017343 fp->uf_args = newargs;
17344 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017345#ifdef FEAT_PROFILE
17346 fp->uf_tml_count = NULL;
17347 fp->uf_tml_total = NULL;
17348 fp->uf_tml_self = NULL;
17349 fp->uf_profiling = FALSE;
17350 if (prof_def_func())
17351 func_do_profile(fp);
17352#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017353 fp->uf_varargs = varargs;
17354 fp->uf_flags = flags;
17355 fp->uf_calls = 0;
17356 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017357 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358
17359erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 ga_clear_strings(&newargs);
17361 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017362ret_free:
17363 vim_free(skip_until);
17364 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367}
17368
17369/*
17370 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017371 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017373 * flags:
17374 * TFN_INT: internal function name OK
17375 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 * Advances "pp" to just after the function name (if no error).
17377 */
17378 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017379trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380 char_u **pp;
17381 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017382 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017383 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017385 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386 char_u *start;
17387 char_u *end;
17388 int lead;
17389 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017391 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017392
17393 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017394 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017396
17397 /* Check for hard coded <SNR>: already translated function ID (from a user
17398 * command). */
17399 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17400 && (*pp)[2] == (int)KE_SNR)
17401 {
17402 *pp += 3;
17403 len = get_id_len(pp) + 3;
17404 return vim_strnsave(start, len);
17405 }
17406
17407 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17408 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017410 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017412
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017413 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17414 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017415 if (end == start)
17416 {
17417 if (!skip)
17418 EMSG(_("E129: Function name required"));
17419 goto theend;
17420 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017421 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017422 {
17423 /*
17424 * Report an invalid expression in braces, unless the expression
17425 * evaluation has been cancelled due to an aborting error, an
17426 * interrupt, or an exception.
17427 */
17428 if (!aborting())
17429 {
17430 if (end != NULL)
17431 EMSG2(_(e_invarg2), start);
17432 }
17433 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017434 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017435 goto theend;
17436 }
17437
17438 if (lv.ll_tv != NULL)
17439 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017440 if (fdp != NULL)
17441 {
17442 fdp->fd_dict = lv.ll_dict;
17443 fdp->fd_newkey = lv.ll_newkey;
17444 lv.ll_newkey = NULL;
17445 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017446 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017447 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17448 {
17449 name = vim_strsave(lv.ll_tv->vval.v_string);
17450 *pp = end;
17451 }
17452 else
17453 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017454 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17455 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017456 EMSG(_(e_funcref));
17457 else
17458 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017459 name = NULL;
17460 }
17461 goto theend;
17462 }
17463
17464 if (lv.ll_name == NULL)
17465 {
17466 /* Error found, but continue after the function name. */
17467 *pp = end;
17468 goto theend;
17469 }
17470
17471 if (lv.ll_exp_name != NULL)
17472 len = STRLEN(lv.ll_exp_name);
17473 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017474 {
17475 if (lead == 2) /* skip over "s:" */
17476 lv.ll_name += 2;
17477 len = (int)(end - lv.ll_name);
17478 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017479
17480 /*
17481 * Copy the function name to allocated memory.
17482 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17483 * Accept <SNR>123_name() outside a script.
17484 */
17485 if (skip)
17486 lead = 0; /* do nothing */
17487 else if (lead > 0)
17488 {
17489 lead = 3;
17490 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17491 {
17492 if (current_SID <= 0)
17493 {
17494 EMSG(_(e_usingsid));
17495 goto theend;
17496 }
17497 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17498 lead += (int)STRLEN(sid_buf);
17499 }
17500 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017501 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017502 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017503 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017504 goto theend;
17505 }
17506 name = alloc((unsigned)(len + lead + 1));
17507 if (name != NULL)
17508 {
17509 if (lead > 0)
17510 {
17511 name[0] = K_SPECIAL;
17512 name[1] = KS_EXTRA;
17513 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017514 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017515 STRCPY(name + 3, sid_buf);
17516 }
17517 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17518 name[len + lead] = NUL;
17519 }
17520 *pp = end;
17521
17522theend:
17523 clear_lval(&lv);
17524 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525}
17526
17527/*
17528 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17529 * Return 2 if "p" starts with "s:".
17530 * Return 0 otherwise.
17531 */
17532 static int
17533eval_fname_script(p)
17534 char_u *p;
17535{
17536 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17537 || STRNICMP(p + 1, "SNR>", 4) == 0))
17538 return 5;
17539 if (p[0] == 's' && p[1] == ':')
17540 return 2;
17541 return 0;
17542}
17543
17544/*
17545 * Return TRUE if "p" starts with "<SID>" or "s:".
17546 * Only works if eval_fname_script() returned non-zero for "p"!
17547 */
17548 static int
17549eval_fname_sid(p)
17550 char_u *p;
17551{
17552 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17553}
17554
17555/*
17556 * List the head of the function: "name(arg1, arg2)".
17557 */
17558 static void
17559list_func_head(fp, indent)
17560 ufunc_T *fp;
17561 int indent;
17562{
17563 int j;
17564
17565 msg_start();
17566 if (indent)
17567 MSG_PUTS(" ");
17568 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017569 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570 {
17571 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017572 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 }
17574 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017575 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017577 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 {
17579 if (j)
17580 MSG_PUTS(", ");
17581 msg_puts(FUNCARG(fp, j));
17582 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017583 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584 {
17585 if (j)
17586 MSG_PUTS(", ");
17587 MSG_PUTS("...");
17588 }
17589 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000017590 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000017591#ifdef FEAT_EVAL
17592 if (p_verbose > 0)
17593 last_set_msg(fp->uf_script_ID);
17594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017595}
17596
17597/*
17598 * Find a function by name, return pointer to it in ufuncs.
17599 * Return NULL for unknown function.
17600 */
17601 static ufunc_T *
17602find_func(name)
17603 char_u *name;
17604{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017605 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017607 hi = hash_find(&func_hashtab, name);
17608 if (!HASHITEM_EMPTY(hi))
17609 return HI2UF(hi);
17610 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611}
17612
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017613#if defined(EXITFREE) || defined(PROTO)
17614 void
17615free_all_functions()
17616{
17617 hashitem_T *hi;
17618
17619 /* Need to start all over every time, because func_free() may change the
17620 * hash table. */
17621 while (func_hashtab.ht_used > 0)
17622 for (hi = func_hashtab.ht_array; ; ++hi)
17623 if (!HASHITEM_EMPTY(hi))
17624 {
17625 func_free(HI2UF(hi));
17626 break;
17627 }
17628}
17629#endif
17630
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017631/*
17632 * Return TRUE if a function "name" exists.
17633 */
17634 static int
17635function_exists(name)
17636 char_u *name;
17637{
17638 char_u *p = name;
17639 int n = FALSE;
17640
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017641 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017642 if (p != NULL)
17643 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017644 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017645 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017646 else
17647 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017648 vim_free(p);
17649 }
17650 return n;
17651}
17652
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017653/*
17654 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017655 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017656 */
17657 static int
17658builtin_function(name)
17659 char_u *name;
17660{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017661 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17662 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017663}
17664
Bram Moolenaar05159a02005-02-26 23:04:13 +000017665#if defined(FEAT_PROFILE) || defined(PROTO)
17666/*
17667 * Start profiling function "fp".
17668 */
17669 static void
17670func_do_profile(fp)
17671 ufunc_T *fp;
17672{
17673 fp->uf_tm_count = 0;
17674 profile_zero(&fp->uf_tm_self);
17675 profile_zero(&fp->uf_tm_total);
17676 if (fp->uf_tml_count == NULL)
17677 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17678 (sizeof(int) * fp->uf_lines.ga_len));
17679 if (fp->uf_tml_total == NULL)
17680 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17681 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17682 if (fp->uf_tml_self == NULL)
17683 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17684 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17685 fp->uf_tml_idx = -1;
17686 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17687 || fp->uf_tml_self == NULL)
17688 return; /* out of memory */
17689
17690 fp->uf_profiling = TRUE;
17691}
17692
17693/*
17694 * Dump the profiling results for all functions in file "fd".
17695 */
17696 void
17697func_dump_profile(fd)
17698 FILE *fd;
17699{
17700 hashitem_T *hi;
17701 int todo;
17702 ufunc_T *fp;
17703 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017704 ufunc_T **sorttab;
17705 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017706
17707 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017708 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17709
Bram Moolenaar05159a02005-02-26 23:04:13 +000017710 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17711 {
17712 if (!HASHITEM_EMPTY(hi))
17713 {
17714 --todo;
17715 fp = HI2UF(hi);
17716 if (fp->uf_profiling)
17717 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017718 if (sorttab != NULL)
17719 sorttab[st_len++] = fp;
17720
Bram Moolenaar05159a02005-02-26 23:04:13 +000017721 if (fp->uf_name[0] == K_SPECIAL)
17722 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17723 else
17724 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17725 if (fp->uf_tm_count == 1)
17726 fprintf(fd, "Called 1 time\n");
17727 else
17728 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17729 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17730 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17731 fprintf(fd, "\n");
17732 fprintf(fd, "count total (s) self (s)\n");
17733
17734 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17735 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017736 prof_func_line(fd, fp->uf_tml_count[i],
17737 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017738 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17739 }
17740 fprintf(fd, "\n");
17741 }
17742 }
17743 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017744
17745 if (sorttab != NULL && st_len > 0)
17746 {
17747 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17748 prof_total_cmp);
17749 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17750 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17751 prof_self_cmp);
17752 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17753 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017754}
Bram Moolenaar73830342005-02-28 22:48:19 +000017755
17756 static void
17757prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17758 FILE *fd;
17759 ufunc_T **sorttab;
17760 int st_len;
17761 char *title;
17762 int prefer_self; /* when equal print only self time */
17763{
17764 int i;
17765 ufunc_T *fp;
17766
17767 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17768 fprintf(fd, "count total (s) self (s) function\n");
17769 for (i = 0; i < 20 && i < st_len; ++i)
17770 {
17771 fp = sorttab[i];
17772 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17773 prefer_self);
17774 if (fp->uf_name[0] == K_SPECIAL)
17775 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17776 else
17777 fprintf(fd, " %s()\n", fp->uf_name);
17778 }
17779 fprintf(fd, "\n");
17780}
17781
17782/*
17783 * Print the count and times for one function or function line.
17784 */
17785 static void
17786prof_func_line(fd, count, total, self, prefer_self)
17787 FILE *fd;
17788 int count;
17789 proftime_T *total;
17790 proftime_T *self;
17791 int prefer_self; /* when equal print only self time */
17792{
17793 if (count > 0)
17794 {
17795 fprintf(fd, "%5d ", count);
17796 if (prefer_self && profile_equal(total, self))
17797 fprintf(fd, " ");
17798 else
17799 fprintf(fd, "%s ", profile_msg(total));
17800 if (!prefer_self && profile_equal(total, self))
17801 fprintf(fd, " ");
17802 else
17803 fprintf(fd, "%s ", profile_msg(self));
17804 }
17805 else
17806 fprintf(fd, " ");
17807}
17808
17809/*
17810 * Compare function for total time sorting.
17811 */
17812 static int
17813#ifdef __BORLANDC__
17814_RTLENTRYF
17815#endif
17816prof_total_cmp(s1, s2)
17817 const void *s1;
17818 const void *s2;
17819{
17820 ufunc_T *p1, *p2;
17821
17822 p1 = *(ufunc_T **)s1;
17823 p2 = *(ufunc_T **)s2;
17824 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17825}
17826
17827/*
17828 * Compare function for self time sorting.
17829 */
17830 static int
17831#ifdef __BORLANDC__
17832_RTLENTRYF
17833#endif
17834prof_self_cmp(s1, s2)
17835 const void *s1;
17836 const void *s2;
17837{
17838 ufunc_T *p1, *p2;
17839
17840 p1 = *(ufunc_T **)s1;
17841 p2 = *(ufunc_T **)s2;
17842 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17843}
17844
Bram Moolenaar05159a02005-02-26 23:04:13 +000017845#endif
17846
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017847/* The names of packages that once were loaded is remembered. */
17848static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
17849
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017850/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017851 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017852 * Return TRUE if a package was loaded.
17853 */
17854 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017855script_autoload(name, reload)
17856 char_u *name;
17857 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017858{
17859 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017860 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017861 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017862 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017863
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017864 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017865 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017866 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017867 return FALSE;
17868
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017869 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017870
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017871 /* Find the name in the list of previously loaded package names. Skip
17872 * "autoload/", it's always the same. */
17873 for (i = 0; i < ga_loaded.ga_len; ++i)
17874 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
17875 break;
17876 if (!reload && i < ga_loaded.ga_len)
17877 ret = FALSE; /* was loaded already */
17878 else
17879 {
17880 /* Remember the name if it wasn't loaded already. */
17881 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
17882 {
17883 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
17884 tofree = NULL;
17885 }
17886
17887 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000017888 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017889 ret = TRUE;
17890 }
17891
17892 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017893 return ret;
17894}
17895
17896/*
17897 * Return the autoload script name for a function or variable name.
17898 * Returns NULL when out of memory.
17899 */
17900 static char_u *
17901autoload_name(name)
17902 char_u *name;
17903{
17904 char_u *p;
17905 char_u *scriptname;
17906
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017907 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017908 scriptname = alloc((unsigned)(STRLEN(name) + 14));
17909 if (scriptname == NULL)
17910 return FALSE;
17911 STRCPY(scriptname, "autoload/");
17912 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017913 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017914 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017915 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017916 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017917 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017918}
17919
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
17921
17922/*
17923 * Function given to ExpandGeneric() to obtain the list of user defined
17924 * function names.
17925 */
17926 char_u *
17927get_user_func_name(xp, idx)
17928 expand_T *xp;
17929 int idx;
17930{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017931 static long_u done;
17932 static hashitem_T *hi;
17933 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934
17935 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017937 done = 0;
17938 hi = func_hashtab.ht_array;
17939 }
17940 if (done < func_hashtab.ht_used)
17941 {
17942 if (done++ > 0)
17943 ++hi;
17944 while (HASHITEM_EMPTY(hi))
17945 ++hi;
17946 fp = HI2UF(hi);
17947
17948 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
17949 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017950
17951 cat_func_name(IObuff, fp);
17952 if (xp->xp_context != EXPAND_USER_FUNC)
17953 {
17954 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017955 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956 STRCAT(IObuff, ")");
17957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958 return IObuff;
17959 }
17960 return NULL;
17961}
17962
17963#endif /* FEAT_CMDL_COMPL */
17964
17965/*
17966 * Copy the function name of "fp" to buffer "buf".
17967 * "buf" must be able to hold the function name plus three bytes.
17968 * Takes care of script-local function names.
17969 */
17970 static void
17971cat_func_name(buf, fp)
17972 char_u *buf;
17973 ufunc_T *fp;
17974{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017975 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976 {
17977 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017978 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979 }
17980 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017981 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982}
17983
17984/*
17985 * ":delfunction {name}"
17986 */
17987 void
17988ex_delfunction(eap)
17989 exarg_T *eap;
17990{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017991 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992 char_u *p;
17993 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017994 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995
17996 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017997 name = trans_function_name(&p, eap->skip, 0, &fudi);
17998 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017999 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018000 {
18001 if (fudi.fd_dict != NULL && !eap->skip)
18002 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018003 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005 if (!ends_excmd(*skipwhite(p)))
18006 {
18007 vim_free(name);
18008 EMSG(_(e_trailing));
18009 return;
18010 }
18011 eap->nextcmd = check_nextcmd(p);
18012 if (eap->nextcmd != NULL)
18013 *p = NUL;
18014
18015 if (!eap->skip)
18016 fp = find_func(name);
18017 vim_free(name);
18018
18019 if (!eap->skip)
18020 {
18021 if (fp == NULL)
18022 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018023 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024 return;
18025 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018026 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027 {
18028 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18029 return;
18030 }
18031
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018032 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018034 /* Delete the dict item that refers to the function, it will
18035 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018036 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018038 else
18039 func_free(fp);
18040 }
18041}
18042
18043/*
18044 * Free a function and remove it from the list of functions.
18045 */
18046 static void
18047func_free(fp)
18048 ufunc_T *fp;
18049{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018050 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018051
18052 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018053 ga_clear_strings(&(fp->uf_args));
18054 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018055#ifdef FEAT_PROFILE
18056 vim_free(fp->uf_tml_count);
18057 vim_free(fp->uf_tml_total);
18058 vim_free(fp->uf_tml_self);
18059#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018060
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018061 /* remove the function from the function hashtable */
18062 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18063 if (HASHITEM_EMPTY(hi))
18064 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018065 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018066 hash_remove(&func_hashtab, hi);
18067
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018068 vim_free(fp);
18069}
18070
18071/*
18072 * Unreference a Function: decrement the reference count and free it when it
18073 * becomes zero. Only for numbered functions.
18074 */
18075 static void
18076func_unref(name)
18077 char_u *name;
18078{
18079 ufunc_T *fp;
18080
18081 if (name != NULL && isdigit(*name))
18082 {
18083 fp = find_func(name);
18084 if (fp == NULL)
18085 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018086 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018087 {
18088 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018089 * when "uf_calls" becomes zero. */
18090 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018091 func_free(fp);
18092 }
18093 }
18094}
18095
18096/*
18097 * Count a reference to a Function.
18098 */
18099 static void
18100func_ref(name)
18101 char_u *name;
18102{
18103 ufunc_T *fp;
18104
18105 if (name != NULL && isdigit(*name))
18106 {
18107 fp = find_func(name);
18108 if (fp == NULL)
18109 EMSG2(_(e_intern2), "func_ref()");
18110 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018111 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112 }
18113}
18114
18115/*
18116 * Call a user function.
18117 */
18118 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018119call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120 ufunc_T *fp; /* pointer to function */
18121 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018122 typval_T *argvars; /* arguments */
18123 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124 linenr_T firstline; /* first line of range */
18125 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018126 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127{
Bram Moolenaar33570922005-01-25 22:26:29 +000018128 char_u *save_sourcing_name;
18129 linenr_T save_sourcing_lnum;
18130 scid_T save_current_SID;
18131 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018132 int save_did_emsg;
18133 static int depth = 0;
18134 dictitem_T *v;
18135 int fixvar_idx = 0; /* index in fixvar[] */
18136 int i;
18137 int ai;
18138 char_u numbuf[NUMBUFLEN];
18139 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018140#ifdef FEAT_PROFILE
18141 proftime_T wait_start;
18142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143
18144 /* If depth of calling is getting too high, don't execute the function */
18145 if (depth >= p_mfd)
18146 {
18147 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018148 rettv->v_type = VAR_NUMBER;
18149 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150 return;
18151 }
18152 ++depth;
18153
18154 line_breakcheck(); /* check for CTRL-C hit */
18155
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018156 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018157 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018159 fc.rettv = rettv;
18160 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 fc.linenr = 0;
18162 fc.returned = FALSE;
18163 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018165 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166 fc.dbg_tick = debug_tick;
18167
Bram Moolenaar33570922005-01-25 22:26:29 +000018168 /*
18169 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18170 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18171 * each argument variable and saves a lot of time.
18172 */
18173 /*
18174 * Init l: variables.
18175 */
18176 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018177 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018178 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018179 /* Set l:self to "selfdict". */
18180 v = &fc.fixvar[fixvar_idx++].var;
18181 STRCPY(v->di_key, "self");
18182 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18183 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18184 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018185 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018186 v->di_tv.vval.v_dict = selfdict;
18187 ++selfdict->dv_refcount;
18188 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018189
Bram Moolenaar33570922005-01-25 22:26:29 +000018190 /*
18191 * Init a: variables.
18192 * Set a:0 to "argcount".
18193 * Set a:000 to a list with room for the "..." arguments.
18194 */
18195 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18196 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018197 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018198 v = &fc.fixvar[fixvar_idx++].var;
18199 STRCPY(v->di_key, "000");
18200 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18201 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18202 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018203 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018204 v->di_tv.vval.v_list = &fc.l_varlist;
18205 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18206 fc.l_varlist.lv_refcount = 99999;
18207
18208 /*
18209 * Set a:firstline to "firstline" and a:lastline to "lastline".
18210 * Set a:name to named arguments.
18211 * Set a:N to the "..." arguments.
18212 */
18213 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18214 (varnumber_T)firstline);
18215 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18216 (varnumber_T)lastline);
18217 for (i = 0; i < argcount; ++i)
18218 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018219 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018220 if (ai < 0)
18221 /* named argument a:name */
18222 name = FUNCARG(fp, i);
18223 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018224 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018225 /* "..." argument a:1, a:2, etc. */
18226 sprintf((char *)numbuf, "%d", ai + 1);
18227 name = numbuf;
18228 }
18229 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18230 {
18231 v = &fc.fixvar[fixvar_idx++].var;
18232 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18233 }
18234 else
18235 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018236 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18237 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018238 if (v == NULL)
18239 break;
18240 v->di_flags = DI_FLAGS_RO;
18241 }
18242 STRCPY(v->di_key, name);
18243 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18244
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018245 /* Note: the values are copied directly to avoid alloc/free.
18246 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018247 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018248 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018249
18250 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18251 {
18252 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18253 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018254 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018255 }
18256 }
18257
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 /* Don't redraw while executing the function. */
18259 ++RedrawingDisabled;
18260 save_sourcing_name = sourcing_name;
18261 save_sourcing_lnum = sourcing_lnum;
18262 sourcing_lnum = 1;
18263 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018264 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 if (sourcing_name != NULL)
18266 {
18267 if (save_sourcing_name != NULL
18268 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18269 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18270 else
18271 STRCPY(sourcing_name, "function ");
18272 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18273
18274 if (p_verbose >= 12)
18275 {
18276 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018277 verbose_enter_scroll();
18278
Bram Moolenaar555b2802005-05-19 21:08:39 +000018279 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018280 if (p_verbose >= 14)
18281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018283 char_u numbuf[NUMBUFLEN];
18284 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018285
18286 msg_puts((char_u *)"(");
18287 for (i = 0; i < argcount; ++i)
18288 {
18289 if (i > 0)
18290 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018291 if (argvars[i].v_type == VAR_NUMBER)
18292 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293 else
18294 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018295 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018296 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018298 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299 }
18300 }
18301 msg_puts((char_u *)")");
18302 }
18303 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018304
18305 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306 --no_wait_return;
18307 }
18308 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018309#ifdef FEAT_PROFILE
18310 if (do_profiling)
18311 {
18312 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18313 func_do_profile(fp);
18314 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018315 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018316 {
18317 ++fp->uf_tm_count;
18318 profile_start(&fp->uf_tm_start);
18319 profile_zero(&fp->uf_tm_children);
18320 }
18321 script_prof_save(&wait_start);
18322 }
18323#endif
18324
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018326 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 save_did_emsg = did_emsg;
18328 did_emsg = FALSE;
18329
18330 /* call do_cmdline() to execute the lines */
18331 do_cmdline(NULL, get_func_line, (void *)&fc,
18332 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18333
18334 --RedrawingDisabled;
18335
18336 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018337 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018339 clear_tv(rettv);
18340 rettv->v_type = VAR_NUMBER;
18341 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 }
18343
Bram Moolenaar05159a02005-02-26 23:04:13 +000018344#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018345 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018346 {
18347 profile_end(&fp->uf_tm_start);
18348 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18349 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18350 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18351 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018352 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018353 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018354 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18355 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018356 }
18357 }
18358#endif
18359
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360 /* when being verbose, mention the return value */
18361 if (p_verbose >= 12)
18362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018364 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018367 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018368 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018369 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18370 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018371 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018373 char_u buf[MSG_BUF_LEN];
18374 char_u numbuf[NUMBUFLEN];
18375 char_u *tofree;
18376
Bram Moolenaar555b2802005-05-19 21:08:39 +000018377 /* The value may be very long. Skip the middle part, so that we
18378 * have some idea how it starts and ends. smsg() would always
18379 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018380 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018381 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018382 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018383 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 }
18385 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018386
18387 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388 --no_wait_return;
18389 }
18390
18391 vim_free(sourcing_name);
18392 sourcing_name = save_sourcing_name;
18393 sourcing_lnum = save_sourcing_lnum;
18394 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018395#ifdef FEAT_PROFILE
18396 if (do_profiling)
18397 script_prof_restore(&wait_start);
18398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399
18400 if (p_verbose >= 12 && sourcing_name != NULL)
18401 {
18402 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018403 verbose_enter_scroll();
18404
Bram Moolenaar555b2802005-05-19 21:08:39 +000018405 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018407
18408 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409 --no_wait_return;
18410 }
18411
18412 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018413 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414
Bram Moolenaar33570922005-01-25 22:26:29 +000018415 /* The a: variables typevals were not alloced, only free the allocated
18416 * variables. */
18417 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18418
18419 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018420 --depth;
18421}
18422
18423/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018424 * Add a number variable "name" to dict "dp" with value "nr".
18425 */
18426 static void
18427add_nr_var(dp, v, name, nr)
18428 dict_T *dp;
18429 dictitem_T *v;
18430 char *name;
18431 varnumber_T nr;
18432{
18433 STRCPY(v->di_key, name);
18434 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18435 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18436 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018437 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018438 v->di_tv.vval.v_number = nr;
18439}
18440
18441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018442 * ":return [expr]"
18443 */
18444 void
18445ex_return(eap)
18446 exarg_T *eap;
18447{
18448 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018449 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450 int returning = FALSE;
18451
18452 if (current_funccal == NULL)
18453 {
18454 EMSG(_("E133: :return not inside a function"));
18455 return;
18456 }
18457
18458 if (eap->skip)
18459 ++emsg_skip;
18460
18461 eap->nextcmd = NULL;
18462 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018463 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464 {
18465 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018466 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018467 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018468 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018469 }
18470 /* It's safer to return also on error. */
18471 else if (!eap->skip)
18472 {
18473 /*
18474 * Return unless the expression evaluation has been cancelled due to an
18475 * aborting error, an interrupt, or an exception.
18476 */
18477 if (!aborting())
18478 returning = do_return(eap, FALSE, TRUE, NULL);
18479 }
18480
18481 /* When skipping or the return gets pending, advance to the next command
18482 * in this line (!returning). Otherwise, ignore the rest of the line.
18483 * Following lines will be ignored by get_func_line(). */
18484 if (returning)
18485 eap->nextcmd = NULL;
18486 else if (eap->nextcmd == NULL) /* no argument */
18487 eap->nextcmd = check_nextcmd(arg);
18488
18489 if (eap->skip)
18490 --emsg_skip;
18491}
18492
18493/*
18494 * Return from a function. Possibly makes the return pending. Also called
18495 * for a pending return at the ":endtry" or after returning from an extra
18496 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018497 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018498 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499 * FALSE when the return gets pending.
18500 */
18501 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018502do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018503 exarg_T *eap;
18504 int reanimate;
18505 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018506 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018507{
18508 int idx;
18509 struct condstack *cstack = eap->cstack;
18510
18511 if (reanimate)
18512 /* Undo the return. */
18513 current_funccal->returned = FALSE;
18514
18515 /*
18516 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18517 * not in its finally clause (which then is to be executed next) is found.
18518 * In this case, make the ":return" pending for execution at the ":endtry".
18519 * Otherwise, return normally.
18520 */
18521 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18522 if (idx >= 0)
18523 {
18524 cstack->cs_pending[idx] = CSTP_RETURN;
18525
18526 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018527 /* A pending return again gets pending. "rettv" points to an
18528 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018530 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531 else
18532 {
18533 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018534 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018536 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018538 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 {
18540 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018541 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018542 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018543 else
18544 EMSG(_(e_outofmem));
18545 }
18546 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018547 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548
18549 if (reanimate)
18550 {
18551 /* The pending return value could be overwritten by a ":return"
18552 * without argument in a finally clause; reset the default
18553 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018554 current_funccal->rettv->v_type = VAR_NUMBER;
18555 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556 }
18557 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018558 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 }
18560 else
18561 {
18562 current_funccal->returned = TRUE;
18563
18564 /* If the return is carried out now, store the return value. For
18565 * a return immediately after reanimation, the value is already
18566 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018567 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018569 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018570 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018572 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 }
18574 }
18575
18576 return idx < 0;
18577}
18578
18579/*
18580 * Free the variable with a pending return value.
18581 */
18582 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018583discard_pending_return(rettv)
18584 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585{
Bram Moolenaar33570922005-01-25 22:26:29 +000018586 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587}
18588
18589/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018590 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591 * is an allocated string. Used by report_pending() for verbose messages.
18592 */
18593 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018594get_return_cmd(rettv)
18595 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018597 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018598 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018599 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018601 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018602 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018603 if (s == NULL)
18604 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018605
18606 STRCPY(IObuff, ":return ");
18607 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18608 if (STRLEN(s) + 8 >= IOSIZE)
18609 STRCPY(IObuff + IOSIZE - 4, "...");
18610 vim_free(tofree);
18611 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612}
18613
18614/*
18615 * Get next function line.
18616 * Called by do_cmdline() to get the next line.
18617 * Returns allocated string, or NULL for end of function.
18618 */
18619/* ARGSUSED */
18620 char_u *
18621get_func_line(c, cookie, indent)
18622 int c; /* not used */
18623 void *cookie;
18624 int indent; /* not used */
18625{
Bram Moolenaar33570922005-01-25 22:26:29 +000018626 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018627 ufunc_T *fp = fcp->func;
18628 char_u *retval;
18629 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630
18631 /* If breakpoints have been added/deleted need to check for it. */
18632 if (fcp->dbg_tick != debug_tick)
18633 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018634 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635 sourcing_lnum);
18636 fcp->dbg_tick = debug_tick;
18637 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018638#ifdef FEAT_PROFILE
18639 if (do_profiling)
18640 func_line_end(cookie);
18641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642
Bram Moolenaar05159a02005-02-26 23:04:13 +000018643 gap = &fp->uf_lines;
18644 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645 retval = NULL;
18646 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18647 retval = NULL;
18648 else
18649 {
18650 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18651 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018652#ifdef FEAT_PROFILE
18653 if (do_profiling)
18654 func_line_start(cookie);
18655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 }
18657
18658 /* Did we encounter a breakpoint? */
18659 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18660 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018661 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018663 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 sourcing_lnum);
18665 fcp->dbg_tick = debug_tick;
18666 }
18667
18668 return retval;
18669}
18670
Bram Moolenaar05159a02005-02-26 23:04:13 +000018671#if defined(FEAT_PROFILE) || defined(PROTO)
18672/*
18673 * Called when starting to read a function line.
18674 * "sourcing_lnum" must be correct!
18675 * When skipping lines it may not actually be executed, but we won't find out
18676 * until later and we need to store the time now.
18677 */
18678 void
18679func_line_start(cookie)
18680 void *cookie;
18681{
18682 funccall_T *fcp = (funccall_T *)cookie;
18683 ufunc_T *fp = fcp->func;
18684
18685 if (fp->uf_profiling && sourcing_lnum >= 1
18686 && sourcing_lnum <= fp->uf_lines.ga_len)
18687 {
18688 fp->uf_tml_idx = sourcing_lnum - 1;
18689 fp->uf_tml_execed = FALSE;
18690 profile_start(&fp->uf_tml_start);
18691 profile_zero(&fp->uf_tml_children);
18692 profile_get_wait(&fp->uf_tml_wait);
18693 }
18694}
18695
18696/*
18697 * Called when actually executing a function line.
18698 */
18699 void
18700func_line_exec(cookie)
18701 void *cookie;
18702{
18703 funccall_T *fcp = (funccall_T *)cookie;
18704 ufunc_T *fp = fcp->func;
18705
18706 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18707 fp->uf_tml_execed = TRUE;
18708}
18709
18710/*
18711 * Called when done with a function line.
18712 */
18713 void
18714func_line_end(cookie)
18715 void *cookie;
18716{
18717 funccall_T *fcp = (funccall_T *)cookie;
18718 ufunc_T *fp = fcp->func;
18719
18720 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18721 {
18722 if (fp->uf_tml_execed)
18723 {
18724 ++fp->uf_tml_count[fp->uf_tml_idx];
18725 profile_end(&fp->uf_tml_start);
18726 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18727 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18728 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18729 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18730 }
18731 fp->uf_tml_idx = -1;
18732 }
18733}
18734#endif
18735
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736/*
18737 * Return TRUE if the currently active function should be ended, because a
18738 * return was encountered or an error occured. Used inside a ":while".
18739 */
18740 int
18741func_has_ended(cookie)
18742 void *cookie;
18743{
Bram Moolenaar33570922005-01-25 22:26:29 +000018744 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745
18746 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18747 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018748 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749 || fcp->returned);
18750}
18751
18752/*
18753 * return TRUE if cookie indicates a function which "abort"s on errors.
18754 */
18755 int
18756func_has_abort(cookie)
18757 void *cookie;
18758{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018759 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760}
18761
18762#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18763typedef enum
18764{
18765 VAR_FLAVOUR_DEFAULT,
18766 VAR_FLAVOUR_SESSION,
18767 VAR_FLAVOUR_VIMINFO
18768} var_flavour_T;
18769
18770static var_flavour_T var_flavour __ARGS((char_u *varname));
18771
18772 static var_flavour_T
18773var_flavour(varname)
18774 char_u *varname;
18775{
18776 char_u *p = varname;
18777
18778 if (ASCII_ISUPPER(*p))
18779 {
18780 while (*(++p))
18781 if (ASCII_ISLOWER(*p))
18782 return VAR_FLAVOUR_SESSION;
18783 return VAR_FLAVOUR_VIMINFO;
18784 }
18785 else
18786 return VAR_FLAVOUR_DEFAULT;
18787}
18788#endif
18789
18790#if defined(FEAT_VIMINFO) || defined(PROTO)
18791/*
18792 * Restore global vars that start with a capital from the viminfo file
18793 */
18794 int
18795read_viminfo_varlist(virp, writing)
18796 vir_T *virp;
18797 int writing;
18798{
18799 char_u *tab;
18800 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018801 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802
18803 if (!writing && (find_viminfo_parameter('!') != NULL))
18804 {
18805 tab = vim_strchr(virp->vir_line + 1, '\t');
18806 if (tab != NULL)
18807 {
18808 *tab++ = '\0'; /* isolate the variable name */
18809 if (*tab == 'S') /* string var */
18810 is_string = TRUE;
18811
18812 tab = vim_strchr(tab, '\t');
18813 if (tab != NULL)
18814 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815 if (is_string)
18816 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018817 tv.v_type = VAR_STRING;
18818 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 }
18821 else
18822 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018823 tv.v_type = VAR_NUMBER;
18824 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018826 set_var(virp->vir_line + 1, &tv, FALSE);
18827 if (is_string)
18828 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829 }
18830 }
18831 }
18832
18833 return viminfo_readline(virp);
18834}
18835
18836/*
18837 * Write global vars that start with a capital to the viminfo file
18838 */
18839 void
18840write_viminfo_varlist(fp)
18841 FILE *fp;
18842{
Bram Moolenaar33570922005-01-25 22:26:29 +000018843 hashitem_T *hi;
18844 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018845 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018846 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018847 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018848 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018849 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850
18851 if (find_viminfo_parameter('!') == NULL)
18852 return;
18853
18854 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018855
Bram Moolenaar33570922005-01-25 22:26:29 +000018856 todo = globvarht.ht_used;
18857 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018859 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018861 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018862 this_var = HI2DI(hi);
18863 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018864 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018865 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018866 {
18867 case VAR_STRING: s = "STR"; break;
18868 case VAR_NUMBER: s = "NUM"; break;
18869 default: continue;
18870 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018871 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018872 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18873 if (p != NULL)
18874 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018875 vim_free(tofree);
18876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877 }
18878 }
18879}
18880#endif
18881
18882#if defined(FEAT_SESSION) || defined(PROTO)
18883 int
18884store_session_globals(fd)
18885 FILE *fd;
18886{
Bram Moolenaar33570922005-01-25 22:26:29 +000018887 hashitem_T *hi;
18888 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018889 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890 char_u *p, *t;
18891
Bram Moolenaar33570922005-01-25 22:26:29 +000018892 todo = globvarht.ht_used;
18893 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018895 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018897 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018898 this_var = HI2DI(hi);
18899 if ((this_var->di_tv.v_type == VAR_NUMBER
18900 || this_var->di_tv.v_type == VAR_STRING)
18901 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018902 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018903 /* Escape special characters with a backslash. Turn a LF and
18904 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018905 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018906 (char_u *)"\\\"\n\r");
18907 if (p == NULL) /* out of memory */
18908 break;
18909 for (t = p; *t != NUL; ++t)
18910 if (*t == '\n')
18911 *t = 'n';
18912 else if (*t == '\r')
18913 *t = 'r';
18914 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000018915 this_var->di_key,
18916 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18917 : ' ',
18918 p,
18919 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18920 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000018921 || put_eol(fd) == FAIL)
18922 {
18923 vim_free(p);
18924 return FAIL;
18925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926 vim_free(p);
18927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 }
18929 }
18930 return OK;
18931}
18932#endif
18933
Bram Moolenaar661b1822005-07-28 22:36:45 +000018934/*
18935 * Display script name where an item was last set.
18936 * Should only be invoked when 'verbose' is non-zero.
18937 */
18938 void
18939last_set_msg(scriptID)
18940 scid_T scriptID;
18941{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018942 char_u *p;
18943
Bram Moolenaar661b1822005-07-28 22:36:45 +000018944 if (scriptID != 0)
18945 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018946 p = home_replace_save(NULL, get_scriptname(scriptID));
18947 if (p != NULL)
18948 {
18949 verbose_enter();
18950 MSG_PUTS(_("\n\tLast set from "));
18951 MSG_PUTS(p);
18952 vim_free(p);
18953 verbose_leave();
18954 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000018955 }
18956}
18957
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958#endif /* FEAT_EVAL */
18959
18960#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
18961
18962
18963#ifdef WIN3264
18964/*
18965 * Functions for ":8" filename modifier: get 8.3 version of a filename.
18966 */
18967static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18968static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
18969static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18970
18971/*
18972 * Get the short pathname of a file.
18973 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
18974 */
18975 static int
18976get_short_pathname(fnamep, bufp, fnamelen)
18977 char_u **fnamep;
18978 char_u **bufp;
18979 int *fnamelen;
18980{
18981 int l,len;
18982 char_u *newbuf;
18983
18984 len = *fnamelen;
18985
18986 l = GetShortPathName(*fnamep, *fnamep, len);
18987 if (l > len - 1)
18988 {
18989 /* If that doesn't work (not enough space), then save the string
18990 * and try again with a new buffer big enough
18991 */
18992 newbuf = vim_strnsave(*fnamep, l);
18993 if (newbuf == NULL)
18994 return 0;
18995
18996 vim_free(*bufp);
18997 *fnamep = *bufp = newbuf;
18998
18999 l = GetShortPathName(*fnamep,*fnamep,l+1);
19000
19001 /* Really should always succeed, as the buffer is big enough */
19002 }
19003
19004 *fnamelen = l;
19005 return 1;
19006}
19007
19008/*
19009 * Create a short path name. Returns the length of the buffer it needs.
19010 * Doesn't copy over the end of the buffer passed in.
19011 */
19012 static int
19013shortpath_for_invalid_fname(fname, bufp, fnamelen)
19014 char_u **fname;
19015 char_u **bufp;
19016 int *fnamelen;
19017{
19018 char_u *s, *p, *pbuf2, *pbuf3;
19019 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019020 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019021
19022 /* Make a copy */
19023 len2 = *fnamelen;
19024 pbuf2 = vim_strnsave(*fname, len2);
19025 pbuf3 = NULL;
19026
19027 s = pbuf2 + len2 - 1; /* Find the end */
19028 slen = 1;
19029 plen = len2;
19030
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019031 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 {
19033 --s;
19034 ++slen;
19035 --plen;
19036 }
19037
19038 do
19039 {
19040 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019041 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042 {
19043 --s;
19044 ++slen;
19045 --plen;
19046 }
19047 if (s <= pbuf2)
19048 break;
19049
19050 /* Remeber the character that is about to be blatted */
19051 ch = *s;
19052 *s = 0; /* get_short_pathname requires a null-terminated string */
19053
19054 /* Try it in situ */
19055 p = pbuf2;
19056 if (!get_short_pathname(&p, &pbuf3, &plen))
19057 {
19058 vim_free(pbuf2);
19059 return -1;
19060 }
19061 *s = ch; /* Preserve the string */
19062 } while (plen == 0);
19063
19064 if (plen > 0)
19065 {
19066 /* Remeber the length of the new string. */
19067 *fnamelen = len = plen + slen;
19068 vim_free(*bufp);
19069 if (len > len2)
19070 {
19071 /* If there's not enough space in the currently allocated string,
19072 * then copy it to a buffer big enough.
19073 */
19074 *fname= *bufp = vim_strnsave(p, len);
19075 if (*fname == NULL)
19076 return -1;
19077 }
19078 else
19079 {
19080 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19081 *fname = *bufp = pbuf2;
19082 if (p != pbuf2)
19083 strncpy(*fname, p, plen);
19084 pbuf2 = NULL;
19085 }
19086 /* Concat the next bit */
19087 strncpy(*fname + plen, s, slen);
19088 (*fname)[len] = '\0';
19089 }
19090 vim_free(pbuf3);
19091 vim_free(pbuf2);
19092 return 0;
19093}
19094
19095/*
19096 * Get a pathname for a partial path.
19097 */
19098 static int
19099shortpath_for_partial(fnamep, bufp, fnamelen)
19100 char_u **fnamep;
19101 char_u **bufp;
19102 int *fnamelen;
19103{
19104 int sepcount, len, tflen;
19105 char_u *p;
19106 char_u *pbuf, *tfname;
19107 int hasTilde;
19108
19109 /* Count up the path seperators from the RHS.. so we know which part
19110 * of the path to return.
19111 */
19112 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019113 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019114 if (vim_ispathsep(*p))
19115 ++sepcount;
19116
19117 /* Need full path first (use expand_env() to remove a "~/") */
19118 hasTilde = (**fnamep == '~');
19119 if (hasTilde)
19120 pbuf = tfname = expand_env_save(*fnamep);
19121 else
19122 pbuf = tfname = FullName_save(*fnamep, FALSE);
19123
19124 len = tflen = STRLEN(tfname);
19125
19126 if (!get_short_pathname(&tfname, &pbuf, &len))
19127 return -1;
19128
19129 if (len == 0)
19130 {
19131 /* Don't have a valid filename, so shorten the rest of the
19132 * path if we can. This CAN give us invalid 8.3 filenames, but
19133 * there's not a lot of point in guessing what it might be.
19134 */
19135 len = tflen;
19136 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19137 return -1;
19138 }
19139
19140 /* Count the paths backward to find the beginning of the desired string. */
19141 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019142 {
19143#ifdef FEAT_MBYTE
19144 if (has_mbyte)
19145 p -= mb_head_off(tfname, p);
19146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147 if (vim_ispathsep(*p))
19148 {
19149 if (sepcount == 0 || (hasTilde && sepcount == 1))
19150 break;
19151 else
19152 sepcount --;
19153 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155 if (hasTilde)
19156 {
19157 --p;
19158 if (p >= tfname)
19159 *p = '~';
19160 else
19161 return -1;
19162 }
19163 else
19164 ++p;
19165
19166 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19167 vim_free(*bufp);
19168 *fnamelen = (int)STRLEN(p);
19169 *bufp = pbuf;
19170 *fnamep = p;
19171
19172 return 0;
19173}
19174#endif /* WIN3264 */
19175
19176/*
19177 * Adjust a filename, according to a string of modifiers.
19178 * *fnamep must be NUL terminated when called. When returning, the length is
19179 * determined by *fnamelen.
19180 * Returns valid flags.
19181 * When there is an error, *fnamep is set to NULL.
19182 */
19183 int
19184modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19185 char_u *src; /* string with modifiers */
19186 int *usedlen; /* characters after src that are used */
19187 char_u **fnamep; /* file name so far */
19188 char_u **bufp; /* buffer for allocated file name or NULL */
19189 int *fnamelen; /* length of fnamep */
19190{
19191 int valid = 0;
19192 char_u *tail;
19193 char_u *s, *p, *pbuf;
19194 char_u dirname[MAXPATHL];
19195 int c;
19196 int has_fullname = 0;
19197#ifdef WIN3264
19198 int has_shortname = 0;
19199#endif
19200
19201repeat:
19202 /* ":p" - full path/file_name */
19203 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19204 {
19205 has_fullname = 1;
19206
19207 valid |= VALID_PATH;
19208 *usedlen += 2;
19209
19210 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19211 if ((*fnamep)[0] == '~'
19212#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19213 && ((*fnamep)[1] == '/'
19214# ifdef BACKSLASH_IN_FILENAME
19215 || (*fnamep)[1] == '\\'
19216# endif
19217 || (*fnamep)[1] == NUL)
19218
19219#endif
19220 )
19221 {
19222 *fnamep = expand_env_save(*fnamep);
19223 vim_free(*bufp); /* free any allocated file name */
19224 *bufp = *fnamep;
19225 if (*fnamep == NULL)
19226 return -1;
19227 }
19228
19229 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019230 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231 {
19232 if (vim_ispathsep(*p)
19233 && p[1] == '.'
19234 && (p[2] == NUL
19235 || vim_ispathsep(p[2])
19236 || (p[2] == '.'
19237 && (p[3] == NUL || vim_ispathsep(p[3])))))
19238 break;
19239 }
19240
19241 /* FullName_save() is slow, don't use it when not needed. */
19242 if (*p != NUL || !vim_isAbsName(*fnamep))
19243 {
19244 *fnamep = FullName_save(*fnamep, *p != NUL);
19245 vim_free(*bufp); /* free any allocated file name */
19246 *bufp = *fnamep;
19247 if (*fnamep == NULL)
19248 return -1;
19249 }
19250
19251 /* Append a path separator to a directory. */
19252 if (mch_isdir(*fnamep))
19253 {
19254 /* Make room for one or two extra characters. */
19255 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19256 vim_free(*bufp); /* free any allocated file name */
19257 *bufp = *fnamep;
19258 if (*fnamep == NULL)
19259 return -1;
19260 add_pathsep(*fnamep);
19261 }
19262 }
19263
19264 /* ":." - path relative to the current directory */
19265 /* ":~" - path relative to the home directory */
19266 /* ":8" - shortname path - postponed till after */
19267 while (src[*usedlen] == ':'
19268 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19269 {
19270 *usedlen += 2;
19271 if (c == '8')
19272 {
19273#ifdef WIN3264
19274 has_shortname = 1; /* Postpone this. */
19275#endif
19276 continue;
19277 }
19278 pbuf = NULL;
19279 /* Need full path first (use expand_env() to remove a "~/") */
19280 if (!has_fullname)
19281 {
19282 if (c == '.' && **fnamep == '~')
19283 p = pbuf = expand_env_save(*fnamep);
19284 else
19285 p = pbuf = FullName_save(*fnamep, FALSE);
19286 }
19287 else
19288 p = *fnamep;
19289
19290 has_fullname = 0;
19291
19292 if (p != NULL)
19293 {
19294 if (c == '.')
19295 {
19296 mch_dirname(dirname, MAXPATHL);
19297 s = shorten_fname(p, dirname);
19298 if (s != NULL)
19299 {
19300 *fnamep = s;
19301 if (pbuf != NULL)
19302 {
19303 vim_free(*bufp); /* free any allocated file name */
19304 *bufp = pbuf;
19305 pbuf = NULL;
19306 }
19307 }
19308 }
19309 else
19310 {
19311 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19312 /* Only replace it when it starts with '~' */
19313 if (*dirname == '~')
19314 {
19315 s = vim_strsave(dirname);
19316 if (s != NULL)
19317 {
19318 *fnamep = s;
19319 vim_free(*bufp);
19320 *bufp = s;
19321 }
19322 }
19323 }
19324 vim_free(pbuf);
19325 }
19326 }
19327
19328 tail = gettail(*fnamep);
19329 *fnamelen = (int)STRLEN(*fnamep);
19330
19331 /* ":h" - head, remove "/file_name", can be repeated */
19332 /* Don't remove the first "/" or "c:\" */
19333 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19334 {
19335 valid |= VALID_HEAD;
19336 *usedlen += 2;
19337 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019338 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 --tail;
19340 *fnamelen = (int)(tail - *fnamep);
19341#ifdef VMS
19342 if (*fnamelen > 0)
19343 *fnamelen += 1; /* the path separator is part of the path */
19344#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019345 while (tail > s && !after_pathsep(s, tail))
19346 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347 }
19348
19349 /* ":8" - shortname */
19350 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19351 {
19352 *usedlen += 2;
19353#ifdef WIN3264
19354 has_shortname = 1;
19355#endif
19356 }
19357
19358#ifdef WIN3264
19359 /* Check shortname after we have done 'heads' and before we do 'tails'
19360 */
19361 if (has_shortname)
19362 {
19363 pbuf = NULL;
19364 /* Copy the string if it is shortened by :h */
19365 if (*fnamelen < (int)STRLEN(*fnamep))
19366 {
19367 p = vim_strnsave(*fnamep, *fnamelen);
19368 if (p == 0)
19369 return -1;
19370 vim_free(*bufp);
19371 *bufp = *fnamep = p;
19372 }
19373
19374 /* Split into two implementations - makes it easier. First is where
19375 * there isn't a full name already, second is where there is.
19376 */
19377 if (!has_fullname && !vim_isAbsName(*fnamep))
19378 {
19379 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19380 return -1;
19381 }
19382 else
19383 {
19384 int l;
19385
19386 /* Simple case, already have the full-name
19387 * Nearly always shorter, so try first time. */
19388 l = *fnamelen;
19389 if (!get_short_pathname(fnamep, bufp, &l))
19390 return -1;
19391
19392 if (l == 0)
19393 {
19394 /* Couldn't find the filename.. search the paths.
19395 */
19396 l = *fnamelen;
19397 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19398 return -1;
19399 }
19400 *fnamelen = l;
19401 }
19402 }
19403#endif /* WIN3264 */
19404
19405 /* ":t" - tail, just the basename */
19406 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19407 {
19408 *usedlen += 2;
19409 *fnamelen -= (int)(tail - *fnamep);
19410 *fnamep = tail;
19411 }
19412
19413 /* ":e" - extension, can be repeated */
19414 /* ":r" - root, without extension, can be repeated */
19415 while (src[*usedlen] == ':'
19416 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19417 {
19418 /* find a '.' in the tail:
19419 * - for second :e: before the current fname
19420 * - otherwise: The last '.'
19421 */
19422 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19423 s = *fnamep - 2;
19424 else
19425 s = *fnamep + *fnamelen - 1;
19426 for ( ; s > tail; --s)
19427 if (s[0] == '.')
19428 break;
19429 if (src[*usedlen + 1] == 'e') /* :e */
19430 {
19431 if (s > tail)
19432 {
19433 *fnamelen += (int)(*fnamep - (s + 1));
19434 *fnamep = s + 1;
19435#ifdef VMS
19436 /* cut version from the extension */
19437 s = *fnamep + *fnamelen - 1;
19438 for ( ; s > *fnamep; --s)
19439 if (s[0] == ';')
19440 break;
19441 if (s > *fnamep)
19442 *fnamelen = s - *fnamep;
19443#endif
19444 }
19445 else if (*fnamep <= tail)
19446 *fnamelen = 0;
19447 }
19448 else /* :r */
19449 {
19450 if (s > tail) /* remove one extension */
19451 *fnamelen = (int)(s - *fnamep);
19452 }
19453 *usedlen += 2;
19454 }
19455
19456 /* ":s?pat?foo?" - substitute */
19457 /* ":gs?pat?foo?" - global substitute */
19458 if (src[*usedlen] == ':'
19459 && (src[*usedlen + 1] == 's'
19460 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19461 {
19462 char_u *str;
19463 char_u *pat;
19464 char_u *sub;
19465 int sep;
19466 char_u *flags;
19467 int didit = FALSE;
19468
19469 flags = (char_u *)"";
19470 s = src + *usedlen + 2;
19471 if (src[*usedlen + 1] == 'g')
19472 {
19473 flags = (char_u *)"g";
19474 ++s;
19475 }
19476
19477 sep = *s++;
19478 if (sep)
19479 {
19480 /* find end of pattern */
19481 p = vim_strchr(s, sep);
19482 if (p != NULL)
19483 {
19484 pat = vim_strnsave(s, (int)(p - s));
19485 if (pat != NULL)
19486 {
19487 s = p + 1;
19488 /* find end of substitution */
19489 p = vim_strchr(s, sep);
19490 if (p != NULL)
19491 {
19492 sub = vim_strnsave(s, (int)(p - s));
19493 str = vim_strnsave(*fnamep, *fnamelen);
19494 if (sub != NULL && str != NULL)
19495 {
19496 *usedlen = (int)(p + 1 - src);
19497 s = do_string_sub(str, pat, sub, flags);
19498 if (s != NULL)
19499 {
19500 *fnamep = s;
19501 *fnamelen = (int)STRLEN(s);
19502 vim_free(*bufp);
19503 *bufp = s;
19504 didit = TRUE;
19505 }
19506 }
19507 vim_free(sub);
19508 vim_free(str);
19509 }
19510 vim_free(pat);
19511 }
19512 }
19513 /* after using ":s", repeat all the modifiers */
19514 if (didit)
19515 goto repeat;
19516 }
19517 }
19518
19519 return valid;
19520}
19521
19522/*
19523 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19524 * "flags" can be "g" to do a global substitute.
19525 * Returns an allocated string, NULL for error.
19526 */
19527 char_u *
19528do_string_sub(str, pat, sub, flags)
19529 char_u *str;
19530 char_u *pat;
19531 char_u *sub;
19532 char_u *flags;
19533{
19534 int sublen;
19535 regmatch_T regmatch;
19536 int i;
19537 int do_all;
19538 char_u *tail;
19539 garray_T ga;
19540 char_u *ret;
19541 char_u *save_cpo;
19542
19543 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19544 save_cpo = p_cpo;
19545 p_cpo = (char_u *)"";
19546
19547 ga_init2(&ga, 1, 200);
19548
19549 do_all = (flags[0] == 'g');
19550
19551 regmatch.rm_ic = p_ic;
19552 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19553 if (regmatch.regprog != NULL)
19554 {
19555 tail = str;
19556 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19557 {
19558 /*
19559 * Get some space for a temporary buffer to do the substitution
19560 * into. It will contain:
19561 * - The text up to where the match is.
19562 * - The substituted text.
19563 * - The text after the match.
19564 */
19565 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19566 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19567 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19568 {
19569 ga_clear(&ga);
19570 break;
19571 }
19572
19573 /* copy the text up to where the match is */
19574 i = (int)(regmatch.startp[0] - tail);
19575 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19576 /* add the substituted text */
19577 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19578 + ga.ga_len + i, TRUE, TRUE, FALSE);
19579 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019580 /* avoid getting stuck on a match with an empty string */
19581 if (tail == regmatch.endp[0])
19582 {
19583 if (*tail == NUL)
19584 break;
19585 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19586 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587 }
19588 else
19589 {
19590 tail = regmatch.endp[0];
19591 if (*tail == NUL)
19592 break;
19593 }
19594 if (!do_all)
19595 break;
19596 }
19597
19598 if (ga.ga_data != NULL)
19599 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19600
19601 vim_free(regmatch.regprog);
19602 }
19603
19604 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19605 ga_clear(&ga);
19606 p_cpo = save_cpo;
19607
19608 return ret;
19609}
19610
19611#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */