blob: cd706b49c103615977e4798d15c92f70333b8309 [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));
583static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000589static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000594static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000595static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
598#ifdef HAVE_STRFTIME
599static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
600#endif
601static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000613static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000629static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
632static int get_env_len __ARGS((char_u **arg));
633static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000634static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000635static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
636#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
637#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
638 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000639static 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 +0000640static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000641static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000642static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
643static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static typval_T *alloc_tv __ARGS((void));
645static typval_T *alloc_string_tv __ARGS((char_u *string));
646static void free_tv __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void init_tv __ARGS((typval_T *varp));
648static long get_tv_number __ARGS((typval_T *varp));
649static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000650static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static char_u *get_tv_string __ARGS((typval_T *varp));
652static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000653static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000655static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
657static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
658static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
659static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
660static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
661static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
662static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000663static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000665static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
667static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
668static int eval_fname_script __ARGS((char_u *p));
669static int eval_fname_sid __ARGS((char_u *p));
670static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static ufunc_T *find_func __ARGS((char_u *name));
672static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000673static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000674#ifdef FEAT_PROFILE
675static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000676static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
677static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
678static int
679# ifdef __BORLANDC__
680 _RTLENTRYF
681# endif
682 prof_total_cmp __ARGS((const void *s1, const void *s2));
683static int
684# ifdef __BORLANDC__
685 _RTLENTRYF
686# endif
687 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000688#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000689static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000690static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000691static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void func_free __ARGS((ufunc_T *fp));
693static void func_unref __ARGS((char_u *name));
694static void func_ref __ARGS((char_u *name));
695static 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));
696static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
697
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000698/* Character used as separated in autoload function/variable names. */
699#define AUTOLOAD_CHAR '#'
700
Bram Moolenaar33570922005-01-25 22:26:29 +0000701/*
702 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000703 */
704 void
705eval_init()
706{
Bram Moolenaar33570922005-01-25 22:26:29 +0000707 int i;
708 struct vimvar *p;
709
710 init_var_dict(&globvardict, &globvars_var);
711 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000712 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000713 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000714
715 for (i = 0; i < VV_LEN; ++i)
716 {
717 p = &vimvars[i];
718 STRCPY(p->vv_di.di_key, p->vv_name);
719 if (p->vv_flags & VV_RO)
720 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
721 else if (p->vv_flags & VV_RO_SBX)
722 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
723 else
724 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000725
726 /* add to v: scope dict, unless the value is not always available */
727 if (p->vv_type != VAR_UNKNOWN)
728 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000729 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000730 /* add to compat scope dict */
731 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000732 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000733}
734
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000735#if defined(EXITFREE) || defined(PROTO)
736 void
737eval_clear()
738{
739 int i;
740 struct vimvar *p;
741
742 for (i = 0; i < VV_LEN; ++i)
743 {
744 p = &vimvars[i];
745 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000746 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000747 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000748 p->vv_di.di_tv.vval.v_string = NULL;
749 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000750 }
751 hash_clear(&vimvarht);
752 hash_clear(&compat_hashtab);
753
754 /* script-local variables */
755 for (i = 1; i <= ga_scripts.ga_len; ++i)
756 vars_clear(&SCRIPT_VARS(i));
757 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000758 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000759
760 /* global variables */
761 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000762
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000763 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000764 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000765 hash_clear(&func_hashtab);
766
767 /* unreferenced lists and dicts */
768 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000769}
770#endif
771
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 * Return the name of the executed function.
774 */
775 char_u *
776func_name(cookie)
777 void *cookie;
778{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000779 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780}
781
782/*
783 * Return the address holding the next breakpoint line for a funccall cookie.
784 */
785 linenr_T *
786func_breakpoint(cookie)
787 void *cookie;
788{
Bram Moolenaar33570922005-01-25 22:26:29 +0000789 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790}
791
792/*
793 * Return the address holding the debug tick for a funccall cookie.
794 */
795 int *
796func_dbg_tick(cookie)
797 void *cookie;
798{
Bram Moolenaar33570922005-01-25 22:26:29 +0000799 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800}
801
802/*
803 * Return the nesting level for a funccall cookie.
804 */
805 int
806func_level(cookie)
807 void *cookie;
808{
Bram Moolenaar33570922005-01-25 22:26:29 +0000809 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810}
811
812/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000813funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
815/*
816 * Return TRUE when a function was ended by a ":return" command.
817 */
818 int
819current_func_returned()
820{
821 return current_funccal->returned;
822}
823
824
825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 * Set an internal variable to a string value. Creates the variable if it does
827 * not already exist.
828 */
829 void
830set_internal_string_var(name, value)
831 char_u *name;
832 char_u *value;
833{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000834 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000835 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836
837 val = vim_strsave(value);
838 if (val != NULL)
839 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000840 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000841 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000843 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000844 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 }
846 }
847}
848
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000849static lval_T *redir_lval = NULL;
850static char_u *redir_endp = NULL;
851static char_u *redir_varname = NULL;
852
853/*
854 * Start recording command output to a variable
855 * Returns OK if successfully completed the setup. FAIL otherwise.
856 */
857 int
858var_redir_start(name, append)
859 char_u *name;
860 int append; /* append to an existing variable */
861{
862 int save_emsg;
863 int err;
864 typval_T tv;
865
866 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000867 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000868 {
869 EMSG(_(e_invarg));
870 return FAIL;
871 }
872
873 redir_varname = vim_strsave(name);
874 if (redir_varname == NULL)
875 return FAIL;
876
877 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
878 if (redir_lval == NULL)
879 {
880 var_redir_stop();
881 return FAIL;
882 }
883
884 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000885 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
886 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000887 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
888 {
889 if (redir_endp != NULL && *redir_endp != NUL)
890 /* Trailing characters are present after the variable name */
891 EMSG(_(e_trailing));
892 else
893 EMSG(_(e_invarg));
894 var_redir_stop();
895 return FAIL;
896 }
897
898 /* check if we can write to the variable: set it to or append an empty
899 * string */
900 save_emsg = did_emsg;
901 did_emsg = FALSE;
902 tv.v_type = VAR_STRING;
903 tv.vval.v_string = (char_u *)"";
904 if (append)
905 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
906 else
907 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
908 err = did_emsg;
909 did_emsg += save_emsg;
910 if (err)
911 {
912 var_redir_stop();
913 return FAIL;
914 }
915 if (redir_lval->ll_newkey != NULL)
916 {
917 /* Dictionary item was created, don't do it again. */
918 vim_free(redir_lval->ll_newkey);
919 redir_lval->ll_newkey = NULL;
920 }
921
922 return OK;
923}
924
925/*
926 * Append "value[len]" to the variable set by var_redir_start().
927 */
928 void
929var_redir_str(value, len)
930 char_u *value;
931 int len;
932{
933 char_u *val;
934 typval_T tv;
935 int save_emsg;
936 int err;
937
938 if (redir_lval == NULL)
939 return;
940
941 if (len == -1)
942 /* Append the entire string */
943 val = vim_strsave(value);
944 else
945 /* Append only the specified number of characters */
946 val = vim_strnsave(value, len);
947 if (val == NULL)
948 return;
949
950 tv.v_type = VAR_STRING;
951 tv.vval.v_string = val;
952
953 save_emsg = did_emsg;
954 did_emsg = FALSE;
955 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
956 err = did_emsg;
957 did_emsg += save_emsg;
958 if (err)
959 var_redir_stop();
960
961 vim_free(tv.vval.v_string);
962}
963
964/*
965 * Stop redirecting command output to a variable.
966 */
967 void
968var_redir_stop()
969{
970 if (redir_lval != NULL)
971 {
972 clear_lval(redir_lval);
973 vim_free(redir_lval);
974 redir_lval = NULL;
975 }
976 vim_free(redir_varname);
977 redir_varname = NULL;
978}
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980# if defined(FEAT_MBYTE) || defined(PROTO)
981 int
982eval_charconvert(enc_from, enc_to, fname_from, fname_to)
983 char_u *enc_from;
984 char_u *enc_to;
985 char_u *fname_from;
986 char_u *fname_to;
987{
988 int err = FALSE;
989
990 set_vim_var_string(VV_CC_FROM, enc_from, -1);
991 set_vim_var_string(VV_CC_TO, enc_to, -1);
992 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
993 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
994 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
995 err = TRUE;
996 set_vim_var_string(VV_CC_FROM, NULL, -1);
997 set_vim_var_string(VV_CC_TO, NULL, -1);
998 set_vim_var_string(VV_FNAME_IN, NULL, -1);
999 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1000
1001 if (err)
1002 return FAIL;
1003 return OK;
1004}
1005# endif
1006
1007# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1008 int
1009eval_printexpr(fname, args)
1010 char_u *fname;
1011 char_u *args;
1012{
1013 int err = FALSE;
1014
1015 set_vim_var_string(VV_FNAME_IN, fname, -1);
1016 set_vim_var_string(VV_CMDARG, args, -1);
1017 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1018 err = TRUE;
1019 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1020 set_vim_var_string(VV_CMDARG, NULL, -1);
1021
1022 if (err)
1023 {
1024 mch_remove(fname);
1025 return FAIL;
1026 }
1027 return OK;
1028}
1029# endif
1030
1031# if defined(FEAT_DIFF) || defined(PROTO)
1032 void
1033eval_diff(origfile, newfile, outfile)
1034 char_u *origfile;
1035 char_u *newfile;
1036 char_u *outfile;
1037{
1038 int err = FALSE;
1039
1040 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1041 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1042 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1043 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1044 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1045 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1046 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1047}
1048
1049 void
1050eval_patch(origfile, difffile, outfile)
1051 char_u *origfile;
1052 char_u *difffile;
1053 char_u *outfile;
1054{
1055 int err;
1056
1057 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1058 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1059 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1060 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1061 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1062 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1063 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1064}
1065# endif
1066
1067/*
1068 * Top level evaluation function, returning a boolean.
1069 * Sets "error" to TRUE if there was an error.
1070 * Return TRUE or FALSE.
1071 */
1072 int
1073eval_to_bool(arg, error, nextcmd, skip)
1074 char_u *arg;
1075 int *error;
1076 char_u **nextcmd;
1077 int skip; /* only parse, don't execute */
1078{
Bram Moolenaar33570922005-01-25 22:26:29 +00001079 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 int retval = FALSE;
1081
1082 if (skip)
1083 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001084 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 else
1087 {
1088 *error = FALSE;
1089 if (!skip)
1090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001091 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001092 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 }
1094 }
1095 if (skip)
1096 --emsg_skip;
1097
1098 return retval;
1099}
1100
1101/*
1102 * Top level evaluation function, returning a string. If "skip" is TRUE,
1103 * only parsing to "nextcmd" is done, without reporting errors. Return
1104 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1105 */
1106 char_u *
1107eval_to_string_skip(arg, nextcmd, skip)
1108 char_u *arg;
1109 char_u **nextcmd;
1110 int skip; /* only parse, don't execute */
1111{
Bram Moolenaar33570922005-01-25 22:26:29 +00001112 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 char_u *retval;
1114
1115 if (skip)
1116 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001117 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 retval = NULL;
1119 else
1120 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001121 retval = vim_strsave(get_tv_string(&tv));
1122 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 }
1124 if (skip)
1125 --emsg_skip;
1126
1127 return retval;
1128}
1129
1130/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001131 * Skip over an expression at "*pp".
1132 * Return FAIL for an error, OK otherwise.
1133 */
1134 int
1135skip_expr(pp)
1136 char_u **pp;
1137{
Bram Moolenaar33570922005-01-25 22:26:29 +00001138 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001139
1140 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001141 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001142}
1143
1144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 * Top level evaluation function, returning a string.
1146 * Return pointer to allocated memory, or NULL for failure.
1147 */
1148 char_u *
1149eval_to_string(arg, nextcmd)
1150 char_u *arg;
1151 char_u **nextcmd;
1152{
Bram Moolenaar33570922005-01-25 22:26:29 +00001153 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 char_u *retval;
1155
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001156 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 retval = NULL;
1158 else
1159 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001160 retval = vim_strsave(get_tv_string(&tv));
1161 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
1163
1164 return retval;
1165}
1166
1167/*
1168 * Call eval_to_string() with "sandbox" set and not using local variables.
1169 */
1170 char_u *
1171eval_to_string_safe(arg, nextcmd)
1172 char_u *arg;
1173 char_u **nextcmd;
1174{
1175 char_u *retval;
1176 void *save_funccalp;
1177
1178 save_funccalp = save_funccal();
1179 ++sandbox;
1180 retval = eval_to_string(arg, nextcmd);
1181 --sandbox;
1182 restore_funccal(save_funccalp);
1183 return retval;
1184}
1185
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186/*
1187 * Top level evaluation function, returning a number.
1188 * Evaluates "expr" silently.
1189 * Returns -1 for an error.
1190 */
1191 int
1192eval_to_number(expr)
1193 char_u *expr;
1194{
Bram Moolenaar33570922005-01-25 22:26:29 +00001195 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001197 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198
1199 ++emsg_off;
1200
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001201 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 retval = -1;
1203 else
1204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001205 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001206 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 }
1208 --emsg_off;
1209
1210 return retval;
1211}
1212
Bram Moolenaara40058a2005-07-11 22:42:07 +00001213/*
1214 * Prepare v: variable "idx" to be used.
1215 * Save the current typeval in "save_tv".
1216 * When not used yet add the variable to the v: hashtable.
1217 */
1218 static void
1219prepare_vimvar(idx, save_tv)
1220 int idx;
1221 typval_T *save_tv;
1222{
1223 *save_tv = vimvars[idx].vv_tv;
1224 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1225 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1226}
1227
1228/*
1229 * Restore v: variable "idx" to typeval "save_tv".
1230 * When no longer defined, remove the variable from the v: hashtable.
1231 */
1232 static void
1233restore_vimvar(idx, save_tv)
1234 int idx;
1235 typval_T *save_tv;
1236{
1237 hashitem_T *hi;
1238
1239 clear_tv(&vimvars[idx].vv_tv);
1240 vimvars[idx].vv_tv = *save_tv;
1241 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1242 {
1243 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1244 if (HASHITEM_EMPTY(hi))
1245 EMSG2(_(e_intern2), "restore_vimvar()");
1246 else
1247 hash_remove(&vimvarht, hi);
1248 }
1249}
1250
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001251#if defined(FEAT_SYN_HL) || defined(PROTO)
1252/*
1253 * Evaluate an expression to a list with suggestions.
1254 * For the "expr:" part of 'spellsuggest'.
1255 */
1256 list_T *
1257eval_spell_expr(badword, expr)
1258 char_u *badword;
1259 char_u *expr;
1260{
1261 typval_T save_val;
1262 typval_T rettv;
1263 list_T *list = NULL;
1264 char_u *p = skipwhite(expr);
1265
1266 /* Set "v:val" to the bad word. */
1267 prepare_vimvar(VV_VAL, &save_val);
1268 vimvars[VV_VAL].vv_type = VAR_STRING;
1269 vimvars[VV_VAL].vv_str = badword;
1270 if (p_verbose == 0)
1271 ++emsg_off;
1272
1273 if (eval1(&p, &rettv, TRUE) == OK)
1274 {
1275 if (rettv.v_type != VAR_LIST)
1276 clear_tv(&rettv);
1277 else
1278 list = rettv.vval.v_list;
1279 }
1280
1281 if (p_verbose == 0)
1282 --emsg_off;
1283 vimvars[VV_VAL].vv_str = NULL;
1284 restore_vimvar(VV_VAL, &save_val);
1285
1286 return list;
1287}
1288
1289/*
1290 * "list" is supposed to contain two items: a word and a number. Return the
1291 * word in "pp" and the number as the return value.
1292 * Return -1 if anything isn't right.
1293 * Used to get the good word and score from the eval_spell_expr() result.
1294 */
1295 int
1296get_spellword(list, pp)
1297 list_T *list;
1298 char_u **pp;
1299{
1300 listitem_T *li;
1301
1302 li = list->lv_first;
1303 if (li == NULL)
1304 return -1;
1305 *pp = get_tv_string(&li->li_tv);
1306
1307 li = li->li_next;
1308 if (li == NULL)
1309 return -1;
1310 return get_tv_number(&li->li_tv);
1311}
1312#endif
1313
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001314/*
1315 * Top level evaluation function,
1316 */
1317 typval_T *
1318eval_expr(arg, nextcmd)
1319 char_u *arg;
1320 char_u **nextcmd;
1321{
1322 typval_T *tv;
1323
1324 tv = (typval_T *)alloc(sizeof(typval_T));
1325 if (!tv)
1326 return NULL;
1327
1328 if (eval0(arg, tv, nextcmd, TRUE) == FAIL)
1329 {
1330 vim_free(tv);
1331 return NULL;
1332 }
1333
1334 return tv;
1335}
1336
1337
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1339/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001340 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001342 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001344 static int
1345call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 char_u *func;
1347 int argc;
1348 char_u **argv;
1349 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351{
Bram Moolenaar33570922005-01-25 22:26:29 +00001352 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 long n;
1354 int len;
1355 int i;
1356 int doesrange;
1357 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001358 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
Bram Moolenaar33570922005-01-25 22:26:29 +00001360 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001362 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363
1364 for (i = 0; i < argc; i++)
1365 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001366 /* Pass a NULL or empty argument as an empty string */
1367 if (argv[i] == NULL || *argv[i] == NUL)
1368 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001369 argvars[i].v_type = VAR_STRING;
1370 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001371 continue;
1372 }
1373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 /* Recognize a number argument, the others must be strings. */
1375 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1376 if (len != 0 && len == (int)STRLEN(argv[i]))
1377 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001378 argvars[i].v_type = VAR_NUMBER;
1379 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
1381 else
1382 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001383 argvars[i].v_type = VAR_STRING;
1384 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
1386 }
1387
1388 if (safe)
1389 {
1390 save_funccalp = save_funccal();
1391 ++sandbox;
1392 }
1393
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001394 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1395 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001397 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 if (safe)
1399 {
1400 --sandbox;
1401 restore_funccal(save_funccalp);
1402 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001403 vim_free(argvars);
1404
1405 if (ret == FAIL)
1406 clear_tv(rettv);
1407
1408 return ret;
1409}
1410
1411/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001412 * Call vimL function "func" and return the result as a string.
1413 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001414 * Uses argv[argc] for the function arguments.
1415 */
1416 void *
1417call_func_retstr(func, argc, argv, safe)
1418 char_u *func;
1419 int argc;
1420 char_u **argv;
1421 int safe; /* use the sandbox */
1422{
1423 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001424 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001425
1426 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1427 return NULL;
1428
1429 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001430 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 return retval;
1432}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001433
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001434#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001435/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001436 * Call vimL function "func" and return the result as a number.
1437 * Returns -1 when calling the function fails.
1438 * Uses argv[argc] for the function arguments.
1439 */
1440 long
1441call_func_retnr(func, argc, argv, safe)
1442 char_u *func;
1443 int argc;
1444 char_u **argv;
1445 int safe; /* use the sandbox */
1446{
1447 typval_T rettv;
1448 long retval;
1449
1450 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1451 return -1;
1452
1453 retval = get_tv_number_chk(&rettv, NULL);
1454 clear_tv(&rettv);
1455 return retval;
1456}
1457#endif
1458
1459/*
1460 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001461 * Uses argv[argc] for the function arguments.
1462 */
1463 void *
1464call_func_retlist(func, argc, argv, safe)
1465 char_u *func;
1466 int argc;
1467 char_u **argv;
1468 int safe; /* use the sandbox */
1469{
1470 typval_T rettv;
1471
1472 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1473 return NULL;
1474
1475 if (rettv.v_type != VAR_LIST)
1476 {
1477 clear_tv(&rettv);
1478 return NULL;
1479 }
1480
1481 return rettv.vval.v_list;
1482}
1483
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484#endif
1485
1486/*
1487 * Save the current function call pointer, and set it to NULL.
1488 * Used when executing autocommands and for ":source".
1489 */
1490 void *
1491save_funccal()
1492{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001493 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 current_funccal = NULL;
1496 return (void *)fc;
1497}
1498
1499 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001500restore_funccal(vfc)
1501 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001503 funccall_T *fc = (funccall_T *)vfc;
1504
1505 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506}
1507
Bram Moolenaar05159a02005-02-26 23:04:13 +00001508#if defined(FEAT_PROFILE) || defined(PROTO)
1509/*
1510 * Prepare profiling for entering a child or something else that is not
1511 * counted for the script/function itself.
1512 * Should always be called in pair with prof_child_exit().
1513 */
1514 void
1515prof_child_enter(tm)
1516 proftime_T *tm; /* place to store waittime */
1517{
1518 funccall_T *fc = current_funccal;
1519
1520 if (fc != NULL && fc->func->uf_profiling)
1521 profile_start(&fc->prof_child);
1522 script_prof_save(tm);
1523}
1524
1525/*
1526 * Take care of time spent in a child.
1527 * Should always be called after prof_child_enter().
1528 */
1529 void
1530prof_child_exit(tm)
1531 proftime_T *tm; /* where waittime was stored */
1532{
1533 funccall_T *fc = current_funccal;
1534
1535 if (fc != NULL && fc->func->uf_profiling)
1536 {
1537 profile_end(&fc->prof_child);
1538 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1539 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1540 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1541 }
1542 script_prof_restore(tm);
1543}
1544#endif
1545
1546
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547#ifdef FEAT_FOLDING
1548/*
1549 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1550 * it in "*cp". Doesn't give error messages.
1551 */
1552 int
1553eval_foldexpr(arg, cp)
1554 char_u *arg;
1555 int *cp;
1556{
Bram Moolenaar33570922005-01-25 22:26:29 +00001557 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 int retval;
1559 char_u *s;
1560
1561 ++emsg_off;
1562 ++sandbox;
1563 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001564 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 retval = 0;
1566 else
1567 {
1568 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001569 if (tv.v_type == VAR_NUMBER)
1570 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001571 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 retval = 0;
1573 else
1574 {
1575 /* If the result is a string, check if there is a non-digit before
1576 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001577 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 if (!VIM_ISDIGIT(*s) && *s != '-')
1579 *cp = *s++;
1580 retval = atol((char *)s);
1581 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001582 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 }
1584 --emsg_off;
1585 --sandbox;
1586
1587 return retval;
1588}
1589#endif
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001592 * ":let" list all variable values
1593 * ":let var1 var2" list variable values
1594 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001595 * ":let var += expr" assignment command.
1596 * ":let var -= expr" assignment command.
1597 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001598 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 */
1600 void
1601ex_let(eap)
1602 exarg_T *eap;
1603{
1604 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001605 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001606 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001608 int var_count = 0;
1609 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001610 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001612 expr = skip_var_list(arg, &var_count, &semicolon);
1613 if (expr == NULL)
1614 return;
1615 expr = vim_strchr(expr, '=');
1616 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001618 /*
1619 * ":let" without "=": list variables
1620 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001621 if (*arg == '[')
1622 EMSG(_(e_invarg));
1623 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001624 /* ":let var1 var2" */
1625 arg = list_arg_vars(eap, arg);
1626 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001628 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001629 list_glob_vars();
1630 list_buf_vars();
1631 list_win_vars();
1632 list_vim_vars();
1633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 eap->nextcmd = check_nextcmd(arg);
1635 }
1636 else
1637 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001638 op[0] = '=';
1639 op[1] = NUL;
1640 if (expr > arg)
1641 {
1642 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1643 op[0] = expr[-1]; /* +=, -= or .= */
1644 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001645 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001646
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (eap->skip)
1648 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001649 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 if (eap->skip)
1651 {
1652 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001653 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 --emsg_skip;
1655 }
1656 else if (i != FAIL)
1657 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001658 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001659 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001660 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 }
1662 }
1663}
1664
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665/*
1666 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1667 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001668 * When "nextchars" is not NULL it points to a string with characters that
1669 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1670 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671 * Returns OK or FAIL;
1672 */
1673 static int
1674ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1675 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001676 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 int copy; /* copy values from "tv", don't move */
1678 int semicolon; /* from skip_var_list() */
1679 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001680 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681{
1682 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001683 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001684 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001685 listitem_T *item;
1686 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001687
1688 if (*arg != '[')
1689 {
1690 /*
1691 * ":let var = expr" or ":for var in list"
1692 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001693 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694 return FAIL;
1695 return OK;
1696 }
1697
1698 /*
1699 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1700 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001701 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 {
1703 EMSG(_(e_listreq));
1704 return FAIL;
1705 }
1706
1707 i = list_len(l);
1708 if (semicolon == 0 && var_count < i)
1709 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001710 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 return FAIL;
1712 }
1713 if (var_count - semicolon > i)
1714 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001715 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 return FAIL;
1717 }
1718
1719 item = l->lv_first;
1720 while (*arg != ']')
1721 {
1722 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001723 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 item = item->li_next;
1725 if (arg == NULL)
1726 return FAIL;
1727
1728 arg = skipwhite(arg);
1729 if (*arg == ';')
1730 {
1731 /* Put the rest of the list (may be empty) in the var after ';'.
1732 * Create a new list for this. */
1733 l = list_alloc();
1734 if (l == NULL)
1735 return FAIL;
1736 while (item != NULL)
1737 {
1738 list_append_tv(l, &item->li_tv);
1739 item = item->li_next;
1740 }
1741
1742 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001743 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744 ltv.vval.v_list = l;
1745 l->lv_refcount = 1;
1746
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001747 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1748 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001749 clear_tv(&ltv);
1750 if (arg == NULL)
1751 return FAIL;
1752 break;
1753 }
1754 else if (*arg != ',' && *arg != ']')
1755 {
1756 EMSG2(_(e_intern2), "ex_let_vars()");
1757 return FAIL;
1758 }
1759 }
1760
1761 return OK;
1762}
1763
1764/*
1765 * Skip over assignable variable "var" or list of variables "[var, var]".
1766 * Used for ":let varvar = expr" and ":for varvar in expr".
1767 * For "[var, var]" increment "*var_count" for each variable.
1768 * for "[var, var; var]" set "semicolon".
1769 * Return NULL for an error.
1770 */
1771 static char_u *
1772skip_var_list(arg, var_count, semicolon)
1773 char_u *arg;
1774 int *var_count;
1775 int *semicolon;
1776{
1777 char_u *p, *s;
1778
1779 if (*arg == '[')
1780 {
1781 /* "[var, var]": find the matching ']'. */
1782 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001783 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784 {
1785 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1786 s = skip_var_one(p);
1787 if (s == p)
1788 {
1789 EMSG2(_(e_invarg2), p);
1790 return NULL;
1791 }
1792 ++*var_count;
1793
1794 p = skipwhite(s);
1795 if (*p == ']')
1796 break;
1797 else if (*p == ';')
1798 {
1799 if (*semicolon == 1)
1800 {
1801 EMSG(_("Double ; in list of variables"));
1802 return NULL;
1803 }
1804 *semicolon = 1;
1805 }
1806 else if (*p != ',')
1807 {
1808 EMSG2(_(e_invarg2), p);
1809 return NULL;
1810 }
1811 }
1812 return p + 1;
1813 }
1814 else
1815 return skip_var_one(arg);
1816}
1817
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001818/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001819 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1820 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001821 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822 static char_u *
1823skip_var_one(arg)
1824 char_u *arg;
1825{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001826 if (*arg == '@' && arg[1] != NUL)
1827 return arg + 2;
1828 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1829 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830}
1831
Bram Moolenaara7043832005-01-21 11:56:39 +00001832/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001833 * List variables for hashtab "ht" with prefix "prefix".
1834 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001835 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001837list_hashtable_vars(ht, prefix, empty)
1838 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001839 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001840 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001841{
Bram Moolenaar33570922005-01-25 22:26:29 +00001842 hashitem_T *hi;
1843 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001844 int todo;
1845
1846 todo = ht->ht_used;
1847 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1848 {
1849 if (!HASHITEM_EMPTY(hi))
1850 {
1851 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001852 di = HI2DI(hi);
1853 if (empty || di->di_tv.v_type != VAR_STRING
1854 || di->di_tv.vval.v_string != NULL)
1855 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001856 }
1857 }
1858}
1859
1860/*
1861 * List global variables.
1862 */
1863 static void
1864list_glob_vars()
1865{
Bram Moolenaar33570922005-01-25 22:26:29 +00001866 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001867}
1868
1869/*
1870 * List buffer variables.
1871 */
1872 static void
1873list_buf_vars()
1874{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001875 char_u numbuf[NUMBUFLEN];
1876
Bram Moolenaar33570922005-01-25 22:26:29 +00001877 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001878
1879 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1880 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001881}
1882
1883/*
1884 * List window variables.
1885 */
1886 static void
1887list_win_vars()
1888{
Bram Moolenaar33570922005-01-25 22:26:29 +00001889 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001890}
1891
1892/*
1893 * List Vim variables.
1894 */
1895 static void
1896list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897{
Bram Moolenaar33570922005-01-25 22:26:29 +00001898 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899}
1900
1901/*
1902 * List variables in "arg".
1903 */
1904 static char_u *
1905list_arg_vars(eap, arg)
1906 exarg_T *eap;
1907 char_u *arg;
1908{
1909 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001910 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001912 char_u *name_start;
1913 char_u *arg_subsc;
1914 char_u *tofree;
1915 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916
1917 while (!ends_excmd(*arg) && !got_int)
1918 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001919 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001921 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001922 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1923 {
1924 emsg_severe = TRUE;
1925 EMSG(_(e_trailing));
1926 break;
1927 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001928 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001929 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001930 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001931 /* get_name_len() takes care of expanding curly braces */
1932 name_start = name = arg;
1933 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1934 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001936 /* This is mainly to keep test 49 working: when expanding
1937 * curly braces fails overrule the exception error message. */
1938 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001940 emsg_severe = TRUE;
1941 EMSG2(_(e_invarg2), arg);
1942 break;
1943 }
1944 error = TRUE;
1945 }
1946 else
1947 {
1948 if (tofree != NULL)
1949 name = tofree;
1950 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001952 else
1953 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001954 /* handle d.key, l[idx], f(expr) */
1955 arg_subsc = arg;
1956 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001957 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001959 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001960 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001961 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001962 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001963 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001964 case 'g': list_glob_vars(); break;
1965 case 'b': list_buf_vars(); break;
1966 case 'w': list_win_vars(); break;
1967 case 'v': list_vim_vars(); break;
1968 default:
1969 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001970 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001971 }
1972 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001973 {
1974 char_u numbuf[NUMBUFLEN];
1975 char_u *tf;
1976 int c;
1977 char_u *s;
1978
1979 s = echo_string(&tv, &tf, numbuf);
1980 c = *arg;
1981 *arg = NUL;
1982 list_one_var_a((char_u *)"",
1983 arg == arg_subsc ? name : name_start,
1984 tv.v_type, s == NULL ? (char_u *)"" : s);
1985 *arg = c;
1986 vim_free(tf);
1987 }
1988 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001989 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990 }
1991 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001992
1993 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001994 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001995
1996 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001997 }
1998
1999 return arg;
2000}
2001
2002/*
2003 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2004 * Returns a pointer to the char just after the var name.
2005 * Returns NULL if there is an error.
2006 */
2007 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002008ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002009 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002010 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002011 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002013 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014{
2015 int c1;
2016 char_u *name;
2017 char_u *p;
2018 char_u *arg_end = NULL;
2019 int len;
2020 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002021 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002022
2023 /*
2024 * ":let $VAR = expr": Set environment variable.
2025 */
2026 if (*arg == '$')
2027 {
2028 /* Find the end of the name. */
2029 ++arg;
2030 name = arg;
2031 len = get_env_len(&arg);
2032 if (len == 0)
2033 EMSG2(_(e_invarg2), name - 1);
2034 else
2035 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002036 if (op != NULL && (*op == '+' || *op == '-'))
2037 EMSG2(_(e_letwrong), op);
2038 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040 EMSG(_(e_letunexp));
2041 else
2042 {
2043 c1 = name[len];
2044 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002045 p = get_tv_string_chk(tv);
2046 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002047 {
2048 int mustfree = FALSE;
2049 char_u *s = vim_getenv(name, &mustfree);
2050
2051 if (s != NULL)
2052 {
2053 p = tofree = concat_str(s, p);
2054 if (mustfree)
2055 vim_free(s);
2056 }
2057 }
2058 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002059 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002060 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002061 if (STRICMP(name, "HOME") == 0)
2062 init_homedir();
2063 else if (didset_vim && STRICMP(name, "VIM") == 0)
2064 didset_vim = FALSE;
2065 else if (didset_vimruntime
2066 && STRICMP(name, "VIMRUNTIME") == 0)
2067 didset_vimruntime = FALSE;
2068 arg_end = arg;
2069 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002070 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002071 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 }
2073 }
2074 }
2075
2076 /*
2077 * ":let &option = expr": Set option value.
2078 * ":let &l:option = expr": Set local option value.
2079 * ":let &g:option = expr": Set global option value.
2080 */
2081 else if (*arg == '&')
2082 {
2083 /* Find the end of the name. */
2084 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002085 if (p == NULL || (endchars != NULL
2086 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002087 EMSG(_(e_letunexp));
2088 else
2089 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002090 long n;
2091 int opt_type;
2092 long numval;
2093 char_u *stringval = NULL;
2094 char_u *s;
2095
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002096 c1 = *p;
2097 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002098
2099 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002100 s = get_tv_string_chk(tv); /* != NULL if number or string */
2101 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002102 {
2103 opt_type = get_option_value(arg, &numval,
2104 &stringval, opt_flags);
2105 if ((opt_type == 1 && *op == '.')
2106 || (opt_type == 0 && *op != '.'))
2107 EMSG2(_(e_letwrong), op);
2108 else
2109 {
2110 if (opt_type == 1) /* number */
2111 {
2112 if (*op == '+')
2113 n = numval + n;
2114 else
2115 n = numval - n;
2116 }
2117 else if (opt_type == 0 && stringval != NULL) /* string */
2118 {
2119 s = concat_str(stringval, s);
2120 vim_free(stringval);
2121 stringval = s;
2122 }
2123 }
2124 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002125 if (s != NULL)
2126 {
2127 set_option_value(arg, n, s, opt_flags);
2128 arg_end = p;
2129 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002130 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002131 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002132 }
2133 }
2134
2135 /*
2136 * ":let @r = expr": Set register contents.
2137 */
2138 else if (*arg == '@')
2139 {
2140 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002141 if (op != NULL && (*op == '+' || *op == '-'))
2142 EMSG2(_(e_letwrong), op);
2143 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002144 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002145 EMSG(_(e_letunexp));
2146 else
2147 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002148 char_u *tofree = NULL;
2149 char_u *s;
2150
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002151 p = get_tv_string_chk(tv);
2152 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002153 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002154 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002155 if (s != NULL)
2156 {
2157 p = tofree = concat_str(s, p);
2158 vim_free(s);
2159 }
2160 }
2161 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002162 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002163 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002164 arg_end = arg + 1;
2165 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002166 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002167 }
2168 }
2169
2170 /*
2171 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002172 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002174 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002176 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002178 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002179 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002180 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002181 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2182 EMSG(_(e_letunexp));
2183 else
2184 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002185 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 arg_end = p;
2187 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002189 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 }
2191
2192 else
2193 EMSG2(_(e_invarg2), arg);
2194
2195 return arg_end;
2196}
2197
2198/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002199 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2200 */
2201 static int
2202check_changedtick(arg)
2203 char_u *arg;
2204{
2205 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2206 {
2207 EMSG2(_(e_readonlyvar), arg);
2208 return TRUE;
2209 }
2210 return FALSE;
2211}
2212
2213/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002214 * Get an lval: variable, Dict item or List item that can be assigned a value
2215 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2216 * "name.key", "name.key[expr]" etc.
2217 * Indexing only works if "name" is an existing List or Dictionary.
2218 * "name" points to the start of the name.
2219 * If "rettv" is not NULL it points to the value to be assigned.
2220 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2221 * wrong; must end in space or cmd separator.
2222 *
2223 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002224 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 */
2227 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002228get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002230 typval_T *rettv;
2231 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002232 int unlet;
2233 int skip;
2234 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002235 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002238 char_u *expr_start, *expr_end;
2239 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002240 dictitem_T *v;
2241 typval_T var1;
2242 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002243 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002244 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002245 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002246 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002247 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002250 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251
2252 if (skip)
2253 {
2254 /* When skipping just find the end of the name. */
2255 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002256 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 }
2258
2259 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002260 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 if (expr_start != NULL)
2262 {
2263 /* Don't expand the name when we already know there is an error. */
2264 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2265 && *p != '[' && *p != '.')
2266 {
2267 EMSG(_(e_trailing));
2268 return NULL;
2269 }
2270
2271 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2272 if (lp->ll_exp_name == NULL)
2273 {
2274 /* Report an invalid expression in braces, unless the
2275 * expression evaluation has been cancelled due to an
2276 * aborting error, an interrupt, or an exception. */
2277 if (!aborting() && !quiet)
2278 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002279 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002280 EMSG2(_(e_invarg2), name);
2281 return NULL;
2282 }
2283 }
2284 lp->ll_name = lp->ll_exp_name;
2285 }
2286 else
2287 lp->ll_name = name;
2288
2289 /* Without [idx] or .key we are done. */
2290 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2291 return p;
2292
2293 cc = *p;
2294 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 if (v == NULL && !quiet)
2297 EMSG2(_(e_undefvar), lp->ll_name);
2298 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 if (v == NULL)
2300 return NULL;
2301
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002302 /*
2303 * Loop until no more [idx] or .key is following.
2304 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002305 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002306 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2309 && !(lp->ll_tv->v_type == VAR_DICT
2310 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002312 if (!quiet)
2313 EMSG(_("E689: Can only index a List or Dictionary"));
2314 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002318 if (!quiet)
2319 EMSG(_("E708: [:] must come last"));
2320 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002322
Bram Moolenaar8c711452005-01-14 21:53:12 +00002323 len = -1;
2324 if (*p == '.')
2325 {
2326 key = p + 1;
2327 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2328 ;
2329 if (len == 0)
2330 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 if (!quiet)
2332 EMSG(_(e_emptykey));
2333 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002334 }
2335 p = key + len;
2336 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002337 else
2338 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002339 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002340 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002341 if (*p == ':')
2342 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002343 else
2344 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002345 empty1 = FALSE;
2346 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002347 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002348 if (get_tv_string_chk(&var1) == NULL)
2349 {
2350 /* not a number or string */
2351 clear_tv(&var1);
2352 return NULL;
2353 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002354 }
2355
2356 /* Optionally get the second index [ :expr]. */
2357 if (*p == ':')
2358 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002360 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002361 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002362 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002363 if (!empty1)
2364 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002366 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002367 if (rettv != NULL && (rettv->v_type != VAR_LIST
2368 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002369 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 if (!quiet)
2371 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002372 if (!empty1)
2373 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002374 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002375 }
2376 p = skipwhite(p + 1);
2377 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002379 else
2380 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002381 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002382 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2383 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002384 if (!empty1)
2385 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002386 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002387 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002388 if (get_tv_string_chk(&var2) == NULL)
2389 {
2390 /* not a number or string */
2391 if (!empty1)
2392 clear_tv(&var1);
2393 clear_tv(&var2);
2394 return NULL;
2395 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002396 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002398 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002399 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002400 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002401
Bram Moolenaar8c711452005-01-14 21:53:12 +00002402 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002403 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002404 if (!quiet)
2405 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002406 if (!empty1)
2407 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002409 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002410 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002411 }
2412
2413 /* Skip to past ']'. */
2414 ++p;
2415 }
2416
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002418 {
2419 if (len == -1)
2420 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002422 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002423 if (*key == NUL)
2424 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 if (!quiet)
2426 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002427 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002428 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002429 }
2430 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 lp->ll_list = NULL;
2432 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002433 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002435 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002436 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002441 if (len == -1)
2442 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002444 }
2445 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002447 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 if (len == -1)
2450 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 p = NULL;
2453 break;
2454 }
2455 if (len == -1)
2456 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 }
2459 else
2460 {
2461 /*
2462 * Get the number and item for the only or first index of the List.
2463 */
2464 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002466 else
2467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 clear_tv(&var1);
2470 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002471 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 lp->ll_list = lp->ll_tv->vval.v_list;
2473 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2474 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 if (!quiet)
2477 EMSGN(_(e_listidx), lp->ll_n1);
2478 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002479 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002481 }
2482
2483 /*
2484 * May need to find the item or absolute index for the second
2485 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 * When no index given: "lp->ll_empty2" is TRUE.
2487 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002491 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 if (ni == NULL)
2497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (!quiet)
2499 EMSGN(_(e_listidx), lp->ll_n2);
2500 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002501 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002503 }
2504
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2506 if (lp->ll_n1 < 0)
2507 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2508 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002509 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 if (!quiet)
2511 EMSGN(_(e_listidx), lp->ll_n2);
2512 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002513 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002514 }
2515
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 }
2519
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 return p;
2521}
2522
2523/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002524 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 */
2526 static void
2527clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002528 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529{
2530 vim_free(lp->ll_exp_name);
2531 vim_free(lp->ll_newkey);
2532}
2533
2534/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002535 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 */
2539 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002540set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002541 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002543 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002545 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546{
2547 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002548 listitem_T *ni;
2549 listitem_T *ri;
2550 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551
2552 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002553 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 cc = *endp;
2557 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002558 if (op != NULL && *op != '=')
2559 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002560 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002561
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002562 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002563 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2564 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002565 {
2566 if (tv_op(&tv, rettv, op) == OK)
2567 set_var(lp->ll_name, &tv, FALSE);
2568 clear_tv(&tv);
2569 }
2570 }
2571 else
2572 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002576 else if (tv_check_lock(lp->ll_newkey == NULL
2577 ? lp->ll_tv->v_lock
2578 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2579 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 else if (lp->ll_range)
2581 {
2582 /*
2583 * Assign the List values to the list items.
2584 */
2585 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002586 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002587 if (op != NULL && *op != '=')
2588 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2589 else
2590 {
2591 clear_tv(&lp->ll_li->li_tv);
2592 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2593 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 ri = ri->li_next;
2595 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2596 break;
2597 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 /* Need to add an empty item. */
2600 ni = listitem_alloc();
2601 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 ri = NULL;
2604 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002605 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002607 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 ni->li_tv.vval.v_number = 0;
2609 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002610 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 lp->ll_li = lp->ll_li->li_next;
2612 ++lp->ll_n1;
2613 }
2614 if (ri != NULL)
2615 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002616 else if (lp->ll_empty2
2617 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 : lp->ll_n1 != lp->ll_n2)
2619 EMSG(_("E711: List value has not enough items"));
2620 }
2621 else
2622 {
2623 /*
2624 * Assign to a List or Dictionary item.
2625 */
2626 if (lp->ll_newkey != NULL)
2627 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002628 if (op != NULL && *op != '=')
2629 {
2630 EMSG2(_(e_letwrong), op);
2631 return;
2632 }
2633
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002635 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 if (di == NULL)
2637 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002638 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2639 {
2640 vim_free(di);
2641 return;
2642 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002645 else if (op != NULL && *op != '=')
2646 {
2647 tv_op(lp->ll_tv, rettv, op);
2648 return;
2649 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002650 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 /*
2654 * Assign the value to the variable or list item.
2655 */
2656 if (copy)
2657 copy_tv(rettv, lp->ll_tv);
2658 else
2659 {
2660 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002661 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 }
2664 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665}
2666
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002667/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002668 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2669 * Returns OK or FAIL.
2670 */
2671 static int
2672tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002673 typval_T *tv1;
2674 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002675 char_u *op;
2676{
2677 long n;
2678 char_u numbuf[NUMBUFLEN];
2679 char_u *s;
2680
2681 /* Can't do anything with a Funcref or a Dict on the right. */
2682 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2683 {
2684 switch (tv1->v_type)
2685 {
2686 case VAR_DICT:
2687 case VAR_FUNC:
2688 break;
2689
2690 case VAR_LIST:
2691 if (*op != '+' || tv2->v_type != VAR_LIST)
2692 break;
2693 /* List += List */
2694 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2695 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2696 return OK;
2697
2698 case VAR_NUMBER:
2699 case VAR_STRING:
2700 if (tv2->v_type == VAR_LIST)
2701 break;
2702 if (*op == '+' || *op == '-')
2703 {
2704 /* nr += nr or nr -= nr*/
2705 n = get_tv_number(tv1);
2706 if (*op == '+')
2707 n += get_tv_number(tv2);
2708 else
2709 n -= get_tv_number(tv2);
2710 clear_tv(tv1);
2711 tv1->v_type = VAR_NUMBER;
2712 tv1->vval.v_number = n;
2713 }
2714 else
2715 {
2716 /* str .= str */
2717 s = get_tv_string(tv1);
2718 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2719 clear_tv(tv1);
2720 tv1->v_type = VAR_STRING;
2721 tv1->vval.v_string = s;
2722 }
2723 return OK;
2724 }
2725 }
2726
2727 EMSG2(_(e_letwrong), op);
2728 return FAIL;
2729}
2730
2731/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002732 * Add a watcher to a list.
2733 */
2734 static void
2735list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002736 list_T *l;
2737 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002738{
2739 lw->lw_next = l->lv_watch;
2740 l->lv_watch = lw;
2741}
2742
2743/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002744 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002745 * No warning when it isn't found...
2746 */
2747 static void
2748list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002749 list_T *l;
2750 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002751{
Bram Moolenaar33570922005-01-25 22:26:29 +00002752 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002753
2754 lwp = &l->lv_watch;
2755 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2756 {
2757 if (lw == lwrem)
2758 {
2759 *lwp = lw->lw_next;
2760 break;
2761 }
2762 lwp = &lw->lw_next;
2763 }
2764}
2765
2766/*
2767 * Just before removing an item from a list: advance watchers to the next
2768 * item.
2769 */
2770 static void
2771list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002772 list_T *l;
2773 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002774{
Bram Moolenaar33570922005-01-25 22:26:29 +00002775 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002776
2777 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2778 if (lw->lw_item == item)
2779 lw->lw_item = item->li_next;
2780}
2781
2782/*
2783 * Evaluate the expression used in a ":for var in expr" command.
2784 * "arg" points to "var".
2785 * Set "*errp" to TRUE for an error, FALSE otherwise;
2786 * Return a pointer that holds the info. Null when there is an error.
2787 */
2788 void *
2789eval_for_line(arg, errp, nextcmdp, skip)
2790 char_u *arg;
2791 int *errp;
2792 char_u **nextcmdp;
2793 int skip;
2794{
Bram Moolenaar33570922005-01-25 22:26:29 +00002795 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002796 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002797 typval_T tv;
2798 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002799
2800 *errp = TRUE; /* default: there is an error */
2801
Bram Moolenaar33570922005-01-25 22:26:29 +00002802 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002803 if (fi == NULL)
2804 return NULL;
2805
2806 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2807 if (expr == NULL)
2808 return fi;
2809
2810 expr = skipwhite(expr);
2811 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2812 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002813 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002814 return fi;
2815 }
2816
2817 if (skip)
2818 ++emsg_skip;
2819 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2820 {
2821 *errp = FALSE;
2822 if (!skip)
2823 {
2824 l = tv.vval.v_list;
2825 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002826 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002827 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002828 clear_tv(&tv);
2829 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002830 else
2831 {
2832 fi->fi_list = l;
2833 list_add_watch(l, &fi->fi_lw);
2834 fi->fi_lw.lw_item = l->lv_first;
2835 }
2836 }
2837 }
2838 if (skip)
2839 --emsg_skip;
2840
2841 return fi;
2842}
2843
2844/*
2845 * Use the first item in a ":for" list. Advance to the next.
2846 * Assign the values to the variable (list). "arg" points to the first one.
2847 * Return TRUE when a valid item was found, FALSE when at end of list or
2848 * something wrong.
2849 */
2850 int
2851next_for_item(fi_void, arg)
2852 void *fi_void;
2853 char_u *arg;
2854{
Bram Moolenaar33570922005-01-25 22:26:29 +00002855 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002856 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002857 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002858
2859 item = fi->fi_lw.lw_item;
2860 if (item == NULL)
2861 result = FALSE;
2862 else
2863 {
2864 fi->fi_lw.lw_item = item->li_next;
2865 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2866 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2867 }
2868 return result;
2869}
2870
2871/*
2872 * Free the structure used to store info used by ":for".
2873 */
2874 void
2875free_for_info(fi_void)
2876 void *fi_void;
2877{
Bram Moolenaar33570922005-01-25 22:26:29 +00002878 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002879
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002880 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002881 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002882 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002883 list_unref(fi->fi_list);
2884 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002885 vim_free(fi);
2886}
2887
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2889
2890 void
2891set_context_for_expression(xp, arg, cmdidx)
2892 expand_T *xp;
2893 char_u *arg;
2894 cmdidx_T cmdidx;
2895{
2896 int got_eq = FALSE;
2897 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002898 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002900 if (cmdidx == CMD_let)
2901 {
2902 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002903 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002904 {
2905 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002906 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002907 {
2908 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002909 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002910 if (vim_iswhite(*p))
2911 break;
2912 }
2913 return;
2914 }
2915 }
2916 else
2917 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2918 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 while ((xp->xp_pattern = vim_strpbrk(arg,
2920 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2921 {
2922 c = *xp->xp_pattern;
2923 if (c == '&')
2924 {
2925 c = xp->xp_pattern[1];
2926 if (c == '&')
2927 {
2928 ++xp->xp_pattern;
2929 xp->xp_context = cmdidx != CMD_let || got_eq
2930 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2931 }
2932 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002935 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2936 xp->xp_pattern += 2;
2937
2938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 }
2940 else if (c == '$')
2941 {
2942 /* environment variable */
2943 xp->xp_context = EXPAND_ENV_VARS;
2944 }
2945 else if (c == '=')
2946 {
2947 got_eq = TRUE;
2948 xp->xp_context = EXPAND_EXPRESSION;
2949 }
2950 else if (c == '<'
2951 && xp->xp_context == EXPAND_FUNCTIONS
2952 && vim_strchr(xp->xp_pattern, '(') == NULL)
2953 {
2954 /* Function name can start with "<SNR>" */
2955 break;
2956 }
2957 else if (cmdidx != CMD_let || got_eq)
2958 {
2959 if (c == '"') /* string */
2960 {
2961 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2962 if (c == '\\' && xp->xp_pattern[1] != NUL)
2963 ++xp->xp_pattern;
2964 xp->xp_context = EXPAND_NOTHING;
2965 }
2966 else if (c == '\'') /* literal string */
2967 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002968 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2970 /* skip */ ;
2971 xp->xp_context = EXPAND_NOTHING;
2972 }
2973 else if (c == '|')
2974 {
2975 if (xp->xp_pattern[1] == '|')
2976 {
2977 ++xp->xp_pattern;
2978 xp->xp_context = EXPAND_EXPRESSION;
2979 }
2980 else
2981 xp->xp_context = EXPAND_COMMANDS;
2982 }
2983 else
2984 xp->xp_context = EXPAND_EXPRESSION;
2985 }
2986 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002987 /* Doesn't look like something valid, expand as an expression
2988 * anyway. */
2989 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 arg = xp->xp_pattern;
2991 if (*arg != NUL)
2992 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2993 /* skip */ ;
2994 }
2995 xp->xp_pattern = arg;
2996}
2997
2998#endif /* FEAT_CMDL_COMPL */
2999
3000/*
3001 * ":1,25call func(arg1, arg2)" function call.
3002 */
3003 void
3004ex_call(eap)
3005 exarg_T *eap;
3006{
3007 char_u *arg = eap->arg;
3008 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003010 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003012 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 linenr_T lnum;
3014 int doesrange;
3015 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003016 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003018 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3019 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003020 if (tofree == NULL)
3021 return;
3022
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003023 /* Increase refcount on dictionary, it could get deleted when evaluating
3024 * the arguments. */
3025 if (fudi.fd_dict != NULL)
3026 ++fudi.fd_dict->dv_refcount;
3027
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003028 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3029 len = STRLEN(tofree);
3030 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
Bram Moolenaar532c7802005-01-27 14:44:31 +00003032 /* Skip white space to allow ":call func ()". Not good, but required for
3033 * backward compatibility. */
3034 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003035 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
3037 if (*startarg != '(')
3038 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003039 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 goto end;
3041 }
3042
3043 /*
3044 * When skipping, evaluate the function once, to find the end of the
3045 * arguments.
3046 * When the function takes a range, this is discovered after the first
3047 * call, and the loop is broken.
3048 */
3049 if (eap->skip)
3050 {
3051 ++emsg_skip;
3052 lnum = eap->line2; /* do it once, also with an invalid range */
3053 }
3054 else
3055 lnum = eap->line1;
3056 for ( ; lnum <= eap->line2; ++lnum)
3057 {
3058 if (!eap->skip && eap->addr_count > 0)
3059 {
3060 curwin->w_cursor.lnum = lnum;
3061 curwin->w_cursor.col = 0;
3062 }
3063 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003065 eap->line1, eap->line2, &doesrange,
3066 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 {
3068 failed = TRUE;
3069 break;
3070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003071 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 if (doesrange || eap->skip)
3073 break;
3074 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003075 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003076 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003077 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 if (aborting())
3079 break;
3080 }
3081 if (eap->skip)
3082 --emsg_skip;
3083
3084 if (!failed)
3085 {
3086 /* Check for trailing illegal characters and a following command. */
3087 if (!ends_excmd(*arg))
3088 {
3089 emsg_severe = TRUE;
3090 EMSG(_(e_trailing));
3091 }
3092 else
3093 eap->nextcmd = check_nextcmd(arg);
3094 }
3095
3096end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003097 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003098 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099}
3100
3101/*
3102 * ":unlet[!] var1 ... " command.
3103 */
3104 void
3105ex_unlet(eap)
3106 exarg_T *eap;
3107{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003108 ex_unletlock(eap, eap->arg, 0);
3109}
3110
3111/*
3112 * ":lockvar" and ":unlockvar" commands
3113 */
3114 void
3115ex_lockvar(eap)
3116 exarg_T *eap;
3117{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003119 int deep = 2;
3120
3121 if (eap->forceit)
3122 deep = -1;
3123 else if (vim_isdigit(*arg))
3124 {
3125 deep = getdigits(&arg);
3126 arg = skipwhite(arg);
3127 }
3128
3129 ex_unletlock(eap, arg, deep);
3130}
3131
3132/*
3133 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3134 */
3135 static void
3136ex_unletlock(eap, argstart, deep)
3137 exarg_T *eap;
3138 char_u *argstart;
3139 int deep;
3140{
3141 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003144 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
3146 do
3147 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003148 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003149 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3150 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003151 if (lv.ll_name == NULL)
3152 error = TRUE; /* error but continue parsing */
3153 if (name_end == NULL || (!vim_iswhite(*name_end)
3154 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003156 if (name_end != NULL)
3157 {
3158 emsg_severe = TRUE;
3159 EMSG(_(e_trailing));
3160 }
3161 if (!(eap->skip || error))
3162 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 break;
3164 }
3165
3166 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003167 {
3168 if (eap->cmdidx == CMD_unlet)
3169 {
3170 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3171 error = TRUE;
3172 }
3173 else
3174 {
3175 if (do_lock_var(&lv, name_end, deep,
3176 eap->cmdidx == CMD_lockvar) == FAIL)
3177 error = TRUE;
3178 }
3179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003181 if (!eap->skip)
3182 clear_lval(&lv);
3183
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 arg = skipwhite(name_end);
3185 } while (!ends_excmd(*arg));
3186
3187 eap->nextcmd = check_nextcmd(arg);
3188}
3189
Bram Moolenaar8c711452005-01-14 21:53:12 +00003190 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003191do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003193 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003194 int forceit;
3195{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003196 int ret = OK;
3197 int cc;
3198
3199 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003200 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003201 cc = *name_end;
3202 *name_end = NUL;
3203
3204 /* Normal name or expanded name. */
3205 if (check_changedtick(lp->ll_name))
3206 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003207 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003208 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003209 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003210 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003211 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3212 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003213 else if (lp->ll_range)
3214 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003215 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003216
3217 /* Delete a range of List items. */
3218 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3219 {
3220 li = lp->ll_li->li_next;
3221 listitem_remove(lp->ll_list, lp->ll_li);
3222 lp->ll_li = li;
3223 ++lp->ll_n1;
3224 }
3225 }
3226 else
3227 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003228 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003229 /* unlet a List item. */
3230 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003231 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003232 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003233 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003234 }
3235
3236 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003237}
3238
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239/*
3240 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003241 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 */
3243 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003244do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003246 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 hashtab_T *ht;
3249 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003250 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
Bram Moolenaar33570922005-01-25 22:26:29 +00003252 ht = find_var_ht(name, &varname);
3253 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003255 hi = hash_find(ht, varname);
3256 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003257 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003258 if (var_check_ro(HI2DI(hi)->di_flags, name))
3259 return FAIL;
3260 delete_var(ht, hi);
3261 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003264 if (forceit)
3265 return OK;
3266 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 return FAIL;
3268}
3269
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003270/*
3271 * Lock or unlock variable indicated by "lp".
3272 * "deep" is the levels to go (-1 for unlimited);
3273 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3274 */
3275 static int
3276do_lock_var(lp, name_end, deep, lock)
3277 lval_T *lp;
3278 char_u *name_end;
3279 int deep;
3280 int lock;
3281{
3282 int ret = OK;
3283 int cc;
3284 dictitem_T *di;
3285
3286 if (deep == 0) /* nothing to do */
3287 return OK;
3288
3289 if (lp->ll_tv == NULL)
3290 {
3291 cc = *name_end;
3292 *name_end = NUL;
3293
3294 /* Normal name or expanded name. */
3295 if (check_changedtick(lp->ll_name))
3296 ret = FAIL;
3297 else
3298 {
3299 di = find_var(lp->ll_name, NULL);
3300 if (di == NULL)
3301 ret = FAIL;
3302 else
3303 {
3304 if (lock)
3305 di->di_flags |= DI_FLAGS_LOCK;
3306 else
3307 di->di_flags &= ~DI_FLAGS_LOCK;
3308 item_lock(&di->di_tv, deep, lock);
3309 }
3310 }
3311 *name_end = cc;
3312 }
3313 else if (lp->ll_range)
3314 {
3315 listitem_T *li = lp->ll_li;
3316
3317 /* (un)lock a range of List items. */
3318 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3319 {
3320 item_lock(&li->li_tv, deep, lock);
3321 li = li->li_next;
3322 ++lp->ll_n1;
3323 }
3324 }
3325 else if (lp->ll_list != NULL)
3326 /* (un)lock a List item. */
3327 item_lock(&lp->ll_li->li_tv, deep, lock);
3328 else
3329 /* un(lock) a Dictionary item. */
3330 item_lock(&lp->ll_di->di_tv, deep, lock);
3331
3332 return ret;
3333}
3334
3335/*
3336 * Lock or unlock an item. "deep" is nr of levels to go.
3337 */
3338 static void
3339item_lock(tv, deep, lock)
3340 typval_T *tv;
3341 int deep;
3342 int lock;
3343{
3344 static int recurse = 0;
3345 list_T *l;
3346 listitem_T *li;
3347 dict_T *d;
3348 hashitem_T *hi;
3349 int todo;
3350
3351 if (recurse >= DICT_MAXNEST)
3352 {
3353 EMSG(_("E743: variable nested too deep for (un)lock"));
3354 return;
3355 }
3356 if (deep == 0)
3357 return;
3358 ++recurse;
3359
3360 /* lock/unlock the item itself */
3361 if (lock)
3362 tv->v_lock |= VAR_LOCKED;
3363 else
3364 tv->v_lock &= ~VAR_LOCKED;
3365
3366 switch (tv->v_type)
3367 {
3368 case VAR_LIST:
3369 if ((l = tv->vval.v_list) != NULL)
3370 {
3371 if (lock)
3372 l->lv_lock |= VAR_LOCKED;
3373 else
3374 l->lv_lock &= ~VAR_LOCKED;
3375 if (deep < 0 || deep > 1)
3376 /* recursive: lock/unlock the items the List contains */
3377 for (li = l->lv_first; li != NULL; li = li->li_next)
3378 item_lock(&li->li_tv, deep - 1, lock);
3379 }
3380 break;
3381 case VAR_DICT:
3382 if ((d = tv->vval.v_dict) != NULL)
3383 {
3384 if (lock)
3385 d->dv_lock |= VAR_LOCKED;
3386 else
3387 d->dv_lock &= ~VAR_LOCKED;
3388 if (deep < 0 || deep > 1)
3389 {
3390 /* recursive: lock/unlock the items the List contains */
3391 todo = d->dv_hashtab.ht_used;
3392 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3393 {
3394 if (!HASHITEM_EMPTY(hi))
3395 {
3396 --todo;
3397 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3398 }
3399 }
3400 }
3401 }
3402 }
3403 --recurse;
3404}
3405
Bram Moolenaara40058a2005-07-11 22:42:07 +00003406/*
3407 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3408 * it refers to a List or Dictionary that is locked.
3409 */
3410 static int
3411tv_islocked(tv)
3412 typval_T *tv;
3413{
3414 return (tv->v_lock & VAR_LOCKED)
3415 || (tv->v_type == VAR_LIST
3416 && tv->vval.v_list != NULL
3417 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3418 || (tv->v_type == VAR_DICT
3419 && tv->vval.v_dict != NULL
3420 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3421}
3422
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3424/*
3425 * Delete all "menutrans_" variables.
3426 */
3427 void
3428del_menutrans_vars()
3429{
Bram Moolenaar33570922005-01-25 22:26:29 +00003430 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003431 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432
Bram Moolenaar33570922005-01-25 22:26:29 +00003433 hash_lock(&globvarht);
3434 todo = globvarht.ht_used;
3435 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003436 {
3437 if (!HASHITEM_EMPTY(hi))
3438 {
3439 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003440 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3441 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003442 }
3443 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003444 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445}
3446#endif
3447
3448#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3449
3450/*
3451 * Local string buffer for the next two functions to store a variable name
3452 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3453 * get_user_var_name().
3454 */
3455
3456static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3457
3458static char_u *varnamebuf = NULL;
3459static int varnamebuflen = 0;
3460
3461/*
3462 * Function to concatenate a prefix and a variable name.
3463 */
3464 static char_u *
3465cat_prefix_varname(prefix, name)
3466 int prefix;
3467 char_u *name;
3468{
3469 int len;
3470
3471 len = (int)STRLEN(name) + 3;
3472 if (len > varnamebuflen)
3473 {
3474 vim_free(varnamebuf);
3475 len += 10; /* some additional space */
3476 varnamebuf = alloc(len);
3477 if (varnamebuf == NULL)
3478 {
3479 varnamebuflen = 0;
3480 return NULL;
3481 }
3482 varnamebuflen = len;
3483 }
3484 *varnamebuf = prefix;
3485 varnamebuf[1] = ':';
3486 STRCPY(varnamebuf + 2, name);
3487 return varnamebuf;
3488}
3489
3490/*
3491 * Function given to ExpandGeneric() to obtain the list of user defined
3492 * (global/buffer/window/built-in) variable names.
3493 */
3494/*ARGSUSED*/
3495 char_u *
3496get_user_var_name(xp, idx)
3497 expand_T *xp;
3498 int idx;
3499{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003500 static long_u gdone;
3501 static long_u bdone;
3502 static long_u wdone;
3503 static int vidx;
3504 static hashitem_T *hi;
3505 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
3507 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003508 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003509
3510 /* Global variables */
3511 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003513 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003514 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003515 else
3516 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003517 while (HASHITEM_EMPTY(hi))
3518 ++hi;
3519 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3520 return cat_prefix_varname('g', hi->hi_key);
3521 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003523
3524 /* b: variables */
3525 ht = &curbuf->b_vars.dv_hashtab;
3526 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003528 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003529 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003530 else
3531 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003532 while (HASHITEM_EMPTY(hi))
3533 ++hi;
3534 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003536 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003538 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 return (char_u *)"b:changedtick";
3540 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003541
3542 /* w: variables */
3543 ht = &curwin->w_vars.dv_hashtab;
3544 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003546 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003547 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003548 else
3549 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003550 while (HASHITEM_EMPTY(hi))
3551 ++hi;
3552 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003554
3555 /* v: variables */
3556 if (vidx < VV_LEN)
3557 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
3559 vim_free(varnamebuf);
3560 varnamebuf = NULL;
3561 varnamebuflen = 0;
3562 return NULL;
3563}
3564
3565#endif /* FEAT_CMDL_COMPL */
3566
3567/*
3568 * types for expressions.
3569 */
3570typedef enum
3571{
3572 TYPE_UNKNOWN = 0
3573 , TYPE_EQUAL /* == */
3574 , TYPE_NEQUAL /* != */
3575 , TYPE_GREATER /* > */
3576 , TYPE_GEQUAL /* >= */
3577 , TYPE_SMALLER /* < */
3578 , TYPE_SEQUAL /* <= */
3579 , TYPE_MATCH /* =~ */
3580 , TYPE_NOMATCH /* !~ */
3581} exptype_T;
3582
3583/*
3584 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003585 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3587 */
3588
3589/*
3590 * Handle zero level expression.
3591 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003592 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 * Return OK or FAIL.
3594 */
3595 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003596eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 char_u **nextcmd;
3600 int evaluate;
3601{
3602 int ret;
3603 char_u *p;
3604
3605 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003606 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 if (ret == FAIL || !ends_excmd(*p))
3608 {
3609 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003610 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 /*
3612 * Report the invalid expression unless the expression evaluation has
3613 * been cancelled due to an aborting error, an interrupt, or an
3614 * exception.
3615 */
3616 if (!aborting())
3617 EMSG2(_(e_invexpr2), arg);
3618 ret = FAIL;
3619 }
3620 if (nextcmd != NULL)
3621 *nextcmd = check_nextcmd(p);
3622
3623 return ret;
3624}
3625
3626/*
3627 * Handle top level expression:
3628 * expr1 ? expr0 : expr0
3629 *
3630 * "arg" must point to the first non-white of the expression.
3631 * "arg" is advanced to the next non-white after the recognized expression.
3632 *
3633 * Return OK or FAIL.
3634 */
3635 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003636eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 int evaluate;
3640{
3641 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003642 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
3644 /*
3645 * Get the first variable.
3646 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003647 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 return FAIL;
3649
3650 if ((*arg)[0] == '?')
3651 {
3652 result = FALSE;
3653 if (evaluate)
3654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003655 int error = FALSE;
3656
3657 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003659 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003660 if (error)
3661 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
3663
3664 /*
3665 * Get the second variable.
3666 */
3667 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003668 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 return FAIL;
3670
3671 /*
3672 * Check for the ":".
3673 */
3674 if ((*arg)[0] != ':')
3675 {
3676 EMSG(_("E109: Missing ':' after '?'"));
3677 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003678 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 return FAIL;
3680 }
3681
3682 /*
3683 * Get the third variable.
3684 */
3685 *arg = skipwhite(*arg + 1);
3686 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3687 {
3688 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003689 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 return FAIL;
3691 }
3692 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003693 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 }
3695
3696 return OK;
3697}
3698
3699/*
3700 * Handle first level expression:
3701 * expr2 || expr2 || expr2 logical OR
3702 *
3703 * "arg" must point to the first non-white of the expression.
3704 * "arg" is advanced to the next non-white after the recognized expression.
3705 *
3706 * Return OK or FAIL.
3707 */
3708 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003709eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 int evaluate;
3713{
Bram Moolenaar33570922005-01-25 22:26:29 +00003714 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 long result;
3716 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003717 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
3719 /*
3720 * Get the first variable.
3721 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003722 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 return FAIL;
3724
3725 /*
3726 * Repeat until there is no following "||".
3727 */
3728 first = TRUE;
3729 result = FALSE;
3730 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3731 {
3732 if (evaluate && first)
3733 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003734 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003736 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003737 if (error)
3738 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 first = FALSE;
3740 }
3741
3742 /*
3743 * Get the second variable.
3744 */
3745 *arg = skipwhite(*arg + 2);
3746 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3747 return FAIL;
3748
3749 /*
3750 * Compute the result.
3751 */
3752 if (evaluate && !result)
3753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003754 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003756 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003757 if (error)
3758 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
3760 if (evaluate)
3761 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003762 rettv->v_type = VAR_NUMBER;
3763 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 }
3765 }
3766
3767 return OK;
3768}
3769
3770/*
3771 * Handle second level expression:
3772 * expr3 && expr3 && expr3 logical AND
3773 *
3774 * "arg" must point to the first non-white of the expression.
3775 * "arg" is advanced to the next non-white after the recognized expression.
3776 *
3777 * Return OK or FAIL.
3778 */
3779 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003780eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 int evaluate;
3784{
Bram Moolenaar33570922005-01-25 22:26:29 +00003785 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 long result;
3787 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003788 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789
3790 /*
3791 * Get the first variable.
3792 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003793 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 return FAIL;
3795
3796 /*
3797 * Repeat until there is no following "&&".
3798 */
3799 first = TRUE;
3800 result = TRUE;
3801 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3802 {
3803 if (evaluate && first)
3804 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003805 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003807 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003808 if (error)
3809 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 first = FALSE;
3811 }
3812
3813 /*
3814 * Get the second variable.
3815 */
3816 *arg = skipwhite(*arg + 2);
3817 if (eval4(arg, &var2, evaluate && result) == FAIL)
3818 return FAIL;
3819
3820 /*
3821 * Compute the result.
3822 */
3823 if (evaluate && result)
3824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003825 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003827 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003828 if (error)
3829 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
3831 if (evaluate)
3832 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003833 rettv->v_type = VAR_NUMBER;
3834 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
3836 }
3837
3838 return OK;
3839}
3840
3841/*
3842 * Handle third level expression:
3843 * var1 == var2
3844 * var1 =~ var2
3845 * var1 != var2
3846 * var1 !~ var2
3847 * var1 > var2
3848 * var1 >= var2
3849 * var1 < var2
3850 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003851 * var1 is var2
3852 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 *
3854 * "arg" must point to the first non-white of the expression.
3855 * "arg" is advanced to the next non-white after the recognized expression.
3856 *
3857 * Return OK or FAIL.
3858 */
3859 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003860eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 int evaluate;
3864{
Bram Moolenaar33570922005-01-25 22:26:29 +00003865 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 char_u *p;
3867 int i;
3868 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003869 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 int len = 2;
3871 long n1, n2;
3872 char_u *s1, *s2;
3873 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3874 regmatch_T regmatch;
3875 int ic;
3876 char_u *save_cpo;
3877
3878 /*
3879 * Get the first variable.
3880 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003881 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 return FAIL;
3883
3884 p = *arg;
3885 switch (p[0])
3886 {
3887 case '=': if (p[1] == '=')
3888 type = TYPE_EQUAL;
3889 else if (p[1] == '~')
3890 type = TYPE_MATCH;
3891 break;
3892 case '!': if (p[1] == '=')
3893 type = TYPE_NEQUAL;
3894 else if (p[1] == '~')
3895 type = TYPE_NOMATCH;
3896 break;
3897 case '>': if (p[1] != '=')
3898 {
3899 type = TYPE_GREATER;
3900 len = 1;
3901 }
3902 else
3903 type = TYPE_GEQUAL;
3904 break;
3905 case '<': if (p[1] != '=')
3906 {
3907 type = TYPE_SMALLER;
3908 len = 1;
3909 }
3910 else
3911 type = TYPE_SEQUAL;
3912 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003913 case 'i': if (p[1] == 's')
3914 {
3915 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3916 len = 5;
3917 if (!vim_isIDc(p[len]))
3918 {
3919 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3920 type_is = TRUE;
3921 }
3922 }
3923 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 }
3925
3926 /*
3927 * If there is a comparitive operator, use it.
3928 */
3929 if (type != TYPE_UNKNOWN)
3930 {
3931 /* extra question mark appended: ignore case */
3932 if (p[len] == '?')
3933 {
3934 ic = TRUE;
3935 ++len;
3936 }
3937 /* extra '#' appended: match case */
3938 else if (p[len] == '#')
3939 {
3940 ic = FALSE;
3941 ++len;
3942 }
3943 /* nothing appened: use 'ignorecase' */
3944 else
3945 ic = p_ic;
3946
3947 /*
3948 * Get the second variable.
3949 */
3950 *arg = skipwhite(p + len);
3951 if (eval5(arg, &var2, evaluate) == FAIL)
3952 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003953 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 return FAIL;
3955 }
3956
3957 if (evaluate)
3958 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003959 if (type_is && rettv->v_type != var2.v_type)
3960 {
3961 /* For "is" a different type always means FALSE, for "notis"
3962 * it means TRUE. */
3963 n1 = (type == TYPE_NEQUAL);
3964 }
3965 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3966 {
3967 if (type_is)
3968 {
3969 n1 = (rettv->v_type == var2.v_type
3970 && rettv->vval.v_list == var2.vval.v_list);
3971 if (type == TYPE_NEQUAL)
3972 n1 = !n1;
3973 }
3974 else if (rettv->v_type != var2.v_type
3975 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3976 {
3977 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003978 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003979 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003980 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003981 clear_tv(rettv);
3982 clear_tv(&var2);
3983 return FAIL;
3984 }
3985 else
3986 {
3987 /* Compare two Lists for being equal or unequal. */
3988 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3989 if (type == TYPE_NEQUAL)
3990 n1 = !n1;
3991 }
3992 }
3993
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003994 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3995 {
3996 if (type_is)
3997 {
3998 n1 = (rettv->v_type == var2.v_type
3999 && rettv->vval.v_dict == var2.vval.v_dict);
4000 if (type == TYPE_NEQUAL)
4001 n1 = !n1;
4002 }
4003 else if (rettv->v_type != var2.v_type
4004 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4005 {
4006 if (rettv->v_type != var2.v_type)
4007 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4008 else
4009 EMSG(_("E736: Invalid operation for Dictionary"));
4010 clear_tv(rettv);
4011 clear_tv(&var2);
4012 return FAIL;
4013 }
4014 else
4015 {
4016 /* Compare two Dictionaries for being equal or unequal. */
4017 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4018 if (type == TYPE_NEQUAL)
4019 n1 = !n1;
4020 }
4021 }
4022
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004023 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4024 {
4025 if (rettv->v_type != var2.v_type
4026 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4027 {
4028 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004029 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004030 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004031 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004032 clear_tv(rettv);
4033 clear_tv(&var2);
4034 return FAIL;
4035 }
4036 else
4037 {
4038 /* Compare two Funcrefs for being equal or unequal. */
4039 if (rettv->vval.v_string == NULL
4040 || var2.vval.v_string == NULL)
4041 n1 = FALSE;
4042 else
4043 n1 = STRCMP(rettv->vval.v_string,
4044 var2.vval.v_string) == 0;
4045 if (type == TYPE_NEQUAL)
4046 n1 = !n1;
4047 }
4048 }
4049
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 /*
4051 * If one of the two variables is a number, compare as a number.
4052 * When using "=~" or "!~", always compare as string.
4053 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004054 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4056 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004057 n1 = get_tv_number(rettv);
4058 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 switch (type)
4060 {
4061 case TYPE_EQUAL: n1 = (n1 == n2); break;
4062 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4063 case TYPE_GREATER: n1 = (n1 > n2); break;
4064 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4065 case TYPE_SMALLER: n1 = (n1 < n2); break;
4066 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4067 case TYPE_UNKNOWN:
4068 case TYPE_MATCH:
4069 case TYPE_NOMATCH: break; /* avoid gcc warning */
4070 }
4071 }
4072 else
4073 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004074 s1 = get_tv_string_buf(rettv, buf1);
4075 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4077 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4078 else
4079 i = 0;
4080 n1 = FALSE;
4081 switch (type)
4082 {
4083 case TYPE_EQUAL: n1 = (i == 0); break;
4084 case TYPE_NEQUAL: n1 = (i != 0); break;
4085 case TYPE_GREATER: n1 = (i > 0); break;
4086 case TYPE_GEQUAL: n1 = (i >= 0); break;
4087 case TYPE_SMALLER: n1 = (i < 0); break;
4088 case TYPE_SEQUAL: n1 = (i <= 0); break;
4089
4090 case TYPE_MATCH:
4091 case TYPE_NOMATCH:
4092 /* avoid 'l' flag in 'cpoptions' */
4093 save_cpo = p_cpo;
4094 p_cpo = (char_u *)"";
4095 regmatch.regprog = vim_regcomp(s2,
4096 RE_MAGIC + RE_STRING);
4097 regmatch.rm_ic = ic;
4098 if (regmatch.regprog != NULL)
4099 {
4100 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4101 vim_free(regmatch.regprog);
4102 if (type == TYPE_NOMATCH)
4103 n1 = !n1;
4104 }
4105 p_cpo = save_cpo;
4106 break;
4107
4108 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4109 }
4110 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 clear_tv(rettv);
4112 clear_tv(&var2);
4113 rettv->v_type = VAR_NUMBER;
4114 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
4116 }
4117
4118 return OK;
4119}
4120
4121/*
4122 * Handle fourth level expression:
4123 * + number addition
4124 * - number subtraction
4125 * . string concatenation
4126 *
4127 * "arg" must point to the first non-white of the expression.
4128 * "arg" is advanced to the next non-white after the recognized expression.
4129 *
4130 * Return OK or FAIL.
4131 */
4132 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 int evaluate;
4137{
Bram Moolenaar33570922005-01-25 22:26:29 +00004138 typval_T var2;
4139 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 int op;
4141 long n1, n2;
4142 char_u *s1, *s2;
4143 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4144 char_u *p;
4145
4146 /*
4147 * Get the first variable.
4148 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004149 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 return FAIL;
4151
4152 /*
4153 * Repeat computing, until no '+', '-' or '.' is following.
4154 */
4155 for (;;)
4156 {
4157 op = **arg;
4158 if (op != '+' && op != '-' && op != '.')
4159 break;
4160
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004161 if (op != '+' || rettv->v_type != VAR_LIST)
4162 {
4163 /* For "list + ...", an illegal use of the first operand as
4164 * a number cannot be determined before evaluating the 2nd
4165 * operand: if this is also a list, all is ok.
4166 * For "something . ...", "something - ..." or "non-list + ...",
4167 * we know that the first operand needs to be a string or number
4168 * without evaluating the 2nd operand. So check before to avoid
4169 * side effects after an error. */
4170 if (evaluate && get_tv_string_chk(rettv) == NULL)
4171 {
4172 clear_tv(rettv);
4173 return FAIL;
4174 }
4175 }
4176
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 /*
4178 * Get the second variable.
4179 */
4180 *arg = skipwhite(*arg + 1);
4181 if (eval6(arg, &var2, evaluate) == FAIL)
4182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004183 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 return FAIL;
4185 }
4186
4187 if (evaluate)
4188 {
4189 /*
4190 * Compute the result.
4191 */
4192 if (op == '.')
4193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004194 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4195 s2 = get_tv_string_buf_chk(&var2, buf2);
4196 if (s2 == NULL) /* type error ? */
4197 {
4198 clear_tv(rettv);
4199 clear_tv(&var2);
4200 return FAIL;
4201 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004202 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004203 clear_tv(rettv);
4204 rettv->v_type = VAR_STRING;
4205 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004207 else if (op == '+' && rettv->v_type == VAR_LIST
4208 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004209 {
4210 /* concatenate Lists */
4211 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4212 &var3) == FAIL)
4213 {
4214 clear_tv(rettv);
4215 clear_tv(&var2);
4216 return FAIL;
4217 }
4218 clear_tv(rettv);
4219 *rettv = var3;
4220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 else
4222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004223 int error = FALSE;
4224
4225 n1 = get_tv_number_chk(rettv, &error);
4226 if (error)
4227 {
4228 /* This can only happen for "list + non-list".
4229 * For "non-list + ..." or "something - ...", we returned
4230 * before evaluating the 2nd operand. */
4231 clear_tv(rettv);
4232 return FAIL;
4233 }
4234 n2 = get_tv_number_chk(&var2, &error);
4235 if (error)
4236 {
4237 clear_tv(rettv);
4238 clear_tv(&var2);
4239 return FAIL;
4240 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 if (op == '+')
4243 n1 = n1 + n2;
4244 else
4245 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 rettv->v_type = VAR_NUMBER;
4247 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
4251 }
4252 return OK;
4253}
4254
4255/*
4256 * Handle fifth level expression:
4257 * * number multiplication
4258 * / number division
4259 * % number modulo
4260 *
4261 * "arg" must point to the first non-white of the expression.
4262 * "arg" is advanced to the next non-white after the recognized expression.
4263 *
4264 * Return OK or FAIL.
4265 */
4266 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 int evaluate;
4271{
Bram Moolenaar33570922005-01-25 22:26:29 +00004272 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 int op;
4274 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004275 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
4277 /*
4278 * Get the first variable.
4279 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004280 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 return FAIL;
4282
4283 /*
4284 * Repeat computing, until no '*', '/' or '%' is following.
4285 */
4286 for (;;)
4287 {
4288 op = **arg;
4289 if (op != '*' && op != '/' && op != '%')
4290 break;
4291
4292 if (evaluate)
4293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004294 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004296 if (error)
4297 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299 else
4300 n1 = 0;
4301
4302 /*
4303 * Get the second variable.
4304 */
4305 *arg = skipwhite(*arg + 1);
4306 if (eval7(arg, &var2, evaluate) == FAIL)
4307 return FAIL;
4308
4309 if (evaluate)
4310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004311 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004313 if (error)
4314 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
4316 /*
4317 * Compute the result.
4318 */
4319 if (op == '*')
4320 n1 = n1 * n2;
4321 else if (op == '/')
4322 {
4323 if (n2 == 0) /* give an error message? */
4324 n1 = 0x7fffffffL;
4325 else
4326 n1 = n1 / n2;
4327 }
4328 else
4329 {
4330 if (n2 == 0) /* give an error message? */
4331 n1 = 0;
4332 else
4333 n1 = n1 % n2;
4334 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004335 rettv->v_type = VAR_NUMBER;
4336 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 }
4338 }
4339
4340 return OK;
4341}
4342
4343/*
4344 * Handle sixth level expression:
4345 * number number constant
4346 * "string" string contstant
4347 * 'string' literal string contstant
4348 * &option-name option value
4349 * @r register contents
4350 * identifier variable value
4351 * function() function call
4352 * $VAR environment variable
4353 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004354 * [expr, expr] List
4355 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 *
4357 * Also handle:
4358 * ! in front logical NOT
4359 * - in front unary minus
4360 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004361 * trailing [] subscript in String or List
4362 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 *
4364 * "arg" must point to the first non-white of the expression.
4365 * "arg" is advanced to the next non-white after the recognized expression.
4366 *
4367 * Return OK or FAIL.
4368 */
4369 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 int evaluate;
4374{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 long n;
4376 int len;
4377 char_u *s;
4378 int val;
4379 char_u *start_leader, *end_leader;
4380 int ret = OK;
4381 char_u *alias;
4382
4383 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004384 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004385 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004387 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
4389 /*
4390 * Skip '!' and '-' characters. They are handled later.
4391 */
4392 start_leader = *arg;
4393 while (**arg == '!' || **arg == '-' || **arg == '+')
4394 *arg = skipwhite(*arg + 1);
4395 end_leader = *arg;
4396
4397 switch (**arg)
4398 {
4399 /*
4400 * Number constant.
4401 */
4402 case '0':
4403 case '1':
4404 case '2':
4405 case '3':
4406 case '4':
4407 case '5':
4408 case '6':
4409 case '7':
4410 case '8':
4411 case '9':
4412 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4413 *arg += len;
4414 if (evaluate)
4415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004416 rettv->v_type = VAR_NUMBER;
4417 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 }
4419 break;
4420
4421 /*
4422 * String constant: "string".
4423 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004424 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 break;
4426
4427 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004428 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004430 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004431 break;
4432
4433 /*
4434 * List: [expr, expr]
4435 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004436 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 break;
4438
4439 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004440 * Dictionary: {key: val, key: val}
4441 */
4442 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4443 break;
4444
4445 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004446 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004448 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 break;
4450
4451 /*
4452 * Environment variable: $VAR.
4453 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004454 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 break;
4456
4457 /*
4458 * Register contents: @r.
4459 */
4460 case '@': ++*arg;
4461 if (evaluate)
4462 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004463 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004464 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 }
4466 if (**arg != NUL)
4467 ++*arg;
4468 break;
4469
4470 /*
4471 * nested expression: (expression).
4472 */
4473 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 if (**arg == ')')
4476 ++*arg;
4477 else if (ret == OK)
4478 {
4479 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004480 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 ret = FAIL;
4482 }
4483 break;
4484
Bram Moolenaar8c711452005-01-14 21:53:12 +00004485 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 break;
4487 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004488
4489 if (ret == NOTDONE)
4490 {
4491 /*
4492 * Must be a variable or function name.
4493 * Can also be a curly-braces kind of name: {expr}.
4494 */
4495 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004496 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004497 if (alias != NULL)
4498 s = alias;
4499
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004500 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004501 ret = FAIL;
4502 else
4503 {
4504 if (**arg == '(') /* recursive! */
4505 {
4506 /* If "s" is the name of a variable of type VAR_FUNC
4507 * use its contents. */
4508 s = deref_func_name(s, &len);
4509
4510 /* Invoke the function. */
4511 ret = get_func_tv(s, len, rettv, arg,
4512 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004513 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004514 /* Stop the expression evaluation when immediately
4515 * aborting on error, or when an interrupt occurred or
4516 * an exception was thrown but not caught. */
4517 if (aborting())
4518 {
4519 if (ret == OK)
4520 clear_tv(rettv);
4521 ret = FAIL;
4522 }
4523 }
4524 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004525 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004526 else
4527 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004528 }
4529
4530 if (alias != NULL)
4531 vim_free(alias);
4532 }
4533
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 *arg = skipwhite(*arg);
4535
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004536 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4537 * expr(expr). */
4538 if (ret == OK)
4539 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540
4541 /*
4542 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4543 */
4544 if (ret == OK && evaluate && end_leader > start_leader)
4545 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004546 int error = FALSE;
4547
4548 val = get_tv_number_chk(rettv, &error);
4549 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004551 clear_tv(rettv);
4552 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004554 else
4555 {
4556 while (end_leader > start_leader)
4557 {
4558 --end_leader;
4559 if (*end_leader == '!')
4560 val = !val;
4561 else if (*end_leader == '-')
4562 val = -val;
4563 }
4564 clear_tv(rettv);
4565 rettv->v_type = VAR_NUMBER;
4566 rettv->vval.v_number = val;
4567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 }
4569
4570 return ret;
4571}
4572
4573/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004574 * Evaluate an "[expr]" or "[expr:expr]" index.
4575 * "*arg" points to the '['.
4576 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4577 */
4578 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004579eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004580 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004581 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004582 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004583 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004584{
4585 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004586 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004587 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004588 long len = -1;
4589 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004590 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004591 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004592
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004593 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004594 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004595 if (verbose)
4596 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004597 return FAIL;
4598 }
4599
Bram Moolenaar8c711452005-01-14 21:53:12 +00004600 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004601 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004602 /*
4603 * dict.name
4604 */
4605 key = *arg + 1;
4606 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4607 ;
4608 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004610 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004611 }
4612 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004613 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004614 /*
4615 * something[idx]
4616 *
4617 * Get the (first) variable from inside the [].
4618 */
4619 *arg = skipwhite(*arg + 1);
4620 if (**arg == ':')
4621 empty1 = TRUE;
4622 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4623 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004624 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4625 {
4626 /* not a number or string */
4627 clear_tv(&var1);
4628 return FAIL;
4629 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004630
4631 /*
4632 * Get the second variable from inside the [:].
4633 */
4634 if (**arg == ':')
4635 {
4636 range = TRUE;
4637 *arg = skipwhite(*arg + 1);
4638 if (**arg == ']')
4639 empty2 = TRUE;
4640 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4641 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004642 if (!empty1)
4643 clear_tv(&var1);
4644 return FAIL;
4645 }
4646 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4647 {
4648 /* not a number or string */
4649 if (!empty1)
4650 clear_tv(&var1);
4651 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004652 return FAIL;
4653 }
4654 }
4655
4656 /* Check for the ']'. */
4657 if (**arg != ']')
4658 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004659 if (verbose)
4660 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004661 clear_tv(&var1);
4662 if (range)
4663 clear_tv(&var2);
4664 return FAIL;
4665 }
4666 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004667 }
4668
4669 if (evaluate)
4670 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671 n1 = 0;
4672 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004674 n1 = get_tv_number(&var1);
4675 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004676 }
4677 if (range)
4678 {
4679 if (empty2)
4680 n2 = -1;
4681 else
4682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004683 n2 = get_tv_number(&var2);
4684 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004685 }
4686 }
4687
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004688 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004689 {
4690 case VAR_NUMBER:
4691 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004692 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004693 len = (long)STRLEN(s);
4694 if (range)
4695 {
4696 /* The resulting variable is a substring. If the indexes
4697 * are out of range the result is empty. */
4698 if (n1 < 0)
4699 {
4700 n1 = len + n1;
4701 if (n1 < 0)
4702 n1 = 0;
4703 }
4704 if (n2 < 0)
4705 n2 = len + n2;
4706 else if (n2 >= len)
4707 n2 = len;
4708 if (n1 >= len || n2 < 0 || n1 > n2)
4709 s = NULL;
4710 else
4711 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4712 }
4713 else
4714 {
4715 /* The resulting variable is a string of a single
4716 * character. If the index is too big or negative the
4717 * result is empty. */
4718 if (n1 >= len || n1 < 0)
4719 s = NULL;
4720 else
4721 s = vim_strnsave(s + n1, 1);
4722 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004723 clear_tv(rettv);
4724 rettv->v_type = VAR_STRING;
4725 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726 break;
4727
4728 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004729 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004730 if (n1 < 0)
4731 n1 = len + n1;
4732 if (!empty1 && (n1 < 0 || n1 >= len))
4733 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004734 if (verbose)
4735 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004736 return FAIL;
4737 }
4738 if (range)
4739 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004740 list_T *l;
4741 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004742
4743 if (n2 < 0)
4744 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004745 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004746 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004747 if (verbose)
4748 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749 return FAIL;
4750 }
4751 l = list_alloc();
4752 if (l == NULL)
4753 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755 n1 <= n2; ++n1)
4756 {
4757 if (list_append_tv(l, &item->li_tv) == FAIL)
4758 {
4759 list_free(l);
4760 return FAIL;
4761 }
4762 item = item->li_next;
4763 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 clear_tv(rettv);
4765 rettv->v_type = VAR_LIST;
4766 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004767 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 }
4769 else
4770 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004771 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004773 clear_tv(rettv);
4774 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775 }
4776 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004777
4778 case VAR_DICT:
4779 if (range)
4780 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004781 if (verbose)
4782 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004783 if (len == -1)
4784 clear_tv(&var1);
4785 return FAIL;
4786 }
4787 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004788 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004789
4790 if (len == -1)
4791 {
4792 key = get_tv_string(&var1);
4793 if (*key == NUL)
4794 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004795 if (verbose)
4796 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004797 clear_tv(&var1);
4798 return FAIL;
4799 }
4800 }
4801
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004802 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004803
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004804 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004805 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004806 if (len == -1)
4807 clear_tv(&var1);
4808 if (item == NULL)
4809 return FAIL;
4810
4811 copy_tv(&item->di_tv, &var1);
4812 clear_tv(rettv);
4813 *rettv = var1;
4814 }
4815 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004816 }
4817 }
4818
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004819 return OK;
4820}
4821
4822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 * Get an option value.
4824 * "arg" points to the '&' or '+' before the option name.
4825 * "arg" is advanced to character after the option name.
4826 * Return OK or FAIL.
4827 */
4828 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004831 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 int evaluate;
4833{
4834 char_u *option_end;
4835 long numval;
4836 char_u *stringval;
4837 int opt_type;
4838 int c;
4839 int working = (**arg == '+'); /* has("+option") */
4840 int ret = OK;
4841 int opt_flags;
4842
4843 /*
4844 * Isolate the option name and find its value.
4845 */
4846 option_end = find_option_end(arg, &opt_flags);
4847 if (option_end == NULL)
4848 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004849 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 EMSG2(_("E112: Option name missing: %s"), *arg);
4851 return FAIL;
4852 }
4853
4854 if (!evaluate)
4855 {
4856 *arg = option_end;
4857 return OK;
4858 }
4859
4860 c = *option_end;
4861 *option_end = NUL;
4862 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004863 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864
4865 if (opt_type == -3) /* invalid name */
4866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004867 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 EMSG2(_("E113: Unknown option: %s"), *arg);
4869 ret = FAIL;
4870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004871 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
4873 if (opt_type == -2) /* hidden string option */
4874 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004875 rettv->v_type = VAR_STRING;
4876 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878 else if (opt_type == -1) /* hidden number option */
4879 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880 rettv->v_type = VAR_NUMBER;
4881 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
4883 else if (opt_type == 1) /* number option */
4884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004885 rettv->v_type = VAR_NUMBER;
4886 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
4888 else /* string option */
4889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004890 rettv->v_type = VAR_STRING;
4891 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 }
4893 }
4894 else if (working && (opt_type == -2 || opt_type == -1))
4895 ret = FAIL;
4896
4897 *option_end = c; /* put back for error messages */
4898 *arg = option_end;
4899
4900 return ret;
4901}
4902
4903/*
4904 * Allocate a variable for a string constant.
4905 * Return OK or FAIL.
4906 */
4907 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004908get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 int evaluate;
4912{
4913 char_u *p;
4914 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 int extra = 0;
4916
4917 /*
4918 * Find the end of the string, skipping backslashed characters.
4919 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004920 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 {
4922 if (*p == '\\' && p[1] != NUL)
4923 {
4924 ++p;
4925 /* A "\<x>" form occupies at least 4 characters, and produces up
4926 * to 6 characters: reserve space for 2 extra */
4927 if (*p == '<')
4928 extra += 2;
4929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
4931
4932 if (*p != '"')
4933 {
4934 EMSG2(_("E114: Missing quote: %s"), *arg);
4935 return FAIL;
4936 }
4937
4938 /* If only parsing, set *arg and return here */
4939 if (!evaluate)
4940 {
4941 *arg = p + 1;
4942 return OK;
4943 }
4944
4945 /*
4946 * Copy the string into allocated memory, handling backslashed
4947 * characters.
4948 */
4949 name = alloc((unsigned)(p - *arg + extra));
4950 if (name == NULL)
4951 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004952 rettv->v_type = VAR_STRING;
4953 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 {
4957 if (*p == '\\')
4958 {
4959 switch (*++p)
4960 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004961 case 'b': *name++ = BS; ++p; break;
4962 case 'e': *name++ = ESC; ++p; break;
4963 case 'f': *name++ = FF; ++p; break;
4964 case 'n': *name++ = NL; ++p; break;
4965 case 'r': *name++ = CAR; ++p; break;
4966 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967
4968 case 'X': /* hex: "\x1", "\x12" */
4969 case 'x':
4970 case 'u': /* Unicode: "\u0023" */
4971 case 'U':
4972 if (vim_isxdigit(p[1]))
4973 {
4974 int n, nr;
4975 int c = toupper(*p);
4976
4977 if (c == 'X')
4978 n = 2;
4979 else
4980 n = 4;
4981 nr = 0;
4982 while (--n >= 0 && vim_isxdigit(p[1]))
4983 {
4984 ++p;
4985 nr = (nr << 4) + hex2nr(*p);
4986 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988#ifdef FEAT_MBYTE
4989 /* For "\u" store the number according to
4990 * 'encoding'. */
4991 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004992 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 else
4994#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 break;
4998
4999 /* octal: "\1", "\12", "\123" */
5000 case '0':
5001 case '1':
5002 case '2':
5003 case '3':
5004 case '4':
5005 case '5':
5006 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005007 case '7': *name = *p++ - '0';
5008 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005010 *name = (*name << 3) + *p++ - '0';
5011 if (*p >= '0' && *p <= '7')
5012 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 break;
5016
5017 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005018 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (extra != 0)
5020 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005021 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 break;
5023 }
5024 /* FALLTHROUGH */
5025
Bram Moolenaar8c711452005-01-14 21:53:12 +00005026 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 break;
5028 }
5029 }
5030 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005034 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 *arg = p + 1;
5036
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 return OK;
5038}
5039
5040/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 * Return OK or FAIL.
5043 */
5044 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005045get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 int evaluate;
5049{
5050 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005051 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005052 int reduce = 0;
5053
5054 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005056 */
5057 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5058 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005060 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005062 break;
5063 ++reduce;
5064 ++p;
5065 }
5066 }
5067
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005069 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005071 return FAIL;
5072 }
5073
Bram Moolenaar8c711452005-01-14 21:53:12 +00005074 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005075 if (!evaluate)
5076 {
5077 *arg = p + 1;
5078 return OK;
5079 }
5080
5081 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005083 */
5084 str = alloc((unsigned)((p - *arg) - reduce));
5085 if (str == NULL)
5086 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 rettv->v_type = VAR_STRING;
5088 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005089
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005091 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005093 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005095 break;
5096 ++p;
5097 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005099 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005101 *arg = p + 1;
5102
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005103 return OK;
5104}
5105
5106/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005107 * Allocate a variable for a List and fill it from "*arg".
5108 * Return OK or FAIL.
5109 */
5110 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005111get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005112 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005113 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005114 int evaluate;
5115{
Bram Moolenaar33570922005-01-25 22:26:29 +00005116 list_T *l = NULL;
5117 typval_T tv;
5118 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005119
5120 if (evaluate)
5121 {
5122 l = list_alloc();
5123 if (l == NULL)
5124 return FAIL;
5125 }
5126
5127 *arg = skipwhite(*arg + 1);
5128 while (**arg != ']' && **arg != NUL)
5129 {
5130 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5131 goto failret;
5132 if (evaluate)
5133 {
5134 item = listitem_alloc();
5135 if (item != NULL)
5136 {
5137 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005138 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005139 list_append(l, item);
5140 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005141 else
5142 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143 }
5144
5145 if (**arg == ']')
5146 break;
5147 if (**arg != ',')
5148 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005150 goto failret;
5151 }
5152 *arg = skipwhite(*arg + 1);
5153 }
5154
5155 if (**arg != ']')
5156 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158failret:
5159 if (evaluate)
5160 list_free(l);
5161 return FAIL;
5162 }
5163
5164 *arg = skipwhite(*arg + 1);
5165 if (evaluate)
5166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167 rettv->v_type = VAR_LIST;
5168 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005169 ++l->lv_refcount;
5170 }
5171
5172 return OK;
5173}
5174
5175/*
5176 * Allocate an empty header for a list.
5177 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005178 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179list_alloc()
5180{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005181 list_T *l;
5182
5183 l = (list_T *)alloc_clear(sizeof(list_T));
5184 if (l != NULL)
5185 {
5186 /* Prepend the list to the list of lists for garbage collection. */
5187 if (first_list != NULL)
5188 first_list->lv_used_prev = l;
5189 l->lv_used_prev = NULL;
5190 l->lv_used_next = first_list;
5191 first_list = l;
5192 }
5193 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005194}
5195
5196/*
5197 * Unreference a list: decrement the reference count and free it when it
5198 * becomes zero.
5199 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005200 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005201list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005202 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005203{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005204 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5205 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005206}
5207
5208/*
5209 * Free a list, including all items it points to.
5210 * Ignores the reference count.
5211 */
5212 static void
5213list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005214 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005215{
Bram Moolenaar33570922005-01-25 22:26:29 +00005216 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005217
Bram Moolenaard9fba312005-06-26 22:34:35 +00005218 /* Avoid that recursive reference to the list frees us again. */
5219 l->lv_refcount = DEL_REFCOUNT;
5220
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005221 /* Remove the list from the list of lists for garbage collection. */
5222 if (l->lv_used_prev == NULL)
5223 first_list = l->lv_used_next;
5224 else
5225 l->lv_used_prev->lv_used_next = l->lv_used_next;
5226 if (l->lv_used_next != NULL)
5227 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5228
Bram Moolenaard9fba312005-06-26 22:34:35 +00005229 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005230 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005231 /* Remove the item before deleting it. */
5232 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005233 listitem_free(item);
5234 }
5235 vim_free(l);
5236}
5237
5238/*
5239 * Allocate a list item.
5240 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005241 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005242listitem_alloc()
5243{
Bram Moolenaar33570922005-01-25 22:26:29 +00005244 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005245}
5246
5247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005248 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249 */
5250 static void
5251listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005252 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005254 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 vim_free(item);
5256}
5257
5258/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005259 * Remove a list item from a List and free it. Also clears the value.
5260 */
5261 static void
5262listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005263 list_T *l;
5264 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005265{
5266 list_remove(l, item, item);
5267 listitem_free(item);
5268}
5269
5270/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 * Get the number of items in a list.
5272 */
5273 static long
5274list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005275 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 if (l == NULL)
5278 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005279 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280}
5281
5282/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005283 * Return TRUE when two lists have exactly the same values.
5284 */
5285 static int
5286list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005287 list_T *l1;
5288 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005289 int ic; /* ignore case for strings */
5290{
Bram Moolenaar33570922005-01-25 22:26:29 +00005291 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005292
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005293 if (list_len(l1) != list_len(l2))
5294 return FALSE;
5295
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005296 for (item1 = l1->lv_first, item2 = l2->lv_first;
5297 item1 != NULL && item2 != NULL;
5298 item1 = item1->li_next, item2 = item2->li_next)
5299 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5300 return FALSE;
5301 return item1 == NULL && item2 == NULL;
5302}
5303
5304/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005305 * Return TRUE when two dictionaries have exactly the same key/values.
5306 */
5307 static int
5308dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005309 dict_T *d1;
5310 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005311 int ic; /* ignore case for strings */
5312{
Bram Moolenaar33570922005-01-25 22:26:29 +00005313 hashitem_T *hi;
5314 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005315 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005316
5317 if (dict_len(d1) != dict_len(d2))
5318 return FALSE;
5319
Bram Moolenaar33570922005-01-25 22:26:29 +00005320 todo = d1->dv_hashtab.ht_used;
5321 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005322 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005323 if (!HASHITEM_EMPTY(hi))
5324 {
5325 item2 = dict_find(d2, hi->hi_key, -1);
5326 if (item2 == NULL)
5327 return FALSE;
5328 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5329 return FALSE;
5330 --todo;
5331 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005332 }
5333 return TRUE;
5334}
5335
5336/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005337 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005338 * Compares the items just like "==" would compare them, but strings and
5339 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005340 */
5341 static int
5342tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005343 typval_T *tv1;
5344 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005345 int ic; /* ignore case */
5346{
5347 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005348 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005349
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005350 if (tv1->v_type != tv2->v_type)
5351 return FALSE;
5352
5353 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005354 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005355 case VAR_LIST:
5356 /* recursive! */
5357 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5358
5359 case VAR_DICT:
5360 /* recursive! */
5361 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5362
5363 case VAR_FUNC:
5364 return (tv1->vval.v_string != NULL
5365 && tv2->vval.v_string != NULL
5366 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5367
5368 case VAR_NUMBER:
5369 return tv1->vval.v_number == tv2->vval.v_number;
5370
5371 case VAR_STRING:
5372 s1 = get_tv_string_buf(tv1, buf1);
5373 s2 = get_tv_string_buf(tv2, buf2);
5374 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005375 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005376
5377 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005378 return TRUE;
5379}
5380
5381/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 * Locate item with index "n" in list "l" and return it.
5383 * A negative index is counted from the end; -1 is the last item.
5384 * Returns NULL when "n" is out of range.
5385 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005386 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005388 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389 long n;
5390{
Bram Moolenaar33570922005-01-25 22:26:29 +00005391 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392 long idx;
5393
5394 if (l == NULL)
5395 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005396
5397 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005399 n = l->lv_len + n;
5400
5401 /* Check for index out of range. */
5402 if (n < 0 || n >= l->lv_len)
5403 return NULL;
5404
5405 /* When there is a cached index may start search from there. */
5406 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005408 if (n < l->lv_idx / 2)
5409 {
5410 /* closest to the start of the list */
5411 item = l->lv_first;
5412 idx = 0;
5413 }
5414 else if (n > (l->lv_idx + l->lv_len) / 2)
5415 {
5416 /* closest to the end of the list */
5417 item = l->lv_last;
5418 idx = l->lv_len - 1;
5419 }
5420 else
5421 {
5422 /* closest to the cached index */
5423 item = l->lv_idx_item;
5424 idx = l->lv_idx;
5425 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 }
5427 else
5428 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005429 if (n < l->lv_len / 2)
5430 {
5431 /* closest to the start of the list */
5432 item = l->lv_first;
5433 idx = 0;
5434 }
5435 else
5436 {
5437 /* closest to the end of the list */
5438 item = l->lv_last;
5439 idx = l->lv_len - 1;
5440 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005442
5443 while (n > idx)
5444 {
5445 /* search forward */
5446 item = item->li_next;
5447 ++idx;
5448 }
5449 while (n < idx)
5450 {
5451 /* search backward */
5452 item = item->li_prev;
5453 --idx;
5454 }
5455
5456 /* cache the used index */
5457 l->lv_idx = idx;
5458 l->lv_idx_item = item;
5459
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 return item;
5461}
5462
5463/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005464 * Locate "item" list "l" and return its index.
5465 * Returns -1 when "item" is not in the list.
5466 */
5467 static long
5468list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005469 list_T *l;
5470 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005471{
5472 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005474
5475 if (l == NULL)
5476 return -1;
5477 idx = 0;
5478 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5479 ++idx;
5480 if (li == NULL)
5481 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005482 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005483}
5484
5485/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486 * Append item "item" to the end of list "l".
5487 */
5488 static void
5489list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005490 list_T *l;
5491 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492{
5493 if (l->lv_last == NULL)
5494 {
5495 /* empty list */
5496 l->lv_first = item;
5497 l->lv_last = item;
5498 item->li_prev = NULL;
5499 }
5500 else
5501 {
5502 l->lv_last->li_next = item;
5503 item->li_prev = l->lv_last;
5504 l->lv_last = item;
5505 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005506 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005507 item->li_next = NULL;
5508}
5509
5510/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005511 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005512 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 */
5514 static int
5515list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005516 list_T *l;
5517 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005519 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520
Bram Moolenaar05159a02005-02-26 23:04:13 +00005521 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005523 copy_tv(tv, &li->li_tv);
5524 list_append(l, li);
5525 return OK;
5526}
5527
5528/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005529 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005530 * Return FAIL when out of memory.
5531 */
5532 int
5533list_append_dict(list, dict)
5534 list_T *list;
5535 dict_T *dict;
5536{
5537 listitem_T *li = listitem_alloc();
5538
5539 if (li == NULL)
5540 return FAIL;
5541 li->li_tv.v_type = VAR_DICT;
5542 li->li_tv.v_lock = 0;
5543 li->li_tv.vval.v_dict = dict;
5544 list_append(list, li);
5545 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 return OK;
5547}
5548
5549/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005550 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005551 * If "item" is NULL append at the end.
5552 * Return FAIL when out of memory.
5553 */
5554 static int
5555list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005556 list_T *l;
5557 typval_T *tv;
5558 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005559{
Bram Moolenaar33570922005-01-25 22:26:29 +00005560 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005561
5562 if (ni == NULL)
5563 return FAIL;
5564 copy_tv(tv, &ni->li_tv);
5565 if (item == NULL)
5566 /* Append new item at end of list. */
5567 list_append(l, ni);
5568 else
5569 {
5570 /* Insert new item before existing item. */
5571 ni->li_prev = item->li_prev;
5572 ni->li_next = item;
5573 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005574 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005575 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005576 ++l->lv_idx;
5577 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005578 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005579 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005580 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005581 l->lv_idx_item = NULL;
5582 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005583 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005584 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005585 }
5586 return OK;
5587}
5588
5589/*
5590 * Extend "l1" with "l2".
5591 * If "bef" is NULL append at the end, otherwise insert before this item.
5592 * Returns FAIL when out of memory.
5593 */
5594 static int
5595list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005596 list_T *l1;
5597 list_T *l2;
5598 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005599{
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005601
5602 for (item = l2->lv_first; item != NULL; item = item->li_next)
5603 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5604 return FAIL;
5605 return OK;
5606}
5607
5608/*
5609 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5610 * Return FAIL when out of memory.
5611 */
5612 static int
5613list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005614 list_T *l1;
5615 list_T *l2;
5616 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005617{
Bram Moolenaar33570922005-01-25 22:26:29 +00005618 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005619
5620 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005621 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005622 if (l == NULL)
5623 return FAIL;
5624 tv->v_type = VAR_LIST;
5625 tv->vval.v_list = l;
5626
5627 /* append all items from the second list */
5628 return list_extend(l, l2, NULL);
5629}
5630
5631/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005632 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005633 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005634 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005635 * Returns NULL when out of memory.
5636 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005637 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005638list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005639 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005641 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005642{
Bram Moolenaar33570922005-01-25 22:26:29 +00005643 list_T *copy;
5644 listitem_T *item;
5645 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646
5647 if (orig == NULL)
5648 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005649
5650 copy = list_alloc();
5651 if (copy != NULL)
5652 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005653 if (copyID != 0)
5654 {
5655 /* Do this before adding the items, because one of the items may
5656 * refer back to this list. */
5657 orig->lv_copyID = copyID;
5658 orig->lv_copylist = copy;
5659 }
5660 for (item = orig->lv_first; item != NULL && !got_int;
5661 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005662 {
5663 ni = listitem_alloc();
5664 if (ni == NULL)
5665 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005666 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005667 {
5668 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5669 {
5670 vim_free(ni);
5671 break;
5672 }
5673 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005674 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005676 list_append(copy, ni);
5677 }
5678 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005679 if (item != NULL)
5680 {
5681 list_unref(copy);
5682 copy = NULL;
5683 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005684 }
5685
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005686 return copy;
5687}
5688
5689/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005690 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005691 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005692 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005693 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005694list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005695 list_T *l;
5696 listitem_T *item;
5697 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005698{
Bram Moolenaar33570922005-01-25 22:26:29 +00005699 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005700
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005701 /* notify watchers */
5702 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005703 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005704 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005705 list_fix_watch(l, ip);
5706 if (ip == item2)
5707 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005708 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005709
5710 if (item2->li_next == NULL)
5711 l->lv_last = item->li_prev;
5712 else
5713 item2->li_next->li_prev = item->li_prev;
5714 if (item->li_prev == NULL)
5715 l->lv_first = item2->li_next;
5716 else
5717 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005718 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005719}
5720
5721/*
5722 * Return an allocated string with the string representation of a list.
5723 * May return NULL.
5724 */
5725 static char_u *
5726list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005727 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728{
5729 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005730
5731 if (tv->vval.v_list == NULL)
5732 return NULL;
5733 ga_init2(&ga, (int)sizeof(char), 80);
5734 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005735 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5736 {
5737 vim_free(ga.ga_data);
5738 return NULL;
5739 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005740 ga_append(&ga, ']');
5741 ga_append(&ga, NUL);
5742 return (char_u *)ga.ga_data;
5743}
5744
5745/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005746 * Join list "l" into a string in "*gap", using separator "sep".
5747 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005748 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005749 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005750 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005751list_join(gap, l, sep, echo)
5752 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005753 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 char_u *sep;
5755 int echo;
5756{
5757 int first = TRUE;
5758 char_u *tofree;
5759 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005760 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005761 char_u *s;
5762
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005763 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005764 {
5765 if (first)
5766 first = FALSE;
5767 else
5768 ga_concat(gap, sep);
5769
5770 if (echo)
5771 s = echo_string(&item->li_tv, &tofree, numbuf);
5772 else
5773 s = tv2string(&item->li_tv, &tofree, numbuf);
5774 if (s != NULL)
5775 ga_concat(gap, s);
5776 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005777 if (s == NULL)
5778 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005779 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005780 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005781}
5782
5783/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005784 * Garbage collection for lists and dictionaries.
5785 *
5786 * We use reference counts to be able to free most items right away when they
5787 * are no longer used. But for composite items it's possible that it becomes
5788 * unused while the reference count is > 0: When there is a recursive
5789 * reference. Example:
5790 * :let l = [1, 2, 3]
5791 * :let d = {9: l}
5792 * :let l[1] = d
5793 *
5794 * Since this is quite unusual we handle this with garbage collection: every
5795 * once in a while find out which lists and dicts are not referenced from any
5796 * variable.
5797 *
5798 * Here is a good reference text about garbage collection (refers to Python
5799 * but it applies to all reference-counting mechanisms):
5800 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005801 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005802
5803/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005804 * Do garbage collection for lists and dicts.
5805 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005806 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005807 int
5808garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005809{
5810 dict_T *dd;
5811 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005812 int copyID = ++current_copyID;
5813 buf_T *buf;
5814 win_T *wp;
5815 int i;
5816 funccall_T *fc;
5817 int did_free = FALSE;
5818
5819 /*
5820 * 1. Go through all accessible variables and mark all lists and dicts
5821 * with copyID.
5822 */
5823 /* script-local variables */
5824 for (i = 1; i <= ga_scripts.ga_len; ++i)
5825 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5826
5827 /* buffer-local variables */
5828 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5829 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5830
5831 /* window-local variables */
5832 FOR_ALL_WINDOWS(wp)
5833 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5834
5835 /* global variables */
5836 set_ref_in_ht(&globvarht, copyID);
5837
5838 /* function-local variables */
5839 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5840 {
5841 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5842 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5843 }
5844
5845 /*
5846 * 2. Go through the list of dicts and free items without the copyID.
5847 */
5848 for (dd = first_dict; dd != NULL; )
5849 if (dd->dv_copyID != copyID)
5850 {
5851 dict_free(dd);
5852 did_free = TRUE;
5853
5854 /* restart, next dict may also have been freed */
5855 dd = first_dict;
5856 }
5857 else
5858 dd = dd->dv_used_next;
5859
5860 /*
5861 * 3. Go through the list of lists and free items without the copyID.
5862 */
5863 for (ll = first_list; ll != NULL; )
5864 if (ll->lv_copyID != copyID)
5865 {
5866 list_free(ll);
5867 did_free = TRUE;
5868
5869 /* restart, next dict may also have been freed */
5870 ll = first_list;
5871 }
5872 else
5873 ll = ll->lv_used_next;
5874
5875 return did_free;
5876}
5877
5878/*
5879 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5880 */
5881 static void
5882set_ref_in_ht(ht, copyID)
5883 hashtab_T *ht;
5884 int copyID;
5885{
5886 int todo;
5887 hashitem_T *hi;
5888
5889 todo = ht->ht_used;
5890 for (hi = ht->ht_array; todo > 0; ++hi)
5891 if (!HASHITEM_EMPTY(hi))
5892 {
5893 --todo;
5894 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5895 }
5896}
5897
5898/*
5899 * Mark all lists and dicts referenced through list "l" with "copyID".
5900 */
5901 static void
5902set_ref_in_list(l, copyID)
5903 list_T *l;
5904 int copyID;
5905{
5906 listitem_T *li;
5907
5908 for (li = l->lv_first; li != NULL; li = li->li_next)
5909 set_ref_in_item(&li->li_tv, copyID);
5910}
5911
5912/*
5913 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5914 */
5915 static void
5916set_ref_in_item(tv, copyID)
5917 typval_T *tv;
5918 int copyID;
5919{
5920 dict_T *dd;
5921 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005922
5923 switch (tv->v_type)
5924 {
5925 case VAR_DICT:
5926 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005927 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005928 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005929 /* Didn't see this dict yet. */
5930 dd->dv_copyID = copyID;
5931 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005932 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005933 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005934
5935 case VAR_LIST:
5936 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005937 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005938 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005939 /* Didn't see this list yet. */
5940 ll->lv_copyID = copyID;
5941 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005942 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005943 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005944 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005945 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005946}
5947
5948/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005949 * Allocate an empty header for a dictionary.
5950 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005951 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005952dict_alloc()
5953{
Bram Moolenaar33570922005-01-25 22:26:29 +00005954 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005955
Bram Moolenaar33570922005-01-25 22:26:29 +00005956 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005957 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005958 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005959 /* Add the list to the hashtable for garbage collection. */
5960 if (first_dict != NULL)
5961 first_dict->dv_used_prev = d;
5962 d->dv_used_next = first_dict;
5963 d->dv_used_prev = NULL;
5964
Bram Moolenaar33570922005-01-25 22:26:29 +00005965 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005966 d->dv_lock = 0;
5967 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005968 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005969 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005970 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005971}
5972
5973/*
5974 * Unreference a Dictionary: decrement the reference count and free it when it
5975 * becomes zero.
5976 */
5977 static void
5978dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005979 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005980{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005981 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
5982 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005983}
5984
5985/*
5986 * Free a Dictionary, including all items it contains.
5987 * Ignores the reference count.
5988 */
5989 static void
5990dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005991 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005992{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005993 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005994 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005995 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005996
Bram Moolenaard9fba312005-06-26 22:34:35 +00005997 /* Avoid that recursive reference to the dict frees us again. */
5998 d->dv_refcount = DEL_REFCOUNT;
5999
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006000 /* Remove the dict from the list of dicts for garbage collection. */
6001 if (d->dv_used_prev == NULL)
6002 first_dict = d->dv_used_next;
6003 else
6004 d->dv_used_prev->dv_used_next = d->dv_used_next;
6005 if (d->dv_used_next != NULL)
6006 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6007
6008 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006009 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 todo = d->dv_hashtab.ht_used;
6011 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006012 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006013 if (!HASHITEM_EMPTY(hi))
6014 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006015 /* Remove the item before deleting it, just in case there is
6016 * something recursive causing trouble. */
6017 di = HI2DI(hi);
6018 hash_remove(&d->dv_hashtab, hi);
6019 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006020 --todo;
6021 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006022 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006024 vim_free(d);
6025}
6026
6027/*
6028 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006029 * The "key" is copied to the new item.
6030 * Note that the value of the item "di_tv" still needs to be initialized!
6031 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006032 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006034dictitem_alloc(key)
6035 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006036{
Bram Moolenaar33570922005-01-25 22:26:29 +00006037 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006038
Bram Moolenaar33570922005-01-25 22:26:29 +00006039 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006040 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006041 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006042 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006043 di->di_flags = 0;
6044 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006045 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006046}
6047
6048/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006049 * Make a copy of a Dictionary item.
6050 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006051 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006052dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006054{
Bram Moolenaar33570922005-01-25 22:26:29 +00006055 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006056
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006058 if (di != NULL)
6059 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006060 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006061 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006062 copy_tv(&org->di_tv, &di->di_tv);
6063 }
6064 return di;
6065}
6066
6067/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006068 * Remove item "item" from Dictionary "dict" and free it.
6069 */
6070 static void
6071dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006072 dict_T *dict;
6073 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006074{
Bram Moolenaar33570922005-01-25 22:26:29 +00006075 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006076
Bram Moolenaar33570922005-01-25 22:26:29 +00006077 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006078 if (HASHITEM_EMPTY(hi))
6079 EMSG2(_(e_intern2), "dictitem_remove()");
6080 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006082 dictitem_free(item);
6083}
6084
6085/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006086 * Free a dict item. Also clears the value.
6087 */
6088 static void
6089dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006090 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006091{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006092 clear_tv(&item->di_tv);
6093 vim_free(item);
6094}
6095
6096/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006097 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6098 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006099 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006100 * Returns NULL when out of memory.
6101 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006103dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006105 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006106 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006107{
Bram Moolenaar33570922005-01-25 22:26:29 +00006108 dict_T *copy;
6109 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006110 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006111 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006112
6113 if (orig == NULL)
6114 return NULL;
6115
6116 copy = dict_alloc();
6117 if (copy != NULL)
6118 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006119 if (copyID != 0)
6120 {
6121 orig->dv_copyID = copyID;
6122 orig->dv_copydict = copy;
6123 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006124 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006125 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006126 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006127 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006128 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006129 --todo;
6130
6131 di = dictitem_alloc(hi->hi_key);
6132 if (di == NULL)
6133 break;
6134 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006135 {
6136 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6137 copyID) == FAIL)
6138 {
6139 vim_free(di);
6140 break;
6141 }
6142 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006143 else
6144 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6145 if (dict_add(copy, di) == FAIL)
6146 {
6147 dictitem_free(di);
6148 break;
6149 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006150 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006151 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006152
Bram Moolenaare9a41262005-01-15 22:18:47 +00006153 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006154 if (todo > 0)
6155 {
6156 dict_unref(copy);
6157 copy = NULL;
6158 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006159 }
6160
6161 return copy;
6162}
6163
6164/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006165 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006166 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006167 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006168 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006169dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006170 dict_T *d;
6171 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006172{
Bram Moolenaar33570922005-01-25 22:26:29 +00006173 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006174}
6175
Bram Moolenaar8c711452005-01-14 21:53:12 +00006176/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006177 * Add a number or string entry to dictionary "d".
6178 * When "str" is NULL use number "nr", otherwise use "str".
6179 * Returns FAIL when out of memory and when key already exists.
6180 */
6181 int
6182dict_add_nr_str(d, key, nr, str)
6183 dict_T *d;
6184 char *key;
6185 long nr;
6186 char_u *str;
6187{
6188 dictitem_T *item;
6189
6190 item = dictitem_alloc((char_u *)key);
6191 if (item == NULL)
6192 return FAIL;
6193 item->di_tv.v_lock = 0;
6194 if (str == NULL)
6195 {
6196 item->di_tv.v_type = VAR_NUMBER;
6197 item->di_tv.vval.v_number = nr;
6198 }
6199 else
6200 {
6201 item->di_tv.v_type = VAR_STRING;
6202 item->di_tv.vval.v_string = vim_strsave(str);
6203 }
6204 if (dict_add(d, item) == FAIL)
6205 {
6206 dictitem_free(item);
6207 return FAIL;
6208 }
6209 return OK;
6210}
6211
6212/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006213 * Get the number of items in a Dictionary.
6214 */
6215 static long
6216dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006217 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006218{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006219 if (d == NULL)
6220 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006221 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006222}
6223
6224/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006225 * Find item "key[len]" in Dictionary "d".
6226 * If "len" is negative use strlen(key).
6227 * Returns NULL when not found.
6228 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006229 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006230dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006231 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006232 char_u *key;
6233 int len;
6234{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006235#define AKEYLEN 200
6236 char_u buf[AKEYLEN];
6237 char_u *akey;
6238 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006239 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006240
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006241 if (len < 0)
6242 akey = key;
6243 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006244 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006245 tofree = akey = vim_strnsave(key, len);
6246 if (akey == NULL)
6247 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006248 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006249 else
6250 {
6251 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006252 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006253 akey = buf;
6254 }
6255
Bram Moolenaar33570922005-01-25 22:26:29 +00006256 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006257 vim_free(tofree);
6258 if (HASHITEM_EMPTY(hi))
6259 return NULL;
6260 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006261}
6262
6263/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006264 * Get a string item from a dictionary in allocated memory.
6265 * Returns NULL if the entry doesn't exist or out of memory.
6266 */
6267 char_u *
6268get_dict_string(d, key)
6269 dict_T *d;
6270 char_u *key;
6271{
6272 dictitem_T *di;
6273
6274 di = dict_find(d, key, -1);
6275 if (di == NULL)
6276 return NULL;
6277 return vim_strsave(get_tv_string(&di->di_tv));
6278}
6279
6280/*
6281 * Get a number item from a dictionary.
6282 * Returns 0 if the entry doesn't exist or out of memory.
6283 */
6284 long
6285get_dict_number(d, key)
6286 dict_T *d;
6287 char_u *key;
6288{
6289 dictitem_T *di;
6290
6291 di = dict_find(d, key, -1);
6292 if (di == NULL)
6293 return 0;
6294 return get_tv_number(&di->di_tv);
6295}
6296
6297/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006298 * Return an allocated string with the string representation of a Dictionary.
6299 * May return NULL.
6300 */
6301 static char_u *
6302dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006303 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006304{
6305 garray_T ga;
6306 int first = TRUE;
6307 char_u *tofree;
6308 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006309 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006310 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006311 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006312 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006313
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006314 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006315 return NULL;
6316 ga_init2(&ga, (int)sizeof(char), 80);
6317 ga_append(&ga, '{');
6318
Bram Moolenaar33570922005-01-25 22:26:29 +00006319 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006320 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006321 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006322 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006323 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006324 --todo;
6325
6326 if (first)
6327 first = FALSE;
6328 else
6329 ga_concat(&ga, (char_u *)", ");
6330
6331 tofree = string_quote(hi->hi_key, FALSE);
6332 if (tofree != NULL)
6333 {
6334 ga_concat(&ga, tofree);
6335 vim_free(tofree);
6336 }
6337 ga_concat(&ga, (char_u *)": ");
6338 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6339 if (s != NULL)
6340 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006341 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006342 if (s == NULL)
6343 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006344 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006345 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006346 if (todo > 0)
6347 {
6348 vim_free(ga.ga_data);
6349 return NULL;
6350 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006351
6352 ga_append(&ga, '}');
6353 ga_append(&ga, NUL);
6354 return (char_u *)ga.ga_data;
6355}
6356
6357/*
6358 * Allocate a variable for a Dictionary and fill it from "*arg".
6359 * Return OK or FAIL. Returns NOTDONE for {expr}.
6360 */
6361 static int
6362get_dict_tv(arg, rettv, evaluate)
6363 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006364 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006365 int evaluate;
6366{
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 dict_T *d = NULL;
6368 typval_T tvkey;
6369 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006370 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006371 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006372 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006373 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006374
6375 /*
6376 * First check if it's not a curly-braces thing: {expr}.
6377 * Must do this without evaluating, otherwise a function may be called
6378 * twice. Unfortunately this means we need to call eval1() twice for the
6379 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006380 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006381 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006382 if (*start != '}')
6383 {
6384 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6385 return FAIL;
6386 if (*start == '}')
6387 return NOTDONE;
6388 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006389
6390 if (evaluate)
6391 {
6392 d = dict_alloc();
6393 if (d == NULL)
6394 return FAIL;
6395 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006396 tvkey.v_type = VAR_UNKNOWN;
6397 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006398
6399 *arg = skipwhite(*arg + 1);
6400 while (**arg != '}' && **arg != NUL)
6401 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006402 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006403 goto failret;
6404 if (**arg != ':')
6405 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006406 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006407 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006408 goto failret;
6409 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006410 key = get_tv_string_buf_chk(&tvkey, buf);
6411 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006412 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006413 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6414 if (key != NULL)
6415 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006416 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006417 goto failret;
6418 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006419
6420 *arg = skipwhite(*arg + 1);
6421 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6422 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006423 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006424 goto failret;
6425 }
6426 if (evaluate)
6427 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006428 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006429 if (item != NULL)
6430 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006431 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006432 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006433 clear_tv(&tv);
6434 goto failret;
6435 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006436 item = dictitem_alloc(key);
6437 clear_tv(&tvkey);
6438 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006439 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006440 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006441 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006442 if (dict_add(d, item) == FAIL)
6443 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006444 }
6445 }
6446
6447 if (**arg == '}')
6448 break;
6449 if (**arg != ',')
6450 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006451 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006452 goto failret;
6453 }
6454 *arg = skipwhite(*arg + 1);
6455 }
6456
6457 if (**arg != '}')
6458 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006459 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006460failret:
6461 if (evaluate)
6462 dict_free(d);
6463 return FAIL;
6464 }
6465
6466 *arg = skipwhite(*arg + 1);
6467 if (evaluate)
6468 {
6469 rettv->v_type = VAR_DICT;
6470 rettv->vval.v_dict = d;
6471 ++d->dv_refcount;
6472 }
6473
6474 return OK;
6475}
6476
Bram Moolenaar8c711452005-01-14 21:53:12 +00006477/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006478 * Return a string with the string representation of a variable.
6479 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006480 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006481 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006482 * May return NULL;
6483 */
6484 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006485echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006486 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006487 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006488 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006489{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006490 static int recurse = 0;
6491 char_u *r = NULL;
6492
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006494 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006495 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006496 *tofree = NULL;
6497 return NULL;
6498 }
6499 ++recurse;
6500
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006501 switch (tv->v_type)
6502 {
6503 case VAR_FUNC:
6504 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006505 r = tv->vval.v_string;
6506 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006507 case VAR_LIST:
6508 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006509 r = *tofree;
6510 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006511 case VAR_DICT:
6512 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006513 r = *tofree;
6514 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006515 case VAR_STRING:
6516 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006517 *tofree = NULL;
6518 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006520 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006521 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006522 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006523 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006524
6525 --recurse;
6526 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006527}
6528
6529/*
6530 * Return a string with the string representation of a variable.
6531 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6532 * "numbuf" is used for a number.
6533 * Puts quotes around strings, so that they can be parsed back by eval().
6534 * May return NULL;
6535 */
6536 static char_u *
6537tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006538 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006539 char_u **tofree;
6540 char_u *numbuf;
6541{
6542 switch (tv->v_type)
6543 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006544 case VAR_FUNC:
6545 *tofree = string_quote(tv->vval.v_string, TRUE);
6546 return *tofree;
6547 case VAR_STRING:
6548 *tofree = string_quote(tv->vval.v_string, FALSE);
6549 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006550 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006551 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006552 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006553 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006554 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006555 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006556 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006557 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558}
6559
6560/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 * Return string "str" in ' quotes, doubling ' characters.
6562 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006563 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006564 */
6565 static char_u *
6566string_quote(str, function)
6567 char_u *str;
6568 int function;
6569{
Bram Moolenaar33570922005-01-25 22:26:29 +00006570 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006571 char_u *p, *r, *s;
6572
Bram Moolenaar33570922005-01-25 22:26:29 +00006573 len = (function ? 13 : 3);
6574 if (str != NULL)
6575 {
6576 len += STRLEN(str);
6577 for (p = str; *p != NUL; mb_ptr_adv(p))
6578 if (*p == '\'')
6579 ++len;
6580 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006581 s = r = alloc(len);
6582 if (r != NULL)
6583 {
6584 if (function)
6585 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006586 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006587 r += 10;
6588 }
6589 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006590 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 if (str != NULL)
6592 for (p = str; *p != NUL; )
6593 {
6594 if (*p == '\'')
6595 *r++ = '\'';
6596 MB_COPY_CHAR(p, r);
6597 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006598 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006599 if (function)
6600 *r++ = ')';
6601 *r++ = NUL;
6602 }
6603 return s;
6604}
6605
6606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 * Get the value of an environment variable.
6608 * "arg" is pointing to the '$'. It is advanced to after the name.
6609 * If the environment variable was not set, silently assume it is empty.
6610 * Always return OK.
6611 */
6612 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006613get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 int evaluate;
6617{
6618 char_u *string = NULL;
6619 int len;
6620 int cc;
6621 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006622 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623
6624 ++*arg;
6625 name = *arg;
6626 len = get_env_len(arg);
6627 if (evaluate)
6628 {
6629 if (len != 0)
6630 {
6631 cc = name[len];
6632 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006633 /* first try vim_getenv(), fast for normal environment vars */
6634 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006636 {
6637 if (!mustfree)
6638 string = vim_strsave(string);
6639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 else
6641 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006642 if (mustfree)
6643 vim_free(string);
6644
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 /* next try expanding things like $VIM and ${HOME} */
6646 string = expand_env_save(name - 1);
6647 if (string != NULL && *string == '$')
6648 {
6649 vim_free(string);
6650 string = NULL;
6651 }
6652 }
6653 name[len] = cc;
6654 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006655 rettv->v_type = VAR_STRING;
6656 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 }
6658
6659 return OK;
6660}
6661
6662/*
6663 * Array with names and number of arguments of all internal functions
6664 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6665 */
6666static struct fst
6667{
6668 char *f_name; /* function name */
6669 char f_min_argc; /* minimal number of arguments */
6670 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006671 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673} functions[] =
6674{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006675 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 {"append", 2, 2, f_append},
6677 {"argc", 0, 0, f_argc},
6678 {"argidx", 0, 0, f_argidx},
6679 {"argv", 1, 1, f_argv},
6680 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006681 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 {"bufexists", 1, 1, f_bufexists},
6683 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6684 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6685 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6686 {"buflisted", 1, 1, f_buflisted},
6687 {"bufloaded", 1, 1, f_bufloaded},
6688 {"bufname", 1, 1, f_bufname},
6689 {"bufnr", 1, 1, f_bufnr},
6690 {"bufwinnr", 1, 1, f_bufwinnr},
6691 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006692 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006693 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 {"char2nr", 1, 1, f_char2nr},
6695 {"cindent", 1, 1, f_cindent},
6696 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006697#if defined(FEAT_INS_EXPAND)
6698 {"complete_add", 1, 1, f_complete_add},
6699 {"complete_check", 0, 0, f_complete_check},
6700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006702 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {"cscope_connection",0,3, f_cscope_connection},
6705 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006706 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 {"delete", 1, 1, f_delete},
6708 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006709 {"diff_filler", 1, 1, f_diff_filler},
6710 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006711 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006713 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 {"eventhandler", 0, 0, f_eventhandler},
6715 {"executable", 1, 1, f_executable},
6716 {"exists", 1, 1, f_exists},
6717 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006718 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6720 {"filereadable", 1, 1, f_filereadable},
6721 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006722 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006723 {"finddir", 1, 3, f_finddir},
6724 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 {"fnamemodify", 2, 2, f_fnamemodify},
6726 {"foldclosed", 1, 1, f_foldclosed},
6727 {"foldclosedend", 1, 1, f_foldclosedend},
6728 {"foldlevel", 1, 1, f_foldlevel},
6729 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006730 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006732 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006733 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006734 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006735 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 {"getbufvar", 2, 2, f_getbufvar},
6737 {"getchar", 0, 1, f_getchar},
6738 {"getcharmod", 0, 0, f_getcharmod},
6739 {"getcmdline", 0, 0, f_getcmdline},
6740 {"getcmdpos", 0, 0, f_getcmdpos},
6741 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006742 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006743 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 {"getfsize", 1, 1, f_getfsize},
6745 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006746 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006747 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006748 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006749 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {"getregtype", 0, 1, f_getregtype},
6751 {"getwinposx", 0, 0, f_getwinposx},
6752 {"getwinposy", 0, 0, f_getwinposy},
6753 {"getwinvar", 2, 2, f_getwinvar},
6754 {"glob", 1, 1, f_glob},
6755 {"globpath", 2, 2, f_globpath},
6756 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006757 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 {"hasmapto", 1, 2, f_hasmapto},
6759 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6760 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6761 {"histadd", 2, 2, f_histadd},
6762 {"histdel", 1, 2, f_histdel},
6763 {"histget", 1, 2, f_histget},
6764 {"histnr", 1, 1, f_histnr},
6765 {"hlID", 1, 1, f_hlID},
6766 {"hlexists", 1, 1, f_hlexists},
6767 {"hostname", 0, 0, f_hostname},
6768 {"iconv", 3, 3, f_iconv},
6769 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006770 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 {"input", 1, 2, f_input},
6772 {"inputdialog", 1, 3, f_inputdialog},
6773 {"inputrestore", 0, 0, f_inputrestore},
6774 {"inputsave", 0, 0, f_inputsave},
6775 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006776 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006778 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006779 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006780 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006781 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006783 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 {"libcall", 3, 3, f_libcall},
6785 {"libcallnr", 3, 3, f_libcallnr},
6786 {"line", 1, 1, f_line},
6787 {"line2byte", 1, 1, f_line2byte},
6788 {"lispindent", 1, 1, f_lispindent},
6789 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {"maparg", 1, 2, f_maparg},
6792 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006793 {"match", 2, 4, f_match},
6794 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006795 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006796 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006797 {"max", 1, 1, f_max},
6798 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006799#ifdef vim_mkdir
6800 {"mkdir", 1, 3, f_mkdir},
6801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 {"mode", 0, 0, f_mode},
6803 {"nextnonblank", 1, 1, f_nextnonblank},
6804 {"nr2char", 1, 1, f_nr2char},
6805 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006806 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006807 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006808 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 {"remote_expr", 2, 3, f_remote_expr},
6810 {"remote_foreground", 1, 1, f_remote_foreground},
6811 {"remote_peek", 1, 2, f_remote_peek},
6812 {"remote_read", 1, 1, f_remote_read},
6813 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006814 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006816 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006818 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 {"search", 1, 2, f_search},
6820 {"searchpair", 3, 5, f_searchpair},
6821 {"server2client", 2, 2, f_server2client},
6822 {"serverlist", 0, 0, f_serverlist},
6823 {"setbufvar", 3, 3, f_setbufvar},
6824 {"setcmdpos", 1, 1, f_setcmdpos},
6825 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006826 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 {"setreg", 2, 3, f_setreg},
6828 {"setwinvar", 3, 3, f_setwinvar},
6829 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006830 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006831 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006832 {"spellbadword", 0, 0, f_spellbadword},
6833 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006834 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835#ifdef HAVE_STRFTIME
6836 {"strftime", 1, 2, f_strftime},
6837#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006838 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006839 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 {"strlen", 1, 1, f_strlen},
6841 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006842 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 {"strtrans", 1, 1, f_strtrans},
6844 {"submatch", 1, 1, f_submatch},
6845 {"substitute", 4, 4, f_substitute},
6846 {"synID", 3, 3, f_synID},
6847 {"synIDattr", 2, 3, f_synIDattr},
6848 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006849 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006850 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 {"tempname", 0, 0, f_tempname},
6852 {"tolower", 1, 1, f_tolower},
6853 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006854 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006856 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 {"virtcol", 1, 1, f_virtcol},
6858 {"visualmode", 0, 1, f_visualmode},
6859 {"winbufnr", 1, 1, f_winbufnr},
6860 {"wincol", 0, 0, f_wincol},
6861 {"winheight", 1, 1, f_winheight},
6862 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006863 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 {"winrestcmd", 0, 0, f_winrestcmd},
6865 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006866 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867};
6868
6869#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6870
6871/*
6872 * Function given to ExpandGeneric() to obtain the list of internal
6873 * or user defined function names.
6874 */
6875 char_u *
6876get_function_name(xp, idx)
6877 expand_T *xp;
6878 int idx;
6879{
6880 static int intidx = -1;
6881 char_u *name;
6882
6883 if (idx == 0)
6884 intidx = -1;
6885 if (intidx < 0)
6886 {
6887 name = get_user_func_name(xp, idx);
6888 if (name != NULL)
6889 return name;
6890 }
6891 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6892 {
6893 STRCPY(IObuff, functions[intidx].f_name);
6894 STRCAT(IObuff, "(");
6895 if (functions[intidx].f_max_argc == 0)
6896 STRCAT(IObuff, ")");
6897 return IObuff;
6898 }
6899
6900 return NULL;
6901}
6902
6903/*
6904 * Function given to ExpandGeneric() to obtain the list of internal or
6905 * user defined variable or function names.
6906 */
6907/*ARGSUSED*/
6908 char_u *
6909get_expr_name(xp, idx)
6910 expand_T *xp;
6911 int idx;
6912{
6913 static int intidx = -1;
6914 char_u *name;
6915
6916 if (idx == 0)
6917 intidx = -1;
6918 if (intidx < 0)
6919 {
6920 name = get_function_name(xp, idx);
6921 if (name != NULL)
6922 return name;
6923 }
6924 return get_user_var_name(xp, ++intidx);
6925}
6926
6927#endif /* FEAT_CMDL_COMPL */
6928
6929/*
6930 * Find internal function in table above.
6931 * Return index, or -1 if not found
6932 */
6933 static int
6934find_internal_func(name)
6935 char_u *name; /* name of the function */
6936{
6937 int first = 0;
6938 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6939 int cmp;
6940 int x;
6941
6942 /*
6943 * Find the function name in the table. Binary search.
6944 */
6945 while (first <= last)
6946 {
6947 x = first + ((unsigned)(last - first) >> 1);
6948 cmp = STRCMP(name, functions[x].f_name);
6949 if (cmp < 0)
6950 last = x - 1;
6951 else if (cmp > 0)
6952 first = x + 1;
6953 else
6954 return x;
6955 }
6956 return -1;
6957}
6958
6959/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006960 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6961 * name it contains, otherwise return "name".
6962 */
6963 static char_u *
6964deref_func_name(name, lenp)
6965 char_u *name;
6966 int *lenp;
6967{
Bram Moolenaar33570922005-01-25 22:26:29 +00006968 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006969 int cc;
6970
6971 cc = name[*lenp];
6972 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006973 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006974 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006975 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006976 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006978 {
6979 *lenp = 0;
6980 return (char_u *)""; /* just in case */
6981 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006982 *lenp = STRLEN(v->di_tv.vval.v_string);
6983 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006984 }
6985
6986 return name;
6987}
6988
6989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 * Allocate a variable for the result of a function.
6991 * Return OK or FAIL.
6992 */
6993 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006994get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6995 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 char_u *name; /* name of the function */
6997 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 char_u **arg; /* argument, pointing to the '(' */
7000 linenr_T firstline; /* first line of range */
7001 linenr_T lastline; /* last line of range */
7002 int *doesrange; /* return: function handled range */
7003 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007004 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
7006 char_u *argp;
7007 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007008 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 int argcount = 0; /* number of arguments found */
7010
7011 /*
7012 * Get the arguments.
7013 */
7014 argp = *arg;
7015 while (argcount < MAX_FUNC_ARGS)
7016 {
7017 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7018 if (*argp == ')' || *argp == ',' || *argp == NUL)
7019 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7021 {
7022 ret = FAIL;
7023 break;
7024 }
7025 ++argcount;
7026 if (*argp != ',')
7027 break;
7028 }
7029 if (*argp == ')')
7030 ++argp;
7031 else
7032 ret = FAIL;
7033
7034 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007035 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007036 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007038 {
7039 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007040 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007041 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007042 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044
7045 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007046 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047
7048 *arg = skipwhite(argp);
7049 return ret;
7050}
7051
7052
7053/*
7054 * Call a function with its resolved parameters
7055 * Return OK or FAIL.
7056 */
7057 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007058call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007059 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 char_u *name; /* name of the function */
7061 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007062 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007064 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 linenr_T firstline; /* first line of range */
7066 linenr_T lastline; /* last line of range */
7067 int *doesrange; /* return: function handled range */
7068 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007069 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
7071 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072#define ERROR_UNKNOWN 0
7073#define ERROR_TOOMANY 1
7074#define ERROR_TOOFEW 2
7075#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007076#define ERROR_DICT 4
7077#define ERROR_NONE 5
7078#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 int error = ERROR_NONE;
7080 int i;
7081 int llen;
7082 ufunc_T *fp;
7083 int cc;
7084#define FLEN_FIXED 40
7085 char_u fname_buf[FLEN_FIXED + 1];
7086 char_u *fname;
7087
7088 /*
7089 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7090 * Change <SNR>123_name() to K_SNR 123_name().
7091 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7092 */
7093 cc = name[len];
7094 name[len] = NUL;
7095 llen = eval_fname_script(name);
7096 if (llen > 0)
7097 {
7098 fname_buf[0] = K_SPECIAL;
7099 fname_buf[1] = KS_EXTRA;
7100 fname_buf[2] = (int)KE_SNR;
7101 i = 3;
7102 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7103 {
7104 if (current_SID <= 0)
7105 error = ERROR_SCRIPT;
7106 else
7107 {
7108 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7109 i = (int)STRLEN(fname_buf);
7110 }
7111 }
7112 if (i + STRLEN(name + llen) < FLEN_FIXED)
7113 {
7114 STRCPY(fname_buf + i, name + llen);
7115 fname = fname_buf;
7116 }
7117 else
7118 {
7119 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7120 if (fname == NULL)
7121 error = ERROR_OTHER;
7122 else
7123 {
7124 mch_memmove(fname, fname_buf, (size_t)i);
7125 STRCPY(fname + i, name + llen);
7126 }
7127 }
7128 }
7129 else
7130 fname = name;
7131
7132 *doesrange = FALSE;
7133
7134
7135 /* execute the function if no errors detected and executing */
7136 if (evaluate && error == ERROR_NONE)
7137 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007138 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 error = ERROR_UNKNOWN;
7140
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007141 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 {
7143 /*
7144 * User defined function.
7145 */
7146 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007147
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007149 /* Trigger FuncUndefined event, may load the function. */
7150 if (fp == NULL
7151 && apply_autocmds(EVENT_FUNCUNDEFINED,
7152 fname, fname, TRUE, NULL)
7153 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007155 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 fp = find_func(fname);
7157 }
7158#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007159 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007160 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007161 {
7162 /* loaded a package, search for the function again */
7163 fp = find_func(fname);
7164 }
7165
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 if (fp != NULL)
7167 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007168 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007170 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007172 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007174 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007175 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 else
7177 {
7178 /*
7179 * Call the user function.
7180 * Save and restore search patterns, script variables and
7181 * redo buffer.
7182 */
7183 save_search_patterns();
7184 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007185 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007186 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007187 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007188 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7189 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7190 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007191 /* Function was unreferenced while being used, free it
7192 * now. */
7193 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 restoreRedobuff();
7195 restore_search_patterns();
7196 error = ERROR_NONE;
7197 }
7198 }
7199 }
7200 else
7201 {
7202 /*
7203 * Find the function name in the table, call its implementation.
7204 */
7205 i = find_internal_func(fname);
7206 if (i >= 0)
7207 {
7208 if (argcount < functions[i].f_min_argc)
7209 error = ERROR_TOOFEW;
7210 else if (argcount > functions[i].f_max_argc)
7211 error = ERROR_TOOMANY;
7212 else
7213 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007214 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007215 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 error = ERROR_NONE;
7217 }
7218 }
7219 }
7220 /*
7221 * The function call (or "FuncUndefined" autocommand sequence) might
7222 * have been aborted by an error, an interrupt, or an explicitly thrown
7223 * exception that has not been caught so far. This situation can be
7224 * tested for by calling aborting(). For an error in an internal
7225 * function or for the "E132" error in call_user_func(), however, the
7226 * throw point at which the "force_abort" flag (temporarily reset by
7227 * emsg()) is normally updated has not been reached yet. We need to
7228 * update that flag first to make aborting() reliable.
7229 */
7230 update_force_abort();
7231 }
7232 if (error == ERROR_NONE)
7233 ret = OK;
7234
7235 /*
7236 * Report an error unless the argument evaluation or function call has been
7237 * cancelled due to an aborting error, an interrupt, or an exception.
7238 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007239 if (!aborting())
7240 {
7241 switch (error)
7242 {
7243 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007244 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245 break;
7246 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007247 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248 break;
7249 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007250 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251 name);
7252 break;
7253 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007254 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255 name);
7256 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007257 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007258 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007259 name);
7260 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261 }
7262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263
7264 name[len] = cc;
7265 if (fname != name && fname != fname_buf)
7266 vim_free(fname);
7267
7268 return ret;
7269}
7270
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007271/*
7272 * Give an error message with a function name. Handle <SNR> things.
7273 */
7274 static void
7275emsg_funcname(msg, name)
7276 char *msg;
7277 char_u *name;
7278{
7279 char_u *p;
7280
7281 if (*name == K_SPECIAL)
7282 p = concat_str((char_u *)"<SNR>", name + 3);
7283 else
7284 p = name;
7285 EMSG2(_(msg), p);
7286 if (p != name)
7287 vim_free(p);
7288}
7289
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290/*********************************************
7291 * Implementation of the built-in functions
7292 */
7293
7294/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007295 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 */
7297 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007298f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 typval_T *argvars;
7300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301{
Bram Moolenaar33570922005-01-25 22:26:29 +00007302 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007304 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007305 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007307 if ((l = argvars[0].vval.v_list) != NULL
7308 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7309 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007310 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007311 }
7312 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007313 EMSG(_(e_listreq));
7314}
7315
7316/*
7317 * "append(lnum, string/list)" function
7318 */
7319 static void
7320f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 typval_T *argvars;
7322 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007323{
7324 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007325 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 list_T *l = NULL;
7327 listitem_T *li = NULL;
7328 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007329 long added = 0;
7330
Bram Moolenaar0d660222005-01-07 21:51:51 +00007331 lnum = get_tv_lnum(argvars);
7332 if (lnum >= 0
7333 && lnum <= curbuf->b_ml.ml_line_count
7334 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007335 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007336 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007337 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007338 l = argvars[1].vval.v_list;
7339 if (l == NULL)
7340 return;
7341 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007342 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007343 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007344 for (;;)
7345 {
7346 if (l == NULL)
7347 tv = &argvars[1]; /* append a string */
7348 else if (li == NULL)
7349 break; /* end of list */
7350 else
7351 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007352 line = get_tv_string_chk(tv);
7353 if (line == NULL) /* type error */
7354 {
7355 rettv->vval.v_number = 1; /* Failed */
7356 break;
7357 }
7358 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007359 ++added;
7360 if (l == NULL)
7361 break;
7362 li = li->li_next;
7363 }
7364
7365 appended_lines_mark(lnum, added);
7366 if (curwin->w_cursor.lnum > lnum)
7367 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007369 else
7370 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371}
7372
7373/*
7374 * "argc()" function
7375 */
7376/* ARGSUSED */
7377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007378f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007379 typval_T *argvars;
7380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007382 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383}
7384
7385/*
7386 * "argidx()" function
7387 */
7388/* ARGSUSED */
7389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007390f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 typval_T *argvars;
7392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007394 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395}
7396
7397/*
7398 * "argv(nr)" function
7399 */
7400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007401f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007402 typval_T *argvars;
7403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404{
7405 int idx;
7406
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007407 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007409 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007411 rettv->vval.v_string = NULL;
7412 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413}
7414
7415/*
7416 * "browse(save, title, initdir, default)" function
7417 */
7418/* ARGSUSED */
7419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007420f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 typval_T *argvars;
7422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423{
7424#ifdef FEAT_BROWSE
7425 int save;
7426 char_u *title;
7427 char_u *initdir;
7428 char_u *defname;
7429 char_u buf[NUMBUFLEN];
7430 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007431 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007433 save = get_tv_number_chk(&argvars[0], &error);
7434 title = get_tv_string_chk(&argvars[1]);
7435 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7436 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007438 if (error || title == NULL || initdir == NULL || defname == NULL)
7439 rettv->vval.v_string = NULL;
7440 else
7441 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007442 do_browse(save ? BROWSE_SAVE : 0,
7443 title, defname, NULL, initdir, NULL, curbuf);
7444#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007445 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007446#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007447 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007448}
7449
7450/*
7451 * "browsedir(title, initdir)" function
7452 */
7453/* ARGSUSED */
7454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007455f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 typval_T *argvars;
7457 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007458{
7459#ifdef FEAT_BROWSE
7460 char_u *title;
7461 char_u *initdir;
7462 char_u buf[NUMBUFLEN];
7463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007464 title = get_tv_string_chk(&argvars[0]);
7465 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007467 if (title == NULL || initdir == NULL)
7468 rettv->vval.v_string = NULL;
7469 else
7470 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007471 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007473 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007475 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476}
7477
Bram Moolenaar33570922005-01-25 22:26:29 +00007478static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007479
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480/*
7481 * Find a buffer by number or exact name.
7482 */
7483 static buf_T *
7484find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007485 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486{
7487 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007489 if (avar->v_type == VAR_NUMBER)
7490 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007491 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007493 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007494 if (buf == NULL)
7495 {
7496 /* No full path name match, try a match with a URL or a "nofile"
7497 * buffer, these don't use the full path. */
7498 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7499 if (buf->b_fname != NULL
7500 && (path_with_url(buf->b_fname)
7501#ifdef FEAT_QUICKFIX
7502 || bt_nofile(buf)
7503#endif
7504 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007505 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007506 break;
7507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 }
7509 return buf;
7510}
7511
7512/*
7513 * "bufexists(expr)" function
7514 */
7515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007516f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007517 typval_T *argvars;
7518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007520 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521}
7522
7523/*
7524 * "buflisted(expr)" function
7525 */
7526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007527f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007528 typval_T *argvars;
7529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530{
7531 buf_T *buf;
7532
7533 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007534 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535}
7536
7537/*
7538 * "bufloaded(expr)" function
7539 */
7540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007541f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 typval_T *argvars;
7543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544{
7545 buf_T *buf;
7546
7547 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007548 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549}
7550
Bram Moolenaar33570922005-01-25 22:26:29 +00007551static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007552
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553/*
7554 * Get buffer by number or pattern.
7555 */
7556 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007557get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007558 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007560 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 int save_magic;
7562 char_u *save_cpo;
7563 buf_T *buf;
7564
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007565 if (tv->v_type == VAR_NUMBER)
7566 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007567 if (tv->v_type != VAR_STRING)
7568 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 if (name == NULL || *name == NUL)
7570 return curbuf;
7571 if (name[0] == '$' && name[1] == NUL)
7572 return lastbuf;
7573
7574 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7575 save_magic = p_magic;
7576 p_magic = TRUE;
7577 save_cpo = p_cpo;
7578 p_cpo = (char_u *)"";
7579
7580 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7581 TRUE, FALSE));
7582
7583 p_magic = save_magic;
7584 p_cpo = save_cpo;
7585
7586 /* If not found, try expanding the name, like done for bufexists(). */
7587 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007588 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589
7590 return buf;
7591}
7592
7593/*
7594 * "bufname(expr)" function
7595 */
7596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007597f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 typval_T *argvars;
7599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600{
7601 buf_T *buf;
7602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007603 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007605 buf = get_buf_tv(&argvars[0]);
7606 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007608 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007610 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 --emsg_off;
7612}
7613
7614/*
7615 * "bufnr(expr)" function
7616 */
7617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007618f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 typval_T *argvars;
7620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621{
7622 buf_T *buf;
7623
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007624 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007626 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007628 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007630 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 --emsg_off;
7632}
7633
7634/*
7635 * "bufwinnr(nr)" function
7636 */
7637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007638f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007639 typval_T *argvars;
7640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641{
7642#ifdef FEAT_WINDOWS
7643 win_T *wp;
7644 int winnr = 0;
7645#endif
7646 buf_T *buf;
7647
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007648 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007650 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651#ifdef FEAT_WINDOWS
7652 for (wp = firstwin; wp; wp = wp->w_next)
7653 {
7654 ++winnr;
7655 if (wp->w_buffer == buf)
7656 break;
7657 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007658 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007660 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661#endif
7662 --emsg_off;
7663}
7664
7665/*
7666 * "byte2line(byte)" function
7667 */
7668/*ARGSUSED*/
7669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007670f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007671 typval_T *argvars;
7672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673{
7674#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007675 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676#else
7677 long boff = 0;
7678
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007679 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007681 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007683 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 (linenr_T)0, &boff);
7685#endif
7686}
7687
7688/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007689 * "byteidx()" function
7690 */
7691/*ARGSUSED*/
7692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007693f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 typval_T *argvars;
7695 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007696{
7697#ifdef FEAT_MBYTE
7698 char_u *t;
7699#endif
7700 char_u *str;
7701 long idx;
7702
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007703 str = get_tv_string_chk(&argvars[0]);
7704 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007705 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007706 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007707 return;
7708
7709#ifdef FEAT_MBYTE
7710 t = str;
7711 for ( ; idx > 0; idx--)
7712 {
7713 if (*t == NUL) /* EOL reached */
7714 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007715 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007716 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007717 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007718#else
7719 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007720 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007721#endif
7722}
7723
7724/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007725 * "call(func, arglist)" function
7726 */
7727 static void
7728f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007729 typval_T *argvars;
7730 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007731{
7732 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007734 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007735 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007736 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007738
7739 rettv->vval.v_number = 0;
7740 if (argvars[1].v_type != VAR_LIST)
7741 {
7742 EMSG(_(e_listreq));
7743 return;
7744 }
7745 if (argvars[1].vval.v_list == NULL)
7746 return;
7747
7748 if (argvars[0].v_type == VAR_FUNC)
7749 func = argvars[0].vval.v_string;
7750 else
7751 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007752 if (*func == NUL)
7753 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007754
Bram Moolenaare9a41262005-01-15 22:18:47 +00007755 if (argvars[2].v_type != VAR_UNKNOWN)
7756 {
7757 if (argvars[2].v_type != VAR_DICT)
7758 {
7759 EMSG(_(e_dictreq));
7760 return;
7761 }
7762 selfdict = argvars[2].vval.v_dict;
7763 }
7764
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007765 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7766 item = item->li_next)
7767 {
7768 if (argc == MAX_FUNC_ARGS)
7769 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007770 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007771 break;
7772 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007773 /* Make a copy of each argument. This is needed to be able to set
7774 * v_lock to VAR_FIXED in the copy without changing the original list.
7775 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007776 copy_tv(&item->li_tv, &argv[argc++]);
7777 }
7778
7779 if (item == NULL)
7780 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007781 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7782 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007783
7784 /* Free the arguments. */
7785 while (argc > 0)
7786 clear_tv(&argv[--argc]);
7787}
7788
7789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 * "char2nr(string)" function
7791 */
7792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007793f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007794 typval_T *argvars;
7795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796{
7797#ifdef FEAT_MBYTE
7798 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007799 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 else
7801#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007802 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803}
7804
7805/*
7806 * "cindent(lnum)" function
7807 */
7808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007809f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007810 typval_T *argvars;
7811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813#ifdef FEAT_CINDENT
7814 pos_T pos;
7815 linenr_T lnum;
7816
7817 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007818 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7820 {
7821 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007822 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 curwin->w_cursor = pos;
7824 }
7825 else
7826#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007827 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828}
7829
7830/*
7831 * "col(string)" function
7832 */
7833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007834f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007835 typval_T *argvars;
7836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837{
7838 colnr_T col = 0;
7839 pos_T *fp;
7840
7841 fp = var2fpos(&argvars[0], FALSE);
7842 if (fp != NULL)
7843 {
7844 if (fp->col == MAXCOL)
7845 {
7846 /* '> can be MAXCOL, get the length of the line then */
7847 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7848 col = STRLEN(ml_get(fp->lnum)) + 1;
7849 else
7850 col = MAXCOL;
7851 }
7852 else
7853 {
7854 col = fp->col + 1;
7855#ifdef FEAT_VIRTUALEDIT
7856 /* col(".") when the cursor is on the NUL at the end of the line
7857 * because of "coladd" can be seen as an extra column. */
7858 if (virtual_active() && fp == &curwin->w_cursor)
7859 {
7860 char_u *p = ml_get_cursor();
7861
7862 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7863 curwin->w_virtcol - curwin->w_cursor.coladd))
7864 {
7865# ifdef FEAT_MBYTE
7866 int l;
7867
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007868 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 col += l;
7870# else
7871 if (*p != NUL && p[1] == NUL)
7872 ++col;
7873# endif
7874 }
7875 }
7876#endif
7877 }
7878 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007879 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880}
7881
Bram Moolenaar572cb562005-08-05 21:35:02 +00007882#if defined(FEAT_INS_EXPAND)
7883/*
7884 * "complete_add()" function
7885 */
7886/*ARGSUSED*/
7887 static void
7888f_complete_add(argvars, rettv)
7889 typval_T *argvars;
7890 typval_T *rettv;
7891{
7892 char_u *s;
7893
7894 s = get_tv_string_chk(&argvars[0]);
7895 if (s != NULL)
7896 rettv->vval.v_number = ins_compl_add(s, -1, NULL, FORWARD, 0);
7897}
7898
7899/*
7900 * "complete_check()" function
7901 */
7902/*ARGSUSED*/
7903 static void
7904f_complete_check(argvars, rettv)
7905 typval_T *argvars;
7906 typval_T *rettv;
7907{
7908 int saved = RedrawingDisabled;
7909
7910 RedrawingDisabled = 0;
7911 ins_compl_check_keys(0);
7912 rettv->vval.v_number = compl_interrupted;
7913 RedrawingDisabled = saved;
7914}
7915#endif
7916
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917/*
7918 * "confirm(message, buttons[, default [, type]])" function
7919 */
7920/*ARGSUSED*/
7921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007922f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007923 typval_T *argvars;
7924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925{
7926#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7927 char_u *message;
7928 char_u *buttons = NULL;
7929 char_u buf[NUMBUFLEN];
7930 char_u buf2[NUMBUFLEN];
7931 int def = 1;
7932 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007933 char_u *typestr;
7934 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007936 message = get_tv_string_chk(&argvars[0]);
7937 if (message == NULL)
7938 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007939 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007941 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7942 if (buttons == NULL)
7943 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007944 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007946 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007947 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007949 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7950 if (typestr == NULL)
7951 error = TRUE;
7952 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007954 switch (TOUPPER_ASC(*typestr))
7955 {
7956 case 'E': type = VIM_ERROR; break;
7957 case 'Q': type = VIM_QUESTION; break;
7958 case 'I': type = VIM_INFO; break;
7959 case 'W': type = VIM_WARNING; break;
7960 case 'G': type = VIM_GENERIC; break;
7961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 }
7963 }
7964 }
7965 }
7966
7967 if (buttons == NULL || *buttons == NUL)
7968 buttons = (char_u *)_("&Ok");
7969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007970 if (error)
7971 rettv->vval.v_number = 0;
7972 else
7973 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 def, NULL);
7975#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007976 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977#endif
7978}
7979
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007980/*
7981 * "copy()" function
7982 */
7983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007985 typval_T *argvars;
7986 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007987{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007988 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007989}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990
7991/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007992 * "count()" function
7993 */
7994 static void
7995f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007996 typval_T *argvars;
7997 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007998{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007999 long n = 0;
8000 int ic = FALSE;
8001
Bram Moolenaare9a41262005-01-15 22:18:47 +00008002 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008003 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008004 listitem_T *li;
8005 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008006 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008007
Bram Moolenaare9a41262005-01-15 22:18:47 +00008008 if ((l = argvars[0].vval.v_list) != NULL)
8009 {
8010 li = l->lv_first;
8011 if (argvars[2].v_type != VAR_UNKNOWN)
8012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008013 int error = FALSE;
8014
8015 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008016 if (argvars[3].v_type != VAR_UNKNOWN)
8017 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008018 idx = get_tv_number_chk(&argvars[3], &error);
8019 if (!error)
8020 {
8021 li = list_find(l, idx);
8022 if (li == NULL)
8023 EMSGN(_(e_listidx), idx);
8024 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008025 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008026 if (error)
8027 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008028 }
8029
8030 for ( ; li != NULL; li = li->li_next)
8031 if (tv_equal(&li->li_tv, &argvars[1], ic))
8032 ++n;
8033 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008034 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008035 else if (argvars[0].v_type == VAR_DICT)
8036 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008037 int todo;
8038 dict_T *d;
8039 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008040
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008041 if ((d = argvars[0].vval.v_dict) != NULL)
8042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008043 int error = FALSE;
8044
Bram Moolenaare9a41262005-01-15 22:18:47 +00008045 if (argvars[2].v_type != VAR_UNKNOWN)
8046 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008047 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008048 if (argvars[3].v_type != VAR_UNKNOWN)
8049 EMSG(_(e_invarg));
8050 }
8051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008052 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008054 {
8055 if (!HASHITEM_EMPTY(hi))
8056 {
8057 --todo;
8058 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8059 ++n;
8060 }
8061 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008062 }
8063 }
8064 else
8065 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008066 rettv->vval.v_number = n;
8067}
8068
8069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8071 *
8072 * Checks the existence of a cscope connection.
8073 */
8074/*ARGSUSED*/
8075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008076f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008077 typval_T *argvars;
8078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079{
8080#ifdef FEAT_CSCOPE
8081 int num = 0;
8082 char_u *dbpath = NULL;
8083 char_u *prepend = NULL;
8084 char_u buf[NUMBUFLEN];
8085
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008086 if (argvars[0].v_type != VAR_UNKNOWN
8087 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008089 num = (int)get_tv_number(&argvars[0]);
8090 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008091 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008092 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 }
8094
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008095 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008097 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098#endif
8099}
8100
8101/*
8102 * "cursor(lnum, col)" function
8103 *
8104 * Moves the cursor to the specified line and column
8105 */
8106/*ARGSUSED*/
8107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008108f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008109 typval_T *argvars;
8110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111{
8112 long line, col;
8113
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008114 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008115 col = get_tv_number_chk(&argvars[1], NULL);
8116 if (line < 0 || col < 0)
8117 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 if (line > 0)
8119 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 if (col > 0)
8121 curwin->w_cursor.col = col - 1;
8122#ifdef FEAT_VIRTUALEDIT
8123 curwin->w_cursor.coladd = 0;
8124#endif
8125
8126 /* Make sure the cursor is in a valid position. */
8127 check_cursor();
8128#ifdef FEAT_MBYTE
8129 /* Correct cursor for multi-byte character. */
8130 if (has_mbyte)
8131 mb_adjust_cursor();
8132#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008133
8134 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135}
8136
8137/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008138 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 */
8140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008141f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008142 typval_T *argvars;
8143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008145 int noref = 0;
8146
8147 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008148 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008149 if (noref < 0 || noref > 1)
8150 EMSG(_(e_invarg));
8151 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008152 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153}
8154
8155/*
8156 * "delete()" function
8157 */
8158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008159f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008160 typval_T *argvars;
8161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162{
8163 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008164 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008166 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167}
8168
8169/*
8170 * "did_filetype()" function
8171 */
8172/*ARGSUSED*/
8173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008174f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008175 typval_T *argvars;
8176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177{
8178#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008179 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008181 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182#endif
8183}
8184
8185/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008186 * "diff_filler()" function
8187 */
8188/*ARGSUSED*/
8189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008190f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008191 typval_T *argvars;
8192 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008193{
8194#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008195 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008196#endif
8197}
8198
8199/*
8200 * "diff_hlID()" function
8201 */
8202/*ARGSUSED*/
8203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008204f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008205 typval_T *argvars;
8206 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008207{
8208#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008209 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008210 static linenr_T prev_lnum = 0;
8211 static int changedtick = 0;
8212 static int fnum = 0;
8213 static int change_start = 0;
8214 static int change_end = 0;
8215 static enum hlf_value hlID = 0;
8216 int filler_lines;
8217 int col;
8218
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008219 if (lnum < 0) /* ignore type error in {lnum} arg */
8220 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008221 if (lnum != prev_lnum
8222 || changedtick != curbuf->b_changedtick
8223 || fnum != curbuf->b_fnum)
8224 {
8225 /* New line, buffer, change: need to get the values. */
8226 filler_lines = diff_check(curwin, lnum);
8227 if (filler_lines < 0)
8228 {
8229 if (filler_lines == -1)
8230 {
8231 change_start = MAXCOL;
8232 change_end = -1;
8233 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8234 hlID = HLF_ADD; /* added line */
8235 else
8236 hlID = HLF_CHD; /* changed line */
8237 }
8238 else
8239 hlID = HLF_ADD; /* added line */
8240 }
8241 else
8242 hlID = (enum hlf_value)0;
8243 prev_lnum = lnum;
8244 changedtick = curbuf->b_changedtick;
8245 fnum = curbuf->b_fnum;
8246 }
8247
8248 if (hlID == HLF_CHD || hlID == HLF_TXD)
8249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008250 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008251 if (col >= change_start && col <= change_end)
8252 hlID = HLF_TXD; /* changed text */
8253 else
8254 hlID = HLF_CHD; /* changed line */
8255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008256 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008257#endif
8258}
8259
8260/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008261 * "empty({expr})" function
8262 */
8263 static void
8264f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008265 typval_T *argvars;
8266 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008267{
8268 int n;
8269
8270 switch (argvars[0].v_type)
8271 {
8272 case VAR_STRING:
8273 case VAR_FUNC:
8274 n = argvars[0].vval.v_string == NULL
8275 || *argvars[0].vval.v_string == NUL;
8276 break;
8277 case VAR_NUMBER:
8278 n = argvars[0].vval.v_number == 0;
8279 break;
8280 case VAR_LIST:
8281 n = argvars[0].vval.v_list == NULL
8282 || argvars[0].vval.v_list->lv_first == NULL;
8283 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008284 case VAR_DICT:
8285 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008286 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008287 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008288 default:
8289 EMSG2(_(e_intern2), "f_empty()");
8290 n = 0;
8291 }
8292
8293 rettv->vval.v_number = n;
8294}
8295
8296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 * "escape({string}, {chars})" function
8298 */
8299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008300f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008301 typval_T *argvars;
8302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303{
8304 char_u buf[NUMBUFLEN];
8305
Bram Moolenaar758711c2005-02-02 23:11:38 +00008306 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8307 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008308 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309}
8310
8311/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008312 * "eval()" function
8313 */
8314/*ARGSUSED*/
8315 static void
8316f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008317 typval_T *argvars;
8318 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008319{
8320 char_u *s;
8321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008322 s = get_tv_string_chk(&argvars[0]);
8323 if (s != NULL)
8324 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008326 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8327 {
8328 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008329 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008330 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008331 else if (*s != NUL)
8332 EMSG(_(e_trailing));
8333}
8334
8335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 * "eventhandler()" function
8337 */
8338/*ARGSUSED*/
8339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008340f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008341 typval_T *argvars;
8342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008344 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345}
8346
8347/*
8348 * "executable()" function
8349 */
8350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008351f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008352 typval_T *argvars;
8353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008355 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356}
8357
8358/*
8359 * "exists()" function
8360 */
8361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008362f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008363 typval_T *argvars;
8364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365{
8366 char_u *p;
8367 char_u *name;
8368 int n = FALSE;
8369 int len = 0;
8370
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008371 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 if (*p == '$') /* environment variable */
8373 {
8374 /* first try "normal" environment variables (fast) */
8375 if (mch_getenv(p + 1) != NULL)
8376 n = TRUE;
8377 else
8378 {
8379 /* try expanding things like $VIM and ${HOME} */
8380 p = expand_env_save(p);
8381 if (p != NULL && *p != '$')
8382 n = TRUE;
8383 vim_free(p);
8384 }
8385 }
8386 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008387 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 else if (*p == '*') /* internal or user defined function */
8389 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008390 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 }
8392 else if (*p == ':')
8393 {
8394 n = cmd_exists(p + 1);
8395 }
8396 else if (*p == '#')
8397 {
8398#ifdef FEAT_AUTOCMD
8399 name = p + 1;
8400 p = vim_strchr(name, '#');
8401 if (p != NULL)
8402 n = au_exists(name, p, p + 1);
8403 else
8404 n = au_exists(name, name + STRLEN(name), NULL);
8405#endif
8406 }
8407 else /* internal variable */
8408 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008409 char_u *tofree;
8410 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008412 /* get_name_len() takes care of expanding curly braces */
8413 name = p;
8414 len = get_name_len(&p, &tofree, TRUE, FALSE);
8415 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008417 if (tofree != NULL)
8418 name = tofree;
8419 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8420 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008422 /* handle d.key, l[idx], f(expr) */
8423 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8424 if (n)
8425 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 }
8427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008429 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 }
8431
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008432 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433}
8434
8435/*
8436 * "expand()" function
8437 */
8438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008439f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008440 typval_T *argvars;
8441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442{
8443 char_u *s;
8444 int len;
8445 char_u *errormsg;
8446 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8447 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008448 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008450 rettv->v_type = VAR_STRING;
8451 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 if (*s == '%' || *s == '#' || *s == '<')
8453 {
8454 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008455 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 --emsg_off;
8457 }
8458 else
8459 {
8460 /* When the optional second argument is non-zero, don't remove matches
8461 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008462 if (argvars[1].v_type != VAR_UNKNOWN
8463 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008465 if (!error)
8466 {
8467 ExpandInit(&xpc);
8468 xpc.xp_context = EXPAND_FILES;
8469 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8470 ExpandCleanup(&xpc);
8471 }
8472 else
8473 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 }
8475}
8476
8477/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008478 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008479 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008480 */
8481 static void
8482f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008483 typval_T *argvars;
8484 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008485{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008486 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008487 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008488 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008489 list_T *l1, *l2;
8490 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008491 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008492 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008493
Bram Moolenaare9a41262005-01-15 22:18:47 +00008494 l1 = argvars[0].vval.v_list;
8495 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008496 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8497 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008498 {
8499 if (argvars[2].v_type != VAR_UNKNOWN)
8500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008501 before = get_tv_number_chk(&argvars[2], &error);
8502 if (error)
8503 return; /* type error; errmsg already given */
8504
Bram Moolenaar758711c2005-02-02 23:11:38 +00008505 if (before == l1->lv_len)
8506 item = NULL;
8507 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008508 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008509 item = list_find(l1, before);
8510 if (item == NULL)
8511 {
8512 EMSGN(_(e_listidx), before);
8513 return;
8514 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008515 }
8516 }
8517 else
8518 item = NULL;
8519 list_extend(l1, l2, item);
8520
Bram Moolenaare9a41262005-01-15 22:18:47 +00008521 copy_tv(&argvars[0], rettv);
8522 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008523 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008524 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8525 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008526 dict_T *d1, *d2;
8527 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008528 char_u *action;
8529 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008530 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008531 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008532
8533 d1 = argvars[0].vval.v_dict;
8534 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008535 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8536 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008537 {
8538 /* Check the third argument. */
8539 if (argvars[2].v_type != VAR_UNKNOWN)
8540 {
8541 static char *(av[]) = {"keep", "force", "error"};
8542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008543 action = get_tv_string_chk(&argvars[2]);
8544 if (action == NULL)
8545 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008546 for (i = 0; i < 3; ++i)
8547 if (STRCMP(action, av[i]) == 0)
8548 break;
8549 if (i == 3)
8550 {
8551 EMSGN(_(e_invarg2), action);
8552 return;
8553 }
8554 }
8555 else
8556 action = (char_u *)"force";
8557
8558 /* Go over all entries in the second dict and add them to the
8559 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008560 todo = d2->dv_hashtab.ht_used;
8561 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008562 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008563 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008564 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008565 --todo;
8566 di1 = dict_find(d1, hi2->hi_key, -1);
8567 if (di1 == NULL)
8568 {
8569 di1 = dictitem_copy(HI2DI(hi2));
8570 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8571 dictitem_free(di1);
8572 }
8573 else if (*action == 'e')
8574 {
8575 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8576 break;
8577 }
8578 else if (*action == 'f')
8579 {
8580 clear_tv(&di1->di_tv);
8581 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8582 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008583 }
8584 }
8585
Bram Moolenaare9a41262005-01-15 22:18:47 +00008586 copy_tv(&argvars[0], rettv);
8587 }
8588 }
8589 else
8590 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008591}
8592
8593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 * "filereadable()" function
8595 */
8596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008597f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008598 typval_T *argvars;
8599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600{
8601 FILE *fd;
8602 char_u *p;
8603 int n;
8604
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008605 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8607 {
8608 n = TRUE;
8609 fclose(fd);
8610 }
8611 else
8612 n = FALSE;
8613
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008614 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615}
8616
8617/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008618 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 * rights to write into.
8620 */
8621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008622f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008623 typval_T *argvars;
8624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008626 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627}
8628
Bram Moolenaar33570922005-01-25 22:26:29 +00008629static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008630
8631 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008632findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008633 typval_T *argvars;
8634 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008635 int dir;
8636{
8637#ifdef FEAT_SEARCHPATH
8638 char_u *fname;
8639 char_u *fresult = NULL;
8640 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8641 char_u *p;
8642 char_u pathbuf[NUMBUFLEN];
8643 int count = 1;
8644 int first = TRUE;
8645
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008646 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008647
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008648 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008650 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8651 if (p == NULL)
8652 count = -1; /* error */
8653 else
8654 {
8655 if (*p != NUL)
8656 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008658 if (argvars[2].v_type != VAR_UNKNOWN)
8659 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8660 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008661 }
8662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008663 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008665 do
8666 {
8667 vim_free(fresult);
8668 fresult = find_file_in_path_option(first ? fname : NULL,
8669 first ? (int)STRLEN(fname) : 0,
8670 0, first, path, dir, NULL);
8671 first = FALSE;
8672 } while (--count > 0 && fresult != NULL);
8673 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008674
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008676#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008677 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008678#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008679 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008680}
8681
Bram Moolenaar33570922005-01-25 22:26:29 +00008682static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8683static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008684
8685/*
8686 * Implementation of map() and filter().
8687 */
8688 static void
8689filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008690 typval_T *argvars;
8691 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008692 int map;
8693{
8694 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008695 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008696 listitem_T *li, *nli;
8697 list_T *l = NULL;
8698 dictitem_T *di;
8699 hashtab_T *ht;
8700 hashitem_T *hi;
8701 dict_T *d = NULL;
8702 typval_T save_val;
8703 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008704 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008705 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008706 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8707
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008708
8709 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008710 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008711 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008712 if ((l = argvars[0].vval.v_list) == NULL
8713 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008714 return;
8715 }
8716 else if (argvars[0].v_type == VAR_DICT)
8717 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008718 if ((d = argvars[0].vval.v_dict) == NULL
8719 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008720 return;
8721 }
8722 else
8723 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008724 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008725 return;
8726 }
8727
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008728 expr = get_tv_string_buf_chk(&argvars[1], buf);
8729 /* On type errors, the preceding call has already displayed an error
8730 * message. Avoid a misleading error message for an empty string that
8731 * was not passed as argument. */
8732 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008733 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008734 prepare_vimvar(VV_VAL, &save_val);
8735 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008737 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008738 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008739 prepare_vimvar(VV_KEY, &save_key);
8740 vimvars[VV_KEY].vv_type = VAR_STRING;
8741
8742 ht = &d->dv_hashtab;
8743 hash_lock(ht);
8744 todo = ht->ht_used;
8745 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008747 if (!HASHITEM_EMPTY(hi))
8748 {
8749 --todo;
8750 di = HI2DI(hi);
8751 if (tv_check_lock(di->di_tv.v_lock, msg))
8752 break;
8753 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8754 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8755 break;
8756 if (!map && rem)
8757 dictitem_remove(d, di);
8758 clear_tv(&vimvars[VV_KEY].vv_tv);
8759 }
8760 }
8761 hash_unlock(ht);
8762
8763 restore_vimvar(VV_KEY, &save_key);
8764 }
8765 else
8766 {
8767 for (li = l->lv_first; li != NULL; li = nli)
8768 {
8769 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008770 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008771 nli = li->li_next;
8772 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008773 break;
8774 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008775 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008776 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008777 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008778
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008779 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008780 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008781
8782 copy_tv(&argvars[0], rettv);
8783}
8784
8785 static int
8786filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008787 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008788 char_u *expr;
8789 int map;
8790 int *remp;
8791{
Bram Moolenaar33570922005-01-25 22:26:29 +00008792 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008793 char_u *s;
8794
Bram Moolenaar33570922005-01-25 22:26:29 +00008795 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008796 s = expr;
8797 if (eval1(&s, &rettv, TRUE) == FAIL)
8798 return FAIL;
8799 if (*s != NUL) /* check for trailing chars after expr */
8800 {
8801 EMSG2(_(e_invexpr2), s);
8802 return FAIL;
8803 }
8804 if (map)
8805 {
8806 /* map(): replace the list item value */
8807 clear_tv(tv);
8808 *tv = rettv;
8809 }
8810 else
8811 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008812 int error = FALSE;
8813
Bram Moolenaare9a41262005-01-15 22:18:47 +00008814 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008815 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008816 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008817 /* On type error, nothing has been removed; return FAIL to stop the
8818 * loop. The error message was given by get_tv_number_chk(). */
8819 if (error)
8820 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008821 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008822 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008823 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008824}
8825
8826/*
8827 * "filter()" function
8828 */
8829 static void
8830f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008831 typval_T *argvars;
8832 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008833{
8834 filter_map(argvars, rettv, FALSE);
8835}
8836
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008837/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008838 * "finddir({fname}[, {path}[, {count}]])" function
8839 */
8840 static void
8841f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008842 typval_T *argvars;
8843 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008844{
8845 findfilendir(argvars, rettv, TRUE);
8846}
8847
8848/*
8849 * "findfile({fname}[, {path}[, {count}]])" function
8850 */
8851 static void
8852f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008853 typval_T *argvars;
8854 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008855{
8856 findfilendir(argvars, rettv, FALSE);
8857}
8858
8859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 * "fnamemodify({fname}, {mods})" function
8861 */
8862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008863f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008864 typval_T *argvars;
8865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866{
8867 char_u *fname;
8868 char_u *mods;
8869 int usedlen = 0;
8870 int len;
8871 char_u *fbuf = NULL;
8872 char_u buf[NUMBUFLEN];
8873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008874 fname = get_tv_string_chk(&argvars[0]);
8875 mods = get_tv_string_buf_chk(&argvars[1], buf);
8876 if (fname == NULL || mods == NULL)
8877 fname = NULL;
8878 else
8879 {
8880 len = (int)STRLEN(fname);
8881 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008884 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008886 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008888 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 vim_free(fbuf);
8890}
8891
Bram Moolenaar33570922005-01-25 22:26:29 +00008892static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893
8894/*
8895 * "foldclosed()" function
8896 */
8897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008898foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008899 typval_T *argvars;
8900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 int end;
8902{
8903#ifdef FEAT_FOLDING
8904 linenr_T lnum;
8905 linenr_T first, last;
8906
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008907 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8909 {
8910 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8911 {
8912 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 return;
8917 }
8918 }
8919#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008920 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921}
8922
8923/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008924 * "foldclosed()" function
8925 */
8926 static void
8927f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008928 typval_T *argvars;
8929 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008930{
8931 foldclosed_both(argvars, rettv, FALSE);
8932}
8933
8934/*
8935 * "foldclosedend()" function
8936 */
8937 static void
8938f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008939 typval_T *argvars;
8940 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008941{
8942 foldclosed_both(argvars, rettv, TRUE);
8943}
8944
8945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 * "foldlevel()" function
8947 */
8948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008950 typval_T *argvars;
8951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952{
8953#ifdef FEAT_FOLDING
8954 linenr_T lnum;
8955
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008956 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 else
8960#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008961 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962}
8963
8964/*
8965 * "foldtext()" function
8966 */
8967/*ARGSUSED*/
8968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008970 typval_T *argvars;
8971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972{
8973#ifdef FEAT_FOLDING
8974 linenr_T lnum;
8975 char_u *s;
8976 char_u *r;
8977 int len;
8978 char *txt;
8979#endif
8980
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981 rettv->v_type = VAR_STRING;
8982 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008984 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8985 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8986 <= curbuf->b_ml.ml_line_count
8987 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 {
8989 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008990 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8991 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 {
8993 if (!linewhite(lnum))
8994 break;
8995 ++lnum;
8996 }
8997
8998 /* Find interesting text in this line. */
8999 s = skipwhite(ml_get(lnum));
9000 /* skip C comment-start */
9001 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009002 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009004 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009005 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009006 {
9007 s = skipwhite(ml_get(lnum + 1));
9008 if (*s == '*')
9009 s = skipwhite(s + 1);
9010 }
9011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 txt = _("+-%s%3ld lines: ");
9013 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009014 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 + 20 /* for %3ld */
9016 + STRLEN(s))); /* concatenated */
9017 if (r != NULL)
9018 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009019 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9020 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9021 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 len = (int)STRLEN(r);
9023 STRCAT(r, s);
9024 /* remove 'foldmarker' and 'commentstring' */
9025 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 }
9028 }
9029#endif
9030}
9031
9032/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009033 * "foldtextresult(lnum)" function
9034 */
9035/*ARGSUSED*/
9036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009037f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009038 typval_T *argvars;
9039 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009040{
9041#ifdef FEAT_FOLDING
9042 linenr_T lnum;
9043 char_u *text;
9044 char_u buf[51];
9045 foldinfo_T foldinfo;
9046 int fold_count;
9047#endif
9048
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 rettv->v_type = VAR_STRING;
9050 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009051#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009052 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009053 /* treat illegal types and illegal string values for {lnum} the same */
9054 if (lnum < 0)
9055 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009056 fold_count = foldedCount(curwin, lnum, &foldinfo);
9057 if (fold_count > 0)
9058 {
9059 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9060 &foldinfo, buf);
9061 if (text == buf)
9062 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009063 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009064 }
9065#endif
9066}
9067
9068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 * "foreground()" function
9070 */
9071/*ARGSUSED*/
9072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009073f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009074 typval_T *argvars;
9075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078#ifdef FEAT_GUI
9079 if (gui.in_use)
9080 gui_mch_set_foreground();
9081#else
9082# ifdef WIN32
9083 win32_set_foreground();
9084# endif
9085#endif
9086}
9087
9088/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009089 * "function()" function
9090 */
9091/*ARGSUSED*/
9092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009093f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009094 typval_T *argvars;
9095 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009096{
9097 char_u *s;
9098
Bram Moolenaara7043832005-01-21 11:56:39 +00009099 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009100 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009101 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009102 EMSG2(_(e_invarg2), s);
9103 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009104 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009105 else
9106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107 rettv->vval.v_string = vim_strsave(s);
9108 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009109 }
9110}
9111
9112/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009113 * "garbagecollect()" function
9114 */
9115/*ARGSUSED*/
9116 static void
9117f_garbagecollect(argvars, rettv)
9118 typval_T *argvars;
9119 typval_T *rettv;
9120{
9121 garbage_collect();
9122}
9123
9124/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009125 * "get()" function
9126 */
9127 static void
9128f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009129 typval_T *argvars;
9130 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009131{
Bram Moolenaar33570922005-01-25 22:26:29 +00009132 listitem_T *li;
9133 list_T *l;
9134 dictitem_T *di;
9135 dict_T *d;
9136 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009137
Bram Moolenaare9a41262005-01-15 22:18:47 +00009138 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009139 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009140 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009142 int error = FALSE;
9143
9144 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9145 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009146 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009147 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009148 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009149 else if (argvars[0].v_type == VAR_DICT)
9150 {
9151 if ((d = argvars[0].vval.v_dict) != NULL)
9152 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009153 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009154 if (di != NULL)
9155 tv = &di->di_tv;
9156 }
9157 }
9158 else
9159 EMSG2(_(e_listdictarg), "get()");
9160
9161 if (tv == NULL)
9162 {
9163 if (argvars[2].v_type == VAR_UNKNOWN)
9164 rettv->vval.v_number = 0;
9165 else
9166 copy_tv(&argvars[2], rettv);
9167 }
9168 else
9169 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009170}
9171
Bram Moolenaar342337a2005-07-21 21:11:17 +00009172static 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 +00009173
9174/*
9175 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009176 * Return a range (from start to end) of lines in rettv from the specified
9177 * buffer.
9178 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009179 */
9180 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009181get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009182 buf_T *buf;
9183 linenr_T start;
9184 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009185 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009186 typval_T *rettv;
9187{
9188 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009189 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009190 listitem_T *li;
9191
Bram Moolenaar342337a2005-07-21 21:11:17 +00009192 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009193 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009194 l = list_alloc();
9195 if (l == NULL)
9196 return;
9197
9198 rettv->vval.v_list = l;
9199 rettv->v_type = VAR_LIST;
9200 ++l->lv_refcount;
9201 }
9202 else
9203 rettv->vval.v_number = 0;
9204
9205 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9206 return;
9207
9208 if (!retlist)
9209 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009210 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9211 p = ml_get_buf(buf, start, FALSE);
9212 else
9213 p = (char_u *)"";
9214
9215 rettv->v_type = VAR_STRING;
9216 rettv->vval.v_string = vim_strsave(p);
9217 }
9218 else
9219 {
9220 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009221 return;
9222
9223 if (start < 1)
9224 start = 1;
9225 if (end > buf->b_ml.ml_line_count)
9226 end = buf->b_ml.ml_line_count;
9227 while (start <= end)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009228 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009229 li = listitem_alloc();
9230 if (li == NULL)
9231 break;
9232 list_append(l, li);
9233 li->li_tv.v_type = VAR_STRING;
9234 li->li_tv.v_lock = 0;
9235 li->li_tv.vval.v_string =
9236 vim_strsave(ml_get_buf(buf, start++, FALSE));
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009237 }
9238 }
9239}
9240
9241/*
9242 * "getbufline()" function
9243 */
9244 static void
9245f_getbufline(argvars, rettv)
9246 typval_T *argvars;
9247 typval_T *rettv;
9248{
9249 linenr_T lnum;
9250 linenr_T end;
9251 buf_T *buf;
9252
9253 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9254 ++emsg_off;
9255 buf = get_buf_tv(&argvars[0]);
9256 --emsg_off;
9257
Bram Moolenaar661b1822005-07-28 22:36:45 +00009258 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009259 if (argvars[2].v_type == VAR_UNKNOWN)
9260 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009261 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009262 end = get_tv_lnum_buf(&argvars[2], buf);
9263
Bram Moolenaar342337a2005-07-21 21:11:17 +00009264 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009265}
9266
Bram Moolenaar0d660222005-01-07 21:51:51 +00009267/*
9268 * "getbufvar()" function
9269 */
9270 static void
9271f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009272 typval_T *argvars;
9273 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009274{
9275 buf_T *buf;
9276 buf_T *save_curbuf;
9277 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009278 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009279
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009280 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9281 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009282 ++emsg_off;
9283 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009284
9285 rettv->v_type = VAR_STRING;
9286 rettv->vval.v_string = NULL;
9287
9288 if (buf != NULL && varname != NULL)
9289 {
9290 if (*varname == '&') /* buffer-local-option */
9291 {
9292 /* set curbuf to be our buf, temporarily */
9293 save_curbuf = curbuf;
9294 curbuf = buf;
9295
9296 get_option_tv(&varname, rettv, TRUE);
9297
9298 /* restore previous notion of curbuf */
9299 curbuf = save_curbuf;
9300 }
9301 else
9302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009303 if (*varname == NUL)
9304 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9305 * scope prefix before the NUL byte is required by
9306 * find_var_in_ht(). */
9307 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009308 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009309 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009310 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009311 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009312 }
9313 }
9314
9315 --emsg_off;
9316}
9317
9318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 * "getchar()" function
9320 */
9321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009322f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009323 typval_T *argvars;
9324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325{
9326 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009327 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328
9329 ++no_mapping;
9330 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009331 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 /* getchar(): blocking wait. */
9333 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009334 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 /* getchar(1): only check if char avail */
9336 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009337 else if (error || vpeekc() == NUL)
9338 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 n = 0;
9340 else
9341 /* getchar(0) and char avail: return char */
9342 n = safe_vgetc();
9343 --no_mapping;
9344 --allow_keys;
9345
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009346 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 if (IS_SPECIAL(n) || mod_mask != 0)
9348 {
9349 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9350 int i = 0;
9351
9352 /* Turn a special key into three bytes, plus modifier. */
9353 if (mod_mask != 0)
9354 {
9355 temp[i++] = K_SPECIAL;
9356 temp[i++] = KS_MODIFIER;
9357 temp[i++] = mod_mask;
9358 }
9359 if (IS_SPECIAL(n))
9360 {
9361 temp[i++] = K_SPECIAL;
9362 temp[i++] = K_SECOND(n);
9363 temp[i++] = K_THIRD(n);
9364 }
9365#ifdef FEAT_MBYTE
9366 else if (has_mbyte)
9367 i += (*mb_char2bytes)(n, temp + i);
9368#endif
9369 else
9370 temp[i++] = n;
9371 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372 rettv->v_type = VAR_STRING;
9373 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 }
9375}
9376
9377/*
9378 * "getcharmod()" function
9379 */
9380/*ARGSUSED*/
9381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009382f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009383 typval_T *argvars;
9384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009386 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387}
9388
9389/*
9390 * "getcmdline()" function
9391 */
9392/*ARGSUSED*/
9393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009395 typval_T *argvars;
9396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398 rettv->v_type = VAR_STRING;
9399 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400}
9401
9402/*
9403 * "getcmdpos()" function
9404 */
9405/*ARGSUSED*/
9406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009407f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009408 typval_T *argvars;
9409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009411 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412}
9413
9414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 * "getcwd()" function
9416 */
9417/*ARGSUSED*/
9418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009419f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009420 typval_T *argvars;
9421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422{
9423 char_u cwd[MAXPATHL];
9424
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009425 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 else
9429 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009430 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009432 if (rettv->vval.v_string != NULL)
9433 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434#endif
9435 }
9436}
9437
9438/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009439 * "getfontname()" function
9440 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009441/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009443f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009444 typval_T *argvars;
9445 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009446{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009447 rettv->v_type = VAR_STRING;
9448 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009449#ifdef FEAT_GUI
9450 if (gui.in_use)
9451 {
9452 GuiFont font;
9453 char_u *name = NULL;
9454
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009455 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009456 {
9457 /* Get the "Normal" font. Either the name saved by
9458 * hl_set_font_name() or from the font ID. */
9459 font = gui.norm_font;
9460 name = hl_get_font_name();
9461 }
9462 else
9463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009465 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9466 return;
9467 font = gui_mch_get_font(name, FALSE);
9468 if (font == NOFONT)
9469 return; /* Invalid font name, return empty string. */
9470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009472 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009473 gui_mch_free_font(font);
9474 }
9475#endif
9476}
9477
9478/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009479 * "getfperm({fname})" function
9480 */
9481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009483 typval_T *argvars;
9484 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009485{
9486 char_u *fname;
9487 struct stat st;
9488 char_u *perm = NULL;
9489 char_u flags[] = "rwx";
9490 int i;
9491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009493
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009494 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009495 if (mch_stat((char *)fname, &st) >= 0)
9496 {
9497 perm = vim_strsave((char_u *)"---------");
9498 if (perm != NULL)
9499 {
9500 for (i = 0; i < 9; i++)
9501 {
9502 if (st.st_mode & (1 << (8 - i)))
9503 perm[i] = flags[i % 3];
9504 }
9505 }
9506 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009507 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009508}
9509
9510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 * "getfsize({fname})" function
9512 */
9513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009514f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009515 typval_T *argvars;
9516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517{
9518 char_u *fname;
9519 struct stat st;
9520
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009523 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524
9525 if (mch_stat((char *)fname, &st) >= 0)
9526 {
9527 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009528 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009530 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 }
9532 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534}
9535
9536/*
9537 * "getftime({fname})" function
9538 */
9539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009540f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009541 typval_T *argvars;
9542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543{
9544 char_u *fname;
9545 struct stat st;
9546
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548
9549 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009550 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553}
9554
9555/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009556 * "getftype({fname})" function
9557 */
9558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009560 typval_T *argvars;
9561 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009562{
9563 char_u *fname;
9564 struct stat st;
9565 char_u *type = NULL;
9566 char *t;
9567
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009568 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009569
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009570 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009571 if (mch_lstat((char *)fname, &st) >= 0)
9572 {
9573#ifdef S_ISREG
9574 if (S_ISREG(st.st_mode))
9575 t = "file";
9576 else if (S_ISDIR(st.st_mode))
9577 t = "dir";
9578# ifdef S_ISLNK
9579 else if (S_ISLNK(st.st_mode))
9580 t = "link";
9581# endif
9582# ifdef S_ISBLK
9583 else if (S_ISBLK(st.st_mode))
9584 t = "bdev";
9585# endif
9586# ifdef S_ISCHR
9587 else if (S_ISCHR(st.st_mode))
9588 t = "cdev";
9589# endif
9590# ifdef S_ISFIFO
9591 else if (S_ISFIFO(st.st_mode))
9592 t = "fifo";
9593# endif
9594# ifdef S_ISSOCK
9595 else if (S_ISSOCK(st.st_mode))
9596 t = "fifo";
9597# endif
9598 else
9599 t = "other";
9600#else
9601# ifdef S_IFMT
9602 switch (st.st_mode & S_IFMT)
9603 {
9604 case S_IFREG: t = "file"; break;
9605 case S_IFDIR: t = "dir"; break;
9606# ifdef S_IFLNK
9607 case S_IFLNK: t = "link"; break;
9608# endif
9609# ifdef S_IFBLK
9610 case S_IFBLK: t = "bdev"; break;
9611# endif
9612# ifdef S_IFCHR
9613 case S_IFCHR: t = "cdev"; break;
9614# endif
9615# ifdef S_IFIFO
9616 case S_IFIFO: t = "fifo"; break;
9617# endif
9618# ifdef S_IFSOCK
9619 case S_IFSOCK: t = "socket"; break;
9620# endif
9621 default: t = "other";
9622 }
9623# else
9624 if (mch_isdir(fname))
9625 t = "dir";
9626 else
9627 t = "file";
9628# endif
9629#endif
9630 type = vim_strsave((char_u *)t);
9631 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009632 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009633}
9634
9635/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009636 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009637 */
9638 static void
9639f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009640 typval_T *argvars;
9641 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009642{
9643 linenr_T lnum;
9644 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009645 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009646
9647 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009648 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009649 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009650 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009651 retlist = FALSE;
9652 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009653 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009654 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009655 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009656 retlist = TRUE;
9657 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009658
Bram Moolenaar342337a2005-07-21 21:11:17 +00009659 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009660}
9661
9662/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009663 * "getqflist()" function
9664 */
9665/*ARGSUSED*/
9666 static void
9667f_getqflist(argvars, rettv)
9668 typval_T *argvars;
9669 typval_T *rettv;
9670{
9671#ifdef FEAT_QUICKFIX
9672 list_T *l;
9673#endif
9674
9675 rettv->vval.v_number = FALSE;
9676#ifdef FEAT_QUICKFIX
9677 l = list_alloc();
9678 if (l != NULL)
9679 {
9680 if (get_errorlist(l) != FAIL)
9681 {
9682 rettv->vval.v_list = l;
9683 rettv->v_type = VAR_LIST;
9684 ++l->lv_refcount;
9685 }
9686 else
9687 list_free(l);
9688 }
9689#endif
9690}
9691
9692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 * "getreg()" function
9694 */
9695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009697 typval_T *argvars;
9698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699{
9700 char_u *strregname;
9701 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009702 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009703 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009705 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009707 strregname = get_tv_string_chk(&argvars[0]);
9708 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009709 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009710 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009713 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 regname = (strregname == NULL ? '"' : *strregname);
9715 if (regname == 0)
9716 regname = '"';
9717
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009719 rettv->vval.v_string = error ? NULL :
9720 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721}
9722
9723/*
9724 * "getregtype()" function
9725 */
9726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009728 typval_T *argvars;
9729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730{
9731 char_u *strregname;
9732 int regname;
9733 char_u buf[NUMBUFLEN + 2];
9734 long reglen = 0;
9735
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009736 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009737 {
9738 strregname = get_tv_string_chk(&argvars[0]);
9739 if (strregname == NULL) /* type error; errmsg already given */
9740 {
9741 rettv->v_type = VAR_STRING;
9742 rettv->vval.v_string = NULL;
9743 return;
9744 }
9745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 else
9747 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009748 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749
9750 regname = (strregname == NULL ? '"' : *strregname);
9751 if (regname == 0)
9752 regname = '"';
9753
9754 buf[0] = NUL;
9755 buf[1] = NUL;
9756 switch (get_reg_type(regname, &reglen))
9757 {
9758 case MLINE: buf[0] = 'V'; break;
9759 case MCHAR: buf[0] = 'v'; break;
9760#ifdef FEAT_VISUAL
9761 case MBLOCK:
9762 buf[0] = Ctrl_V;
9763 sprintf((char *)buf + 1, "%ld", reglen + 1);
9764 break;
9765#endif
9766 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767 rettv->v_type = VAR_STRING;
9768 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769}
9770
9771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 * "getwinposx()" function
9773 */
9774/*ARGSUSED*/
9775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009777 typval_T *argvars;
9778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781#ifdef FEAT_GUI
9782 if (gui.in_use)
9783 {
9784 int x, y;
9785
9786 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 }
9789#endif
9790}
9791
9792/*
9793 * "getwinposy()" function
9794 */
9795/*ARGSUSED*/
9796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009797f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009798 typval_T *argvars;
9799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802#ifdef FEAT_GUI
9803 if (gui.in_use)
9804 {
9805 int x, y;
9806
9807 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009808 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 }
9810#endif
9811}
9812
Bram Moolenaara40058a2005-07-11 22:42:07 +00009813static win_T *find_win_by_nr __ARGS((typval_T *vp));
9814
9815 static win_T *
9816find_win_by_nr(vp)
9817 typval_T *vp;
9818{
9819#ifdef FEAT_WINDOWS
9820 win_T *wp;
9821#endif
9822 int nr;
9823
9824 nr = get_tv_number_chk(vp, NULL);
9825
9826#ifdef FEAT_WINDOWS
9827 if (nr < 0)
9828 return NULL;
9829 if (nr == 0)
9830 return curwin;
9831
9832 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9833 if (--nr <= 0)
9834 break;
9835 return wp;
9836#else
9837 if (nr == 0 || nr == 1)
9838 return curwin;
9839 return NULL;
9840#endif
9841}
9842
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843/*
9844 * "getwinvar()" function
9845 */
9846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009847f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009848 typval_T *argvars;
9849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850{
9851 win_T *win, *oldcurwin;
9852 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009853 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009856 varname = get_tv_string_chk(&argvars[1]);
9857 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009859 rettv->v_type = VAR_STRING;
9860 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861
9862 if (win != NULL && varname != NULL)
9863 {
9864 if (*varname == '&') /* window-local-option */
9865 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009866 /* Set curwin to be our win, temporarily. Also set curbuf, so
9867 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 oldcurwin = curwin;
9869 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009870 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009872 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873
9874 /* restore previous notion of curwin */
9875 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009876 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 }
9878 else
9879 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009880 if (*varname == NUL)
9881 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9882 * scope prefix before the NUL byte is required by
9883 * find_var_in_ht(). */
9884 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009886 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009888 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 }
9890 }
9891
9892 --emsg_off;
9893}
9894
9895/*
9896 * "glob()" function
9897 */
9898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009899f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009900 typval_T *argvars;
9901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902{
9903 expand_T xpc;
9904
9905 ExpandInit(&xpc);
9906 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009907 rettv->v_type = VAR_STRING;
9908 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9910 ExpandCleanup(&xpc);
9911}
9912
9913/*
9914 * "globpath()" function
9915 */
9916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009917f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009918 typval_T *argvars;
9919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920{
9921 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009922 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009924 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009925 if (file == NULL)
9926 rettv->vval.v_string = NULL;
9927 else
9928 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929}
9930
9931/*
9932 * "has()" function
9933 */
9934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009935f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009936 typval_T *argvars;
9937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938{
9939 int i;
9940 char_u *name;
9941 int n = FALSE;
9942 static char *(has_list[]) =
9943 {
9944#ifdef AMIGA
9945 "amiga",
9946# ifdef FEAT_ARP
9947 "arp",
9948# endif
9949#endif
9950#ifdef __BEOS__
9951 "beos",
9952#endif
9953#ifdef MSDOS
9954# ifdef DJGPP
9955 "dos32",
9956# else
9957 "dos16",
9958# endif
9959#endif
9960#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9961 "mac",
9962#endif
9963#if defined(MACOS_X_UNIX)
9964 "macunix",
9965#endif
9966#ifdef OS2
9967 "os2",
9968#endif
9969#ifdef __QNX__
9970 "qnx",
9971#endif
9972#ifdef RISCOS
9973 "riscos",
9974#endif
9975#ifdef UNIX
9976 "unix",
9977#endif
9978#ifdef VMS
9979 "vms",
9980#endif
9981#ifdef WIN16
9982 "win16",
9983#endif
9984#ifdef WIN32
9985 "win32",
9986#endif
9987#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9988 "win32unix",
9989#endif
9990#ifdef WIN64
9991 "win64",
9992#endif
9993#ifdef EBCDIC
9994 "ebcdic",
9995#endif
9996#ifndef CASE_INSENSITIVE_FILENAME
9997 "fname_case",
9998#endif
9999#ifdef FEAT_ARABIC
10000 "arabic",
10001#endif
10002#ifdef FEAT_AUTOCMD
10003 "autocmd",
10004#endif
10005#ifdef FEAT_BEVAL
10006 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010007# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10008 "balloon_multiline",
10009# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010#endif
10011#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10012 "builtin_terms",
10013# ifdef ALL_BUILTIN_TCAPS
10014 "all_builtin_terms",
10015# endif
10016#endif
10017#ifdef FEAT_BYTEOFF
10018 "byte_offset",
10019#endif
10020#ifdef FEAT_CINDENT
10021 "cindent",
10022#endif
10023#ifdef FEAT_CLIENTSERVER
10024 "clientserver",
10025#endif
10026#ifdef FEAT_CLIPBOARD
10027 "clipboard",
10028#endif
10029#ifdef FEAT_CMDL_COMPL
10030 "cmdline_compl",
10031#endif
10032#ifdef FEAT_CMDHIST
10033 "cmdline_hist",
10034#endif
10035#ifdef FEAT_COMMENTS
10036 "comments",
10037#endif
10038#ifdef FEAT_CRYPT
10039 "cryptv",
10040#endif
10041#ifdef FEAT_CSCOPE
10042 "cscope",
10043#endif
10044#ifdef DEBUG
10045 "debug",
10046#endif
10047#ifdef FEAT_CON_DIALOG
10048 "dialog_con",
10049#endif
10050#ifdef FEAT_GUI_DIALOG
10051 "dialog_gui",
10052#endif
10053#ifdef FEAT_DIFF
10054 "diff",
10055#endif
10056#ifdef FEAT_DIGRAPHS
10057 "digraphs",
10058#endif
10059#ifdef FEAT_DND
10060 "dnd",
10061#endif
10062#ifdef FEAT_EMACS_TAGS
10063 "emacs_tags",
10064#endif
10065 "eval", /* always present, of course! */
10066#ifdef FEAT_EX_EXTRA
10067 "ex_extra",
10068#endif
10069#ifdef FEAT_SEARCH_EXTRA
10070 "extra_search",
10071#endif
10072#ifdef FEAT_FKMAP
10073 "farsi",
10074#endif
10075#ifdef FEAT_SEARCHPATH
10076 "file_in_path",
10077#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010078#if defined(UNIX) && !defined(USE_SYSTEM)
10079 "filterpipe",
10080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081#ifdef FEAT_FIND_ID
10082 "find_in_path",
10083#endif
10084#ifdef FEAT_FOLDING
10085 "folding",
10086#endif
10087#ifdef FEAT_FOOTER
10088 "footer",
10089#endif
10090#if !defined(USE_SYSTEM) && defined(UNIX)
10091 "fork",
10092#endif
10093#ifdef FEAT_GETTEXT
10094 "gettext",
10095#endif
10096#ifdef FEAT_GUI
10097 "gui",
10098#endif
10099#ifdef FEAT_GUI_ATHENA
10100# ifdef FEAT_GUI_NEXTAW
10101 "gui_neXtaw",
10102# else
10103 "gui_athena",
10104# endif
10105#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +000010106#ifdef FEAT_GUI_KDE
10107 "gui_kde",
10108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109#ifdef FEAT_GUI_GTK
10110 "gui_gtk",
10111# ifdef HAVE_GTK2
10112 "gui_gtk2",
10113# endif
10114#endif
10115#ifdef FEAT_GUI_MAC
10116 "gui_mac",
10117#endif
10118#ifdef FEAT_GUI_MOTIF
10119 "gui_motif",
10120#endif
10121#ifdef FEAT_GUI_PHOTON
10122 "gui_photon",
10123#endif
10124#ifdef FEAT_GUI_W16
10125 "gui_win16",
10126#endif
10127#ifdef FEAT_GUI_W32
10128 "gui_win32",
10129#endif
10130#ifdef FEAT_HANGULIN
10131 "hangul_input",
10132#endif
10133#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10134 "iconv",
10135#endif
10136#ifdef FEAT_INS_EXPAND
10137 "insert_expand",
10138#endif
10139#ifdef FEAT_JUMPLIST
10140 "jumplist",
10141#endif
10142#ifdef FEAT_KEYMAP
10143 "keymap",
10144#endif
10145#ifdef FEAT_LANGMAP
10146 "langmap",
10147#endif
10148#ifdef FEAT_LIBCALL
10149 "libcall",
10150#endif
10151#ifdef FEAT_LINEBREAK
10152 "linebreak",
10153#endif
10154#ifdef FEAT_LISP
10155 "lispindent",
10156#endif
10157#ifdef FEAT_LISTCMDS
10158 "listcmds",
10159#endif
10160#ifdef FEAT_LOCALMAP
10161 "localmap",
10162#endif
10163#ifdef FEAT_MENU
10164 "menu",
10165#endif
10166#ifdef FEAT_SESSION
10167 "mksession",
10168#endif
10169#ifdef FEAT_MODIFY_FNAME
10170 "modify_fname",
10171#endif
10172#ifdef FEAT_MOUSE
10173 "mouse",
10174#endif
10175#ifdef FEAT_MOUSESHAPE
10176 "mouseshape",
10177#endif
10178#if defined(UNIX) || defined(VMS)
10179# ifdef FEAT_MOUSE_DEC
10180 "mouse_dec",
10181# endif
10182# ifdef FEAT_MOUSE_GPM
10183 "mouse_gpm",
10184# endif
10185# ifdef FEAT_MOUSE_JSB
10186 "mouse_jsbterm",
10187# endif
10188# ifdef FEAT_MOUSE_NET
10189 "mouse_netterm",
10190# endif
10191# ifdef FEAT_MOUSE_PTERM
10192 "mouse_pterm",
10193# endif
10194# ifdef FEAT_MOUSE_XTERM
10195 "mouse_xterm",
10196# endif
10197#endif
10198#ifdef FEAT_MBYTE
10199 "multi_byte",
10200#endif
10201#ifdef FEAT_MBYTE_IME
10202 "multi_byte_ime",
10203#endif
10204#ifdef FEAT_MULTI_LANG
10205 "multi_lang",
10206#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010207#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010208#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010209 "mzscheme",
10210#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212#ifdef FEAT_OLE
10213 "ole",
10214#endif
10215#ifdef FEAT_OSFILETYPE
10216 "osfiletype",
10217#endif
10218#ifdef FEAT_PATH_EXTRA
10219 "path_extra",
10220#endif
10221#ifdef FEAT_PERL
10222#ifndef DYNAMIC_PERL
10223 "perl",
10224#endif
10225#endif
10226#ifdef FEAT_PYTHON
10227#ifndef DYNAMIC_PYTHON
10228 "python",
10229#endif
10230#endif
10231#ifdef FEAT_POSTSCRIPT
10232 "postscript",
10233#endif
10234#ifdef FEAT_PRINTER
10235 "printer",
10236#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010237#ifdef FEAT_PROFILE
10238 "profile",
10239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240#ifdef FEAT_QUICKFIX
10241 "quickfix",
10242#endif
10243#ifdef FEAT_RIGHTLEFT
10244 "rightleft",
10245#endif
10246#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10247 "ruby",
10248#endif
10249#ifdef FEAT_SCROLLBIND
10250 "scrollbind",
10251#endif
10252#ifdef FEAT_CMDL_INFO
10253 "showcmd",
10254 "cmdline_info",
10255#endif
10256#ifdef FEAT_SIGNS
10257 "signs",
10258#endif
10259#ifdef FEAT_SMARTINDENT
10260 "smartindent",
10261#endif
10262#ifdef FEAT_SNIFF
10263 "sniff",
10264#endif
10265#ifdef FEAT_STL_OPT
10266 "statusline",
10267#endif
10268#ifdef FEAT_SUN_WORKSHOP
10269 "sun_workshop",
10270#endif
10271#ifdef FEAT_NETBEANS_INTG
10272 "netbeans_intg",
10273#endif
10274#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010275 "spell",
10276#endif
10277#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 "syntax",
10279#endif
10280#if defined(USE_SYSTEM) || !defined(UNIX)
10281 "system",
10282#endif
10283#ifdef FEAT_TAG_BINS
10284 "tag_binary",
10285#endif
10286#ifdef FEAT_TAG_OLDSTATIC
10287 "tag_old_static",
10288#endif
10289#ifdef FEAT_TAG_ANYWHITE
10290 "tag_any_white",
10291#endif
10292#ifdef FEAT_TCL
10293# ifndef DYNAMIC_TCL
10294 "tcl",
10295# endif
10296#endif
10297#ifdef TERMINFO
10298 "terminfo",
10299#endif
10300#ifdef FEAT_TERMRESPONSE
10301 "termresponse",
10302#endif
10303#ifdef FEAT_TEXTOBJ
10304 "textobjects",
10305#endif
10306#ifdef HAVE_TGETENT
10307 "tgetent",
10308#endif
10309#ifdef FEAT_TITLE
10310 "title",
10311#endif
10312#ifdef FEAT_TOOLBAR
10313 "toolbar",
10314#endif
10315#ifdef FEAT_USR_CMDS
10316 "user-commands", /* was accidentally included in 5.4 */
10317 "user_commands",
10318#endif
10319#ifdef FEAT_VIMINFO
10320 "viminfo",
10321#endif
10322#ifdef FEAT_VERTSPLIT
10323 "vertsplit",
10324#endif
10325#ifdef FEAT_VIRTUALEDIT
10326 "virtualedit",
10327#endif
10328#ifdef FEAT_VISUAL
10329 "visual",
10330#endif
10331#ifdef FEAT_VISUALEXTRA
10332 "visualextra",
10333#endif
10334#ifdef FEAT_VREPLACE
10335 "vreplace",
10336#endif
10337#ifdef FEAT_WILDIGN
10338 "wildignore",
10339#endif
10340#ifdef FEAT_WILDMENU
10341 "wildmenu",
10342#endif
10343#ifdef FEAT_WINDOWS
10344 "windows",
10345#endif
10346#ifdef FEAT_WAK
10347 "winaltkeys",
10348#endif
10349#ifdef FEAT_WRITEBACKUP
10350 "writebackup",
10351#endif
10352#ifdef FEAT_XIM
10353 "xim",
10354#endif
10355#ifdef FEAT_XFONTSET
10356 "xfontset",
10357#endif
10358#ifdef USE_XSMP
10359 "xsmp",
10360#endif
10361#ifdef USE_XSMP_INTERACT
10362 "xsmp_interact",
10363#endif
10364#ifdef FEAT_XCLIPBOARD
10365 "xterm_clipboard",
10366#endif
10367#ifdef FEAT_XTERM_SAVE
10368 "xterm_save",
10369#endif
10370#if defined(UNIX) && defined(FEAT_X11)
10371 "X11",
10372#endif
10373 NULL
10374 };
10375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010376 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 for (i = 0; has_list[i] != NULL; ++i)
10378 if (STRICMP(name, has_list[i]) == 0)
10379 {
10380 n = TRUE;
10381 break;
10382 }
10383
10384 if (n == FALSE)
10385 {
10386 if (STRNICMP(name, "patch", 5) == 0)
10387 n = has_patch(atoi((char *)name + 5));
10388 else if (STRICMP(name, "vim_starting") == 0)
10389 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010390#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10391 else if (STRICMP(name, "balloon_multiline") == 0)
10392 n = multiline_balloon_available();
10393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394#ifdef DYNAMIC_TCL
10395 else if (STRICMP(name, "tcl") == 0)
10396 n = tcl_enabled(FALSE);
10397#endif
10398#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10399 else if (STRICMP(name, "iconv") == 0)
10400 n = iconv_enabled(FALSE);
10401#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010402#ifdef DYNAMIC_MZSCHEME
10403 else if (STRICMP(name, "mzscheme") == 0)
10404 n = mzscheme_enabled(FALSE);
10405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406#ifdef DYNAMIC_RUBY
10407 else if (STRICMP(name, "ruby") == 0)
10408 n = ruby_enabled(FALSE);
10409#endif
10410#ifdef DYNAMIC_PYTHON
10411 else if (STRICMP(name, "python") == 0)
10412 n = python_enabled(FALSE);
10413#endif
10414#ifdef DYNAMIC_PERL
10415 else if (STRICMP(name, "perl") == 0)
10416 n = perl_enabled(FALSE);
10417#endif
10418#ifdef FEAT_GUI
10419 else if (STRICMP(name, "gui_running") == 0)
10420 n = (gui.in_use || gui.starting);
10421# ifdef FEAT_GUI_W32
10422 else if (STRICMP(name, "gui_win32s") == 0)
10423 n = gui_is_win32s();
10424# endif
10425# ifdef FEAT_BROWSE
10426 else if (STRICMP(name, "browse") == 0)
10427 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10428# endif
10429#endif
10430#ifdef FEAT_SYN_HL
10431 else if (STRICMP(name, "syntax_items") == 0)
10432 n = syntax_present(curbuf);
10433#endif
10434#if defined(WIN3264)
10435 else if (STRICMP(name, "win95") == 0)
10436 n = mch_windows95();
10437#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010438#ifdef FEAT_NETBEANS_INTG
10439 else if (STRICMP(name, "netbeans_enabled") == 0)
10440 n = usingNetbeans;
10441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 }
10443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010444 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445}
10446
10447/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010448 * "has_key()" function
10449 */
10450 static void
10451f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010452 typval_T *argvars;
10453 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010454{
10455 rettv->vval.v_number = 0;
10456 if (argvars[0].v_type != VAR_DICT)
10457 {
10458 EMSG(_(e_dictreq));
10459 return;
10460 }
10461 if (argvars[0].vval.v_dict == NULL)
10462 return;
10463
10464 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010465 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010466}
10467
10468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 * "hasmapto()" function
10470 */
10471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010472f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010473 typval_T *argvars;
10474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475{
10476 char_u *name;
10477 char_u *mode;
10478 char_u buf[NUMBUFLEN];
10479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010480 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010481 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 mode = (char_u *)"nvo";
10483 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010484 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485
10486 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010487 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010489 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490}
10491
10492/*
10493 * "histadd()" function
10494 */
10495/*ARGSUSED*/
10496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010497f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010498 typval_T *argvars;
10499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500{
10501#ifdef FEAT_CMDHIST
10502 int histype;
10503 char_u *str;
10504 char_u buf[NUMBUFLEN];
10505#endif
10506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010507 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 if (check_restricted() || check_secure())
10509 return;
10510#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010511 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10512 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 if (histype >= 0)
10514 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010515 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 if (*str != NUL)
10517 {
10518 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010519 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 return;
10521 }
10522 }
10523#endif
10524}
10525
10526/*
10527 * "histdel()" function
10528 */
10529/*ARGSUSED*/
10530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010531f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010532 typval_T *argvars;
10533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534{
10535#ifdef FEAT_CMDHIST
10536 int n;
10537 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010538 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010540 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10541 if (str == NULL)
10542 n = 0;
10543 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010545 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010546 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010548 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010549 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 else
10551 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010552 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010553 get_tv_string_buf(&argvars[1], buf));
10554 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557#endif
10558}
10559
10560/*
10561 * "histget()" function
10562 */
10563/*ARGSUSED*/
10564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010566 typval_T *argvars;
10567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568{
10569#ifdef FEAT_CMDHIST
10570 int type;
10571 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010572 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010574 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10575 if (str == NULL)
10576 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010578 {
10579 type = get_histtype(str);
10580 if (argvars[1].v_type == VAR_UNKNOWN)
10581 idx = get_history_idx(type);
10582 else
10583 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10584 /* -1 on type error */
10585 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010588 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010590 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591}
10592
10593/*
10594 * "histnr()" function
10595 */
10596/*ARGSUSED*/
10597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010598f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010599 typval_T *argvars;
10600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601{
10602 int i;
10603
10604#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010605 char_u *history = get_tv_string_chk(&argvars[0]);
10606
10607 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 if (i >= HIST_CMD && i < HIST_COUNT)
10609 i = get_history_idx(i);
10610 else
10611#endif
10612 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010613 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614}
10615
10616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 * "highlightID(name)" function
10618 */
10619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010620f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010621 typval_T *argvars;
10622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010624 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625}
10626
10627/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010628 * "highlight_exists()" function
10629 */
10630 static void
10631f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010632 typval_T *argvars;
10633 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010634{
10635 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10636}
10637
10638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639 * "hostname()" function
10640 */
10641/*ARGSUSED*/
10642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010643f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010644 typval_T *argvars;
10645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646{
10647 char_u hostname[256];
10648
10649 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010650 rettv->v_type = VAR_STRING;
10651 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652}
10653
10654/*
10655 * iconv() function
10656 */
10657/*ARGSUSED*/
10658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010659f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010660 typval_T *argvars;
10661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662{
10663#ifdef FEAT_MBYTE
10664 char_u buf1[NUMBUFLEN];
10665 char_u buf2[NUMBUFLEN];
10666 char_u *from, *to, *str;
10667 vimconv_T vimconv;
10668#endif
10669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010670 rettv->v_type = VAR_STRING;
10671 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672
10673#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010674 str = get_tv_string(&argvars[0]);
10675 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10676 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 vimconv.vc_type = CONV_NONE;
10678 convert_setup(&vimconv, from, to);
10679
10680 /* If the encodings are equal, no conversion needed. */
10681 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010682 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010684 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685
10686 convert_setup(&vimconv, NULL, NULL);
10687 vim_free(from);
10688 vim_free(to);
10689#endif
10690}
10691
10692/*
10693 * "indent()" function
10694 */
10695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010696f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010697 typval_T *argvars;
10698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699{
10700 linenr_T lnum;
10701
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010702 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010706 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707}
10708
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010709/*
10710 * "index()" function
10711 */
10712 static void
10713f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010714 typval_T *argvars;
10715 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010716{
Bram Moolenaar33570922005-01-25 22:26:29 +000010717 list_T *l;
10718 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010719 long idx = 0;
10720 int ic = FALSE;
10721
10722 rettv->vval.v_number = -1;
10723 if (argvars[0].v_type != VAR_LIST)
10724 {
10725 EMSG(_(e_listreq));
10726 return;
10727 }
10728 l = argvars[0].vval.v_list;
10729 if (l != NULL)
10730 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010731 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010732 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010733 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010734 int error = FALSE;
10735
Bram Moolenaar758711c2005-02-02 23:11:38 +000010736 /* Start at specified item. Use the cached index that list_find()
10737 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010738 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010739 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010740 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010741 ic = get_tv_number_chk(&argvars[3], &error);
10742 if (error)
10743 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010744 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010745
Bram Moolenaar758711c2005-02-02 23:11:38 +000010746 for ( ; item != NULL; item = item->li_next, ++idx)
10747 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010748 {
10749 rettv->vval.v_number = idx;
10750 break;
10751 }
10752 }
10753}
10754
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755static int inputsecret_flag = 0;
10756
10757/*
10758 * "input()" function
10759 * Also handles inputsecret() when inputsecret is set.
10760 */
10761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010762f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010763 typval_T *argvars;
10764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010766 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 char_u *p = NULL;
10768 int c;
10769 char_u buf[NUMBUFLEN];
10770 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010771 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010773 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774
10775#ifdef NO_CONSOLE_INPUT
10776 /* While starting up, there is no place to enter text. */
10777 if (no_console_input())
10778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010779 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 return;
10781 }
10782#endif
10783
10784 cmd_silent = FALSE; /* Want to see the prompt. */
10785 if (prompt != NULL)
10786 {
10787 /* Only the part of the message after the last NL is considered as
10788 * prompt for the command line */
10789 p = vim_strrchr(prompt, '\n');
10790 if (p == NULL)
10791 p = prompt;
10792 else
10793 {
10794 ++p;
10795 c = *p;
10796 *p = NUL;
10797 msg_start();
10798 msg_clr_eos();
10799 msg_puts_attr(prompt, echo_attr);
10800 msg_didout = FALSE;
10801 msg_starthere();
10802 *p = c;
10803 }
10804 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010806 if (argvars[1].v_type != VAR_UNKNOWN)
10807 {
10808 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10809 if (defstr != NULL)
10810 stuffReadbuffSpec(defstr);
10811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010813 if (defstr != NULL)
10814 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10816
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010817 /* since the user typed this, no need to wait for return */
10818 need_wait_return = FALSE;
10819 msg_didout = FALSE;
10820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821 cmd_silent = cmd_silent_save;
10822}
10823
10824/*
10825 * "inputdialog()" function
10826 */
10827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010828f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010829 typval_T *argvars;
10830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831{
10832#if defined(FEAT_GUI_TEXTDIALOG)
10833 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10834 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10835 {
10836 char_u *message;
10837 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010838 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010840 message = get_tv_string_chk(&argvars[0]);
10841 if (argvars[1].v_type != VAR_UNKNOWN
10842 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010843 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 else
10845 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010846 if (message != NULL && defstr != NULL
10847 && do_dialog(VIM_QUESTION, NULL, message,
10848 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010849 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 else
10851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010852 if (message != NULL && defstr != NULL
10853 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010854 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010855 rettv->vval.v_string = vim_strsave(
10856 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010858 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010860 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 }
10862 else
10863#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010864 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865}
10866
10867static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10868
10869/*
10870 * "inputrestore()" function
10871 */
10872/*ARGSUSED*/
10873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010875 typval_T *argvars;
10876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877{
10878 if (ga_userinput.ga_len > 0)
10879 {
10880 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10882 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 }
10885 else if (p_verbose > 1)
10886 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010887 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010888 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 }
10890}
10891
10892/*
10893 * "inputsave()" function
10894 */
10895/*ARGSUSED*/
10896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010897f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010898 typval_T *argvars;
10899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900{
10901 /* Add an entry to the stack of typehead storage. */
10902 if (ga_grow(&ga_userinput, 1) == OK)
10903 {
10904 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10905 + ga_userinput.ga_len);
10906 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010907 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 }
10909 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010910 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911}
10912
10913/*
10914 * "inputsecret()" function
10915 */
10916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010918 typval_T *argvars;
10919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920{
10921 ++cmdline_star;
10922 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010923 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 --cmdline_star;
10925 --inputsecret_flag;
10926}
10927
10928/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010929 * "insert()" function
10930 */
10931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010932f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010933 typval_T *argvars;
10934 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010935{
10936 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010937 listitem_T *item;
10938 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010939 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010940
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010941 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010942 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010943 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010944 else if ((l = argvars[0].vval.v_list) != NULL
10945 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010946 {
10947 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010948 before = get_tv_number_chk(&argvars[2], &error);
10949 if (error)
10950 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010951
Bram Moolenaar758711c2005-02-02 23:11:38 +000010952 if (before == l->lv_len)
10953 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010954 else
10955 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010956 item = list_find(l, before);
10957 if (item == NULL)
10958 {
10959 EMSGN(_(e_listidx), before);
10960 l = NULL;
10961 }
10962 }
10963 if (l != NULL)
10964 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010965 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010966 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010967 }
10968 }
10969}
10970
10971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 * "isdirectory()" function
10973 */
10974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010976 typval_T *argvars;
10977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010979 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980}
10981
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010982/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010983 * "islocked()" function
10984 */
10985 static void
10986f_islocked(argvars, rettv)
10987 typval_T *argvars;
10988 typval_T *rettv;
10989{
10990 lval_T lv;
10991 char_u *end;
10992 dictitem_T *di;
10993
10994 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010995 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10996 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010997 if (end != NULL && lv.ll_name != NULL)
10998 {
10999 if (*end != NUL)
11000 EMSG(_(e_trailing));
11001 else
11002 {
11003 if (lv.ll_tv == NULL)
11004 {
11005 if (check_changedtick(lv.ll_name))
11006 rettv->vval.v_number = 1; /* always locked */
11007 else
11008 {
11009 di = find_var(lv.ll_name, NULL);
11010 if (di != NULL)
11011 {
11012 /* Consider a variable locked when:
11013 * 1. the variable itself is locked
11014 * 2. the value of the variable is locked.
11015 * 3. the List or Dict value is locked.
11016 */
11017 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11018 || tv_islocked(&di->di_tv));
11019 }
11020 }
11021 }
11022 else if (lv.ll_range)
11023 EMSG(_("E745: Range not allowed"));
11024 else if (lv.ll_newkey != NULL)
11025 EMSG2(_(e_dictkey), lv.ll_newkey);
11026 else if (lv.ll_list != NULL)
11027 /* List item. */
11028 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11029 else
11030 /* Dictionary item. */
11031 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11032 }
11033 }
11034
11035 clear_lval(&lv);
11036}
11037
Bram Moolenaar33570922005-01-25 22:26:29 +000011038static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011039
11040/*
11041 * Turn a dict into a list:
11042 * "what" == 0: list of keys
11043 * "what" == 1: list of values
11044 * "what" == 2: list of items
11045 */
11046 static void
11047dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011048 typval_T *argvars;
11049 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011050 int what;
11051{
Bram Moolenaar33570922005-01-25 22:26:29 +000011052 list_T *l;
11053 list_T *l2;
11054 dictitem_T *di;
11055 hashitem_T *hi;
11056 listitem_T *li;
11057 listitem_T *li2;
11058 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011059 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011060
11061 rettv->vval.v_number = 0;
11062 if (argvars[0].v_type != VAR_DICT)
11063 {
11064 EMSG(_(e_dictreq));
11065 return;
11066 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011067 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011068 return;
11069
11070 l = list_alloc();
11071 if (l == NULL)
11072 return;
11073 rettv->v_type = VAR_LIST;
11074 rettv->vval.v_list = l;
11075 ++l->lv_refcount;
11076
Bram Moolenaar33570922005-01-25 22:26:29 +000011077 todo = d->dv_hashtab.ht_used;
11078 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011079 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011080 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011081 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011082 --todo;
11083 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011084
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011085 li = listitem_alloc();
11086 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011087 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011088 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011089
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011090 if (what == 0)
11091 {
11092 /* keys() */
11093 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011094 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011095 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11096 }
11097 else if (what == 1)
11098 {
11099 /* values() */
11100 copy_tv(&di->di_tv, &li->li_tv);
11101 }
11102 else
11103 {
11104 /* items() */
11105 l2 = list_alloc();
11106 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011107 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011108 li->li_tv.vval.v_list = l2;
11109 if (l2 == NULL)
11110 break;
11111 ++l2->lv_refcount;
11112
11113 li2 = listitem_alloc();
11114 if (li2 == NULL)
11115 break;
11116 list_append(l2, li2);
11117 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011118 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011119 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11120
11121 li2 = listitem_alloc();
11122 if (li2 == NULL)
11123 break;
11124 list_append(l2, li2);
11125 copy_tv(&di->di_tv, &li2->li_tv);
11126 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011127 }
11128 }
11129}
11130
11131/*
11132 * "items(dict)" function
11133 */
11134 static void
11135f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011136 typval_T *argvars;
11137 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011138{
11139 dict_list(argvars, rettv, 2);
11140}
11141
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011143 * "join()" function
11144 */
11145 static void
11146f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011147 typval_T *argvars;
11148 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011149{
11150 garray_T ga;
11151 char_u *sep;
11152
11153 rettv->vval.v_number = 0;
11154 if (argvars[0].v_type != VAR_LIST)
11155 {
11156 EMSG(_(e_listreq));
11157 return;
11158 }
11159 if (argvars[0].vval.v_list == NULL)
11160 return;
11161 if (argvars[1].v_type == VAR_UNKNOWN)
11162 sep = (char_u *)" ";
11163 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011164 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011165
11166 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011167
11168 if (sep != NULL)
11169 {
11170 ga_init2(&ga, (int)sizeof(char), 80);
11171 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11172 ga_append(&ga, NUL);
11173 rettv->vval.v_string = (char_u *)ga.ga_data;
11174 }
11175 else
11176 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011177}
11178
11179/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011180 * "keys()" function
11181 */
11182 static void
11183f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011184 typval_T *argvars;
11185 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011186{
11187 dict_list(argvars, rettv, 0);
11188}
11189
11190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 * "last_buffer_nr()" function.
11192 */
11193/*ARGSUSED*/
11194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011195f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011196 typval_T *argvars;
11197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198{
11199 int n = 0;
11200 buf_T *buf;
11201
11202 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11203 if (n < buf->b_fnum)
11204 n = buf->b_fnum;
11205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011206 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011207}
11208
11209/*
11210 * "len()" function
11211 */
11212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011214 typval_T *argvars;
11215 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011216{
11217 switch (argvars[0].v_type)
11218 {
11219 case VAR_STRING:
11220 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011221 rettv->vval.v_number = (varnumber_T)STRLEN(
11222 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011223 break;
11224 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011225 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011226 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011227 case VAR_DICT:
11228 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11229 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011230 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011231 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011232 break;
11233 }
11234}
11235
Bram Moolenaar33570922005-01-25 22:26:29 +000011236static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011237
11238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011239libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011240 typval_T *argvars;
11241 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011242 int type;
11243{
11244#ifdef FEAT_LIBCALL
11245 char_u *string_in;
11246 char_u **string_result;
11247 int nr_result;
11248#endif
11249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011250 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011251 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011253 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011255
11256 if (check_restricted() || check_secure())
11257 return;
11258
11259#ifdef FEAT_LIBCALL
11260 /* The first two args must be strings, otherwise its meaningless */
11261 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11262 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011263 string_in = NULL;
11264 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011265 string_in = argvars[2].vval.v_string;
11266 if (type == VAR_NUMBER)
11267 string_result = NULL;
11268 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011269 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011270 if (mch_libcall(argvars[0].vval.v_string,
11271 argvars[1].vval.v_string,
11272 string_in,
11273 argvars[2].vval.v_number,
11274 string_result,
11275 &nr_result) == OK
11276 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011277 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011278 }
11279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280}
11281
11282/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011283 * "libcall()" function
11284 */
11285 static void
11286f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011287 typval_T *argvars;
11288 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011289{
11290 libcall_common(argvars, rettv, VAR_STRING);
11291}
11292
11293/*
11294 * "libcallnr()" function
11295 */
11296 static void
11297f_libcallnr(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_NUMBER);
11302}
11303
11304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 * "line(string)" function
11306 */
11307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011309 typval_T *argvars;
11310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311{
11312 linenr_T lnum = 0;
11313 pos_T *fp;
11314
11315 fp = var2fpos(&argvars[0], TRUE);
11316 if (fp != NULL)
11317 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319}
11320
11321/*
11322 * "line2byte(lnum)" function
11323 */
11324/*ARGSUSED*/
11325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011326f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011327 typval_T *argvars;
11328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329{
11330#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332#else
11333 linenr_T lnum;
11334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011337 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11340 if (rettv->vval.v_number >= 0)
11341 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342#endif
11343}
11344
11345/*
11346 * "lispindent(lnum)" function
11347 */
11348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011350 typval_T *argvars;
11351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352{
11353#ifdef FEAT_LISP
11354 pos_T pos;
11355 linenr_T lnum;
11356
11357 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11360 {
11361 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363 curwin->w_cursor = pos;
11364 }
11365 else
11366#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011367 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368}
11369
11370/*
11371 * "localtime()" function
11372 */
11373/*ARGSUSED*/
11374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011375f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011376 typval_T *argvars;
11377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011379 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380}
11381
Bram Moolenaar33570922005-01-25 22:26:29 +000011382static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383
11384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011386 typval_T *argvars;
11387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 int exact;
11389{
11390 char_u *keys;
11391 char_u *which;
11392 char_u buf[NUMBUFLEN];
11393 char_u *keys_buf = NULL;
11394 char_u *rhs;
11395 int mode;
11396 garray_T ga;
11397
11398 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011399 rettv->v_type = VAR_STRING;
11400 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403 if (*keys == NUL)
11404 return;
11405
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011406 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011407 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 else
11409 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011410 if (which == NULL)
11411 return;
11412
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 mode = get_map_mode(&which, 0);
11414
11415 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011416 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 vim_free(keys_buf);
11418 if (rhs != NULL)
11419 {
11420 ga_init(&ga);
11421 ga.ga_itemsize = 1;
11422 ga.ga_growsize = 40;
11423
11424 while (*rhs != NUL)
11425 ga_concat(&ga, str2special(&rhs, FALSE));
11426
11427 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011428 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 }
11430}
11431
11432/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011433 * "map()" function
11434 */
11435 static void
11436f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011437 typval_T *argvars;
11438 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011439{
11440 filter_map(argvars, rettv, TRUE);
11441}
11442
11443/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011444 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445 */
11446 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011447f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011448 typval_T *argvars;
11449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011451 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452}
11453
11454/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011455 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456 */
11457 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011458f_mapcheck(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, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463}
11464
Bram Moolenaar33570922005-01-25 22:26:29 +000011465static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466
11467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011469 typval_T *argvars;
11470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 int type;
11472{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011473 char_u *str = NULL;
11474 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 char_u *pat;
11476 regmatch_T regmatch;
11477 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011478 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 char_u *save_cpo;
11480 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011481 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011482 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011483 list_T *l = NULL;
11484 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011485 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011486 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487
11488 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11489 save_cpo = p_cpo;
11490 p_cpo = (char_u *)"";
11491
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011492 rettv->vval.v_number = -1;
11493 if (type == 3)
11494 {
11495 /* return empty list when there are no matches */
11496 if ((rettv->vval.v_list = list_alloc()) == NULL)
11497 goto theend;
11498 rettv->v_type = VAR_LIST;
11499 ++rettv->vval.v_list->lv_refcount;
11500 }
11501 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503 rettv->v_type = VAR_STRING;
11504 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011507 if (argvars[0].v_type == VAR_LIST)
11508 {
11509 if ((l = argvars[0].vval.v_list) == NULL)
11510 goto theend;
11511 li = l->lv_first;
11512 }
11513 else
11514 expr = str = get_tv_string(&argvars[0]);
11515
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011516 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11517 if (pat == NULL)
11518 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011519
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011520 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011522 int error = FALSE;
11523
11524 start = get_tv_number_chk(&argvars[2], &error);
11525 if (error)
11526 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011527 if (l != NULL)
11528 {
11529 li = list_find(l, start);
11530 if (li == NULL)
11531 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011532 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011533 }
11534 else
11535 {
11536 if (start < 0)
11537 start = 0;
11538 if (start > (long)STRLEN(str))
11539 goto theend;
11540 str += start;
11541 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011542
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011543 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011544 nth = get_tv_number_chk(&argvars[3], &error);
11545 if (error)
11546 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547 }
11548
11549 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11550 if (regmatch.regprog != NULL)
11551 {
11552 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011553
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011554 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011555 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011556 if (l != NULL)
11557 {
11558 if (li == NULL)
11559 {
11560 match = FALSE;
11561 break;
11562 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011563 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011564 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011565 if (str == NULL)
11566 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011567 }
11568
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011569 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011570
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011571 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011572 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011573 if (l == NULL && !match)
11574 break;
11575
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011576 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011577 if (l != NULL)
11578 {
11579 li = li->li_next;
11580 ++idx;
11581 }
11582 else
11583 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011584#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011585 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011586#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011587 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011588#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011589 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011590 }
11591
11592 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011594 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011595 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011596 int i;
11597
11598 /* return list with matched string and submatches */
11599 for (i = 0; i < NSUBEXP; ++i)
11600 {
11601 if (regmatch.endp[i] == NULL)
11602 break;
11603 li = listitem_alloc();
11604 if (li == NULL)
11605 break;
11606 li->li_tv.v_type = VAR_STRING;
11607 li->li_tv.v_lock = 0;
11608 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11609 (int)(regmatch.endp[i] - regmatch.startp[i]));
11610 list_append(rettv->vval.v_list, li);
11611 }
11612 }
11613 else if (type == 2)
11614 {
11615 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011616 if (l != NULL)
11617 copy_tv(&li->li_tv, rettv);
11618 else
11619 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011621 }
11622 else if (l != NULL)
11623 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624 else
11625 {
11626 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 (varnumber_T)(regmatch.startp[0] - str);
11629 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011630 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011632 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633 }
11634 }
11635 vim_free(regmatch.regprog);
11636 }
11637
11638theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011639 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640 p_cpo = save_cpo;
11641}
11642
11643/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011644 * "match()" function
11645 */
11646 static void
11647f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011648 typval_T *argvars;
11649 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011650{
11651 find_some_match(argvars, rettv, 1);
11652}
11653
11654/*
11655 * "matchend()" function
11656 */
11657 static void
11658f_matchend(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, 0);
11663}
11664
11665/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011666 * "matchlist()" function
11667 */
11668 static void
11669f_matchlist(argvars, rettv)
11670 typval_T *argvars;
11671 typval_T *rettv;
11672{
11673 find_some_match(argvars, rettv, 3);
11674}
11675
11676/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011677 * "matchstr()" function
11678 */
11679 static void
11680f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011681 typval_T *argvars;
11682 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011683{
11684 find_some_match(argvars, rettv, 2);
11685}
11686
Bram Moolenaar33570922005-01-25 22:26:29 +000011687static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011688
11689 static void
11690max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011691 typval_T *argvars;
11692 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011693 int domax;
11694{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011695 long n = 0;
11696 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011697 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011698
11699 if (argvars[0].v_type == VAR_LIST)
11700 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011701 list_T *l;
11702 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011703
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011704 l = argvars[0].vval.v_list;
11705 if (l != NULL)
11706 {
11707 li = l->lv_first;
11708 if (li != NULL)
11709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011710 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011711 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011712 {
11713 li = li->li_next;
11714 if (li == NULL)
11715 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011716 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011717 if (domax ? i > n : i < n)
11718 n = i;
11719 }
11720 }
11721 }
11722 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011723 else if (argvars[0].v_type == VAR_DICT)
11724 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011725 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011726 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011727 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011728 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011729
11730 d = argvars[0].vval.v_dict;
11731 if (d != NULL)
11732 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011733 todo = d->dv_hashtab.ht_used;
11734 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011735 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011736 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011737 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011738 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011739 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011740 if (first)
11741 {
11742 n = i;
11743 first = FALSE;
11744 }
11745 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011746 n = i;
11747 }
11748 }
11749 }
11750 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011751 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011752 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011753 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011754}
11755
11756/*
11757 * "max()" function
11758 */
11759 static void
11760f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011761 typval_T *argvars;
11762 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011763{
11764 max_min(argvars, rettv, TRUE);
11765}
11766
11767/*
11768 * "min()" function
11769 */
11770 static void
11771f_min(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, FALSE);
11776}
11777
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011778static int mkdir_recurse __ARGS((char_u *dir, int prot));
11779
11780/*
11781 * Create the directory in which "dir" is located, and higher levels when
11782 * needed.
11783 */
11784 static int
11785mkdir_recurse(dir, prot)
11786 char_u *dir;
11787 int prot;
11788{
11789 char_u *p;
11790 char_u *updir;
11791 int r = FAIL;
11792
11793 /* Get end of directory name in "dir".
11794 * We're done when it's "/" or "c:/". */
11795 p = gettail_sep(dir);
11796 if (p <= get_past_head(dir))
11797 return OK;
11798
11799 /* If the directory exists we're done. Otherwise: create it.*/
11800 updir = vim_strnsave(dir, (int)(p - dir));
11801 if (updir == NULL)
11802 return FAIL;
11803 if (mch_isdir(updir))
11804 r = OK;
11805 else if (mkdir_recurse(updir, prot) == OK)
11806 r = vim_mkdir_emsg(updir, prot);
11807 vim_free(updir);
11808 return r;
11809}
11810
11811#ifdef vim_mkdir
11812/*
11813 * "mkdir()" function
11814 */
11815 static void
11816f_mkdir(argvars, rettv)
11817 typval_T *argvars;
11818 typval_T *rettv;
11819{
11820 char_u *dir;
11821 char_u buf[NUMBUFLEN];
11822 int prot = 0755;
11823
11824 rettv->vval.v_number = FAIL;
11825 if (check_restricted() || check_secure())
11826 return;
11827
11828 dir = get_tv_string_buf(&argvars[0], buf);
11829 if (argvars[1].v_type != VAR_UNKNOWN)
11830 {
11831 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011832 prot = get_tv_number_chk(&argvars[2], NULL);
11833 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011834 mkdir_recurse(dir, prot);
11835 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011836 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011837}
11838#endif
11839
Bram Moolenaar0d660222005-01-07 21:51:51 +000011840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 * "mode()" function
11842 */
11843/*ARGSUSED*/
11844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011845f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011846 typval_T *argvars;
11847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848{
11849 char_u buf[2];
11850
11851#ifdef FEAT_VISUAL
11852 if (VIsual_active)
11853 {
11854 if (VIsual_select)
11855 buf[0] = VIsual_mode + 's' - 'v';
11856 else
11857 buf[0] = VIsual_mode;
11858 }
11859 else
11860#endif
11861 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11862 buf[0] = 'r';
11863 else if (State & INSERT)
11864 {
11865 if (State & REPLACE_FLAG)
11866 buf[0] = 'R';
11867 else
11868 buf[0] = 'i';
11869 }
11870 else if (State & CMDLINE)
11871 buf[0] = 'c';
11872 else
11873 buf[0] = 'n';
11874
11875 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011876 rettv->vval.v_string = vim_strsave(buf);
11877 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878}
11879
11880/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011881 * "nextnonblank()" function
11882 */
11883 static void
11884f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011885 typval_T *argvars;
11886 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011887{
11888 linenr_T lnum;
11889
11890 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11891 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011892 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011893 {
11894 lnum = 0;
11895 break;
11896 }
11897 if (*skipwhite(ml_get(lnum)) != NUL)
11898 break;
11899 }
11900 rettv->vval.v_number = lnum;
11901}
11902
11903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904 * "nr2char()" function
11905 */
11906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011907f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011908 typval_T *argvars;
11909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910{
11911 char_u buf[NUMBUFLEN];
11912
11913#ifdef FEAT_MBYTE
11914 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011915 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916 else
11917#endif
11918 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011919 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920 buf[1] = NUL;
11921 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011922 rettv->v_type = VAR_STRING;
11923 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011924}
11925
11926/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011927 * "prevnonblank()" function
11928 */
11929 static void
11930f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011931 typval_T *argvars;
11932 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011933{
11934 linenr_T lnum;
11935
11936 lnum = get_tv_lnum(argvars);
11937 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11938 lnum = 0;
11939 else
11940 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11941 --lnum;
11942 rettv->vval.v_number = lnum;
11943}
11944
Bram Moolenaar8c711452005-01-14 21:53:12 +000011945/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000011946 * "printf()" function
11947 */
11948 static void
11949f_printf(argvars, rettv)
11950 typval_T *argvars;
11951 typval_T *rettv;
11952{
11953 rettv->v_type = VAR_STRING;
11954 rettv->vval.v_string = NULL;
11955#ifdef HAVE_STDARG_H
11956 {
11957 char_u buf[NUMBUFLEN];
11958 int len;
11959 char_u *s;
11960 int saved_did_emsg = did_emsg;
11961 char *fmt;
11962
11963 /* Get the required length, allocate the buffer and do it for real. */
11964 did_emsg = FALSE;
11965 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
11966 len = vim_vsnprintf(NULL, 0, fmt, NULL, argvars + 1);
11967 if (!did_emsg)
11968 {
11969 s = alloc(len + 1);
11970 if (s != NULL)
11971 {
11972 rettv->vval.v_string = s;
11973 (void)vim_vsnprintf((char *)s, len + 1, fmt, NULL, argvars + 1);
11974 }
11975 }
11976 did_emsg |= saved_did_emsg;
11977 }
11978#endif
11979}
11980
11981/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011982 * "range()" function
11983 */
11984 static void
11985f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011986 typval_T *argvars;
11987 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011988{
11989 long start;
11990 long end;
11991 long stride = 1;
11992 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011993 list_T *l;
11994 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011995 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011997 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011998 if (argvars[1].v_type == VAR_UNKNOWN)
11999 {
12000 end = start - 1;
12001 start = 0;
12002 }
12003 else
12004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012005 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012006 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012007 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012008 }
12009
12010 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012011 if (error)
12012 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012013 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012014 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012015 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012016 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012017 else
12018 {
12019 l = list_alloc();
12020 if (l != NULL)
12021 {
12022 rettv->v_type = VAR_LIST;
12023 rettv->vval.v_list = l;
12024 ++l->lv_refcount;
12025
12026 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
12027 {
12028 li = listitem_alloc();
12029 if (li == NULL)
12030 break;
12031 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012032 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012033 li->li_tv.vval.v_number = i;
12034 list_append(l, li);
12035 }
12036 }
12037 }
12038}
12039
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012040/*
12041 * "readfile()" function
12042 */
12043 static void
12044f_readfile(argvars, rettv)
12045 typval_T *argvars;
12046 typval_T *rettv;
12047{
12048 int binary = FALSE;
12049 char_u *fname;
12050 FILE *fd;
12051 list_T *l;
12052 listitem_T *li;
12053#define FREAD_SIZE 200 /* optimized for text lines */
12054 char_u buf[FREAD_SIZE];
12055 int readlen; /* size of last fread() */
12056 int buflen; /* nr of valid chars in buf[] */
12057 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12058 int tolist; /* first byte in buf[] still to be put in list */
12059 int chop; /* how many CR to chop off */
12060 char_u *prev = NULL; /* previously read bytes, if any */
12061 int prevlen = 0; /* length of "prev" if not NULL */
12062 char_u *s;
12063 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012064 long maxline = MAXLNUM;
12065 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012066
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012067 if (argvars[1].v_type != VAR_UNKNOWN)
12068 {
12069 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12070 binary = TRUE;
12071 if (argvars[2].v_type != VAR_UNKNOWN)
12072 maxline = get_tv_number(&argvars[2]);
12073 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012074
12075 l = list_alloc();
12076 if (l == NULL)
12077 return;
12078 rettv->v_type = VAR_LIST;
12079 rettv->vval.v_list = l;
12080 l->lv_refcount = 1;
12081
12082 /* Always open the file in binary mode, library functions have a mind of
12083 * their own about CR-LF conversion. */
12084 fname = get_tv_string(&argvars[0]);
12085 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12086 {
12087 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12088 return;
12089 }
12090
12091 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012092 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012093 {
12094 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12095 buflen = filtd + readlen;
12096 tolist = 0;
12097 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12098 {
12099 if (buf[filtd] == '\n' || readlen <= 0)
12100 {
12101 /* Only when in binary mode add an empty list item when the
12102 * last line ends in a '\n'. */
12103 if (!binary && readlen == 0 && filtd == 0)
12104 break;
12105
12106 /* Found end-of-line or end-of-file: add a text line to the
12107 * list. */
12108 chop = 0;
12109 if (!binary)
12110 while (filtd - chop - 1 >= tolist
12111 && buf[filtd - chop - 1] == '\r')
12112 ++chop;
12113 len = filtd - tolist - chop;
12114 if (prev == NULL)
12115 s = vim_strnsave(buf + tolist, len);
12116 else
12117 {
12118 s = alloc((unsigned)(prevlen + len + 1));
12119 if (s != NULL)
12120 {
12121 mch_memmove(s, prev, prevlen);
12122 vim_free(prev);
12123 prev = NULL;
12124 mch_memmove(s + prevlen, buf + tolist, len);
12125 s[prevlen + len] = NUL;
12126 }
12127 }
12128 tolist = filtd + 1;
12129
12130 li = listitem_alloc();
12131 if (li == NULL)
12132 {
12133 vim_free(s);
12134 break;
12135 }
12136 li->li_tv.v_type = VAR_STRING;
12137 li->li_tv.v_lock = 0;
12138 li->li_tv.vval.v_string = s;
12139 list_append(l, li);
12140
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012141 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012142 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012143 if (readlen <= 0)
12144 break;
12145 }
12146 else if (buf[filtd] == NUL)
12147 buf[filtd] = '\n';
12148 }
12149 if (readlen <= 0)
12150 break;
12151
12152 if (tolist == 0)
12153 {
12154 /* "buf" is full, need to move text to an allocated buffer */
12155 if (prev == NULL)
12156 {
12157 prev = vim_strnsave(buf, buflen);
12158 prevlen = buflen;
12159 }
12160 else
12161 {
12162 s = alloc((unsigned)(prevlen + buflen));
12163 if (s != NULL)
12164 {
12165 mch_memmove(s, prev, prevlen);
12166 mch_memmove(s + prevlen, buf, buflen);
12167 vim_free(prev);
12168 prev = s;
12169 prevlen += buflen;
12170 }
12171 }
12172 filtd = 0;
12173 }
12174 else
12175 {
12176 mch_memmove(buf, buf + tolist, buflen - tolist);
12177 filtd -= tolist;
12178 }
12179 }
12180
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012181 /*
12182 * For a negative line count use only the lines at the end of the file,
12183 * free the rest.
12184 */
12185 if (maxline < 0)
12186 while (cnt > -maxline)
12187 {
12188 listitem_remove(l, l->lv_first);
12189 --cnt;
12190 }
12191
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012192 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012193 fclose(fd);
12194}
12195
12196
Bram Moolenaar0d660222005-01-07 21:51:51 +000012197#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12198static void make_connection __ARGS((void));
12199static int check_connection __ARGS((void));
12200
12201 static void
12202make_connection()
12203{
12204 if (X_DISPLAY == NULL
12205# ifdef FEAT_GUI
12206 && !gui.in_use
12207# endif
12208 )
12209 {
12210 x_force_connect = TRUE;
12211 setup_term_clip();
12212 x_force_connect = FALSE;
12213 }
12214}
12215
12216 static int
12217check_connection()
12218{
12219 make_connection();
12220 if (X_DISPLAY == NULL)
12221 {
12222 EMSG(_("E240: No connection to Vim server"));
12223 return FAIL;
12224 }
12225 return OK;
12226}
12227#endif
12228
12229#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012230static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012231
12232 static void
12233remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012234 typval_T *argvars;
12235 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012236 int expr;
12237{
12238 char_u *server_name;
12239 char_u *keys;
12240 char_u *r = NULL;
12241 char_u buf[NUMBUFLEN];
12242# ifdef WIN32
12243 HWND w;
12244# else
12245 Window w;
12246# endif
12247
12248 if (check_restricted() || check_secure())
12249 return;
12250
12251# ifdef FEAT_X11
12252 if (check_connection() == FAIL)
12253 return;
12254# endif
12255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012256 server_name = get_tv_string_chk(&argvars[0]);
12257 if (server_name == NULL)
12258 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012259 keys = get_tv_string_buf(&argvars[1], buf);
12260# ifdef WIN32
12261 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12262# else
12263 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12264 < 0)
12265# endif
12266 {
12267 if (r != NULL)
12268 EMSG(r); /* sending worked but evaluation failed */
12269 else
12270 EMSG2(_("E241: Unable to send to %s"), server_name);
12271 return;
12272 }
12273
12274 rettv->vval.v_string = r;
12275
12276 if (argvars[2].v_type != VAR_UNKNOWN)
12277 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012278 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012279 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012280 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012281
12282 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012283 v.di_tv.v_type = VAR_STRING;
12284 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012285 idvar = get_tv_string_chk(&argvars[2]);
12286 if (idvar != NULL)
12287 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012288 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012289 }
12290}
12291#endif
12292
12293/*
12294 * "remote_expr()" function
12295 */
12296/*ARGSUSED*/
12297 static void
12298f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012299 typval_T *argvars;
12300 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012301{
12302 rettv->v_type = VAR_STRING;
12303 rettv->vval.v_string = NULL;
12304#ifdef FEAT_CLIENTSERVER
12305 remote_common(argvars, rettv, TRUE);
12306#endif
12307}
12308
12309/*
12310 * "remote_foreground()" function
12311 */
12312/*ARGSUSED*/
12313 static void
12314f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012315 typval_T *argvars;
12316 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012317{
12318 rettv->vval.v_number = 0;
12319#ifdef FEAT_CLIENTSERVER
12320# ifdef WIN32
12321 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012322 {
12323 char_u *server_name = get_tv_string_chk(&argvars[0]);
12324
12325 if (server_name != NULL)
12326 serverForeground(server_name);
12327 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012328# else
12329 /* Send a foreground() expression to the server. */
12330 argvars[1].v_type = VAR_STRING;
12331 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12332 argvars[2].v_type = VAR_UNKNOWN;
12333 remote_common(argvars, rettv, TRUE);
12334 vim_free(argvars[1].vval.v_string);
12335# endif
12336#endif
12337}
12338
12339/*ARGSUSED*/
12340 static void
12341f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012342 typval_T *argvars;
12343 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012344{
12345#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012346 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012347 char_u *s = NULL;
12348# ifdef WIN32
12349 int n = 0;
12350# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012351 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012352
12353 if (check_restricted() || check_secure())
12354 {
12355 rettv->vval.v_number = -1;
12356 return;
12357 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012358 serverid = get_tv_string_chk(&argvars[0]);
12359 if (serverid == NULL)
12360 {
12361 rettv->vval.v_number = -1;
12362 return; /* type error; errmsg already given */
12363 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012364# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012365 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012366 if (n == 0)
12367 rettv->vval.v_number = -1;
12368 else
12369 {
12370 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12371 rettv->vval.v_number = (s != NULL);
12372 }
12373# else
12374 rettv->vval.v_number = 0;
12375 if (check_connection() == FAIL)
12376 return;
12377
12378 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012379 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012380# endif
12381
12382 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12383 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012384 char_u *retvar;
12385
Bram Moolenaar33570922005-01-25 22:26:29 +000012386 v.di_tv.v_type = VAR_STRING;
12387 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012388 retvar = get_tv_string_chk(&argvars[1]);
12389 if (retvar != NULL)
12390 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012391 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012392 }
12393#else
12394 rettv->vval.v_number = -1;
12395#endif
12396}
12397
12398/*ARGSUSED*/
12399 static void
12400f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012401 typval_T *argvars;
12402 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012403{
12404 char_u *r = NULL;
12405
12406#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012407 char_u *serverid = get_tv_string_chk(&argvars[0]);
12408
12409 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012410 {
12411# ifdef WIN32
12412 /* The server's HWND is encoded in the 'id' parameter */
12413 int n = 0;
12414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012415 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012416 if (n != 0)
12417 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12418 if (r == NULL)
12419# else
12420 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012421 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012422# endif
12423 EMSG(_("E277: Unable to read a server reply"));
12424 }
12425#endif
12426 rettv->v_type = VAR_STRING;
12427 rettv->vval.v_string = r;
12428}
12429
12430/*
12431 * "remote_send()" function
12432 */
12433/*ARGSUSED*/
12434 static void
12435f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012436 typval_T *argvars;
12437 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012438{
12439 rettv->v_type = VAR_STRING;
12440 rettv->vval.v_string = NULL;
12441#ifdef FEAT_CLIENTSERVER
12442 remote_common(argvars, rettv, FALSE);
12443#endif
12444}
12445
12446/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012447 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012448 */
12449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012450f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012451 typval_T *argvars;
12452 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012453{
Bram Moolenaar33570922005-01-25 22:26:29 +000012454 list_T *l;
12455 listitem_T *item, *item2;
12456 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012457 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012458 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012459 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012460 dict_T *d;
12461 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012462
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012463 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012464 if (argvars[0].v_type == VAR_DICT)
12465 {
12466 if (argvars[2].v_type != VAR_UNKNOWN)
12467 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012468 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012469 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012471 key = get_tv_string_chk(&argvars[1]);
12472 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012474 di = dict_find(d, key, -1);
12475 if (di == NULL)
12476 EMSG2(_(e_dictkey), key);
12477 else
12478 {
12479 *rettv = di->di_tv;
12480 init_tv(&di->di_tv);
12481 dictitem_remove(d, di);
12482 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012483 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012484 }
12485 }
12486 else if (argvars[0].v_type != VAR_LIST)
12487 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012488 else if ((l = argvars[0].vval.v_list) != NULL
12489 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012490 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012491 int error = FALSE;
12492
12493 idx = get_tv_number_chk(&argvars[1], &error);
12494 if (error)
12495 ; /* type error: do nothing, errmsg already given */
12496 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012497 EMSGN(_(e_listidx), idx);
12498 else
12499 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012500 if (argvars[2].v_type == VAR_UNKNOWN)
12501 {
12502 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012503 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012504 *rettv = item->li_tv;
12505 vim_free(item);
12506 }
12507 else
12508 {
12509 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012510 end = get_tv_number_chk(&argvars[2], &error);
12511 if (error)
12512 ; /* type error: do nothing */
12513 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012514 EMSGN(_(e_listidx), end);
12515 else
12516 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012517 int cnt = 0;
12518
12519 for (li = item; li != NULL; li = li->li_next)
12520 {
12521 ++cnt;
12522 if (li == item2)
12523 break;
12524 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012525 if (li == NULL) /* didn't find "item2" after "item" */
12526 EMSG(_(e_invrange));
12527 else
12528 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012529 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012530 l = list_alloc();
12531 if (l != NULL)
12532 {
12533 rettv->v_type = VAR_LIST;
12534 rettv->vval.v_list = l;
12535 l->lv_first = item;
12536 l->lv_last = item2;
12537 l->lv_refcount = 1;
12538 item->li_prev = NULL;
12539 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012540 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012541 }
12542 }
12543 }
12544 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012545 }
12546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012547}
12548
12549/*
12550 * "rename({from}, {to})" function
12551 */
12552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012553f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012554 typval_T *argvars;
12555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556{
12557 char_u buf[NUMBUFLEN];
12558
12559 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012560 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012562 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12563 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564}
12565
12566/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012567 * "repeat()" function
12568 */
12569/*ARGSUSED*/
12570 static void
12571f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012572 typval_T *argvars;
12573 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012574{
12575 char_u *p;
12576 int n;
12577 int slen;
12578 int len;
12579 char_u *r;
12580 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012581 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012582
12583 n = get_tv_number(&argvars[1]);
12584 if (argvars[0].v_type == VAR_LIST)
12585 {
12586 l = list_alloc();
12587 if (l != NULL && argvars[0].vval.v_list != NULL)
12588 {
12589 l->lv_refcount = 1;
12590 while (n-- > 0)
12591 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12592 break;
12593 }
12594 rettv->v_type = VAR_LIST;
12595 rettv->vval.v_list = l;
12596 }
12597 else
12598 {
12599 p = get_tv_string(&argvars[0]);
12600 rettv->v_type = VAR_STRING;
12601 rettv->vval.v_string = NULL;
12602
12603 slen = (int)STRLEN(p);
12604 len = slen * n;
12605 if (len <= 0)
12606 return;
12607
12608 r = alloc(len + 1);
12609 if (r != NULL)
12610 {
12611 for (i = 0; i < n; i++)
12612 mch_memmove(r + i * slen, p, (size_t)slen);
12613 r[len] = NUL;
12614 }
12615
12616 rettv->vval.v_string = r;
12617 }
12618}
12619
12620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621 * "resolve()" function
12622 */
12623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012624f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012625 typval_T *argvars;
12626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012627{
12628 char_u *p;
12629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012630 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631#ifdef FEAT_SHORTCUT
12632 {
12633 char_u *v = NULL;
12634
12635 v = mch_resolve_shortcut(p);
12636 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012637 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012639 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640 }
12641#else
12642# ifdef HAVE_READLINK
12643 {
12644 char_u buf[MAXPATHL + 1];
12645 char_u *cpy;
12646 int len;
12647 char_u *remain = NULL;
12648 char_u *q;
12649 int is_relative_to_current = FALSE;
12650 int has_trailing_pathsep = FALSE;
12651 int limit = 100;
12652
12653 p = vim_strsave(p);
12654
12655 if (p[0] == '.' && (vim_ispathsep(p[1])
12656 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12657 is_relative_to_current = TRUE;
12658
12659 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012660 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661 has_trailing_pathsep = TRUE;
12662
12663 q = getnextcomp(p);
12664 if (*q != NUL)
12665 {
12666 /* Separate the first path component in "p", and keep the
12667 * remainder (beginning with the path separator). */
12668 remain = vim_strsave(q - 1);
12669 q[-1] = NUL;
12670 }
12671
12672 for (;;)
12673 {
12674 for (;;)
12675 {
12676 len = readlink((char *)p, (char *)buf, MAXPATHL);
12677 if (len <= 0)
12678 break;
12679 buf[len] = NUL;
12680
12681 if (limit-- == 0)
12682 {
12683 vim_free(p);
12684 vim_free(remain);
12685 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687 goto fail;
12688 }
12689
12690 /* Ensure that the result will have a trailing path separator
12691 * if the argument has one. */
12692 if (remain == NULL && has_trailing_pathsep)
12693 add_pathsep(buf);
12694
12695 /* Separate the first path component in the link value and
12696 * concatenate the remainders. */
12697 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12698 if (*q != NUL)
12699 {
12700 if (remain == NULL)
12701 remain = vim_strsave(q - 1);
12702 else
12703 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012704 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705 if (cpy != NULL)
12706 {
12707 STRCAT(cpy, remain);
12708 vim_free(remain);
12709 remain = cpy;
12710 }
12711 }
12712 q[-1] = NUL;
12713 }
12714
12715 q = gettail(p);
12716 if (q > p && *q == NUL)
12717 {
12718 /* Ignore trailing path separator. */
12719 q[-1] = NUL;
12720 q = gettail(p);
12721 }
12722 if (q > p && !mch_isFullName(buf))
12723 {
12724 /* symlink is relative to directory of argument */
12725 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12726 if (cpy != NULL)
12727 {
12728 STRCPY(cpy, p);
12729 STRCPY(gettail(cpy), buf);
12730 vim_free(p);
12731 p = cpy;
12732 }
12733 }
12734 else
12735 {
12736 vim_free(p);
12737 p = vim_strsave(buf);
12738 }
12739 }
12740
12741 if (remain == NULL)
12742 break;
12743
12744 /* Append the first path component of "remain" to "p". */
12745 q = getnextcomp(remain + 1);
12746 len = q - remain - (*q != NUL);
12747 cpy = vim_strnsave(p, STRLEN(p) + len);
12748 if (cpy != NULL)
12749 {
12750 STRNCAT(cpy, remain, len);
12751 vim_free(p);
12752 p = cpy;
12753 }
12754 /* Shorten "remain". */
12755 if (*q != NUL)
12756 STRCPY(remain, q - 1);
12757 else
12758 {
12759 vim_free(remain);
12760 remain = NULL;
12761 }
12762 }
12763
12764 /* If the result is a relative path name, make it explicitly relative to
12765 * the current directory if and only if the argument had this form. */
12766 if (!vim_ispathsep(*p))
12767 {
12768 if (is_relative_to_current
12769 && *p != NUL
12770 && !(p[0] == '.'
12771 && (p[1] == NUL
12772 || vim_ispathsep(p[1])
12773 || (p[1] == '.'
12774 && (p[2] == NUL
12775 || vim_ispathsep(p[2]))))))
12776 {
12777 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012778 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779 if (cpy != NULL)
12780 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781 vim_free(p);
12782 p = cpy;
12783 }
12784 }
12785 else if (!is_relative_to_current)
12786 {
12787 /* Strip leading "./". */
12788 q = p;
12789 while (q[0] == '.' && vim_ispathsep(q[1]))
12790 q += 2;
12791 if (q > p)
12792 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12793 }
12794 }
12795
12796 /* Ensure that the result will have no trailing path separator
12797 * if the argument had none. But keep "/" or "//". */
12798 if (!has_trailing_pathsep)
12799 {
12800 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012801 if (after_pathsep(p, q))
12802 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803 }
12804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012805 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806 }
12807# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809# endif
12810#endif
12811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012812 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813
12814#ifdef HAVE_READLINK
12815fail:
12816#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818}
12819
12820/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012821 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 */
12823 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012824f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012825 typval_T *argvars;
12826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827{
Bram Moolenaar33570922005-01-25 22:26:29 +000012828 list_T *l;
12829 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830
Bram Moolenaar0d660222005-01-07 21:51:51 +000012831 rettv->vval.v_number = 0;
12832 if (argvars[0].v_type != VAR_LIST)
12833 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012834 else if ((l = argvars[0].vval.v_list) != NULL
12835 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012836 {
12837 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012838 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012839 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012840 while (li != NULL)
12841 {
12842 ni = li->li_prev;
12843 list_append(l, li);
12844 li = ni;
12845 }
12846 rettv->vval.v_list = l;
12847 rettv->v_type = VAR_LIST;
12848 ++l->lv_refcount;
12849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850}
12851
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012852#define SP_NOMOVE 1 /* don't move cursor */
12853#define SP_REPEAT 2 /* repeat to find outer pair */
12854#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000012855#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012856
Bram Moolenaar33570922005-01-25 22:26:29 +000012857static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012858
12859/*
12860 * Get flags for a search function.
12861 * Possibly sets "p_ws".
12862 * Returns BACKWARD, FORWARD or zero (for an error).
12863 */
12864 static int
12865get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012866 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012867 int *flagsp;
12868{
12869 int dir = FORWARD;
12870 char_u *flags;
12871 char_u nbuf[NUMBUFLEN];
12872 int mask;
12873
12874 if (varp->v_type != VAR_UNKNOWN)
12875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012876 flags = get_tv_string_buf_chk(varp, nbuf);
12877 if (flags == NULL)
12878 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012879 while (*flags != NUL)
12880 {
12881 switch (*flags)
12882 {
12883 case 'b': dir = BACKWARD; break;
12884 case 'w': p_ws = TRUE; break;
12885 case 'W': p_ws = FALSE; break;
12886 default: mask = 0;
12887 if (flagsp != NULL)
12888 switch (*flags)
12889 {
12890 case 'n': mask = SP_NOMOVE; break;
12891 case 'r': mask = SP_REPEAT; break;
12892 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012893 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012894 }
12895 if (mask == 0)
12896 {
12897 EMSG2(_(e_invarg2), flags);
12898 dir = 0;
12899 }
12900 else
12901 *flagsp |= mask;
12902 }
12903 if (dir == 0)
12904 break;
12905 ++flags;
12906 }
12907 }
12908 return dir;
12909}
12910
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911/*
12912 * "search()" function
12913 */
12914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012915f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012916 typval_T *argvars;
12917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918{
12919 char_u *pat;
12920 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012921 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922 int save_p_ws = p_ws;
12923 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012924 int flags = 0;
12925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012926 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012928 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012929 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12930 if (dir == 0)
12931 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012932 /*
12933 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
12934 * Check to make sure only those flags are set.
12935 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
12936 * flags cannot be set. Check for that condition also.
12937 */
12938 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
12939 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012940 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012941 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012942 goto theend;
12943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012945 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12947 SEARCH_KEEP, RE_SEARCH) != FAIL)
12948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012949 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012950 if (flags & SP_SETPCMARK)
12951 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952 curwin->w_cursor = pos;
12953 /* "/$" will put the cursor after the end of the line, may need to
12954 * correct that here */
12955 check_cursor();
12956 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012957
12958 /* If 'n' flag is used: restore cursor position. */
12959 if (flags & SP_NOMOVE)
12960 curwin->w_cursor = save_cursor;
12961theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962 p_ws = save_p_ws;
12963}
12964
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965/*
12966 * "searchpair()" function
12967 */
12968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012969f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012970 typval_T *argvars;
12971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972{
12973 char_u *spat, *mpat, *epat;
12974 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976 int dir;
12977 int flags = 0;
12978 char_u nbuf1[NUMBUFLEN];
12979 char_u nbuf2[NUMBUFLEN];
12980 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012982 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012985 spat = get_tv_string_chk(&argvars[0]);
12986 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
12987 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
12988 if (spat == NULL || mpat == NULL || epat == NULL)
12989 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012990
Bram Moolenaar071d4272004-06-13 20:20:40 +000012991 /* Handle the optional fourth argument: flags */
12992 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012993 if (dir == 0)
12994 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012995 /*
12996 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
12997 */
12998 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
12999 {
13000 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13001 goto theend;
13002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003
13004 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013005 if (argvars[3].v_type == VAR_UNKNOWN
13006 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007 skip = (char_u *)"";
13008 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013009 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13010 if (skip == NULL)
13011 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013013 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
13014
13015theend:
13016 p_ws = save_p_ws;
13017}
13018
13019/*
13020 * Search for a start/middle/end thing.
13021 * Used by searchpair(), see its documentation for the details.
13022 * Returns 0 or -1 for no match,
13023 */
13024 long
13025do_searchpair(spat, mpat, epat, dir, skip, flags)
13026 char_u *spat; /* start pattern */
13027 char_u *mpat; /* middle pattern */
13028 char_u *epat; /* end pattern */
13029 int dir; /* BACKWARD or FORWARD */
13030 char_u *skip; /* skip expression */
13031 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
13032{
13033 char_u *save_cpo;
13034 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13035 long retval = 0;
13036 pos_T pos;
13037 pos_T firstpos;
13038 pos_T foundpos;
13039 pos_T save_cursor;
13040 pos_T save_pos;
13041 int n;
13042 int r;
13043 int nest = 1;
13044 int err;
13045
13046 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13047 save_cpo = p_cpo;
13048 p_cpo = (char_u *)"";
13049
13050 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13051 * start/middle/end (pat3, for the top pair). */
13052 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13053 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13054 if (pat2 == NULL || pat3 == NULL)
13055 goto theend;
13056 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13057 if (*mpat == NUL)
13058 STRCPY(pat3, pat2);
13059 else
13060 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13061 spat, epat, mpat);
13062
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063 save_cursor = curwin->w_cursor;
13064 pos = curwin->w_cursor;
13065 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013066 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067 pat = pat3;
13068 for (;;)
13069 {
13070 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13071 SEARCH_KEEP, RE_SEARCH);
13072 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13073 /* didn't find it or found the first match again: FAIL */
13074 break;
13075
13076 if (firstpos.lnum == 0)
13077 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013078 if (equalpos(pos, foundpos))
13079 {
13080 /* Found the same position again. Can happen with a pattern that
13081 * has "\zs" at the end and searching backwards. Advance one
13082 * character and try again. */
13083 if (dir == BACKWARD)
13084 decl(&pos);
13085 else
13086 incl(&pos);
13087 }
13088 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089
13090 /* If the skip pattern matches, ignore this match. */
13091 if (*skip != NUL)
13092 {
13093 save_pos = curwin->w_cursor;
13094 curwin->w_cursor = pos;
13095 r = eval_to_bool(skip, &err, NULL, FALSE);
13096 curwin->w_cursor = save_pos;
13097 if (err)
13098 {
13099 /* Evaluating {skip} caused an error, break here. */
13100 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013101 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102 break;
13103 }
13104 if (r)
13105 continue;
13106 }
13107
13108 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13109 {
13110 /* Found end when searching backwards or start when searching
13111 * forward: nested pair. */
13112 ++nest;
13113 pat = pat2; /* nested, don't search for middle */
13114 }
13115 else
13116 {
13117 /* Found end when searching forward or start when searching
13118 * backward: end of (nested) pair; or found middle in outer pair. */
13119 if (--nest == 1)
13120 pat = pat3; /* outer level, search for middle */
13121 }
13122
13123 if (nest == 0)
13124 {
13125 /* Found the match: return matchcount or line number. */
13126 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013127 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013129 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013130 if (flags & SP_SETPCMARK)
13131 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 curwin->w_cursor = pos;
13133 if (!(flags & SP_REPEAT))
13134 break;
13135 nest = 1; /* search for next unmatched */
13136 }
13137 }
13138
13139 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013140 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141 curwin->w_cursor = save_cursor;
13142
13143theend:
13144 vim_free(pat2);
13145 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013147
13148 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149}
13150
Bram Moolenaar0d660222005-01-07 21:51:51 +000013151/*ARGSUSED*/
13152 static void
13153f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013154 typval_T *argvars;
13155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013157#ifdef FEAT_CLIENTSERVER
13158 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013159 char_u *server = get_tv_string_chk(&argvars[0]);
13160 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161
Bram Moolenaar0d660222005-01-07 21:51:51 +000013162 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013163 if (server == NULL || reply == NULL)
13164 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013165 if (check_restricted() || check_secure())
13166 return;
13167# ifdef FEAT_X11
13168 if (check_connection() == FAIL)
13169 return;
13170# endif
13171
13172 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013174 EMSG(_("E258: Unable to send to client"));
13175 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013177 rettv->vval.v_number = 0;
13178#else
13179 rettv->vval.v_number = -1;
13180#endif
13181}
13182
13183/*ARGSUSED*/
13184 static void
13185f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013186 typval_T *argvars;
13187 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013188{
13189 char_u *r = NULL;
13190
13191#ifdef FEAT_CLIENTSERVER
13192# ifdef WIN32
13193 r = serverGetVimNames();
13194# else
13195 make_connection();
13196 if (X_DISPLAY != NULL)
13197 r = serverGetVimNames(X_DISPLAY);
13198# endif
13199#endif
13200 rettv->v_type = VAR_STRING;
13201 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202}
13203
13204/*
13205 * "setbufvar()" function
13206 */
13207/*ARGSUSED*/
13208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013209f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013210 typval_T *argvars;
13211 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212{
13213 buf_T *buf;
13214#ifdef FEAT_AUTOCMD
13215 aco_save_T aco;
13216#else
13217 buf_T *save_curbuf;
13218#endif
13219 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013220 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221 char_u nbuf[NUMBUFLEN];
13222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013223 rettv->vval.v_number = 0;
13224
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225 if (check_restricted() || check_secure())
13226 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013227 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13228 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013229 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230 varp = &argvars[2];
13231
13232 if (buf != NULL && varname != NULL && varp != NULL)
13233 {
13234 /* set curbuf to be our buf, temporarily */
13235#ifdef FEAT_AUTOCMD
13236 aucmd_prepbuf(&aco, buf);
13237#else
13238 save_curbuf = curbuf;
13239 curbuf = buf;
13240#endif
13241
13242 if (*varname == '&')
13243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013244 long numval;
13245 char_u *strval;
13246 int error = FALSE;
13247
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013249 numval = get_tv_number_chk(varp, &error);
13250 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013251 if (!error && strval != NULL)
13252 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 }
13254 else
13255 {
13256 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13257 if (bufvarname != NULL)
13258 {
13259 STRCPY(bufvarname, "b:");
13260 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013261 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 vim_free(bufvarname);
13263 }
13264 }
13265
13266 /* reset notion of buffer */
13267#ifdef FEAT_AUTOCMD
13268 aucmd_restbuf(&aco);
13269#else
13270 curbuf = save_curbuf;
13271#endif
13272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273}
13274
13275/*
13276 * "setcmdpos()" function
13277 */
13278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013279f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013280 typval_T *argvars;
13281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013283 int pos = (int)get_tv_number(&argvars[0]) - 1;
13284
13285 if (pos >= 0)
13286 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287}
13288
13289/*
13290 * "setline()" function
13291 */
13292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013293f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013294 typval_T *argvars;
13295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296{
13297 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013298 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013299 list_T *l = NULL;
13300 listitem_T *li = NULL;
13301 long added = 0;
13302 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013304 lnum = get_tv_lnum(&argvars[0]);
13305 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013307 l = argvars[1].vval.v_list;
13308 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013310 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013311 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013312
13313 rettv->vval.v_number = 0; /* OK */
13314 for (;;)
13315 {
13316 if (l != NULL)
13317 {
13318 /* list argument, get next string */
13319 if (li == NULL)
13320 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013321 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013322 li = li->li_next;
13323 }
13324
13325 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013326 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013327 break;
13328 if (lnum <= curbuf->b_ml.ml_line_count)
13329 {
13330 /* existing line, replace it */
13331 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13332 {
13333 changed_bytes(lnum, 0);
13334 check_cursor_col();
13335 rettv->vval.v_number = 0; /* OK */
13336 }
13337 }
13338 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13339 {
13340 /* lnum is one past the last line, append the line */
13341 ++added;
13342 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13343 rettv->vval.v_number = 0; /* OK */
13344 }
13345
13346 if (l == NULL) /* only one string argument */
13347 break;
13348 ++lnum;
13349 }
13350
13351 if (added > 0)
13352 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353}
13354
13355/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013356 * "setqflist()" function
13357 */
13358/*ARGSUSED*/
13359 static void
13360f_setqflist(argvars, rettv)
13361 typval_T *argvars;
13362 typval_T *rettv;
13363{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013364 char_u *act;
13365 int action = ' ';
13366
Bram Moolenaar2641f772005-03-25 21:58:17 +000013367 rettv->vval.v_number = -1;
13368
13369#ifdef FEAT_QUICKFIX
13370 if (argvars[0].v_type != VAR_LIST)
13371 EMSG(_(e_listreq));
13372 else
13373 {
13374 list_T *l = argvars[0].vval.v_list;
13375
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013376 if (argvars[1].v_type == VAR_STRING)
13377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013378 act = get_tv_string_chk(&argvars[1]);
13379 if (act == NULL)
13380 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013381 if (*act == 'a' || *act == 'r')
13382 action = *act;
13383 }
13384
13385 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013386 rettv->vval.v_number = 0;
13387 }
13388#endif
13389}
13390
13391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392 * "setreg()" function
13393 */
13394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013395f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013396 typval_T *argvars;
13397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398{
13399 int regname;
13400 char_u *strregname;
13401 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013402 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 int append;
13404 char_u yank_type;
13405 long block_len;
13406
13407 block_len = -1;
13408 yank_type = MAUTO;
13409 append = FALSE;
13410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013411 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013412 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013414 if (strregname == NULL)
13415 return; /* type error; errmsg already given */
13416 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 if (regname == 0 || regname == '@')
13418 regname = '"';
13419 else if (regname == '=')
13420 return;
13421
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013422 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013424 stropt = get_tv_string_chk(&argvars[2]);
13425 if (stropt == NULL)
13426 return; /* type error */
13427 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428 switch (*stropt)
13429 {
13430 case 'a': case 'A': /* append */
13431 append = TRUE;
13432 break;
13433 case 'v': case 'c': /* character-wise selection */
13434 yank_type = MCHAR;
13435 break;
13436 case 'V': case 'l': /* line-wise selection */
13437 yank_type = MLINE;
13438 break;
13439#ifdef FEAT_VISUAL
13440 case 'b': case Ctrl_V: /* block-wise selection */
13441 yank_type = MBLOCK;
13442 if (VIM_ISDIGIT(stropt[1]))
13443 {
13444 ++stropt;
13445 block_len = getdigits(&stropt) - 1;
13446 --stropt;
13447 }
13448 break;
13449#endif
13450 }
13451 }
13452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013453 strval = get_tv_string_chk(&argvars[1]);
13454 if (strval != NULL)
13455 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013457 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458}
13459
13460
13461/*
13462 * "setwinvar(expr)" function
13463 */
13464/*ARGSUSED*/
13465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013466f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013467 typval_T *argvars;
13468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469{
13470 win_T *win;
13471#ifdef FEAT_WINDOWS
13472 win_T *save_curwin;
13473#endif
13474 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013475 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476 char_u nbuf[NUMBUFLEN];
13477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013478 rettv->vval.v_number = 0;
13479
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480 if (check_restricted() || check_secure())
13481 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013483 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484 varp = &argvars[2];
13485
13486 if (win != NULL && varname != NULL && varp != NULL)
13487 {
13488#ifdef FEAT_WINDOWS
13489 /* set curwin to be our win, temporarily */
13490 save_curwin = curwin;
13491 curwin = win;
13492 curbuf = curwin->w_buffer;
13493#endif
13494
13495 if (*varname == '&')
13496 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013497 long numval;
13498 char_u *strval;
13499 int error = FALSE;
13500
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013502 numval = get_tv_number_chk(varp, &error);
13503 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013504 if (!error && strval != NULL)
13505 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506 }
13507 else
13508 {
13509 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13510 if (winvarname != NULL)
13511 {
13512 STRCPY(winvarname, "w:");
13513 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013514 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 vim_free(winvarname);
13516 }
13517 }
13518
13519#ifdef FEAT_WINDOWS
13520 /* Restore current window, if it's still valid (autocomands can make
13521 * it invalid). */
13522 if (win_valid(save_curwin))
13523 {
13524 curwin = save_curwin;
13525 curbuf = curwin->w_buffer;
13526 }
13527#endif
13528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529}
13530
13531/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013532 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533 */
13534 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013535f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013536 typval_T *argvars;
13537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013539 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540
Bram Moolenaar0d660222005-01-07 21:51:51 +000013541 p = get_tv_string(&argvars[0]);
13542 rettv->vval.v_string = vim_strsave(p);
13543 simplify_filename(rettv->vval.v_string); /* simplify in place */
13544 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545}
13546
Bram Moolenaar0d660222005-01-07 21:51:51 +000013547static int
13548#ifdef __BORLANDC__
13549 _RTLENTRYF
13550#endif
13551 item_compare __ARGS((const void *s1, const void *s2));
13552static int
13553#ifdef __BORLANDC__
13554 _RTLENTRYF
13555#endif
13556 item_compare2 __ARGS((const void *s1, const void *s2));
13557
13558static int item_compare_ic;
13559static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013560static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013561#define ITEM_COMPARE_FAIL 999
13562
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013564 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013566 static int
13567#ifdef __BORLANDC__
13568_RTLENTRYF
13569#endif
13570item_compare(s1, s2)
13571 const void *s1;
13572 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013574 char_u *p1, *p2;
13575 char_u *tofree1, *tofree2;
13576 int res;
13577 char_u numbuf1[NUMBUFLEN];
13578 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579
Bram Moolenaar33570922005-01-25 22:26:29 +000013580 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13581 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013582 if (item_compare_ic)
13583 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013585 res = STRCMP(p1, p2);
13586 vim_free(tofree1);
13587 vim_free(tofree2);
13588 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589}
13590
13591 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013592#ifdef __BORLANDC__
13593_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013595item_compare2(s1, s2)
13596 const void *s1;
13597 const void *s2;
13598{
13599 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013600 typval_T rettv;
13601 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013602 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013604 /* shortcut after failure in previous call; compare all items equal */
13605 if (item_compare_func_err)
13606 return 0;
13607
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013608 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13609 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013610 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13611 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013612
13613 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13614 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013615 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013616 clear_tv(&argv[0]);
13617 clear_tv(&argv[1]);
13618
13619 if (res == FAIL)
13620 res = ITEM_COMPARE_FAIL;
13621 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013622 /* return value has wrong type */
13623 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13624 if (item_compare_func_err)
13625 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013626 clear_tv(&rettv);
13627 return res;
13628}
13629
13630/*
13631 * "sort({list})" function
13632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013634f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013635 typval_T *argvars;
13636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637{
Bram Moolenaar33570922005-01-25 22:26:29 +000013638 list_T *l;
13639 listitem_T *li;
13640 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013641 long len;
13642 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643
Bram Moolenaar0d660222005-01-07 21:51:51 +000013644 rettv->vval.v_number = 0;
13645 if (argvars[0].v_type != VAR_LIST)
13646 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 else
13648 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013649 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013650 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013651 return;
13652 rettv->vval.v_list = l;
13653 rettv->v_type = VAR_LIST;
13654 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655
Bram Moolenaar0d660222005-01-07 21:51:51 +000013656 len = list_len(l);
13657 if (len <= 1)
13658 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659
Bram Moolenaar0d660222005-01-07 21:51:51 +000013660 item_compare_ic = FALSE;
13661 item_compare_func = NULL;
13662 if (argvars[1].v_type != VAR_UNKNOWN)
13663 {
13664 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013665 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013666 else
13667 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013668 int error = FALSE;
13669
13670 i = get_tv_number_chk(&argvars[1], &error);
13671 if (error)
13672 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013673 if (i == 1)
13674 item_compare_ic = TRUE;
13675 else
13676 item_compare_func = get_tv_string(&argvars[1]);
13677 }
13678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679
Bram Moolenaar0d660222005-01-07 21:51:51 +000013680 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013681 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013682 if (ptrs == NULL)
13683 return;
13684 i = 0;
13685 for (li = l->lv_first; li != NULL; li = li->li_next)
13686 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013688 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013689 /* test the compare function */
13690 if (item_compare_func != NULL
13691 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13692 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013693 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013695 {
13696 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013697 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013698 item_compare_func == NULL ? item_compare : item_compare2);
13699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013700 if (!item_compare_func_err)
13701 {
13702 /* Clear the List and append the items in the sorted order. */
13703 l->lv_first = l->lv_last = NULL;
13704 l->lv_len = 0;
13705 for (i = 0; i < len; ++i)
13706 list_append(l, ptrs[i]);
13707 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013708 }
13709
13710 vim_free(ptrs);
13711 }
13712}
13713
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013714/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013715 * "soundfold({word})" function
13716 */
13717 static void
13718f_soundfold(argvars, rettv)
13719 typval_T *argvars;
13720 typval_T *rettv;
13721{
13722 char_u *s;
13723
13724 rettv->v_type = VAR_STRING;
13725 s = get_tv_string(&argvars[0]);
13726#ifdef FEAT_SYN_HL
13727 rettv->vval.v_string = eval_soundfold(s);
13728#else
13729 rettv->vval.v_string = vim_strsave(s);
13730#endif
13731}
13732
13733/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013734 * "spellbadword()" function
13735 */
13736/* ARGSUSED */
13737 static void
13738f_spellbadword(argvars, rettv)
13739 typval_T *argvars;
13740 typval_T *rettv;
13741{
13742 int attr;
13743 char_u *ptr;
13744 int len;
13745
13746 rettv->vval.v_string = NULL;
13747 rettv->v_type = VAR_STRING;
13748
13749#ifdef FEAT_SYN_HL
13750 /* Find the start of the badly spelled word. */
13751 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
13752 return;
13753
13754 /* Get the length of the word and copy it. */
13755 ptr = ml_get_cursor();
Bram Moolenaar51ac12f2005-07-02 23:21:11 +000013756 len = spell_check(curwin, ptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013757 rettv->vval.v_string = vim_strnsave(ptr, len);
13758#endif
13759}
13760
13761/*
13762 * "spellsuggest()" function
13763 */
13764 static void
13765f_spellsuggest(argvars, rettv)
13766 typval_T *argvars;
13767 typval_T *rettv;
13768{
13769 char_u *str;
13770 int maxcount;
13771 garray_T ga;
13772 list_T *l;
13773 listitem_T *li;
13774 int i;
13775
13776 l = list_alloc();
13777 if (l == NULL)
13778 return;
13779 rettv->v_type = VAR_LIST;
13780 rettv->vval.v_list = l;
13781 ++l->lv_refcount;
13782
13783#ifdef FEAT_SYN_HL
13784 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13785 {
13786 str = get_tv_string(&argvars[0]);
13787 if (argvars[1].v_type != VAR_UNKNOWN)
13788 {
13789 maxcount = get_tv_number(&argvars[1]);
13790 if (maxcount <= 0)
13791 return;
13792 }
13793 else
13794 maxcount = 25;
13795
Bram Moolenaar8c45cdf2005-08-11 20:11:38 +000013796 spell_suggest_list(&ga, str, maxcount, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013797
13798 for (i = 0; i < ga.ga_len; ++i)
13799 {
13800 str = ((char_u **)ga.ga_data)[i];
13801
13802 li = listitem_alloc();
13803 if (li == NULL)
13804 vim_free(str);
13805 else
13806 {
13807 li->li_tv.v_type = VAR_STRING;
13808 li->li_tv.v_lock = 0;
13809 li->li_tv.vval.v_string = str;
13810 list_append(l, li);
13811 }
13812 }
13813 ga_clear(&ga);
13814 }
13815#endif
13816}
13817
Bram Moolenaar0d660222005-01-07 21:51:51 +000013818 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013819f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013820 typval_T *argvars;
13821 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013822{
13823 char_u *str;
13824 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013825 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013826 regmatch_T regmatch;
13827 char_u patbuf[NUMBUFLEN];
13828 char_u *save_cpo;
13829 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013830 listitem_T *ni;
13831 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013832 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013833 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013834 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013835
13836 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13837 save_cpo = p_cpo;
13838 p_cpo = (char_u *)"";
13839
13840 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013841 if (argvars[1].v_type != VAR_UNKNOWN)
13842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013843 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13844 if (pat == NULL)
13845 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013846 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013847 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013848 }
13849 if (pat == NULL || *pat == NUL)
13850 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013851
13852 l = list_alloc();
13853 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013855 rettv->v_type = VAR_LIST;
13856 rettv->vval.v_list = l;
13857 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013858 if (typeerr)
13859 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860
Bram Moolenaar0d660222005-01-07 21:51:51 +000013861 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13862 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013864 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013865 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013866 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013867 if (*str == NUL)
13868 match = FALSE; /* empty item at the end */
13869 else
13870 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013871 if (match)
13872 end = regmatch.startp[0];
13873 else
13874 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013875 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13876 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013877 {
13878 ni = listitem_alloc();
13879 if (ni == NULL)
13880 break;
13881 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013882 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013883 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13884 list_append(l, ni);
13885 }
13886 if (!match)
13887 break;
13888 /* Advance to just after the match. */
13889 if (regmatch.endp[0] > str)
13890 col = 0;
13891 else
13892 {
13893 /* Don't get stuck at the same match. */
13894#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013895 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013896#else
13897 col = 1;
13898#endif
13899 }
13900 str = regmatch.endp[0];
13901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902
Bram Moolenaar0d660222005-01-07 21:51:51 +000013903 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905
Bram Moolenaar0d660222005-01-07 21:51:51 +000013906 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907}
13908
13909#ifdef HAVE_STRFTIME
13910/*
13911 * "strftime({format}[, {time}])" function
13912 */
13913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013914f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013915 typval_T *argvars;
13916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917{
13918 char_u result_buf[256];
13919 struct tm *curtime;
13920 time_t seconds;
13921 char_u *p;
13922
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013923 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013925 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013926 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927 seconds = time(NULL);
13928 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013929 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930 curtime = localtime(&seconds);
13931 /* MSVC returns NULL for an invalid value of seconds. */
13932 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013933 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934 else
13935 {
13936# ifdef FEAT_MBYTE
13937 vimconv_T conv;
13938 char_u *enc;
13939
13940 conv.vc_type = CONV_NONE;
13941 enc = enc_locale();
13942 convert_setup(&conv, p_enc, enc);
13943 if (conv.vc_type != CONV_NONE)
13944 p = string_convert(&conv, p, NULL);
13945# endif
13946 if (p != NULL)
13947 (void)strftime((char *)result_buf, sizeof(result_buf),
13948 (char *)p, curtime);
13949 else
13950 result_buf[0] = NUL;
13951
13952# ifdef FEAT_MBYTE
13953 if (conv.vc_type != CONV_NONE)
13954 vim_free(p);
13955 convert_setup(&conv, enc, p_enc);
13956 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013957 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 else
13959# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013960 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961
13962# ifdef FEAT_MBYTE
13963 /* Release conversion descriptors */
13964 convert_setup(&conv, NULL, NULL);
13965 vim_free(enc);
13966# endif
13967 }
13968}
13969#endif
13970
13971/*
13972 * "stridx()" function
13973 */
13974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013975f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013976 typval_T *argvars;
13977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978{
13979 char_u buf[NUMBUFLEN];
13980 char_u *needle;
13981 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000013982 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000013984 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013986 needle = get_tv_string_chk(&argvars[1]);
13987 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000013988 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013989 if (needle == NULL || haystack == NULL)
13990 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991
Bram Moolenaar33570922005-01-25 22:26:29 +000013992 if (argvars[2].v_type != VAR_UNKNOWN)
13993 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013994 int error = FALSE;
13995
13996 start_idx = get_tv_number_chk(&argvars[2], &error);
13997 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000013998 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013999 if (start_idx >= 0)
14000 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014001 }
14002
14003 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14004 if (pos != NULL)
14005 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006}
14007
14008/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014009 * "string()" function
14010 */
14011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014012f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014013 typval_T *argvars;
14014 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014015{
14016 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014017 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014019 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014020 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014021 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014022 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023}
14024
14025/*
14026 * "strlen()" function
14027 */
14028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014029f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014030 typval_T *argvars;
14031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014033 rettv->vval.v_number = (varnumber_T)(STRLEN(
14034 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035}
14036
14037/*
14038 * "strpart()" function
14039 */
14040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014041f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014042 typval_T *argvars;
14043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044{
14045 char_u *p;
14046 int n;
14047 int len;
14048 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014049 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014051 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052 slen = (int)STRLEN(p);
14053
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014054 n = get_tv_number_chk(&argvars[1], &error);
14055 if (error)
14056 len = 0;
14057 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014058 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059 else
14060 len = slen - n; /* default len: all bytes that are available. */
14061
14062 /*
14063 * Only return the overlap between the specified part and the actual
14064 * string.
14065 */
14066 if (n < 0)
14067 {
14068 len += n;
14069 n = 0;
14070 }
14071 else if (n > slen)
14072 n = slen;
14073 if (len < 0)
14074 len = 0;
14075 else if (n + len > slen)
14076 len = slen - n;
14077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014078 rettv->v_type = VAR_STRING;
14079 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080}
14081
14082/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014083 * "strridx()" function
14084 */
14085 static void
14086f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014087 typval_T *argvars;
14088 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014089{
14090 char_u buf[NUMBUFLEN];
14091 char_u *needle;
14092 char_u *haystack;
14093 char_u *rest;
14094 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014095 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014097 needle = get_tv_string_chk(&argvars[1]);
14098 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014099 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014100
14101 rettv->vval.v_number = -1;
14102 if (needle == NULL || haystack == NULL)
14103 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014104 if (argvars[2].v_type != VAR_UNKNOWN)
14105 {
14106 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014107 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014108 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014109 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014110 }
14111 else
14112 end_idx = haystack_len;
14113
Bram Moolenaar0d660222005-01-07 21:51:51 +000014114 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014115 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014116 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014117 lastmatch = haystack + end_idx;
14118 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014119 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014120 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014121 for (rest = haystack; *rest != '\0'; ++rest)
14122 {
14123 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014124 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014125 break;
14126 lastmatch = rest;
14127 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014128 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014129
14130 if (lastmatch == NULL)
14131 rettv->vval.v_number = -1;
14132 else
14133 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14134}
14135
14136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137 * "strtrans()" function
14138 */
14139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014140f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014141 typval_T *argvars;
14142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014144 rettv->v_type = VAR_STRING;
14145 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146}
14147
14148/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014149 * "submatch()" function
14150 */
14151 static void
14152f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014153 typval_T *argvars;
14154 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014155{
14156 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014157 rettv->vval.v_string =
14158 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014159}
14160
14161/*
14162 * "substitute()" function
14163 */
14164 static void
14165f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014166 typval_T *argvars;
14167 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014168{
14169 char_u patbuf[NUMBUFLEN];
14170 char_u subbuf[NUMBUFLEN];
14171 char_u flagsbuf[NUMBUFLEN];
14172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014173 char_u *str = get_tv_string_chk(&argvars[0]);
14174 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14175 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14176 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14177
Bram Moolenaar0d660222005-01-07 21:51:51 +000014178 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014179 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14180 rettv->vval.v_string = NULL;
14181 else
14182 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014183}
14184
14185/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014186 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187 */
14188/*ARGSUSED*/
14189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014190f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014191 typval_T *argvars;
14192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193{
14194 int id = 0;
14195#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014196 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197 long col;
14198 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014199 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014201 lnum = get_tv_lnum(argvars); /* -1 on type error */
14202 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14203 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014204
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014205 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014206 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
14207 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208#endif
14209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014210 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211}
14212
14213/*
14214 * "synIDattr(id, what [, mode])" function
14215 */
14216/*ARGSUSED*/
14217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014218f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014219 typval_T *argvars;
14220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221{
14222 char_u *p = NULL;
14223#ifdef FEAT_SYN_HL
14224 int id;
14225 char_u *what;
14226 char_u *mode;
14227 char_u modebuf[NUMBUFLEN];
14228 int modec;
14229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014230 id = get_tv_number(&argvars[0]);
14231 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014232 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014234 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235 modec = TOLOWER_ASC(mode[0]);
14236 if (modec != 't' && modec != 'c'
14237#ifdef FEAT_GUI
14238 && modec != 'g'
14239#endif
14240 )
14241 modec = 0; /* replace invalid with current */
14242 }
14243 else
14244 {
14245#ifdef FEAT_GUI
14246 if (gui.in_use)
14247 modec = 'g';
14248 else
14249#endif
14250 if (t_colors > 1)
14251 modec = 'c';
14252 else
14253 modec = 't';
14254 }
14255
14256
14257 switch (TOLOWER_ASC(what[0]))
14258 {
14259 case 'b':
14260 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14261 p = highlight_color(id, what, modec);
14262 else /* bold */
14263 p = highlight_has_attr(id, HL_BOLD, modec);
14264 break;
14265
14266 case 'f': /* fg[#] */
14267 p = highlight_color(id, what, modec);
14268 break;
14269
14270 case 'i':
14271 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14272 p = highlight_has_attr(id, HL_INVERSE, modec);
14273 else /* italic */
14274 p = highlight_has_attr(id, HL_ITALIC, modec);
14275 break;
14276
14277 case 'n': /* name */
14278 p = get_highlight_name(NULL, id - 1);
14279 break;
14280
14281 case 'r': /* reverse */
14282 p = highlight_has_attr(id, HL_INVERSE, modec);
14283 break;
14284
14285 case 's': /* standout */
14286 p = highlight_has_attr(id, HL_STANDOUT, modec);
14287 break;
14288
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014289 case 'u':
14290 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14291 /* underline */
14292 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14293 else
14294 /* undercurl */
14295 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296 break;
14297 }
14298
14299 if (p != NULL)
14300 p = vim_strsave(p);
14301#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014302 rettv->v_type = VAR_STRING;
14303 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304}
14305
14306/*
14307 * "synIDtrans(id)" function
14308 */
14309/*ARGSUSED*/
14310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014311f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014312 typval_T *argvars;
14313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314{
14315 int id;
14316
14317#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014318 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319
14320 if (id > 0)
14321 id = syn_get_final_id(id);
14322 else
14323#endif
14324 id = 0;
14325
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014326 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327}
14328
14329/*
14330 * "system()" function
14331 */
14332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014333f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014334 typval_T *argvars;
14335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014337 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014339 char_u *infile = NULL;
14340 char_u buf[NUMBUFLEN];
14341 int err = FALSE;
14342 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014344 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014345 {
14346 /*
14347 * Write the string to a temp file, to be used for input of the shell
14348 * command.
14349 */
14350 if ((infile = vim_tempname('i')) == NULL)
14351 {
14352 EMSG(_(e_notmp));
14353 return;
14354 }
14355
14356 fd = mch_fopen((char *)infile, WRITEBIN);
14357 if (fd == NULL)
14358 {
14359 EMSG2(_(e_notopen), infile);
14360 goto done;
14361 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014362 p = get_tv_string_buf_chk(&argvars[1], buf);
14363 if (p == NULL)
14364 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014365 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14366 err = TRUE;
14367 if (fclose(fd) != 0)
14368 err = TRUE;
14369 if (err)
14370 {
14371 EMSG(_("E677: Error writing temp file"));
14372 goto done;
14373 }
14374 }
14375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014376 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014377
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378#ifdef USE_CR
14379 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014380 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381 {
14382 char_u *s;
14383
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014384 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385 {
14386 if (*s == CAR)
14387 *s = NL;
14388 }
14389 }
14390#else
14391# ifdef USE_CRNL
14392 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014393 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 {
14395 char_u *s, *d;
14396
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014397 d = res;
14398 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399 {
14400 if (s[0] == CAR && s[1] == NL)
14401 ++s;
14402 *d++ = *s;
14403 }
14404 *d = NUL;
14405 }
14406# endif
14407#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014408
14409done:
14410 if (infile != NULL)
14411 {
14412 mch_remove(infile);
14413 vim_free(infile);
14414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014415 rettv->v_type = VAR_STRING;
14416 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417}
14418
14419/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014420 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014421 */
14422 static void
14423f_taglist(argvars, rettv)
14424 typval_T *argvars;
14425 typval_T *rettv;
14426{
14427 char_u *tag_pattern;
14428 list_T *l;
14429
14430 tag_pattern = get_tv_string(&argvars[0]);
14431
14432 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014433 if (*tag_pattern == NUL)
14434 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014435
14436 l = list_alloc();
14437 if (l != NULL)
14438 {
14439 if (get_tags(l, tag_pattern) != FAIL)
14440 {
14441 rettv->vval.v_list = l;
14442 rettv->v_type = VAR_LIST;
14443 ++l->lv_refcount;
14444 }
14445 else
14446 list_free(l);
14447 }
14448}
14449
14450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 * "tempname()" function
14452 */
14453/*ARGSUSED*/
14454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014455f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014456 typval_T *argvars;
14457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458{
14459 static int x = 'A';
14460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461 rettv->v_type = VAR_STRING;
14462 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463
14464 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14465 * names. Skip 'I' and 'O', they are used for shell redirection. */
14466 do
14467 {
14468 if (x == 'Z')
14469 x = '0';
14470 else if (x == '9')
14471 x = 'A';
14472 else
14473 {
14474#ifdef EBCDIC
14475 if (x == 'I')
14476 x = 'J';
14477 else if (x == 'R')
14478 x = 'S';
14479 else
14480#endif
14481 ++x;
14482 }
14483 } while (x == 'I' || x == 'O');
14484}
14485
14486/*
14487 * "tolower(string)" function
14488 */
14489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014490f_tolower(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 char_u *p;
14495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014496 p = vim_strsave(get_tv_string(&argvars[0]));
14497 rettv->v_type = VAR_STRING;
14498 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499
14500 if (p != NULL)
14501 while (*p != NUL)
14502 {
14503#ifdef FEAT_MBYTE
14504 int l;
14505
14506 if (enc_utf8)
14507 {
14508 int c, lc;
14509
14510 c = utf_ptr2char(p);
14511 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014512 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 /* TODO: reallocate string when byte count changes. */
14514 if (utf_char2len(lc) == l)
14515 utf_char2bytes(lc, p);
14516 p += l;
14517 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014518 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519 p += l; /* skip multi-byte character */
14520 else
14521#endif
14522 {
14523 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14524 ++p;
14525 }
14526 }
14527}
14528
14529/*
14530 * "toupper(string)" function
14531 */
14532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014533f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014534 typval_T *argvars;
14535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014537 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014538 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539}
14540
14541/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014542 * "tr(string, fromstr, tostr)" function
14543 */
14544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014545f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014546 typval_T *argvars;
14547 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014548{
14549 char_u *instr;
14550 char_u *fromstr;
14551 char_u *tostr;
14552 char_u *p;
14553#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014554 int inlen;
14555 int fromlen;
14556 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014557 int idx;
14558 char_u *cpstr;
14559 int cplen;
14560 int first = TRUE;
14561#endif
14562 char_u buf[NUMBUFLEN];
14563 char_u buf2[NUMBUFLEN];
14564 garray_T ga;
14565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014566 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014567 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14568 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014569
14570 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014571 rettv->v_type = VAR_STRING;
14572 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014573 if (fromstr == NULL || tostr == NULL)
14574 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014575 ga_init2(&ga, (int)sizeof(char), 80);
14576
14577#ifdef FEAT_MBYTE
14578 if (!has_mbyte)
14579#endif
14580 /* not multi-byte: fromstr and tostr must be the same length */
14581 if (STRLEN(fromstr) != STRLEN(tostr))
14582 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014583#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014584error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014585#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014586 EMSG2(_(e_invarg2), fromstr);
14587 ga_clear(&ga);
14588 return;
14589 }
14590
14591 /* fromstr and tostr have to contain the same number of chars */
14592 while (*instr != NUL)
14593 {
14594#ifdef FEAT_MBYTE
14595 if (has_mbyte)
14596 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014597 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014598 cpstr = instr;
14599 cplen = inlen;
14600 idx = 0;
14601 for (p = fromstr; *p != NUL; p += fromlen)
14602 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014603 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014604 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14605 {
14606 for (p = tostr; *p != NUL; p += tolen)
14607 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014608 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014609 if (idx-- == 0)
14610 {
14611 cplen = tolen;
14612 cpstr = p;
14613 break;
14614 }
14615 }
14616 if (*p == NUL) /* tostr is shorter than fromstr */
14617 goto error;
14618 break;
14619 }
14620 ++idx;
14621 }
14622
14623 if (first && cpstr == instr)
14624 {
14625 /* Check that fromstr and tostr have the same number of
14626 * (multi-byte) characters. Done only once when a character
14627 * of instr doesn't appear in fromstr. */
14628 first = FALSE;
14629 for (p = tostr; *p != NUL; p += tolen)
14630 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014631 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014632 --idx;
14633 }
14634 if (idx != 0)
14635 goto error;
14636 }
14637
14638 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014639 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014640 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014641
14642 instr += inlen;
14643 }
14644 else
14645#endif
14646 {
14647 /* When not using multi-byte chars we can do it faster. */
14648 p = vim_strchr(fromstr, *instr);
14649 if (p != NULL)
14650 ga_append(&ga, tostr[p - fromstr]);
14651 else
14652 ga_append(&ga, *instr);
14653 ++instr;
14654 }
14655 }
14656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014657 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014658}
14659
14660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661 * "type(expr)" function
14662 */
14663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014664f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014665 typval_T *argvars;
14666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014668 int n;
14669
14670 switch (argvars[0].v_type)
14671 {
14672 case VAR_NUMBER: n = 0; break;
14673 case VAR_STRING: n = 1; break;
14674 case VAR_FUNC: n = 2; break;
14675 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014676 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014677 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14678 }
14679 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680}
14681
14682/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014683 * "values(dict)" function
14684 */
14685 static void
14686f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014687 typval_T *argvars;
14688 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014689{
14690 dict_list(argvars, rettv, 1);
14691}
14692
14693/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014694 * "virtcol(string)" function
14695 */
14696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014697f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014698 typval_T *argvars;
14699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700{
14701 colnr_T vcol = 0;
14702 pos_T *fp;
14703
14704 fp = var2fpos(&argvars[0], FALSE);
14705 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14706 {
14707 getvvcol(curwin, fp, NULL, NULL, &vcol);
14708 ++vcol;
14709 }
14710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014711 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712}
14713
14714/*
14715 * "visualmode()" function
14716 */
14717/*ARGSUSED*/
14718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014719f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014720 typval_T *argvars;
14721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722{
14723#ifdef FEAT_VISUAL
14724 char_u str[2];
14725
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014726 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727 str[0] = curbuf->b_visual_mode_eval;
14728 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014729 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730
14731 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014732 if ((argvars[0].v_type == VAR_NUMBER
14733 && argvars[0].vval.v_number != 0)
14734 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014735 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 curbuf->b_visual_mode_eval = NUL;
14737#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014738 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739#endif
14740}
14741
14742/*
14743 * "winbufnr(nr)" function
14744 */
14745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014746f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014747 typval_T *argvars;
14748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749{
14750 win_T *wp;
14751
14752 wp = find_win_by_nr(&argvars[0]);
14753 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014754 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014756 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757}
14758
14759/*
14760 * "wincol()" function
14761 */
14762/*ARGSUSED*/
14763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014764f_wincol(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 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014769 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770}
14771
14772/*
14773 * "winheight(nr)" function
14774 */
14775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014776f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014777 typval_T *argvars;
14778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779{
14780 win_T *wp;
14781
14782 wp = find_win_by_nr(&argvars[0]);
14783 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014784 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014786 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787}
14788
14789/*
14790 * "winline()" function
14791 */
14792/*ARGSUSED*/
14793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014794f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014795 typval_T *argvars;
14796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014797{
14798 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014799 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014800}
14801
14802/*
14803 * "winnr()" function
14804 */
14805/* ARGSUSED */
14806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014807f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014808 typval_T *argvars;
14809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810{
14811 int nr = 1;
14812#ifdef FEAT_WINDOWS
14813 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014814 win_T *twin = curwin;
14815 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014817 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014819 arg = get_tv_string_chk(&argvars[0]);
14820 if (arg == NULL)
14821 nr = 0; /* type error; errmsg already given */
14822 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014823 twin = lastwin;
14824 else if (STRCMP(arg, "#") == 0)
14825 {
14826 twin = prevwin;
14827 if (prevwin == NULL)
14828 nr = 0;
14829 }
14830 else
14831 {
14832 EMSG2(_(e_invexpr2), arg);
14833 nr = 0;
14834 }
14835 }
14836
14837 if (nr > 0)
14838 for (wp = firstwin; wp != twin; wp = wp->w_next)
14839 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014841 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842}
14843
14844/*
14845 * "winrestcmd()" function
14846 */
14847/* ARGSUSED */
14848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014849f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014850 typval_T *argvars;
14851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852{
14853#ifdef FEAT_WINDOWS
14854 win_T *wp;
14855 int winnr = 1;
14856 garray_T ga;
14857 char_u buf[50];
14858
14859 ga_init2(&ga, (int)sizeof(char), 70);
14860 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14861 {
14862 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14863 ga_concat(&ga, buf);
14864# ifdef FEAT_VERTSPLIT
14865 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14866 ga_concat(&ga, buf);
14867# endif
14868 ++winnr;
14869 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014870 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014872 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014874 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014875#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014876 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014877}
14878
14879/*
14880 * "winwidth(nr)" function
14881 */
14882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014883f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014884 typval_T *argvars;
14885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014886{
14887 win_T *wp;
14888
14889 wp = find_win_by_nr(&argvars[0]);
14890 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014891 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892 else
14893#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014894 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014896 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014897#endif
14898}
14899
Bram Moolenaar071d4272004-06-13 20:20:40 +000014900/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014901 * "writefile()" function
14902 */
14903 static void
14904f_writefile(argvars, rettv)
14905 typval_T *argvars;
14906 typval_T *rettv;
14907{
14908 int binary = FALSE;
14909 char_u *fname;
14910 FILE *fd;
14911 listitem_T *li;
14912 char_u *s;
14913 int ret = 0;
14914 int c;
14915
14916 if (argvars[0].v_type != VAR_LIST)
14917 {
14918 EMSG2(_(e_listarg), "writefile()");
14919 return;
14920 }
14921 if (argvars[0].vval.v_list == NULL)
14922 return;
14923
14924 if (argvars[2].v_type != VAR_UNKNOWN
14925 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
14926 binary = TRUE;
14927
14928 /* Always open the file in binary mode, library functions have a mind of
14929 * their own about CR-LF conversion. */
14930 fname = get_tv_string(&argvars[1]);
14931 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
14932 {
14933 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14934 ret = -1;
14935 }
14936 else
14937 {
14938 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
14939 li = li->li_next)
14940 {
14941 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
14942 {
14943 if (*s == '\n')
14944 c = putc(NUL, fd);
14945 else
14946 c = putc(*s, fd);
14947 if (c == EOF)
14948 {
14949 ret = -1;
14950 break;
14951 }
14952 }
14953 if (!binary || li->li_next != NULL)
14954 if (putc('\n', fd) == EOF)
14955 {
14956 ret = -1;
14957 break;
14958 }
14959 if (ret < 0)
14960 {
14961 EMSG(_(e_write));
14962 break;
14963 }
14964 }
14965 fclose(fd);
14966 }
14967
14968 rettv->vval.v_number = ret;
14969}
14970
14971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 * Translate a String variable into a position.
14973 */
14974 static pos_T *
14975var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000014976 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977 int lnum; /* TRUE when $ is last line */
14978{
14979 char_u *name;
14980 static pos_T pos;
14981 pos_T *pp;
14982
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014983 name = get_tv_string_chk(varp);
14984 if (name == NULL)
14985 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986 if (name[0] == '.') /* cursor */
14987 return &curwin->w_cursor;
14988 if (name[0] == '\'') /* mark */
14989 {
14990 pp = getmark(name[1], FALSE);
14991 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
14992 return NULL;
14993 return pp;
14994 }
14995 if (name[0] == '$') /* last column or line */
14996 {
14997 if (lnum)
14998 {
14999 pos.lnum = curbuf->b_ml.ml_line_count;
15000 pos.col = 0;
15001 }
15002 else
15003 {
15004 pos.lnum = curwin->w_cursor.lnum;
15005 pos.col = (colnr_T)STRLEN(ml_get_curline());
15006 }
15007 return &pos;
15008 }
15009 return NULL;
15010}
15011
15012/*
15013 * Get the length of an environment variable name.
15014 * Advance "arg" to the first character after the name.
15015 * Return 0 for error.
15016 */
15017 static int
15018get_env_len(arg)
15019 char_u **arg;
15020{
15021 char_u *p;
15022 int len;
15023
15024 for (p = *arg; vim_isIDc(*p); ++p)
15025 ;
15026 if (p == *arg) /* no name found */
15027 return 0;
15028
15029 len = (int)(p - *arg);
15030 *arg = p;
15031 return len;
15032}
15033
15034/*
15035 * Get the length of the name of a function or internal variable.
15036 * "arg" is advanced to the first non-white character after the name.
15037 * Return 0 if something is wrong.
15038 */
15039 static int
15040get_id_len(arg)
15041 char_u **arg;
15042{
15043 char_u *p;
15044 int len;
15045
15046 /* Find the end of the name. */
15047 for (p = *arg; eval_isnamec(*p); ++p)
15048 ;
15049 if (p == *arg) /* no name found */
15050 return 0;
15051
15052 len = (int)(p - *arg);
15053 *arg = skipwhite(p);
15054
15055 return len;
15056}
15057
15058/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015059 * Get the length of the name of a variable or function.
15060 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015062 * Return -1 if curly braces expansion failed.
15063 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064 * If the name contains 'magic' {}'s, expand them and return the
15065 * expanded name in an allocated string via 'alias' - caller must free.
15066 */
15067 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015068get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015069 char_u **arg;
15070 char_u **alias;
15071 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015072 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073{
15074 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075 char_u *p;
15076 char_u *expr_start;
15077 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015078
15079 *alias = NULL; /* default to no alias */
15080
15081 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15082 && (*arg)[2] == (int)KE_SNR)
15083 {
15084 /* hard coded <SNR>, already translated */
15085 *arg += 3;
15086 return get_id_len(arg) + 3;
15087 }
15088 len = eval_fname_script(*arg);
15089 if (len > 0)
15090 {
15091 /* literal "<SID>", "s:" or "<SNR>" */
15092 *arg += len;
15093 }
15094
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015096 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015098 p = find_name_end(*arg, &expr_start, &expr_end,
15099 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100 if (expr_start != NULL)
15101 {
15102 char_u *temp_string;
15103
15104 if (!evaluate)
15105 {
15106 len += (int)(p - *arg);
15107 *arg = skipwhite(p);
15108 return len;
15109 }
15110
15111 /*
15112 * Include any <SID> etc in the expanded string:
15113 * Thus the -len here.
15114 */
15115 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15116 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015117 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015118 *alias = temp_string;
15119 *arg = skipwhite(p);
15120 return (int)STRLEN(temp_string);
15121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122
15123 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015124 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125 EMSG2(_(e_invexpr2), *arg);
15126
15127 return len;
15128}
15129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015130/*
15131 * Find the end of a variable or function name, taking care of magic braces.
15132 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15133 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015134 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015135 * Return a pointer to just after the name. Equal to "arg" if there is no
15136 * valid name.
15137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015139find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140 char_u *arg;
15141 char_u **expr_start;
15142 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015143 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015145 int mb_nest = 0;
15146 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147 char_u *p;
15148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015149 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015151 *expr_start = NULL;
15152 *expr_end = NULL;
15153 }
15154
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015155 /* Quick check for valid starting character. */
15156 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15157 return arg;
15158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015159 for (p = arg; *p != NUL
15160 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015161 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015162 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015163 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015164 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015165 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015166 if (*p == '\'')
15167 {
15168 /* skip over 'string' to avoid counting [ and ] inside it. */
15169 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15170 ;
15171 if (*p == NUL)
15172 break;
15173 }
15174 else if (*p == '"')
15175 {
15176 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15177 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15178 if (*p == '\\' && p[1] != NUL)
15179 ++p;
15180 if (*p == NUL)
15181 break;
15182 }
15183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015184 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015186 if (*p == '[')
15187 ++br_nest;
15188 else if (*p == ']')
15189 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015192 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015194 if (*p == '{')
15195 {
15196 mb_nest++;
15197 if (expr_start != NULL && *expr_start == NULL)
15198 *expr_start = p;
15199 }
15200 else if (*p == '}')
15201 {
15202 mb_nest--;
15203 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15204 *expr_end = p;
15205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015207 }
15208
15209 return p;
15210}
15211
15212/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015213 * Expands out the 'magic' {}'s in a variable/function name.
15214 * Note that this can call itself recursively, to deal with
15215 * constructs like foo{bar}{baz}{bam}
15216 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15217 * "in_start" ^
15218 * "expr_start" ^
15219 * "expr_end" ^
15220 * "in_end" ^
15221 *
15222 * Returns a new allocated string, which the caller must free.
15223 * Returns NULL for failure.
15224 */
15225 static char_u *
15226make_expanded_name(in_start, expr_start, expr_end, in_end)
15227 char_u *in_start;
15228 char_u *expr_start;
15229 char_u *expr_end;
15230 char_u *in_end;
15231{
15232 char_u c1;
15233 char_u *retval = NULL;
15234 char_u *temp_result;
15235 char_u *nextcmd = NULL;
15236
15237 if (expr_end == NULL || in_end == NULL)
15238 return NULL;
15239 *expr_start = NUL;
15240 *expr_end = NUL;
15241 c1 = *in_end;
15242 *in_end = NUL;
15243
15244 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15245 if (temp_result != NULL && nextcmd == NULL)
15246 {
15247 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15248 + (in_end - expr_end) + 1));
15249 if (retval != NULL)
15250 {
15251 STRCPY(retval, in_start);
15252 STRCAT(retval, temp_result);
15253 STRCAT(retval, expr_end + 1);
15254 }
15255 }
15256 vim_free(temp_result);
15257
15258 *in_end = c1; /* put char back for error messages */
15259 *expr_start = '{';
15260 *expr_end = '}';
15261
15262 if (retval != NULL)
15263 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015264 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015265 if (expr_start != NULL)
15266 {
15267 /* Further expansion! */
15268 temp_result = make_expanded_name(retval, expr_start,
15269 expr_end, temp_result);
15270 vim_free(retval);
15271 retval = temp_result;
15272 }
15273 }
15274
15275 return retval;
15276}
15277
15278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015280 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281 */
15282 static int
15283eval_isnamec(c)
15284 int c;
15285{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015286 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15287}
15288
15289/*
15290 * Return TRUE if character "c" can be used as the first character in a
15291 * variable or function name (excluding '{' and '}').
15292 */
15293 static int
15294eval_isnamec1(c)
15295 int c;
15296{
15297 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298}
15299
15300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 * Set number v: variable to "val".
15302 */
15303 void
15304set_vim_var_nr(idx, val)
15305 int idx;
15306 long val;
15307{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015308 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309}
15310
15311/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015312 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015313 */
15314 long
15315get_vim_var_nr(idx)
15316 int idx;
15317{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015318 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319}
15320
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015321#if defined(FEAT_AUTOCMD) || defined(PROTO)
15322/*
15323 * Get string v: variable value. Uses a static buffer, can only be used once.
15324 */
15325 char_u *
15326get_vim_var_str(idx)
15327 int idx;
15328{
15329 return get_tv_string(&vimvars[idx].vv_tv);
15330}
15331#endif
15332
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333/*
15334 * Set v:count, v:count1 and v:prevcount.
15335 */
15336 void
15337set_vcount(count, count1)
15338 long count;
15339 long count1;
15340{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015341 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15342 vimvars[VV_COUNT].vv_nr = count;
15343 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344}
15345
15346/*
15347 * Set string v: variable to a copy of "val".
15348 */
15349 void
15350set_vim_var_string(idx, val, len)
15351 int idx;
15352 char_u *val;
15353 int len; /* length of "val" to use or -1 (whole string) */
15354{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015355 /* Need to do this (at least) once, since we can't initialize a union.
15356 * Will always be invoked when "v:progname" is set. */
15357 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15358
Bram Moolenaare9a41262005-01-15 22:18:47 +000015359 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015361 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015363 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015365 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366}
15367
15368/*
15369 * Set v:register if needed.
15370 */
15371 void
15372set_reg_var(c)
15373 int c;
15374{
15375 char_u regname;
15376
15377 if (c == 0 || c == ' ')
15378 regname = '"';
15379 else
15380 regname = c;
15381 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015382 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383 set_vim_var_string(VV_REG, &regname, 1);
15384}
15385
15386/*
15387 * Get or set v:exception. If "oldval" == NULL, return the current value.
15388 * Otherwise, restore the value to "oldval" and return NULL.
15389 * Must always be called in pairs to save and restore v:exception! Does not
15390 * take care of memory allocations.
15391 */
15392 char_u *
15393v_exception(oldval)
15394 char_u *oldval;
15395{
15396 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015397 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398
Bram Moolenaare9a41262005-01-15 22:18:47 +000015399 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400 return NULL;
15401}
15402
15403/*
15404 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15405 * Otherwise, restore the value to "oldval" and return NULL.
15406 * Must always be called in pairs to save and restore v:throwpoint! Does not
15407 * take care of memory allocations.
15408 */
15409 char_u *
15410v_throwpoint(oldval)
15411 char_u *oldval;
15412{
15413 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015414 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415
Bram Moolenaare9a41262005-01-15 22:18:47 +000015416 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417 return NULL;
15418}
15419
15420#if defined(FEAT_AUTOCMD) || defined(PROTO)
15421/*
15422 * Set v:cmdarg.
15423 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15424 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15425 * Must always be called in pairs!
15426 */
15427 char_u *
15428set_cmdarg(eap, oldarg)
15429 exarg_T *eap;
15430 char_u *oldarg;
15431{
15432 char_u *oldval;
15433 char_u *newval;
15434 unsigned len;
15435
Bram Moolenaare9a41262005-01-15 22:18:47 +000015436 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015437 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015439 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015440 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015441 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442 }
15443
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015444 if (eap->force_bin == FORCE_BIN)
15445 len = 6;
15446 else if (eap->force_bin == FORCE_NOBIN)
15447 len = 8;
15448 else
15449 len = 0;
15450 if (eap->force_ff != 0)
15451 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15452# ifdef FEAT_MBYTE
15453 if (eap->force_enc != 0)
15454 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15455# endif
15456
15457 newval = alloc(len + 1);
15458 if (newval == NULL)
15459 return NULL;
15460
15461 if (eap->force_bin == FORCE_BIN)
15462 sprintf((char *)newval, " ++bin");
15463 else if (eap->force_bin == FORCE_NOBIN)
15464 sprintf((char *)newval, " ++nobin");
15465 else
15466 *newval = NUL;
15467 if (eap->force_ff != 0)
15468 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15469 eap->cmd + eap->force_ff);
15470# ifdef FEAT_MBYTE
15471 if (eap->force_enc != 0)
15472 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15473 eap->cmd + eap->force_enc);
15474# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015475 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015476 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477}
15478#endif
15479
15480/*
15481 * Get the value of internal variable "name".
15482 * Return OK or FAIL.
15483 */
15484 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015485get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486 char_u *name;
15487 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015488 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015489 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490{
15491 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015492 typval_T *tv = NULL;
15493 typval_T atv;
15494 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496
15497 /* truncate the name, so that we can use strcmp() */
15498 cc = name[len];
15499 name[len] = NUL;
15500
15501 /*
15502 * Check for "b:changedtick".
15503 */
15504 if (STRCMP(name, "b:changedtick") == 0)
15505 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015506 atv.v_type = VAR_NUMBER;
15507 atv.vval.v_number = curbuf->b_changedtick;
15508 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509 }
15510
15511 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512 * Check for user-defined variables.
15513 */
15514 else
15515 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015516 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015518 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519 }
15520
Bram Moolenaare9a41262005-01-15 22:18:47 +000015521 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015523 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015524 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 ret = FAIL;
15526 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015527 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015528 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529
15530 name[len] = cc;
15531
15532 return ret;
15533}
15534
15535/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015536 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15537 * Also handle function call with Funcref variable: func(expr)
15538 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15539 */
15540 static int
15541handle_subscript(arg, rettv, evaluate, verbose)
15542 char_u **arg;
15543 typval_T *rettv;
15544 int evaluate; /* do more than finding the end */
15545 int verbose; /* give error messages */
15546{
15547 int ret = OK;
15548 dict_T *selfdict = NULL;
15549 char_u *s;
15550 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015551 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015552
15553 while (ret == OK
15554 && (**arg == '['
15555 || (**arg == '.' && rettv->v_type == VAR_DICT)
15556 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15557 && !vim_iswhite(*(*arg - 1)))
15558 {
15559 if (**arg == '(')
15560 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015561 /* need to copy the funcref so that we can clear rettv */
15562 functv = *rettv;
15563 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015564
15565 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015566 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015567 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015568 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15569 &len, evaluate, selfdict);
15570
15571 /* Clear the funcref afterwards, so that deleting it while
15572 * evaluating the arguments is possible (see test55). */
15573 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015574
15575 /* Stop the expression evaluation when immediately aborting on
15576 * error, or when an interrupt occurred or an exception was thrown
15577 * but not caught. */
15578 if (aborting())
15579 {
15580 if (ret == OK)
15581 clear_tv(rettv);
15582 ret = FAIL;
15583 }
15584 dict_unref(selfdict);
15585 selfdict = NULL;
15586 }
15587 else /* **arg == '[' || **arg == '.' */
15588 {
15589 dict_unref(selfdict);
15590 if (rettv->v_type == VAR_DICT)
15591 {
15592 selfdict = rettv->vval.v_dict;
15593 if (selfdict != NULL)
15594 ++selfdict->dv_refcount;
15595 }
15596 else
15597 selfdict = NULL;
15598 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15599 {
15600 clear_tv(rettv);
15601 ret = FAIL;
15602 }
15603 }
15604 }
15605 dict_unref(selfdict);
15606 return ret;
15607}
15608
15609/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015610 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15611 * value).
15612 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015613 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015614alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015615{
Bram Moolenaar33570922005-01-25 22:26:29 +000015616 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015617}
15618
15619/*
15620 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 * The string "s" must have been allocated, it is consumed.
15622 * Return NULL for out of memory, the variable otherwise.
15623 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015624 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015625alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626 char_u *s;
15627{
Bram Moolenaar33570922005-01-25 22:26:29 +000015628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015630 rettv = alloc_tv();
15631 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015633 rettv->v_type = VAR_STRING;
15634 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635 }
15636 else
15637 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015638 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639}
15640
15641/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015642 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643 */
15644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015645free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015646 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647{
15648 if (varp != NULL)
15649 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015650 switch (varp->v_type)
15651 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015652 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015653 func_unref(varp->vval.v_string);
15654 /*FALLTHROUGH*/
15655 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015656 vim_free(varp->vval.v_string);
15657 break;
15658 case VAR_LIST:
15659 list_unref(varp->vval.v_list);
15660 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015661 case VAR_DICT:
15662 dict_unref(varp->vval.v_dict);
15663 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015664 case VAR_NUMBER:
15665 case VAR_UNKNOWN:
15666 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015667 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015668 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015669 break;
15670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 vim_free(varp);
15672 }
15673}
15674
15675/*
15676 * Free the memory for a variable value and set the value to NULL or 0.
15677 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015678 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015679clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015680 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681{
15682 if (varp != NULL)
15683 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015684 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015686 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015687 func_unref(varp->vval.v_string);
15688 /*FALLTHROUGH*/
15689 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015690 vim_free(varp->vval.v_string);
15691 varp->vval.v_string = NULL;
15692 break;
15693 case VAR_LIST:
15694 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015695 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015696 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015697 case VAR_DICT:
15698 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015699 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015700 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015701 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015702 varp->vval.v_number = 0;
15703 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015704 case VAR_UNKNOWN:
15705 break;
15706 default:
15707 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015708 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015709 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710 }
15711}
15712
15713/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015714 * Set the value of a variable to NULL without freeing items.
15715 */
15716 static void
15717init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015718 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015719{
15720 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015721 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015722}
15723
15724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 * Get the number value of a variable.
15726 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015727 * For incompatible types, return 0.
15728 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15729 * caller of incompatible types: it sets *denote to TRUE if "denote"
15730 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731 */
15732 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015733get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015734 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015736 int error = FALSE;
15737
15738 return get_tv_number_chk(varp, &error); /* return 0L on error */
15739}
15740
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015741 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015742get_tv_number_chk(varp, denote)
15743 typval_T *varp;
15744 int *denote;
15745{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015746 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015748 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015750 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015751 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015752 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015753 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015754 break;
15755 case VAR_STRING:
15756 if (varp->vval.v_string != NULL)
15757 vim_str2nr(varp->vval.v_string, NULL, NULL,
15758 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015759 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015760 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015761 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015762 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015763 case VAR_DICT:
15764 EMSG(_("E728: Using a Dictionary as a number"));
15765 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015766 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015767 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015768 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015770 if (denote == NULL) /* useful for values that must be unsigned */
15771 n = -1;
15772 else
15773 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015774 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775}
15776
15777/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015778 * Get the lnum from the first argument.
15779 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015780 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781 */
15782 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015783get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015784 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785{
Bram Moolenaar33570922005-01-25 22:26:29 +000015786 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787 linenr_T lnum;
15788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015789 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790 if (lnum == 0) /* no valid number, try using line() */
15791 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015792 rettv.v_type = VAR_NUMBER;
15793 f_line(argvars, &rettv);
15794 lnum = rettv.vval.v_number;
15795 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796 }
15797 return lnum;
15798}
15799
15800/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015801 * Get the lnum from the first argument.
15802 * Also accepts "$", then "buf" is used.
15803 * Returns 0 on error.
15804 */
15805 static linenr_T
15806get_tv_lnum_buf(argvars, buf)
15807 typval_T *argvars;
15808 buf_T *buf;
15809{
15810 if (argvars[0].v_type == VAR_STRING
15811 && argvars[0].vval.v_string != NULL
15812 && argvars[0].vval.v_string[0] == '$'
15813 && buf != NULL)
15814 return buf->b_ml.ml_line_count;
15815 return get_tv_number_chk(&argvars[0], NULL);
15816}
15817
15818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819 * Get the string value of a variable.
15820 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015821 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15822 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823 * If the String variable has never been set, return an empty string.
15824 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015825 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15826 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827 */
15828 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015829get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015830 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831{
15832 static char_u mybuf[NUMBUFLEN];
15833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015834 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835}
15836
15837 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015838get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015839 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015840 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015842 char_u *res = get_tv_string_buf_chk(varp, buf);
15843
15844 return res != NULL ? res : (char_u *)"";
15845}
15846
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015847 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015848get_tv_string_chk(varp)
15849 typval_T *varp;
15850{
15851 static char_u mybuf[NUMBUFLEN];
15852
15853 return get_tv_string_buf_chk(varp, mybuf);
15854}
15855
15856 static char_u *
15857get_tv_string_buf_chk(varp, buf)
15858 typval_T *varp;
15859 char_u *buf;
15860{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015861 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015863 case VAR_NUMBER:
15864 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15865 return buf;
15866 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015867 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015868 break;
15869 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015870 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015871 break;
15872 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015873 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015874 break;
15875 case VAR_STRING:
15876 if (varp->vval.v_string != NULL)
15877 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015878 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015879 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015880 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015881 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015883 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884}
15885
15886/*
15887 * Find variable "name" in the list of variables.
15888 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015889 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015890 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015891 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015893 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015894find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015896 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015899 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900
Bram Moolenaara7043832005-01-21 11:56:39 +000015901 ht = find_var_ht(name, &varname);
15902 if (htp != NULL)
15903 *htp = ht;
15904 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015905 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015906 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907}
15908
15909/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015910 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000015911 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015912 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015913 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015914find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000015915 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000015916 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015917 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000015918{
Bram Moolenaar33570922005-01-25 22:26:29 +000015919 hashitem_T *hi;
15920
15921 if (*varname == NUL)
15922 {
15923 /* Must be something like "s:", otherwise "ht" would be NULL. */
15924 switch (varname[-2])
15925 {
15926 case 's': return &SCRIPT_SV(current_SID).sv_var;
15927 case 'g': return &globvars_var;
15928 case 'v': return &vimvars_var;
15929 case 'b': return &curbuf->b_bufvar;
15930 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015931 case 'l': return current_funccal == NULL
15932 ? NULL : &current_funccal->l_vars_var;
15933 case 'a': return current_funccal == NULL
15934 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000015935 }
15936 return NULL;
15937 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015938
15939 hi = hash_find(ht, varname);
15940 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015941 {
15942 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015943 * worked find the variable again. Don't auto-load a script if it was
15944 * loaded already, otherwise it would be loaded every time when
15945 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015946 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015947 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015948 hi = hash_find(ht, varname);
15949 if (HASHITEM_EMPTY(hi))
15950 return NULL;
15951 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015952 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015953}
15954
15955/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015956 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015957 * Set "varname" to the start of name without ':'.
15958 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015959 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015960find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015961 char_u *name;
15962 char_u **varname;
15963{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015964 hashitem_T *hi;
15965
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966 if (name[1] != ':')
15967 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015968 /* The name must not start with a colon or #. */
15969 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015970 return NULL;
15971 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015972
15973 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015974 hi = hash_find(&compat_hashtab, name);
15975 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000015976 return &compat_hashtab;
15977
Bram Moolenaar071d4272004-06-13 20:20:40 +000015978 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015979 return &globvarht; /* global variable */
15980 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015981 }
15982 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015983 if (*name == 'g') /* global variable */
15984 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015985 /* There must be no ':' or '#' in the rest of the name, unless g: is used
15986 */
15987 if (vim_strchr(name + 2, ':') != NULL
15988 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015989 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015991 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015993 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000015994 if (*name == 'v') /* v: variable */
15995 return &vimvarht;
15996 if (*name == 'a' && current_funccal != NULL) /* function argument */
15997 return &current_funccal->l_avars.dv_hashtab;
15998 if (*name == 'l' && current_funccal != NULL) /* local function variable */
15999 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000 if (*name == 's' /* script variable */
16001 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16002 return &SCRIPT_VARS(current_SID);
16003 return NULL;
16004}
16005
16006/*
16007 * Get the string value of a (global/local) variable.
16008 * Returns NULL when it doesn't exist.
16009 */
16010 char_u *
16011get_var_value(name)
16012 char_u *name;
16013{
Bram Moolenaar33570922005-01-25 22:26:29 +000016014 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015
Bram Moolenaara7043832005-01-21 11:56:39 +000016016 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 if (v == NULL)
16018 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016019 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020}
16021
16022/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016023 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 * sourcing this script and when executing functions defined in the script.
16025 */
16026 void
16027new_script_vars(id)
16028 scid_T id;
16029{
Bram Moolenaara7043832005-01-21 11:56:39 +000016030 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016031 hashtab_T *ht;
16032 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016033
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16035 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016036 /* Re-allocating ga_data means that an ht_array pointing to
16037 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016038 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016039 for (i = 1; i <= ga_scripts.ga_len; ++i)
16040 {
16041 ht = &SCRIPT_VARS(i);
16042 if (ht->ht_mask == HT_INIT_SIZE - 1)
16043 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016044 sv = &SCRIPT_SV(i);
16045 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016046 }
16047
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048 while (ga_scripts.ga_len < id)
16049 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016050 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16051 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053 }
16054 }
16055}
16056
16057/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016058 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16059 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016060 */
16061 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016062init_var_dict(dict, dict_var)
16063 dict_T *dict;
16064 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016065{
Bram Moolenaar33570922005-01-25 22:26:29 +000016066 hash_init(&dict->dv_hashtab);
16067 dict->dv_refcount = 99999;
16068 dict_var->di_tv.vval.v_dict = dict;
16069 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016070 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016071 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16072 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073}
16074
16075/*
16076 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016077 * Frees all allocated variables and the value they contain.
16078 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079 */
16080 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016081vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016082 hashtab_T *ht;
16083{
16084 vars_clear_ext(ht, TRUE);
16085}
16086
16087/*
16088 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16089 */
16090 static void
16091vars_clear_ext(ht, free_val)
16092 hashtab_T *ht;
16093 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094{
Bram Moolenaara7043832005-01-21 11:56:39 +000016095 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016096 hashitem_T *hi;
16097 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098
Bram Moolenaar33570922005-01-25 22:26:29 +000016099 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016100 todo = ht->ht_used;
16101 for (hi = ht->ht_array; todo > 0; ++hi)
16102 {
16103 if (!HASHITEM_EMPTY(hi))
16104 {
16105 --todo;
16106
Bram Moolenaar33570922005-01-25 22:26:29 +000016107 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016108 * ht_array might change then. hash_clear() takes care of it
16109 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016110 v = HI2DI(hi);
16111 if (free_val)
16112 clear_tv(&v->di_tv);
16113 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16114 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016115 }
16116 }
16117 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016118 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119}
16120
Bram Moolenaara7043832005-01-21 11:56:39 +000016121/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016122 * Delete a variable from hashtab "ht" at item "hi".
16123 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016126delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016127 hashtab_T *ht;
16128 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129{
Bram Moolenaar33570922005-01-25 22:26:29 +000016130 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016131
16132 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016133 clear_tv(&di->di_tv);
16134 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135}
16136
16137/*
16138 * List the value of one internal variable.
16139 */
16140 static void
16141list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016142 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143 char_u *prefix;
16144{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016145 char_u *tofree;
16146 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016147 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016148
Bram Moolenaar33570922005-01-25 22:26:29 +000016149 s = echo_string(&v->di_tv, &tofree, numbuf);
16150 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016151 s == NULL ? (char_u *)"" : s);
16152 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153}
16154
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155 static void
16156list_one_var_a(prefix, name, type, string)
16157 char_u *prefix;
16158 char_u *name;
16159 int type;
16160 char_u *string;
16161{
16162 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16163 if (name != NULL) /* "a:" vars don't have a name stored */
16164 msg_puts(name);
16165 msg_putchar(' ');
16166 msg_advance(22);
16167 if (type == VAR_NUMBER)
16168 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016169 else if (type == VAR_FUNC)
16170 msg_putchar('*');
16171 else if (type == VAR_LIST)
16172 {
16173 msg_putchar('[');
16174 if (*string == '[')
16175 ++string;
16176 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016177 else if (type == VAR_DICT)
16178 {
16179 msg_putchar('{');
16180 if (*string == '{')
16181 ++string;
16182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183 else
16184 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016185
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016187
16188 if (type == VAR_FUNC)
16189 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190}
16191
16192/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016193 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016194 * If the variable already exists, the value is updated.
16195 * Otherwise the variable is created.
16196 */
16197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016198set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016200 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016201 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202{
Bram Moolenaar33570922005-01-25 22:26:29 +000016203 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016205 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016206 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016208 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016209 {
16210 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16211 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16212 ? name[2] : name[0]))
16213 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016214 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016215 return;
16216 }
16217 if (function_exists(name))
16218 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016219 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016220 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016221 return;
16222 }
16223 }
16224
Bram Moolenaara7043832005-01-21 11:56:39 +000016225 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016226 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016227 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016228 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016229 return;
16230 }
16231
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016232 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016233 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016235 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016236 if (var_check_ro(v->di_flags, name)
16237 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016238 return;
16239 if (v->di_tv.v_type != tv->v_type
16240 && !((v->di_tv.v_type == VAR_STRING
16241 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016242 && (tv->v_type == VAR_STRING
16243 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016244 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016245 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016246 return;
16247 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016248
16249 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016250 * Handle setting internal v: variables separately: we don't change
16251 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016252 */
16253 if (ht == &vimvarht)
16254 {
16255 if (v->di_tv.v_type == VAR_STRING)
16256 {
16257 vim_free(v->di_tv.vval.v_string);
16258 if (copy || tv->v_type != VAR_STRING)
16259 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16260 else
16261 {
16262 /* Take over the string to avoid an extra alloc/free. */
16263 v->di_tv.vval.v_string = tv->vval.v_string;
16264 tv->vval.v_string = NULL;
16265 }
16266 }
16267 else if (v->di_tv.v_type != VAR_NUMBER)
16268 EMSG2(_(e_intern2), "set_var()");
16269 else
16270 v->di_tv.vval.v_number = get_tv_number(tv);
16271 return;
16272 }
16273
16274 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275 }
16276 else /* add a new variable */
16277 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016278 /* Make sure the variable name is valid. */
16279 for (p = varname; *p != NUL; ++p)
16280 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16281 {
16282 EMSG2(_(e_illvar), varname);
16283 return;
16284 }
16285
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016286 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16287 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016288 if (v == NULL)
16289 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016290 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016291 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016293 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 return;
16295 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016296 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016299 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016300 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016301 else
16302 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016303 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016304 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016305 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307}
16308
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016309/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016310 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16311 * Also give an error message.
16312 */
16313 static int
16314var_check_ro(flags, name)
16315 int flags;
16316 char_u *name;
16317{
16318 if (flags & DI_FLAGS_RO)
16319 {
16320 EMSG2(_(e_readonlyvar), name);
16321 return TRUE;
16322 }
16323 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16324 {
16325 EMSG2(_(e_readonlysbx), name);
16326 return TRUE;
16327 }
16328 return FALSE;
16329}
16330
16331/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016332 * Return TRUE if typeval "tv" is set to be locked (immutable).
16333 * Also give an error message, using "name".
16334 */
16335 static int
16336tv_check_lock(lock, name)
16337 int lock;
16338 char_u *name;
16339{
16340 if (lock & VAR_LOCKED)
16341 {
16342 EMSG2(_("E741: Value is locked: %s"),
16343 name == NULL ? (char_u *)_("Unknown") : name);
16344 return TRUE;
16345 }
16346 if (lock & VAR_FIXED)
16347 {
16348 EMSG2(_("E742: Cannot change value of %s"),
16349 name == NULL ? (char_u *)_("Unknown") : name);
16350 return TRUE;
16351 }
16352 return FALSE;
16353}
16354
16355/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016356 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016357 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016358 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016361copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016362 typval_T *from;
16363 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016365 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016366 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016367 switch (from->v_type)
16368 {
16369 case VAR_NUMBER:
16370 to->vval.v_number = from->vval.v_number;
16371 break;
16372 case VAR_STRING:
16373 case VAR_FUNC:
16374 if (from->vval.v_string == NULL)
16375 to->vval.v_string = NULL;
16376 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016377 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016378 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016379 if (from->v_type == VAR_FUNC)
16380 func_ref(to->vval.v_string);
16381 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016382 break;
16383 case VAR_LIST:
16384 if (from->vval.v_list == NULL)
16385 to->vval.v_list = NULL;
16386 else
16387 {
16388 to->vval.v_list = from->vval.v_list;
16389 ++to->vval.v_list->lv_refcount;
16390 }
16391 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016392 case VAR_DICT:
16393 if (from->vval.v_dict == NULL)
16394 to->vval.v_dict = NULL;
16395 else
16396 {
16397 to->vval.v_dict = from->vval.v_dict;
16398 ++to->vval.v_dict->dv_refcount;
16399 }
16400 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016401 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016402 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016403 break;
16404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405}
16406
16407/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016408 * Make a copy of an item.
16409 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016410 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16411 * reference to an already copied list/dict can be used.
16412 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016413 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016414 static int
16415item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016416 typval_T *from;
16417 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016418 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016419 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016420{
16421 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016422 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016423
Bram Moolenaar33570922005-01-25 22:26:29 +000016424 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016425 {
16426 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016427 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016428 }
16429 ++recurse;
16430
16431 switch (from->v_type)
16432 {
16433 case VAR_NUMBER:
16434 case VAR_STRING:
16435 case VAR_FUNC:
16436 copy_tv(from, to);
16437 break;
16438 case VAR_LIST:
16439 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016440 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016441 if (from->vval.v_list == NULL)
16442 to->vval.v_list = NULL;
16443 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16444 {
16445 /* use the copy made earlier */
16446 to->vval.v_list = from->vval.v_list->lv_copylist;
16447 ++to->vval.v_list->lv_refcount;
16448 }
16449 else
16450 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16451 if (to->vval.v_list == NULL)
16452 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016453 break;
16454 case VAR_DICT:
16455 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016456 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016457 if (from->vval.v_dict == NULL)
16458 to->vval.v_dict = NULL;
16459 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16460 {
16461 /* use the copy made earlier */
16462 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16463 ++to->vval.v_dict->dv_refcount;
16464 }
16465 else
16466 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16467 if (to->vval.v_dict == NULL)
16468 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016469 break;
16470 default:
16471 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016472 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016473 }
16474 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016475 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016476}
16477
16478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479 * ":echo expr1 ..." print each argument separated with a space, add a
16480 * newline at the end.
16481 * ":echon expr1 ..." print each argument plain.
16482 */
16483 void
16484ex_echo(eap)
16485 exarg_T *eap;
16486{
16487 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016488 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016489 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016490 char_u *p;
16491 int needclr = TRUE;
16492 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016493 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494
16495 if (eap->skip)
16496 ++emsg_skip;
16497 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16498 {
16499 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016500 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501 {
16502 /*
16503 * Report the invalid expression unless the expression evaluation
16504 * has been cancelled due to an aborting error, an interrupt, or an
16505 * exception.
16506 */
16507 if (!aborting())
16508 EMSG2(_(e_invexpr2), p);
16509 break;
16510 }
16511 if (!eap->skip)
16512 {
16513 if (atstart)
16514 {
16515 atstart = FALSE;
16516 /* Call msg_start() after eval1(), evaluating the expression
16517 * may cause a message to appear. */
16518 if (eap->cmdidx == CMD_echo)
16519 msg_start();
16520 }
16521 else if (eap->cmdidx == CMD_echo)
16522 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016523 p = echo_string(&rettv, &tofree, numbuf);
16524 if (p != NULL)
16525 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016527 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016529 if (*p != TAB && needclr)
16530 {
16531 /* remove any text still there from the command */
16532 msg_clr_eos();
16533 needclr = FALSE;
16534 }
16535 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 }
16537 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016538 {
16539#ifdef FEAT_MBYTE
16540 if (has_mbyte)
16541 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016542 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016543
16544 (void)msg_outtrans_len_attr(p, i, echo_attr);
16545 p += i - 1;
16546 }
16547 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016549 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016552 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016554 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 arg = skipwhite(arg);
16556 }
16557 eap->nextcmd = check_nextcmd(arg);
16558
16559 if (eap->skip)
16560 --emsg_skip;
16561 else
16562 {
16563 /* remove text that may still be there from the command */
16564 if (needclr)
16565 msg_clr_eos();
16566 if (eap->cmdidx == CMD_echo)
16567 msg_end();
16568 }
16569}
16570
16571/*
16572 * ":echohl {name}".
16573 */
16574 void
16575ex_echohl(eap)
16576 exarg_T *eap;
16577{
16578 int id;
16579
16580 id = syn_name2id(eap->arg);
16581 if (id == 0)
16582 echo_attr = 0;
16583 else
16584 echo_attr = syn_id2attr(id);
16585}
16586
16587/*
16588 * ":execute expr1 ..." execute the result of an expression.
16589 * ":echomsg expr1 ..." Print a message
16590 * ":echoerr expr1 ..." Print an error
16591 * Each gets spaces around each argument and a newline at the end for
16592 * echo commands
16593 */
16594 void
16595ex_execute(eap)
16596 exarg_T *eap;
16597{
16598 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016599 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600 int ret = OK;
16601 char_u *p;
16602 garray_T ga;
16603 int len;
16604 int save_did_emsg;
16605
16606 ga_init2(&ga, 1, 80);
16607
16608 if (eap->skip)
16609 ++emsg_skip;
16610 while (*arg != NUL && *arg != '|' && *arg != '\n')
16611 {
16612 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016613 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614 {
16615 /*
16616 * Report the invalid expression unless the expression evaluation
16617 * has been cancelled due to an aborting error, an interrupt, or an
16618 * exception.
16619 */
16620 if (!aborting())
16621 EMSG2(_(e_invexpr2), p);
16622 ret = FAIL;
16623 break;
16624 }
16625
16626 if (!eap->skip)
16627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016628 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629 len = (int)STRLEN(p);
16630 if (ga_grow(&ga, len + 2) == FAIL)
16631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016632 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016633 ret = FAIL;
16634 break;
16635 }
16636 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639 ga.ga_len += len;
16640 }
16641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016642 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643 arg = skipwhite(arg);
16644 }
16645
16646 if (ret != FAIL && ga.ga_data != NULL)
16647 {
16648 if (eap->cmdidx == CMD_echomsg)
16649 MSG_ATTR(ga.ga_data, echo_attr);
16650 else if (eap->cmdidx == CMD_echoerr)
16651 {
16652 /* We don't want to abort following commands, restore did_emsg. */
16653 save_did_emsg = did_emsg;
16654 EMSG((char_u *)ga.ga_data);
16655 if (!force_abort)
16656 did_emsg = save_did_emsg;
16657 }
16658 else if (eap->cmdidx == CMD_execute)
16659 do_cmdline((char_u *)ga.ga_data,
16660 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16661 }
16662
16663 ga_clear(&ga);
16664
16665 if (eap->skip)
16666 --emsg_skip;
16667
16668 eap->nextcmd = check_nextcmd(arg);
16669}
16670
16671/*
16672 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16673 * "arg" points to the "&" or '+' when called, to "option" when returning.
16674 * Returns NULL when no option name found. Otherwise pointer to the char
16675 * after the option name.
16676 */
16677 static char_u *
16678find_option_end(arg, opt_flags)
16679 char_u **arg;
16680 int *opt_flags;
16681{
16682 char_u *p = *arg;
16683
16684 ++p;
16685 if (*p == 'g' && p[1] == ':')
16686 {
16687 *opt_flags = OPT_GLOBAL;
16688 p += 2;
16689 }
16690 else if (*p == 'l' && p[1] == ':')
16691 {
16692 *opt_flags = OPT_LOCAL;
16693 p += 2;
16694 }
16695 else
16696 *opt_flags = 0;
16697
16698 if (!ASCII_ISALPHA(*p))
16699 return NULL;
16700 *arg = p;
16701
16702 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16703 p += 4; /* termcap option */
16704 else
16705 while (ASCII_ISALPHA(*p))
16706 ++p;
16707 return p;
16708}
16709
16710/*
16711 * ":function"
16712 */
16713 void
16714ex_function(eap)
16715 exarg_T *eap;
16716{
16717 char_u *theline;
16718 int j;
16719 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 char_u *name = NULL;
16722 char_u *p;
16723 char_u *arg;
16724 garray_T newargs;
16725 garray_T newlines;
16726 int varargs = FALSE;
16727 int mustend = FALSE;
16728 int flags = 0;
16729 ufunc_T *fp;
16730 int indent;
16731 int nesting;
16732 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016733 dictitem_T *v;
16734 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016735 static int func_nr = 0; /* number for nameless function */
16736 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016737 hashtab_T *ht;
16738 int todo;
16739 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740
16741 /*
16742 * ":function" without argument: list functions.
16743 */
16744 if (ends_excmd(*eap->arg))
16745 {
16746 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016747 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016748 todo = func_hashtab.ht_used;
16749 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016750 {
16751 if (!HASHITEM_EMPTY(hi))
16752 {
16753 --todo;
16754 fp = HI2UF(hi);
16755 if (!isdigit(*fp->uf_name))
16756 list_func_head(fp, FALSE);
16757 }
16758 }
16759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 eap->nextcmd = check_nextcmd(eap->arg);
16761 return;
16762 }
16763
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016764 /*
16765 * Get the function name. There are these situations:
16766 * func normal function name
16767 * "name" == func, "fudi.fd_dict" == NULL
16768 * dict.func new dictionary entry
16769 * "name" == NULL, "fudi.fd_dict" set,
16770 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16771 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016772 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016773 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16774 * dict.func existing dict entry that's not a Funcref
16775 * "name" == NULL, "fudi.fd_dict" set,
16776 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016779 name = trans_function_name(&p, eap->skip, 0, &fudi);
16780 paren = (vim_strchr(p, '(') != NULL);
16781 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016782 {
16783 /*
16784 * Return on an invalid expression in braces, unless the expression
16785 * evaluation has been cancelled due to an aborting error, an
16786 * interrupt, or an exception.
16787 */
16788 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016789 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016790 if (!eap->skip && fudi.fd_newkey != NULL)
16791 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016792 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016793 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795 else
16796 eap->skip = TRUE;
16797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 /* An error in a function call during evaluation of an expression in magic
16799 * braces should not cause the function not to be defined. */
16800 saved_did_emsg = did_emsg;
16801 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802
16803 /*
16804 * ":function func" with only function name: list function.
16805 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016806 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807 {
16808 if (!ends_excmd(*skipwhite(p)))
16809 {
16810 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016811 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812 }
16813 eap->nextcmd = check_nextcmd(p);
16814 if (eap->nextcmd != NULL)
16815 *p = NUL;
16816 if (!eap->skip && !got_int)
16817 {
16818 fp = find_func(name);
16819 if (fp != NULL)
16820 {
16821 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016822 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823 {
16824 msg_putchar('\n');
16825 msg_outnum((long)(j + 1));
16826 if (j < 9)
16827 msg_putchar(' ');
16828 if (j < 99)
16829 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016830 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831 out_flush(); /* show a line at a time */
16832 ui_breakcheck();
16833 }
16834 if (!got_int)
16835 {
16836 msg_putchar('\n');
16837 msg_puts((char_u *)" endfunction");
16838 }
16839 }
16840 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016841 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016843 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 }
16845
16846 /*
16847 * ":function name(arg1, arg2)" Define function.
16848 */
16849 p = skipwhite(p);
16850 if (*p != '(')
16851 {
16852 if (!eap->skip)
16853 {
16854 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016855 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856 }
16857 /* attempt to continue by skipping some text */
16858 if (vim_strchr(p, '(') != NULL)
16859 p = vim_strchr(p, '(');
16860 }
16861 p = skipwhite(p + 1);
16862
16863 ga_init2(&newargs, (int)sizeof(char_u *), 3);
16864 ga_init2(&newlines, (int)sizeof(char_u *), 3);
16865
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016866 if (!eap->skip)
16867 {
16868 /* Check the name of the function. */
16869 if (name != NULL)
16870 arg = name;
16871 else
16872 arg = fudi.fd_newkey;
16873 if (arg != NULL)
16874 {
16875 if (*arg == K_SPECIAL)
16876 j = 3;
16877 else
16878 j = 0;
16879 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
16880 : eval_isnamec(arg[j])))
16881 ++j;
16882 if (arg[j] != NUL)
16883 emsg_funcname(_(e_invarg2), arg);
16884 }
16885 }
16886
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887 /*
16888 * Isolate the arguments: "arg1, arg2, ...)"
16889 */
16890 while (*p != ')')
16891 {
16892 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
16893 {
16894 varargs = TRUE;
16895 p += 3;
16896 mustend = TRUE;
16897 }
16898 else
16899 {
16900 arg = p;
16901 while (ASCII_ISALNUM(*p) || *p == '_')
16902 ++p;
16903 if (arg == p || isdigit(*arg)
16904 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
16905 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
16906 {
16907 if (!eap->skip)
16908 EMSG2(_("E125: Illegal argument: %s"), arg);
16909 break;
16910 }
16911 if (ga_grow(&newargs, 1) == FAIL)
16912 goto erret;
16913 c = *p;
16914 *p = NUL;
16915 arg = vim_strsave(arg);
16916 if (arg == NULL)
16917 goto erret;
16918 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
16919 *p = c;
16920 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921 if (*p == ',')
16922 ++p;
16923 else
16924 mustend = TRUE;
16925 }
16926 p = skipwhite(p);
16927 if (mustend && *p != ')')
16928 {
16929 if (!eap->skip)
16930 EMSG2(_(e_invarg2), eap->arg);
16931 break;
16932 }
16933 }
16934 ++p; /* skip the ')' */
16935
Bram Moolenaare9a41262005-01-15 22:18:47 +000016936 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937 for (;;)
16938 {
16939 p = skipwhite(p);
16940 if (STRNCMP(p, "range", 5) == 0)
16941 {
16942 flags |= FC_RANGE;
16943 p += 5;
16944 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016945 else if (STRNCMP(p, "dict", 4) == 0)
16946 {
16947 flags |= FC_DICT;
16948 p += 4;
16949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950 else if (STRNCMP(p, "abort", 5) == 0)
16951 {
16952 flags |= FC_ABORT;
16953 p += 5;
16954 }
16955 else
16956 break;
16957 }
16958
16959 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
16960 EMSG(_(e_trailing));
16961
16962 /*
16963 * Read the body of the function, until ":endfunction" is found.
16964 */
16965 if (KeyTyped)
16966 {
16967 /* Check if the function already exists, don't let the user type the
16968 * whole function before telling him it doesn't work! For a script we
16969 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016970 if (!eap->skip && !eap->forceit)
16971 {
16972 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
16973 EMSG(_(e_funcdict));
16974 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016975 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016978 if (!eap->skip && did_emsg)
16979 goto erret;
16980
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981 msg_putchar('\n'); /* don't overwrite the function name */
16982 cmdline_row = msg_row;
16983 }
16984
16985 indent = 2;
16986 nesting = 0;
16987 for (;;)
16988 {
16989 msg_scroll = TRUE;
16990 need_wait_return = FALSE;
16991 if (eap->getline == NULL)
16992 theline = getcmdline(':', 0L, indent);
16993 else
16994 theline = eap->getline(':', eap->cookie, indent);
16995 if (KeyTyped)
16996 lines_left = Rows - 1;
16997 if (theline == NULL)
16998 {
16999 EMSG(_("E126: Missing :endfunction"));
17000 goto erret;
17001 }
17002
17003 if (skip_until != NULL)
17004 {
17005 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17006 * don't check for ":endfunc". */
17007 if (STRCMP(theline, skip_until) == 0)
17008 {
17009 vim_free(skip_until);
17010 skip_until = NULL;
17011 }
17012 }
17013 else
17014 {
17015 /* skip ':' and blanks*/
17016 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17017 ;
17018
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017019 /* Check for "endfunction". */
17020 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 {
17022 vim_free(theline);
17023 break;
17024 }
17025
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017026 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027 * at "end". */
17028 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17029 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017030 else if (STRNCMP(p, "if", 2) == 0
17031 || STRNCMP(p, "wh", 2) == 0
17032 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033 || STRNCMP(p, "try", 3) == 0)
17034 indent += 2;
17035
17036 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017037 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017039 if (*p == '!')
17040 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041 p += eval_fname_script(p);
17042 if (ASCII_ISALPHA(*p))
17043 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017044 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 if (*skipwhite(p) == '(')
17046 {
17047 ++nesting;
17048 indent += 2;
17049 }
17050 }
17051 }
17052
17053 /* Check for ":append" or ":insert". */
17054 p = skip_range(p, NULL);
17055 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17056 || (p[0] == 'i'
17057 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17058 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17059 skip_until = vim_strsave((char_u *)".");
17060
17061 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17062 arg = skipwhite(skiptowhite(p));
17063 if (arg[0] == '<' && arg[1] =='<'
17064 && ((p[0] == 'p' && p[1] == 'y'
17065 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17066 || (p[0] == 'p' && p[1] == 'e'
17067 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17068 || (p[0] == 't' && p[1] == 'c'
17069 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17070 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17071 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017072 || (p[0] == 'm' && p[1] == 'z'
17073 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074 ))
17075 {
17076 /* ":python <<" continues until a dot, like ":append" */
17077 p = skipwhite(arg + 2);
17078 if (*p == NUL)
17079 skip_until = vim_strsave((char_u *)".");
17080 else
17081 skip_until = vim_strsave(p);
17082 }
17083 }
17084
17085 /* Add the line to the function. */
17086 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017087 {
17088 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017090 }
17091
17092 /* Copy the line to newly allocated memory. get_one_sourceline()
17093 * allocates 250 bytes per line, this saves 80% on average. The cost
17094 * is an extra alloc/free. */
17095 p = vim_strsave(theline);
17096 if (p != NULL)
17097 {
17098 vim_free(theline);
17099 theline = p;
17100 }
17101
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17103 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104 }
17105
17106 /* Don't define the function when skipping commands or when an error was
17107 * detected. */
17108 if (eap->skip || did_emsg)
17109 goto erret;
17110
17111 /*
17112 * If there are no errors, add the function
17113 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017114 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017115 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017116 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017117 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017118 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017119 emsg_funcname("E707: Function name conflicts with variable: %s",
17120 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017121 goto erret;
17122 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017123
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017124 fp = find_func(name);
17125 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017127 if (!eap->forceit)
17128 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017129 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017130 goto erret;
17131 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017132 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017133 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017134 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017135 name);
17136 goto erret;
17137 }
17138 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017139 ga_clear_strings(&(fp->uf_args));
17140 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017141 vim_free(name);
17142 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144 }
17145 else
17146 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017147 char numbuf[20];
17148
17149 fp = NULL;
17150 if (fudi.fd_newkey == NULL && !eap->forceit)
17151 {
17152 EMSG(_(e_funcdict));
17153 goto erret;
17154 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017155 if (fudi.fd_di == NULL)
17156 {
17157 /* Can't add a function to a locked dictionary */
17158 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17159 goto erret;
17160 }
17161 /* Can't change an existing function if it is locked */
17162 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17163 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017164
17165 /* Give the function a sequential number. Can only be used with a
17166 * Funcref! */
17167 vim_free(name);
17168 sprintf(numbuf, "%d", ++func_nr);
17169 name = vim_strsave((char_u *)numbuf);
17170 if (name == NULL)
17171 goto erret;
17172 }
17173
17174 if (fp == NULL)
17175 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017176 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017177 {
17178 int slen, plen;
17179 char_u *scriptname;
17180
17181 /* Check that the autoload name matches the script name. */
17182 j = FAIL;
17183 if (sourcing_name != NULL)
17184 {
17185 scriptname = autoload_name(name);
17186 if (scriptname != NULL)
17187 {
17188 p = vim_strchr(scriptname, '/');
17189 plen = STRLEN(p);
17190 slen = STRLEN(sourcing_name);
17191 if (slen > plen && fnamecmp(p,
17192 sourcing_name + slen - plen) == 0)
17193 j = OK;
17194 vim_free(scriptname);
17195 }
17196 }
17197 if (j == FAIL)
17198 {
17199 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17200 goto erret;
17201 }
17202 }
17203
17204 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205 if (fp == NULL)
17206 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017207
17208 if (fudi.fd_dict != NULL)
17209 {
17210 if (fudi.fd_di == NULL)
17211 {
17212 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017213 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017214 if (fudi.fd_di == NULL)
17215 {
17216 vim_free(fp);
17217 goto erret;
17218 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017219 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17220 {
17221 vim_free(fudi.fd_di);
17222 goto erret;
17223 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017224 }
17225 else
17226 /* overwrite existing dict entry */
17227 clear_tv(&fudi.fd_di->di_tv);
17228 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017229 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017230 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017231 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017232 }
17233
Bram Moolenaar071d4272004-06-13 20:20:40 +000017234 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017235 STRCPY(fp->uf_name, name);
17236 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017237 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017238 fp->uf_args = newargs;
17239 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017240#ifdef FEAT_PROFILE
17241 fp->uf_tml_count = NULL;
17242 fp->uf_tml_total = NULL;
17243 fp->uf_tml_self = NULL;
17244 fp->uf_profiling = FALSE;
17245 if (prof_def_func())
17246 func_do_profile(fp);
17247#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017248 fp->uf_varargs = varargs;
17249 fp->uf_flags = flags;
17250 fp->uf_calls = 0;
17251 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017252 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253
17254erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017255 ga_clear_strings(&newargs);
17256 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017257ret_free:
17258 vim_free(skip_until);
17259 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262}
17263
17264/*
17265 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017266 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017268 * flags:
17269 * TFN_INT: internal function name OK
17270 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271 * Advances "pp" to just after the function name (if no error).
17272 */
17273 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017274trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275 char_u **pp;
17276 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017277 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017278 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017280 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281 char_u *start;
17282 char_u *end;
17283 int lead;
17284 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017286 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017287
17288 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017289 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017291
17292 /* Check for hard coded <SNR>: already translated function ID (from a user
17293 * command). */
17294 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17295 && (*pp)[2] == (int)KE_SNR)
17296 {
17297 *pp += 3;
17298 len = get_id_len(pp) + 3;
17299 return vim_strnsave(start, len);
17300 }
17301
17302 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17303 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017305 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017307
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017308 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17309 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017310 if (end == start)
17311 {
17312 if (!skip)
17313 EMSG(_("E129: Function name required"));
17314 goto theend;
17315 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017316 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017317 {
17318 /*
17319 * Report an invalid expression in braces, unless the expression
17320 * evaluation has been cancelled due to an aborting error, an
17321 * interrupt, or an exception.
17322 */
17323 if (!aborting())
17324 {
17325 if (end != NULL)
17326 EMSG2(_(e_invarg2), start);
17327 }
17328 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017329 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017330 goto theend;
17331 }
17332
17333 if (lv.ll_tv != NULL)
17334 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017335 if (fdp != NULL)
17336 {
17337 fdp->fd_dict = lv.ll_dict;
17338 fdp->fd_newkey = lv.ll_newkey;
17339 lv.ll_newkey = NULL;
17340 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017341 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017342 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17343 {
17344 name = vim_strsave(lv.ll_tv->vval.v_string);
17345 *pp = end;
17346 }
17347 else
17348 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017349 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17350 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017351 EMSG(_(e_funcref));
17352 else
17353 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017354 name = NULL;
17355 }
17356 goto theend;
17357 }
17358
17359 if (lv.ll_name == NULL)
17360 {
17361 /* Error found, but continue after the function name. */
17362 *pp = end;
17363 goto theend;
17364 }
17365
17366 if (lv.ll_exp_name != NULL)
17367 len = STRLEN(lv.ll_exp_name);
17368 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017369 {
17370 if (lead == 2) /* skip over "s:" */
17371 lv.ll_name += 2;
17372 len = (int)(end - lv.ll_name);
17373 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017374
17375 /*
17376 * Copy the function name to allocated memory.
17377 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17378 * Accept <SNR>123_name() outside a script.
17379 */
17380 if (skip)
17381 lead = 0; /* do nothing */
17382 else if (lead > 0)
17383 {
17384 lead = 3;
17385 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17386 {
17387 if (current_SID <= 0)
17388 {
17389 EMSG(_(e_usingsid));
17390 goto theend;
17391 }
17392 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17393 lead += (int)STRLEN(sid_buf);
17394 }
17395 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017396 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017397 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017398 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017399 goto theend;
17400 }
17401 name = alloc((unsigned)(len + lead + 1));
17402 if (name != NULL)
17403 {
17404 if (lead > 0)
17405 {
17406 name[0] = K_SPECIAL;
17407 name[1] = KS_EXTRA;
17408 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017409 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017410 STRCPY(name + 3, sid_buf);
17411 }
17412 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17413 name[len + lead] = NUL;
17414 }
17415 *pp = end;
17416
17417theend:
17418 clear_lval(&lv);
17419 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420}
17421
17422/*
17423 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17424 * Return 2 if "p" starts with "s:".
17425 * Return 0 otherwise.
17426 */
17427 static int
17428eval_fname_script(p)
17429 char_u *p;
17430{
17431 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17432 || STRNICMP(p + 1, "SNR>", 4) == 0))
17433 return 5;
17434 if (p[0] == 's' && p[1] == ':')
17435 return 2;
17436 return 0;
17437}
17438
17439/*
17440 * Return TRUE if "p" starts with "<SID>" or "s:".
17441 * Only works if eval_fname_script() returned non-zero for "p"!
17442 */
17443 static int
17444eval_fname_sid(p)
17445 char_u *p;
17446{
17447 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17448}
17449
17450/*
17451 * List the head of the function: "name(arg1, arg2)".
17452 */
17453 static void
17454list_func_head(fp, indent)
17455 ufunc_T *fp;
17456 int indent;
17457{
17458 int j;
17459
17460 msg_start();
17461 if (indent)
17462 MSG_PUTS(" ");
17463 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017464 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465 {
17466 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017467 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468 }
17469 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017470 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017472 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 {
17474 if (j)
17475 MSG_PUTS(", ");
17476 msg_puts(FUNCARG(fp, j));
17477 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017478 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 {
17480 if (j)
17481 MSG_PUTS(", ");
17482 MSG_PUTS("...");
17483 }
17484 msg_putchar(')');
17485}
17486
17487/*
17488 * Find a function by name, return pointer to it in ufuncs.
17489 * Return NULL for unknown function.
17490 */
17491 static ufunc_T *
17492find_func(name)
17493 char_u *name;
17494{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017495 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017497 hi = hash_find(&func_hashtab, name);
17498 if (!HASHITEM_EMPTY(hi))
17499 return HI2UF(hi);
17500 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501}
17502
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017503#if defined(EXITFREE) || defined(PROTO)
17504 void
17505free_all_functions()
17506{
17507 hashitem_T *hi;
17508
17509 /* Need to start all over every time, because func_free() may change the
17510 * hash table. */
17511 while (func_hashtab.ht_used > 0)
17512 for (hi = func_hashtab.ht_array; ; ++hi)
17513 if (!HASHITEM_EMPTY(hi))
17514 {
17515 func_free(HI2UF(hi));
17516 break;
17517 }
17518}
17519#endif
17520
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017521/*
17522 * Return TRUE if a function "name" exists.
17523 */
17524 static int
17525function_exists(name)
17526 char_u *name;
17527{
17528 char_u *p = name;
17529 int n = FALSE;
17530
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017531 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017532 if (p != NULL)
17533 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017534 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017535 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017536 else
17537 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017538 vim_free(p);
17539 }
17540 return n;
17541}
17542
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017543/*
17544 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017545 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017546 */
17547 static int
17548builtin_function(name)
17549 char_u *name;
17550{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017551 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17552 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017553}
17554
Bram Moolenaar05159a02005-02-26 23:04:13 +000017555#if defined(FEAT_PROFILE) || defined(PROTO)
17556/*
17557 * Start profiling function "fp".
17558 */
17559 static void
17560func_do_profile(fp)
17561 ufunc_T *fp;
17562{
17563 fp->uf_tm_count = 0;
17564 profile_zero(&fp->uf_tm_self);
17565 profile_zero(&fp->uf_tm_total);
17566 if (fp->uf_tml_count == NULL)
17567 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17568 (sizeof(int) * fp->uf_lines.ga_len));
17569 if (fp->uf_tml_total == NULL)
17570 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17571 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17572 if (fp->uf_tml_self == NULL)
17573 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17574 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17575 fp->uf_tml_idx = -1;
17576 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17577 || fp->uf_tml_self == NULL)
17578 return; /* out of memory */
17579
17580 fp->uf_profiling = TRUE;
17581}
17582
17583/*
17584 * Dump the profiling results for all functions in file "fd".
17585 */
17586 void
17587func_dump_profile(fd)
17588 FILE *fd;
17589{
17590 hashitem_T *hi;
17591 int todo;
17592 ufunc_T *fp;
17593 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017594 ufunc_T **sorttab;
17595 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017596
17597 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017598 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17599
Bram Moolenaar05159a02005-02-26 23:04:13 +000017600 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17601 {
17602 if (!HASHITEM_EMPTY(hi))
17603 {
17604 --todo;
17605 fp = HI2UF(hi);
17606 if (fp->uf_profiling)
17607 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017608 if (sorttab != NULL)
17609 sorttab[st_len++] = fp;
17610
Bram Moolenaar05159a02005-02-26 23:04:13 +000017611 if (fp->uf_name[0] == K_SPECIAL)
17612 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17613 else
17614 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17615 if (fp->uf_tm_count == 1)
17616 fprintf(fd, "Called 1 time\n");
17617 else
17618 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17619 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17620 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17621 fprintf(fd, "\n");
17622 fprintf(fd, "count total (s) self (s)\n");
17623
17624 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17625 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017626 prof_func_line(fd, fp->uf_tml_count[i],
17627 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017628 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17629 }
17630 fprintf(fd, "\n");
17631 }
17632 }
17633 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017634
17635 if (sorttab != NULL && st_len > 0)
17636 {
17637 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17638 prof_total_cmp);
17639 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17640 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17641 prof_self_cmp);
17642 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17643 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017644}
Bram Moolenaar73830342005-02-28 22:48:19 +000017645
17646 static void
17647prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17648 FILE *fd;
17649 ufunc_T **sorttab;
17650 int st_len;
17651 char *title;
17652 int prefer_self; /* when equal print only self time */
17653{
17654 int i;
17655 ufunc_T *fp;
17656
17657 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17658 fprintf(fd, "count total (s) self (s) function\n");
17659 for (i = 0; i < 20 && i < st_len; ++i)
17660 {
17661 fp = sorttab[i];
17662 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17663 prefer_self);
17664 if (fp->uf_name[0] == K_SPECIAL)
17665 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17666 else
17667 fprintf(fd, " %s()\n", fp->uf_name);
17668 }
17669 fprintf(fd, "\n");
17670}
17671
17672/*
17673 * Print the count and times for one function or function line.
17674 */
17675 static void
17676prof_func_line(fd, count, total, self, prefer_self)
17677 FILE *fd;
17678 int count;
17679 proftime_T *total;
17680 proftime_T *self;
17681 int prefer_self; /* when equal print only self time */
17682{
17683 if (count > 0)
17684 {
17685 fprintf(fd, "%5d ", count);
17686 if (prefer_self && profile_equal(total, self))
17687 fprintf(fd, " ");
17688 else
17689 fprintf(fd, "%s ", profile_msg(total));
17690 if (!prefer_self && profile_equal(total, self))
17691 fprintf(fd, " ");
17692 else
17693 fprintf(fd, "%s ", profile_msg(self));
17694 }
17695 else
17696 fprintf(fd, " ");
17697}
17698
17699/*
17700 * Compare function for total time sorting.
17701 */
17702 static int
17703#ifdef __BORLANDC__
17704_RTLENTRYF
17705#endif
17706prof_total_cmp(s1, s2)
17707 const void *s1;
17708 const void *s2;
17709{
17710 ufunc_T *p1, *p2;
17711
17712 p1 = *(ufunc_T **)s1;
17713 p2 = *(ufunc_T **)s2;
17714 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17715}
17716
17717/*
17718 * Compare function for self time sorting.
17719 */
17720 static int
17721#ifdef __BORLANDC__
17722_RTLENTRYF
17723#endif
17724prof_self_cmp(s1, s2)
17725 const void *s1;
17726 const void *s2;
17727{
17728 ufunc_T *p1, *p2;
17729
17730 p1 = *(ufunc_T **)s1;
17731 p2 = *(ufunc_T **)s2;
17732 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17733}
17734
Bram Moolenaar05159a02005-02-26 23:04:13 +000017735#endif
17736
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017737/* The names of packages that once were loaded is remembered. */
17738static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
17739
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017740/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017741 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017742 * Return TRUE if a package was loaded.
17743 */
17744 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017745script_autoload(name, reload)
17746 char_u *name;
17747 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017748{
17749 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017750 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017751 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017752 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017753
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017754 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017755 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017756 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017757 return FALSE;
17758
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017759 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017760
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017761 /* Find the name in the list of previously loaded package names. Skip
17762 * "autoload/", it's always the same. */
17763 for (i = 0; i < ga_loaded.ga_len; ++i)
17764 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
17765 break;
17766 if (!reload && i < ga_loaded.ga_len)
17767 ret = FALSE; /* was loaded already */
17768 else
17769 {
17770 /* Remember the name if it wasn't loaded already. */
17771 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
17772 {
17773 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
17774 tofree = NULL;
17775 }
17776
17777 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000017778 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017779 ret = TRUE;
17780 }
17781
17782 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017783 return ret;
17784}
17785
17786/*
17787 * Return the autoload script name for a function or variable name.
17788 * Returns NULL when out of memory.
17789 */
17790 static char_u *
17791autoload_name(name)
17792 char_u *name;
17793{
17794 char_u *p;
17795 char_u *scriptname;
17796
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017797 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017798 scriptname = alloc((unsigned)(STRLEN(name) + 14));
17799 if (scriptname == NULL)
17800 return FALSE;
17801 STRCPY(scriptname, "autoload/");
17802 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017803 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017804 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017805 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017806 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017807 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017808}
17809
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
17811
17812/*
17813 * Function given to ExpandGeneric() to obtain the list of user defined
17814 * function names.
17815 */
17816 char_u *
17817get_user_func_name(xp, idx)
17818 expand_T *xp;
17819 int idx;
17820{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017821 static long_u done;
17822 static hashitem_T *hi;
17823 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824
17825 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017826 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017827 done = 0;
17828 hi = func_hashtab.ht_array;
17829 }
17830 if (done < func_hashtab.ht_used)
17831 {
17832 if (done++ > 0)
17833 ++hi;
17834 while (HASHITEM_EMPTY(hi))
17835 ++hi;
17836 fp = HI2UF(hi);
17837
17838 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
17839 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840
17841 cat_func_name(IObuff, fp);
17842 if (xp->xp_context != EXPAND_USER_FUNC)
17843 {
17844 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017845 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846 STRCAT(IObuff, ")");
17847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848 return IObuff;
17849 }
17850 return NULL;
17851}
17852
17853#endif /* FEAT_CMDL_COMPL */
17854
17855/*
17856 * Copy the function name of "fp" to buffer "buf".
17857 * "buf" must be able to hold the function name plus three bytes.
17858 * Takes care of script-local function names.
17859 */
17860 static void
17861cat_func_name(buf, fp)
17862 char_u *buf;
17863 ufunc_T *fp;
17864{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017865 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866 {
17867 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017868 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869 }
17870 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017871 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872}
17873
17874/*
17875 * ":delfunction {name}"
17876 */
17877 void
17878ex_delfunction(eap)
17879 exarg_T *eap;
17880{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017881 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017882 char_u *p;
17883 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017884 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885
17886 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017887 name = trans_function_name(&p, eap->skip, 0, &fudi);
17888 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017890 {
17891 if (fudi.fd_dict != NULL && !eap->skip)
17892 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895 if (!ends_excmd(*skipwhite(p)))
17896 {
17897 vim_free(name);
17898 EMSG(_(e_trailing));
17899 return;
17900 }
17901 eap->nextcmd = check_nextcmd(p);
17902 if (eap->nextcmd != NULL)
17903 *p = NUL;
17904
17905 if (!eap->skip)
17906 fp = find_func(name);
17907 vim_free(name);
17908
17909 if (!eap->skip)
17910 {
17911 if (fp == NULL)
17912 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017913 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914 return;
17915 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017916 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 {
17918 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
17919 return;
17920 }
17921
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017922 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017924 /* Delete the dict item that refers to the function, it will
17925 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017926 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017928 else
17929 func_free(fp);
17930 }
17931}
17932
17933/*
17934 * Free a function and remove it from the list of functions.
17935 */
17936 static void
17937func_free(fp)
17938 ufunc_T *fp;
17939{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017940 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017941
17942 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017943 ga_clear_strings(&(fp->uf_args));
17944 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000017945#ifdef FEAT_PROFILE
17946 vim_free(fp->uf_tml_count);
17947 vim_free(fp->uf_tml_total);
17948 vim_free(fp->uf_tml_self);
17949#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017950
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017951 /* remove the function from the function hashtable */
17952 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
17953 if (HASHITEM_EMPTY(hi))
17954 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017955 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017956 hash_remove(&func_hashtab, hi);
17957
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017958 vim_free(fp);
17959}
17960
17961/*
17962 * Unreference a Function: decrement the reference count and free it when it
17963 * becomes zero. Only for numbered functions.
17964 */
17965 static void
17966func_unref(name)
17967 char_u *name;
17968{
17969 ufunc_T *fp;
17970
17971 if (name != NULL && isdigit(*name))
17972 {
17973 fp = find_func(name);
17974 if (fp == NULL)
17975 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017976 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017977 {
17978 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017979 * when "uf_calls" becomes zero. */
17980 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017981 func_free(fp);
17982 }
17983 }
17984}
17985
17986/*
17987 * Count a reference to a Function.
17988 */
17989 static void
17990func_ref(name)
17991 char_u *name;
17992{
17993 ufunc_T *fp;
17994
17995 if (name != NULL && isdigit(*name))
17996 {
17997 fp = find_func(name);
17998 if (fp == NULL)
17999 EMSG2(_(e_intern2), "func_ref()");
18000 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018001 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002 }
18003}
18004
18005/*
18006 * Call a user function.
18007 */
18008 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018009call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010 ufunc_T *fp; /* pointer to function */
18011 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018012 typval_T *argvars; /* arguments */
18013 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014 linenr_T firstline; /* first line of range */
18015 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018016 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017{
Bram Moolenaar33570922005-01-25 22:26:29 +000018018 char_u *save_sourcing_name;
18019 linenr_T save_sourcing_lnum;
18020 scid_T save_current_SID;
18021 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018022 int save_did_emsg;
18023 static int depth = 0;
18024 dictitem_T *v;
18025 int fixvar_idx = 0; /* index in fixvar[] */
18026 int i;
18027 int ai;
18028 char_u numbuf[NUMBUFLEN];
18029 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018030#ifdef FEAT_PROFILE
18031 proftime_T wait_start;
18032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033
18034 /* If depth of calling is getting too high, don't execute the function */
18035 if (depth >= p_mfd)
18036 {
18037 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018038 rettv->v_type = VAR_NUMBER;
18039 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 return;
18041 }
18042 ++depth;
18043
18044 line_breakcheck(); /* check for CTRL-C hit */
18045
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018046 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018047 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018049 fc.rettv = rettv;
18050 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051 fc.linenr = 0;
18052 fc.returned = FALSE;
18053 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018055 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056 fc.dbg_tick = debug_tick;
18057
Bram Moolenaar33570922005-01-25 22:26:29 +000018058 /*
18059 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18060 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18061 * each argument variable and saves a lot of time.
18062 */
18063 /*
18064 * Init l: variables.
18065 */
18066 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018067 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018068 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018069 /* Set l:self to "selfdict". */
18070 v = &fc.fixvar[fixvar_idx++].var;
18071 STRCPY(v->di_key, "self");
18072 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18073 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18074 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018075 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018076 v->di_tv.vval.v_dict = selfdict;
18077 ++selfdict->dv_refcount;
18078 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018079
Bram Moolenaar33570922005-01-25 22:26:29 +000018080 /*
18081 * Init a: variables.
18082 * Set a:0 to "argcount".
18083 * Set a:000 to a list with room for the "..." arguments.
18084 */
18085 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18086 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018087 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018088 v = &fc.fixvar[fixvar_idx++].var;
18089 STRCPY(v->di_key, "000");
18090 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18091 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18092 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018093 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018094 v->di_tv.vval.v_list = &fc.l_varlist;
18095 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18096 fc.l_varlist.lv_refcount = 99999;
18097
18098 /*
18099 * Set a:firstline to "firstline" and a:lastline to "lastline".
18100 * Set a:name to named arguments.
18101 * Set a:N to the "..." arguments.
18102 */
18103 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18104 (varnumber_T)firstline);
18105 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18106 (varnumber_T)lastline);
18107 for (i = 0; i < argcount; ++i)
18108 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018109 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018110 if (ai < 0)
18111 /* named argument a:name */
18112 name = FUNCARG(fp, i);
18113 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018114 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018115 /* "..." argument a:1, a:2, etc. */
18116 sprintf((char *)numbuf, "%d", ai + 1);
18117 name = numbuf;
18118 }
18119 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18120 {
18121 v = &fc.fixvar[fixvar_idx++].var;
18122 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18123 }
18124 else
18125 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018126 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18127 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018128 if (v == NULL)
18129 break;
18130 v->di_flags = DI_FLAGS_RO;
18131 }
18132 STRCPY(v->di_key, name);
18133 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18134
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018135 /* Note: the values are copied directly to avoid alloc/free.
18136 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018137 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018138 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018139
18140 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18141 {
18142 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18143 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018144 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018145 }
18146 }
18147
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 /* Don't redraw while executing the function. */
18149 ++RedrawingDisabled;
18150 save_sourcing_name = sourcing_name;
18151 save_sourcing_lnum = sourcing_lnum;
18152 sourcing_lnum = 1;
18153 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018154 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155 if (sourcing_name != NULL)
18156 {
18157 if (save_sourcing_name != NULL
18158 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18159 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18160 else
18161 STRCPY(sourcing_name, "function ");
18162 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18163
18164 if (p_verbose >= 12)
18165 {
18166 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018167 verbose_enter_scroll();
18168
Bram Moolenaar555b2802005-05-19 21:08:39 +000018169 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 if (p_verbose >= 14)
18171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018173 char_u numbuf[NUMBUFLEN];
18174 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175
18176 msg_puts((char_u *)"(");
18177 for (i = 0; i < argcount; ++i)
18178 {
18179 if (i > 0)
18180 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018181 if (argvars[i].v_type == VAR_NUMBER)
18182 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183 else
18184 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018185 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018186 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018188 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189 }
18190 }
18191 msg_puts((char_u *)")");
18192 }
18193 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018194
18195 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196 --no_wait_return;
18197 }
18198 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018199#ifdef FEAT_PROFILE
18200 if (do_profiling)
18201 {
18202 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18203 func_do_profile(fp);
18204 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018205 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018206 {
18207 ++fp->uf_tm_count;
18208 profile_start(&fp->uf_tm_start);
18209 profile_zero(&fp->uf_tm_children);
18210 }
18211 script_prof_save(&wait_start);
18212 }
18213#endif
18214
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018216 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 save_did_emsg = did_emsg;
18218 did_emsg = FALSE;
18219
18220 /* call do_cmdline() to execute the lines */
18221 do_cmdline(NULL, get_func_line, (void *)&fc,
18222 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18223
18224 --RedrawingDisabled;
18225
18226 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018227 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018229 clear_tv(rettv);
18230 rettv->v_type = VAR_NUMBER;
18231 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232 }
18233
Bram Moolenaar05159a02005-02-26 23:04:13 +000018234#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018235 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018236 {
18237 profile_end(&fp->uf_tm_start);
18238 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18239 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18240 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18241 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018242 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018243 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018244 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18245 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018246 }
18247 }
18248#endif
18249
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250 /* when being verbose, mention the return value */
18251 if (p_verbose >= 12)
18252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018254 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018257 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018258 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018259 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18260 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018261 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018263 char_u buf[MSG_BUF_LEN];
18264 char_u numbuf[NUMBUFLEN];
18265 char_u *tofree;
18266
Bram Moolenaar555b2802005-05-19 21:08:39 +000018267 /* The value may be very long. Skip the middle part, so that we
18268 * have some idea how it starts and ends. smsg() would always
18269 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018270 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018271 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018272 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018273 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274 }
18275 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018276
18277 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278 --no_wait_return;
18279 }
18280
18281 vim_free(sourcing_name);
18282 sourcing_name = save_sourcing_name;
18283 sourcing_lnum = save_sourcing_lnum;
18284 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018285#ifdef FEAT_PROFILE
18286 if (do_profiling)
18287 script_prof_restore(&wait_start);
18288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289
18290 if (p_verbose >= 12 && sourcing_name != NULL)
18291 {
18292 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018293 verbose_enter_scroll();
18294
Bram Moolenaar555b2802005-05-19 21:08:39 +000018295 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018297
18298 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299 --no_wait_return;
18300 }
18301
18302 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018303 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304
Bram Moolenaar33570922005-01-25 22:26:29 +000018305 /* The a: variables typevals were not alloced, only free the allocated
18306 * variables. */
18307 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18308
18309 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310 --depth;
18311}
18312
18313/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018314 * Add a number variable "name" to dict "dp" with value "nr".
18315 */
18316 static void
18317add_nr_var(dp, v, name, nr)
18318 dict_T *dp;
18319 dictitem_T *v;
18320 char *name;
18321 varnumber_T nr;
18322{
18323 STRCPY(v->di_key, name);
18324 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18325 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18326 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018327 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018328 v->di_tv.vval.v_number = nr;
18329}
18330
18331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332 * ":return [expr]"
18333 */
18334 void
18335ex_return(eap)
18336 exarg_T *eap;
18337{
18338 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018339 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340 int returning = FALSE;
18341
18342 if (current_funccal == NULL)
18343 {
18344 EMSG(_("E133: :return not inside a function"));
18345 return;
18346 }
18347
18348 if (eap->skip)
18349 ++emsg_skip;
18350
18351 eap->nextcmd = NULL;
18352 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018353 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354 {
18355 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018356 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018358 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359 }
18360 /* It's safer to return also on error. */
18361 else if (!eap->skip)
18362 {
18363 /*
18364 * Return unless the expression evaluation has been cancelled due to an
18365 * aborting error, an interrupt, or an exception.
18366 */
18367 if (!aborting())
18368 returning = do_return(eap, FALSE, TRUE, NULL);
18369 }
18370
18371 /* When skipping or the return gets pending, advance to the next command
18372 * in this line (!returning). Otherwise, ignore the rest of the line.
18373 * Following lines will be ignored by get_func_line(). */
18374 if (returning)
18375 eap->nextcmd = NULL;
18376 else if (eap->nextcmd == NULL) /* no argument */
18377 eap->nextcmd = check_nextcmd(arg);
18378
18379 if (eap->skip)
18380 --emsg_skip;
18381}
18382
18383/*
18384 * Return from a function. Possibly makes the return pending. Also called
18385 * for a pending return at the ":endtry" or after returning from an extra
18386 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018387 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018388 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389 * FALSE when the return gets pending.
18390 */
18391 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018392do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393 exarg_T *eap;
18394 int reanimate;
18395 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018396 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397{
18398 int idx;
18399 struct condstack *cstack = eap->cstack;
18400
18401 if (reanimate)
18402 /* Undo the return. */
18403 current_funccal->returned = FALSE;
18404
18405 /*
18406 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18407 * not in its finally clause (which then is to be executed next) is found.
18408 * In this case, make the ":return" pending for execution at the ":endtry".
18409 * Otherwise, return normally.
18410 */
18411 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18412 if (idx >= 0)
18413 {
18414 cstack->cs_pending[idx] = CSTP_RETURN;
18415
18416 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018417 /* A pending return again gets pending. "rettv" points to an
18418 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018420 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421 else
18422 {
18423 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018424 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018426 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018428 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429 {
18430 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018431 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018432 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018433 else
18434 EMSG(_(e_outofmem));
18435 }
18436 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018437 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438
18439 if (reanimate)
18440 {
18441 /* The pending return value could be overwritten by a ":return"
18442 * without argument in a finally clause; reset the default
18443 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018444 current_funccal->rettv->v_type = VAR_NUMBER;
18445 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446 }
18447 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018448 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449 }
18450 else
18451 {
18452 current_funccal->returned = TRUE;
18453
18454 /* If the return is carried out now, store the return value. For
18455 * a return immediately after reanimation, the value is already
18456 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018457 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018459 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018460 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018462 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463 }
18464 }
18465
18466 return idx < 0;
18467}
18468
18469/*
18470 * Free the variable with a pending return value.
18471 */
18472 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018473discard_pending_return(rettv)
18474 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475{
Bram Moolenaar33570922005-01-25 22:26:29 +000018476 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477}
18478
18479/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018480 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481 * is an allocated string. Used by report_pending() for verbose messages.
18482 */
18483 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018484get_return_cmd(rettv)
18485 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018487 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018488 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018489 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018491 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018492 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018493 if (s == NULL)
18494 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018495
18496 STRCPY(IObuff, ":return ");
18497 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18498 if (STRLEN(s) + 8 >= IOSIZE)
18499 STRCPY(IObuff + IOSIZE - 4, "...");
18500 vim_free(tofree);
18501 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018502}
18503
18504/*
18505 * Get next function line.
18506 * Called by do_cmdline() to get the next line.
18507 * Returns allocated string, or NULL for end of function.
18508 */
18509/* ARGSUSED */
18510 char_u *
18511get_func_line(c, cookie, indent)
18512 int c; /* not used */
18513 void *cookie;
18514 int indent; /* not used */
18515{
Bram Moolenaar33570922005-01-25 22:26:29 +000018516 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018517 ufunc_T *fp = fcp->func;
18518 char_u *retval;
18519 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520
18521 /* If breakpoints have been added/deleted need to check for it. */
18522 if (fcp->dbg_tick != debug_tick)
18523 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018524 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525 sourcing_lnum);
18526 fcp->dbg_tick = debug_tick;
18527 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018528#ifdef FEAT_PROFILE
18529 if (do_profiling)
18530 func_line_end(cookie);
18531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532
Bram Moolenaar05159a02005-02-26 23:04:13 +000018533 gap = &fp->uf_lines;
18534 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535 retval = NULL;
18536 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18537 retval = NULL;
18538 else
18539 {
18540 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18541 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018542#ifdef FEAT_PROFILE
18543 if (do_profiling)
18544 func_line_start(cookie);
18545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546 }
18547
18548 /* Did we encounter a breakpoint? */
18549 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18550 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018551 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018553 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554 sourcing_lnum);
18555 fcp->dbg_tick = debug_tick;
18556 }
18557
18558 return retval;
18559}
18560
Bram Moolenaar05159a02005-02-26 23:04:13 +000018561#if defined(FEAT_PROFILE) || defined(PROTO)
18562/*
18563 * Called when starting to read a function line.
18564 * "sourcing_lnum" must be correct!
18565 * When skipping lines it may not actually be executed, but we won't find out
18566 * until later and we need to store the time now.
18567 */
18568 void
18569func_line_start(cookie)
18570 void *cookie;
18571{
18572 funccall_T *fcp = (funccall_T *)cookie;
18573 ufunc_T *fp = fcp->func;
18574
18575 if (fp->uf_profiling && sourcing_lnum >= 1
18576 && sourcing_lnum <= fp->uf_lines.ga_len)
18577 {
18578 fp->uf_tml_idx = sourcing_lnum - 1;
18579 fp->uf_tml_execed = FALSE;
18580 profile_start(&fp->uf_tml_start);
18581 profile_zero(&fp->uf_tml_children);
18582 profile_get_wait(&fp->uf_tml_wait);
18583 }
18584}
18585
18586/*
18587 * Called when actually executing a function line.
18588 */
18589 void
18590func_line_exec(cookie)
18591 void *cookie;
18592{
18593 funccall_T *fcp = (funccall_T *)cookie;
18594 ufunc_T *fp = fcp->func;
18595
18596 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18597 fp->uf_tml_execed = TRUE;
18598}
18599
18600/*
18601 * Called when done with a function line.
18602 */
18603 void
18604func_line_end(cookie)
18605 void *cookie;
18606{
18607 funccall_T *fcp = (funccall_T *)cookie;
18608 ufunc_T *fp = fcp->func;
18609
18610 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18611 {
18612 if (fp->uf_tml_execed)
18613 {
18614 ++fp->uf_tml_count[fp->uf_tml_idx];
18615 profile_end(&fp->uf_tml_start);
18616 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18617 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18618 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18619 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18620 }
18621 fp->uf_tml_idx = -1;
18622 }
18623}
18624#endif
18625
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626/*
18627 * Return TRUE if the currently active function should be ended, because a
18628 * return was encountered or an error occured. Used inside a ":while".
18629 */
18630 int
18631func_has_ended(cookie)
18632 void *cookie;
18633{
Bram Moolenaar33570922005-01-25 22:26:29 +000018634 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635
18636 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18637 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018638 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639 || fcp->returned);
18640}
18641
18642/*
18643 * return TRUE if cookie indicates a function which "abort"s on errors.
18644 */
18645 int
18646func_has_abort(cookie)
18647 void *cookie;
18648{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018649 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650}
18651
18652#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18653typedef enum
18654{
18655 VAR_FLAVOUR_DEFAULT,
18656 VAR_FLAVOUR_SESSION,
18657 VAR_FLAVOUR_VIMINFO
18658} var_flavour_T;
18659
18660static var_flavour_T var_flavour __ARGS((char_u *varname));
18661
18662 static var_flavour_T
18663var_flavour(varname)
18664 char_u *varname;
18665{
18666 char_u *p = varname;
18667
18668 if (ASCII_ISUPPER(*p))
18669 {
18670 while (*(++p))
18671 if (ASCII_ISLOWER(*p))
18672 return VAR_FLAVOUR_SESSION;
18673 return VAR_FLAVOUR_VIMINFO;
18674 }
18675 else
18676 return VAR_FLAVOUR_DEFAULT;
18677}
18678#endif
18679
18680#if defined(FEAT_VIMINFO) || defined(PROTO)
18681/*
18682 * Restore global vars that start with a capital from the viminfo file
18683 */
18684 int
18685read_viminfo_varlist(virp, writing)
18686 vir_T *virp;
18687 int writing;
18688{
18689 char_u *tab;
18690 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018691 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018692
18693 if (!writing && (find_viminfo_parameter('!') != NULL))
18694 {
18695 tab = vim_strchr(virp->vir_line + 1, '\t');
18696 if (tab != NULL)
18697 {
18698 *tab++ = '\0'; /* isolate the variable name */
18699 if (*tab == 'S') /* string var */
18700 is_string = TRUE;
18701
18702 tab = vim_strchr(tab, '\t');
18703 if (tab != NULL)
18704 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 if (is_string)
18706 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018707 tv.v_type = VAR_STRING;
18708 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018710 }
18711 else
18712 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018713 tv.v_type = VAR_NUMBER;
18714 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018716 set_var(virp->vir_line + 1, &tv, FALSE);
18717 if (is_string)
18718 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719 }
18720 }
18721 }
18722
18723 return viminfo_readline(virp);
18724}
18725
18726/*
18727 * Write global vars that start with a capital to the viminfo file
18728 */
18729 void
18730write_viminfo_varlist(fp)
18731 FILE *fp;
18732{
Bram Moolenaar33570922005-01-25 22:26:29 +000018733 hashitem_T *hi;
18734 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018735 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018736 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018737 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018738 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018739 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740
18741 if (find_viminfo_parameter('!') == NULL)
18742 return;
18743
18744 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018745
Bram Moolenaar33570922005-01-25 22:26:29 +000018746 todo = globvarht.ht_used;
18747 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018749 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018751 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018752 this_var = HI2DI(hi);
18753 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018754 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018755 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018756 {
18757 case VAR_STRING: s = "STR"; break;
18758 case VAR_NUMBER: s = "NUM"; break;
18759 default: continue;
18760 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018761 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018762 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18763 if (p != NULL)
18764 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018765 vim_free(tofree);
18766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767 }
18768 }
18769}
18770#endif
18771
18772#if defined(FEAT_SESSION) || defined(PROTO)
18773 int
18774store_session_globals(fd)
18775 FILE *fd;
18776{
Bram Moolenaar33570922005-01-25 22:26:29 +000018777 hashitem_T *hi;
18778 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018779 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780 char_u *p, *t;
18781
Bram Moolenaar33570922005-01-25 22:26:29 +000018782 todo = globvarht.ht_used;
18783 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018785 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018787 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018788 this_var = HI2DI(hi);
18789 if ((this_var->di_tv.v_type == VAR_NUMBER
18790 || this_var->di_tv.v_type == VAR_STRING)
18791 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018792 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018793 /* Escape special characters with a backslash. Turn a LF and
18794 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018795 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018796 (char_u *)"\\\"\n\r");
18797 if (p == NULL) /* out of memory */
18798 break;
18799 for (t = p; *t != NUL; ++t)
18800 if (*t == '\n')
18801 *t = 'n';
18802 else if (*t == '\r')
18803 *t = 'r';
18804 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000018805 this_var->di_key,
18806 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18807 : ' ',
18808 p,
18809 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18810 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000018811 || put_eol(fd) == FAIL)
18812 {
18813 vim_free(p);
18814 return FAIL;
18815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816 vim_free(p);
18817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818 }
18819 }
18820 return OK;
18821}
18822#endif
18823
Bram Moolenaar661b1822005-07-28 22:36:45 +000018824/*
18825 * Display script name where an item was last set.
18826 * Should only be invoked when 'verbose' is non-zero.
18827 */
18828 void
18829last_set_msg(scriptID)
18830 scid_T scriptID;
18831{
18832 if (scriptID != 0)
18833 {
18834 verbose_enter();
18835 MSG_PUTS(_("\n\tLast set from "));
18836 MSG_PUTS(get_scriptname(scriptID));
18837 verbose_leave();
18838 }
18839}
18840
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841#endif /* FEAT_EVAL */
18842
18843#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
18844
18845
18846#ifdef WIN3264
18847/*
18848 * Functions for ":8" filename modifier: get 8.3 version of a filename.
18849 */
18850static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18851static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
18852static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18853
18854/*
18855 * Get the short pathname of a file.
18856 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
18857 */
18858 static int
18859get_short_pathname(fnamep, bufp, fnamelen)
18860 char_u **fnamep;
18861 char_u **bufp;
18862 int *fnamelen;
18863{
18864 int l,len;
18865 char_u *newbuf;
18866
18867 len = *fnamelen;
18868
18869 l = GetShortPathName(*fnamep, *fnamep, len);
18870 if (l > len - 1)
18871 {
18872 /* If that doesn't work (not enough space), then save the string
18873 * and try again with a new buffer big enough
18874 */
18875 newbuf = vim_strnsave(*fnamep, l);
18876 if (newbuf == NULL)
18877 return 0;
18878
18879 vim_free(*bufp);
18880 *fnamep = *bufp = newbuf;
18881
18882 l = GetShortPathName(*fnamep,*fnamep,l+1);
18883
18884 /* Really should always succeed, as the buffer is big enough */
18885 }
18886
18887 *fnamelen = l;
18888 return 1;
18889}
18890
18891/*
18892 * Create a short path name. Returns the length of the buffer it needs.
18893 * Doesn't copy over the end of the buffer passed in.
18894 */
18895 static int
18896shortpath_for_invalid_fname(fname, bufp, fnamelen)
18897 char_u **fname;
18898 char_u **bufp;
18899 int *fnamelen;
18900{
18901 char_u *s, *p, *pbuf2, *pbuf3;
18902 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018903 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904
18905 /* Make a copy */
18906 len2 = *fnamelen;
18907 pbuf2 = vim_strnsave(*fname, len2);
18908 pbuf3 = NULL;
18909
18910 s = pbuf2 + len2 - 1; /* Find the end */
18911 slen = 1;
18912 plen = len2;
18913
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018914 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 {
18916 --s;
18917 ++slen;
18918 --plen;
18919 }
18920
18921 do
18922 {
18923 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018924 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018925 {
18926 --s;
18927 ++slen;
18928 --plen;
18929 }
18930 if (s <= pbuf2)
18931 break;
18932
18933 /* Remeber the character that is about to be blatted */
18934 ch = *s;
18935 *s = 0; /* get_short_pathname requires a null-terminated string */
18936
18937 /* Try it in situ */
18938 p = pbuf2;
18939 if (!get_short_pathname(&p, &pbuf3, &plen))
18940 {
18941 vim_free(pbuf2);
18942 return -1;
18943 }
18944 *s = ch; /* Preserve the string */
18945 } while (plen == 0);
18946
18947 if (plen > 0)
18948 {
18949 /* Remeber the length of the new string. */
18950 *fnamelen = len = plen + slen;
18951 vim_free(*bufp);
18952 if (len > len2)
18953 {
18954 /* If there's not enough space in the currently allocated string,
18955 * then copy it to a buffer big enough.
18956 */
18957 *fname= *bufp = vim_strnsave(p, len);
18958 if (*fname == NULL)
18959 return -1;
18960 }
18961 else
18962 {
18963 /* Transfer pbuf2 to being the main buffer (it's big enough) */
18964 *fname = *bufp = pbuf2;
18965 if (p != pbuf2)
18966 strncpy(*fname, p, plen);
18967 pbuf2 = NULL;
18968 }
18969 /* Concat the next bit */
18970 strncpy(*fname + plen, s, slen);
18971 (*fname)[len] = '\0';
18972 }
18973 vim_free(pbuf3);
18974 vim_free(pbuf2);
18975 return 0;
18976}
18977
18978/*
18979 * Get a pathname for a partial path.
18980 */
18981 static int
18982shortpath_for_partial(fnamep, bufp, fnamelen)
18983 char_u **fnamep;
18984 char_u **bufp;
18985 int *fnamelen;
18986{
18987 int sepcount, len, tflen;
18988 char_u *p;
18989 char_u *pbuf, *tfname;
18990 int hasTilde;
18991
18992 /* Count up the path seperators from the RHS.. so we know which part
18993 * of the path to return.
18994 */
18995 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018996 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018997 if (vim_ispathsep(*p))
18998 ++sepcount;
18999
19000 /* Need full path first (use expand_env() to remove a "~/") */
19001 hasTilde = (**fnamep == '~');
19002 if (hasTilde)
19003 pbuf = tfname = expand_env_save(*fnamep);
19004 else
19005 pbuf = tfname = FullName_save(*fnamep, FALSE);
19006
19007 len = tflen = STRLEN(tfname);
19008
19009 if (!get_short_pathname(&tfname, &pbuf, &len))
19010 return -1;
19011
19012 if (len == 0)
19013 {
19014 /* Don't have a valid filename, so shorten the rest of the
19015 * path if we can. This CAN give us invalid 8.3 filenames, but
19016 * there's not a lot of point in guessing what it might be.
19017 */
19018 len = tflen;
19019 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19020 return -1;
19021 }
19022
19023 /* Count the paths backward to find the beginning of the desired string. */
19024 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019025 {
19026#ifdef FEAT_MBYTE
19027 if (has_mbyte)
19028 p -= mb_head_off(tfname, p);
19029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019030 if (vim_ispathsep(*p))
19031 {
19032 if (sepcount == 0 || (hasTilde && sepcount == 1))
19033 break;
19034 else
19035 sepcount --;
19036 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038 if (hasTilde)
19039 {
19040 --p;
19041 if (p >= tfname)
19042 *p = '~';
19043 else
19044 return -1;
19045 }
19046 else
19047 ++p;
19048
19049 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19050 vim_free(*bufp);
19051 *fnamelen = (int)STRLEN(p);
19052 *bufp = pbuf;
19053 *fnamep = p;
19054
19055 return 0;
19056}
19057#endif /* WIN3264 */
19058
19059/*
19060 * Adjust a filename, according to a string of modifiers.
19061 * *fnamep must be NUL terminated when called. When returning, the length is
19062 * determined by *fnamelen.
19063 * Returns valid flags.
19064 * When there is an error, *fnamep is set to NULL.
19065 */
19066 int
19067modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19068 char_u *src; /* string with modifiers */
19069 int *usedlen; /* characters after src that are used */
19070 char_u **fnamep; /* file name so far */
19071 char_u **bufp; /* buffer for allocated file name or NULL */
19072 int *fnamelen; /* length of fnamep */
19073{
19074 int valid = 0;
19075 char_u *tail;
19076 char_u *s, *p, *pbuf;
19077 char_u dirname[MAXPATHL];
19078 int c;
19079 int has_fullname = 0;
19080#ifdef WIN3264
19081 int has_shortname = 0;
19082#endif
19083
19084repeat:
19085 /* ":p" - full path/file_name */
19086 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19087 {
19088 has_fullname = 1;
19089
19090 valid |= VALID_PATH;
19091 *usedlen += 2;
19092
19093 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19094 if ((*fnamep)[0] == '~'
19095#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19096 && ((*fnamep)[1] == '/'
19097# ifdef BACKSLASH_IN_FILENAME
19098 || (*fnamep)[1] == '\\'
19099# endif
19100 || (*fnamep)[1] == NUL)
19101
19102#endif
19103 )
19104 {
19105 *fnamep = expand_env_save(*fnamep);
19106 vim_free(*bufp); /* free any allocated file name */
19107 *bufp = *fnamep;
19108 if (*fnamep == NULL)
19109 return -1;
19110 }
19111
19112 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019113 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019114 {
19115 if (vim_ispathsep(*p)
19116 && p[1] == '.'
19117 && (p[2] == NUL
19118 || vim_ispathsep(p[2])
19119 || (p[2] == '.'
19120 && (p[3] == NUL || vim_ispathsep(p[3])))))
19121 break;
19122 }
19123
19124 /* FullName_save() is slow, don't use it when not needed. */
19125 if (*p != NUL || !vim_isAbsName(*fnamep))
19126 {
19127 *fnamep = FullName_save(*fnamep, *p != NUL);
19128 vim_free(*bufp); /* free any allocated file name */
19129 *bufp = *fnamep;
19130 if (*fnamep == NULL)
19131 return -1;
19132 }
19133
19134 /* Append a path separator to a directory. */
19135 if (mch_isdir(*fnamep))
19136 {
19137 /* Make room for one or two extra characters. */
19138 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19139 vim_free(*bufp); /* free any allocated file name */
19140 *bufp = *fnamep;
19141 if (*fnamep == NULL)
19142 return -1;
19143 add_pathsep(*fnamep);
19144 }
19145 }
19146
19147 /* ":." - path relative to the current directory */
19148 /* ":~" - path relative to the home directory */
19149 /* ":8" - shortname path - postponed till after */
19150 while (src[*usedlen] == ':'
19151 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19152 {
19153 *usedlen += 2;
19154 if (c == '8')
19155 {
19156#ifdef WIN3264
19157 has_shortname = 1; /* Postpone this. */
19158#endif
19159 continue;
19160 }
19161 pbuf = NULL;
19162 /* Need full path first (use expand_env() to remove a "~/") */
19163 if (!has_fullname)
19164 {
19165 if (c == '.' && **fnamep == '~')
19166 p = pbuf = expand_env_save(*fnamep);
19167 else
19168 p = pbuf = FullName_save(*fnamep, FALSE);
19169 }
19170 else
19171 p = *fnamep;
19172
19173 has_fullname = 0;
19174
19175 if (p != NULL)
19176 {
19177 if (c == '.')
19178 {
19179 mch_dirname(dirname, MAXPATHL);
19180 s = shorten_fname(p, dirname);
19181 if (s != NULL)
19182 {
19183 *fnamep = s;
19184 if (pbuf != NULL)
19185 {
19186 vim_free(*bufp); /* free any allocated file name */
19187 *bufp = pbuf;
19188 pbuf = NULL;
19189 }
19190 }
19191 }
19192 else
19193 {
19194 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19195 /* Only replace it when it starts with '~' */
19196 if (*dirname == '~')
19197 {
19198 s = vim_strsave(dirname);
19199 if (s != NULL)
19200 {
19201 *fnamep = s;
19202 vim_free(*bufp);
19203 *bufp = s;
19204 }
19205 }
19206 }
19207 vim_free(pbuf);
19208 }
19209 }
19210
19211 tail = gettail(*fnamep);
19212 *fnamelen = (int)STRLEN(*fnamep);
19213
19214 /* ":h" - head, remove "/file_name", can be repeated */
19215 /* Don't remove the first "/" or "c:\" */
19216 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19217 {
19218 valid |= VALID_HEAD;
19219 *usedlen += 2;
19220 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019221 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019222 --tail;
19223 *fnamelen = (int)(tail - *fnamep);
19224#ifdef VMS
19225 if (*fnamelen > 0)
19226 *fnamelen += 1; /* the path separator is part of the path */
19227#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019228 while (tail > s && !after_pathsep(s, tail))
19229 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230 }
19231
19232 /* ":8" - shortname */
19233 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19234 {
19235 *usedlen += 2;
19236#ifdef WIN3264
19237 has_shortname = 1;
19238#endif
19239 }
19240
19241#ifdef WIN3264
19242 /* Check shortname after we have done 'heads' and before we do 'tails'
19243 */
19244 if (has_shortname)
19245 {
19246 pbuf = NULL;
19247 /* Copy the string if it is shortened by :h */
19248 if (*fnamelen < (int)STRLEN(*fnamep))
19249 {
19250 p = vim_strnsave(*fnamep, *fnamelen);
19251 if (p == 0)
19252 return -1;
19253 vim_free(*bufp);
19254 *bufp = *fnamep = p;
19255 }
19256
19257 /* Split into two implementations - makes it easier. First is where
19258 * there isn't a full name already, second is where there is.
19259 */
19260 if (!has_fullname && !vim_isAbsName(*fnamep))
19261 {
19262 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19263 return -1;
19264 }
19265 else
19266 {
19267 int l;
19268
19269 /* Simple case, already have the full-name
19270 * Nearly always shorter, so try first time. */
19271 l = *fnamelen;
19272 if (!get_short_pathname(fnamep, bufp, &l))
19273 return -1;
19274
19275 if (l == 0)
19276 {
19277 /* Couldn't find the filename.. search the paths.
19278 */
19279 l = *fnamelen;
19280 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19281 return -1;
19282 }
19283 *fnamelen = l;
19284 }
19285 }
19286#endif /* WIN3264 */
19287
19288 /* ":t" - tail, just the basename */
19289 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19290 {
19291 *usedlen += 2;
19292 *fnamelen -= (int)(tail - *fnamep);
19293 *fnamep = tail;
19294 }
19295
19296 /* ":e" - extension, can be repeated */
19297 /* ":r" - root, without extension, can be repeated */
19298 while (src[*usedlen] == ':'
19299 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19300 {
19301 /* find a '.' in the tail:
19302 * - for second :e: before the current fname
19303 * - otherwise: The last '.'
19304 */
19305 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19306 s = *fnamep - 2;
19307 else
19308 s = *fnamep + *fnamelen - 1;
19309 for ( ; s > tail; --s)
19310 if (s[0] == '.')
19311 break;
19312 if (src[*usedlen + 1] == 'e') /* :e */
19313 {
19314 if (s > tail)
19315 {
19316 *fnamelen += (int)(*fnamep - (s + 1));
19317 *fnamep = s + 1;
19318#ifdef VMS
19319 /* cut version from the extension */
19320 s = *fnamep + *fnamelen - 1;
19321 for ( ; s > *fnamep; --s)
19322 if (s[0] == ';')
19323 break;
19324 if (s > *fnamep)
19325 *fnamelen = s - *fnamep;
19326#endif
19327 }
19328 else if (*fnamep <= tail)
19329 *fnamelen = 0;
19330 }
19331 else /* :r */
19332 {
19333 if (s > tail) /* remove one extension */
19334 *fnamelen = (int)(s - *fnamep);
19335 }
19336 *usedlen += 2;
19337 }
19338
19339 /* ":s?pat?foo?" - substitute */
19340 /* ":gs?pat?foo?" - global substitute */
19341 if (src[*usedlen] == ':'
19342 && (src[*usedlen + 1] == 's'
19343 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19344 {
19345 char_u *str;
19346 char_u *pat;
19347 char_u *sub;
19348 int sep;
19349 char_u *flags;
19350 int didit = FALSE;
19351
19352 flags = (char_u *)"";
19353 s = src + *usedlen + 2;
19354 if (src[*usedlen + 1] == 'g')
19355 {
19356 flags = (char_u *)"g";
19357 ++s;
19358 }
19359
19360 sep = *s++;
19361 if (sep)
19362 {
19363 /* find end of pattern */
19364 p = vim_strchr(s, sep);
19365 if (p != NULL)
19366 {
19367 pat = vim_strnsave(s, (int)(p - s));
19368 if (pat != NULL)
19369 {
19370 s = p + 1;
19371 /* find end of substitution */
19372 p = vim_strchr(s, sep);
19373 if (p != NULL)
19374 {
19375 sub = vim_strnsave(s, (int)(p - s));
19376 str = vim_strnsave(*fnamep, *fnamelen);
19377 if (sub != NULL && str != NULL)
19378 {
19379 *usedlen = (int)(p + 1 - src);
19380 s = do_string_sub(str, pat, sub, flags);
19381 if (s != NULL)
19382 {
19383 *fnamep = s;
19384 *fnamelen = (int)STRLEN(s);
19385 vim_free(*bufp);
19386 *bufp = s;
19387 didit = TRUE;
19388 }
19389 }
19390 vim_free(sub);
19391 vim_free(str);
19392 }
19393 vim_free(pat);
19394 }
19395 }
19396 /* after using ":s", repeat all the modifiers */
19397 if (didit)
19398 goto repeat;
19399 }
19400 }
19401
19402 return valid;
19403}
19404
19405/*
19406 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19407 * "flags" can be "g" to do a global substitute.
19408 * Returns an allocated string, NULL for error.
19409 */
19410 char_u *
19411do_string_sub(str, pat, sub, flags)
19412 char_u *str;
19413 char_u *pat;
19414 char_u *sub;
19415 char_u *flags;
19416{
19417 int sublen;
19418 regmatch_T regmatch;
19419 int i;
19420 int do_all;
19421 char_u *tail;
19422 garray_T ga;
19423 char_u *ret;
19424 char_u *save_cpo;
19425
19426 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19427 save_cpo = p_cpo;
19428 p_cpo = (char_u *)"";
19429
19430 ga_init2(&ga, 1, 200);
19431
19432 do_all = (flags[0] == 'g');
19433
19434 regmatch.rm_ic = p_ic;
19435 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19436 if (regmatch.regprog != NULL)
19437 {
19438 tail = str;
19439 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19440 {
19441 /*
19442 * Get some space for a temporary buffer to do the substitution
19443 * into. It will contain:
19444 * - The text up to where the match is.
19445 * - The substituted text.
19446 * - The text after the match.
19447 */
19448 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19449 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19450 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19451 {
19452 ga_clear(&ga);
19453 break;
19454 }
19455
19456 /* copy the text up to where the match is */
19457 i = (int)(regmatch.startp[0] - tail);
19458 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19459 /* add the substituted text */
19460 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19461 + ga.ga_len + i, TRUE, TRUE, FALSE);
19462 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 /* avoid getting stuck on a match with an empty string */
19464 if (tail == regmatch.endp[0])
19465 {
19466 if (*tail == NUL)
19467 break;
19468 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19469 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470 }
19471 else
19472 {
19473 tail = regmatch.endp[0];
19474 if (*tail == NUL)
19475 break;
19476 }
19477 if (!do_all)
19478 break;
19479 }
19480
19481 if (ga.ga_data != NULL)
19482 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19483
19484 vim_free(regmatch.regprog);
19485 }
19486
19487 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19488 ga_clear(&ga);
19489 p_cpo = save_cpo;
19490
19491 return ret;
19492}
19493
19494#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */