blob: 596fa526cf5bc1e5657b255c6654a30307358658 [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));
463static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
468static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000494static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000496static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000509static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000537static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000554static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000558#ifdef vim_mkdir
559static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
560#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000565static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000567static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000585static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000590static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000591static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
594#ifdef HAVE_STRFTIME
595static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
596#endif
597static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000609static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000610static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000625static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
628static int get_env_len __ARGS((char_u **arg));
629static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000630static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000631static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
632#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
633#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
634 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000635static 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 +0000636static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000637static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000638static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
639static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static typval_T *alloc_tv __ARGS((void));
641static typval_T *alloc_string_tv __ARGS((char_u *string));
642static void free_tv __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void init_tv __ARGS((typval_T *varp));
644static long get_tv_number __ARGS((typval_T *varp));
645static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000646static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static char_u *get_tv_string __ARGS((typval_T *varp));
648static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000649static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000651static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
653static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
654static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
655static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
656static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
657static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
658static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000659static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000661static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
663static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
664static int eval_fname_script __ARGS((char_u *p));
665static int eval_fname_sid __ARGS((char_u *p));
666static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static ufunc_T *find_func __ARGS((char_u *name));
668static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000669static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000670#ifdef FEAT_PROFILE
671static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000672static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
673static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
674static int
675# ifdef __BORLANDC__
676 _RTLENTRYF
677# endif
678 prof_total_cmp __ARGS((const void *s1, const void *s2));
679static int
680# ifdef __BORLANDC__
681 _RTLENTRYF
682# endif
683 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000684#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000685static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000686static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000687static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void func_free __ARGS((ufunc_T *fp));
689static void func_unref __ARGS((char_u *name));
690static void func_ref __ARGS((char_u *name));
691static 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));
692static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
693
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000694/* Character used as separated in autoload function/variable names. */
695#define AUTOLOAD_CHAR '#'
696
Bram Moolenaar33570922005-01-25 22:26:29 +0000697/*
698 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000699 */
700 void
701eval_init()
702{
Bram Moolenaar33570922005-01-25 22:26:29 +0000703 int i;
704 struct vimvar *p;
705
706 init_var_dict(&globvardict, &globvars_var);
707 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000708 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000709 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000710
711 for (i = 0; i < VV_LEN; ++i)
712 {
713 p = &vimvars[i];
714 STRCPY(p->vv_di.di_key, p->vv_name);
715 if (p->vv_flags & VV_RO)
716 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
717 else if (p->vv_flags & VV_RO_SBX)
718 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
719 else
720 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000721
722 /* add to v: scope dict, unless the value is not always available */
723 if (p->vv_type != VAR_UNKNOWN)
724 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000725 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000726 /* add to compat scope dict */
727 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000728 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000729}
730
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000731#if defined(EXITFREE) || defined(PROTO)
732 void
733eval_clear()
734{
735 int i;
736 struct vimvar *p;
737
738 for (i = 0; i < VV_LEN; ++i)
739 {
740 p = &vimvars[i];
741 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000742 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000743 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000744 p->vv_di.di_tv.vval.v_string = NULL;
745 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000746 }
747 hash_clear(&vimvarht);
748 hash_clear(&compat_hashtab);
749
750 /* script-local variables */
751 for (i = 1; i <= ga_scripts.ga_len; ++i)
752 vars_clear(&SCRIPT_VARS(i));
753 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000754 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000755
756 /* global variables */
757 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000758
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000759 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000760 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000761 hash_clear(&func_hashtab);
762
763 /* unreferenced lists and dicts */
764 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000765}
766#endif
767
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 * Return the name of the executed function.
770 */
771 char_u *
772func_name(cookie)
773 void *cookie;
774{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000775 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776}
777
778/*
779 * Return the address holding the next breakpoint line for a funccall cookie.
780 */
781 linenr_T *
782func_breakpoint(cookie)
783 void *cookie;
784{
Bram Moolenaar33570922005-01-25 22:26:29 +0000785 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786}
787
788/*
789 * Return the address holding the debug tick for a funccall cookie.
790 */
791 int *
792func_dbg_tick(cookie)
793 void *cookie;
794{
Bram Moolenaar33570922005-01-25 22:26:29 +0000795 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797
798/*
799 * Return the nesting level for a funccall cookie.
800 */
801 int
802func_level(cookie)
803 void *cookie;
804{
Bram Moolenaar33570922005-01-25 22:26:29 +0000805 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806}
807
808/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000809funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
811/*
812 * Return TRUE when a function was ended by a ":return" command.
813 */
814 int
815current_func_returned()
816{
817 return current_funccal->returned;
818}
819
820
821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 * Set an internal variable to a string value. Creates the variable if it does
823 * not already exist.
824 */
825 void
826set_internal_string_var(name, value)
827 char_u *name;
828 char_u *value;
829{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000830 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000831 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
833 val = vim_strsave(value);
834 if (val != NULL)
835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000836 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000837 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000839 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000840 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 }
842 }
843}
844
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000845static lval_T *redir_lval = NULL;
846static char_u *redir_endp = NULL;
847static char_u *redir_varname = NULL;
848
849/*
850 * Start recording command output to a variable
851 * Returns OK if successfully completed the setup. FAIL otherwise.
852 */
853 int
854var_redir_start(name, append)
855 char_u *name;
856 int append; /* append to an existing variable */
857{
858 int save_emsg;
859 int err;
860 typval_T tv;
861
862 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000863 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000864 {
865 EMSG(_(e_invarg));
866 return FAIL;
867 }
868
869 redir_varname = vim_strsave(name);
870 if (redir_varname == NULL)
871 return FAIL;
872
873 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
874 if (redir_lval == NULL)
875 {
876 var_redir_stop();
877 return FAIL;
878 }
879
880 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000881 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
882 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000883 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
884 {
885 if (redir_endp != NULL && *redir_endp != NUL)
886 /* Trailing characters are present after the variable name */
887 EMSG(_(e_trailing));
888 else
889 EMSG(_(e_invarg));
890 var_redir_stop();
891 return FAIL;
892 }
893
894 /* check if we can write to the variable: set it to or append an empty
895 * string */
896 save_emsg = did_emsg;
897 did_emsg = FALSE;
898 tv.v_type = VAR_STRING;
899 tv.vval.v_string = (char_u *)"";
900 if (append)
901 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
902 else
903 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
904 err = did_emsg;
905 did_emsg += save_emsg;
906 if (err)
907 {
908 var_redir_stop();
909 return FAIL;
910 }
911 if (redir_lval->ll_newkey != NULL)
912 {
913 /* Dictionary item was created, don't do it again. */
914 vim_free(redir_lval->ll_newkey);
915 redir_lval->ll_newkey = NULL;
916 }
917
918 return OK;
919}
920
921/*
922 * Append "value[len]" to the variable set by var_redir_start().
923 */
924 void
925var_redir_str(value, len)
926 char_u *value;
927 int len;
928{
929 char_u *val;
930 typval_T tv;
931 int save_emsg;
932 int err;
933
934 if (redir_lval == NULL)
935 return;
936
937 if (len == -1)
938 /* Append the entire string */
939 val = vim_strsave(value);
940 else
941 /* Append only the specified number of characters */
942 val = vim_strnsave(value, len);
943 if (val == NULL)
944 return;
945
946 tv.v_type = VAR_STRING;
947 tv.vval.v_string = val;
948
949 save_emsg = did_emsg;
950 did_emsg = FALSE;
951 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
952 err = did_emsg;
953 did_emsg += save_emsg;
954 if (err)
955 var_redir_stop();
956
957 vim_free(tv.vval.v_string);
958}
959
960/*
961 * Stop redirecting command output to a variable.
962 */
963 void
964var_redir_stop()
965{
966 if (redir_lval != NULL)
967 {
968 clear_lval(redir_lval);
969 vim_free(redir_lval);
970 redir_lval = NULL;
971 }
972 vim_free(redir_varname);
973 redir_varname = NULL;
974}
975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976# if defined(FEAT_MBYTE) || defined(PROTO)
977 int
978eval_charconvert(enc_from, enc_to, fname_from, fname_to)
979 char_u *enc_from;
980 char_u *enc_to;
981 char_u *fname_from;
982 char_u *fname_to;
983{
984 int err = FALSE;
985
986 set_vim_var_string(VV_CC_FROM, enc_from, -1);
987 set_vim_var_string(VV_CC_TO, enc_to, -1);
988 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
989 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
990 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
991 err = TRUE;
992 set_vim_var_string(VV_CC_FROM, NULL, -1);
993 set_vim_var_string(VV_CC_TO, NULL, -1);
994 set_vim_var_string(VV_FNAME_IN, NULL, -1);
995 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
996
997 if (err)
998 return FAIL;
999 return OK;
1000}
1001# endif
1002
1003# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1004 int
1005eval_printexpr(fname, args)
1006 char_u *fname;
1007 char_u *args;
1008{
1009 int err = FALSE;
1010
1011 set_vim_var_string(VV_FNAME_IN, fname, -1);
1012 set_vim_var_string(VV_CMDARG, args, -1);
1013 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1014 err = TRUE;
1015 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1016 set_vim_var_string(VV_CMDARG, NULL, -1);
1017
1018 if (err)
1019 {
1020 mch_remove(fname);
1021 return FAIL;
1022 }
1023 return OK;
1024}
1025# endif
1026
1027# if defined(FEAT_DIFF) || defined(PROTO)
1028 void
1029eval_diff(origfile, newfile, outfile)
1030 char_u *origfile;
1031 char_u *newfile;
1032 char_u *outfile;
1033{
1034 int err = FALSE;
1035
1036 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1037 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1038 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1039 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1040 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1041 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1042 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1043}
1044
1045 void
1046eval_patch(origfile, difffile, outfile)
1047 char_u *origfile;
1048 char_u *difffile;
1049 char_u *outfile;
1050{
1051 int err;
1052
1053 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1054 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1055 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1056 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1057 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1058 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1059 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1060}
1061# endif
1062
1063/*
1064 * Top level evaluation function, returning a boolean.
1065 * Sets "error" to TRUE if there was an error.
1066 * Return TRUE or FALSE.
1067 */
1068 int
1069eval_to_bool(arg, error, nextcmd, skip)
1070 char_u *arg;
1071 int *error;
1072 char_u **nextcmd;
1073 int skip; /* only parse, don't execute */
1074{
Bram Moolenaar33570922005-01-25 22:26:29 +00001075 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 int retval = FALSE;
1077
1078 if (skip)
1079 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001080 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 else
1083 {
1084 *error = FALSE;
1085 if (!skip)
1086 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001087 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001088 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090 }
1091 if (skip)
1092 --emsg_skip;
1093
1094 return retval;
1095}
1096
1097/*
1098 * Top level evaluation function, returning a string. If "skip" is TRUE,
1099 * only parsing to "nextcmd" is done, without reporting errors. Return
1100 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1101 */
1102 char_u *
1103eval_to_string_skip(arg, nextcmd, skip)
1104 char_u *arg;
1105 char_u **nextcmd;
1106 int skip; /* only parse, don't execute */
1107{
Bram Moolenaar33570922005-01-25 22:26:29 +00001108 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 char_u *retval;
1110
1111 if (skip)
1112 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001113 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 retval = NULL;
1115 else
1116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001117 retval = vim_strsave(get_tv_string(&tv));
1118 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
1120 if (skip)
1121 --emsg_skip;
1122
1123 return retval;
1124}
1125
1126/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001127 * Skip over an expression at "*pp".
1128 * Return FAIL for an error, OK otherwise.
1129 */
1130 int
1131skip_expr(pp)
1132 char_u **pp;
1133{
Bram Moolenaar33570922005-01-25 22:26:29 +00001134 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001135
1136 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001137 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001138}
1139
1140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 * Top level evaluation function, returning a string.
1142 * Return pointer to allocated memory, or NULL for failure.
1143 */
1144 char_u *
1145eval_to_string(arg, nextcmd)
1146 char_u *arg;
1147 char_u **nextcmd;
1148{
Bram Moolenaar33570922005-01-25 22:26:29 +00001149 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 char_u *retval;
1151
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001152 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 retval = NULL;
1154 else
1155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001156 retval = vim_strsave(get_tv_string(&tv));
1157 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 }
1159
1160 return retval;
1161}
1162
1163/*
1164 * Call eval_to_string() with "sandbox" set and not using local variables.
1165 */
1166 char_u *
1167eval_to_string_safe(arg, nextcmd)
1168 char_u *arg;
1169 char_u **nextcmd;
1170{
1171 char_u *retval;
1172 void *save_funccalp;
1173
1174 save_funccalp = save_funccal();
1175 ++sandbox;
1176 retval = eval_to_string(arg, nextcmd);
1177 --sandbox;
1178 restore_funccal(save_funccalp);
1179 return retval;
1180}
1181
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182/*
1183 * Top level evaluation function, returning a number.
1184 * Evaluates "expr" silently.
1185 * Returns -1 for an error.
1186 */
1187 int
1188eval_to_number(expr)
1189 char_u *expr;
1190{
Bram Moolenaar33570922005-01-25 22:26:29 +00001191 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001193 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
1195 ++emsg_off;
1196
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001197 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 retval = -1;
1199 else
1200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001201 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 }
1204 --emsg_off;
1205
1206 return retval;
1207}
1208
Bram Moolenaara40058a2005-07-11 22:42:07 +00001209/*
1210 * Prepare v: variable "idx" to be used.
1211 * Save the current typeval in "save_tv".
1212 * When not used yet add the variable to the v: hashtable.
1213 */
1214 static void
1215prepare_vimvar(idx, save_tv)
1216 int idx;
1217 typval_T *save_tv;
1218{
1219 *save_tv = vimvars[idx].vv_tv;
1220 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1221 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1222}
1223
1224/*
1225 * Restore v: variable "idx" to typeval "save_tv".
1226 * When no longer defined, remove the variable from the v: hashtable.
1227 */
1228 static void
1229restore_vimvar(idx, save_tv)
1230 int idx;
1231 typval_T *save_tv;
1232{
1233 hashitem_T *hi;
1234
1235 clear_tv(&vimvars[idx].vv_tv);
1236 vimvars[idx].vv_tv = *save_tv;
1237 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1238 {
1239 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1240 if (HASHITEM_EMPTY(hi))
1241 EMSG2(_(e_intern2), "restore_vimvar()");
1242 else
1243 hash_remove(&vimvarht, hi);
1244 }
1245}
1246
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001247#if defined(FEAT_SYN_HL) || defined(PROTO)
1248/*
1249 * Evaluate an expression to a list with suggestions.
1250 * For the "expr:" part of 'spellsuggest'.
1251 */
1252 list_T *
1253eval_spell_expr(badword, expr)
1254 char_u *badword;
1255 char_u *expr;
1256{
1257 typval_T save_val;
1258 typval_T rettv;
1259 list_T *list = NULL;
1260 char_u *p = skipwhite(expr);
1261
1262 /* Set "v:val" to the bad word. */
1263 prepare_vimvar(VV_VAL, &save_val);
1264 vimvars[VV_VAL].vv_type = VAR_STRING;
1265 vimvars[VV_VAL].vv_str = badword;
1266 if (p_verbose == 0)
1267 ++emsg_off;
1268
1269 if (eval1(&p, &rettv, TRUE) == OK)
1270 {
1271 if (rettv.v_type != VAR_LIST)
1272 clear_tv(&rettv);
1273 else
1274 list = rettv.vval.v_list;
1275 }
1276
1277 if (p_verbose == 0)
1278 --emsg_off;
1279 vimvars[VV_VAL].vv_str = NULL;
1280 restore_vimvar(VV_VAL, &save_val);
1281
1282 return list;
1283}
1284
1285/*
1286 * "list" is supposed to contain two items: a word and a number. Return the
1287 * word in "pp" and the number as the return value.
1288 * Return -1 if anything isn't right.
1289 * Used to get the good word and score from the eval_spell_expr() result.
1290 */
1291 int
1292get_spellword(list, pp)
1293 list_T *list;
1294 char_u **pp;
1295{
1296 listitem_T *li;
1297
1298 li = list->lv_first;
1299 if (li == NULL)
1300 return -1;
1301 *pp = get_tv_string(&li->li_tv);
1302
1303 li = li->li_next;
1304 if (li == NULL)
1305 return -1;
1306 return get_tv_number(&li->li_tv);
1307}
1308#endif
1309
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001310/*
1311 * Top level evaluation function,
1312 */
1313 typval_T *
1314eval_expr(arg, nextcmd)
1315 char_u *arg;
1316 char_u **nextcmd;
1317{
1318 typval_T *tv;
1319
1320 tv = (typval_T *)alloc(sizeof(typval_T));
1321 if (!tv)
1322 return NULL;
1323
1324 if (eval0(arg, tv, nextcmd, TRUE) == FAIL)
1325 {
1326 vim_free(tv);
1327 return NULL;
1328 }
1329
1330 return tv;
1331}
1332
1333
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1335/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001336 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001338 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001340 static int
1341call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *func;
1343 int argc;
1344 char_u **argv;
1345 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
Bram Moolenaar33570922005-01-25 22:26:29 +00001348 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 long n;
1350 int len;
1351 int i;
1352 int doesrange;
1353 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001354 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
Bram Moolenaar33570922005-01-25 22:26:29 +00001356 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001358 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
1360 for (i = 0; i < argc; i++)
1361 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001362 /* Pass a NULL or empty argument as an empty string */
1363 if (argv[i] == NULL || *argv[i] == NUL)
1364 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001365 argvars[i].v_type = VAR_STRING;
1366 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001367 continue;
1368 }
1369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 /* Recognize a number argument, the others must be strings. */
1371 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1372 if (len != 0 && len == (int)STRLEN(argv[i]))
1373 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001374 argvars[i].v_type = VAR_NUMBER;
1375 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 }
1377 else
1378 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001379 argvars[i].v_type = VAR_STRING;
1380 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382 }
1383
1384 if (safe)
1385 {
1386 save_funccalp = save_funccal();
1387 ++sandbox;
1388 }
1389
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001390 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1391 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001393 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 if (safe)
1395 {
1396 --sandbox;
1397 restore_funccal(save_funccalp);
1398 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001399 vim_free(argvars);
1400
1401 if (ret == FAIL)
1402 clear_tv(rettv);
1403
1404 return ret;
1405}
1406
1407/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001408 * Call vimL function "func" and return the result as a string.
1409 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001410 * Uses argv[argc] for the function arguments.
1411 */
1412 void *
1413call_func_retstr(func, argc, argv, safe)
1414 char_u *func;
1415 int argc;
1416 char_u **argv;
1417 int safe; /* use the sandbox */
1418{
1419 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001420 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001421
1422 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1423 return NULL;
1424
1425 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001426 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 return retval;
1428}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001429
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001430#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001431/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001432 * Call vimL function "func" and return the result as a number.
1433 * Returns -1 when calling the function fails.
1434 * Uses argv[argc] for the function arguments.
1435 */
1436 long
1437call_func_retnr(func, argc, argv, safe)
1438 char_u *func;
1439 int argc;
1440 char_u **argv;
1441 int safe; /* use the sandbox */
1442{
1443 typval_T rettv;
1444 long retval;
1445
1446 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1447 return -1;
1448
1449 retval = get_tv_number_chk(&rettv, NULL);
1450 clear_tv(&rettv);
1451 return retval;
1452}
1453#endif
1454
1455/*
1456 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001457 * Uses argv[argc] for the function arguments.
1458 */
1459 void *
1460call_func_retlist(func, argc, argv, safe)
1461 char_u *func;
1462 int argc;
1463 char_u **argv;
1464 int safe; /* use the sandbox */
1465{
1466 typval_T rettv;
1467
1468 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1469 return NULL;
1470
1471 if (rettv.v_type != VAR_LIST)
1472 {
1473 clear_tv(&rettv);
1474 return NULL;
1475 }
1476
1477 return rettv.vval.v_list;
1478}
1479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480#endif
1481
1482/*
1483 * Save the current function call pointer, and set it to NULL.
1484 * Used when executing autocommands and for ":source".
1485 */
1486 void *
1487save_funccal()
1488{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001489 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 current_funccal = NULL;
1492 return (void *)fc;
1493}
1494
1495 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001496restore_funccal(vfc)
1497 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001499 funccall_T *fc = (funccall_T *)vfc;
1500
1501 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502}
1503
Bram Moolenaar05159a02005-02-26 23:04:13 +00001504#if defined(FEAT_PROFILE) || defined(PROTO)
1505/*
1506 * Prepare profiling for entering a child or something else that is not
1507 * counted for the script/function itself.
1508 * Should always be called in pair with prof_child_exit().
1509 */
1510 void
1511prof_child_enter(tm)
1512 proftime_T *tm; /* place to store waittime */
1513{
1514 funccall_T *fc = current_funccal;
1515
1516 if (fc != NULL && fc->func->uf_profiling)
1517 profile_start(&fc->prof_child);
1518 script_prof_save(tm);
1519}
1520
1521/*
1522 * Take care of time spent in a child.
1523 * Should always be called after prof_child_enter().
1524 */
1525 void
1526prof_child_exit(tm)
1527 proftime_T *tm; /* where waittime was stored */
1528{
1529 funccall_T *fc = current_funccal;
1530
1531 if (fc != NULL && fc->func->uf_profiling)
1532 {
1533 profile_end(&fc->prof_child);
1534 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1535 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1536 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1537 }
1538 script_prof_restore(tm);
1539}
1540#endif
1541
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543#ifdef FEAT_FOLDING
1544/*
1545 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1546 * it in "*cp". Doesn't give error messages.
1547 */
1548 int
1549eval_foldexpr(arg, cp)
1550 char_u *arg;
1551 int *cp;
1552{
Bram Moolenaar33570922005-01-25 22:26:29 +00001553 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 int retval;
1555 char_u *s;
1556
1557 ++emsg_off;
1558 ++sandbox;
1559 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001560 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 retval = 0;
1562 else
1563 {
1564 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001565 if (tv.v_type == VAR_NUMBER)
1566 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001567 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 retval = 0;
1569 else
1570 {
1571 /* If the result is a string, check if there is a non-digit before
1572 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001573 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 if (!VIM_ISDIGIT(*s) && *s != '-')
1575 *cp = *s++;
1576 retval = atol((char *)s);
1577 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001578 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
1580 --emsg_off;
1581 --sandbox;
1582
1583 return retval;
1584}
1585#endif
1586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001588 * ":let" list all variable values
1589 * ":let var1 var2" list variable values
1590 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001591 * ":let var += expr" assignment command.
1592 * ":let var -= expr" assignment command.
1593 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 */
1596 void
1597ex_let(eap)
1598 exarg_T *eap;
1599{
1600 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001601 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001602 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001604 int var_count = 0;
1605 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001606 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001608 expr = skip_var_list(arg, &var_count, &semicolon);
1609 if (expr == NULL)
1610 return;
1611 expr = vim_strchr(expr, '=');
1612 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001614 /*
1615 * ":let" without "=": list variables
1616 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001617 if (*arg == '[')
1618 EMSG(_(e_invarg));
1619 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001620 /* ":let var1 var2" */
1621 arg = list_arg_vars(eap, arg);
1622 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001624 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001625 list_glob_vars();
1626 list_buf_vars();
1627 list_win_vars();
1628 list_vim_vars();
1629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 eap->nextcmd = check_nextcmd(arg);
1631 }
1632 else
1633 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001634 op[0] = '=';
1635 op[1] = NUL;
1636 if (expr > arg)
1637 {
1638 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1639 op[0] = expr[-1]; /* +=, -= or .= */
1640 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001641 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001642
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if (eap->skip)
1644 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001645 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (eap->skip)
1647 {
1648 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001649 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 --emsg_skip;
1651 }
1652 else if (i != FAIL)
1653 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001655 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001656 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 }
1658 }
1659}
1660
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001661/*
1662 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1663 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001664 * When "nextchars" is not NULL it points to a string with characters that
1665 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1666 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667 * Returns OK or FAIL;
1668 */
1669 static int
1670ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1671 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001672 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 int copy; /* copy values from "tv", don't move */
1674 int semicolon; /* from skip_var_list() */
1675 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001676 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677{
1678 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001679 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001680 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001681 listitem_T *item;
1682 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001683
1684 if (*arg != '[')
1685 {
1686 /*
1687 * ":let var = expr" or ":for var in list"
1688 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001689 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690 return FAIL;
1691 return OK;
1692 }
1693
1694 /*
1695 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1696 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001697 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001698 {
1699 EMSG(_(e_listreq));
1700 return FAIL;
1701 }
1702
1703 i = list_len(l);
1704 if (semicolon == 0 && var_count < i)
1705 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001706 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 return FAIL;
1708 }
1709 if (var_count - semicolon > i)
1710 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001711 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712 return FAIL;
1713 }
1714
1715 item = l->lv_first;
1716 while (*arg != ']')
1717 {
1718 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001720 item = item->li_next;
1721 if (arg == NULL)
1722 return FAIL;
1723
1724 arg = skipwhite(arg);
1725 if (*arg == ';')
1726 {
1727 /* Put the rest of the list (may be empty) in the var after ';'.
1728 * Create a new list for this. */
1729 l = list_alloc();
1730 if (l == NULL)
1731 return FAIL;
1732 while (item != NULL)
1733 {
1734 list_append_tv(l, &item->li_tv);
1735 item = item->li_next;
1736 }
1737
1738 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001739 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001740 ltv.vval.v_list = l;
1741 l->lv_refcount = 1;
1742
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001743 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1744 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001745 clear_tv(&ltv);
1746 if (arg == NULL)
1747 return FAIL;
1748 break;
1749 }
1750 else if (*arg != ',' && *arg != ']')
1751 {
1752 EMSG2(_(e_intern2), "ex_let_vars()");
1753 return FAIL;
1754 }
1755 }
1756
1757 return OK;
1758}
1759
1760/*
1761 * Skip over assignable variable "var" or list of variables "[var, var]".
1762 * Used for ":let varvar = expr" and ":for varvar in expr".
1763 * For "[var, var]" increment "*var_count" for each variable.
1764 * for "[var, var; var]" set "semicolon".
1765 * Return NULL for an error.
1766 */
1767 static char_u *
1768skip_var_list(arg, var_count, semicolon)
1769 char_u *arg;
1770 int *var_count;
1771 int *semicolon;
1772{
1773 char_u *p, *s;
1774
1775 if (*arg == '[')
1776 {
1777 /* "[var, var]": find the matching ']'. */
1778 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001779 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780 {
1781 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1782 s = skip_var_one(p);
1783 if (s == p)
1784 {
1785 EMSG2(_(e_invarg2), p);
1786 return NULL;
1787 }
1788 ++*var_count;
1789
1790 p = skipwhite(s);
1791 if (*p == ']')
1792 break;
1793 else if (*p == ';')
1794 {
1795 if (*semicolon == 1)
1796 {
1797 EMSG(_("Double ; in list of variables"));
1798 return NULL;
1799 }
1800 *semicolon = 1;
1801 }
1802 else if (*p != ',')
1803 {
1804 EMSG2(_(e_invarg2), p);
1805 return NULL;
1806 }
1807 }
1808 return p + 1;
1809 }
1810 else
1811 return skip_var_one(arg);
1812}
1813
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001814/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001815 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1816 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001817 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818 static char_u *
1819skip_var_one(arg)
1820 char_u *arg;
1821{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001822 if (*arg == '@' && arg[1] != NUL)
1823 return arg + 2;
1824 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1825 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001826}
1827
Bram Moolenaara7043832005-01-21 11:56:39 +00001828/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001829 * List variables for hashtab "ht" with prefix "prefix".
1830 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001831 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001833list_hashtable_vars(ht, prefix, empty)
1834 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001835 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001836 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001837{
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 hashitem_T *hi;
1839 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001840 int todo;
1841
1842 todo = ht->ht_used;
1843 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1844 {
1845 if (!HASHITEM_EMPTY(hi))
1846 {
1847 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001848 di = HI2DI(hi);
1849 if (empty || di->di_tv.v_type != VAR_STRING
1850 || di->di_tv.vval.v_string != NULL)
1851 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001852 }
1853 }
1854}
1855
1856/*
1857 * List global variables.
1858 */
1859 static void
1860list_glob_vars()
1861{
Bram Moolenaar33570922005-01-25 22:26:29 +00001862 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001863}
1864
1865/*
1866 * List buffer variables.
1867 */
1868 static void
1869list_buf_vars()
1870{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001871 char_u numbuf[NUMBUFLEN];
1872
Bram Moolenaar33570922005-01-25 22:26:29 +00001873 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001874
1875 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1876 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001877}
1878
1879/*
1880 * List window variables.
1881 */
1882 static void
1883list_win_vars()
1884{
Bram Moolenaar33570922005-01-25 22:26:29 +00001885 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001886}
1887
1888/*
1889 * List Vim variables.
1890 */
1891 static void
1892list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893{
Bram Moolenaar33570922005-01-25 22:26:29 +00001894 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895}
1896
1897/*
1898 * List variables in "arg".
1899 */
1900 static char_u *
1901list_arg_vars(eap, arg)
1902 exarg_T *eap;
1903 char_u *arg;
1904{
1905 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001906 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001908 char_u *name_start;
1909 char_u *arg_subsc;
1910 char_u *tofree;
1911 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001912
1913 while (!ends_excmd(*arg) && !got_int)
1914 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001915 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001917 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001918 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1919 {
1920 emsg_severe = TRUE;
1921 EMSG(_(e_trailing));
1922 break;
1923 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001925 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001927 /* get_name_len() takes care of expanding curly braces */
1928 name_start = name = arg;
1929 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1930 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001932 /* This is mainly to keep test 49 working: when expanding
1933 * curly braces fails overrule the exception error message. */
1934 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001936 emsg_severe = TRUE;
1937 EMSG2(_(e_invarg2), arg);
1938 break;
1939 }
1940 error = TRUE;
1941 }
1942 else
1943 {
1944 if (tofree != NULL)
1945 name = tofree;
1946 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 else
1949 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001950 /* handle d.key, l[idx], f(expr) */
1951 arg_subsc = arg;
1952 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001953 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001955 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001956 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001957 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001958 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001959 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001960 case 'g': list_glob_vars(); break;
1961 case 'b': list_buf_vars(); break;
1962 case 'w': list_win_vars(); break;
1963 case 'v': list_vim_vars(); break;
1964 default:
1965 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001966 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001967 }
1968 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001969 {
1970 char_u numbuf[NUMBUFLEN];
1971 char_u *tf;
1972 int c;
1973 char_u *s;
1974
1975 s = echo_string(&tv, &tf, numbuf);
1976 c = *arg;
1977 *arg = NUL;
1978 list_one_var_a((char_u *)"",
1979 arg == arg_subsc ? name : name_start,
1980 tv.v_type, s == NULL ? (char_u *)"" : s);
1981 *arg = c;
1982 vim_free(tf);
1983 }
1984 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001985 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986 }
1987 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001988
1989 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001991
1992 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001993 }
1994
1995 return arg;
1996}
1997
1998/*
1999 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2000 * Returns a pointer to the char just after the var name.
2001 * Returns NULL if there is an error.
2002 */
2003 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002004ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002005 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002006 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002007 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002009 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002010{
2011 int c1;
2012 char_u *name;
2013 char_u *p;
2014 char_u *arg_end = NULL;
2015 int len;
2016 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002017 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018
2019 /*
2020 * ":let $VAR = expr": Set environment variable.
2021 */
2022 if (*arg == '$')
2023 {
2024 /* Find the end of the name. */
2025 ++arg;
2026 name = arg;
2027 len = get_env_len(&arg);
2028 if (len == 0)
2029 EMSG2(_(e_invarg2), name - 1);
2030 else
2031 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002032 if (op != NULL && (*op == '+' || *op == '-'))
2033 EMSG2(_(e_letwrong), op);
2034 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002035 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002036 EMSG(_(e_letunexp));
2037 else
2038 {
2039 c1 = name[len];
2040 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002041 p = get_tv_string_chk(tv);
2042 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002043 {
2044 int mustfree = FALSE;
2045 char_u *s = vim_getenv(name, &mustfree);
2046
2047 if (s != NULL)
2048 {
2049 p = tofree = concat_str(s, p);
2050 if (mustfree)
2051 vim_free(s);
2052 }
2053 }
2054 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002055 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002056 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002057 if (STRICMP(name, "HOME") == 0)
2058 init_homedir();
2059 else if (didset_vim && STRICMP(name, "VIM") == 0)
2060 didset_vim = FALSE;
2061 else if (didset_vimruntime
2062 && STRICMP(name, "VIMRUNTIME") == 0)
2063 didset_vimruntime = FALSE;
2064 arg_end = arg;
2065 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002066 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002067 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002068 }
2069 }
2070 }
2071
2072 /*
2073 * ":let &option = expr": Set option value.
2074 * ":let &l:option = expr": Set local option value.
2075 * ":let &g:option = expr": Set global option value.
2076 */
2077 else if (*arg == '&')
2078 {
2079 /* Find the end of the name. */
2080 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002081 if (p == NULL || (endchars != NULL
2082 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002083 EMSG(_(e_letunexp));
2084 else
2085 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002086 long n;
2087 int opt_type;
2088 long numval;
2089 char_u *stringval = NULL;
2090 char_u *s;
2091
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002092 c1 = *p;
2093 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002094
2095 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002096 s = get_tv_string_chk(tv); /* != NULL if number or string */
2097 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002098 {
2099 opt_type = get_option_value(arg, &numval,
2100 &stringval, opt_flags);
2101 if ((opt_type == 1 && *op == '.')
2102 || (opt_type == 0 && *op != '.'))
2103 EMSG2(_(e_letwrong), op);
2104 else
2105 {
2106 if (opt_type == 1) /* number */
2107 {
2108 if (*op == '+')
2109 n = numval + n;
2110 else
2111 n = numval - n;
2112 }
2113 else if (opt_type == 0 && stringval != NULL) /* string */
2114 {
2115 s = concat_str(stringval, s);
2116 vim_free(stringval);
2117 stringval = s;
2118 }
2119 }
2120 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002121 if (s != NULL)
2122 {
2123 set_option_value(arg, n, s, opt_flags);
2124 arg_end = p;
2125 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002126 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002127 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002128 }
2129 }
2130
2131 /*
2132 * ":let @r = expr": Set register contents.
2133 */
2134 else if (*arg == '@')
2135 {
2136 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002137 if (op != NULL && (*op == '+' || *op == '-'))
2138 EMSG2(_(e_letwrong), op);
2139 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002140 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002141 EMSG(_(e_letunexp));
2142 else
2143 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002144 char_u *tofree = NULL;
2145 char_u *s;
2146
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002147 p = get_tv_string_chk(tv);
2148 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002149 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002150 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002151 if (s != NULL)
2152 {
2153 p = tofree = concat_str(s, p);
2154 vim_free(s);
2155 }
2156 }
2157 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002158 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002159 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002160 arg_end = arg + 1;
2161 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002162 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002163 }
2164 }
2165
2166 /*
2167 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002168 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002169 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002170 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002171 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002172 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002174 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002175 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002176 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002177 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2178 EMSG(_(e_letunexp));
2179 else
2180 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002181 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182 arg_end = p;
2183 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002185 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 }
2187
2188 else
2189 EMSG2(_(e_invarg2), arg);
2190
2191 return arg_end;
2192}
2193
2194/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002195 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2196 */
2197 static int
2198check_changedtick(arg)
2199 char_u *arg;
2200{
2201 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2202 {
2203 EMSG2(_(e_readonlyvar), arg);
2204 return TRUE;
2205 }
2206 return FALSE;
2207}
2208
2209/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002210 * Get an lval: variable, Dict item or List item that can be assigned a value
2211 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2212 * "name.key", "name.key[expr]" etc.
2213 * Indexing only works if "name" is an existing List or Dictionary.
2214 * "name" points to the start of the name.
2215 * If "rettv" is not NULL it points to the value to be assigned.
2216 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2217 * wrong; must end in space or cmd separator.
2218 *
2219 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002220 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002221 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 */
2223 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002224get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002226 typval_T *rettv;
2227 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002228 int unlet;
2229 int skip;
2230 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002231 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 char_u *expr_start, *expr_end;
2235 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002236 dictitem_T *v;
2237 typval_T var1;
2238 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002240 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002241 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002242 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002243 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002245 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002246 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247
2248 if (skip)
2249 {
2250 /* When skipping just find the end of the name. */
2251 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002252 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002253 }
2254
2255 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002256 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 if (expr_start != NULL)
2258 {
2259 /* Don't expand the name when we already know there is an error. */
2260 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2261 && *p != '[' && *p != '.')
2262 {
2263 EMSG(_(e_trailing));
2264 return NULL;
2265 }
2266
2267 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2268 if (lp->ll_exp_name == NULL)
2269 {
2270 /* Report an invalid expression in braces, unless the
2271 * expression evaluation has been cancelled due to an
2272 * aborting error, an interrupt, or an exception. */
2273 if (!aborting() && !quiet)
2274 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002275 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002276 EMSG2(_(e_invarg2), name);
2277 return NULL;
2278 }
2279 }
2280 lp->ll_name = lp->ll_exp_name;
2281 }
2282 else
2283 lp->ll_name = name;
2284
2285 /* Without [idx] or .key we are done. */
2286 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2287 return p;
2288
2289 cc = *p;
2290 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002291 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002292 if (v == NULL && !quiet)
2293 EMSG2(_(e_undefvar), lp->ll_name);
2294 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 if (v == NULL)
2296 return NULL;
2297
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002298 /*
2299 * Loop until no more [idx] or .key is following.
2300 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002301 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002302 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2305 && !(lp->ll_tv->v_type == VAR_DICT
2306 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 if (!quiet)
2309 EMSG(_("E689: Can only index a List or Dictionary"));
2310 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002312 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 if (!quiet)
2315 EMSG(_("E708: [:] must come last"));
2316 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002318
Bram Moolenaar8c711452005-01-14 21:53:12 +00002319 len = -1;
2320 if (*p == '.')
2321 {
2322 key = p + 1;
2323 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2324 ;
2325 if (len == 0)
2326 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002327 if (!quiet)
2328 EMSG(_(e_emptykey));
2329 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002330 }
2331 p = key + len;
2332 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002333 else
2334 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002335 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002336 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002337 if (*p == ':')
2338 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002339 else
2340 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002341 empty1 = FALSE;
2342 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002343 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002344 if (get_tv_string_chk(&var1) == NULL)
2345 {
2346 /* not a number or string */
2347 clear_tv(&var1);
2348 return NULL;
2349 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002350 }
2351
2352 /* Optionally get the second index [ :expr]. */
2353 if (*p == ':')
2354 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002355 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002356 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002357 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002358 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002359 if (!empty1)
2360 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002361 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002362 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002363 if (rettv != NULL && (rettv->v_type != VAR_LIST
2364 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002365 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 if (!quiet)
2367 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002368 if (!empty1)
2369 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002371 }
2372 p = skipwhite(p + 1);
2373 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002374 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002375 else
2376 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002378 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2379 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002380 if (!empty1)
2381 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002383 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 if (get_tv_string_chk(&var2) == NULL)
2385 {
2386 /* not a number or string */
2387 if (!empty1)
2388 clear_tv(&var1);
2389 clear_tv(&var2);
2390 return NULL;
2391 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002392 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002394 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002395 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002396 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002397
Bram Moolenaar8c711452005-01-14 21:53:12 +00002398 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002399 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002400 if (!quiet)
2401 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002402 if (!empty1)
2403 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002404 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002405 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 }
2408
2409 /* Skip to past ']'. */
2410 ++p;
2411 }
2412
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002414 {
2415 if (len == -1)
2416 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002418 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002419 if (*key == NUL)
2420 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 if (!quiet)
2422 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002423 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002425 }
2426 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 lp->ll_list = NULL;
2428 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002429 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002431 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002432 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002434 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002437 if (len == -1)
2438 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 }
2441 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002445 if (len == -1)
2446 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 p = NULL;
2449 break;
2450 }
2451 if (len == -1)
2452 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 }
2455 else
2456 {
2457 /*
2458 * Get the number and item for the only or first index of the List.
2459 */
2460 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002462 else
2463 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002464 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002465 clear_tv(&var1);
2466 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002467 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 lp->ll_list = lp->ll_tv->vval.v_list;
2469 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2470 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002471 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 if (!quiet)
2473 EMSGN(_(e_listidx), lp->ll_n1);
2474 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 }
2478
2479 /*
2480 * May need to find the item or absolute index for the second
2481 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 * When no index given: "lp->ll_empty2" is TRUE.
2483 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002487 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 if (ni == NULL)
2493 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 if (!quiet)
2495 EMSGN(_(e_listidx), lp->ll_n2);
2496 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002497 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 }
2500
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2502 if (lp->ll_n1 < 0)
2503 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2504 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 if (!quiet)
2507 EMSGN(_(e_listidx), lp->ll_n2);
2508 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002509 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002510 }
2511
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002513 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 }
2515
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 return p;
2517}
2518
2519/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002520 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 */
2522 static void
2523clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002524 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525{
2526 vim_free(lp->ll_exp_name);
2527 vim_free(lp->ll_newkey);
2528}
2529
2530/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002531 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 */
2535 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002536set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002539 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002541 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542{
2543 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 listitem_T *ni;
2545 listitem_T *ri;
2546 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547
2548 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002549 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002551 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 cc = *endp;
2553 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002554 if (op != NULL && *op != '=')
2555 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002557
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002558 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002559 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2560 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002561 {
2562 if (tv_op(&tv, rettv, op) == OK)
2563 set_var(lp->ll_name, &tv, FALSE);
2564 clear_tv(&tv);
2565 }
2566 }
2567 else
2568 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002572 else if (tv_check_lock(lp->ll_newkey == NULL
2573 ? lp->ll_tv->v_lock
2574 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2575 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 else if (lp->ll_range)
2577 {
2578 /*
2579 * Assign the List values to the list items.
2580 */
2581 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002582 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002583 if (op != NULL && *op != '=')
2584 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2585 else
2586 {
2587 clear_tv(&lp->ll_li->li_tv);
2588 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2589 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 ri = ri->li_next;
2591 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2592 break;
2593 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002594 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 /* Need to add an empty item. */
2596 ni = listitem_alloc();
2597 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 ri = NULL;
2600 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002601 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002603 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 ni->li_tv.vval.v_number = 0;
2605 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002606 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 lp->ll_li = lp->ll_li->li_next;
2608 ++lp->ll_n1;
2609 }
2610 if (ri != NULL)
2611 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002612 else if (lp->ll_empty2
2613 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 : lp->ll_n1 != lp->ll_n2)
2615 EMSG(_("E711: List value has not enough items"));
2616 }
2617 else
2618 {
2619 /*
2620 * Assign to a List or Dictionary item.
2621 */
2622 if (lp->ll_newkey != NULL)
2623 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002624 if (op != NULL && *op != '=')
2625 {
2626 EMSG2(_(e_letwrong), op);
2627 return;
2628 }
2629
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002631 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 if (di == NULL)
2633 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002634 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2635 {
2636 vim_free(di);
2637 return;
2638 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002640 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002641 else if (op != NULL && *op != '=')
2642 {
2643 tv_op(lp->ll_tv, rettv, op);
2644 return;
2645 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002646 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 /*
2650 * Assign the value to the variable or list item.
2651 */
2652 if (copy)
2653 copy_tv(rettv, lp->ll_tv);
2654 else
2655 {
2656 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002657 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 }
2660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002661}
2662
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002663/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002664 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2665 * Returns OK or FAIL.
2666 */
2667 static int
2668tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002669 typval_T *tv1;
2670 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002671 char_u *op;
2672{
2673 long n;
2674 char_u numbuf[NUMBUFLEN];
2675 char_u *s;
2676
2677 /* Can't do anything with a Funcref or a Dict on the right. */
2678 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2679 {
2680 switch (tv1->v_type)
2681 {
2682 case VAR_DICT:
2683 case VAR_FUNC:
2684 break;
2685
2686 case VAR_LIST:
2687 if (*op != '+' || tv2->v_type != VAR_LIST)
2688 break;
2689 /* List += List */
2690 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2691 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2692 return OK;
2693
2694 case VAR_NUMBER:
2695 case VAR_STRING:
2696 if (tv2->v_type == VAR_LIST)
2697 break;
2698 if (*op == '+' || *op == '-')
2699 {
2700 /* nr += nr or nr -= nr*/
2701 n = get_tv_number(tv1);
2702 if (*op == '+')
2703 n += get_tv_number(tv2);
2704 else
2705 n -= get_tv_number(tv2);
2706 clear_tv(tv1);
2707 tv1->v_type = VAR_NUMBER;
2708 tv1->vval.v_number = n;
2709 }
2710 else
2711 {
2712 /* str .= str */
2713 s = get_tv_string(tv1);
2714 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2715 clear_tv(tv1);
2716 tv1->v_type = VAR_STRING;
2717 tv1->vval.v_string = s;
2718 }
2719 return OK;
2720 }
2721 }
2722
2723 EMSG2(_(e_letwrong), op);
2724 return FAIL;
2725}
2726
2727/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002728 * Add a watcher to a list.
2729 */
2730 static void
2731list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002732 list_T *l;
2733 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002734{
2735 lw->lw_next = l->lv_watch;
2736 l->lv_watch = lw;
2737}
2738
2739/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002740 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002741 * No warning when it isn't found...
2742 */
2743 static void
2744list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002745 list_T *l;
2746 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002747{
Bram Moolenaar33570922005-01-25 22:26:29 +00002748 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002749
2750 lwp = &l->lv_watch;
2751 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2752 {
2753 if (lw == lwrem)
2754 {
2755 *lwp = lw->lw_next;
2756 break;
2757 }
2758 lwp = &lw->lw_next;
2759 }
2760}
2761
2762/*
2763 * Just before removing an item from a list: advance watchers to the next
2764 * item.
2765 */
2766 static void
2767list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002768 list_T *l;
2769 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002770{
Bram Moolenaar33570922005-01-25 22:26:29 +00002771 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002772
2773 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2774 if (lw->lw_item == item)
2775 lw->lw_item = item->li_next;
2776}
2777
2778/*
2779 * Evaluate the expression used in a ":for var in expr" command.
2780 * "arg" points to "var".
2781 * Set "*errp" to TRUE for an error, FALSE otherwise;
2782 * Return a pointer that holds the info. Null when there is an error.
2783 */
2784 void *
2785eval_for_line(arg, errp, nextcmdp, skip)
2786 char_u *arg;
2787 int *errp;
2788 char_u **nextcmdp;
2789 int skip;
2790{
Bram Moolenaar33570922005-01-25 22:26:29 +00002791 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002792 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002793 typval_T tv;
2794 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002795
2796 *errp = TRUE; /* default: there is an error */
2797
Bram Moolenaar33570922005-01-25 22:26:29 +00002798 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002799 if (fi == NULL)
2800 return NULL;
2801
2802 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2803 if (expr == NULL)
2804 return fi;
2805
2806 expr = skipwhite(expr);
2807 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2808 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002809 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002810 return fi;
2811 }
2812
2813 if (skip)
2814 ++emsg_skip;
2815 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2816 {
2817 *errp = FALSE;
2818 if (!skip)
2819 {
2820 l = tv.vval.v_list;
2821 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002822 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002823 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002824 clear_tv(&tv);
2825 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002826 else
2827 {
2828 fi->fi_list = l;
2829 list_add_watch(l, &fi->fi_lw);
2830 fi->fi_lw.lw_item = l->lv_first;
2831 }
2832 }
2833 }
2834 if (skip)
2835 --emsg_skip;
2836
2837 return fi;
2838}
2839
2840/*
2841 * Use the first item in a ":for" list. Advance to the next.
2842 * Assign the values to the variable (list). "arg" points to the first one.
2843 * Return TRUE when a valid item was found, FALSE when at end of list or
2844 * something wrong.
2845 */
2846 int
2847next_for_item(fi_void, arg)
2848 void *fi_void;
2849 char_u *arg;
2850{
Bram Moolenaar33570922005-01-25 22:26:29 +00002851 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002852 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002853 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002854
2855 item = fi->fi_lw.lw_item;
2856 if (item == NULL)
2857 result = FALSE;
2858 else
2859 {
2860 fi->fi_lw.lw_item = item->li_next;
2861 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2862 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2863 }
2864 return result;
2865}
2866
2867/*
2868 * Free the structure used to store info used by ":for".
2869 */
2870 void
2871free_for_info(fi_void)
2872 void *fi_void;
2873{
Bram Moolenaar33570922005-01-25 22:26:29 +00002874 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002875
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002876 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002877 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002878 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002879 list_unref(fi->fi_list);
2880 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002881 vim_free(fi);
2882}
2883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2885
2886 void
2887set_context_for_expression(xp, arg, cmdidx)
2888 expand_T *xp;
2889 char_u *arg;
2890 cmdidx_T cmdidx;
2891{
2892 int got_eq = FALSE;
2893 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002894 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002896 if (cmdidx == CMD_let)
2897 {
2898 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002899 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002900 {
2901 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002902 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002903 {
2904 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002905 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002906 if (vim_iswhite(*p))
2907 break;
2908 }
2909 return;
2910 }
2911 }
2912 else
2913 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2914 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 while ((xp->xp_pattern = vim_strpbrk(arg,
2916 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2917 {
2918 c = *xp->xp_pattern;
2919 if (c == '&')
2920 {
2921 c = xp->xp_pattern[1];
2922 if (c == '&')
2923 {
2924 ++xp->xp_pattern;
2925 xp->xp_context = cmdidx != CMD_let || got_eq
2926 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2927 }
2928 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002931 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2932 xp->xp_pattern += 2;
2933
2934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 }
2936 else if (c == '$')
2937 {
2938 /* environment variable */
2939 xp->xp_context = EXPAND_ENV_VARS;
2940 }
2941 else if (c == '=')
2942 {
2943 got_eq = TRUE;
2944 xp->xp_context = EXPAND_EXPRESSION;
2945 }
2946 else if (c == '<'
2947 && xp->xp_context == EXPAND_FUNCTIONS
2948 && vim_strchr(xp->xp_pattern, '(') == NULL)
2949 {
2950 /* Function name can start with "<SNR>" */
2951 break;
2952 }
2953 else if (cmdidx != CMD_let || got_eq)
2954 {
2955 if (c == '"') /* string */
2956 {
2957 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2958 if (c == '\\' && xp->xp_pattern[1] != NUL)
2959 ++xp->xp_pattern;
2960 xp->xp_context = EXPAND_NOTHING;
2961 }
2962 else if (c == '\'') /* literal string */
2963 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002964 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2966 /* skip */ ;
2967 xp->xp_context = EXPAND_NOTHING;
2968 }
2969 else if (c == '|')
2970 {
2971 if (xp->xp_pattern[1] == '|')
2972 {
2973 ++xp->xp_pattern;
2974 xp->xp_context = EXPAND_EXPRESSION;
2975 }
2976 else
2977 xp->xp_context = EXPAND_COMMANDS;
2978 }
2979 else
2980 xp->xp_context = EXPAND_EXPRESSION;
2981 }
2982 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002983 /* Doesn't look like something valid, expand as an expression
2984 * anyway. */
2985 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 arg = xp->xp_pattern;
2987 if (*arg != NUL)
2988 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2989 /* skip */ ;
2990 }
2991 xp->xp_pattern = arg;
2992}
2993
2994#endif /* FEAT_CMDL_COMPL */
2995
2996/*
2997 * ":1,25call func(arg1, arg2)" function call.
2998 */
2999 void
3000ex_call(eap)
3001 exarg_T *eap;
3002{
3003 char_u *arg = eap->arg;
3004 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003006 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003008 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 linenr_T lnum;
3010 int doesrange;
3011 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003012 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003014 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3015 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003016 if (tofree == NULL)
3017 return;
3018
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003019 /* Increase refcount on dictionary, it could get deleted when evaluating
3020 * the arguments. */
3021 if (fudi.fd_dict != NULL)
3022 ++fudi.fd_dict->dv_refcount;
3023
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003024 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3025 len = STRLEN(tofree);
3026 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027
Bram Moolenaar532c7802005-01-27 14:44:31 +00003028 /* Skip white space to allow ":call func ()". Not good, but required for
3029 * backward compatibility. */
3030 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003031 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032
3033 if (*startarg != '(')
3034 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003035 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 goto end;
3037 }
3038
3039 /*
3040 * When skipping, evaluate the function once, to find the end of the
3041 * arguments.
3042 * When the function takes a range, this is discovered after the first
3043 * call, and the loop is broken.
3044 */
3045 if (eap->skip)
3046 {
3047 ++emsg_skip;
3048 lnum = eap->line2; /* do it once, also with an invalid range */
3049 }
3050 else
3051 lnum = eap->line1;
3052 for ( ; lnum <= eap->line2; ++lnum)
3053 {
3054 if (!eap->skip && eap->addr_count > 0)
3055 {
3056 curwin->w_cursor.lnum = lnum;
3057 curwin->w_cursor.col = 0;
3058 }
3059 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003060 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003061 eap->line1, eap->line2, &doesrange,
3062 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 {
3064 failed = TRUE;
3065 break;
3066 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003067 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 if (doesrange || eap->skip)
3069 break;
3070 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003071 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003072 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003073 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 if (aborting())
3075 break;
3076 }
3077 if (eap->skip)
3078 --emsg_skip;
3079
3080 if (!failed)
3081 {
3082 /* Check for trailing illegal characters and a following command. */
3083 if (!ends_excmd(*arg))
3084 {
3085 emsg_severe = TRUE;
3086 EMSG(_(e_trailing));
3087 }
3088 else
3089 eap->nextcmd = check_nextcmd(arg);
3090 }
3091
3092end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003093 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003094 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095}
3096
3097/*
3098 * ":unlet[!] var1 ... " command.
3099 */
3100 void
3101ex_unlet(eap)
3102 exarg_T *eap;
3103{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003104 ex_unletlock(eap, eap->arg, 0);
3105}
3106
3107/*
3108 * ":lockvar" and ":unlockvar" commands
3109 */
3110 void
3111ex_lockvar(eap)
3112 exarg_T *eap;
3113{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003115 int deep = 2;
3116
3117 if (eap->forceit)
3118 deep = -1;
3119 else if (vim_isdigit(*arg))
3120 {
3121 deep = getdigits(&arg);
3122 arg = skipwhite(arg);
3123 }
3124
3125 ex_unletlock(eap, arg, deep);
3126}
3127
3128/*
3129 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3130 */
3131 static void
3132ex_unletlock(eap, argstart, deep)
3133 exarg_T *eap;
3134 char_u *argstart;
3135 int deep;
3136{
3137 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003140 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
3142 do
3143 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003144 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003145 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3146 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003147 if (lv.ll_name == NULL)
3148 error = TRUE; /* error but continue parsing */
3149 if (name_end == NULL || (!vim_iswhite(*name_end)
3150 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003152 if (name_end != NULL)
3153 {
3154 emsg_severe = TRUE;
3155 EMSG(_(e_trailing));
3156 }
3157 if (!(eap->skip || error))
3158 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 break;
3160 }
3161
3162 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003163 {
3164 if (eap->cmdidx == CMD_unlet)
3165 {
3166 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3167 error = TRUE;
3168 }
3169 else
3170 {
3171 if (do_lock_var(&lv, name_end, deep,
3172 eap->cmdidx == CMD_lockvar) == FAIL)
3173 error = TRUE;
3174 }
3175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003177 if (!eap->skip)
3178 clear_lval(&lv);
3179
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 arg = skipwhite(name_end);
3181 } while (!ends_excmd(*arg));
3182
3183 eap->nextcmd = check_nextcmd(arg);
3184}
3185
Bram Moolenaar8c711452005-01-14 21:53:12 +00003186 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003187do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003189 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003190 int forceit;
3191{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003192 int ret = OK;
3193 int cc;
3194
3195 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003196 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003197 cc = *name_end;
3198 *name_end = NUL;
3199
3200 /* Normal name or expanded name. */
3201 if (check_changedtick(lp->ll_name))
3202 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003203 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003204 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003205 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003206 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003207 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3208 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003209 else if (lp->ll_range)
3210 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003211 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003212
3213 /* Delete a range of List items. */
3214 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3215 {
3216 li = lp->ll_li->li_next;
3217 listitem_remove(lp->ll_list, lp->ll_li);
3218 lp->ll_li = li;
3219 ++lp->ll_n1;
3220 }
3221 }
3222 else
3223 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003224 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003225 /* unlet a List item. */
3226 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003227 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003228 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003229 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003230 }
3231
3232 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003233}
3234
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235/*
3236 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003237 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 */
3239 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003240do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003242 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243{
Bram Moolenaar33570922005-01-25 22:26:29 +00003244 hashtab_T *ht;
3245 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003246 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 ht = find_var_ht(name, &varname);
3249 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003251 hi = hash_find(ht, varname);
3252 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003253 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003254 if (var_check_ro(HI2DI(hi)->di_flags, name))
3255 return FAIL;
3256 delete_var(ht, hi);
3257 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003260 if (forceit)
3261 return OK;
3262 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 return FAIL;
3264}
3265
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003266/*
3267 * Lock or unlock variable indicated by "lp".
3268 * "deep" is the levels to go (-1 for unlimited);
3269 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3270 */
3271 static int
3272do_lock_var(lp, name_end, deep, lock)
3273 lval_T *lp;
3274 char_u *name_end;
3275 int deep;
3276 int lock;
3277{
3278 int ret = OK;
3279 int cc;
3280 dictitem_T *di;
3281
3282 if (deep == 0) /* nothing to do */
3283 return OK;
3284
3285 if (lp->ll_tv == NULL)
3286 {
3287 cc = *name_end;
3288 *name_end = NUL;
3289
3290 /* Normal name or expanded name. */
3291 if (check_changedtick(lp->ll_name))
3292 ret = FAIL;
3293 else
3294 {
3295 di = find_var(lp->ll_name, NULL);
3296 if (di == NULL)
3297 ret = FAIL;
3298 else
3299 {
3300 if (lock)
3301 di->di_flags |= DI_FLAGS_LOCK;
3302 else
3303 di->di_flags &= ~DI_FLAGS_LOCK;
3304 item_lock(&di->di_tv, deep, lock);
3305 }
3306 }
3307 *name_end = cc;
3308 }
3309 else if (lp->ll_range)
3310 {
3311 listitem_T *li = lp->ll_li;
3312
3313 /* (un)lock a range of List items. */
3314 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3315 {
3316 item_lock(&li->li_tv, deep, lock);
3317 li = li->li_next;
3318 ++lp->ll_n1;
3319 }
3320 }
3321 else if (lp->ll_list != NULL)
3322 /* (un)lock a List item. */
3323 item_lock(&lp->ll_li->li_tv, deep, lock);
3324 else
3325 /* un(lock) a Dictionary item. */
3326 item_lock(&lp->ll_di->di_tv, deep, lock);
3327
3328 return ret;
3329}
3330
3331/*
3332 * Lock or unlock an item. "deep" is nr of levels to go.
3333 */
3334 static void
3335item_lock(tv, deep, lock)
3336 typval_T *tv;
3337 int deep;
3338 int lock;
3339{
3340 static int recurse = 0;
3341 list_T *l;
3342 listitem_T *li;
3343 dict_T *d;
3344 hashitem_T *hi;
3345 int todo;
3346
3347 if (recurse >= DICT_MAXNEST)
3348 {
3349 EMSG(_("E743: variable nested too deep for (un)lock"));
3350 return;
3351 }
3352 if (deep == 0)
3353 return;
3354 ++recurse;
3355
3356 /* lock/unlock the item itself */
3357 if (lock)
3358 tv->v_lock |= VAR_LOCKED;
3359 else
3360 tv->v_lock &= ~VAR_LOCKED;
3361
3362 switch (tv->v_type)
3363 {
3364 case VAR_LIST:
3365 if ((l = tv->vval.v_list) != NULL)
3366 {
3367 if (lock)
3368 l->lv_lock |= VAR_LOCKED;
3369 else
3370 l->lv_lock &= ~VAR_LOCKED;
3371 if (deep < 0 || deep > 1)
3372 /* recursive: lock/unlock the items the List contains */
3373 for (li = l->lv_first; li != NULL; li = li->li_next)
3374 item_lock(&li->li_tv, deep - 1, lock);
3375 }
3376 break;
3377 case VAR_DICT:
3378 if ((d = tv->vval.v_dict) != NULL)
3379 {
3380 if (lock)
3381 d->dv_lock |= VAR_LOCKED;
3382 else
3383 d->dv_lock &= ~VAR_LOCKED;
3384 if (deep < 0 || deep > 1)
3385 {
3386 /* recursive: lock/unlock the items the List contains */
3387 todo = d->dv_hashtab.ht_used;
3388 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3389 {
3390 if (!HASHITEM_EMPTY(hi))
3391 {
3392 --todo;
3393 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3394 }
3395 }
3396 }
3397 }
3398 }
3399 --recurse;
3400}
3401
Bram Moolenaara40058a2005-07-11 22:42:07 +00003402/*
3403 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3404 * it refers to a List or Dictionary that is locked.
3405 */
3406 static int
3407tv_islocked(tv)
3408 typval_T *tv;
3409{
3410 return (tv->v_lock & VAR_LOCKED)
3411 || (tv->v_type == VAR_LIST
3412 && tv->vval.v_list != NULL
3413 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3414 || (tv->v_type == VAR_DICT
3415 && tv->vval.v_dict != NULL
3416 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3417}
3418
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3420/*
3421 * Delete all "menutrans_" variables.
3422 */
3423 void
3424del_menutrans_vars()
3425{
Bram Moolenaar33570922005-01-25 22:26:29 +00003426 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003427 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
Bram Moolenaar33570922005-01-25 22:26:29 +00003429 hash_lock(&globvarht);
3430 todo = globvarht.ht_used;
3431 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003432 {
3433 if (!HASHITEM_EMPTY(hi))
3434 {
3435 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003436 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3437 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003438 }
3439 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003440 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441}
3442#endif
3443
3444#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3445
3446/*
3447 * Local string buffer for the next two functions to store a variable name
3448 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3449 * get_user_var_name().
3450 */
3451
3452static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3453
3454static char_u *varnamebuf = NULL;
3455static int varnamebuflen = 0;
3456
3457/*
3458 * Function to concatenate a prefix and a variable name.
3459 */
3460 static char_u *
3461cat_prefix_varname(prefix, name)
3462 int prefix;
3463 char_u *name;
3464{
3465 int len;
3466
3467 len = (int)STRLEN(name) + 3;
3468 if (len > varnamebuflen)
3469 {
3470 vim_free(varnamebuf);
3471 len += 10; /* some additional space */
3472 varnamebuf = alloc(len);
3473 if (varnamebuf == NULL)
3474 {
3475 varnamebuflen = 0;
3476 return NULL;
3477 }
3478 varnamebuflen = len;
3479 }
3480 *varnamebuf = prefix;
3481 varnamebuf[1] = ':';
3482 STRCPY(varnamebuf + 2, name);
3483 return varnamebuf;
3484}
3485
3486/*
3487 * Function given to ExpandGeneric() to obtain the list of user defined
3488 * (global/buffer/window/built-in) variable names.
3489 */
3490/*ARGSUSED*/
3491 char_u *
3492get_user_var_name(xp, idx)
3493 expand_T *xp;
3494 int idx;
3495{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003496 static long_u gdone;
3497 static long_u bdone;
3498 static long_u wdone;
3499 static int vidx;
3500 static hashitem_T *hi;
3501 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
3503 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003504 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003505
3506 /* Global variables */
3507 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003509 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003510 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003511 else
3512 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003513 while (HASHITEM_EMPTY(hi))
3514 ++hi;
3515 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3516 return cat_prefix_varname('g', hi->hi_key);
3517 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003519
3520 /* b: variables */
3521 ht = &curbuf->b_vars.dv_hashtab;
3522 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003524 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003525 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003526 else
3527 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003528 while (HASHITEM_EMPTY(hi))
3529 ++hi;
3530 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003532 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003534 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 return (char_u *)"b:changedtick";
3536 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003537
3538 /* w: variables */
3539 ht = &curwin->w_vars.dv_hashtab;
3540 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003542 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003543 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003544 else
3545 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003546 while (HASHITEM_EMPTY(hi))
3547 ++hi;
3548 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003550
3551 /* v: variables */
3552 if (vidx < VV_LEN)
3553 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554
3555 vim_free(varnamebuf);
3556 varnamebuf = NULL;
3557 varnamebuflen = 0;
3558 return NULL;
3559}
3560
3561#endif /* FEAT_CMDL_COMPL */
3562
3563/*
3564 * types for expressions.
3565 */
3566typedef enum
3567{
3568 TYPE_UNKNOWN = 0
3569 , TYPE_EQUAL /* == */
3570 , TYPE_NEQUAL /* != */
3571 , TYPE_GREATER /* > */
3572 , TYPE_GEQUAL /* >= */
3573 , TYPE_SMALLER /* < */
3574 , TYPE_SEQUAL /* <= */
3575 , TYPE_MATCH /* =~ */
3576 , TYPE_NOMATCH /* !~ */
3577} exptype_T;
3578
3579/*
3580 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003581 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3583 */
3584
3585/*
3586 * Handle zero level expression.
3587 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003588 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 * Return OK or FAIL.
3590 */
3591 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003592eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 char_u **nextcmd;
3596 int evaluate;
3597{
3598 int ret;
3599 char_u *p;
3600
3601 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003602 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 if (ret == FAIL || !ends_excmd(*p))
3604 {
3605 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003606 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 /*
3608 * Report the invalid expression unless the expression evaluation has
3609 * been cancelled due to an aborting error, an interrupt, or an
3610 * exception.
3611 */
3612 if (!aborting())
3613 EMSG2(_(e_invexpr2), arg);
3614 ret = FAIL;
3615 }
3616 if (nextcmd != NULL)
3617 *nextcmd = check_nextcmd(p);
3618
3619 return ret;
3620}
3621
3622/*
3623 * Handle top level expression:
3624 * expr1 ? expr0 : expr0
3625 *
3626 * "arg" must point to the first non-white of the expression.
3627 * "arg" is advanced to the next non-white after the recognized expression.
3628 *
3629 * Return OK or FAIL.
3630 */
3631 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003632eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 int evaluate;
3636{
3637 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003638 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
3640 /*
3641 * Get the first variable.
3642 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003643 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 return FAIL;
3645
3646 if ((*arg)[0] == '?')
3647 {
3648 result = FALSE;
3649 if (evaluate)
3650 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003651 int error = FALSE;
3652
3653 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003655 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003656 if (error)
3657 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
3659
3660 /*
3661 * Get the second variable.
3662 */
3663 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003664 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 return FAIL;
3666
3667 /*
3668 * Check for the ":".
3669 */
3670 if ((*arg)[0] != ':')
3671 {
3672 EMSG(_("E109: Missing ':' after '?'"));
3673 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003674 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 return FAIL;
3676 }
3677
3678 /*
3679 * Get the third variable.
3680 */
3681 *arg = skipwhite(*arg + 1);
3682 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3683 {
3684 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003685 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 return FAIL;
3687 }
3688 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003689 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
3691
3692 return OK;
3693}
3694
3695/*
3696 * Handle first level expression:
3697 * expr2 || expr2 || expr2 logical OR
3698 *
3699 * "arg" must point to the first non-white of the expression.
3700 * "arg" is advanced to the next non-white after the recognized expression.
3701 *
3702 * Return OK or FAIL.
3703 */
3704 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003705eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 int evaluate;
3709{
Bram Moolenaar33570922005-01-25 22:26:29 +00003710 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 long result;
3712 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003713 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714
3715 /*
3716 * Get the first variable.
3717 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003718 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 return FAIL;
3720
3721 /*
3722 * Repeat until there is no following "||".
3723 */
3724 first = TRUE;
3725 result = FALSE;
3726 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3727 {
3728 if (evaluate && first)
3729 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003730 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003732 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003733 if (error)
3734 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 first = FALSE;
3736 }
3737
3738 /*
3739 * Get the second variable.
3740 */
3741 *arg = skipwhite(*arg + 2);
3742 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3743 return FAIL;
3744
3745 /*
3746 * Compute the result.
3747 */
3748 if (evaluate && !result)
3749 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003750 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003752 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003753 if (error)
3754 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756 if (evaluate)
3757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003758 rettv->v_type = VAR_NUMBER;
3759 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
3761 }
3762
3763 return OK;
3764}
3765
3766/*
3767 * Handle second level expression:
3768 * expr3 && expr3 && expr3 logical AND
3769 *
3770 * "arg" must point to the first non-white of the expression.
3771 * "arg" is advanced to the next non-white after the recognized expression.
3772 *
3773 * Return OK or FAIL.
3774 */
3775 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003776eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 int evaluate;
3780{
Bram Moolenaar33570922005-01-25 22:26:29 +00003781 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 long result;
3783 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003784 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785
3786 /*
3787 * Get the first variable.
3788 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003789 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 return FAIL;
3791
3792 /*
3793 * Repeat until there is no following "&&".
3794 */
3795 first = TRUE;
3796 result = TRUE;
3797 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3798 {
3799 if (evaluate && first)
3800 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003801 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003803 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003804 if (error)
3805 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 first = FALSE;
3807 }
3808
3809 /*
3810 * Get the second variable.
3811 */
3812 *arg = skipwhite(*arg + 2);
3813 if (eval4(arg, &var2, evaluate && result) == FAIL)
3814 return FAIL;
3815
3816 /*
3817 * Compute the result.
3818 */
3819 if (evaluate && result)
3820 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003821 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003823 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003824 if (error)
3825 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 }
3827 if (evaluate)
3828 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003829 rettv->v_type = VAR_NUMBER;
3830 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832 }
3833
3834 return OK;
3835}
3836
3837/*
3838 * Handle third level expression:
3839 * var1 == var2
3840 * var1 =~ var2
3841 * var1 != var2
3842 * var1 !~ var2
3843 * var1 > var2
3844 * var1 >= var2
3845 * var1 < var2
3846 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003847 * var1 is var2
3848 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 *
3850 * "arg" must point to the first non-white of the expression.
3851 * "arg" is advanced to the next non-white after the recognized expression.
3852 *
3853 * Return OK or FAIL.
3854 */
3855 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003856eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 int evaluate;
3860{
Bram Moolenaar33570922005-01-25 22:26:29 +00003861 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 char_u *p;
3863 int i;
3864 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003865 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 int len = 2;
3867 long n1, n2;
3868 char_u *s1, *s2;
3869 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3870 regmatch_T regmatch;
3871 int ic;
3872 char_u *save_cpo;
3873
3874 /*
3875 * Get the first variable.
3876 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003877 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 return FAIL;
3879
3880 p = *arg;
3881 switch (p[0])
3882 {
3883 case '=': if (p[1] == '=')
3884 type = TYPE_EQUAL;
3885 else if (p[1] == '~')
3886 type = TYPE_MATCH;
3887 break;
3888 case '!': if (p[1] == '=')
3889 type = TYPE_NEQUAL;
3890 else if (p[1] == '~')
3891 type = TYPE_NOMATCH;
3892 break;
3893 case '>': if (p[1] != '=')
3894 {
3895 type = TYPE_GREATER;
3896 len = 1;
3897 }
3898 else
3899 type = TYPE_GEQUAL;
3900 break;
3901 case '<': if (p[1] != '=')
3902 {
3903 type = TYPE_SMALLER;
3904 len = 1;
3905 }
3906 else
3907 type = TYPE_SEQUAL;
3908 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003909 case 'i': if (p[1] == 's')
3910 {
3911 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3912 len = 5;
3913 if (!vim_isIDc(p[len]))
3914 {
3915 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3916 type_is = TRUE;
3917 }
3918 }
3919 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921
3922 /*
3923 * If there is a comparitive operator, use it.
3924 */
3925 if (type != TYPE_UNKNOWN)
3926 {
3927 /* extra question mark appended: ignore case */
3928 if (p[len] == '?')
3929 {
3930 ic = TRUE;
3931 ++len;
3932 }
3933 /* extra '#' appended: match case */
3934 else if (p[len] == '#')
3935 {
3936 ic = FALSE;
3937 ++len;
3938 }
3939 /* nothing appened: use 'ignorecase' */
3940 else
3941 ic = p_ic;
3942
3943 /*
3944 * Get the second variable.
3945 */
3946 *arg = skipwhite(p + len);
3947 if (eval5(arg, &var2, evaluate) == FAIL)
3948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003949 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 return FAIL;
3951 }
3952
3953 if (evaluate)
3954 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003955 if (type_is && rettv->v_type != var2.v_type)
3956 {
3957 /* For "is" a different type always means FALSE, for "notis"
3958 * it means TRUE. */
3959 n1 = (type == TYPE_NEQUAL);
3960 }
3961 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3962 {
3963 if (type_is)
3964 {
3965 n1 = (rettv->v_type == var2.v_type
3966 && rettv->vval.v_list == var2.vval.v_list);
3967 if (type == TYPE_NEQUAL)
3968 n1 = !n1;
3969 }
3970 else if (rettv->v_type != var2.v_type
3971 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3972 {
3973 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003974 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003975 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003976 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003977 clear_tv(rettv);
3978 clear_tv(&var2);
3979 return FAIL;
3980 }
3981 else
3982 {
3983 /* Compare two Lists for being equal or unequal. */
3984 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3985 if (type == TYPE_NEQUAL)
3986 n1 = !n1;
3987 }
3988 }
3989
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003990 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3991 {
3992 if (type_is)
3993 {
3994 n1 = (rettv->v_type == var2.v_type
3995 && rettv->vval.v_dict == var2.vval.v_dict);
3996 if (type == TYPE_NEQUAL)
3997 n1 = !n1;
3998 }
3999 else if (rettv->v_type != var2.v_type
4000 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4001 {
4002 if (rettv->v_type != var2.v_type)
4003 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4004 else
4005 EMSG(_("E736: Invalid operation for Dictionary"));
4006 clear_tv(rettv);
4007 clear_tv(&var2);
4008 return FAIL;
4009 }
4010 else
4011 {
4012 /* Compare two Dictionaries for being equal or unequal. */
4013 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4014 if (type == TYPE_NEQUAL)
4015 n1 = !n1;
4016 }
4017 }
4018
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004019 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4020 {
4021 if (rettv->v_type != var2.v_type
4022 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4023 {
4024 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004025 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004026 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004027 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004028 clear_tv(rettv);
4029 clear_tv(&var2);
4030 return FAIL;
4031 }
4032 else
4033 {
4034 /* Compare two Funcrefs for being equal or unequal. */
4035 if (rettv->vval.v_string == NULL
4036 || var2.vval.v_string == NULL)
4037 n1 = FALSE;
4038 else
4039 n1 = STRCMP(rettv->vval.v_string,
4040 var2.vval.v_string) == 0;
4041 if (type == TYPE_NEQUAL)
4042 n1 = !n1;
4043 }
4044 }
4045
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 /*
4047 * If one of the two variables is a number, compare as a number.
4048 * When using "=~" or "!~", always compare as string.
4049 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004050 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4052 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004053 n1 = get_tv_number(rettv);
4054 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 switch (type)
4056 {
4057 case TYPE_EQUAL: n1 = (n1 == n2); break;
4058 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4059 case TYPE_GREATER: n1 = (n1 > n2); break;
4060 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4061 case TYPE_SMALLER: n1 = (n1 < n2); break;
4062 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4063 case TYPE_UNKNOWN:
4064 case TYPE_MATCH:
4065 case TYPE_NOMATCH: break; /* avoid gcc warning */
4066 }
4067 }
4068 else
4069 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004070 s1 = get_tv_string_buf(rettv, buf1);
4071 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4073 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4074 else
4075 i = 0;
4076 n1 = FALSE;
4077 switch (type)
4078 {
4079 case TYPE_EQUAL: n1 = (i == 0); break;
4080 case TYPE_NEQUAL: n1 = (i != 0); break;
4081 case TYPE_GREATER: n1 = (i > 0); break;
4082 case TYPE_GEQUAL: n1 = (i >= 0); break;
4083 case TYPE_SMALLER: n1 = (i < 0); break;
4084 case TYPE_SEQUAL: n1 = (i <= 0); break;
4085
4086 case TYPE_MATCH:
4087 case TYPE_NOMATCH:
4088 /* avoid 'l' flag in 'cpoptions' */
4089 save_cpo = p_cpo;
4090 p_cpo = (char_u *)"";
4091 regmatch.regprog = vim_regcomp(s2,
4092 RE_MAGIC + RE_STRING);
4093 regmatch.rm_ic = ic;
4094 if (regmatch.regprog != NULL)
4095 {
4096 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4097 vim_free(regmatch.regprog);
4098 if (type == TYPE_NOMATCH)
4099 n1 = !n1;
4100 }
4101 p_cpo = save_cpo;
4102 break;
4103
4104 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4105 }
4106 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 clear_tv(rettv);
4108 clear_tv(&var2);
4109 rettv->v_type = VAR_NUMBER;
4110 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
4112 }
4113
4114 return OK;
4115}
4116
4117/*
4118 * Handle fourth level expression:
4119 * + number addition
4120 * - number subtraction
4121 * . string concatenation
4122 *
4123 * "arg" must point to the first non-white of the expression.
4124 * "arg" is advanced to the next non-white after the recognized expression.
4125 *
4126 * Return OK or FAIL.
4127 */
4128 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 int evaluate;
4133{
Bram Moolenaar33570922005-01-25 22:26:29 +00004134 typval_T var2;
4135 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 int op;
4137 long n1, n2;
4138 char_u *s1, *s2;
4139 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4140 char_u *p;
4141
4142 /*
4143 * Get the first variable.
4144 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004145 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 return FAIL;
4147
4148 /*
4149 * Repeat computing, until no '+', '-' or '.' is following.
4150 */
4151 for (;;)
4152 {
4153 op = **arg;
4154 if (op != '+' && op != '-' && op != '.')
4155 break;
4156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004157 if (op != '+' || rettv->v_type != VAR_LIST)
4158 {
4159 /* For "list + ...", an illegal use of the first operand as
4160 * a number cannot be determined before evaluating the 2nd
4161 * operand: if this is also a list, all is ok.
4162 * For "something . ...", "something - ..." or "non-list + ...",
4163 * we know that the first operand needs to be a string or number
4164 * without evaluating the 2nd operand. So check before to avoid
4165 * side effects after an error. */
4166 if (evaluate && get_tv_string_chk(rettv) == NULL)
4167 {
4168 clear_tv(rettv);
4169 return FAIL;
4170 }
4171 }
4172
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 /*
4174 * Get the second variable.
4175 */
4176 *arg = skipwhite(*arg + 1);
4177 if (eval6(arg, &var2, evaluate) == FAIL)
4178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004179 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 return FAIL;
4181 }
4182
4183 if (evaluate)
4184 {
4185 /*
4186 * Compute the result.
4187 */
4188 if (op == '.')
4189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4191 s2 = get_tv_string_buf_chk(&var2, buf2);
4192 if (s2 == NULL) /* type error ? */
4193 {
4194 clear_tv(rettv);
4195 clear_tv(&var2);
4196 return FAIL;
4197 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004198 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004199 clear_tv(rettv);
4200 rettv->v_type = VAR_STRING;
4201 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004203 else if (op == '+' && rettv->v_type == VAR_LIST
4204 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004205 {
4206 /* concatenate Lists */
4207 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4208 &var3) == FAIL)
4209 {
4210 clear_tv(rettv);
4211 clear_tv(&var2);
4212 return FAIL;
4213 }
4214 clear_tv(rettv);
4215 *rettv = var3;
4216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 else
4218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004219 int error = FALSE;
4220
4221 n1 = get_tv_number_chk(rettv, &error);
4222 if (error)
4223 {
4224 /* This can only happen for "list + non-list".
4225 * For "non-list + ..." or "something - ...", we returned
4226 * before evaluating the 2nd operand. */
4227 clear_tv(rettv);
4228 return FAIL;
4229 }
4230 n2 = get_tv_number_chk(&var2, &error);
4231 if (error)
4232 {
4233 clear_tv(rettv);
4234 clear_tv(&var2);
4235 return FAIL;
4236 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 if (op == '+')
4239 n1 = n1 + n2;
4240 else
4241 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004242 rettv->v_type = VAR_NUMBER;
4243 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247 }
4248 return OK;
4249}
4250
4251/*
4252 * Handle fifth level expression:
4253 * * number multiplication
4254 * / number division
4255 * % number modulo
4256 *
4257 * "arg" must point to the first non-white of the expression.
4258 * "arg" is advanced to the next non-white after the recognized expression.
4259 *
4260 * Return OK or FAIL.
4261 */
4262 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 int evaluate;
4267{
Bram Moolenaar33570922005-01-25 22:26:29 +00004268 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 int op;
4270 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
4273 /*
4274 * Get the first variable.
4275 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004276 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 return FAIL;
4278
4279 /*
4280 * Repeat computing, until no '*', '/' or '%' is following.
4281 */
4282 for (;;)
4283 {
4284 op = **arg;
4285 if (op != '*' && op != '/' && op != '%')
4286 break;
4287
4288 if (evaluate)
4289 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004290 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004292 if (error)
4293 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
4295 else
4296 n1 = 0;
4297
4298 /*
4299 * Get the second variable.
4300 */
4301 *arg = skipwhite(*arg + 1);
4302 if (eval7(arg, &var2, evaluate) == FAIL)
4303 return FAIL;
4304
4305 if (evaluate)
4306 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004307 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004308 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004309 if (error)
4310 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
4312 /*
4313 * Compute the result.
4314 */
4315 if (op == '*')
4316 n1 = n1 * n2;
4317 else if (op == '/')
4318 {
4319 if (n2 == 0) /* give an error message? */
4320 n1 = 0x7fffffffL;
4321 else
4322 n1 = n1 / n2;
4323 }
4324 else
4325 {
4326 if (n2 == 0) /* give an error message? */
4327 n1 = 0;
4328 else
4329 n1 = n1 % n2;
4330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004331 rettv->v_type = VAR_NUMBER;
4332 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
4334 }
4335
4336 return OK;
4337}
4338
4339/*
4340 * Handle sixth level expression:
4341 * number number constant
4342 * "string" string contstant
4343 * 'string' literal string contstant
4344 * &option-name option value
4345 * @r register contents
4346 * identifier variable value
4347 * function() function call
4348 * $VAR environment variable
4349 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004350 * [expr, expr] List
4351 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 *
4353 * Also handle:
4354 * ! in front logical NOT
4355 * - in front unary minus
4356 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004357 * trailing [] subscript in String or List
4358 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 *
4360 * "arg" must point to the first non-white of the expression.
4361 * "arg" is advanced to the next non-white after the recognized expression.
4362 *
4363 * Return OK or FAIL.
4364 */
4365 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 int evaluate;
4370{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 long n;
4372 int len;
4373 char_u *s;
4374 int val;
4375 char_u *start_leader, *end_leader;
4376 int ret = OK;
4377 char_u *alias;
4378
4379 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004380 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004381 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004383 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
4385 /*
4386 * Skip '!' and '-' characters. They are handled later.
4387 */
4388 start_leader = *arg;
4389 while (**arg == '!' || **arg == '-' || **arg == '+')
4390 *arg = skipwhite(*arg + 1);
4391 end_leader = *arg;
4392
4393 switch (**arg)
4394 {
4395 /*
4396 * Number constant.
4397 */
4398 case '0':
4399 case '1':
4400 case '2':
4401 case '3':
4402 case '4':
4403 case '5':
4404 case '6':
4405 case '7':
4406 case '8':
4407 case '9':
4408 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4409 *arg += len;
4410 if (evaluate)
4411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004412 rettv->v_type = VAR_NUMBER;
4413 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415 break;
4416
4417 /*
4418 * String constant: "string".
4419 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004420 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 break;
4422
4423 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004424 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004426 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004427 break;
4428
4429 /*
4430 * List: [expr, expr]
4431 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004432 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 break;
4434
4435 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004436 * Dictionary: {key: val, key: val}
4437 */
4438 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4439 break;
4440
4441 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004442 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004444 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 break;
4446
4447 /*
4448 * Environment variable: $VAR.
4449 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004450 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 break;
4452
4453 /*
4454 * Register contents: @r.
4455 */
4456 case '@': ++*arg;
4457 if (evaluate)
4458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004459 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004460 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
4462 if (**arg != NUL)
4463 ++*arg;
4464 break;
4465
4466 /*
4467 * nested expression: (expression).
4468 */
4469 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 if (**arg == ')')
4472 ++*arg;
4473 else if (ret == OK)
4474 {
4475 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004476 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 ret = FAIL;
4478 }
4479 break;
4480
Bram Moolenaar8c711452005-01-14 21:53:12 +00004481 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 break;
4483 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004484
4485 if (ret == NOTDONE)
4486 {
4487 /*
4488 * Must be a variable or function name.
4489 * Can also be a curly-braces kind of name: {expr}.
4490 */
4491 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004492 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004493 if (alias != NULL)
4494 s = alias;
4495
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004496 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004497 ret = FAIL;
4498 else
4499 {
4500 if (**arg == '(') /* recursive! */
4501 {
4502 /* If "s" is the name of a variable of type VAR_FUNC
4503 * use its contents. */
4504 s = deref_func_name(s, &len);
4505
4506 /* Invoke the function. */
4507 ret = get_func_tv(s, len, rettv, arg,
4508 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004509 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004510 /* Stop the expression evaluation when immediately
4511 * aborting on error, or when an interrupt occurred or
4512 * an exception was thrown but not caught. */
4513 if (aborting())
4514 {
4515 if (ret == OK)
4516 clear_tv(rettv);
4517 ret = FAIL;
4518 }
4519 }
4520 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004521 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004522 else
4523 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004524 }
4525
4526 if (alias != NULL)
4527 vim_free(alias);
4528 }
4529
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 *arg = skipwhite(*arg);
4531
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004532 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4533 * expr(expr). */
4534 if (ret == OK)
4535 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
4537 /*
4538 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4539 */
4540 if (ret == OK && evaluate && end_leader > start_leader)
4541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004542 int error = FALSE;
4543
4544 val = get_tv_number_chk(rettv, &error);
4545 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004547 clear_tv(rettv);
4548 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004550 else
4551 {
4552 while (end_leader > start_leader)
4553 {
4554 --end_leader;
4555 if (*end_leader == '!')
4556 val = !val;
4557 else if (*end_leader == '-')
4558 val = -val;
4559 }
4560 clear_tv(rettv);
4561 rettv->v_type = VAR_NUMBER;
4562 rettv->vval.v_number = val;
4563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 }
4565
4566 return ret;
4567}
4568
4569/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004570 * Evaluate an "[expr]" or "[expr:expr]" index.
4571 * "*arg" points to the '['.
4572 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4573 */
4574 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004575eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004576 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004577 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004578 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004579 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004580{
4581 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004582 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004584 long len = -1;
4585 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004586 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004587 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004588
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004589 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004591 if (verbose)
4592 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 return FAIL;
4594 }
4595
Bram Moolenaar8c711452005-01-14 21:53:12 +00004596 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004597 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004598 /*
4599 * dict.name
4600 */
4601 key = *arg + 1;
4602 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4603 ;
4604 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004605 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004606 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004607 }
4608 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004610 /*
4611 * something[idx]
4612 *
4613 * Get the (first) variable from inside the [].
4614 */
4615 *arg = skipwhite(*arg + 1);
4616 if (**arg == ':')
4617 empty1 = TRUE;
4618 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4619 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004620 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4621 {
4622 /* not a number or string */
4623 clear_tv(&var1);
4624 return FAIL;
4625 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004626
4627 /*
4628 * Get the second variable from inside the [:].
4629 */
4630 if (**arg == ':')
4631 {
4632 range = TRUE;
4633 *arg = skipwhite(*arg + 1);
4634 if (**arg == ']')
4635 empty2 = TRUE;
4636 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4637 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004638 if (!empty1)
4639 clear_tv(&var1);
4640 return FAIL;
4641 }
4642 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4643 {
4644 /* not a number or string */
4645 if (!empty1)
4646 clear_tv(&var1);
4647 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004648 return FAIL;
4649 }
4650 }
4651
4652 /* Check for the ']'. */
4653 if (**arg != ']')
4654 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004655 if (verbose)
4656 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004657 clear_tv(&var1);
4658 if (range)
4659 clear_tv(&var2);
4660 return FAIL;
4661 }
4662 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004663 }
4664
4665 if (evaluate)
4666 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004667 n1 = 0;
4668 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004670 n1 = get_tv_number(&var1);
4671 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004672 }
4673 if (range)
4674 {
4675 if (empty2)
4676 n2 = -1;
4677 else
4678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004679 n2 = get_tv_number(&var2);
4680 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004681 }
4682 }
4683
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004684 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004685 {
4686 case VAR_NUMBER:
4687 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004688 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004689 len = (long)STRLEN(s);
4690 if (range)
4691 {
4692 /* The resulting variable is a substring. If the indexes
4693 * are out of range the result is empty. */
4694 if (n1 < 0)
4695 {
4696 n1 = len + n1;
4697 if (n1 < 0)
4698 n1 = 0;
4699 }
4700 if (n2 < 0)
4701 n2 = len + n2;
4702 else if (n2 >= len)
4703 n2 = len;
4704 if (n1 >= len || n2 < 0 || n1 > n2)
4705 s = NULL;
4706 else
4707 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4708 }
4709 else
4710 {
4711 /* The resulting variable is a string of a single
4712 * character. If the index is too big or negative the
4713 * result is empty. */
4714 if (n1 >= len || n1 < 0)
4715 s = NULL;
4716 else
4717 s = vim_strnsave(s + n1, 1);
4718 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004719 clear_tv(rettv);
4720 rettv->v_type = VAR_STRING;
4721 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722 break;
4723
4724 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004725 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726 if (n1 < 0)
4727 n1 = len + n1;
4728 if (!empty1 && (n1 < 0 || n1 >= len))
4729 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004730 if (verbose)
4731 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004732 return FAIL;
4733 }
4734 if (range)
4735 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004736 list_T *l;
4737 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738
4739 if (n2 < 0)
4740 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004741 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004742 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004743 if (verbose)
4744 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 return FAIL;
4746 }
4747 l = list_alloc();
4748 if (l == NULL)
4749 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004750 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004751 n1 <= n2; ++n1)
4752 {
4753 if (list_append_tv(l, &item->li_tv) == FAIL)
4754 {
4755 list_free(l);
4756 return FAIL;
4757 }
4758 item = item->li_next;
4759 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004760 clear_tv(rettv);
4761 rettv->v_type = VAR_LIST;
4762 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004763 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764 }
4765 else
4766 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004767 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004769 clear_tv(rettv);
4770 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004771 }
4772 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004773
4774 case VAR_DICT:
4775 if (range)
4776 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004777 if (verbose)
4778 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004779 if (len == -1)
4780 clear_tv(&var1);
4781 return FAIL;
4782 }
4783 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004784 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004785
4786 if (len == -1)
4787 {
4788 key = get_tv_string(&var1);
4789 if (*key == NUL)
4790 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004791 if (verbose)
4792 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004793 clear_tv(&var1);
4794 return FAIL;
4795 }
4796 }
4797
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004798 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004799
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004800 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004801 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004802 if (len == -1)
4803 clear_tv(&var1);
4804 if (item == NULL)
4805 return FAIL;
4806
4807 copy_tv(&item->di_tv, &var1);
4808 clear_tv(rettv);
4809 *rettv = var1;
4810 }
4811 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004812 }
4813 }
4814
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004815 return OK;
4816}
4817
4818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 * Get an option value.
4820 * "arg" points to the '&' or '+' before the option name.
4821 * "arg" is advanced to character after the option name.
4822 * Return OK or FAIL.
4823 */
4824 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004825get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004827 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 int evaluate;
4829{
4830 char_u *option_end;
4831 long numval;
4832 char_u *stringval;
4833 int opt_type;
4834 int c;
4835 int working = (**arg == '+'); /* has("+option") */
4836 int ret = OK;
4837 int opt_flags;
4838
4839 /*
4840 * Isolate the option name and find its value.
4841 */
4842 option_end = find_option_end(arg, &opt_flags);
4843 if (option_end == NULL)
4844 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004845 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 EMSG2(_("E112: Option name missing: %s"), *arg);
4847 return FAIL;
4848 }
4849
4850 if (!evaluate)
4851 {
4852 *arg = option_end;
4853 return OK;
4854 }
4855
4856 c = *option_end;
4857 *option_end = NUL;
4858 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004859 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860
4861 if (opt_type == -3) /* invalid name */
4862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004863 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 EMSG2(_("E113: Unknown option: %s"), *arg);
4865 ret = FAIL;
4866 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004867 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 {
4869 if (opt_type == -2) /* hidden string option */
4870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004871 rettv->v_type = VAR_STRING;
4872 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874 else if (opt_type == -1) /* hidden number option */
4875 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004876 rettv->v_type = VAR_NUMBER;
4877 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879 else if (opt_type == 1) /* number option */
4880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004881 rettv->v_type = VAR_NUMBER;
4882 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
4884 else /* string option */
4885 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004886 rettv->v_type = VAR_STRING;
4887 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889 }
4890 else if (working && (opt_type == -2 || opt_type == -1))
4891 ret = FAIL;
4892
4893 *option_end = c; /* put back for error messages */
4894 *arg = option_end;
4895
4896 return ret;
4897}
4898
4899/*
4900 * Allocate a variable for a string constant.
4901 * Return OK or FAIL.
4902 */
4903 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 int evaluate;
4908{
4909 char_u *p;
4910 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 int extra = 0;
4912
4913 /*
4914 * Find the end of the string, skipping backslashed characters.
4915 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004916 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
4918 if (*p == '\\' && p[1] != NUL)
4919 {
4920 ++p;
4921 /* A "\<x>" form occupies at least 4 characters, and produces up
4922 * to 6 characters: reserve space for 2 extra */
4923 if (*p == '<')
4924 extra += 2;
4925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
4927
4928 if (*p != '"')
4929 {
4930 EMSG2(_("E114: Missing quote: %s"), *arg);
4931 return FAIL;
4932 }
4933
4934 /* If only parsing, set *arg and return here */
4935 if (!evaluate)
4936 {
4937 *arg = p + 1;
4938 return OK;
4939 }
4940
4941 /*
4942 * Copy the string into allocated memory, handling backslashed
4943 * characters.
4944 */
4945 name = alloc((unsigned)(p - *arg + extra));
4946 if (name == NULL)
4947 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004948 rettv->v_type = VAR_STRING;
4949 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
Bram Moolenaar8c711452005-01-14 21:53:12 +00004951 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 {
4953 if (*p == '\\')
4954 {
4955 switch (*++p)
4956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004957 case 'b': *name++ = BS; ++p; break;
4958 case 'e': *name++ = ESC; ++p; break;
4959 case 'f': *name++ = FF; ++p; break;
4960 case 'n': *name++ = NL; ++p; break;
4961 case 'r': *name++ = CAR; ++p; break;
4962 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963
4964 case 'X': /* hex: "\x1", "\x12" */
4965 case 'x':
4966 case 'u': /* Unicode: "\u0023" */
4967 case 'U':
4968 if (vim_isxdigit(p[1]))
4969 {
4970 int n, nr;
4971 int c = toupper(*p);
4972
4973 if (c == 'X')
4974 n = 2;
4975 else
4976 n = 4;
4977 nr = 0;
4978 while (--n >= 0 && vim_isxdigit(p[1]))
4979 {
4980 ++p;
4981 nr = (nr << 4) + hex2nr(*p);
4982 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984#ifdef FEAT_MBYTE
4985 /* For "\u" store the number according to
4986 * 'encoding'. */
4987 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 else
4990#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004991 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 break;
4994
4995 /* octal: "\1", "\12", "\123" */
4996 case '0':
4997 case '1':
4998 case '2':
4999 case '3':
5000 case '4':
5001 case '5':
5002 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005003 case '7': *name = *p++ - '0';
5004 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005006 *name = (*name << 3) + *p++ - '0';
5007 if (*p >= '0' && *p <= '7')
5008 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005010 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 break;
5012
5013 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 if (extra != 0)
5016 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005017 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 break;
5019 }
5020 /* FALLTHROUGH */
5021
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 break;
5024 }
5025 }
5026 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005030 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 *arg = p + 1;
5032
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 return OK;
5034}
5035
5036/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005037 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 * Return OK or FAIL.
5039 */
5040 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005041get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 int evaluate;
5045{
5046 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005047 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005048 int reduce = 0;
5049
5050 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005052 */
5053 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5054 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005056 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005058 break;
5059 ++reduce;
5060 ++p;
5061 }
5062 }
5063
Bram Moolenaar8c711452005-01-14 21:53:12 +00005064 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005065 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005067 return FAIL;
5068 }
5069
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005071 if (!evaluate)
5072 {
5073 *arg = p + 1;
5074 return OK;
5075 }
5076
5077 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005078 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005079 */
5080 str = alloc((unsigned)((p - *arg) - reduce));
5081 if (str == NULL)
5082 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005083 rettv->v_type = VAR_STRING;
5084 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005085
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005087 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005089 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005091 break;
5092 ++p;
5093 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005095 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005097 *arg = p + 1;
5098
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005099 return OK;
5100}
5101
5102/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005103 * Allocate a variable for a List and fill it from "*arg".
5104 * Return OK or FAIL.
5105 */
5106 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005107get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005108 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005109 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005110 int evaluate;
5111{
Bram Moolenaar33570922005-01-25 22:26:29 +00005112 list_T *l = NULL;
5113 typval_T tv;
5114 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005115
5116 if (evaluate)
5117 {
5118 l = list_alloc();
5119 if (l == NULL)
5120 return FAIL;
5121 }
5122
5123 *arg = skipwhite(*arg + 1);
5124 while (**arg != ']' && **arg != NUL)
5125 {
5126 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5127 goto failret;
5128 if (evaluate)
5129 {
5130 item = listitem_alloc();
5131 if (item != NULL)
5132 {
5133 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005134 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005135 list_append(l, item);
5136 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005137 else
5138 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005139 }
5140
5141 if (**arg == ']')
5142 break;
5143 if (**arg != ',')
5144 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005146 goto failret;
5147 }
5148 *arg = skipwhite(*arg + 1);
5149 }
5150
5151 if (**arg != ']')
5152 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005154failret:
5155 if (evaluate)
5156 list_free(l);
5157 return FAIL;
5158 }
5159
5160 *arg = skipwhite(*arg + 1);
5161 if (evaluate)
5162 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005163 rettv->v_type = VAR_LIST;
5164 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165 ++l->lv_refcount;
5166 }
5167
5168 return OK;
5169}
5170
5171/*
5172 * Allocate an empty header for a list.
5173 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005174 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005175list_alloc()
5176{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005177 list_T *l;
5178
5179 l = (list_T *)alloc_clear(sizeof(list_T));
5180 if (l != NULL)
5181 {
5182 /* Prepend the list to the list of lists for garbage collection. */
5183 if (first_list != NULL)
5184 first_list->lv_used_prev = l;
5185 l->lv_used_prev = NULL;
5186 l->lv_used_next = first_list;
5187 first_list = l;
5188 }
5189 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005190}
5191
5192/*
5193 * Unreference a list: decrement the reference count and free it when it
5194 * becomes zero.
5195 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005196 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005197list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005198 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005199{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005200 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5201 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005202}
5203
5204/*
5205 * Free a list, including all items it points to.
5206 * Ignores the reference count.
5207 */
5208 static void
5209list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005210 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005211{
Bram Moolenaar33570922005-01-25 22:26:29 +00005212 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005213
Bram Moolenaard9fba312005-06-26 22:34:35 +00005214 /* Avoid that recursive reference to the list frees us again. */
5215 l->lv_refcount = DEL_REFCOUNT;
5216
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005217 /* Remove the list from the list of lists for garbage collection. */
5218 if (l->lv_used_prev == NULL)
5219 first_list = l->lv_used_next;
5220 else
5221 l->lv_used_prev->lv_used_next = l->lv_used_next;
5222 if (l->lv_used_next != NULL)
5223 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5224
Bram Moolenaard9fba312005-06-26 22:34:35 +00005225 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005227 /* Remove the item before deleting it. */
5228 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005229 listitem_free(item);
5230 }
5231 vim_free(l);
5232}
5233
5234/*
5235 * Allocate a list item.
5236 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005237 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238listitem_alloc()
5239{
Bram Moolenaar33570922005-01-25 22:26:29 +00005240 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241}
5242
5243/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005244 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005245 */
5246 static void
5247listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005248 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005250 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 vim_free(item);
5252}
5253
5254/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005255 * Remove a list item from a List and free it. Also clears the value.
5256 */
5257 static void
5258listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005259 list_T *l;
5260 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005261{
5262 list_remove(l, item, item);
5263 listitem_free(item);
5264}
5265
5266/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267 * Get the number of items in a list.
5268 */
5269 static long
5270list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005271 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 if (l == NULL)
5274 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005275 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276}
5277
5278/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005279 * Return TRUE when two lists have exactly the same values.
5280 */
5281 static int
5282list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005283 list_T *l1;
5284 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005285 int ic; /* ignore case for strings */
5286{
Bram Moolenaar33570922005-01-25 22:26:29 +00005287 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005288
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005289 if (list_len(l1) != list_len(l2))
5290 return FALSE;
5291
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005292 for (item1 = l1->lv_first, item2 = l2->lv_first;
5293 item1 != NULL && item2 != NULL;
5294 item1 = item1->li_next, item2 = item2->li_next)
5295 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5296 return FALSE;
5297 return item1 == NULL && item2 == NULL;
5298}
5299
5300/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005301 * Return TRUE when two dictionaries have exactly the same key/values.
5302 */
5303 static int
5304dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005305 dict_T *d1;
5306 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005307 int ic; /* ignore case for strings */
5308{
Bram Moolenaar33570922005-01-25 22:26:29 +00005309 hashitem_T *hi;
5310 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005311 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005312
5313 if (dict_len(d1) != dict_len(d2))
5314 return FALSE;
5315
Bram Moolenaar33570922005-01-25 22:26:29 +00005316 todo = d1->dv_hashtab.ht_used;
5317 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005318 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005319 if (!HASHITEM_EMPTY(hi))
5320 {
5321 item2 = dict_find(d2, hi->hi_key, -1);
5322 if (item2 == NULL)
5323 return FALSE;
5324 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5325 return FALSE;
5326 --todo;
5327 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005328 }
5329 return TRUE;
5330}
5331
5332/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005333 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005334 * Compares the items just like "==" would compare them, but strings and
5335 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005336 */
5337 static int
5338tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005339 typval_T *tv1;
5340 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005341 int ic; /* ignore case */
5342{
5343 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005344 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005345
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005346 if (tv1->v_type != tv2->v_type)
5347 return FALSE;
5348
5349 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005350 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005351 case VAR_LIST:
5352 /* recursive! */
5353 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5354
5355 case VAR_DICT:
5356 /* recursive! */
5357 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5358
5359 case VAR_FUNC:
5360 return (tv1->vval.v_string != NULL
5361 && tv2->vval.v_string != NULL
5362 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5363
5364 case VAR_NUMBER:
5365 return tv1->vval.v_number == tv2->vval.v_number;
5366
5367 case VAR_STRING:
5368 s1 = get_tv_string_buf(tv1, buf1);
5369 s2 = get_tv_string_buf(tv2, buf2);
5370 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005371 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005372
5373 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005374 return TRUE;
5375}
5376
5377/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 * Locate item with index "n" in list "l" and return it.
5379 * A negative index is counted from the end; -1 is the last item.
5380 * Returns NULL when "n" is out of range.
5381 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005382 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005384 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 long n;
5386{
Bram Moolenaar33570922005-01-25 22:26:29 +00005387 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 long idx;
5389
5390 if (l == NULL)
5391 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005392
5393 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005395 n = l->lv_len + n;
5396
5397 /* Check for index out of range. */
5398 if (n < 0 || n >= l->lv_len)
5399 return NULL;
5400
5401 /* When there is a cached index may start search from there. */
5402 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005404 if (n < l->lv_idx / 2)
5405 {
5406 /* closest to the start of the list */
5407 item = l->lv_first;
5408 idx = 0;
5409 }
5410 else if (n > (l->lv_idx + l->lv_len) / 2)
5411 {
5412 /* closest to the end of the list */
5413 item = l->lv_last;
5414 idx = l->lv_len - 1;
5415 }
5416 else
5417 {
5418 /* closest to the cached index */
5419 item = l->lv_idx_item;
5420 idx = l->lv_idx;
5421 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 }
5423 else
5424 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005425 if (n < l->lv_len / 2)
5426 {
5427 /* closest to the start of the list */
5428 item = l->lv_first;
5429 idx = 0;
5430 }
5431 else
5432 {
5433 /* closest to the end of the list */
5434 item = l->lv_last;
5435 idx = l->lv_len - 1;
5436 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005438
5439 while (n > idx)
5440 {
5441 /* search forward */
5442 item = item->li_next;
5443 ++idx;
5444 }
5445 while (n < idx)
5446 {
5447 /* search backward */
5448 item = item->li_prev;
5449 --idx;
5450 }
5451
5452 /* cache the used index */
5453 l->lv_idx = idx;
5454 l->lv_idx_item = item;
5455
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 return item;
5457}
5458
5459/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005460 * Locate "item" list "l" and return its index.
5461 * Returns -1 when "item" is not in the list.
5462 */
5463 static long
5464list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005465 list_T *l;
5466 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005467{
5468 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005469 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005470
5471 if (l == NULL)
5472 return -1;
5473 idx = 0;
5474 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5475 ++idx;
5476 if (li == NULL)
5477 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005478 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005479}
5480
5481/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 * Append item "item" to the end of list "l".
5483 */
5484 static void
5485list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005486 list_T *l;
5487 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005488{
5489 if (l->lv_last == NULL)
5490 {
5491 /* empty list */
5492 l->lv_first = item;
5493 l->lv_last = item;
5494 item->li_prev = NULL;
5495 }
5496 else
5497 {
5498 l->lv_last->li_next = item;
5499 item->li_prev = l->lv_last;
5500 l->lv_last = item;
5501 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005502 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 item->li_next = NULL;
5504}
5505
5506/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005507 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005508 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 */
5510 static int
5511list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005512 list_T *l;
5513 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005514{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005515 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516
Bram Moolenaar05159a02005-02-26 23:04:13 +00005517 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005519 copy_tv(tv, &li->li_tv);
5520 list_append(l, li);
5521 return OK;
5522}
5523
5524/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005525 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005526 * Return FAIL when out of memory.
5527 */
5528 int
5529list_append_dict(list, dict)
5530 list_T *list;
5531 dict_T *dict;
5532{
5533 listitem_T *li = listitem_alloc();
5534
5535 if (li == NULL)
5536 return FAIL;
5537 li->li_tv.v_type = VAR_DICT;
5538 li->li_tv.v_lock = 0;
5539 li->li_tv.vval.v_dict = dict;
5540 list_append(list, li);
5541 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005542 return OK;
5543}
5544
5545/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005546 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005547 * If "item" is NULL append at the end.
5548 * Return FAIL when out of memory.
5549 */
5550 static int
5551list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005552 list_T *l;
5553 typval_T *tv;
5554 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005555{
Bram Moolenaar33570922005-01-25 22:26:29 +00005556 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005557
5558 if (ni == NULL)
5559 return FAIL;
5560 copy_tv(tv, &ni->li_tv);
5561 if (item == NULL)
5562 /* Append new item at end of list. */
5563 list_append(l, ni);
5564 else
5565 {
5566 /* Insert new item before existing item. */
5567 ni->li_prev = item->li_prev;
5568 ni->li_next = item;
5569 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005570 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005571 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005572 ++l->lv_idx;
5573 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005574 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005575 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005576 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005577 l->lv_idx_item = NULL;
5578 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005579 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005580 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005581 }
5582 return OK;
5583}
5584
5585/*
5586 * Extend "l1" with "l2".
5587 * If "bef" is NULL append at the end, otherwise insert before this item.
5588 * Returns FAIL when out of memory.
5589 */
5590 static int
5591list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005592 list_T *l1;
5593 list_T *l2;
5594 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005595{
Bram Moolenaar33570922005-01-25 22:26:29 +00005596 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005597
5598 for (item = l2->lv_first; item != NULL; item = item->li_next)
5599 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5600 return FAIL;
5601 return OK;
5602}
5603
5604/*
5605 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5606 * Return FAIL when out of memory.
5607 */
5608 static int
5609list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005610 list_T *l1;
5611 list_T *l2;
5612 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005613{
Bram Moolenaar33570922005-01-25 22:26:29 +00005614 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005615
5616 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005617 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005618 if (l == NULL)
5619 return FAIL;
5620 tv->v_type = VAR_LIST;
5621 tv->vval.v_list = l;
5622
5623 /* append all items from the second list */
5624 return list_extend(l, l2, NULL);
5625}
5626
5627/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005628 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005629 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005630 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005631 * Returns NULL when out of memory.
5632 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005633 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005634list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005635 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005636 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005637 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005638{
Bram Moolenaar33570922005-01-25 22:26:29 +00005639 list_T *copy;
5640 listitem_T *item;
5641 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005642
5643 if (orig == NULL)
5644 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005645
5646 copy = list_alloc();
5647 if (copy != NULL)
5648 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005649 if (copyID != 0)
5650 {
5651 /* Do this before adding the items, because one of the items may
5652 * refer back to this list. */
5653 orig->lv_copyID = copyID;
5654 orig->lv_copylist = copy;
5655 }
5656 for (item = orig->lv_first; item != NULL && !got_int;
5657 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005658 {
5659 ni = listitem_alloc();
5660 if (ni == NULL)
5661 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005662 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005663 {
5664 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5665 {
5666 vim_free(ni);
5667 break;
5668 }
5669 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005670 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005672 list_append(copy, ni);
5673 }
5674 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005675 if (item != NULL)
5676 {
5677 list_unref(copy);
5678 copy = NULL;
5679 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005680 }
5681
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005682 return copy;
5683}
5684
5685/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005686 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005687 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005688 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005689 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005690list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005691 list_T *l;
5692 listitem_T *item;
5693 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005694{
Bram Moolenaar33570922005-01-25 22:26:29 +00005695 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005697 /* notify watchers */
5698 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005699 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005700 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005701 list_fix_watch(l, ip);
5702 if (ip == item2)
5703 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005704 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005705
5706 if (item2->li_next == NULL)
5707 l->lv_last = item->li_prev;
5708 else
5709 item2->li_next->li_prev = item->li_prev;
5710 if (item->li_prev == NULL)
5711 l->lv_first = item2->li_next;
5712 else
5713 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005714 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005715}
5716
5717/*
5718 * Return an allocated string with the string representation of a list.
5719 * May return NULL.
5720 */
5721 static char_u *
5722list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005723 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005724{
5725 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726
5727 if (tv->vval.v_list == NULL)
5728 return NULL;
5729 ga_init2(&ga, (int)sizeof(char), 80);
5730 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005731 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5732 {
5733 vim_free(ga.ga_data);
5734 return NULL;
5735 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005736 ga_append(&ga, ']');
5737 ga_append(&ga, NUL);
5738 return (char_u *)ga.ga_data;
5739}
5740
5741/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005742 * Join list "l" into a string in "*gap", using separator "sep".
5743 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005744 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005746 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747list_join(gap, l, sep, echo)
5748 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005749 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005750 char_u *sep;
5751 int echo;
5752{
5753 int first = TRUE;
5754 char_u *tofree;
5755 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005756 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005757 char_u *s;
5758
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005759 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 {
5761 if (first)
5762 first = FALSE;
5763 else
5764 ga_concat(gap, sep);
5765
5766 if (echo)
5767 s = echo_string(&item->li_tv, &tofree, numbuf);
5768 else
5769 s = tv2string(&item->li_tv, &tofree, numbuf);
5770 if (s != NULL)
5771 ga_concat(gap, s);
5772 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005773 if (s == NULL)
5774 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005775 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005776 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005777}
5778
5779/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005780 * Garbage collection for lists and dictionaries.
5781 *
5782 * We use reference counts to be able to free most items right away when they
5783 * are no longer used. But for composite items it's possible that it becomes
5784 * unused while the reference count is > 0: When there is a recursive
5785 * reference. Example:
5786 * :let l = [1, 2, 3]
5787 * :let d = {9: l}
5788 * :let l[1] = d
5789 *
5790 * Since this is quite unusual we handle this with garbage collection: every
5791 * once in a while find out which lists and dicts are not referenced from any
5792 * variable.
5793 *
5794 * Here is a good reference text about garbage collection (refers to Python
5795 * but it applies to all reference-counting mechanisms):
5796 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005797 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005798
5799/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005800 * Do garbage collection for lists and dicts.
5801 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005802 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005803 int
5804garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005805{
5806 dict_T *dd;
5807 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005808 int copyID = ++current_copyID;
5809 buf_T *buf;
5810 win_T *wp;
5811 int i;
5812 funccall_T *fc;
5813 int did_free = FALSE;
5814
5815 /*
5816 * 1. Go through all accessible variables and mark all lists and dicts
5817 * with copyID.
5818 */
5819 /* script-local variables */
5820 for (i = 1; i <= ga_scripts.ga_len; ++i)
5821 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5822
5823 /* buffer-local variables */
5824 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5825 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5826
5827 /* window-local variables */
5828 FOR_ALL_WINDOWS(wp)
5829 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5830
5831 /* global variables */
5832 set_ref_in_ht(&globvarht, copyID);
5833
5834 /* function-local variables */
5835 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5836 {
5837 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5838 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5839 }
5840
5841 /*
5842 * 2. Go through the list of dicts and free items without the copyID.
5843 */
5844 for (dd = first_dict; dd != NULL; )
5845 if (dd->dv_copyID != copyID)
5846 {
5847 dict_free(dd);
5848 did_free = TRUE;
5849
5850 /* restart, next dict may also have been freed */
5851 dd = first_dict;
5852 }
5853 else
5854 dd = dd->dv_used_next;
5855
5856 /*
5857 * 3. Go through the list of lists and free items without the copyID.
5858 */
5859 for (ll = first_list; ll != NULL; )
5860 if (ll->lv_copyID != copyID)
5861 {
5862 list_free(ll);
5863 did_free = TRUE;
5864
5865 /* restart, next dict may also have been freed */
5866 ll = first_list;
5867 }
5868 else
5869 ll = ll->lv_used_next;
5870
5871 return did_free;
5872}
5873
5874/*
5875 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5876 */
5877 static void
5878set_ref_in_ht(ht, copyID)
5879 hashtab_T *ht;
5880 int copyID;
5881{
5882 int todo;
5883 hashitem_T *hi;
5884
5885 todo = ht->ht_used;
5886 for (hi = ht->ht_array; todo > 0; ++hi)
5887 if (!HASHITEM_EMPTY(hi))
5888 {
5889 --todo;
5890 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5891 }
5892}
5893
5894/*
5895 * Mark all lists and dicts referenced through list "l" with "copyID".
5896 */
5897 static void
5898set_ref_in_list(l, copyID)
5899 list_T *l;
5900 int copyID;
5901{
5902 listitem_T *li;
5903
5904 for (li = l->lv_first; li != NULL; li = li->li_next)
5905 set_ref_in_item(&li->li_tv, copyID);
5906}
5907
5908/*
5909 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5910 */
5911 static void
5912set_ref_in_item(tv, copyID)
5913 typval_T *tv;
5914 int copyID;
5915{
5916 dict_T *dd;
5917 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005918
5919 switch (tv->v_type)
5920 {
5921 case VAR_DICT:
5922 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005923 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005924 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005925 /* Didn't see this dict yet. */
5926 dd->dv_copyID = copyID;
5927 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005928 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005929 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005930
5931 case VAR_LIST:
5932 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005933 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005934 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005935 /* Didn't see this list yet. */
5936 ll->lv_copyID = copyID;
5937 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005938 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005939 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005940 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005941 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005942}
5943
5944/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945 * Allocate an empty header for a dictionary.
5946 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005947 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005948dict_alloc()
5949{
Bram Moolenaar33570922005-01-25 22:26:29 +00005950 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005951
Bram Moolenaar33570922005-01-25 22:26:29 +00005952 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005953 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005954 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005955 /* Add the list to the hashtable for garbage collection. */
5956 if (first_dict != NULL)
5957 first_dict->dv_used_prev = d;
5958 d->dv_used_next = first_dict;
5959 d->dv_used_prev = NULL;
5960
Bram Moolenaar33570922005-01-25 22:26:29 +00005961 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005962 d->dv_lock = 0;
5963 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005964 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005965 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005966 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005967}
5968
5969/*
5970 * Unreference a Dictionary: decrement the reference count and free it when it
5971 * becomes zero.
5972 */
5973 static void
5974dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005975 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005976{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005977 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
5978 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005979}
5980
5981/*
5982 * Free a Dictionary, including all items it contains.
5983 * Ignores the reference count.
5984 */
5985 static void
5986dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005987 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005988{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005989 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005990 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005991 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005992
Bram Moolenaard9fba312005-06-26 22:34:35 +00005993 /* Avoid that recursive reference to the dict frees us again. */
5994 d->dv_refcount = DEL_REFCOUNT;
5995
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005996 /* Remove the dict from the list of dicts for garbage collection. */
5997 if (d->dv_used_prev == NULL)
5998 first_dict = d->dv_used_next;
5999 else
6000 d->dv_used_prev->dv_used_next = d->dv_used_next;
6001 if (d->dv_used_next != NULL)
6002 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6003
6004 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006005 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006006 todo = d->dv_hashtab.ht_used;
6007 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006008 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006009 if (!HASHITEM_EMPTY(hi))
6010 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006011 /* Remove the item before deleting it, just in case there is
6012 * something recursive causing trouble. */
6013 di = HI2DI(hi);
6014 hash_remove(&d->dv_hashtab, hi);
6015 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006016 --todo;
6017 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006018 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006019 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006020 vim_free(d);
6021}
6022
6023/*
6024 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006025 * The "key" is copied to the new item.
6026 * Note that the value of the item "di_tv" still needs to be initialized!
6027 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006028 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006030dictitem_alloc(key)
6031 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006032{
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006034
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006036 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006037 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006038 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006039 di->di_flags = 0;
6040 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006041 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006042}
6043
6044/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006045 * Make a copy of a Dictionary item.
6046 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006047 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006048dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006050{
Bram Moolenaar33570922005-01-25 22:26:29 +00006051 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006052
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006054 if (di != NULL)
6055 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006056 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006057 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006058 copy_tv(&org->di_tv, &di->di_tv);
6059 }
6060 return di;
6061}
6062
6063/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006064 * Remove item "item" from Dictionary "dict" and free it.
6065 */
6066 static void
6067dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006068 dict_T *dict;
6069 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006070{
Bram Moolenaar33570922005-01-25 22:26:29 +00006071 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006072
Bram Moolenaar33570922005-01-25 22:26:29 +00006073 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006074 if (HASHITEM_EMPTY(hi))
6075 EMSG2(_(e_intern2), "dictitem_remove()");
6076 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006077 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006078 dictitem_free(item);
6079}
6080
6081/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006082 * Free a dict item. Also clears the value.
6083 */
6084 static void
6085dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006086 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006087{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006088 clear_tv(&item->di_tv);
6089 vim_free(item);
6090}
6091
6092/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006093 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6094 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006095 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006096 * Returns NULL when out of memory.
6097 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006098 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006099dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006100 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006101 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006102 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006103{
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 dict_T *copy;
6105 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006106 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006108
6109 if (orig == NULL)
6110 return NULL;
6111
6112 copy = dict_alloc();
6113 if (copy != NULL)
6114 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006115 if (copyID != 0)
6116 {
6117 orig->dv_copyID = copyID;
6118 orig->dv_copydict = copy;
6119 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006120 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006121 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006122 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006123 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006124 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006125 --todo;
6126
6127 di = dictitem_alloc(hi->hi_key);
6128 if (di == NULL)
6129 break;
6130 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006131 {
6132 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6133 copyID) == FAIL)
6134 {
6135 vim_free(di);
6136 break;
6137 }
6138 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006139 else
6140 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6141 if (dict_add(copy, di) == FAIL)
6142 {
6143 dictitem_free(di);
6144 break;
6145 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006146 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006147 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006148
Bram Moolenaare9a41262005-01-15 22:18:47 +00006149 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006150 if (todo > 0)
6151 {
6152 dict_unref(copy);
6153 copy = NULL;
6154 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006155 }
6156
6157 return copy;
6158}
6159
6160/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006161 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006162 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006163 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006164 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006165dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006166 dict_T *d;
6167 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006168{
Bram Moolenaar33570922005-01-25 22:26:29 +00006169 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006170}
6171
Bram Moolenaar8c711452005-01-14 21:53:12 +00006172/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006173 * Add a number or string entry to dictionary "d".
6174 * When "str" is NULL use number "nr", otherwise use "str".
6175 * Returns FAIL when out of memory and when key already exists.
6176 */
6177 int
6178dict_add_nr_str(d, key, nr, str)
6179 dict_T *d;
6180 char *key;
6181 long nr;
6182 char_u *str;
6183{
6184 dictitem_T *item;
6185
6186 item = dictitem_alloc((char_u *)key);
6187 if (item == NULL)
6188 return FAIL;
6189 item->di_tv.v_lock = 0;
6190 if (str == NULL)
6191 {
6192 item->di_tv.v_type = VAR_NUMBER;
6193 item->di_tv.vval.v_number = nr;
6194 }
6195 else
6196 {
6197 item->di_tv.v_type = VAR_STRING;
6198 item->di_tv.vval.v_string = vim_strsave(str);
6199 }
6200 if (dict_add(d, item) == FAIL)
6201 {
6202 dictitem_free(item);
6203 return FAIL;
6204 }
6205 return OK;
6206}
6207
6208/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006209 * Get the number of items in a Dictionary.
6210 */
6211 static long
6212dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006213 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006214{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006215 if (d == NULL)
6216 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006217 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006218}
6219
6220/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006221 * Find item "key[len]" in Dictionary "d".
6222 * If "len" is negative use strlen(key).
6223 * Returns NULL when not found.
6224 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006225 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006226dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006227 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006228 char_u *key;
6229 int len;
6230{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006231#define AKEYLEN 200
6232 char_u buf[AKEYLEN];
6233 char_u *akey;
6234 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006235 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006236
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006237 if (len < 0)
6238 akey = key;
6239 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006240 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006241 tofree = akey = vim_strnsave(key, len);
6242 if (akey == NULL)
6243 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006244 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006245 else
6246 {
6247 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006248 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006249 akey = buf;
6250 }
6251
Bram Moolenaar33570922005-01-25 22:26:29 +00006252 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006253 vim_free(tofree);
6254 if (HASHITEM_EMPTY(hi))
6255 return NULL;
6256 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006257}
6258
6259/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006260 * Get a string item from a dictionary in allocated memory.
6261 * Returns NULL if the entry doesn't exist or out of memory.
6262 */
6263 char_u *
6264get_dict_string(d, key)
6265 dict_T *d;
6266 char_u *key;
6267{
6268 dictitem_T *di;
6269
6270 di = dict_find(d, key, -1);
6271 if (di == NULL)
6272 return NULL;
6273 return vim_strsave(get_tv_string(&di->di_tv));
6274}
6275
6276/*
6277 * Get a number item from a dictionary.
6278 * Returns 0 if the entry doesn't exist or out of memory.
6279 */
6280 long
6281get_dict_number(d, key)
6282 dict_T *d;
6283 char_u *key;
6284{
6285 dictitem_T *di;
6286
6287 di = dict_find(d, key, -1);
6288 if (di == NULL)
6289 return 0;
6290 return get_tv_number(&di->di_tv);
6291}
6292
6293/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006294 * Return an allocated string with the string representation of a Dictionary.
6295 * May return NULL.
6296 */
6297 static char_u *
6298dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006299 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006300{
6301 garray_T ga;
6302 int first = TRUE;
6303 char_u *tofree;
6304 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006305 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006306 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006307 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006308 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006309
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006310 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006311 return NULL;
6312 ga_init2(&ga, (int)sizeof(char), 80);
6313 ga_append(&ga, '{');
6314
Bram Moolenaar33570922005-01-25 22:26:29 +00006315 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006316 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006317 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006318 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006319 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006320 --todo;
6321
6322 if (first)
6323 first = FALSE;
6324 else
6325 ga_concat(&ga, (char_u *)", ");
6326
6327 tofree = string_quote(hi->hi_key, FALSE);
6328 if (tofree != NULL)
6329 {
6330 ga_concat(&ga, tofree);
6331 vim_free(tofree);
6332 }
6333 ga_concat(&ga, (char_u *)": ");
6334 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6335 if (s != NULL)
6336 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006337 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006338 if (s == NULL)
6339 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006340 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006341 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006342 if (todo > 0)
6343 {
6344 vim_free(ga.ga_data);
6345 return NULL;
6346 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006347
6348 ga_append(&ga, '}');
6349 ga_append(&ga, NUL);
6350 return (char_u *)ga.ga_data;
6351}
6352
6353/*
6354 * Allocate a variable for a Dictionary and fill it from "*arg".
6355 * Return OK or FAIL. Returns NOTDONE for {expr}.
6356 */
6357 static int
6358get_dict_tv(arg, rettv, evaluate)
6359 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006360 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006361 int evaluate;
6362{
Bram Moolenaar33570922005-01-25 22:26:29 +00006363 dict_T *d = NULL;
6364 typval_T tvkey;
6365 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006366 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006368 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006369 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006370
6371 /*
6372 * First check if it's not a curly-braces thing: {expr}.
6373 * Must do this without evaluating, otherwise a function may be called
6374 * twice. Unfortunately this means we need to call eval1() twice for the
6375 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006376 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006377 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006378 if (*start != '}')
6379 {
6380 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6381 return FAIL;
6382 if (*start == '}')
6383 return NOTDONE;
6384 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006385
6386 if (evaluate)
6387 {
6388 d = dict_alloc();
6389 if (d == NULL)
6390 return FAIL;
6391 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006392 tvkey.v_type = VAR_UNKNOWN;
6393 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006394
6395 *arg = skipwhite(*arg + 1);
6396 while (**arg != '}' && **arg != NUL)
6397 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006398 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006399 goto failret;
6400 if (**arg != ':')
6401 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006402 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006403 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006404 goto failret;
6405 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006406 key = get_tv_string_buf_chk(&tvkey, buf);
6407 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006409 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6410 if (key != NULL)
6411 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006412 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006413 goto failret;
6414 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006415
6416 *arg = skipwhite(*arg + 1);
6417 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6418 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006419 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006420 goto failret;
6421 }
6422 if (evaluate)
6423 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006424 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006425 if (item != NULL)
6426 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006427 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006428 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006429 clear_tv(&tv);
6430 goto failret;
6431 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006432 item = dictitem_alloc(key);
6433 clear_tv(&tvkey);
6434 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006435 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006436 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006437 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006438 if (dict_add(d, item) == FAIL)
6439 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006440 }
6441 }
6442
6443 if (**arg == '}')
6444 break;
6445 if (**arg != ',')
6446 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006447 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006448 goto failret;
6449 }
6450 *arg = skipwhite(*arg + 1);
6451 }
6452
6453 if (**arg != '}')
6454 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006455 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006456failret:
6457 if (evaluate)
6458 dict_free(d);
6459 return FAIL;
6460 }
6461
6462 *arg = skipwhite(*arg + 1);
6463 if (evaluate)
6464 {
6465 rettv->v_type = VAR_DICT;
6466 rettv->vval.v_dict = d;
6467 ++d->dv_refcount;
6468 }
6469
6470 return OK;
6471}
6472
Bram Moolenaar8c711452005-01-14 21:53:12 +00006473/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006474 * Return a string with the string representation of a variable.
6475 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006476 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006477 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006478 * May return NULL;
6479 */
6480 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006481echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006482 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006483 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006485{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006486 static int recurse = 0;
6487 char_u *r = NULL;
6488
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006490 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006491 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006492 *tofree = NULL;
6493 return NULL;
6494 }
6495 ++recurse;
6496
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 switch (tv->v_type)
6498 {
6499 case VAR_FUNC:
6500 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006501 r = tv->vval.v_string;
6502 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503 case VAR_LIST:
6504 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006505 r = *tofree;
6506 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006507 case VAR_DICT:
6508 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006509 r = *tofree;
6510 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006511 case VAR_STRING:
6512 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006513 *tofree = NULL;
6514 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006515 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006516 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006517 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006518 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006519 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520
6521 --recurse;
6522 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006523}
6524
6525/*
6526 * Return a string with the string representation of a variable.
6527 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6528 * "numbuf" is used for a number.
6529 * Puts quotes around strings, so that they can be parsed back by eval().
6530 * May return NULL;
6531 */
6532 static char_u *
6533tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006534 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006535 char_u **tofree;
6536 char_u *numbuf;
6537{
6538 switch (tv->v_type)
6539 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006540 case VAR_FUNC:
6541 *tofree = string_quote(tv->vval.v_string, TRUE);
6542 return *tofree;
6543 case VAR_STRING:
6544 *tofree = string_quote(tv->vval.v_string, FALSE);
6545 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006546 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006547 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006548 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006549 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006550 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006551 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006552 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006553 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006554}
6555
6556/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006557 * Return string "str" in ' quotes, doubling ' characters.
6558 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006559 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006560 */
6561 static char_u *
6562string_quote(str, function)
6563 char_u *str;
6564 int function;
6565{
Bram Moolenaar33570922005-01-25 22:26:29 +00006566 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006567 char_u *p, *r, *s;
6568
Bram Moolenaar33570922005-01-25 22:26:29 +00006569 len = (function ? 13 : 3);
6570 if (str != NULL)
6571 {
6572 len += STRLEN(str);
6573 for (p = str; *p != NUL; mb_ptr_adv(p))
6574 if (*p == '\'')
6575 ++len;
6576 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006577 s = r = alloc(len);
6578 if (r != NULL)
6579 {
6580 if (function)
6581 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006582 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006583 r += 10;
6584 }
6585 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006586 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006587 if (str != NULL)
6588 for (p = str; *p != NUL; )
6589 {
6590 if (*p == '\'')
6591 *r++ = '\'';
6592 MB_COPY_CHAR(p, r);
6593 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006594 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006595 if (function)
6596 *r++ = ')';
6597 *r++ = NUL;
6598 }
6599 return s;
6600}
6601
6602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 * Get the value of an environment variable.
6604 * "arg" is pointing to the '$'. It is advanced to after the name.
6605 * If the environment variable was not set, silently assume it is empty.
6606 * Always return OK.
6607 */
6608 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006609get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 int evaluate;
6613{
6614 char_u *string = NULL;
6615 int len;
6616 int cc;
6617 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006618 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619
6620 ++*arg;
6621 name = *arg;
6622 len = get_env_len(arg);
6623 if (evaluate)
6624 {
6625 if (len != 0)
6626 {
6627 cc = name[len];
6628 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006629 /* first try vim_getenv(), fast for normal environment vars */
6630 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006632 {
6633 if (!mustfree)
6634 string = vim_strsave(string);
6635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 else
6637 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006638 if (mustfree)
6639 vim_free(string);
6640
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 /* next try expanding things like $VIM and ${HOME} */
6642 string = expand_env_save(name - 1);
6643 if (string != NULL && *string == '$')
6644 {
6645 vim_free(string);
6646 string = NULL;
6647 }
6648 }
6649 name[len] = cc;
6650 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006651 rettv->v_type = VAR_STRING;
6652 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 }
6654
6655 return OK;
6656}
6657
6658/*
6659 * Array with names and number of arguments of all internal functions
6660 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6661 */
6662static struct fst
6663{
6664 char *f_name; /* function name */
6665 char f_min_argc; /* minimal number of arguments */
6666 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006667 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006668 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669} functions[] =
6670{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006671 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 {"append", 2, 2, f_append},
6673 {"argc", 0, 0, f_argc},
6674 {"argidx", 0, 0, f_argidx},
6675 {"argv", 1, 1, f_argv},
6676 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006677 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 {"bufexists", 1, 1, f_bufexists},
6679 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6680 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6681 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6682 {"buflisted", 1, 1, f_buflisted},
6683 {"bufloaded", 1, 1, f_bufloaded},
6684 {"bufname", 1, 1, f_bufname},
6685 {"bufnr", 1, 1, f_bufnr},
6686 {"bufwinnr", 1, 1, f_bufwinnr},
6687 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006688 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006689 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 {"char2nr", 1, 1, f_char2nr},
6691 {"cindent", 1, 1, f_cindent},
6692 {"col", 1, 1, f_col},
6693 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006695 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 {"cscope_connection",0,3, f_cscope_connection},
6697 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006698 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 {"delete", 1, 1, f_delete},
6700 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006701 {"diff_filler", 1, 1, f_diff_filler},
6702 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006703 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006705 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 {"eventhandler", 0, 0, f_eventhandler},
6707 {"executable", 1, 1, f_executable},
6708 {"exists", 1, 1, f_exists},
6709 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006710 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6712 {"filereadable", 1, 1, f_filereadable},
6713 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006714 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006715 {"finddir", 1, 3, f_finddir},
6716 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 {"fnamemodify", 2, 2, f_fnamemodify},
6718 {"foldclosed", 1, 1, f_foldclosed},
6719 {"foldclosedend", 1, 1, f_foldclosedend},
6720 {"foldlevel", 1, 1, f_foldlevel},
6721 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006722 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006724 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006725 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006726 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006727 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 {"getbufvar", 2, 2, f_getbufvar},
6729 {"getchar", 0, 1, f_getchar},
6730 {"getcharmod", 0, 0, f_getcharmod},
6731 {"getcmdline", 0, 0, f_getcmdline},
6732 {"getcmdpos", 0, 0, f_getcmdpos},
6733 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006734 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006735 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 {"getfsize", 1, 1, f_getfsize},
6737 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006738 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006739 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006740 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006741 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 {"getregtype", 0, 1, f_getregtype},
6743 {"getwinposx", 0, 0, f_getwinposx},
6744 {"getwinposy", 0, 0, f_getwinposy},
6745 {"getwinvar", 2, 2, f_getwinvar},
6746 {"glob", 1, 1, f_glob},
6747 {"globpath", 2, 2, f_globpath},
6748 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006749 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {"hasmapto", 1, 2, f_hasmapto},
6751 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6752 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6753 {"histadd", 2, 2, f_histadd},
6754 {"histdel", 1, 2, f_histdel},
6755 {"histget", 1, 2, f_histget},
6756 {"histnr", 1, 1, f_histnr},
6757 {"hlID", 1, 1, f_hlID},
6758 {"hlexists", 1, 1, f_hlexists},
6759 {"hostname", 0, 0, f_hostname},
6760 {"iconv", 3, 3, f_iconv},
6761 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006762 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 {"input", 1, 2, f_input},
6764 {"inputdialog", 1, 3, f_inputdialog},
6765 {"inputrestore", 0, 0, f_inputrestore},
6766 {"inputsave", 0, 0, f_inputsave},
6767 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006768 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006770 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006771 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006772 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006773 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006775 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 {"libcall", 3, 3, f_libcall},
6777 {"libcallnr", 3, 3, f_libcallnr},
6778 {"line", 1, 1, f_line},
6779 {"line2byte", 1, 1, f_line2byte},
6780 {"lispindent", 1, 1, f_lispindent},
6781 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 {"maparg", 1, 2, f_maparg},
6784 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006785 {"match", 2, 4, f_match},
6786 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006787 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006788 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006789 {"max", 1, 1, f_max},
6790 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006791#ifdef vim_mkdir
6792 {"mkdir", 1, 3, f_mkdir},
6793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 {"mode", 0, 0, f_mode},
6795 {"nextnonblank", 1, 1, f_nextnonblank},
6796 {"nr2char", 1, 1, f_nr2char},
6797 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006798 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006799 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006800 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {"remote_expr", 2, 3, f_remote_expr},
6802 {"remote_foreground", 1, 1, f_remote_foreground},
6803 {"remote_peek", 1, 2, f_remote_peek},
6804 {"remote_read", 1, 1, f_remote_read},
6805 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006806 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006808 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006810 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {"search", 1, 2, f_search},
6812 {"searchpair", 3, 5, f_searchpair},
6813 {"server2client", 2, 2, f_server2client},
6814 {"serverlist", 0, 0, f_serverlist},
6815 {"setbufvar", 3, 3, f_setbufvar},
6816 {"setcmdpos", 1, 1, f_setcmdpos},
6817 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006818 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 {"setreg", 2, 3, f_setreg},
6820 {"setwinvar", 3, 3, f_setwinvar},
6821 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006822 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006823 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006824 {"spellbadword", 0, 0, f_spellbadword},
6825 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006826 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827#ifdef HAVE_STRFTIME
6828 {"strftime", 1, 2, f_strftime},
6829#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006830 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006831 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 {"strlen", 1, 1, f_strlen},
6833 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006834 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 {"strtrans", 1, 1, f_strtrans},
6836 {"submatch", 1, 1, f_submatch},
6837 {"substitute", 4, 4, f_substitute},
6838 {"synID", 3, 3, f_synID},
6839 {"synIDattr", 2, 3, f_synIDattr},
6840 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006841 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006842 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 {"tempname", 0, 0, f_tempname},
6844 {"tolower", 1, 1, f_tolower},
6845 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006846 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006848 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 {"virtcol", 1, 1, f_virtcol},
6850 {"visualmode", 0, 1, f_visualmode},
6851 {"winbufnr", 1, 1, f_winbufnr},
6852 {"wincol", 0, 0, f_wincol},
6853 {"winheight", 1, 1, f_winheight},
6854 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006855 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 {"winrestcmd", 0, 0, f_winrestcmd},
6857 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006858 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859};
6860
6861#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6862
6863/*
6864 * Function given to ExpandGeneric() to obtain the list of internal
6865 * or user defined function names.
6866 */
6867 char_u *
6868get_function_name(xp, idx)
6869 expand_T *xp;
6870 int idx;
6871{
6872 static int intidx = -1;
6873 char_u *name;
6874
6875 if (idx == 0)
6876 intidx = -1;
6877 if (intidx < 0)
6878 {
6879 name = get_user_func_name(xp, idx);
6880 if (name != NULL)
6881 return name;
6882 }
6883 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6884 {
6885 STRCPY(IObuff, functions[intidx].f_name);
6886 STRCAT(IObuff, "(");
6887 if (functions[intidx].f_max_argc == 0)
6888 STRCAT(IObuff, ")");
6889 return IObuff;
6890 }
6891
6892 return NULL;
6893}
6894
6895/*
6896 * Function given to ExpandGeneric() to obtain the list of internal or
6897 * user defined variable or function names.
6898 */
6899/*ARGSUSED*/
6900 char_u *
6901get_expr_name(xp, idx)
6902 expand_T *xp;
6903 int idx;
6904{
6905 static int intidx = -1;
6906 char_u *name;
6907
6908 if (idx == 0)
6909 intidx = -1;
6910 if (intidx < 0)
6911 {
6912 name = get_function_name(xp, idx);
6913 if (name != NULL)
6914 return name;
6915 }
6916 return get_user_var_name(xp, ++intidx);
6917}
6918
6919#endif /* FEAT_CMDL_COMPL */
6920
6921/*
6922 * Find internal function in table above.
6923 * Return index, or -1 if not found
6924 */
6925 static int
6926find_internal_func(name)
6927 char_u *name; /* name of the function */
6928{
6929 int first = 0;
6930 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6931 int cmp;
6932 int x;
6933
6934 /*
6935 * Find the function name in the table. Binary search.
6936 */
6937 while (first <= last)
6938 {
6939 x = first + ((unsigned)(last - first) >> 1);
6940 cmp = STRCMP(name, functions[x].f_name);
6941 if (cmp < 0)
6942 last = x - 1;
6943 else if (cmp > 0)
6944 first = x + 1;
6945 else
6946 return x;
6947 }
6948 return -1;
6949}
6950
6951/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006952 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6953 * name it contains, otherwise return "name".
6954 */
6955 static char_u *
6956deref_func_name(name, lenp)
6957 char_u *name;
6958 int *lenp;
6959{
Bram Moolenaar33570922005-01-25 22:26:29 +00006960 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006961 int cc;
6962
6963 cc = name[*lenp];
6964 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006965 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006966 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006967 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006968 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006969 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006970 {
6971 *lenp = 0;
6972 return (char_u *)""; /* just in case */
6973 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006974 *lenp = STRLEN(v->di_tv.vval.v_string);
6975 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006976 }
6977
6978 return name;
6979}
6980
6981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 * Allocate a variable for the result of a function.
6983 * Return OK or FAIL.
6984 */
6985 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006986get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6987 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 char_u *name; /* name of the function */
6989 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 char_u **arg; /* argument, pointing to the '(' */
6992 linenr_T firstline; /* first line of range */
6993 linenr_T lastline; /* last line of range */
6994 int *doesrange; /* return: function handled range */
6995 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006996 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997{
6998 char_u *argp;
6999 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007000 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 int argcount = 0; /* number of arguments found */
7002
7003 /*
7004 * Get the arguments.
7005 */
7006 argp = *arg;
7007 while (argcount < MAX_FUNC_ARGS)
7008 {
7009 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7010 if (*argp == ')' || *argp == ',' || *argp == NUL)
7011 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7013 {
7014 ret = FAIL;
7015 break;
7016 }
7017 ++argcount;
7018 if (*argp != ',')
7019 break;
7020 }
7021 if (*argp == ')')
7022 ++argp;
7023 else
7024 ret = FAIL;
7025
7026 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007027 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007028 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007030 {
7031 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007032 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007033 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007034 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036
7037 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007038 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039
7040 *arg = skipwhite(argp);
7041 return ret;
7042}
7043
7044
7045/*
7046 * Call a function with its resolved parameters
7047 * Return OK or FAIL.
7048 */
7049 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007050call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007051 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 char_u *name; /* name of the function */
7053 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007054 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007056 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 linenr_T firstline; /* first line of range */
7058 linenr_T lastline; /* last line of range */
7059 int *doesrange; /* return: function handled range */
7060 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007061 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062{
7063 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064#define ERROR_UNKNOWN 0
7065#define ERROR_TOOMANY 1
7066#define ERROR_TOOFEW 2
7067#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007068#define ERROR_DICT 4
7069#define ERROR_NONE 5
7070#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 int error = ERROR_NONE;
7072 int i;
7073 int llen;
7074 ufunc_T *fp;
7075 int cc;
7076#define FLEN_FIXED 40
7077 char_u fname_buf[FLEN_FIXED + 1];
7078 char_u *fname;
7079
7080 /*
7081 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7082 * Change <SNR>123_name() to K_SNR 123_name().
7083 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7084 */
7085 cc = name[len];
7086 name[len] = NUL;
7087 llen = eval_fname_script(name);
7088 if (llen > 0)
7089 {
7090 fname_buf[0] = K_SPECIAL;
7091 fname_buf[1] = KS_EXTRA;
7092 fname_buf[2] = (int)KE_SNR;
7093 i = 3;
7094 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7095 {
7096 if (current_SID <= 0)
7097 error = ERROR_SCRIPT;
7098 else
7099 {
7100 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7101 i = (int)STRLEN(fname_buf);
7102 }
7103 }
7104 if (i + STRLEN(name + llen) < FLEN_FIXED)
7105 {
7106 STRCPY(fname_buf + i, name + llen);
7107 fname = fname_buf;
7108 }
7109 else
7110 {
7111 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7112 if (fname == NULL)
7113 error = ERROR_OTHER;
7114 else
7115 {
7116 mch_memmove(fname, fname_buf, (size_t)i);
7117 STRCPY(fname + i, name + llen);
7118 }
7119 }
7120 }
7121 else
7122 fname = name;
7123
7124 *doesrange = FALSE;
7125
7126
7127 /* execute the function if no errors detected and executing */
7128 if (evaluate && error == ERROR_NONE)
7129 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007130 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 error = ERROR_UNKNOWN;
7132
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007133 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {
7135 /*
7136 * User defined function.
7137 */
7138 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007139
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007141 /* Trigger FuncUndefined event, may load the function. */
7142 if (fp == NULL
7143 && apply_autocmds(EVENT_FUNCUNDEFINED,
7144 fname, fname, TRUE, NULL)
7145 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007147 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 fp = find_func(fname);
7149 }
7150#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007151 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007152 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007153 {
7154 /* loaded a package, search for the function again */
7155 fp = find_func(fname);
7156 }
7157
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 if (fp != NULL)
7159 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007160 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007162 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007164 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007166 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007167 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 else
7169 {
7170 /*
7171 * Call the user function.
7172 * Save and restore search patterns, script variables and
7173 * redo buffer.
7174 */
7175 save_search_patterns();
7176 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007177 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007178 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007179 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007180 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7181 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7182 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007183 /* Function was unreferenced while being used, free it
7184 * now. */
7185 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 restoreRedobuff();
7187 restore_search_patterns();
7188 error = ERROR_NONE;
7189 }
7190 }
7191 }
7192 else
7193 {
7194 /*
7195 * Find the function name in the table, call its implementation.
7196 */
7197 i = find_internal_func(fname);
7198 if (i >= 0)
7199 {
7200 if (argcount < functions[i].f_min_argc)
7201 error = ERROR_TOOFEW;
7202 else if (argcount > functions[i].f_max_argc)
7203 error = ERROR_TOOMANY;
7204 else
7205 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007206 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007207 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 error = ERROR_NONE;
7209 }
7210 }
7211 }
7212 /*
7213 * The function call (or "FuncUndefined" autocommand sequence) might
7214 * have been aborted by an error, an interrupt, or an explicitly thrown
7215 * exception that has not been caught so far. This situation can be
7216 * tested for by calling aborting(). For an error in an internal
7217 * function or for the "E132" error in call_user_func(), however, the
7218 * throw point at which the "force_abort" flag (temporarily reset by
7219 * emsg()) is normally updated has not been reached yet. We need to
7220 * update that flag first to make aborting() reliable.
7221 */
7222 update_force_abort();
7223 }
7224 if (error == ERROR_NONE)
7225 ret = OK;
7226
7227 /*
7228 * Report an error unless the argument evaluation or function call has been
7229 * cancelled due to an aborting error, an interrupt, or an exception.
7230 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231 if (!aborting())
7232 {
7233 switch (error)
7234 {
7235 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007236 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007237 break;
7238 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007239 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240 break;
7241 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007242 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243 name);
7244 break;
7245 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007246 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247 name);
7248 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007249 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007250 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007251 name);
7252 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253 }
7254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255
7256 name[len] = cc;
7257 if (fname != name && fname != fname_buf)
7258 vim_free(fname);
7259
7260 return ret;
7261}
7262
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007263/*
7264 * Give an error message with a function name. Handle <SNR> things.
7265 */
7266 static void
7267emsg_funcname(msg, name)
7268 char *msg;
7269 char_u *name;
7270{
7271 char_u *p;
7272
7273 if (*name == K_SPECIAL)
7274 p = concat_str((char_u *)"<SNR>", name + 3);
7275 else
7276 p = name;
7277 EMSG2(_(msg), p);
7278 if (p != name)
7279 vim_free(p);
7280}
7281
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282/*********************************************
7283 * Implementation of the built-in functions
7284 */
7285
7286/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007287 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 */
7289 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007290f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007291 typval_T *argvars;
7292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293{
Bram Moolenaar33570922005-01-25 22:26:29 +00007294 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007296 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007297 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007299 if ((l = argvars[0].vval.v_list) != NULL
7300 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7301 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007302 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007303 }
7304 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007305 EMSG(_(e_listreq));
7306}
7307
7308/*
7309 * "append(lnum, string/list)" function
7310 */
7311 static void
7312f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 typval_T *argvars;
7314 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007315{
7316 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007317 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007318 list_T *l = NULL;
7319 listitem_T *li = NULL;
7320 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007321 long added = 0;
7322
Bram Moolenaar0d660222005-01-07 21:51:51 +00007323 lnum = get_tv_lnum(argvars);
7324 if (lnum >= 0
7325 && lnum <= curbuf->b_ml.ml_line_count
7326 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007327 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007328 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007329 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007330 l = argvars[1].vval.v_list;
7331 if (l == NULL)
7332 return;
7333 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007334 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007335 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007336 for (;;)
7337 {
7338 if (l == NULL)
7339 tv = &argvars[1]; /* append a string */
7340 else if (li == NULL)
7341 break; /* end of list */
7342 else
7343 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007344 line = get_tv_string_chk(tv);
7345 if (line == NULL) /* type error */
7346 {
7347 rettv->vval.v_number = 1; /* Failed */
7348 break;
7349 }
7350 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007351 ++added;
7352 if (l == NULL)
7353 break;
7354 li = li->li_next;
7355 }
7356
7357 appended_lines_mark(lnum, added);
7358 if (curwin->w_cursor.lnum > lnum)
7359 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007361 else
7362 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363}
7364
7365/*
7366 * "argc()" function
7367 */
7368/* ARGSUSED */
7369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007370f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007371 typval_T *argvars;
7372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007374 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375}
7376
7377/*
7378 * "argidx()" function
7379 */
7380/* ARGSUSED */
7381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007382f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 typval_T *argvars;
7384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007386 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387}
7388
7389/*
7390 * "argv(nr)" function
7391 */
7392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007393f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007394 typval_T *argvars;
7395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396{
7397 int idx;
7398
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007399 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007401 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007403 rettv->vval.v_string = NULL;
7404 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405}
7406
7407/*
7408 * "browse(save, title, initdir, default)" function
7409 */
7410/* ARGSUSED */
7411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007412f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007413 typval_T *argvars;
7414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
7416#ifdef FEAT_BROWSE
7417 int save;
7418 char_u *title;
7419 char_u *initdir;
7420 char_u *defname;
7421 char_u buf[NUMBUFLEN];
7422 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007423 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007425 save = get_tv_number_chk(&argvars[0], &error);
7426 title = get_tv_string_chk(&argvars[1]);
7427 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7428 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007430 if (error || title == NULL || initdir == NULL || defname == NULL)
7431 rettv->vval.v_string = NULL;
7432 else
7433 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007434 do_browse(save ? BROWSE_SAVE : 0,
7435 title, defname, NULL, initdir, NULL, curbuf);
7436#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007437 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007438#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007439 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007440}
7441
7442/*
7443 * "browsedir(title, initdir)" function
7444 */
7445/* ARGSUSED */
7446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007447f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 typval_T *argvars;
7449 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007450{
7451#ifdef FEAT_BROWSE
7452 char_u *title;
7453 char_u *initdir;
7454 char_u buf[NUMBUFLEN];
7455
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007456 title = get_tv_string_chk(&argvars[0]);
7457 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007459 if (title == NULL || initdir == NULL)
7460 rettv->vval.v_string = NULL;
7461 else
7462 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007463 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007465 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007467 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468}
7469
Bram Moolenaar33570922005-01-25 22:26:29 +00007470static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007471
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472/*
7473 * Find a buffer by number or exact name.
7474 */
7475 static buf_T *
7476find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007477 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478{
7479 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007481 if (avar->v_type == VAR_NUMBER)
7482 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007483 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007485 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007486 if (buf == NULL)
7487 {
7488 /* No full path name match, try a match with a URL or a "nofile"
7489 * buffer, these don't use the full path. */
7490 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7491 if (buf->b_fname != NULL
7492 && (path_with_url(buf->b_fname)
7493#ifdef FEAT_QUICKFIX
7494 || bt_nofile(buf)
7495#endif
7496 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007497 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007498 break;
7499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 }
7501 return buf;
7502}
7503
7504/*
7505 * "bufexists(expr)" function
7506 */
7507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007508f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007509 typval_T *argvars;
7510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007512 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513}
7514
7515/*
7516 * "buflisted(expr)" function
7517 */
7518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007519f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007520 typval_T *argvars;
7521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522{
7523 buf_T *buf;
7524
7525 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007526 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527}
7528
7529/*
7530 * "bufloaded(expr)" function
7531 */
7532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007533f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007534 typval_T *argvars;
7535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536{
7537 buf_T *buf;
7538
7539 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007540 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541}
7542
Bram Moolenaar33570922005-01-25 22:26:29 +00007543static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007544
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545/*
7546 * Get buffer by number or pattern.
7547 */
7548 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007549get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007550 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007552 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 int save_magic;
7554 char_u *save_cpo;
7555 buf_T *buf;
7556
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007557 if (tv->v_type == VAR_NUMBER)
7558 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007559 if (tv->v_type != VAR_STRING)
7560 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 if (name == NULL || *name == NUL)
7562 return curbuf;
7563 if (name[0] == '$' && name[1] == NUL)
7564 return lastbuf;
7565
7566 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7567 save_magic = p_magic;
7568 p_magic = TRUE;
7569 save_cpo = p_cpo;
7570 p_cpo = (char_u *)"";
7571
7572 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7573 TRUE, FALSE));
7574
7575 p_magic = save_magic;
7576 p_cpo = save_cpo;
7577
7578 /* If not found, try expanding the name, like done for bufexists(). */
7579 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007580 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581
7582 return buf;
7583}
7584
7585/*
7586 * "bufname(expr)" function
7587 */
7588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007589f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007590 typval_T *argvars;
7591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592{
7593 buf_T *buf;
7594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007595 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007597 buf = get_buf_tv(&argvars[0]);
7598 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007600 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007602 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 --emsg_off;
7604}
7605
7606/*
7607 * "bufnr(expr)" function
7608 */
7609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007610f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007611 typval_T *argvars;
7612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613{
7614 buf_T *buf;
7615
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007616 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007618 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007620 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007622 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 --emsg_off;
7624}
7625
7626/*
7627 * "bufwinnr(nr)" function
7628 */
7629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007630f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007631 typval_T *argvars;
7632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
7634#ifdef FEAT_WINDOWS
7635 win_T *wp;
7636 int winnr = 0;
7637#endif
7638 buf_T *buf;
7639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007640 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007642 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643#ifdef FEAT_WINDOWS
7644 for (wp = firstwin; wp; wp = wp->w_next)
7645 {
7646 ++winnr;
7647 if (wp->w_buffer == buf)
7648 break;
7649 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007650 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007652 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653#endif
7654 --emsg_off;
7655}
7656
7657/*
7658 * "byte2line(byte)" function
7659 */
7660/*ARGSUSED*/
7661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007662f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007663 typval_T *argvars;
7664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665{
7666#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007667 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668#else
7669 long boff = 0;
7670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007671 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007675 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 (linenr_T)0, &boff);
7677#endif
7678}
7679
7680/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007681 * "byteidx()" function
7682 */
7683/*ARGSUSED*/
7684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007686 typval_T *argvars;
7687 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007688{
7689#ifdef FEAT_MBYTE
7690 char_u *t;
7691#endif
7692 char_u *str;
7693 long idx;
7694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007695 str = get_tv_string_chk(&argvars[0]);
7696 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007697 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007698 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007699 return;
7700
7701#ifdef FEAT_MBYTE
7702 t = str;
7703 for ( ; idx > 0; idx--)
7704 {
7705 if (*t == NUL) /* EOL reached */
7706 return;
7707 t += mb_ptr2len_check(t);
7708 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007709 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007710#else
7711 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007712 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007713#endif
7714}
7715
7716/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007717 * "call(func, arglist)" function
7718 */
7719 static void
7720f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 typval_T *argvars;
7722 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007723{
7724 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007725 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007726 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007727 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007728 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007729 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007730
7731 rettv->vval.v_number = 0;
7732 if (argvars[1].v_type != VAR_LIST)
7733 {
7734 EMSG(_(e_listreq));
7735 return;
7736 }
7737 if (argvars[1].vval.v_list == NULL)
7738 return;
7739
7740 if (argvars[0].v_type == VAR_FUNC)
7741 func = argvars[0].vval.v_string;
7742 else
7743 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007744 if (*func == NUL)
7745 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007746
Bram Moolenaare9a41262005-01-15 22:18:47 +00007747 if (argvars[2].v_type != VAR_UNKNOWN)
7748 {
7749 if (argvars[2].v_type != VAR_DICT)
7750 {
7751 EMSG(_(e_dictreq));
7752 return;
7753 }
7754 selfdict = argvars[2].vval.v_dict;
7755 }
7756
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007757 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7758 item = item->li_next)
7759 {
7760 if (argc == MAX_FUNC_ARGS)
7761 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007762 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007763 break;
7764 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007765 /* Make a copy of each argument. This is needed to be able to set
7766 * v_lock to VAR_FIXED in the copy without changing the original list.
7767 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007768 copy_tv(&item->li_tv, &argv[argc++]);
7769 }
7770
7771 if (item == NULL)
7772 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007773 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7774 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007775
7776 /* Free the arguments. */
7777 while (argc > 0)
7778 clear_tv(&argv[--argc]);
7779}
7780
7781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 * "char2nr(string)" function
7783 */
7784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007785f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007786 typval_T *argvars;
7787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788{
7789#ifdef FEAT_MBYTE
7790 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007791 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 else
7793#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795}
7796
7797/*
7798 * "cindent(lnum)" function
7799 */
7800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007801f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007802 typval_T *argvars;
7803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804{
7805#ifdef FEAT_CINDENT
7806 pos_T pos;
7807 linenr_T lnum;
7808
7809 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007810 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7812 {
7813 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007814 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 curwin->w_cursor = pos;
7816 }
7817 else
7818#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007819 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820}
7821
7822/*
7823 * "col(string)" function
7824 */
7825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007826f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007827 typval_T *argvars;
7828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829{
7830 colnr_T col = 0;
7831 pos_T *fp;
7832
7833 fp = var2fpos(&argvars[0], FALSE);
7834 if (fp != NULL)
7835 {
7836 if (fp->col == MAXCOL)
7837 {
7838 /* '> can be MAXCOL, get the length of the line then */
7839 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7840 col = STRLEN(ml_get(fp->lnum)) + 1;
7841 else
7842 col = MAXCOL;
7843 }
7844 else
7845 {
7846 col = fp->col + 1;
7847#ifdef FEAT_VIRTUALEDIT
7848 /* col(".") when the cursor is on the NUL at the end of the line
7849 * because of "coladd" can be seen as an extra column. */
7850 if (virtual_active() && fp == &curwin->w_cursor)
7851 {
7852 char_u *p = ml_get_cursor();
7853
7854 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7855 curwin->w_virtcol - curwin->w_cursor.coladd))
7856 {
7857# ifdef FEAT_MBYTE
7858 int l;
7859
7860 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7861 col += l;
7862# else
7863 if (*p != NUL && p[1] == NUL)
7864 ++col;
7865# endif
7866 }
7867 }
7868#endif
7869 }
7870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872}
7873
7874/*
7875 * "confirm(message, buttons[, default [, type]])" function
7876 */
7877/*ARGSUSED*/
7878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007879f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007880 typval_T *argvars;
7881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882{
7883#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7884 char_u *message;
7885 char_u *buttons = NULL;
7886 char_u buf[NUMBUFLEN];
7887 char_u buf2[NUMBUFLEN];
7888 int def = 1;
7889 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007890 char_u *typestr;
7891 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007893 message = get_tv_string_chk(&argvars[0]);
7894 if (message == NULL)
7895 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007896 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007898 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7899 if (buttons == NULL)
7900 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007901 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007903 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007904 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007906 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7907 if (typestr == NULL)
7908 error = TRUE;
7909 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007911 switch (TOUPPER_ASC(*typestr))
7912 {
7913 case 'E': type = VIM_ERROR; break;
7914 case 'Q': type = VIM_QUESTION; break;
7915 case 'I': type = VIM_INFO; break;
7916 case 'W': type = VIM_WARNING; break;
7917 case 'G': type = VIM_GENERIC; break;
7918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 }
7920 }
7921 }
7922 }
7923
7924 if (buttons == NULL || *buttons == NUL)
7925 buttons = (char_u *)_("&Ok");
7926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007927 if (error)
7928 rettv->vval.v_number = 0;
7929 else
7930 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 def, NULL);
7932#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007933 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934#endif
7935}
7936
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007937/*
7938 * "copy()" function
7939 */
7940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007941f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007942 typval_T *argvars;
7943 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007944{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007945 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007946}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947
7948/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007949 * "count()" function
7950 */
7951 static void
7952f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007953 typval_T *argvars;
7954 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007955{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007956 long n = 0;
7957 int ic = FALSE;
7958
Bram Moolenaare9a41262005-01-15 22:18:47 +00007959 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007960 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007961 listitem_T *li;
7962 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007963 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007964
Bram Moolenaare9a41262005-01-15 22:18:47 +00007965 if ((l = argvars[0].vval.v_list) != NULL)
7966 {
7967 li = l->lv_first;
7968 if (argvars[2].v_type != VAR_UNKNOWN)
7969 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007970 int error = FALSE;
7971
7972 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007973 if (argvars[3].v_type != VAR_UNKNOWN)
7974 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007975 idx = get_tv_number_chk(&argvars[3], &error);
7976 if (!error)
7977 {
7978 li = list_find(l, idx);
7979 if (li == NULL)
7980 EMSGN(_(e_listidx), idx);
7981 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007982 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007983 if (error)
7984 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007985 }
7986
7987 for ( ; li != NULL; li = li->li_next)
7988 if (tv_equal(&li->li_tv, &argvars[1], ic))
7989 ++n;
7990 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007991 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007992 else if (argvars[0].v_type == VAR_DICT)
7993 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007994 int todo;
7995 dict_T *d;
7996 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007997
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007998 if ((d = argvars[0].vval.v_dict) != NULL)
7999 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008000 int error = FALSE;
8001
Bram Moolenaare9a41262005-01-15 22:18:47 +00008002 if (argvars[2].v_type != VAR_UNKNOWN)
8003 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008004 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008005 if (argvars[3].v_type != VAR_UNKNOWN)
8006 EMSG(_(e_invarg));
8007 }
8008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008009 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008010 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008011 {
8012 if (!HASHITEM_EMPTY(hi))
8013 {
8014 --todo;
8015 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8016 ++n;
8017 }
8018 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008019 }
8020 }
8021 else
8022 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008023 rettv->vval.v_number = n;
8024}
8025
8026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8028 *
8029 * Checks the existence of a cscope connection.
8030 */
8031/*ARGSUSED*/
8032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008033f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008034 typval_T *argvars;
8035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036{
8037#ifdef FEAT_CSCOPE
8038 int num = 0;
8039 char_u *dbpath = NULL;
8040 char_u *prepend = NULL;
8041 char_u buf[NUMBUFLEN];
8042
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008043 if (argvars[0].v_type != VAR_UNKNOWN
8044 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008046 num = (int)get_tv_number(&argvars[0]);
8047 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008048 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008049 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 }
8051
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008052 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008054 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055#endif
8056}
8057
8058/*
8059 * "cursor(lnum, col)" function
8060 *
8061 * Moves the cursor to the specified line and column
8062 */
8063/*ARGSUSED*/
8064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008065f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008066 typval_T *argvars;
8067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068{
8069 long line, col;
8070
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008071 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008072 col = get_tv_number_chk(&argvars[1], NULL);
8073 if (line < 0 || col < 0)
8074 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 if (line > 0)
8076 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 if (col > 0)
8078 curwin->w_cursor.col = col - 1;
8079#ifdef FEAT_VIRTUALEDIT
8080 curwin->w_cursor.coladd = 0;
8081#endif
8082
8083 /* Make sure the cursor is in a valid position. */
8084 check_cursor();
8085#ifdef FEAT_MBYTE
8086 /* Correct cursor for multi-byte character. */
8087 if (has_mbyte)
8088 mb_adjust_cursor();
8089#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008090
8091 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092}
8093
8094/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008095 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 */
8097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008098f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008099 typval_T *argvars;
8100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008102 int noref = 0;
8103
8104 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008105 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008106 if (noref < 0 || noref > 1)
8107 EMSG(_(e_invarg));
8108 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008109 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110}
8111
8112/*
8113 * "delete()" function
8114 */
8115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008116f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008117 typval_T *argvars;
8118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119{
8120 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008121 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008123 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124}
8125
8126/*
8127 * "did_filetype()" function
8128 */
8129/*ARGSUSED*/
8130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008131f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008132 typval_T *argvars;
8133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134{
8135#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008136 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008138 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139#endif
8140}
8141
8142/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008143 * "diff_filler()" function
8144 */
8145/*ARGSUSED*/
8146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008147f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008148 typval_T *argvars;
8149 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008150{
8151#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008152 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008153#endif
8154}
8155
8156/*
8157 * "diff_hlID()" function
8158 */
8159/*ARGSUSED*/
8160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008161f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008162 typval_T *argvars;
8163 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008164{
8165#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008166 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008167 static linenr_T prev_lnum = 0;
8168 static int changedtick = 0;
8169 static int fnum = 0;
8170 static int change_start = 0;
8171 static int change_end = 0;
8172 static enum hlf_value hlID = 0;
8173 int filler_lines;
8174 int col;
8175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008176 if (lnum < 0) /* ignore type error in {lnum} arg */
8177 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008178 if (lnum != prev_lnum
8179 || changedtick != curbuf->b_changedtick
8180 || fnum != curbuf->b_fnum)
8181 {
8182 /* New line, buffer, change: need to get the values. */
8183 filler_lines = diff_check(curwin, lnum);
8184 if (filler_lines < 0)
8185 {
8186 if (filler_lines == -1)
8187 {
8188 change_start = MAXCOL;
8189 change_end = -1;
8190 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8191 hlID = HLF_ADD; /* added line */
8192 else
8193 hlID = HLF_CHD; /* changed line */
8194 }
8195 else
8196 hlID = HLF_ADD; /* added line */
8197 }
8198 else
8199 hlID = (enum hlf_value)0;
8200 prev_lnum = lnum;
8201 changedtick = curbuf->b_changedtick;
8202 fnum = curbuf->b_fnum;
8203 }
8204
8205 if (hlID == HLF_CHD || hlID == HLF_TXD)
8206 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008207 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008208 if (col >= change_start && col <= change_end)
8209 hlID = HLF_TXD; /* changed text */
8210 else
8211 hlID = HLF_CHD; /* changed line */
8212 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008213 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008214#endif
8215}
8216
8217/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008218 * "empty({expr})" function
8219 */
8220 static void
8221f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008222 typval_T *argvars;
8223 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008224{
8225 int n;
8226
8227 switch (argvars[0].v_type)
8228 {
8229 case VAR_STRING:
8230 case VAR_FUNC:
8231 n = argvars[0].vval.v_string == NULL
8232 || *argvars[0].vval.v_string == NUL;
8233 break;
8234 case VAR_NUMBER:
8235 n = argvars[0].vval.v_number == 0;
8236 break;
8237 case VAR_LIST:
8238 n = argvars[0].vval.v_list == NULL
8239 || argvars[0].vval.v_list->lv_first == NULL;
8240 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008241 case VAR_DICT:
8242 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008243 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008244 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008245 default:
8246 EMSG2(_(e_intern2), "f_empty()");
8247 n = 0;
8248 }
8249
8250 rettv->vval.v_number = n;
8251}
8252
8253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 * "escape({string}, {chars})" function
8255 */
8256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008257f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008258 typval_T *argvars;
8259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260{
8261 char_u buf[NUMBUFLEN];
8262
Bram Moolenaar758711c2005-02-02 23:11:38 +00008263 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8264 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008265 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266}
8267
8268/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008269 * "eval()" function
8270 */
8271/*ARGSUSED*/
8272 static void
8273f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008274 typval_T *argvars;
8275 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008276{
8277 char_u *s;
8278
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008279 s = get_tv_string_chk(&argvars[0]);
8280 if (s != NULL)
8281 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008283 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8284 {
8285 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008286 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008287 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008288 else if (*s != NUL)
8289 EMSG(_(e_trailing));
8290}
8291
8292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 * "eventhandler()" function
8294 */
8295/*ARGSUSED*/
8296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008297f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008298 typval_T *argvars;
8299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008301 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302}
8303
8304/*
8305 * "executable()" function
8306 */
8307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008308f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008309 typval_T *argvars;
8310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008312 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313}
8314
8315/*
8316 * "exists()" function
8317 */
8318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008319f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008320 typval_T *argvars;
8321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322{
8323 char_u *p;
8324 char_u *name;
8325 int n = FALSE;
8326 int len = 0;
8327
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008328 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 if (*p == '$') /* environment variable */
8330 {
8331 /* first try "normal" environment variables (fast) */
8332 if (mch_getenv(p + 1) != NULL)
8333 n = TRUE;
8334 else
8335 {
8336 /* try expanding things like $VIM and ${HOME} */
8337 p = expand_env_save(p);
8338 if (p != NULL && *p != '$')
8339 n = TRUE;
8340 vim_free(p);
8341 }
8342 }
8343 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008344 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 else if (*p == '*') /* internal or user defined function */
8346 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008347 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 }
8349 else if (*p == ':')
8350 {
8351 n = cmd_exists(p + 1);
8352 }
8353 else if (*p == '#')
8354 {
8355#ifdef FEAT_AUTOCMD
8356 name = p + 1;
8357 p = vim_strchr(name, '#');
8358 if (p != NULL)
8359 n = au_exists(name, p, p + 1);
8360 else
8361 n = au_exists(name, name + STRLEN(name), NULL);
8362#endif
8363 }
8364 else /* internal variable */
8365 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008366 char_u *tofree;
8367 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008369 /* get_name_len() takes care of expanding curly braces */
8370 name = p;
8371 len = get_name_len(&p, &tofree, TRUE, FALSE);
8372 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008374 if (tofree != NULL)
8375 name = tofree;
8376 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8377 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008379 /* handle d.key, l[idx], f(expr) */
8380 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8381 if (n)
8382 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 }
8384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008386 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 }
8388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008389 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390}
8391
8392/*
8393 * "expand()" function
8394 */
8395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008396f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008397 typval_T *argvars;
8398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399{
8400 char_u *s;
8401 int len;
8402 char_u *errormsg;
8403 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8404 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008405 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008407 rettv->v_type = VAR_STRING;
8408 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 if (*s == '%' || *s == '#' || *s == '<')
8410 {
8411 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008412 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 --emsg_off;
8414 }
8415 else
8416 {
8417 /* When the optional second argument is non-zero, don't remove matches
8418 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008419 if (argvars[1].v_type != VAR_UNKNOWN
8420 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008422 if (!error)
8423 {
8424 ExpandInit(&xpc);
8425 xpc.xp_context = EXPAND_FILES;
8426 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8427 ExpandCleanup(&xpc);
8428 }
8429 else
8430 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 }
8432}
8433
8434/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008435 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008436 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008437 */
8438 static void
8439f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008440 typval_T *argvars;
8441 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008442{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008443 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008444 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008445 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008446 list_T *l1, *l2;
8447 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008448 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008449 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008450
Bram Moolenaare9a41262005-01-15 22:18:47 +00008451 l1 = argvars[0].vval.v_list;
8452 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008453 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8454 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008455 {
8456 if (argvars[2].v_type != VAR_UNKNOWN)
8457 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008458 before = get_tv_number_chk(&argvars[2], &error);
8459 if (error)
8460 return; /* type error; errmsg already given */
8461
Bram Moolenaar758711c2005-02-02 23:11:38 +00008462 if (before == l1->lv_len)
8463 item = NULL;
8464 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008465 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008466 item = list_find(l1, before);
8467 if (item == NULL)
8468 {
8469 EMSGN(_(e_listidx), before);
8470 return;
8471 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008472 }
8473 }
8474 else
8475 item = NULL;
8476 list_extend(l1, l2, item);
8477
Bram Moolenaare9a41262005-01-15 22:18:47 +00008478 copy_tv(&argvars[0], rettv);
8479 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008480 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008481 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8482 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008483 dict_T *d1, *d2;
8484 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008485 char_u *action;
8486 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008487 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008488 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008489
8490 d1 = argvars[0].vval.v_dict;
8491 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008492 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8493 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008494 {
8495 /* Check the third argument. */
8496 if (argvars[2].v_type != VAR_UNKNOWN)
8497 {
8498 static char *(av[]) = {"keep", "force", "error"};
8499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008500 action = get_tv_string_chk(&argvars[2]);
8501 if (action == NULL)
8502 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008503 for (i = 0; i < 3; ++i)
8504 if (STRCMP(action, av[i]) == 0)
8505 break;
8506 if (i == 3)
8507 {
8508 EMSGN(_(e_invarg2), action);
8509 return;
8510 }
8511 }
8512 else
8513 action = (char_u *)"force";
8514
8515 /* Go over all entries in the second dict and add them to the
8516 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008517 todo = d2->dv_hashtab.ht_used;
8518 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008519 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008520 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008521 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008522 --todo;
8523 di1 = dict_find(d1, hi2->hi_key, -1);
8524 if (di1 == NULL)
8525 {
8526 di1 = dictitem_copy(HI2DI(hi2));
8527 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8528 dictitem_free(di1);
8529 }
8530 else if (*action == 'e')
8531 {
8532 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8533 break;
8534 }
8535 else if (*action == 'f')
8536 {
8537 clear_tv(&di1->di_tv);
8538 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8539 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008540 }
8541 }
8542
Bram Moolenaare9a41262005-01-15 22:18:47 +00008543 copy_tv(&argvars[0], rettv);
8544 }
8545 }
8546 else
8547 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008548}
8549
8550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 * "filereadable()" function
8552 */
8553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008554f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008555 typval_T *argvars;
8556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557{
8558 FILE *fd;
8559 char_u *p;
8560 int n;
8561
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008562 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8564 {
8565 n = TRUE;
8566 fclose(fd);
8567 }
8568 else
8569 n = FALSE;
8570
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008571 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572}
8573
8574/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008575 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 * rights to write into.
8577 */
8578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008580 typval_T *argvars;
8581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008583 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584}
8585
Bram Moolenaar33570922005-01-25 22:26:29 +00008586static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008587
8588 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008589findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008590 typval_T *argvars;
8591 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008592 int dir;
8593{
8594#ifdef FEAT_SEARCHPATH
8595 char_u *fname;
8596 char_u *fresult = NULL;
8597 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8598 char_u *p;
8599 char_u pathbuf[NUMBUFLEN];
8600 int count = 1;
8601 int first = TRUE;
8602
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008603 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008604
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008605 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008606 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008607 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8608 if (p == NULL)
8609 count = -1; /* error */
8610 else
8611 {
8612 if (*p != NUL)
8613 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008614
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008615 if (argvars[2].v_type != VAR_UNKNOWN)
8616 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8617 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008618 }
8619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008620 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008622 do
8623 {
8624 vim_free(fresult);
8625 fresult = find_file_in_path_option(first ? fname : NULL,
8626 first ? (int)STRLEN(fname) : 0,
8627 0, first, path, dir, NULL);
8628 first = FALSE;
8629 } while (--count > 0 && fresult != NULL);
8630 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008631
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008632 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008633#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008634 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008635#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008636 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008637}
8638
Bram Moolenaar33570922005-01-25 22:26:29 +00008639static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8640static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008641
8642/*
8643 * Implementation of map() and filter().
8644 */
8645 static void
8646filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008647 typval_T *argvars;
8648 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008649 int map;
8650{
8651 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008652 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008653 listitem_T *li, *nli;
8654 list_T *l = NULL;
8655 dictitem_T *di;
8656 hashtab_T *ht;
8657 hashitem_T *hi;
8658 dict_T *d = NULL;
8659 typval_T save_val;
8660 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008661 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008662 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008663 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8664
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008665
8666 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008667 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008668 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008669 if ((l = argvars[0].vval.v_list) == NULL
8670 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008671 return;
8672 }
8673 else if (argvars[0].v_type == VAR_DICT)
8674 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008675 if ((d = argvars[0].vval.v_dict) == NULL
8676 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008677 return;
8678 }
8679 else
8680 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008681 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008682 return;
8683 }
8684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008685 expr = get_tv_string_buf_chk(&argvars[1], buf);
8686 /* On type errors, the preceding call has already displayed an error
8687 * message. Avoid a misleading error message for an empty string that
8688 * was not passed as argument. */
8689 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008691 prepare_vimvar(VV_VAL, &save_val);
8692 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008693
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008694 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008695 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008696 prepare_vimvar(VV_KEY, &save_key);
8697 vimvars[VV_KEY].vv_type = VAR_STRING;
8698
8699 ht = &d->dv_hashtab;
8700 hash_lock(ht);
8701 todo = ht->ht_used;
8702 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008704 if (!HASHITEM_EMPTY(hi))
8705 {
8706 --todo;
8707 di = HI2DI(hi);
8708 if (tv_check_lock(di->di_tv.v_lock, msg))
8709 break;
8710 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8711 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8712 break;
8713 if (!map && rem)
8714 dictitem_remove(d, di);
8715 clear_tv(&vimvars[VV_KEY].vv_tv);
8716 }
8717 }
8718 hash_unlock(ht);
8719
8720 restore_vimvar(VV_KEY, &save_key);
8721 }
8722 else
8723 {
8724 for (li = l->lv_first; li != NULL; li = nli)
8725 {
8726 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008727 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008728 nli = li->li_next;
8729 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008730 break;
8731 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008732 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008733 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008734 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008736 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008737 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008738
8739 copy_tv(&argvars[0], rettv);
8740}
8741
8742 static int
8743filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008744 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008745 char_u *expr;
8746 int map;
8747 int *remp;
8748{
Bram Moolenaar33570922005-01-25 22:26:29 +00008749 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008750 char_u *s;
8751
Bram Moolenaar33570922005-01-25 22:26:29 +00008752 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008753 s = expr;
8754 if (eval1(&s, &rettv, TRUE) == FAIL)
8755 return FAIL;
8756 if (*s != NUL) /* check for trailing chars after expr */
8757 {
8758 EMSG2(_(e_invexpr2), s);
8759 return FAIL;
8760 }
8761 if (map)
8762 {
8763 /* map(): replace the list item value */
8764 clear_tv(tv);
8765 *tv = rettv;
8766 }
8767 else
8768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008769 int error = FALSE;
8770
Bram Moolenaare9a41262005-01-15 22:18:47 +00008771 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008772 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008773 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008774 /* On type error, nothing has been removed; return FAIL to stop the
8775 * loop. The error message was given by get_tv_number_chk(). */
8776 if (error)
8777 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008778 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008779 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008780 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008781}
8782
8783/*
8784 * "filter()" function
8785 */
8786 static void
8787f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008788 typval_T *argvars;
8789 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008790{
8791 filter_map(argvars, rettv, FALSE);
8792}
8793
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008794/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008795 * "finddir({fname}[, {path}[, {count}]])" function
8796 */
8797 static void
8798f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008799 typval_T *argvars;
8800 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008801{
8802 findfilendir(argvars, rettv, TRUE);
8803}
8804
8805/*
8806 * "findfile({fname}[, {path}[, {count}]])" function
8807 */
8808 static void
8809f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008810 typval_T *argvars;
8811 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008812{
8813 findfilendir(argvars, rettv, FALSE);
8814}
8815
8816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 * "fnamemodify({fname}, {mods})" function
8818 */
8819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008820f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008821 typval_T *argvars;
8822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823{
8824 char_u *fname;
8825 char_u *mods;
8826 int usedlen = 0;
8827 int len;
8828 char_u *fbuf = NULL;
8829 char_u buf[NUMBUFLEN];
8830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008831 fname = get_tv_string_chk(&argvars[0]);
8832 mods = get_tv_string_buf_chk(&argvars[1], buf);
8833 if (fname == NULL || mods == NULL)
8834 fname = NULL;
8835 else
8836 {
8837 len = (int)STRLEN(fname);
8838 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008841 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008843 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008845 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 vim_free(fbuf);
8847}
8848
Bram Moolenaar33570922005-01-25 22:26:29 +00008849static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850
8851/*
8852 * "foldclosed()" function
8853 */
8854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008856 typval_T *argvars;
8857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 int end;
8859{
8860#ifdef FEAT_FOLDING
8861 linenr_T lnum;
8862 linenr_T first, last;
8863
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008864 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8866 {
8867 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8868 {
8869 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 return;
8874 }
8875 }
8876#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008877 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878}
8879
8880/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008881 * "foldclosed()" function
8882 */
8883 static void
8884f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008885 typval_T *argvars;
8886 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008887{
8888 foldclosed_both(argvars, rettv, FALSE);
8889}
8890
8891/*
8892 * "foldclosedend()" function
8893 */
8894 static void
8895f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008896 typval_T *argvars;
8897 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008898{
8899 foldclosed_both(argvars, rettv, TRUE);
8900}
8901
8902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 * "foldlevel()" function
8904 */
8905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008906f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008907 typval_T *argvars;
8908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909{
8910#ifdef FEAT_FOLDING
8911 linenr_T lnum;
8912
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 else
8917#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008918 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919}
8920
8921/*
8922 * "foldtext()" function
8923 */
8924/*ARGSUSED*/
8925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008926f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008927 typval_T *argvars;
8928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929{
8930#ifdef FEAT_FOLDING
8931 linenr_T lnum;
8932 char_u *s;
8933 char_u *r;
8934 int len;
8935 char *txt;
8936#endif
8937
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008938 rettv->v_type = VAR_STRING;
8939 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008941 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8942 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8943 <= curbuf->b_ml.ml_line_count
8944 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 {
8946 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008947 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8948 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 {
8950 if (!linewhite(lnum))
8951 break;
8952 ++lnum;
8953 }
8954
8955 /* Find interesting text in this line. */
8956 s = skipwhite(ml_get(lnum));
8957 /* skip C comment-start */
8958 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008959 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008961 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008962 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008963 {
8964 s = skipwhite(ml_get(lnum + 1));
8965 if (*s == '*')
8966 s = skipwhite(s + 1);
8967 }
8968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 txt = _("+-%s%3ld lines: ");
8970 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008971 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 + 20 /* for %3ld */
8973 + STRLEN(s))); /* concatenated */
8974 if (r != NULL)
8975 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008976 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8977 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8978 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 len = (int)STRLEN(r);
8980 STRCAT(r, s);
8981 /* remove 'foldmarker' and 'commentstring' */
8982 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008983 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 }
8985 }
8986#endif
8987}
8988
8989/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008990 * "foldtextresult(lnum)" function
8991 */
8992/*ARGSUSED*/
8993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008994f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008995 typval_T *argvars;
8996 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008997{
8998#ifdef FEAT_FOLDING
8999 linenr_T lnum;
9000 char_u *text;
9001 char_u buf[51];
9002 foldinfo_T foldinfo;
9003 int fold_count;
9004#endif
9005
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006 rettv->v_type = VAR_STRING;
9007 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009008#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009009 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009010 /* treat illegal types and illegal string values for {lnum} the same */
9011 if (lnum < 0)
9012 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009013 fold_count = foldedCount(curwin, lnum, &foldinfo);
9014 if (fold_count > 0)
9015 {
9016 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9017 &foldinfo, buf);
9018 if (text == buf)
9019 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009020 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009021 }
9022#endif
9023}
9024
9025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 * "foreground()" function
9027 */
9028/*ARGSUSED*/
9029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009030f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009031 typval_T *argvars;
9032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009034 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035#ifdef FEAT_GUI
9036 if (gui.in_use)
9037 gui_mch_set_foreground();
9038#else
9039# ifdef WIN32
9040 win32_set_foreground();
9041# endif
9042#endif
9043}
9044
9045/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009046 * "function()" function
9047 */
9048/*ARGSUSED*/
9049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 typval_T *argvars;
9052 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009053{
9054 char_u *s;
9055
Bram Moolenaara7043832005-01-21 11:56:39 +00009056 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009058 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009059 EMSG2(_(e_invarg2), s);
9060 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009061 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009062 else
9063 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009064 rettv->vval.v_string = vim_strsave(s);
9065 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009066 }
9067}
9068
9069/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009070 * "garbagecollect()" function
9071 */
9072/*ARGSUSED*/
9073 static void
9074f_garbagecollect(argvars, rettv)
9075 typval_T *argvars;
9076 typval_T *rettv;
9077{
9078 garbage_collect();
9079}
9080
9081/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009082 * "get()" function
9083 */
9084 static void
9085f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009086 typval_T *argvars;
9087 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009088{
Bram Moolenaar33570922005-01-25 22:26:29 +00009089 listitem_T *li;
9090 list_T *l;
9091 dictitem_T *di;
9092 dict_T *d;
9093 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009094
Bram Moolenaare9a41262005-01-15 22:18:47 +00009095 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009096 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009097 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009099 int error = FALSE;
9100
9101 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9102 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009103 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009104 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009105 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009106 else if (argvars[0].v_type == VAR_DICT)
9107 {
9108 if ((d = argvars[0].vval.v_dict) != NULL)
9109 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009110 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009111 if (di != NULL)
9112 tv = &di->di_tv;
9113 }
9114 }
9115 else
9116 EMSG2(_(e_listdictarg), "get()");
9117
9118 if (tv == NULL)
9119 {
9120 if (argvars[2].v_type == VAR_UNKNOWN)
9121 rettv->vval.v_number = 0;
9122 else
9123 copy_tv(&argvars[2], rettv);
9124 }
9125 else
9126 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009127}
9128
Bram Moolenaar342337a2005-07-21 21:11:17 +00009129static 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 +00009130
9131/*
9132 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009133 * Return a range (from start to end) of lines in rettv from the specified
9134 * buffer.
9135 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009136 */
9137 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009138get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009139 buf_T *buf;
9140 linenr_T start;
9141 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009142 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009143 typval_T *rettv;
9144{
9145 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009146 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009147 listitem_T *li;
9148
Bram Moolenaar342337a2005-07-21 21:11:17 +00009149 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009150 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009151 l = list_alloc();
9152 if (l == NULL)
9153 return;
9154
9155 rettv->vval.v_list = l;
9156 rettv->v_type = VAR_LIST;
9157 ++l->lv_refcount;
9158 }
9159 else
9160 rettv->vval.v_number = 0;
9161
9162 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9163 return;
9164
9165 if (!retlist)
9166 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009167 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9168 p = ml_get_buf(buf, start, FALSE);
9169 else
9170 p = (char_u *)"";
9171
9172 rettv->v_type = VAR_STRING;
9173 rettv->vval.v_string = vim_strsave(p);
9174 }
9175 else
9176 {
9177 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009178 return;
9179
9180 if (start < 1)
9181 start = 1;
9182 if (end > buf->b_ml.ml_line_count)
9183 end = buf->b_ml.ml_line_count;
9184 while (start <= end)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009185 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009186 li = listitem_alloc();
9187 if (li == NULL)
9188 break;
9189 list_append(l, li);
9190 li->li_tv.v_type = VAR_STRING;
9191 li->li_tv.v_lock = 0;
9192 li->li_tv.vval.v_string =
9193 vim_strsave(ml_get_buf(buf, start++, FALSE));
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009194 }
9195 }
9196}
9197
9198/*
9199 * "getbufline()" function
9200 */
9201 static void
9202f_getbufline(argvars, rettv)
9203 typval_T *argvars;
9204 typval_T *rettv;
9205{
9206 linenr_T lnum;
9207 linenr_T end;
9208 buf_T *buf;
9209
9210 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9211 ++emsg_off;
9212 buf = get_buf_tv(&argvars[0]);
9213 --emsg_off;
9214
Bram Moolenaar661b1822005-07-28 22:36:45 +00009215 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009216 if (argvars[2].v_type == VAR_UNKNOWN)
9217 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009218 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009219 end = get_tv_lnum_buf(&argvars[2], buf);
9220
Bram Moolenaar342337a2005-07-21 21:11:17 +00009221 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009222}
9223
Bram Moolenaar0d660222005-01-07 21:51:51 +00009224/*
9225 * "getbufvar()" function
9226 */
9227 static void
9228f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009229 typval_T *argvars;
9230 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009231{
9232 buf_T *buf;
9233 buf_T *save_curbuf;
9234 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009235 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009236
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009237 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9238 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009239 ++emsg_off;
9240 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009241
9242 rettv->v_type = VAR_STRING;
9243 rettv->vval.v_string = NULL;
9244
9245 if (buf != NULL && varname != NULL)
9246 {
9247 if (*varname == '&') /* buffer-local-option */
9248 {
9249 /* set curbuf to be our buf, temporarily */
9250 save_curbuf = curbuf;
9251 curbuf = buf;
9252
9253 get_option_tv(&varname, rettv, TRUE);
9254
9255 /* restore previous notion of curbuf */
9256 curbuf = save_curbuf;
9257 }
9258 else
9259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009260 if (*varname == NUL)
9261 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9262 * scope prefix before the NUL byte is required by
9263 * find_var_in_ht(). */
9264 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009265 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009266 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009267 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009268 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009269 }
9270 }
9271
9272 --emsg_off;
9273}
9274
9275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 * "getchar()" function
9277 */
9278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009279f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009280 typval_T *argvars;
9281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282{
9283 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009284 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285
9286 ++no_mapping;
9287 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009288 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 /* getchar(): blocking wait. */
9290 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009291 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 /* getchar(1): only check if char avail */
9293 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009294 else if (error || vpeekc() == NUL)
9295 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 n = 0;
9297 else
9298 /* getchar(0) and char avail: return char */
9299 n = safe_vgetc();
9300 --no_mapping;
9301 --allow_keys;
9302
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 if (IS_SPECIAL(n) || mod_mask != 0)
9305 {
9306 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9307 int i = 0;
9308
9309 /* Turn a special key into three bytes, plus modifier. */
9310 if (mod_mask != 0)
9311 {
9312 temp[i++] = K_SPECIAL;
9313 temp[i++] = KS_MODIFIER;
9314 temp[i++] = mod_mask;
9315 }
9316 if (IS_SPECIAL(n))
9317 {
9318 temp[i++] = K_SPECIAL;
9319 temp[i++] = K_SECOND(n);
9320 temp[i++] = K_THIRD(n);
9321 }
9322#ifdef FEAT_MBYTE
9323 else if (has_mbyte)
9324 i += (*mb_char2bytes)(n, temp + i);
9325#endif
9326 else
9327 temp[i++] = n;
9328 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009329 rettv->v_type = VAR_STRING;
9330 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 }
9332}
9333
9334/*
9335 * "getcharmod()" function
9336 */
9337/*ARGSUSED*/
9338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009339f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009340 typval_T *argvars;
9341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009343 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344}
9345
9346/*
9347 * "getcmdline()" function
9348 */
9349/*ARGSUSED*/
9350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009352 typval_T *argvars;
9353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009355 rettv->v_type = VAR_STRING;
9356 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357}
9358
9359/*
9360 * "getcmdpos()" function
9361 */
9362/*ARGSUSED*/
9363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009364f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009365 typval_T *argvars;
9366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369}
9370
9371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 * "getcwd()" function
9373 */
9374/*ARGSUSED*/
9375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009376f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009377 typval_T *argvars;
9378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379{
9380 char_u cwd[MAXPATHL];
9381
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009382 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009384 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 else
9386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009389 if (rettv->vval.v_string != NULL)
9390 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391#endif
9392 }
9393}
9394
9395/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009396 * "getfontname()" function
9397 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009398/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009401 typval_T *argvars;
9402 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009403{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404 rettv->v_type = VAR_STRING;
9405 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009406#ifdef FEAT_GUI
9407 if (gui.in_use)
9408 {
9409 GuiFont font;
9410 char_u *name = NULL;
9411
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009412 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009413 {
9414 /* Get the "Normal" font. Either the name saved by
9415 * hl_set_font_name() or from the font ID. */
9416 font = gui.norm_font;
9417 name = hl_get_font_name();
9418 }
9419 else
9420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009421 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009422 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9423 return;
9424 font = gui_mch_get_font(name, FALSE);
9425 if (font == NOFONT)
9426 return; /* Invalid font name, return empty string. */
9427 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009428 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009429 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009430 gui_mch_free_font(font);
9431 }
9432#endif
9433}
9434
9435/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009436 * "getfperm({fname})" function
9437 */
9438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009439f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009440 typval_T *argvars;
9441 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009442{
9443 char_u *fname;
9444 struct stat st;
9445 char_u *perm = NULL;
9446 char_u flags[] = "rwx";
9447 int i;
9448
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009450
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009452 if (mch_stat((char *)fname, &st) >= 0)
9453 {
9454 perm = vim_strsave((char_u *)"---------");
9455 if (perm != NULL)
9456 {
9457 for (i = 0; i < 9; i++)
9458 {
9459 if (st.st_mode & (1 << (8 - i)))
9460 perm[i] = flags[i % 3];
9461 }
9462 }
9463 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009465}
9466
9467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 * "getfsize({fname})" function
9469 */
9470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009472 typval_T *argvars;
9473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474{
9475 char_u *fname;
9476 struct stat st;
9477
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009478 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009480 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481
9482 if (mch_stat((char *)fname, &st) >= 0)
9483 {
9484 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009485 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009487 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 }
9489 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491}
9492
9493/*
9494 * "getftime({fname})" function
9495 */
9496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009498 typval_T *argvars;
9499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500{
9501 char_u *fname;
9502 struct stat st;
9503
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505
9506 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009507 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009509 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510}
9511
9512/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009513 * "getftype({fname})" function
9514 */
9515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009517 typval_T *argvars;
9518 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009519{
9520 char_u *fname;
9521 struct stat st;
9522 char_u *type = NULL;
9523 char *t;
9524
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009525 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009526
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009527 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009528 if (mch_lstat((char *)fname, &st) >= 0)
9529 {
9530#ifdef S_ISREG
9531 if (S_ISREG(st.st_mode))
9532 t = "file";
9533 else if (S_ISDIR(st.st_mode))
9534 t = "dir";
9535# ifdef S_ISLNK
9536 else if (S_ISLNK(st.st_mode))
9537 t = "link";
9538# endif
9539# ifdef S_ISBLK
9540 else if (S_ISBLK(st.st_mode))
9541 t = "bdev";
9542# endif
9543# ifdef S_ISCHR
9544 else if (S_ISCHR(st.st_mode))
9545 t = "cdev";
9546# endif
9547# ifdef S_ISFIFO
9548 else if (S_ISFIFO(st.st_mode))
9549 t = "fifo";
9550# endif
9551# ifdef S_ISSOCK
9552 else if (S_ISSOCK(st.st_mode))
9553 t = "fifo";
9554# endif
9555 else
9556 t = "other";
9557#else
9558# ifdef S_IFMT
9559 switch (st.st_mode & S_IFMT)
9560 {
9561 case S_IFREG: t = "file"; break;
9562 case S_IFDIR: t = "dir"; break;
9563# ifdef S_IFLNK
9564 case S_IFLNK: t = "link"; break;
9565# endif
9566# ifdef S_IFBLK
9567 case S_IFBLK: t = "bdev"; break;
9568# endif
9569# ifdef S_IFCHR
9570 case S_IFCHR: t = "cdev"; break;
9571# endif
9572# ifdef S_IFIFO
9573 case S_IFIFO: t = "fifo"; break;
9574# endif
9575# ifdef S_IFSOCK
9576 case S_IFSOCK: t = "socket"; break;
9577# endif
9578 default: t = "other";
9579 }
9580# else
9581 if (mch_isdir(fname))
9582 t = "dir";
9583 else
9584 t = "file";
9585# endif
9586#endif
9587 type = vim_strsave((char_u *)t);
9588 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009590}
9591
9592/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009593 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009594 */
9595 static void
9596f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009597 typval_T *argvars;
9598 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009599{
9600 linenr_T lnum;
9601 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009602 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009603
9604 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009605 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009606 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009607 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009608 retlist = FALSE;
9609 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009610 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009611 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009612 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009613 retlist = TRUE;
9614 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009615
Bram Moolenaar342337a2005-07-21 21:11:17 +00009616 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009617}
9618
9619/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009620 * "getqflist()" function
9621 */
9622/*ARGSUSED*/
9623 static void
9624f_getqflist(argvars, rettv)
9625 typval_T *argvars;
9626 typval_T *rettv;
9627{
9628#ifdef FEAT_QUICKFIX
9629 list_T *l;
9630#endif
9631
9632 rettv->vval.v_number = FALSE;
9633#ifdef FEAT_QUICKFIX
9634 l = list_alloc();
9635 if (l != NULL)
9636 {
9637 if (get_errorlist(l) != FAIL)
9638 {
9639 rettv->vval.v_list = l;
9640 rettv->v_type = VAR_LIST;
9641 ++l->lv_refcount;
9642 }
9643 else
9644 list_free(l);
9645 }
9646#endif
9647}
9648
9649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 * "getreg()" function
9651 */
9652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009653f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009654 typval_T *argvars;
9655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656{
9657 char_u *strregname;
9658 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009659 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009660 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009662 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009663 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009664 strregname = get_tv_string_chk(&argvars[0]);
9665 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009666 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009667 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009670 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 regname = (strregname == NULL ? '"' : *strregname);
9672 if (regname == 0)
9673 regname = '"';
9674
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009675 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009676 rettv->vval.v_string = error ? NULL :
9677 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678}
9679
9680/*
9681 * "getregtype()" function
9682 */
9683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009684f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009685 typval_T *argvars;
9686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687{
9688 char_u *strregname;
9689 int regname;
9690 char_u buf[NUMBUFLEN + 2];
9691 long reglen = 0;
9692
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009693 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009694 {
9695 strregname = get_tv_string_chk(&argvars[0]);
9696 if (strregname == NULL) /* type error; errmsg already given */
9697 {
9698 rettv->v_type = VAR_STRING;
9699 rettv->vval.v_string = NULL;
9700 return;
9701 }
9702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 else
9704 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009705 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706
9707 regname = (strregname == NULL ? '"' : *strregname);
9708 if (regname == 0)
9709 regname = '"';
9710
9711 buf[0] = NUL;
9712 buf[1] = NUL;
9713 switch (get_reg_type(regname, &reglen))
9714 {
9715 case MLINE: buf[0] = 'V'; break;
9716 case MCHAR: buf[0] = 'v'; break;
9717#ifdef FEAT_VISUAL
9718 case MBLOCK:
9719 buf[0] = Ctrl_V;
9720 sprintf((char *)buf + 1, "%ld", reglen + 1);
9721 break;
9722#endif
9723 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009724 rettv->v_type = VAR_STRING;
9725 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726}
9727
9728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 * "getwinposx()" function
9730 */
9731/*ARGSUSED*/
9732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009733f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009734 typval_T *argvars;
9735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738#ifdef FEAT_GUI
9739 if (gui.in_use)
9740 {
9741 int x, y;
9742
9743 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009744 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 }
9746#endif
9747}
9748
9749/*
9750 * "getwinposy()" function
9751 */
9752/*ARGSUSED*/
9753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009754f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009755 typval_T *argvars;
9756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009758 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759#ifdef FEAT_GUI
9760 if (gui.in_use)
9761 {
9762 int x, y;
9763
9764 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009765 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 }
9767#endif
9768}
9769
Bram Moolenaara40058a2005-07-11 22:42:07 +00009770static win_T *find_win_by_nr __ARGS((typval_T *vp));
9771
9772 static win_T *
9773find_win_by_nr(vp)
9774 typval_T *vp;
9775{
9776#ifdef FEAT_WINDOWS
9777 win_T *wp;
9778#endif
9779 int nr;
9780
9781 nr = get_tv_number_chk(vp, NULL);
9782
9783#ifdef FEAT_WINDOWS
9784 if (nr < 0)
9785 return NULL;
9786 if (nr == 0)
9787 return curwin;
9788
9789 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9790 if (--nr <= 0)
9791 break;
9792 return wp;
9793#else
9794 if (nr == 0 || nr == 1)
9795 return curwin;
9796 return NULL;
9797#endif
9798}
9799
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800/*
9801 * "getwinvar()" function
9802 */
9803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009804f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009805 typval_T *argvars;
9806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807{
9808 win_T *win, *oldcurwin;
9809 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009810 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009813 varname = get_tv_string_chk(&argvars[1]);
9814 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009816 rettv->v_type = VAR_STRING;
9817 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818
9819 if (win != NULL && varname != NULL)
9820 {
9821 if (*varname == '&') /* window-local-option */
9822 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009823 /* Set curwin to be our win, temporarily. Also set curbuf, so
9824 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 oldcurwin = curwin;
9826 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009827 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009829 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830
9831 /* restore previous notion of curwin */
9832 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009833 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 }
9835 else
9836 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009837 if (*varname == NUL)
9838 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9839 * scope prefix before the NUL byte is required by
9840 * find_var_in_ht(). */
9841 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009843 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009845 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 }
9847 }
9848
9849 --emsg_off;
9850}
9851
9852/*
9853 * "glob()" function
9854 */
9855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009857 typval_T *argvars;
9858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859{
9860 expand_T xpc;
9861
9862 ExpandInit(&xpc);
9863 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009864 rettv->v_type = VAR_STRING;
9865 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9867 ExpandCleanup(&xpc);
9868}
9869
9870/*
9871 * "globpath()" function
9872 */
9873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009874f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009875 typval_T *argvars;
9876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877{
9878 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009879 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009881 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009882 if (file == NULL)
9883 rettv->vval.v_string = NULL;
9884 else
9885 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886}
9887
9888/*
9889 * "has()" function
9890 */
9891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009892f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009893 typval_T *argvars;
9894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895{
9896 int i;
9897 char_u *name;
9898 int n = FALSE;
9899 static char *(has_list[]) =
9900 {
9901#ifdef AMIGA
9902 "amiga",
9903# ifdef FEAT_ARP
9904 "arp",
9905# endif
9906#endif
9907#ifdef __BEOS__
9908 "beos",
9909#endif
9910#ifdef MSDOS
9911# ifdef DJGPP
9912 "dos32",
9913# else
9914 "dos16",
9915# endif
9916#endif
9917#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9918 "mac",
9919#endif
9920#if defined(MACOS_X_UNIX)
9921 "macunix",
9922#endif
9923#ifdef OS2
9924 "os2",
9925#endif
9926#ifdef __QNX__
9927 "qnx",
9928#endif
9929#ifdef RISCOS
9930 "riscos",
9931#endif
9932#ifdef UNIX
9933 "unix",
9934#endif
9935#ifdef VMS
9936 "vms",
9937#endif
9938#ifdef WIN16
9939 "win16",
9940#endif
9941#ifdef WIN32
9942 "win32",
9943#endif
9944#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9945 "win32unix",
9946#endif
9947#ifdef WIN64
9948 "win64",
9949#endif
9950#ifdef EBCDIC
9951 "ebcdic",
9952#endif
9953#ifndef CASE_INSENSITIVE_FILENAME
9954 "fname_case",
9955#endif
9956#ifdef FEAT_ARABIC
9957 "arabic",
9958#endif
9959#ifdef FEAT_AUTOCMD
9960 "autocmd",
9961#endif
9962#ifdef FEAT_BEVAL
9963 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +00009964# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
9965 "balloon_multiline",
9966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967#endif
9968#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9969 "builtin_terms",
9970# ifdef ALL_BUILTIN_TCAPS
9971 "all_builtin_terms",
9972# endif
9973#endif
9974#ifdef FEAT_BYTEOFF
9975 "byte_offset",
9976#endif
9977#ifdef FEAT_CINDENT
9978 "cindent",
9979#endif
9980#ifdef FEAT_CLIENTSERVER
9981 "clientserver",
9982#endif
9983#ifdef FEAT_CLIPBOARD
9984 "clipboard",
9985#endif
9986#ifdef FEAT_CMDL_COMPL
9987 "cmdline_compl",
9988#endif
9989#ifdef FEAT_CMDHIST
9990 "cmdline_hist",
9991#endif
9992#ifdef FEAT_COMMENTS
9993 "comments",
9994#endif
9995#ifdef FEAT_CRYPT
9996 "cryptv",
9997#endif
9998#ifdef FEAT_CSCOPE
9999 "cscope",
10000#endif
10001#ifdef DEBUG
10002 "debug",
10003#endif
10004#ifdef FEAT_CON_DIALOG
10005 "dialog_con",
10006#endif
10007#ifdef FEAT_GUI_DIALOG
10008 "dialog_gui",
10009#endif
10010#ifdef FEAT_DIFF
10011 "diff",
10012#endif
10013#ifdef FEAT_DIGRAPHS
10014 "digraphs",
10015#endif
10016#ifdef FEAT_DND
10017 "dnd",
10018#endif
10019#ifdef FEAT_EMACS_TAGS
10020 "emacs_tags",
10021#endif
10022 "eval", /* always present, of course! */
10023#ifdef FEAT_EX_EXTRA
10024 "ex_extra",
10025#endif
10026#ifdef FEAT_SEARCH_EXTRA
10027 "extra_search",
10028#endif
10029#ifdef FEAT_FKMAP
10030 "farsi",
10031#endif
10032#ifdef FEAT_SEARCHPATH
10033 "file_in_path",
10034#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010035#if defined(UNIX) && !defined(USE_SYSTEM)
10036 "filterpipe",
10037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038#ifdef FEAT_FIND_ID
10039 "find_in_path",
10040#endif
10041#ifdef FEAT_FOLDING
10042 "folding",
10043#endif
10044#ifdef FEAT_FOOTER
10045 "footer",
10046#endif
10047#if !defined(USE_SYSTEM) && defined(UNIX)
10048 "fork",
10049#endif
10050#ifdef FEAT_GETTEXT
10051 "gettext",
10052#endif
10053#ifdef FEAT_GUI
10054 "gui",
10055#endif
10056#ifdef FEAT_GUI_ATHENA
10057# ifdef FEAT_GUI_NEXTAW
10058 "gui_neXtaw",
10059# else
10060 "gui_athena",
10061# endif
10062#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +000010063#ifdef FEAT_GUI_KDE
10064 "gui_kde",
10065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066#ifdef FEAT_GUI_GTK
10067 "gui_gtk",
10068# ifdef HAVE_GTK2
10069 "gui_gtk2",
10070# endif
10071#endif
10072#ifdef FEAT_GUI_MAC
10073 "gui_mac",
10074#endif
10075#ifdef FEAT_GUI_MOTIF
10076 "gui_motif",
10077#endif
10078#ifdef FEAT_GUI_PHOTON
10079 "gui_photon",
10080#endif
10081#ifdef FEAT_GUI_W16
10082 "gui_win16",
10083#endif
10084#ifdef FEAT_GUI_W32
10085 "gui_win32",
10086#endif
10087#ifdef FEAT_HANGULIN
10088 "hangul_input",
10089#endif
10090#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10091 "iconv",
10092#endif
10093#ifdef FEAT_INS_EXPAND
10094 "insert_expand",
10095#endif
10096#ifdef FEAT_JUMPLIST
10097 "jumplist",
10098#endif
10099#ifdef FEAT_KEYMAP
10100 "keymap",
10101#endif
10102#ifdef FEAT_LANGMAP
10103 "langmap",
10104#endif
10105#ifdef FEAT_LIBCALL
10106 "libcall",
10107#endif
10108#ifdef FEAT_LINEBREAK
10109 "linebreak",
10110#endif
10111#ifdef FEAT_LISP
10112 "lispindent",
10113#endif
10114#ifdef FEAT_LISTCMDS
10115 "listcmds",
10116#endif
10117#ifdef FEAT_LOCALMAP
10118 "localmap",
10119#endif
10120#ifdef FEAT_MENU
10121 "menu",
10122#endif
10123#ifdef FEAT_SESSION
10124 "mksession",
10125#endif
10126#ifdef FEAT_MODIFY_FNAME
10127 "modify_fname",
10128#endif
10129#ifdef FEAT_MOUSE
10130 "mouse",
10131#endif
10132#ifdef FEAT_MOUSESHAPE
10133 "mouseshape",
10134#endif
10135#if defined(UNIX) || defined(VMS)
10136# ifdef FEAT_MOUSE_DEC
10137 "mouse_dec",
10138# endif
10139# ifdef FEAT_MOUSE_GPM
10140 "mouse_gpm",
10141# endif
10142# ifdef FEAT_MOUSE_JSB
10143 "mouse_jsbterm",
10144# endif
10145# ifdef FEAT_MOUSE_NET
10146 "mouse_netterm",
10147# endif
10148# ifdef FEAT_MOUSE_PTERM
10149 "mouse_pterm",
10150# endif
10151# ifdef FEAT_MOUSE_XTERM
10152 "mouse_xterm",
10153# endif
10154#endif
10155#ifdef FEAT_MBYTE
10156 "multi_byte",
10157#endif
10158#ifdef FEAT_MBYTE_IME
10159 "multi_byte_ime",
10160#endif
10161#ifdef FEAT_MULTI_LANG
10162 "multi_lang",
10163#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010164#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010165#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010166 "mzscheme",
10167#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169#ifdef FEAT_OLE
10170 "ole",
10171#endif
10172#ifdef FEAT_OSFILETYPE
10173 "osfiletype",
10174#endif
10175#ifdef FEAT_PATH_EXTRA
10176 "path_extra",
10177#endif
10178#ifdef FEAT_PERL
10179#ifndef DYNAMIC_PERL
10180 "perl",
10181#endif
10182#endif
10183#ifdef FEAT_PYTHON
10184#ifndef DYNAMIC_PYTHON
10185 "python",
10186#endif
10187#endif
10188#ifdef FEAT_POSTSCRIPT
10189 "postscript",
10190#endif
10191#ifdef FEAT_PRINTER
10192 "printer",
10193#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010194#ifdef FEAT_PROFILE
10195 "profile",
10196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197#ifdef FEAT_QUICKFIX
10198 "quickfix",
10199#endif
10200#ifdef FEAT_RIGHTLEFT
10201 "rightleft",
10202#endif
10203#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10204 "ruby",
10205#endif
10206#ifdef FEAT_SCROLLBIND
10207 "scrollbind",
10208#endif
10209#ifdef FEAT_CMDL_INFO
10210 "showcmd",
10211 "cmdline_info",
10212#endif
10213#ifdef FEAT_SIGNS
10214 "signs",
10215#endif
10216#ifdef FEAT_SMARTINDENT
10217 "smartindent",
10218#endif
10219#ifdef FEAT_SNIFF
10220 "sniff",
10221#endif
10222#ifdef FEAT_STL_OPT
10223 "statusline",
10224#endif
10225#ifdef FEAT_SUN_WORKSHOP
10226 "sun_workshop",
10227#endif
10228#ifdef FEAT_NETBEANS_INTG
10229 "netbeans_intg",
10230#endif
10231#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010232 "spell",
10233#endif
10234#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 "syntax",
10236#endif
10237#if defined(USE_SYSTEM) || !defined(UNIX)
10238 "system",
10239#endif
10240#ifdef FEAT_TAG_BINS
10241 "tag_binary",
10242#endif
10243#ifdef FEAT_TAG_OLDSTATIC
10244 "tag_old_static",
10245#endif
10246#ifdef FEAT_TAG_ANYWHITE
10247 "tag_any_white",
10248#endif
10249#ifdef FEAT_TCL
10250# ifndef DYNAMIC_TCL
10251 "tcl",
10252# endif
10253#endif
10254#ifdef TERMINFO
10255 "terminfo",
10256#endif
10257#ifdef FEAT_TERMRESPONSE
10258 "termresponse",
10259#endif
10260#ifdef FEAT_TEXTOBJ
10261 "textobjects",
10262#endif
10263#ifdef HAVE_TGETENT
10264 "tgetent",
10265#endif
10266#ifdef FEAT_TITLE
10267 "title",
10268#endif
10269#ifdef FEAT_TOOLBAR
10270 "toolbar",
10271#endif
10272#ifdef FEAT_USR_CMDS
10273 "user-commands", /* was accidentally included in 5.4 */
10274 "user_commands",
10275#endif
10276#ifdef FEAT_VIMINFO
10277 "viminfo",
10278#endif
10279#ifdef FEAT_VERTSPLIT
10280 "vertsplit",
10281#endif
10282#ifdef FEAT_VIRTUALEDIT
10283 "virtualedit",
10284#endif
10285#ifdef FEAT_VISUAL
10286 "visual",
10287#endif
10288#ifdef FEAT_VISUALEXTRA
10289 "visualextra",
10290#endif
10291#ifdef FEAT_VREPLACE
10292 "vreplace",
10293#endif
10294#ifdef FEAT_WILDIGN
10295 "wildignore",
10296#endif
10297#ifdef FEAT_WILDMENU
10298 "wildmenu",
10299#endif
10300#ifdef FEAT_WINDOWS
10301 "windows",
10302#endif
10303#ifdef FEAT_WAK
10304 "winaltkeys",
10305#endif
10306#ifdef FEAT_WRITEBACKUP
10307 "writebackup",
10308#endif
10309#ifdef FEAT_XIM
10310 "xim",
10311#endif
10312#ifdef FEAT_XFONTSET
10313 "xfontset",
10314#endif
10315#ifdef USE_XSMP
10316 "xsmp",
10317#endif
10318#ifdef USE_XSMP_INTERACT
10319 "xsmp_interact",
10320#endif
10321#ifdef FEAT_XCLIPBOARD
10322 "xterm_clipboard",
10323#endif
10324#ifdef FEAT_XTERM_SAVE
10325 "xterm_save",
10326#endif
10327#if defined(UNIX) && defined(FEAT_X11)
10328 "X11",
10329#endif
10330 NULL
10331 };
10332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010333 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 for (i = 0; has_list[i] != NULL; ++i)
10335 if (STRICMP(name, has_list[i]) == 0)
10336 {
10337 n = TRUE;
10338 break;
10339 }
10340
10341 if (n == FALSE)
10342 {
10343 if (STRNICMP(name, "patch", 5) == 0)
10344 n = has_patch(atoi((char *)name + 5));
10345 else if (STRICMP(name, "vim_starting") == 0)
10346 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010347#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10348 else if (STRICMP(name, "balloon_multiline") == 0)
10349 n = multiline_balloon_available();
10350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351#ifdef DYNAMIC_TCL
10352 else if (STRICMP(name, "tcl") == 0)
10353 n = tcl_enabled(FALSE);
10354#endif
10355#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10356 else if (STRICMP(name, "iconv") == 0)
10357 n = iconv_enabled(FALSE);
10358#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010359#ifdef DYNAMIC_MZSCHEME
10360 else if (STRICMP(name, "mzscheme") == 0)
10361 n = mzscheme_enabled(FALSE);
10362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363#ifdef DYNAMIC_RUBY
10364 else if (STRICMP(name, "ruby") == 0)
10365 n = ruby_enabled(FALSE);
10366#endif
10367#ifdef DYNAMIC_PYTHON
10368 else if (STRICMP(name, "python") == 0)
10369 n = python_enabled(FALSE);
10370#endif
10371#ifdef DYNAMIC_PERL
10372 else if (STRICMP(name, "perl") == 0)
10373 n = perl_enabled(FALSE);
10374#endif
10375#ifdef FEAT_GUI
10376 else if (STRICMP(name, "gui_running") == 0)
10377 n = (gui.in_use || gui.starting);
10378# ifdef FEAT_GUI_W32
10379 else if (STRICMP(name, "gui_win32s") == 0)
10380 n = gui_is_win32s();
10381# endif
10382# ifdef FEAT_BROWSE
10383 else if (STRICMP(name, "browse") == 0)
10384 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10385# endif
10386#endif
10387#ifdef FEAT_SYN_HL
10388 else if (STRICMP(name, "syntax_items") == 0)
10389 n = syntax_present(curbuf);
10390#endif
10391#if defined(WIN3264)
10392 else if (STRICMP(name, "win95") == 0)
10393 n = mch_windows95();
10394#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010395#ifdef FEAT_NETBEANS_INTG
10396 else if (STRICMP(name, "netbeans_enabled") == 0)
10397 n = usingNetbeans;
10398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 }
10400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010401 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402}
10403
10404/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010405 * "has_key()" function
10406 */
10407 static void
10408f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010409 typval_T *argvars;
10410 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010411{
10412 rettv->vval.v_number = 0;
10413 if (argvars[0].v_type != VAR_DICT)
10414 {
10415 EMSG(_(e_dictreq));
10416 return;
10417 }
10418 if (argvars[0].vval.v_dict == NULL)
10419 return;
10420
10421 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010422 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010423}
10424
10425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 * "hasmapto()" function
10427 */
10428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010429f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010430 typval_T *argvars;
10431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432{
10433 char_u *name;
10434 char_u *mode;
10435 char_u buf[NUMBUFLEN];
10436
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010437 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010438 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 mode = (char_u *)"nvo";
10440 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010441 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442
10443 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010444 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010446 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447}
10448
10449/*
10450 * "histadd()" function
10451 */
10452/*ARGSUSED*/
10453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010454f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010455 typval_T *argvars;
10456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457{
10458#ifdef FEAT_CMDHIST
10459 int histype;
10460 char_u *str;
10461 char_u buf[NUMBUFLEN];
10462#endif
10463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010464 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 if (check_restricted() || check_secure())
10466 return;
10467#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010468 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10469 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 if (histype >= 0)
10471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010472 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 if (*str != NUL)
10474 {
10475 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010476 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 return;
10478 }
10479 }
10480#endif
10481}
10482
10483/*
10484 * "histdel()" function
10485 */
10486/*ARGSUSED*/
10487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010488f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010489 typval_T *argvars;
10490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491{
10492#ifdef FEAT_CMDHIST
10493 int n;
10494 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010495 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010497 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10498 if (str == NULL)
10499 n = 0;
10500 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010502 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010503 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010505 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010506 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 else
10508 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010509 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010510 get_tv_string_buf(&argvars[1], buf));
10511 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010513 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514#endif
10515}
10516
10517/*
10518 * "histget()" function
10519 */
10520/*ARGSUSED*/
10521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010522f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010523 typval_T *argvars;
10524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525{
10526#ifdef FEAT_CMDHIST
10527 int type;
10528 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010529 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010531 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10532 if (str == NULL)
10533 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010535 {
10536 type = get_histtype(str);
10537 if (argvars[1].v_type == VAR_UNKNOWN)
10538 idx = get_history_idx(type);
10539 else
10540 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10541 /* -1 on type error */
10542 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010545 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010547 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548}
10549
10550/*
10551 * "histnr()" function
10552 */
10553/*ARGSUSED*/
10554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010556 typval_T *argvars;
10557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558{
10559 int i;
10560
10561#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010562 char_u *history = get_tv_string_chk(&argvars[0]);
10563
10564 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 if (i >= HIST_CMD && i < HIST_COUNT)
10566 i = get_history_idx(i);
10567 else
10568#endif
10569 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010570 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571}
10572
10573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 * "highlightID(name)" function
10575 */
10576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010577f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010578 typval_T *argvars;
10579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010581 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582}
10583
10584/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010585 * "highlight_exists()" function
10586 */
10587 static void
10588f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010589 typval_T *argvars;
10590 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010591{
10592 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10593}
10594
10595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 * "hostname()" function
10597 */
10598/*ARGSUSED*/
10599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010600f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010601 typval_T *argvars;
10602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603{
10604 char_u hostname[256];
10605
10606 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010607 rettv->v_type = VAR_STRING;
10608 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609}
10610
10611/*
10612 * iconv() function
10613 */
10614/*ARGSUSED*/
10615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010616f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010617 typval_T *argvars;
10618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619{
10620#ifdef FEAT_MBYTE
10621 char_u buf1[NUMBUFLEN];
10622 char_u buf2[NUMBUFLEN];
10623 char_u *from, *to, *str;
10624 vimconv_T vimconv;
10625#endif
10626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010627 rettv->v_type = VAR_STRING;
10628 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629
10630#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010631 str = get_tv_string(&argvars[0]);
10632 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10633 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 vimconv.vc_type = CONV_NONE;
10635 convert_setup(&vimconv, from, to);
10636
10637 /* If the encodings are equal, no conversion needed. */
10638 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010639 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010641 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642
10643 convert_setup(&vimconv, NULL, NULL);
10644 vim_free(from);
10645 vim_free(to);
10646#endif
10647}
10648
10649/*
10650 * "indent()" function
10651 */
10652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010653f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010654 typval_T *argvars;
10655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656{
10657 linenr_T lnum;
10658
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010659 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010661 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010663 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664}
10665
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010666/*
10667 * "index()" function
10668 */
10669 static void
10670f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 typval_T *argvars;
10672 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010673{
Bram Moolenaar33570922005-01-25 22:26:29 +000010674 list_T *l;
10675 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010676 long idx = 0;
10677 int ic = FALSE;
10678
10679 rettv->vval.v_number = -1;
10680 if (argvars[0].v_type != VAR_LIST)
10681 {
10682 EMSG(_(e_listreq));
10683 return;
10684 }
10685 l = argvars[0].vval.v_list;
10686 if (l != NULL)
10687 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010688 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010689 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010691 int error = FALSE;
10692
Bram Moolenaar758711c2005-02-02 23:11:38 +000010693 /* Start at specified item. Use the cached index that list_find()
10694 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010695 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010696 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010697 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010698 ic = get_tv_number_chk(&argvars[3], &error);
10699 if (error)
10700 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010701 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010702
Bram Moolenaar758711c2005-02-02 23:11:38 +000010703 for ( ; item != NULL; item = item->li_next, ++idx)
10704 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010705 {
10706 rettv->vval.v_number = idx;
10707 break;
10708 }
10709 }
10710}
10711
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712static int inputsecret_flag = 0;
10713
10714/*
10715 * "input()" function
10716 * Also handles inputsecret() when inputsecret is set.
10717 */
10718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010719f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010720 typval_T *argvars;
10721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010723 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 char_u *p = NULL;
10725 int c;
10726 char_u buf[NUMBUFLEN];
10727 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010728 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010730 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731
10732#ifdef NO_CONSOLE_INPUT
10733 /* While starting up, there is no place to enter text. */
10734 if (no_console_input())
10735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010736 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 return;
10738 }
10739#endif
10740
10741 cmd_silent = FALSE; /* Want to see the prompt. */
10742 if (prompt != NULL)
10743 {
10744 /* Only the part of the message after the last NL is considered as
10745 * prompt for the command line */
10746 p = vim_strrchr(prompt, '\n');
10747 if (p == NULL)
10748 p = prompt;
10749 else
10750 {
10751 ++p;
10752 c = *p;
10753 *p = NUL;
10754 msg_start();
10755 msg_clr_eos();
10756 msg_puts_attr(prompt, echo_attr);
10757 msg_didout = FALSE;
10758 msg_starthere();
10759 *p = c;
10760 }
10761 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010763 if (argvars[1].v_type != VAR_UNKNOWN)
10764 {
10765 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10766 if (defstr != NULL)
10767 stuffReadbuffSpec(defstr);
10768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010770 if (defstr != NULL)
10771 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010774 /* since the user typed this, no need to wait for return */
10775 need_wait_return = FALSE;
10776 msg_didout = FALSE;
10777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 cmd_silent = cmd_silent_save;
10779}
10780
10781/*
10782 * "inputdialog()" function
10783 */
10784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010785f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010786 typval_T *argvars;
10787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788{
10789#if defined(FEAT_GUI_TEXTDIALOG)
10790 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10791 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10792 {
10793 char_u *message;
10794 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010795 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010797 message = get_tv_string_chk(&argvars[0]);
10798 if (argvars[1].v_type != VAR_UNKNOWN
10799 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010800 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 else
10802 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010803 if (message != NULL && defstr != NULL
10804 && do_dialog(VIM_QUESTION, NULL, message,
10805 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010806 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 else
10808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010809 if (message != NULL && defstr != NULL
10810 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010811 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010812 rettv->vval.v_string = vim_strsave(
10813 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010815 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 }
10819 else
10820#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010821 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822}
10823
10824static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10825
10826/*
10827 * "inputrestore()" function
10828 */
10829/*ARGSUSED*/
10830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010832 typval_T *argvars;
10833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834{
10835 if (ga_userinput.ga_len > 0)
10836 {
10837 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10839 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 }
10842 else if (p_verbose > 1)
10843 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010844 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010845 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 }
10847}
10848
10849/*
10850 * "inputsave()" function
10851 */
10852/*ARGSUSED*/
10853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010854f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010855 typval_T *argvars;
10856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857{
10858 /* Add an entry to the stack of typehead storage. */
10859 if (ga_grow(&ga_userinput, 1) == OK)
10860 {
10861 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10862 + ga_userinput.ga_len);
10863 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010864 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 }
10866 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010867 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868}
10869
10870/*
10871 * "inputsecret()" function
10872 */
10873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874f_inputsecret(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 ++cmdline_star;
10879 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 --cmdline_star;
10882 --inputsecret_flag;
10883}
10884
10885/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010886 * "insert()" function
10887 */
10888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010889f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010890 typval_T *argvars;
10891 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010892{
10893 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010894 listitem_T *item;
10895 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010896 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010897
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010898 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010899 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010900 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010901 else if ((l = argvars[0].vval.v_list) != NULL
10902 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010903 {
10904 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010905 before = get_tv_number_chk(&argvars[2], &error);
10906 if (error)
10907 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010908
Bram Moolenaar758711c2005-02-02 23:11:38 +000010909 if (before == l->lv_len)
10910 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010911 else
10912 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010913 item = list_find(l, before);
10914 if (item == NULL)
10915 {
10916 EMSGN(_(e_listidx), before);
10917 l = NULL;
10918 }
10919 }
10920 if (l != NULL)
10921 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010922 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010923 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010924 }
10925 }
10926}
10927
10928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 * "isdirectory()" function
10930 */
10931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010932f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010933 typval_T *argvars;
10934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010936 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937}
10938
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010939/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010940 * "islocked()" function
10941 */
10942 static void
10943f_islocked(argvars, rettv)
10944 typval_T *argvars;
10945 typval_T *rettv;
10946{
10947 lval_T lv;
10948 char_u *end;
10949 dictitem_T *di;
10950
10951 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010952 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10953 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010954 if (end != NULL && lv.ll_name != NULL)
10955 {
10956 if (*end != NUL)
10957 EMSG(_(e_trailing));
10958 else
10959 {
10960 if (lv.ll_tv == NULL)
10961 {
10962 if (check_changedtick(lv.ll_name))
10963 rettv->vval.v_number = 1; /* always locked */
10964 else
10965 {
10966 di = find_var(lv.ll_name, NULL);
10967 if (di != NULL)
10968 {
10969 /* Consider a variable locked when:
10970 * 1. the variable itself is locked
10971 * 2. the value of the variable is locked.
10972 * 3. the List or Dict value is locked.
10973 */
10974 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10975 || tv_islocked(&di->di_tv));
10976 }
10977 }
10978 }
10979 else if (lv.ll_range)
10980 EMSG(_("E745: Range not allowed"));
10981 else if (lv.ll_newkey != NULL)
10982 EMSG2(_(e_dictkey), lv.ll_newkey);
10983 else if (lv.ll_list != NULL)
10984 /* List item. */
10985 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10986 else
10987 /* Dictionary item. */
10988 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10989 }
10990 }
10991
10992 clear_lval(&lv);
10993}
10994
Bram Moolenaar33570922005-01-25 22:26:29 +000010995static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010996
10997/*
10998 * Turn a dict into a list:
10999 * "what" == 0: list of keys
11000 * "what" == 1: list of values
11001 * "what" == 2: list of items
11002 */
11003 static void
11004dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011005 typval_T *argvars;
11006 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011007 int what;
11008{
Bram Moolenaar33570922005-01-25 22:26:29 +000011009 list_T *l;
11010 list_T *l2;
11011 dictitem_T *di;
11012 hashitem_T *hi;
11013 listitem_T *li;
11014 listitem_T *li2;
11015 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011016 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011017
11018 rettv->vval.v_number = 0;
11019 if (argvars[0].v_type != VAR_DICT)
11020 {
11021 EMSG(_(e_dictreq));
11022 return;
11023 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011024 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011025 return;
11026
11027 l = list_alloc();
11028 if (l == NULL)
11029 return;
11030 rettv->v_type = VAR_LIST;
11031 rettv->vval.v_list = l;
11032 ++l->lv_refcount;
11033
Bram Moolenaar33570922005-01-25 22:26:29 +000011034 todo = d->dv_hashtab.ht_used;
11035 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011036 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011037 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011038 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011039 --todo;
11040 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011041
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011042 li = listitem_alloc();
11043 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011044 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011045 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011046
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011047 if (what == 0)
11048 {
11049 /* keys() */
11050 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011051 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011052 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11053 }
11054 else if (what == 1)
11055 {
11056 /* values() */
11057 copy_tv(&di->di_tv, &li->li_tv);
11058 }
11059 else
11060 {
11061 /* items() */
11062 l2 = list_alloc();
11063 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011064 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011065 li->li_tv.vval.v_list = l2;
11066 if (l2 == NULL)
11067 break;
11068 ++l2->lv_refcount;
11069
11070 li2 = listitem_alloc();
11071 if (li2 == NULL)
11072 break;
11073 list_append(l2, li2);
11074 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011075 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011076 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11077
11078 li2 = listitem_alloc();
11079 if (li2 == NULL)
11080 break;
11081 list_append(l2, li2);
11082 copy_tv(&di->di_tv, &li2->li_tv);
11083 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011084 }
11085 }
11086}
11087
11088/*
11089 * "items(dict)" function
11090 */
11091 static void
11092f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011093 typval_T *argvars;
11094 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011095{
11096 dict_list(argvars, rettv, 2);
11097}
11098
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011100 * "join()" function
11101 */
11102 static void
11103f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011104 typval_T *argvars;
11105 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011106{
11107 garray_T ga;
11108 char_u *sep;
11109
11110 rettv->vval.v_number = 0;
11111 if (argvars[0].v_type != VAR_LIST)
11112 {
11113 EMSG(_(e_listreq));
11114 return;
11115 }
11116 if (argvars[0].vval.v_list == NULL)
11117 return;
11118 if (argvars[1].v_type == VAR_UNKNOWN)
11119 sep = (char_u *)" ";
11120 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011121 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011122
11123 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011124
11125 if (sep != NULL)
11126 {
11127 ga_init2(&ga, (int)sizeof(char), 80);
11128 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11129 ga_append(&ga, NUL);
11130 rettv->vval.v_string = (char_u *)ga.ga_data;
11131 }
11132 else
11133 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011134}
11135
11136/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011137 * "keys()" function
11138 */
11139 static void
11140f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011141 typval_T *argvars;
11142 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011143{
11144 dict_list(argvars, rettv, 0);
11145}
11146
11147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 * "last_buffer_nr()" function.
11149 */
11150/*ARGSUSED*/
11151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011153 typval_T *argvars;
11154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155{
11156 int n = 0;
11157 buf_T *buf;
11158
11159 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11160 if (n < buf->b_fnum)
11161 n = buf->b_fnum;
11162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011163 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011164}
11165
11166/*
11167 * "len()" function
11168 */
11169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011170f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011171 typval_T *argvars;
11172 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011173{
11174 switch (argvars[0].v_type)
11175 {
11176 case VAR_STRING:
11177 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011178 rettv->vval.v_number = (varnumber_T)STRLEN(
11179 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011180 break;
11181 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011182 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011183 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011184 case VAR_DICT:
11185 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11186 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011187 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011188 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011189 break;
11190 }
11191}
11192
Bram Moolenaar33570922005-01-25 22:26:29 +000011193static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011194
11195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011196libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011197 typval_T *argvars;
11198 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011199 int type;
11200{
11201#ifdef FEAT_LIBCALL
11202 char_u *string_in;
11203 char_u **string_result;
11204 int nr_result;
11205#endif
11206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011207 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011208 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011209 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011210 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011211 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011212
11213 if (check_restricted() || check_secure())
11214 return;
11215
11216#ifdef FEAT_LIBCALL
11217 /* The first two args must be strings, otherwise its meaningless */
11218 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11219 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011220 string_in = NULL;
11221 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011222 string_in = argvars[2].vval.v_string;
11223 if (type == VAR_NUMBER)
11224 string_result = NULL;
11225 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011227 if (mch_libcall(argvars[0].vval.v_string,
11228 argvars[1].vval.v_string,
11229 string_in,
11230 argvars[2].vval.v_number,
11231 string_result,
11232 &nr_result) == OK
11233 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011234 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011235 }
11236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237}
11238
11239/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011240 * "libcall()" function
11241 */
11242 static void
11243f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011244 typval_T *argvars;
11245 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011246{
11247 libcall_common(argvars, rettv, VAR_STRING);
11248}
11249
11250/*
11251 * "libcallnr()" function
11252 */
11253 static void
11254f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011255 typval_T *argvars;
11256 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011257{
11258 libcall_common(argvars, rettv, VAR_NUMBER);
11259}
11260
11261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262 * "line(string)" function
11263 */
11264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011266 typval_T *argvars;
11267 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268{
11269 linenr_T lnum = 0;
11270 pos_T *fp;
11271
11272 fp = var2fpos(&argvars[0], TRUE);
11273 if (fp != NULL)
11274 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276}
11277
11278/*
11279 * "line2byte(lnum)" function
11280 */
11281/*ARGSUSED*/
11282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011284 typval_T *argvars;
11285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286{
11287#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289#else
11290 linenr_T lnum;
11291
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011292 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11297 if (rettv->vval.v_number >= 0)
11298 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299#endif
11300}
11301
11302/*
11303 * "lispindent(lnum)" function
11304 */
11305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011306f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011307 typval_T *argvars;
11308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309{
11310#ifdef FEAT_LISP
11311 pos_T pos;
11312 linenr_T lnum;
11313
11314 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011315 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11317 {
11318 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011319 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320 curwin->w_cursor = pos;
11321 }
11322 else
11323#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011324 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325}
11326
11327/*
11328 * "localtime()" function
11329 */
11330/*ARGSUSED*/
11331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011332f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011333 typval_T *argvars;
11334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337}
11338
Bram Moolenaar33570922005-01-25 22:26:29 +000011339static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340
11341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011343 typval_T *argvars;
11344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 int exact;
11346{
11347 char_u *keys;
11348 char_u *which;
11349 char_u buf[NUMBUFLEN];
11350 char_u *keys_buf = NULL;
11351 char_u *rhs;
11352 int mode;
11353 garray_T ga;
11354
11355 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011356 rettv->v_type = VAR_STRING;
11357 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 if (*keys == NUL)
11361 return;
11362
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011363 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011364 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 else
11366 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011367 if (which == NULL)
11368 return;
11369
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 mode = get_map_mode(&which, 0);
11371
11372 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011373 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 vim_free(keys_buf);
11375 if (rhs != NULL)
11376 {
11377 ga_init(&ga);
11378 ga.ga_itemsize = 1;
11379 ga.ga_growsize = 40;
11380
11381 while (*rhs != NUL)
11382 ga_concat(&ga, str2special(&rhs, FALSE));
11383
11384 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386 }
11387}
11388
11389/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011390 * "map()" function
11391 */
11392 static void
11393f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011394 typval_T *argvars;
11395 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011396{
11397 filter_map(argvars, rettv, TRUE);
11398}
11399
11400/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011401 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 */
11403 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011404f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011405 typval_T *argvars;
11406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011408 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409}
11410
11411/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011412 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 */
11414 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011415f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011416 typval_T *argvars;
11417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011419 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420}
11421
Bram Moolenaar33570922005-01-25 22:26:29 +000011422static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423
11424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011425find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011426 typval_T *argvars;
11427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428 int type;
11429{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011430 char_u *str = NULL;
11431 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 char_u *pat;
11433 regmatch_T regmatch;
11434 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011435 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 char_u *save_cpo;
11437 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011438 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011439 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011440 list_T *l = NULL;
11441 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011442 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011443 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444
11445 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11446 save_cpo = p_cpo;
11447 p_cpo = (char_u *)"";
11448
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011449 rettv->vval.v_number = -1;
11450 if (type == 3)
11451 {
11452 /* return empty list when there are no matches */
11453 if ((rettv->vval.v_list = list_alloc()) == NULL)
11454 goto theend;
11455 rettv->v_type = VAR_LIST;
11456 ++rettv->vval.v_list->lv_refcount;
11457 }
11458 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460 rettv->v_type = VAR_STRING;
11461 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011464 if (argvars[0].v_type == VAR_LIST)
11465 {
11466 if ((l = argvars[0].vval.v_list) == NULL)
11467 goto theend;
11468 li = l->lv_first;
11469 }
11470 else
11471 expr = str = get_tv_string(&argvars[0]);
11472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011473 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11474 if (pat == NULL)
11475 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011476
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011477 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011479 int error = FALSE;
11480
11481 start = get_tv_number_chk(&argvars[2], &error);
11482 if (error)
11483 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011484 if (l != NULL)
11485 {
11486 li = list_find(l, start);
11487 if (li == NULL)
11488 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011489 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011490 }
11491 else
11492 {
11493 if (start < 0)
11494 start = 0;
11495 if (start > (long)STRLEN(str))
11496 goto theend;
11497 str += start;
11498 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011499
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011500 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011501 nth = get_tv_number_chk(&argvars[3], &error);
11502 if (error)
11503 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504 }
11505
11506 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11507 if (regmatch.regprog != NULL)
11508 {
11509 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011510
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011511 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011512 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011513 if (l != NULL)
11514 {
11515 if (li == NULL)
11516 {
11517 match = FALSE;
11518 break;
11519 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011520 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011521 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011522 if (str == NULL)
11523 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011524 }
11525
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011526 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011527
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011528 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011529 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011530 if (l == NULL && !match)
11531 break;
11532
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011533 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011534 if (l != NULL)
11535 {
11536 li = li->li_next;
11537 ++idx;
11538 }
11539 else
11540 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011541#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011542 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011543#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011544 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011545#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011546 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011547 }
11548
11549 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011551 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011552 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011553 int i;
11554
11555 /* return list with matched string and submatches */
11556 for (i = 0; i < NSUBEXP; ++i)
11557 {
11558 if (regmatch.endp[i] == NULL)
11559 break;
11560 li = listitem_alloc();
11561 if (li == NULL)
11562 break;
11563 li->li_tv.v_type = VAR_STRING;
11564 li->li_tv.v_lock = 0;
11565 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11566 (int)(regmatch.endp[i] - regmatch.startp[i]));
11567 list_append(rettv->vval.v_list, li);
11568 }
11569 }
11570 else if (type == 2)
11571 {
11572 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011573 if (l != NULL)
11574 copy_tv(&li->li_tv, rettv);
11575 else
11576 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011578 }
11579 else if (l != NULL)
11580 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581 else
11582 {
11583 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011584 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 (varnumber_T)(regmatch.startp[0] - str);
11586 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 }
11591 }
11592 vim_free(regmatch.regprog);
11593 }
11594
11595theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011596 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597 p_cpo = save_cpo;
11598}
11599
11600/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011601 * "match()" function
11602 */
11603 static void
11604f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011605 typval_T *argvars;
11606 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011607{
11608 find_some_match(argvars, rettv, 1);
11609}
11610
11611/*
11612 * "matchend()" function
11613 */
11614 static void
11615f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011616 typval_T *argvars;
11617 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011618{
11619 find_some_match(argvars, rettv, 0);
11620}
11621
11622/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011623 * "matchlist()" function
11624 */
11625 static void
11626f_matchlist(argvars, rettv)
11627 typval_T *argvars;
11628 typval_T *rettv;
11629{
11630 find_some_match(argvars, rettv, 3);
11631}
11632
11633/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011634 * "matchstr()" function
11635 */
11636 static void
11637f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011638 typval_T *argvars;
11639 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011640{
11641 find_some_match(argvars, rettv, 2);
11642}
11643
Bram Moolenaar33570922005-01-25 22:26:29 +000011644static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011645
11646 static void
11647max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011648 typval_T *argvars;
11649 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011650 int domax;
11651{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011652 long n = 0;
11653 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011654 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011655
11656 if (argvars[0].v_type == VAR_LIST)
11657 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011658 list_T *l;
11659 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011660
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011661 l = argvars[0].vval.v_list;
11662 if (l != NULL)
11663 {
11664 li = l->lv_first;
11665 if (li != NULL)
11666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011667 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011668 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011669 {
11670 li = li->li_next;
11671 if (li == NULL)
11672 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011673 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011674 if (domax ? i > n : i < n)
11675 n = i;
11676 }
11677 }
11678 }
11679 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011680 else if (argvars[0].v_type == VAR_DICT)
11681 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011682 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011683 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011684 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011685 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011686
11687 d = argvars[0].vval.v_dict;
11688 if (d != NULL)
11689 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011690 todo = d->dv_hashtab.ht_used;
11691 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011692 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011693 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011694 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011695 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011696 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011697 if (first)
11698 {
11699 n = i;
11700 first = FALSE;
11701 }
11702 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011703 n = i;
11704 }
11705 }
11706 }
11707 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011708 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011709 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011710 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011711}
11712
11713/*
11714 * "max()" function
11715 */
11716 static void
11717f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011718 typval_T *argvars;
11719 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011720{
11721 max_min(argvars, rettv, TRUE);
11722}
11723
11724/*
11725 * "min()" function
11726 */
11727 static void
11728f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011729 typval_T *argvars;
11730 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011731{
11732 max_min(argvars, rettv, FALSE);
11733}
11734
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011735static int mkdir_recurse __ARGS((char_u *dir, int prot));
11736
11737/*
11738 * Create the directory in which "dir" is located, and higher levels when
11739 * needed.
11740 */
11741 static int
11742mkdir_recurse(dir, prot)
11743 char_u *dir;
11744 int prot;
11745{
11746 char_u *p;
11747 char_u *updir;
11748 int r = FAIL;
11749
11750 /* Get end of directory name in "dir".
11751 * We're done when it's "/" or "c:/". */
11752 p = gettail_sep(dir);
11753 if (p <= get_past_head(dir))
11754 return OK;
11755
11756 /* If the directory exists we're done. Otherwise: create it.*/
11757 updir = vim_strnsave(dir, (int)(p - dir));
11758 if (updir == NULL)
11759 return FAIL;
11760 if (mch_isdir(updir))
11761 r = OK;
11762 else if (mkdir_recurse(updir, prot) == OK)
11763 r = vim_mkdir_emsg(updir, prot);
11764 vim_free(updir);
11765 return r;
11766}
11767
11768#ifdef vim_mkdir
11769/*
11770 * "mkdir()" function
11771 */
11772 static void
11773f_mkdir(argvars, rettv)
11774 typval_T *argvars;
11775 typval_T *rettv;
11776{
11777 char_u *dir;
11778 char_u buf[NUMBUFLEN];
11779 int prot = 0755;
11780
11781 rettv->vval.v_number = FAIL;
11782 if (check_restricted() || check_secure())
11783 return;
11784
11785 dir = get_tv_string_buf(&argvars[0], buf);
11786 if (argvars[1].v_type != VAR_UNKNOWN)
11787 {
11788 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011789 prot = get_tv_number_chk(&argvars[2], NULL);
11790 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011791 mkdir_recurse(dir, prot);
11792 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011793 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011794}
11795#endif
11796
Bram Moolenaar0d660222005-01-07 21:51:51 +000011797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798 * "mode()" function
11799 */
11800/*ARGSUSED*/
11801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011803 typval_T *argvars;
11804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805{
11806 char_u buf[2];
11807
11808#ifdef FEAT_VISUAL
11809 if (VIsual_active)
11810 {
11811 if (VIsual_select)
11812 buf[0] = VIsual_mode + 's' - 'v';
11813 else
11814 buf[0] = VIsual_mode;
11815 }
11816 else
11817#endif
11818 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11819 buf[0] = 'r';
11820 else if (State & INSERT)
11821 {
11822 if (State & REPLACE_FLAG)
11823 buf[0] = 'R';
11824 else
11825 buf[0] = 'i';
11826 }
11827 else if (State & CMDLINE)
11828 buf[0] = 'c';
11829 else
11830 buf[0] = 'n';
11831
11832 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011833 rettv->vval.v_string = vim_strsave(buf);
11834 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835}
11836
11837/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011838 * "nextnonblank()" function
11839 */
11840 static void
11841f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011842 typval_T *argvars;
11843 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011844{
11845 linenr_T lnum;
11846
11847 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011849 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011850 {
11851 lnum = 0;
11852 break;
11853 }
11854 if (*skipwhite(ml_get(lnum)) != NUL)
11855 break;
11856 }
11857 rettv->vval.v_number = lnum;
11858}
11859
11860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861 * "nr2char()" function
11862 */
11863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011864f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011865 typval_T *argvars;
11866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867{
11868 char_u buf[NUMBUFLEN];
11869
11870#ifdef FEAT_MBYTE
11871 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011872 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 else
11874#endif
11875 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011876 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 buf[1] = NUL;
11878 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011879 rettv->v_type = VAR_STRING;
11880 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011881}
11882
11883/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011884 * "prevnonblank()" function
11885 */
11886 static void
11887f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011888 typval_T *argvars;
11889 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011890{
11891 linenr_T lnum;
11892
11893 lnum = get_tv_lnum(argvars);
11894 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11895 lnum = 0;
11896 else
11897 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11898 --lnum;
11899 rettv->vval.v_number = lnum;
11900}
11901
Bram Moolenaar8c711452005-01-14 21:53:12 +000011902/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000011903 * "printf()" function
11904 */
11905 static void
11906f_printf(argvars, rettv)
11907 typval_T *argvars;
11908 typval_T *rettv;
11909{
11910 rettv->v_type = VAR_STRING;
11911 rettv->vval.v_string = NULL;
11912#ifdef HAVE_STDARG_H
11913 {
11914 char_u buf[NUMBUFLEN];
11915 int len;
11916 char_u *s;
11917 int saved_did_emsg = did_emsg;
11918 char *fmt;
11919
11920 /* Get the required length, allocate the buffer and do it for real. */
11921 did_emsg = FALSE;
11922 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
11923 len = vim_vsnprintf(NULL, 0, fmt, NULL, argvars + 1);
11924 if (!did_emsg)
11925 {
11926 s = alloc(len + 1);
11927 if (s != NULL)
11928 {
11929 rettv->vval.v_string = s;
11930 (void)vim_vsnprintf((char *)s, len + 1, fmt, NULL, argvars + 1);
11931 }
11932 }
11933 did_emsg |= saved_did_emsg;
11934 }
11935#endif
11936}
11937
11938/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011939 * "range()" function
11940 */
11941 static void
11942f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011943 typval_T *argvars;
11944 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011945{
11946 long start;
11947 long end;
11948 long stride = 1;
11949 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011950 list_T *l;
11951 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011952 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011953
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011954 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011955 if (argvars[1].v_type == VAR_UNKNOWN)
11956 {
11957 end = start - 1;
11958 start = 0;
11959 }
11960 else
11961 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011962 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011963 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011964 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011965 }
11966
11967 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011968 if (error)
11969 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000011970 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011971 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000011972 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011973 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011974 else
11975 {
11976 l = list_alloc();
11977 if (l != NULL)
11978 {
11979 rettv->v_type = VAR_LIST;
11980 rettv->vval.v_list = l;
11981 ++l->lv_refcount;
11982
11983 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11984 {
11985 li = listitem_alloc();
11986 if (li == NULL)
11987 break;
11988 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011989 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011990 li->li_tv.vval.v_number = i;
11991 list_append(l, li);
11992 }
11993 }
11994 }
11995}
11996
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011997/*
11998 * "readfile()" function
11999 */
12000 static void
12001f_readfile(argvars, rettv)
12002 typval_T *argvars;
12003 typval_T *rettv;
12004{
12005 int binary = FALSE;
12006 char_u *fname;
12007 FILE *fd;
12008 list_T *l;
12009 listitem_T *li;
12010#define FREAD_SIZE 200 /* optimized for text lines */
12011 char_u buf[FREAD_SIZE];
12012 int readlen; /* size of last fread() */
12013 int buflen; /* nr of valid chars in buf[] */
12014 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12015 int tolist; /* first byte in buf[] still to be put in list */
12016 int chop; /* how many CR to chop off */
12017 char_u *prev = NULL; /* previously read bytes, if any */
12018 int prevlen = 0; /* length of "prev" if not NULL */
12019 char_u *s;
12020 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012021 long maxline = MAXLNUM;
12022 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012023
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012024 if (argvars[1].v_type != VAR_UNKNOWN)
12025 {
12026 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12027 binary = TRUE;
12028 if (argvars[2].v_type != VAR_UNKNOWN)
12029 maxline = get_tv_number(&argvars[2]);
12030 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012031
12032 l = list_alloc();
12033 if (l == NULL)
12034 return;
12035 rettv->v_type = VAR_LIST;
12036 rettv->vval.v_list = l;
12037 l->lv_refcount = 1;
12038
12039 /* Always open the file in binary mode, library functions have a mind of
12040 * their own about CR-LF conversion. */
12041 fname = get_tv_string(&argvars[0]);
12042 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12043 {
12044 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12045 return;
12046 }
12047
12048 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012049 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012050 {
12051 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12052 buflen = filtd + readlen;
12053 tolist = 0;
12054 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12055 {
12056 if (buf[filtd] == '\n' || readlen <= 0)
12057 {
12058 /* Only when in binary mode add an empty list item when the
12059 * last line ends in a '\n'. */
12060 if (!binary && readlen == 0 && filtd == 0)
12061 break;
12062
12063 /* Found end-of-line or end-of-file: add a text line to the
12064 * list. */
12065 chop = 0;
12066 if (!binary)
12067 while (filtd - chop - 1 >= tolist
12068 && buf[filtd - chop - 1] == '\r')
12069 ++chop;
12070 len = filtd - tolist - chop;
12071 if (prev == NULL)
12072 s = vim_strnsave(buf + tolist, len);
12073 else
12074 {
12075 s = alloc((unsigned)(prevlen + len + 1));
12076 if (s != NULL)
12077 {
12078 mch_memmove(s, prev, prevlen);
12079 vim_free(prev);
12080 prev = NULL;
12081 mch_memmove(s + prevlen, buf + tolist, len);
12082 s[prevlen + len] = NUL;
12083 }
12084 }
12085 tolist = filtd + 1;
12086
12087 li = listitem_alloc();
12088 if (li == NULL)
12089 {
12090 vim_free(s);
12091 break;
12092 }
12093 li->li_tv.v_type = VAR_STRING;
12094 li->li_tv.v_lock = 0;
12095 li->li_tv.vval.v_string = s;
12096 list_append(l, li);
12097
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012098 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012099 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012100 if (readlen <= 0)
12101 break;
12102 }
12103 else if (buf[filtd] == NUL)
12104 buf[filtd] = '\n';
12105 }
12106 if (readlen <= 0)
12107 break;
12108
12109 if (tolist == 0)
12110 {
12111 /* "buf" is full, need to move text to an allocated buffer */
12112 if (prev == NULL)
12113 {
12114 prev = vim_strnsave(buf, buflen);
12115 prevlen = buflen;
12116 }
12117 else
12118 {
12119 s = alloc((unsigned)(prevlen + buflen));
12120 if (s != NULL)
12121 {
12122 mch_memmove(s, prev, prevlen);
12123 mch_memmove(s + prevlen, buf, buflen);
12124 vim_free(prev);
12125 prev = s;
12126 prevlen += buflen;
12127 }
12128 }
12129 filtd = 0;
12130 }
12131 else
12132 {
12133 mch_memmove(buf, buf + tolist, buflen - tolist);
12134 filtd -= tolist;
12135 }
12136 }
12137
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012138 /*
12139 * For a negative line count use only the lines at the end of the file,
12140 * free the rest.
12141 */
12142 if (maxline < 0)
12143 while (cnt > -maxline)
12144 {
12145 listitem_remove(l, l->lv_first);
12146 --cnt;
12147 }
12148
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012149 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012150 fclose(fd);
12151}
12152
12153
Bram Moolenaar0d660222005-01-07 21:51:51 +000012154#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12155static void make_connection __ARGS((void));
12156static int check_connection __ARGS((void));
12157
12158 static void
12159make_connection()
12160{
12161 if (X_DISPLAY == NULL
12162# ifdef FEAT_GUI
12163 && !gui.in_use
12164# endif
12165 )
12166 {
12167 x_force_connect = TRUE;
12168 setup_term_clip();
12169 x_force_connect = FALSE;
12170 }
12171}
12172
12173 static int
12174check_connection()
12175{
12176 make_connection();
12177 if (X_DISPLAY == NULL)
12178 {
12179 EMSG(_("E240: No connection to Vim server"));
12180 return FAIL;
12181 }
12182 return OK;
12183}
12184#endif
12185
12186#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012187static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012188
12189 static void
12190remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012191 typval_T *argvars;
12192 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012193 int expr;
12194{
12195 char_u *server_name;
12196 char_u *keys;
12197 char_u *r = NULL;
12198 char_u buf[NUMBUFLEN];
12199# ifdef WIN32
12200 HWND w;
12201# else
12202 Window w;
12203# endif
12204
12205 if (check_restricted() || check_secure())
12206 return;
12207
12208# ifdef FEAT_X11
12209 if (check_connection() == FAIL)
12210 return;
12211# endif
12212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012213 server_name = get_tv_string_chk(&argvars[0]);
12214 if (server_name == NULL)
12215 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012216 keys = get_tv_string_buf(&argvars[1], buf);
12217# ifdef WIN32
12218 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12219# else
12220 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12221 < 0)
12222# endif
12223 {
12224 if (r != NULL)
12225 EMSG(r); /* sending worked but evaluation failed */
12226 else
12227 EMSG2(_("E241: Unable to send to %s"), server_name);
12228 return;
12229 }
12230
12231 rettv->vval.v_string = r;
12232
12233 if (argvars[2].v_type != VAR_UNKNOWN)
12234 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012235 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012236 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012237 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012238
12239 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012240 v.di_tv.v_type = VAR_STRING;
12241 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012242 idvar = get_tv_string_chk(&argvars[2]);
12243 if (idvar != NULL)
12244 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012245 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012246 }
12247}
12248#endif
12249
12250/*
12251 * "remote_expr()" function
12252 */
12253/*ARGSUSED*/
12254 static void
12255f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012256 typval_T *argvars;
12257 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012258{
12259 rettv->v_type = VAR_STRING;
12260 rettv->vval.v_string = NULL;
12261#ifdef FEAT_CLIENTSERVER
12262 remote_common(argvars, rettv, TRUE);
12263#endif
12264}
12265
12266/*
12267 * "remote_foreground()" function
12268 */
12269/*ARGSUSED*/
12270 static void
12271f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012272 typval_T *argvars;
12273 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012274{
12275 rettv->vval.v_number = 0;
12276#ifdef FEAT_CLIENTSERVER
12277# ifdef WIN32
12278 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012279 {
12280 char_u *server_name = get_tv_string_chk(&argvars[0]);
12281
12282 if (server_name != NULL)
12283 serverForeground(server_name);
12284 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012285# else
12286 /* Send a foreground() expression to the server. */
12287 argvars[1].v_type = VAR_STRING;
12288 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12289 argvars[2].v_type = VAR_UNKNOWN;
12290 remote_common(argvars, rettv, TRUE);
12291 vim_free(argvars[1].vval.v_string);
12292# endif
12293#endif
12294}
12295
12296/*ARGSUSED*/
12297 static void
12298f_remote_peek(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#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012303 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012304 char_u *s = NULL;
12305# ifdef WIN32
12306 int n = 0;
12307# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012308 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012309
12310 if (check_restricted() || check_secure())
12311 {
12312 rettv->vval.v_number = -1;
12313 return;
12314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012315 serverid = get_tv_string_chk(&argvars[0]);
12316 if (serverid == NULL)
12317 {
12318 rettv->vval.v_number = -1;
12319 return; /* type error; errmsg already given */
12320 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012321# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012322 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012323 if (n == 0)
12324 rettv->vval.v_number = -1;
12325 else
12326 {
12327 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12328 rettv->vval.v_number = (s != NULL);
12329 }
12330# else
12331 rettv->vval.v_number = 0;
12332 if (check_connection() == FAIL)
12333 return;
12334
12335 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012336 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012337# endif
12338
12339 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012341 char_u *retvar;
12342
Bram Moolenaar33570922005-01-25 22:26:29 +000012343 v.di_tv.v_type = VAR_STRING;
12344 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012345 retvar = get_tv_string_chk(&argvars[1]);
12346 if (retvar != NULL)
12347 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012348 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012349 }
12350#else
12351 rettv->vval.v_number = -1;
12352#endif
12353}
12354
12355/*ARGSUSED*/
12356 static void
12357f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012358 typval_T *argvars;
12359 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012360{
12361 char_u *r = NULL;
12362
12363#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012364 char_u *serverid = get_tv_string_chk(&argvars[0]);
12365
12366 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012367 {
12368# ifdef WIN32
12369 /* The server's HWND is encoded in the 'id' parameter */
12370 int n = 0;
12371
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012372 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012373 if (n != 0)
12374 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12375 if (r == NULL)
12376# else
12377 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012378 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012379# endif
12380 EMSG(_("E277: Unable to read a server reply"));
12381 }
12382#endif
12383 rettv->v_type = VAR_STRING;
12384 rettv->vval.v_string = r;
12385}
12386
12387/*
12388 * "remote_send()" function
12389 */
12390/*ARGSUSED*/
12391 static void
12392f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012393 typval_T *argvars;
12394 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012395{
12396 rettv->v_type = VAR_STRING;
12397 rettv->vval.v_string = NULL;
12398#ifdef FEAT_CLIENTSERVER
12399 remote_common(argvars, rettv, FALSE);
12400#endif
12401}
12402
12403/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012404 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012405 */
12406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012407f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012408 typval_T *argvars;
12409 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012410{
Bram Moolenaar33570922005-01-25 22:26:29 +000012411 list_T *l;
12412 listitem_T *item, *item2;
12413 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012414 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012415 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012416 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012417 dict_T *d;
12418 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012419
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012420 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012421 if (argvars[0].v_type == VAR_DICT)
12422 {
12423 if (argvars[2].v_type != VAR_UNKNOWN)
12424 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012425 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012426 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012428 key = get_tv_string_chk(&argvars[1]);
12429 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012431 di = dict_find(d, key, -1);
12432 if (di == NULL)
12433 EMSG2(_(e_dictkey), key);
12434 else
12435 {
12436 *rettv = di->di_tv;
12437 init_tv(&di->di_tv);
12438 dictitem_remove(d, di);
12439 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012440 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012441 }
12442 }
12443 else if (argvars[0].v_type != VAR_LIST)
12444 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012445 else if ((l = argvars[0].vval.v_list) != NULL
12446 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012448 int error = FALSE;
12449
12450 idx = get_tv_number_chk(&argvars[1], &error);
12451 if (error)
12452 ; /* type error: do nothing, errmsg already given */
12453 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012454 EMSGN(_(e_listidx), idx);
12455 else
12456 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012457 if (argvars[2].v_type == VAR_UNKNOWN)
12458 {
12459 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012460 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012461 *rettv = item->li_tv;
12462 vim_free(item);
12463 }
12464 else
12465 {
12466 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012467 end = get_tv_number_chk(&argvars[2], &error);
12468 if (error)
12469 ; /* type error: do nothing */
12470 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012471 EMSGN(_(e_listidx), end);
12472 else
12473 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012474 int cnt = 0;
12475
12476 for (li = item; li != NULL; li = li->li_next)
12477 {
12478 ++cnt;
12479 if (li == item2)
12480 break;
12481 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012482 if (li == NULL) /* didn't find "item2" after "item" */
12483 EMSG(_(e_invrange));
12484 else
12485 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012486 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012487 l = list_alloc();
12488 if (l != NULL)
12489 {
12490 rettv->v_type = VAR_LIST;
12491 rettv->vval.v_list = l;
12492 l->lv_first = item;
12493 l->lv_last = item2;
12494 l->lv_refcount = 1;
12495 item->li_prev = NULL;
12496 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012497 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012498 }
12499 }
12500 }
12501 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012502 }
12503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504}
12505
12506/*
12507 * "rename({from}, {to})" function
12508 */
12509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012510f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012511 typval_T *argvars;
12512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513{
12514 char_u buf[NUMBUFLEN];
12515
12516 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012517 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012519 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12520 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012521}
12522
12523/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012524 * "repeat()" function
12525 */
12526/*ARGSUSED*/
12527 static void
12528f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012529 typval_T *argvars;
12530 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012531{
12532 char_u *p;
12533 int n;
12534 int slen;
12535 int len;
12536 char_u *r;
12537 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012538 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012539
12540 n = get_tv_number(&argvars[1]);
12541 if (argvars[0].v_type == VAR_LIST)
12542 {
12543 l = list_alloc();
12544 if (l != NULL && argvars[0].vval.v_list != NULL)
12545 {
12546 l->lv_refcount = 1;
12547 while (n-- > 0)
12548 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12549 break;
12550 }
12551 rettv->v_type = VAR_LIST;
12552 rettv->vval.v_list = l;
12553 }
12554 else
12555 {
12556 p = get_tv_string(&argvars[0]);
12557 rettv->v_type = VAR_STRING;
12558 rettv->vval.v_string = NULL;
12559
12560 slen = (int)STRLEN(p);
12561 len = slen * n;
12562 if (len <= 0)
12563 return;
12564
12565 r = alloc(len + 1);
12566 if (r != NULL)
12567 {
12568 for (i = 0; i < n; i++)
12569 mch_memmove(r + i * slen, p, (size_t)slen);
12570 r[len] = NUL;
12571 }
12572
12573 rettv->vval.v_string = r;
12574 }
12575}
12576
12577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012578 * "resolve()" function
12579 */
12580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012581f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012582 typval_T *argvars;
12583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584{
12585 char_u *p;
12586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012587 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588#ifdef FEAT_SHORTCUT
12589 {
12590 char_u *v = NULL;
12591
12592 v = mch_resolve_shortcut(p);
12593 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012594 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012596 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597 }
12598#else
12599# ifdef HAVE_READLINK
12600 {
12601 char_u buf[MAXPATHL + 1];
12602 char_u *cpy;
12603 int len;
12604 char_u *remain = NULL;
12605 char_u *q;
12606 int is_relative_to_current = FALSE;
12607 int has_trailing_pathsep = FALSE;
12608 int limit = 100;
12609
12610 p = vim_strsave(p);
12611
12612 if (p[0] == '.' && (vim_ispathsep(p[1])
12613 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12614 is_relative_to_current = TRUE;
12615
12616 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012617 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618 has_trailing_pathsep = TRUE;
12619
12620 q = getnextcomp(p);
12621 if (*q != NUL)
12622 {
12623 /* Separate the first path component in "p", and keep the
12624 * remainder (beginning with the path separator). */
12625 remain = vim_strsave(q - 1);
12626 q[-1] = NUL;
12627 }
12628
12629 for (;;)
12630 {
12631 for (;;)
12632 {
12633 len = readlink((char *)p, (char *)buf, MAXPATHL);
12634 if (len <= 0)
12635 break;
12636 buf[len] = NUL;
12637
12638 if (limit-- == 0)
12639 {
12640 vim_free(p);
12641 vim_free(remain);
12642 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644 goto fail;
12645 }
12646
12647 /* Ensure that the result will have a trailing path separator
12648 * if the argument has one. */
12649 if (remain == NULL && has_trailing_pathsep)
12650 add_pathsep(buf);
12651
12652 /* Separate the first path component in the link value and
12653 * concatenate the remainders. */
12654 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12655 if (*q != NUL)
12656 {
12657 if (remain == NULL)
12658 remain = vim_strsave(q - 1);
12659 else
12660 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012661 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662 if (cpy != NULL)
12663 {
12664 STRCAT(cpy, remain);
12665 vim_free(remain);
12666 remain = cpy;
12667 }
12668 }
12669 q[-1] = NUL;
12670 }
12671
12672 q = gettail(p);
12673 if (q > p && *q == NUL)
12674 {
12675 /* Ignore trailing path separator. */
12676 q[-1] = NUL;
12677 q = gettail(p);
12678 }
12679 if (q > p && !mch_isFullName(buf))
12680 {
12681 /* symlink is relative to directory of argument */
12682 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12683 if (cpy != NULL)
12684 {
12685 STRCPY(cpy, p);
12686 STRCPY(gettail(cpy), buf);
12687 vim_free(p);
12688 p = cpy;
12689 }
12690 }
12691 else
12692 {
12693 vim_free(p);
12694 p = vim_strsave(buf);
12695 }
12696 }
12697
12698 if (remain == NULL)
12699 break;
12700
12701 /* Append the first path component of "remain" to "p". */
12702 q = getnextcomp(remain + 1);
12703 len = q - remain - (*q != NUL);
12704 cpy = vim_strnsave(p, STRLEN(p) + len);
12705 if (cpy != NULL)
12706 {
12707 STRNCAT(cpy, remain, len);
12708 vim_free(p);
12709 p = cpy;
12710 }
12711 /* Shorten "remain". */
12712 if (*q != NUL)
12713 STRCPY(remain, q - 1);
12714 else
12715 {
12716 vim_free(remain);
12717 remain = NULL;
12718 }
12719 }
12720
12721 /* If the result is a relative path name, make it explicitly relative to
12722 * the current directory if and only if the argument had this form. */
12723 if (!vim_ispathsep(*p))
12724 {
12725 if (is_relative_to_current
12726 && *p != NUL
12727 && !(p[0] == '.'
12728 && (p[1] == NUL
12729 || vim_ispathsep(p[1])
12730 || (p[1] == '.'
12731 && (p[2] == NUL
12732 || vim_ispathsep(p[2]))))))
12733 {
12734 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012735 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 if (cpy != NULL)
12737 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738 vim_free(p);
12739 p = cpy;
12740 }
12741 }
12742 else if (!is_relative_to_current)
12743 {
12744 /* Strip leading "./". */
12745 q = p;
12746 while (q[0] == '.' && vim_ispathsep(q[1]))
12747 q += 2;
12748 if (q > p)
12749 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12750 }
12751 }
12752
12753 /* Ensure that the result will have no trailing path separator
12754 * if the argument had none. But keep "/" or "//". */
12755 if (!has_trailing_pathsep)
12756 {
12757 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012758 if (after_pathsep(p, q))
12759 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 }
12761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763 }
12764# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012765 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766# endif
12767#endif
12768
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012769 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770
12771#ifdef HAVE_READLINK
12772fail:
12773#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775}
12776
12777/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012778 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779 */
12780 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012781f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012782 typval_T *argvars;
12783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784{
Bram Moolenaar33570922005-01-25 22:26:29 +000012785 list_T *l;
12786 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787
Bram Moolenaar0d660222005-01-07 21:51:51 +000012788 rettv->vval.v_number = 0;
12789 if (argvars[0].v_type != VAR_LIST)
12790 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012791 else if ((l = argvars[0].vval.v_list) != NULL
12792 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012793 {
12794 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012795 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012796 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012797 while (li != NULL)
12798 {
12799 ni = li->li_prev;
12800 list_append(l, li);
12801 li = ni;
12802 }
12803 rettv->vval.v_list = l;
12804 rettv->v_type = VAR_LIST;
12805 ++l->lv_refcount;
12806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807}
12808
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012809#define SP_NOMOVE 1 /* don't move cursor */
12810#define SP_REPEAT 2 /* repeat to find outer pair */
12811#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000012812#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012813
Bram Moolenaar33570922005-01-25 22:26:29 +000012814static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012815
12816/*
12817 * Get flags for a search function.
12818 * Possibly sets "p_ws".
12819 * Returns BACKWARD, FORWARD or zero (for an error).
12820 */
12821 static int
12822get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012823 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012824 int *flagsp;
12825{
12826 int dir = FORWARD;
12827 char_u *flags;
12828 char_u nbuf[NUMBUFLEN];
12829 int mask;
12830
12831 if (varp->v_type != VAR_UNKNOWN)
12832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012833 flags = get_tv_string_buf_chk(varp, nbuf);
12834 if (flags == NULL)
12835 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012836 while (*flags != NUL)
12837 {
12838 switch (*flags)
12839 {
12840 case 'b': dir = BACKWARD; break;
12841 case 'w': p_ws = TRUE; break;
12842 case 'W': p_ws = FALSE; break;
12843 default: mask = 0;
12844 if (flagsp != NULL)
12845 switch (*flags)
12846 {
12847 case 'n': mask = SP_NOMOVE; break;
12848 case 'r': mask = SP_REPEAT; break;
12849 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012850 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012851 }
12852 if (mask == 0)
12853 {
12854 EMSG2(_(e_invarg2), flags);
12855 dir = 0;
12856 }
12857 else
12858 *flagsp |= mask;
12859 }
12860 if (dir == 0)
12861 break;
12862 ++flags;
12863 }
12864 }
12865 return dir;
12866}
12867
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868/*
12869 * "search()" function
12870 */
12871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012872f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012873 typval_T *argvars;
12874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875{
12876 char_u *pat;
12877 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012878 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879 int save_p_ws = p_ws;
12880 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012881 int flags = 0;
12882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012886 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12887 if (dir == 0)
12888 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012889 /*
12890 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
12891 * Check to make sure only those flags are set.
12892 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
12893 * flags cannot be set. Check for that condition also.
12894 */
12895 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
12896 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012898 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012899 goto theend;
12900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012902 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12904 SEARCH_KEEP, RE_SEARCH) != FAIL)
12905 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012906 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012907 if (flags & SP_SETPCMARK)
12908 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909 curwin->w_cursor = pos;
12910 /* "/$" will put the cursor after the end of the line, may need to
12911 * correct that here */
12912 check_cursor();
12913 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012914
12915 /* If 'n' flag is used: restore cursor position. */
12916 if (flags & SP_NOMOVE)
12917 curwin->w_cursor = save_cursor;
12918theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919 p_ws = save_p_ws;
12920}
12921
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922/*
12923 * "searchpair()" function
12924 */
12925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012926f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012927 typval_T *argvars;
12928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929{
12930 char_u *spat, *mpat, *epat;
12931 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933 int dir;
12934 int flags = 0;
12935 char_u nbuf1[NUMBUFLEN];
12936 char_u nbuf2[NUMBUFLEN];
12937 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012942 spat = get_tv_string_chk(&argvars[0]);
12943 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
12944 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
12945 if (spat == NULL || mpat == NULL || epat == NULL)
12946 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948 /* Handle the optional fourth argument: flags */
12949 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012950 if (dir == 0)
12951 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012952 /*
12953 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
12954 */
12955 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
12956 {
12957 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
12958 goto theend;
12959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960
12961 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012962 if (argvars[3].v_type == VAR_UNKNOWN
12963 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964 skip = (char_u *)"";
12965 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012966 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
12967 if (skip == NULL)
12968 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012970 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
12971
12972theend:
12973 p_ws = save_p_ws;
12974}
12975
12976/*
12977 * Search for a start/middle/end thing.
12978 * Used by searchpair(), see its documentation for the details.
12979 * Returns 0 or -1 for no match,
12980 */
12981 long
12982do_searchpair(spat, mpat, epat, dir, skip, flags)
12983 char_u *spat; /* start pattern */
12984 char_u *mpat; /* middle pattern */
12985 char_u *epat; /* end pattern */
12986 int dir; /* BACKWARD or FORWARD */
12987 char_u *skip; /* skip expression */
12988 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
12989{
12990 char_u *save_cpo;
12991 char_u *pat, *pat2 = NULL, *pat3 = NULL;
12992 long retval = 0;
12993 pos_T pos;
12994 pos_T firstpos;
12995 pos_T foundpos;
12996 pos_T save_cursor;
12997 pos_T save_pos;
12998 int n;
12999 int r;
13000 int nest = 1;
13001 int err;
13002
13003 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13004 save_cpo = p_cpo;
13005 p_cpo = (char_u *)"";
13006
13007 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13008 * start/middle/end (pat3, for the top pair). */
13009 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13010 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13011 if (pat2 == NULL || pat3 == NULL)
13012 goto theend;
13013 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13014 if (*mpat == NUL)
13015 STRCPY(pat3, pat2);
13016 else
13017 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13018 spat, epat, mpat);
13019
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020 save_cursor = curwin->w_cursor;
13021 pos = curwin->w_cursor;
13022 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013023 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024 pat = pat3;
13025 for (;;)
13026 {
13027 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13028 SEARCH_KEEP, RE_SEARCH);
13029 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13030 /* didn't find it or found the first match again: FAIL */
13031 break;
13032
13033 if (firstpos.lnum == 0)
13034 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013035 if (equalpos(pos, foundpos))
13036 {
13037 /* Found the same position again. Can happen with a pattern that
13038 * has "\zs" at the end and searching backwards. Advance one
13039 * character and try again. */
13040 if (dir == BACKWARD)
13041 decl(&pos);
13042 else
13043 incl(&pos);
13044 }
13045 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046
13047 /* If the skip pattern matches, ignore this match. */
13048 if (*skip != NUL)
13049 {
13050 save_pos = curwin->w_cursor;
13051 curwin->w_cursor = pos;
13052 r = eval_to_bool(skip, &err, NULL, FALSE);
13053 curwin->w_cursor = save_pos;
13054 if (err)
13055 {
13056 /* Evaluating {skip} caused an error, break here. */
13057 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013058 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059 break;
13060 }
13061 if (r)
13062 continue;
13063 }
13064
13065 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13066 {
13067 /* Found end when searching backwards or start when searching
13068 * forward: nested pair. */
13069 ++nest;
13070 pat = pat2; /* nested, don't search for middle */
13071 }
13072 else
13073 {
13074 /* Found end when searching forward or start when searching
13075 * backward: end of (nested) pair; or found middle in outer pair. */
13076 if (--nest == 1)
13077 pat = pat3; /* outer level, search for middle */
13078 }
13079
13080 if (nest == 0)
13081 {
13082 /* Found the match: return matchcount or line number. */
13083 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013084 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013086 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013087 if (flags & SP_SETPCMARK)
13088 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089 curwin->w_cursor = pos;
13090 if (!(flags & SP_REPEAT))
13091 break;
13092 nest = 1; /* search for next unmatched */
13093 }
13094 }
13095
13096 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013097 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013098 curwin->w_cursor = save_cursor;
13099
13100theend:
13101 vim_free(pat2);
13102 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013104
13105 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106}
13107
Bram Moolenaar0d660222005-01-07 21:51:51 +000013108/*ARGSUSED*/
13109 static void
13110f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013111 typval_T *argvars;
13112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013114#ifdef FEAT_CLIENTSERVER
13115 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013116 char_u *server = get_tv_string_chk(&argvars[0]);
13117 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118
Bram Moolenaar0d660222005-01-07 21:51:51 +000013119 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013120 if (server == NULL || reply == NULL)
13121 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013122 if (check_restricted() || check_secure())
13123 return;
13124# ifdef FEAT_X11
13125 if (check_connection() == FAIL)
13126 return;
13127# endif
13128
13129 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013131 EMSG(_("E258: Unable to send to client"));
13132 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013134 rettv->vval.v_number = 0;
13135#else
13136 rettv->vval.v_number = -1;
13137#endif
13138}
13139
13140/*ARGSUSED*/
13141 static void
13142f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013143 typval_T *argvars;
13144 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013145{
13146 char_u *r = NULL;
13147
13148#ifdef FEAT_CLIENTSERVER
13149# ifdef WIN32
13150 r = serverGetVimNames();
13151# else
13152 make_connection();
13153 if (X_DISPLAY != NULL)
13154 r = serverGetVimNames(X_DISPLAY);
13155# endif
13156#endif
13157 rettv->v_type = VAR_STRING;
13158 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159}
13160
13161/*
13162 * "setbufvar()" function
13163 */
13164/*ARGSUSED*/
13165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013166f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013167 typval_T *argvars;
13168 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169{
13170 buf_T *buf;
13171#ifdef FEAT_AUTOCMD
13172 aco_save_T aco;
13173#else
13174 buf_T *save_curbuf;
13175#endif
13176 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013177 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178 char_u nbuf[NUMBUFLEN];
13179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013180 rettv->vval.v_number = 0;
13181
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182 if (check_restricted() || check_secure())
13183 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013184 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13185 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013186 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187 varp = &argvars[2];
13188
13189 if (buf != NULL && varname != NULL && varp != NULL)
13190 {
13191 /* set curbuf to be our buf, temporarily */
13192#ifdef FEAT_AUTOCMD
13193 aucmd_prepbuf(&aco, buf);
13194#else
13195 save_curbuf = curbuf;
13196 curbuf = buf;
13197#endif
13198
13199 if (*varname == '&')
13200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013201 long numval;
13202 char_u *strval;
13203 int error = FALSE;
13204
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013206 numval = get_tv_number_chk(varp, &error);
13207 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013208 if (!error && strval != NULL)
13209 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210 }
13211 else
13212 {
13213 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13214 if (bufvarname != NULL)
13215 {
13216 STRCPY(bufvarname, "b:");
13217 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013218 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 vim_free(bufvarname);
13220 }
13221 }
13222
13223 /* reset notion of buffer */
13224#ifdef FEAT_AUTOCMD
13225 aucmd_restbuf(&aco);
13226#else
13227 curbuf = save_curbuf;
13228#endif
13229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230}
13231
13232/*
13233 * "setcmdpos()" function
13234 */
13235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013236f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013237 typval_T *argvars;
13238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013240 int pos = (int)get_tv_number(&argvars[0]) - 1;
13241
13242 if (pos >= 0)
13243 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244}
13245
13246/*
13247 * "setline()" function
13248 */
13249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013250f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013251 typval_T *argvars;
13252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253{
13254 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013255 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013256 list_T *l = NULL;
13257 listitem_T *li = NULL;
13258 long added = 0;
13259 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013261 lnum = get_tv_lnum(&argvars[0]);
13262 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013264 l = argvars[1].vval.v_list;
13265 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013267 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013268 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013269
13270 rettv->vval.v_number = 0; /* OK */
13271 for (;;)
13272 {
13273 if (l != NULL)
13274 {
13275 /* list argument, get next string */
13276 if (li == NULL)
13277 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013278 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013279 li = li->li_next;
13280 }
13281
13282 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013283 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013284 break;
13285 if (lnum <= curbuf->b_ml.ml_line_count)
13286 {
13287 /* existing line, replace it */
13288 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13289 {
13290 changed_bytes(lnum, 0);
13291 check_cursor_col();
13292 rettv->vval.v_number = 0; /* OK */
13293 }
13294 }
13295 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13296 {
13297 /* lnum is one past the last line, append the line */
13298 ++added;
13299 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13300 rettv->vval.v_number = 0; /* OK */
13301 }
13302
13303 if (l == NULL) /* only one string argument */
13304 break;
13305 ++lnum;
13306 }
13307
13308 if (added > 0)
13309 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310}
13311
13312/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013313 * "setqflist()" function
13314 */
13315/*ARGSUSED*/
13316 static void
13317f_setqflist(argvars, rettv)
13318 typval_T *argvars;
13319 typval_T *rettv;
13320{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013321 char_u *act;
13322 int action = ' ';
13323
Bram Moolenaar2641f772005-03-25 21:58:17 +000013324 rettv->vval.v_number = -1;
13325
13326#ifdef FEAT_QUICKFIX
13327 if (argvars[0].v_type != VAR_LIST)
13328 EMSG(_(e_listreq));
13329 else
13330 {
13331 list_T *l = argvars[0].vval.v_list;
13332
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013333 if (argvars[1].v_type == VAR_STRING)
13334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013335 act = get_tv_string_chk(&argvars[1]);
13336 if (act == NULL)
13337 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013338 if (*act == 'a' || *act == 'r')
13339 action = *act;
13340 }
13341
13342 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013343 rettv->vval.v_number = 0;
13344 }
13345#endif
13346}
13347
13348/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349 * "setreg()" function
13350 */
13351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013352f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013353 typval_T *argvars;
13354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355{
13356 int regname;
13357 char_u *strregname;
13358 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013359 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360 int append;
13361 char_u yank_type;
13362 long block_len;
13363
13364 block_len = -1;
13365 yank_type = MAUTO;
13366 append = FALSE;
13367
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013368 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013369 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013371 if (strregname == NULL)
13372 return; /* type error; errmsg already given */
13373 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374 if (regname == 0 || regname == '@')
13375 regname = '"';
13376 else if (regname == '=')
13377 return;
13378
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013379 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013381 stropt = get_tv_string_chk(&argvars[2]);
13382 if (stropt == NULL)
13383 return; /* type error */
13384 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385 switch (*stropt)
13386 {
13387 case 'a': case 'A': /* append */
13388 append = TRUE;
13389 break;
13390 case 'v': case 'c': /* character-wise selection */
13391 yank_type = MCHAR;
13392 break;
13393 case 'V': case 'l': /* line-wise selection */
13394 yank_type = MLINE;
13395 break;
13396#ifdef FEAT_VISUAL
13397 case 'b': case Ctrl_V: /* block-wise selection */
13398 yank_type = MBLOCK;
13399 if (VIM_ISDIGIT(stropt[1]))
13400 {
13401 ++stropt;
13402 block_len = getdigits(&stropt) - 1;
13403 --stropt;
13404 }
13405 break;
13406#endif
13407 }
13408 }
13409
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013410 strval = get_tv_string_chk(&argvars[1]);
13411 if (strval != NULL)
13412 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013414 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415}
13416
13417
13418/*
13419 * "setwinvar(expr)" function
13420 */
13421/*ARGSUSED*/
13422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013423f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013424 typval_T *argvars;
13425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426{
13427 win_T *win;
13428#ifdef FEAT_WINDOWS
13429 win_T *save_curwin;
13430#endif
13431 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013432 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433 char_u nbuf[NUMBUFLEN];
13434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013435 rettv->vval.v_number = 0;
13436
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437 if (check_restricted() || check_secure())
13438 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013440 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441 varp = &argvars[2];
13442
13443 if (win != NULL && varname != NULL && varp != NULL)
13444 {
13445#ifdef FEAT_WINDOWS
13446 /* set curwin to be our win, temporarily */
13447 save_curwin = curwin;
13448 curwin = win;
13449 curbuf = curwin->w_buffer;
13450#endif
13451
13452 if (*varname == '&')
13453 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013454 long numval;
13455 char_u *strval;
13456 int error = FALSE;
13457
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013459 numval = get_tv_number_chk(varp, &error);
13460 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013461 if (!error && strval != NULL)
13462 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463 }
13464 else
13465 {
13466 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13467 if (winvarname != NULL)
13468 {
13469 STRCPY(winvarname, "w:");
13470 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013471 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472 vim_free(winvarname);
13473 }
13474 }
13475
13476#ifdef FEAT_WINDOWS
13477 /* Restore current window, if it's still valid (autocomands can make
13478 * it invalid). */
13479 if (win_valid(save_curwin))
13480 {
13481 curwin = save_curwin;
13482 curbuf = curwin->w_buffer;
13483 }
13484#endif
13485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486}
13487
13488/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013489 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490 */
13491 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013492f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013493 typval_T *argvars;
13494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013496 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497
Bram Moolenaar0d660222005-01-07 21:51:51 +000013498 p = get_tv_string(&argvars[0]);
13499 rettv->vval.v_string = vim_strsave(p);
13500 simplify_filename(rettv->vval.v_string); /* simplify in place */
13501 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502}
13503
Bram Moolenaar0d660222005-01-07 21:51:51 +000013504static int
13505#ifdef __BORLANDC__
13506 _RTLENTRYF
13507#endif
13508 item_compare __ARGS((const void *s1, const void *s2));
13509static int
13510#ifdef __BORLANDC__
13511 _RTLENTRYF
13512#endif
13513 item_compare2 __ARGS((const void *s1, const void *s2));
13514
13515static int item_compare_ic;
13516static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013517static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013518#define ITEM_COMPARE_FAIL 999
13519
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013521 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013523 static int
13524#ifdef __BORLANDC__
13525_RTLENTRYF
13526#endif
13527item_compare(s1, s2)
13528 const void *s1;
13529 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013531 char_u *p1, *p2;
13532 char_u *tofree1, *tofree2;
13533 int res;
13534 char_u numbuf1[NUMBUFLEN];
13535 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536
Bram Moolenaar33570922005-01-25 22:26:29 +000013537 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13538 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013539 if (item_compare_ic)
13540 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013542 res = STRCMP(p1, p2);
13543 vim_free(tofree1);
13544 vim_free(tofree2);
13545 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546}
13547
13548 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013549#ifdef __BORLANDC__
13550_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013552item_compare2(s1, s2)
13553 const void *s1;
13554 const void *s2;
13555{
13556 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013557 typval_T rettv;
13558 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013559 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013561 /* shortcut after failure in previous call; compare all items equal */
13562 if (item_compare_func_err)
13563 return 0;
13564
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013565 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13566 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013567 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13568 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013569
13570 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13571 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013572 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013573 clear_tv(&argv[0]);
13574 clear_tv(&argv[1]);
13575
13576 if (res == FAIL)
13577 res = ITEM_COMPARE_FAIL;
13578 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013579 /* return value has wrong type */
13580 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13581 if (item_compare_func_err)
13582 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013583 clear_tv(&rettv);
13584 return res;
13585}
13586
13587/*
13588 * "sort({list})" function
13589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013591f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013592 typval_T *argvars;
13593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594{
Bram Moolenaar33570922005-01-25 22:26:29 +000013595 list_T *l;
13596 listitem_T *li;
13597 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013598 long len;
13599 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600
Bram Moolenaar0d660222005-01-07 21:51:51 +000013601 rettv->vval.v_number = 0;
13602 if (argvars[0].v_type != VAR_LIST)
13603 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604 else
13605 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013606 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013607 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013608 return;
13609 rettv->vval.v_list = l;
13610 rettv->v_type = VAR_LIST;
13611 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612
Bram Moolenaar0d660222005-01-07 21:51:51 +000013613 len = list_len(l);
13614 if (len <= 1)
13615 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616
Bram Moolenaar0d660222005-01-07 21:51:51 +000013617 item_compare_ic = FALSE;
13618 item_compare_func = NULL;
13619 if (argvars[1].v_type != VAR_UNKNOWN)
13620 {
13621 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013622 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013623 else
13624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013625 int error = FALSE;
13626
13627 i = get_tv_number_chk(&argvars[1], &error);
13628 if (error)
13629 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013630 if (i == 1)
13631 item_compare_ic = TRUE;
13632 else
13633 item_compare_func = get_tv_string(&argvars[1]);
13634 }
13635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636
Bram Moolenaar0d660222005-01-07 21:51:51 +000013637 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013638 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013639 if (ptrs == NULL)
13640 return;
13641 i = 0;
13642 for (li = l->lv_first; li != NULL; li = li->li_next)
13643 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013645 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013646 /* test the compare function */
13647 if (item_compare_func != NULL
13648 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13649 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013650 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013652 {
13653 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013654 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013655 item_compare_func == NULL ? item_compare : item_compare2);
13656
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013657 if (!item_compare_func_err)
13658 {
13659 /* Clear the List and append the items in the sorted order. */
13660 l->lv_first = l->lv_last = NULL;
13661 l->lv_len = 0;
13662 for (i = 0; i < len; ++i)
13663 list_append(l, ptrs[i]);
13664 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013665 }
13666
13667 vim_free(ptrs);
13668 }
13669}
13670
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013671/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013672 * "soundfold({word})" function
13673 */
13674 static void
13675f_soundfold(argvars, rettv)
13676 typval_T *argvars;
13677 typval_T *rettv;
13678{
13679 char_u *s;
13680
13681 rettv->v_type = VAR_STRING;
13682 s = get_tv_string(&argvars[0]);
13683#ifdef FEAT_SYN_HL
13684 rettv->vval.v_string = eval_soundfold(s);
13685#else
13686 rettv->vval.v_string = vim_strsave(s);
13687#endif
13688}
13689
13690/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013691 * "spellbadword()" function
13692 */
13693/* ARGSUSED */
13694 static void
13695f_spellbadword(argvars, rettv)
13696 typval_T *argvars;
13697 typval_T *rettv;
13698{
13699 int attr;
13700 char_u *ptr;
13701 int len;
13702
13703 rettv->vval.v_string = NULL;
13704 rettv->v_type = VAR_STRING;
13705
13706#ifdef FEAT_SYN_HL
13707 /* Find the start of the badly spelled word. */
13708 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
13709 return;
13710
13711 /* Get the length of the word and copy it. */
13712 ptr = ml_get_cursor();
Bram Moolenaar51ac12f2005-07-02 23:21:11 +000013713 len = spell_check(curwin, ptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013714 rettv->vval.v_string = vim_strnsave(ptr, len);
13715#endif
13716}
13717
13718/*
13719 * "spellsuggest()" function
13720 */
13721 static void
13722f_spellsuggest(argvars, rettv)
13723 typval_T *argvars;
13724 typval_T *rettv;
13725{
13726 char_u *str;
13727 int maxcount;
13728 garray_T ga;
13729 list_T *l;
13730 listitem_T *li;
13731 int i;
13732
13733 l = list_alloc();
13734 if (l == NULL)
13735 return;
13736 rettv->v_type = VAR_LIST;
13737 rettv->vval.v_list = l;
13738 ++l->lv_refcount;
13739
13740#ifdef FEAT_SYN_HL
13741 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13742 {
13743 str = get_tv_string(&argvars[0]);
13744 if (argvars[1].v_type != VAR_UNKNOWN)
13745 {
13746 maxcount = get_tv_number(&argvars[1]);
13747 if (maxcount <= 0)
13748 return;
13749 }
13750 else
13751 maxcount = 25;
13752
13753 spell_suggest_list(&ga, str, maxcount);
13754
13755 for (i = 0; i < ga.ga_len; ++i)
13756 {
13757 str = ((char_u **)ga.ga_data)[i];
13758
13759 li = listitem_alloc();
13760 if (li == NULL)
13761 vim_free(str);
13762 else
13763 {
13764 li->li_tv.v_type = VAR_STRING;
13765 li->li_tv.v_lock = 0;
13766 li->li_tv.vval.v_string = str;
13767 list_append(l, li);
13768 }
13769 }
13770 ga_clear(&ga);
13771 }
13772#endif
13773}
13774
Bram Moolenaar0d660222005-01-07 21:51:51 +000013775 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013776f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013777 typval_T *argvars;
13778 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013779{
13780 char_u *str;
13781 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013782 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013783 regmatch_T regmatch;
13784 char_u patbuf[NUMBUFLEN];
13785 char_u *save_cpo;
13786 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013787 listitem_T *ni;
13788 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013789 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013790 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013791 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013792
13793 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13794 save_cpo = p_cpo;
13795 p_cpo = (char_u *)"";
13796
13797 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013798 if (argvars[1].v_type != VAR_UNKNOWN)
13799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013800 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13801 if (pat == NULL)
13802 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013803 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013804 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013805 }
13806 if (pat == NULL || *pat == NUL)
13807 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013808
13809 l = list_alloc();
13810 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013812 rettv->v_type = VAR_LIST;
13813 rettv->vval.v_list = l;
13814 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013815 if (typeerr)
13816 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817
Bram Moolenaar0d660222005-01-07 21:51:51 +000013818 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13819 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013821 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013822 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013823 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013824 if (*str == NUL)
13825 match = FALSE; /* empty item at the end */
13826 else
13827 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013828 if (match)
13829 end = regmatch.startp[0];
13830 else
13831 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013832 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13833 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013834 {
13835 ni = listitem_alloc();
13836 if (ni == NULL)
13837 break;
13838 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013839 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013840 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13841 list_append(l, ni);
13842 }
13843 if (!match)
13844 break;
13845 /* Advance to just after the match. */
13846 if (regmatch.endp[0] > str)
13847 col = 0;
13848 else
13849 {
13850 /* Don't get stuck at the same match. */
13851#ifdef FEAT_MBYTE
13852 col = mb_ptr2len_check(regmatch.endp[0]);
13853#else
13854 col = 1;
13855#endif
13856 }
13857 str = regmatch.endp[0];
13858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859
Bram Moolenaar0d660222005-01-07 21:51:51 +000013860 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862
Bram Moolenaar0d660222005-01-07 21:51:51 +000013863 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864}
13865
13866#ifdef HAVE_STRFTIME
13867/*
13868 * "strftime({format}[, {time}])" function
13869 */
13870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013871f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013872 typval_T *argvars;
13873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874{
13875 char_u result_buf[256];
13876 struct tm *curtime;
13877 time_t seconds;
13878 char_u *p;
13879
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013880 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013882 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013883 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884 seconds = time(NULL);
13885 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013886 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887 curtime = localtime(&seconds);
13888 /* MSVC returns NULL for an invalid value of seconds. */
13889 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013890 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891 else
13892 {
13893# ifdef FEAT_MBYTE
13894 vimconv_T conv;
13895 char_u *enc;
13896
13897 conv.vc_type = CONV_NONE;
13898 enc = enc_locale();
13899 convert_setup(&conv, p_enc, enc);
13900 if (conv.vc_type != CONV_NONE)
13901 p = string_convert(&conv, p, NULL);
13902# endif
13903 if (p != NULL)
13904 (void)strftime((char *)result_buf, sizeof(result_buf),
13905 (char *)p, curtime);
13906 else
13907 result_buf[0] = NUL;
13908
13909# ifdef FEAT_MBYTE
13910 if (conv.vc_type != CONV_NONE)
13911 vim_free(p);
13912 convert_setup(&conv, enc, p_enc);
13913 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013914 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915 else
13916# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013917 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918
13919# ifdef FEAT_MBYTE
13920 /* Release conversion descriptors */
13921 convert_setup(&conv, NULL, NULL);
13922 vim_free(enc);
13923# endif
13924 }
13925}
13926#endif
13927
13928/*
13929 * "stridx()" function
13930 */
13931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013932f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013933 typval_T *argvars;
13934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935{
13936 char_u buf[NUMBUFLEN];
13937 char_u *needle;
13938 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000013939 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000013941 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013943 needle = get_tv_string_chk(&argvars[1]);
13944 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000013945 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013946 if (needle == NULL || haystack == NULL)
13947 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948
Bram Moolenaar33570922005-01-25 22:26:29 +000013949 if (argvars[2].v_type != VAR_UNKNOWN)
13950 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013951 int error = FALSE;
13952
13953 start_idx = get_tv_number_chk(&argvars[2], &error);
13954 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000013955 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013956 if (start_idx >= 0)
13957 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000013958 }
13959
13960 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13961 if (pos != NULL)
13962 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963}
13964
13965/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013966 * "string()" function
13967 */
13968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013969f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013970 typval_T *argvars;
13971 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013972{
13973 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013974 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013976 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013977 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013978 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013979 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980}
13981
13982/*
13983 * "strlen()" function
13984 */
13985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013986f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013987 typval_T *argvars;
13988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013990 rettv->vval.v_number = (varnumber_T)(STRLEN(
13991 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992}
13993
13994/*
13995 * "strpart()" function
13996 */
13997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013998f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013999 typval_T *argvars;
14000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001{
14002 char_u *p;
14003 int n;
14004 int len;
14005 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014006 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014008 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 slen = (int)STRLEN(p);
14010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014011 n = get_tv_number_chk(&argvars[1], &error);
14012 if (error)
14013 len = 0;
14014 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014015 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 else
14017 len = slen - n; /* default len: all bytes that are available. */
14018
14019 /*
14020 * Only return the overlap between the specified part and the actual
14021 * string.
14022 */
14023 if (n < 0)
14024 {
14025 len += n;
14026 n = 0;
14027 }
14028 else if (n > slen)
14029 n = slen;
14030 if (len < 0)
14031 len = 0;
14032 else if (n + len > slen)
14033 len = slen - n;
14034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014035 rettv->v_type = VAR_STRING;
14036 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037}
14038
14039/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014040 * "strridx()" function
14041 */
14042 static void
14043f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014044 typval_T *argvars;
14045 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014046{
14047 char_u buf[NUMBUFLEN];
14048 char_u *needle;
14049 char_u *haystack;
14050 char_u *rest;
14051 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014052 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014053
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014054 needle = get_tv_string_chk(&argvars[1]);
14055 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014056 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014057
14058 rettv->vval.v_number = -1;
14059 if (needle == NULL || haystack == NULL)
14060 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014061 if (argvars[2].v_type != VAR_UNKNOWN)
14062 {
14063 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014064 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014065 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014066 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014067 }
14068 else
14069 end_idx = haystack_len;
14070
Bram Moolenaar0d660222005-01-07 21:51:51 +000014071 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014072 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014073 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014074 lastmatch = haystack + end_idx;
14075 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014076 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014077 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014078 for (rest = haystack; *rest != '\0'; ++rest)
14079 {
14080 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014081 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014082 break;
14083 lastmatch = rest;
14084 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014085 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014086
14087 if (lastmatch == NULL)
14088 rettv->vval.v_number = -1;
14089 else
14090 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14091}
14092
14093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094 * "strtrans()" function
14095 */
14096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014097f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014098 typval_T *argvars;
14099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014101 rettv->v_type = VAR_STRING;
14102 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103}
14104
14105/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014106 * "submatch()" function
14107 */
14108 static void
14109f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014110 typval_T *argvars;
14111 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014112{
14113 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014114 rettv->vval.v_string =
14115 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014116}
14117
14118/*
14119 * "substitute()" function
14120 */
14121 static void
14122f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014123 typval_T *argvars;
14124 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014125{
14126 char_u patbuf[NUMBUFLEN];
14127 char_u subbuf[NUMBUFLEN];
14128 char_u flagsbuf[NUMBUFLEN];
14129
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014130 char_u *str = get_tv_string_chk(&argvars[0]);
14131 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14132 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14133 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14134
Bram Moolenaar0d660222005-01-07 21:51:51 +000014135 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014136 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14137 rettv->vval.v_string = NULL;
14138 else
14139 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014140}
14141
14142/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014143 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144 */
14145/*ARGSUSED*/
14146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014147f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014148 typval_T *argvars;
14149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150{
14151 int id = 0;
14152#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014153 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154 long col;
14155 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014156 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014158 lnum = get_tv_lnum(argvars); /* -1 on type error */
14159 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14160 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014162 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014163 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
14164 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165#endif
14166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014167 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168}
14169
14170/*
14171 * "synIDattr(id, what [, mode])" function
14172 */
14173/*ARGSUSED*/
14174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014175f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014176 typval_T *argvars;
14177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178{
14179 char_u *p = NULL;
14180#ifdef FEAT_SYN_HL
14181 int id;
14182 char_u *what;
14183 char_u *mode;
14184 char_u modebuf[NUMBUFLEN];
14185 int modec;
14186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014187 id = get_tv_number(&argvars[0]);
14188 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014189 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014191 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192 modec = TOLOWER_ASC(mode[0]);
14193 if (modec != 't' && modec != 'c'
14194#ifdef FEAT_GUI
14195 && modec != 'g'
14196#endif
14197 )
14198 modec = 0; /* replace invalid with current */
14199 }
14200 else
14201 {
14202#ifdef FEAT_GUI
14203 if (gui.in_use)
14204 modec = 'g';
14205 else
14206#endif
14207 if (t_colors > 1)
14208 modec = 'c';
14209 else
14210 modec = 't';
14211 }
14212
14213
14214 switch (TOLOWER_ASC(what[0]))
14215 {
14216 case 'b':
14217 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14218 p = highlight_color(id, what, modec);
14219 else /* bold */
14220 p = highlight_has_attr(id, HL_BOLD, modec);
14221 break;
14222
14223 case 'f': /* fg[#] */
14224 p = highlight_color(id, what, modec);
14225 break;
14226
14227 case 'i':
14228 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14229 p = highlight_has_attr(id, HL_INVERSE, modec);
14230 else /* italic */
14231 p = highlight_has_attr(id, HL_ITALIC, modec);
14232 break;
14233
14234 case 'n': /* name */
14235 p = get_highlight_name(NULL, id - 1);
14236 break;
14237
14238 case 'r': /* reverse */
14239 p = highlight_has_attr(id, HL_INVERSE, modec);
14240 break;
14241
14242 case 's': /* standout */
14243 p = highlight_has_attr(id, HL_STANDOUT, modec);
14244 break;
14245
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014246 case 'u':
14247 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14248 /* underline */
14249 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14250 else
14251 /* undercurl */
14252 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253 break;
14254 }
14255
14256 if (p != NULL)
14257 p = vim_strsave(p);
14258#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014259 rettv->v_type = VAR_STRING;
14260 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261}
14262
14263/*
14264 * "synIDtrans(id)" function
14265 */
14266/*ARGSUSED*/
14267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014268f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014269 typval_T *argvars;
14270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271{
14272 int id;
14273
14274#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014275 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276
14277 if (id > 0)
14278 id = syn_get_final_id(id);
14279 else
14280#endif
14281 id = 0;
14282
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014283 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014284}
14285
14286/*
14287 * "system()" function
14288 */
14289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014290f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014291 typval_T *argvars;
14292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014294 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014296 char_u *infile = NULL;
14297 char_u buf[NUMBUFLEN];
14298 int err = FALSE;
14299 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014301 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014302 {
14303 /*
14304 * Write the string to a temp file, to be used for input of the shell
14305 * command.
14306 */
14307 if ((infile = vim_tempname('i')) == NULL)
14308 {
14309 EMSG(_(e_notmp));
14310 return;
14311 }
14312
14313 fd = mch_fopen((char *)infile, WRITEBIN);
14314 if (fd == NULL)
14315 {
14316 EMSG2(_(e_notopen), infile);
14317 goto done;
14318 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014319 p = get_tv_string_buf_chk(&argvars[1], buf);
14320 if (p == NULL)
14321 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014322 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14323 err = TRUE;
14324 if (fclose(fd) != 0)
14325 err = TRUE;
14326 if (err)
14327 {
14328 EMSG(_("E677: Error writing temp file"));
14329 goto done;
14330 }
14331 }
14332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014333 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014334
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335#ifdef USE_CR
14336 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014337 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338 {
14339 char_u *s;
14340
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014341 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 {
14343 if (*s == CAR)
14344 *s = NL;
14345 }
14346 }
14347#else
14348# ifdef USE_CRNL
14349 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014350 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351 {
14352 char_u *s, *d;
14353
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014354 d = res;
14355 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356 {
14357 if (s[0] == CAR && s[1] == NL)
14358 ++s;
14359 *d++ = *s;
14360 }
14361 *d = NUL;
14362 }
14363# endif
14364#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014365
14366done:
14367 if (infile != NULL)
14368 {
14369 mch_remove(infile);
14370 vim_free(infile);
14371 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014372 rettv->v_type = VAR_STRING;
14373 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374}
14375
14376/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014377 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014378 */
14379 static void
14380f_taglist(argvars, rettv)
14381 typval_T *argvars;
14382 typval_T *rettv;
14383{
14384 char_u *tag_pattern;
14385 list_T *l;
14386
14387 tag_pattern = get_tv_string(&argvars[0]);
14388
14389 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014390 if (*tag_pattern == NUL)
14391 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014392
14393 l = list_alloc();
14394 if (l != NULL)
14395 {
14396 if (get_tags(l, tag_pattern) != FAIL)
14397 {
14398 rettv->vval.v_list = l;
14399 rettv->v_type = VAR_LIST;
14400 ++l->lv_refcount;
14401 }
14402 else
14403 list_free(l);
14404 }
14405}
14406
14407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408 * "tempname()" function
14409 */
14410/*ARGSUSED*/
14411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014412f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014413 typval_T *argvars;
14414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415{
14416 static int x = 'A';
14417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014418 rettv->v_type = VAR_STRING;
14419 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420
14421 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14422 * names. Skip 'I' and 'O', they are used for shell redirection. */
14423 do
14424 {
14425 if (x == 'Z')
14426 x = '0';
14427 else if (x == '9')
14428 x = 'A';
14429 else
14430 {
14431#ifdef EBCDIC
14432 if (x == 'I')
14433 x = 'J';
14434 else if (x == 'R')
14435 x = 'S';
14436 else
14437#endif
14438 ++x;
14439 }
14440 } while (x == 'I' || x == 'O');
14441}
14442
14443/*
14444 * "tolower(string)" function
14445 */
14446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014447f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014448 typval_T *argvars;
14449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450{
14451 char_u *p;
14452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014453 p = vim_strsave(get_tv_string(&argvars[0]));
14454 rettv->v_type = VAR_STRING;
14455 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456
14457 if (p != NULL)
14458 while (*p != NUL)
14459 {
14460#ifdef FEAT_MBYTE
14461 int l;
14462
14463 if (enc_utf8)
14464 {
14465 int c, lc;
14466
14467 c = utf_ptr2char(p);
14468 lc = utf_tolower(c);
14469 l = utf_ptr2len_check(p);
14470 /* TODO: reallocate string when byte count changes. */
14471 if (utf_char2len(lc) == l)
14472 utf_char2bytes(lc, p);
14473 p += l;
14474 }
14475 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
14476 p += l; /* skip multi-byte character */
14477 else
14478#endif
14479 {
14480 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14481 ++p;
14482 }
14483 }
14484}
14485
14486/*
14487 * "toupper(string)" function
14488 */
14489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014490f_toupper(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014494 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014495 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496}
14497
14498/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014499 * "tr(string, fromstr, tostr)" function
14500 */
14501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014502f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014503 typval_T *argvars;
14504 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014505{
14506 char_u *instr;
14507 char_u *fromstr;
14508 char_u *tostr;
14509 char_u *p;
14510#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014511 int inlen;
14512 int fromlen;
14513 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014514 int idx;
14515 char_u *cpstr;
14516 int cplen;
14517 int first = TRUE;
14518#endif
14519 char_u buf[NUMBUFLEN];
14520 char_u buf2[NUMBUFLEN];
14521 garray_T ga;
14522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014523 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014524 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14525 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014526
14527 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014528 rettv->v_type = VAR_STRING;
14529 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014530 if (fromstr == NULL || tostr == NULL)
14531 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014532 ga_init2(&ga, (int)sizeof(char), 80);
14533
14534#ifdef FEAT_MBYTE
14535 if (!has_mbyte)
14536#endif
14537 /* not multi-byte: fromstr and tostr must be the same length */
14538 if (STRLEN(fromstr) != STRLEN(tostr))
14539 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014540#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014541error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014542#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014543 EMSG2(_(e_invarg2), fromstr);
14544 ga_clear(&ga);
14545 return;
14546 }
14547
14548 /* fromstr and tostr have to contain the same number of chars */
14549 while (*instr != NUL)
14550 {
14551#ifdef FEAT_MBYTE
14552 if (has_mbyte)
14553 {
14554 inlen = mb_ptr2len_check(instr);
14555 cpstr = instr;
14556 cplen = inlen;
14557 idx = 0;
14558 for (p = fromstr; *p != NUL; p += fromlen)
14559 {
14560 fromlen = mb_ptr2len_check(p);
14561 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14562 {
14563 for (p = tostr; *p != NUL; p += tolen)
14564 {
14565 tolen = mb_ptr2len_check(p);
14566 if (idx-- == 0)
14567 {
14568 cplen = tolen;
14569 cpstr = p;
14570 break;
14571 }
14572 }
14573 if (*p == NUL) /* tostr is shorter than fromstr */
14574 goto error;
14575 break;
14576 }
14577 ++idx;
14578 }
14579
14580 if (first && cpstr == instr)
14581 {
14582 /* Check that fromstr and tostr have the same number of
14583 * (multi-byte) characters. Done only once when a character
14584 * of instr doesn't appear in fromstr. */
14585 first = FALSE;
14586 for (p = tostr; *p != NUL; p += tolen)
14587 {
14588 tolen = mb_ptr2len_check(p);
14589 --idx;
14590 }
14591 if (idx != 0)
14592 goto error;
14593 }
14594
14595 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014596 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014597 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014598
14599 instr += inlen;
14600 }
14601 else
14602#endif
14603 {
14604 /* When not using multi-byte chars we can do it faster. */
14605 p = vim_strchr(fromstr, *instr);
14606 if (p != NULL)
14607 ga_append(&ga, tostr[p - fromstr]);
14608 else
14609 ga_append(&ga, *instr);
14610 ++instr;
14611 }
14612 }
14613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014614 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014615}
14616
14617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618 * "type(expr)" function
14619 */
14620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014621f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014622 typval_T *argvars;
14623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014625 int n;
14626
14627 switch (argvars[0].v_type)
14628 {
14629 case VAR_NUMBER: n = 0; break;
14630 case VAR_STRING: n = 1; break;
14631 case VAR_FUNC: n = 2; break;
14632 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014633 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014634 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14635 }
14636 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637}
14638
14639/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014640 * "values(dict)" function
14641 */
14642 static void
14643f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014644 typval_T *argvars;
14645 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014646{
14647 dict_list(argvars, rettv, 1);
14648}
14649
14650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014651 * "virtcol(string)" function
14652 */
14653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014654f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014655 typval_T *argvars;
14656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014657{
14658 colnr_T vcol = 0;
14659 pos_T *fp;
14660
14661 fp = var2fpos(&argvars[0], FALSE);
14662 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14663 {
14664 getvvcol(curwin, fp, NULL, NULL, &vcol);
14665 ++vcol;
14666 }
14667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014668 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669}
14670
14671/*
14672 * "visualmode()" function
14673 */
14674/*ARGSUSED*/
14675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014676f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014677 typval_T *argvars;
14678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679{
14680#ifdef FEAT_VISUAL
14681 char_u str[2];
14682
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014683 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684 str[0] = curbuf->b_visual_mode_eval;
14685 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014686 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687
14688 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014689 if ((argvars[0].v_type == VAR_NUMBER
14690 && argvars[0].vval.v_number != 0)
14691 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014692 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693 curbuf->b_visual_mode_eval = NUL;
14694#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014695 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014696#endif
14697}
14698
14699/*
14700 * "winbufnr(nr)" function
14701 */
14702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014703f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014704 typval_T *argvars;
14705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706{
14707 win_T *wp;
14708
14709 wp = find_win_by_nr(&argvars[0]);
14710 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014711 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014713 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014714}
14715
14716/*
14717 * "wincol()" function
14718 */
14719/*ARGSUSED*/
14720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014721f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014722 typval_T *argvars;
14723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724{
14725 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014726 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727}
14728
14729/*
14730 * "winheight(nr)" function
14731 */
14732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014733f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014734 typval_T *argvars;
14735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736{
14737 win_T *wp;
14738
14739 wp = find_win_by_nr(&argvars[0]);
14740 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014741 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014743 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744}
14745
14746/*
14747 * "winline()" function
14748 */
14749/*ARGSUSED*/
14750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014751f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014752 typval_T *argvars;
14753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754{
14755 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014756 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757}
14758
14759/*
14760 * "winnr()" function
14761 */
14762/* ARGSUSED */
14763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014764f_winnr(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 int nr = 1;
14769#ifdef FEAT_WINDOWS
14770 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014771 win_T *twin = curwin;
14772 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014774 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014776 arg = get_tv_string_chk(&argvars[0]);
14777 if (arg == NULL)
14778 nr = 0; /* type error; errmsg already given */
14779 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014780 twin = lastwin;
14781 else if (STRCMP(arg, "#") == 0)
14782 {
14783 twin = prevwin;
14784 if (prevwin == NULL)
14785 nr = 0;
14786 }
14787 else
14788 {
14789 EMSG2(_(e_invexpr2), arg);
14790 nr = 0;
14791 }
14792 }
14793
14794 if (nr > 0)
14795 for (wp = firstwin; wp != twin; wp = wp->w_next)
14796 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014797#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014798 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014799}
14800
14801/*
14802 * "winrestcmd()" function
14803 */
14804/* ARGSUSED */
14805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014806f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014807 typval_T *argvars;
14808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809{
14810#ifdef FEAT_WINDOWS
14811 win_T *wp;
14812 int winnr = 1;
14813 garray_T ga;
14814 char_u buf[50];
14815
14816 ga_init2(&ga, (int)sizeof(char), 70);
14817 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14818 {
14819 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14820 ga_concat(&ga, buf);
14821# ifdef FEAT_VERTSPLIT
14822 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14823 ga_concat(&ga, buf);
14824# endif
14825 ++winnr;
14826 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014827 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014829 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014831 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014833 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834}
14835
14836/*
14837 * "winwidth(nr)" function
14838 */
14839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014840f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014841 typval_T *argvars;
14842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843{
14844 win_T *wp;
14845
14846 wp = find_win_by_nr(&argvars[0]);
14847 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014848 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849 else
14850#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014851 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014853 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854#endif
14855}
14856
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014858 * "writefile()" function
14859 */
14860 static void
14861f_writefile(argvars, rettv)
14862 typval_T *argvars;
14863 typval_T *rettv;
14864{
14865 int binary = FALSE;
14866 char_u *fname;
14867 FILE *fd;
14868 listitem_T *li;
14869 char_u *s;
14870 int ret = 0;
14871 int c;
14872
14873 if (argvars[0].v_type != VAR_LIST)
14874 {
14875 EMSG2(_(e_listarg), "writefile()");
14876 return;
14877 }
14878 if (argvars[0].vval.v_list == NULL)
14879 return;
14880
14881 if (argvars[2].v_type != VAR_UNKNOWN
14882 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
14883 binary = TRUE;
14884
14885 /* Always open the file in binary mode, library functions have a mind of
14886 * their own about CR-LF conversion. */
14887 fname = get_tv_string(&argvars[1]);
14888 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
14889 {
14890 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14891 ret = -1;
14892 }
14893 else
14894 {
14895 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
14896 li = li->li_next)
14897 {
14898 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
14899 {
14900 if (*s == '\n')
14901 c = putc(NUL, fd);
14902 else
14903 c = putc(*s, fd);
14904 if (c == EOF)
14905 {
14906 ret = -1;
14907 break;
14908 }
14909 }
14910 if (!binary || li->li_next != NULL)
14911 if (putc('\n', fd) == EOF)
14912 {
14913 ret = -1;
14914 break;
14915 }
14916 if (ret < 0)
14917 {
14918 EMSG(_(e_write));
14919 break;
14920 }
14921 }
14922 fclose(fd);
14923 }
14924
14925 rettv->vval.v_number = ret;
14926}
14927
14928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929 * Translate a String variable into a position.
14930 */
14931 static pos_T *
14932var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000014933 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014934 int lnum; /* TRUE when $ is last line */
14935{
14936 char_u *name;
14937 static pos_T pos;
14938 pos_T *pp;
14939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014940 name = get_tv_string_chk(varp);
14941 if (name == NULL)
14942 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943 if (name[0] == '.') /* cursor */
14944 return &curwin->w_cursor;
14945 if (name[0] == '\'') /* mark */
14946 {
14947 pp = getmark(name[1], FALSE);
14948 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
14949 return NULL;
14950 return pp;
14951 }
14952 if (name[0] == '$') /* last column or line */
14953 {
14954 if (lnum)
14955 {
14956 pos.lnum = curbuf->b_ml.ml_line_count;
14957 pos.col = 0;
14958 }
14959 else
14960 {
14961 pos.lnum = curwin->w_cursor.lnum;
14962 pos.col = (colnr_T)STRLEN(ml_get_curline());
14963 }
14964 return &pos;
14965 }
14966 return NULL;
14967}
14968
14969/*
14970 * Get the length of an environment variable name.
14971 * Advance "arg" to the first character after the name.
14972 * Return 0 for error.
14973 */
14974 static int
14975get_env_len(arg)
14976 char_u **arg;
14977{
14978 char_u *p;
14979 int len;
14980
14981 for (p = *arg; vim_isIDc(*p); ++p)
14982 ;
14983 if (p == *arg) /* no name found */
14984 return 0;
14985
14986 len = (int)(p - *arg);
14987 *arg = p;
14988 return len;
14989}
14990
14991/*
14992 * Get the length of the name of a function or internal variable.
14993 * "arg" is advanced to the first non-white character after the name.
14994 * Return 0 if something is wrong.
14995 */
14996 static int
14997get_id_len(arg)
14998 char_u **arg;
14999{
15000 char_u *p;
15001 int len;
15002
15003 /* Find the end of the name. */
15004 for (p = *arg; eval_isnamec(*p); ++p)
15005 ;
15006 if (p == *arg) /* no name found */
15007 return 0;
15008
15009 len = (int)(p - *arg);
15010 *arg = skipwhite(p);
15011
15012 return len;
15013}
15014
15015/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015016 * Get the length of the name of a variable or function.
15017 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015019 * Return -1 if curly braces expansion failed.
15020 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021 * If the name contains 'magic' {}'s, expand them and return the
15022 * expanded name in an allocated string via 'alias' - caller must free.
15023 */
15024 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015025get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026 char_u **arg;
15027 char_u **alias;
15028 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015029 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030{
15031 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032 char_u *p;
15033 char_u *expr_start;
15034 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035
15036 *alias = NULL; /* default to no alias */
15037
15038 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15039 && (*arg)[2] == (int)KE_SNR)
15040 {
15041 /* hard coded <SNR>, already translated */
15042 *arg += 3;
15043 return get_id_len(arg) + 3;
15044 }
15045 len = eval_fname_script(*arg);
15046 if (len > 0)
15047 {
15048 /* literal "<SID>", "s:" or "<SNR>" */
15049 *arg += len;
15050 }
15051
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015053 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015055 p = find_name_end(*arg, &expr_start, &expr_end,
15056 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057 if (expr_start != NULL)
15058 {
15059 char_u *temp_string;
15060
15061 if (!evaluate)
15062 {
15063 len += (int)(p - *arg);
15064 *arg = skipwhite(p);
15065 return len;
15066 }
15067
15068 /*
15069 * Include any <SID> etc in the expanded string:
15070 * Thus the -len here.
15071 */
15072 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15073 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015074 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075 *alias = temp_string;
15076 *arg = skipwhite(p);
15077 return (int)STRLEN(temp_string);
15078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015079
15080 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015081 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082 EMSG2(_(e_invexpr2), *arg);
15083
15084 return len;
15085}
15086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015087/*
15088 * Find the end of a variable or function name, taking care of magic braces.
15089 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15090 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015091 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015092 * Return a pointer to just after the name. Equal to "arg" if there is no
15093 * valid name.
15094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015096find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097 char_u *arg;
15098 char_u **expr_start;
15099 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015100 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015102 int mb_nest = 0;
15103 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015104 char_u *p;
15105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015106 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015108 *expr_start = NULL;
15109 *expr_end = NULL;
15110 }
15111
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015112 /* Quick check for valid starting character. */
15113 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15114 return arg;
15115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015116 for (p = arg; *p != NUL
15117 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015118 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015119 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015120 || mb_nest != 0
15121 || br_nest != 0); ++p)
15122 {
15123 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015124 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015125 if (*p == '[')
15126 ++br_nest;
15127 else if (*p == ']')
15128 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015130 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015132 if (*p == '{')
15133 {
15134 mb_nest++;
15135 if (expr_start != NULL && *expr_start == NULL)
15136 *expr_start = p;
15137 }
15138 else if (*p == '}')
15139 {
15140 mb_nest--;
15141 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15142 *expr_end = p;
15143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145 }
15146
15147 return p;
15148}
15149
15150/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015151 * Expands out the 'magic' {}'s in a variable/function name.
15152 * Note that this can call itself recursively, to deal with
15153 * constructs like foo{bar}{baz}{bam}
15154 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15155 * "in_start" ^
15156 * "expr_start" ^
15157 * "expr_end" ^
15158 * "in_end" ^
15159 *
15160 * Returns a new allocated string, which the caller must free.
15161 * Returns NULL for failure.
15162 */
15163 static char_u *
15164make_expanded_name(in_start, expr_start, expr_end, in_end)
15165 char_u *in_start;
15166 char_u *expr_start;
15167 char_u *expr_end;
15168 char_u *in_end;
15169{
15170 char_u c1;
15171 char_u *retval = NULL;
15172 char_u *temp_result;
15173 char_u *nextcmd = NULL;
15174
15175 if (expr_end == NULL || in_end == NULL)
15176 return NULL;
15177 *expr_start = NUL;
15178 *expr_end = NUL;
15179 c1 = *in_end;
15180 *in_end = NUL;
15181
15182 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15183 if (temp_result != NULL && nextcmd == NULL)
15184 {
15185 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15186 + (in_end - expr_end) + 1));
15187 if (retval != NULL)
15188 {
15189 STRCPY(retval, in_start);
15190 STRCAT(retval, temp_result);
15191 STRCAT(retval, expr_end + 1);
15192 }
15193 }
15194 vim_free(temp_result);
15195
15196 *in_end = c1; /* put char back for error messages */
15197 *expr_start = '{';
15198 *expr_end = '}';
15199
15200 if (retval != NULL)
15201 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015202 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015203 if (expr_start != NULL)
15204 {
15205 /* Further expansion! */
15206 temp_result = make_expanded_name(retval, expr_start,
15207 expr_end, temp_result);
15208 vim_free(retval);
15209 retval = temp_result;
15210 }
15211 }
15212
15213 return retval;
15214}
15215
15216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015217 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015218 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015219 */
15220 static int
15221eval_isnamec(c)
15222 int c;
15223{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015224 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15225}
15226
15227/*
15228 * Return TRUE if character "c" can be used as the first character in a
15229 * variable or function name (excluding '{' and '}').
15230 */
15231 static int
15232eval_isnamec1(c)
15233 int c;
15234{
15235 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236}
15237
15238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015239 * Set number v: variable to "val".
15240 */
15241 void
15242set_vim_var_nr(idx, val)
15243 int idx;
15244 long val;
15245{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015246 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015247}
15248
15249/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015250 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015251 */
15252 long
15253get_vim_var_nr(idx)
15254 int idx;
15255{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015256 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257}
15258
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015259#if defined(FEAT_AUTOCMD) || defined(PROTO)
15260/*
15261 * Get string v: variable value. Uses a static buffer, can only be used once.
15262 */
15263 char_u *
15264get_vim_var_str(idx)
15265 int idx;
15266{
15267 return get_tv_string(&vimvars[idx].vv_tv);
15268}
15269#endif
15270
Bram Moolenaar071d4272004-06-13 20:20:40 +000015271/*
15272 * Set v:count, v:count1 and v:prevcount.
15273 */
15274 void
15275set_vcount(count, count1)
15276 long count;
15277 long count1;
15278{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015279 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15280 vimvars[VV_COUNT].vv_nr = count;
15281 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282}
15283
15284/*
15285 * Set string v: variable to a copy of "val".
15286 */
15287 void
15288set_vim_var_string(idx, val, len)
15289 int idx;
15290 char_u *val;
15291 int len; /* length of "val" to use or -1 (whole string) */
15292{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015293 /* Need to do this (at least) once, since we can't initialize a union.
15294 * Will always be invoked when "v:progname" is set. */
15295 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15296
Bram Moolenaare9a41262005-01-15 22:18:47 +000015297 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015299 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015300 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015301 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015303 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015304}
15305
15306/*
15307 * Set v:register if needed.
15308 */
15309 void
15310set_reg_var(c)
15311 int c;
15312{
15313 char_u regname;
15314
15315 if (c == 0 || c == ' ')
15316 regname = '"';
15317 else
15318 regname = c;
15319 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015320 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321 set_vim_var_string(VV_REG, &regname, 1);
15322}
15323
15324/*
15325 * Get or set v:exception. If "oldval" == NULL, return the current value.
15326 * Otherwise, restore the value to "oldval" and return NULL.
15327 * Must always be called in pairs to save and restore v:exception! Does not
15328 * take care of memory allocations.
15329 */
15330 char_u *
15331v_exception(oldval)
15332 char_u *oldval;
15333{
15334 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015335 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336
Bram Moolenaare9a41262005-01-15 22:18:47 +000015337 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015338 return NULL;
15339}
15340
15341/*
15342 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15343 * Otherwise, restore the value to "oldval" and return NULL.
15344 * Must always be called in pairs to save and restore v:throwpoint! Does not
15345 * take care of memory allocations.
15346 */
15347 char_u *
15348v_throwpoint(oldval)
15349 char_u *oldval;
15350{
15351 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015352 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353
Bram Moolenaare9a41262005-01-15 22:18:47 +000015354 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355 return NULL;
15356}
15357
15358#if defined(FEAT_AUTOCMD) || defined(PROTO)
15359/*
15360 * Set v:cmdarg.
15361 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15362 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15363 * Must always be called in pairs!
15364 */
15365 char_u *
15366set_cmdarg(eap, oldarg)
15367 exarg_T *eap;
15368 char_u *oldarg;
15369{
15370 char_u *oldval;
15371 char_u *newval;
15372 unsigned len;
15373
Bram Moolenaare9a41262005-01-15 22:18:47 +000015374 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015375 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015377 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015378 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015379 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 }
15381
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015382 if (eap->force_bin == FORCE_BIN)
15383 len = 6;
15384 else if (eap->force_bin == FORCE_NOBIN)
15385 len = 8;
15386 else
15387 len = 0;
15388 if (eap->force_ff != 0)
15389 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15390# ifdef FEAT_MBYTE
15391 if (eap->force_enc != 0)
15392 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15393# endif
15394
15395 newval = alloc(len + 1);
15396 if (newval == NULL)
15397 return NULL;
15398
15399 if (eap->force_bin == FORCE_BIN)
15400 sprintf((char *)newval, " ++bin");
15401 else if (eap->force_bin == FORCE_NOBIN)
15402 sprintf((char *)newval, " ++nobin");
15403 else
15404 *newval = NUL;
15405 if (eap->force_ff != 0)
15406 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15407 eap->cmd + eap->force_ff);
15408# ifdef FEAT_MBYTE
15409 if (eap->force_enc != 0)
15410 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15411 eap->cmd + eap->force_enc);
15412# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015413 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015414 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415}
15416#endif
15417
15418/*
15419 * Get the value of internal variable "name".
15420 * Return OK or FAIL.
15421 */
15422 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015423get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424 char_u *name;
15425 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015426 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015427 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428{
15429 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015430 typval_T *tv = NULL;
15431 typval_T atv;
15432 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015433 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434
15435 /* truncate the name, so that we can use strcmp() */
15436 cc = name[len];
15437 name[len] = NUL;
15438
15439 /*
15440 * Check for "b:changedtick".
15441 */
15442 if (STRCMP(name, "b:changedtick") == 0)
15443 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015444 atv.v_type = VAR_NUMBER;
15445 atv.vval.v_number = curbuf->b_changedtick;
15446 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447 }
15448
15449 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450 * Check for user-defined variables.
15451 */
15452 else
15453 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015454 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015456 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457 }
15458
Bram Moolenaare9a41262005-01-15 22:18:47 +000015459 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015461 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015462 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463 ret = FAIL;
15464 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015465 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015466 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467
15468 name[len] = cc;
15469
15470 return ret;
15471}
15472
15473/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015474 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15475 * Also handle function call with Funcref variable: func(expr)
15476 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15477 */
15478 static int
15479handle_subscript(arg, rettv, evaluate, verbose)
15480 char_u **arg;
15481 typval_T *rettv;
15482 int evaluate; /* do more than finding the end */
15483 int verbose; /* give error messages */
15484{
15485 int ret = OK;
15486 dict_T *selfdict = NULL;
15487 char_u *s;
15488 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015489 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015490
15491 while (ret == OK
15492 && (**arg == '['
15493 || (**arg == '.' && rettv->v_type == VAR_DICT)
15494 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15495 && !vim_iswhite(*(*arg - 1)))
15496 {
15497 if (**arg == '(')
15498 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015499 /* need to copy the funcref so that we can clear rettv */
15500 functv = *rettv;
15501 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015502
15503 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015504 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015505 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015506 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15507 &len, evaluate, selfdict);
15508
15509 /* Clear the funcref afterwards, so that deleting it while
15510 * evaluating the arguments is possible (see test55). */
15511 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015512
15513 /* Stop the expression evaluation when immediately aborting on
15514 * error, or when an interrupt occurred or an exception was thrown
15515 * but not caught. */
15516 if (aborting())
15517 {
15518 if (ret == OK)
15519 clear_tv(rettv);
15520 ret = FAIL;
15521 }
15522 dict_unref(selfdict);
15523 selfdict = NULL;
15524 }
15525 else /* **arg == '[' || **arg == '.' */
15526 {
15527 dict_unref(selfdict);
15528 if (rettv->v_type == VAR_DICT)
15529 {
15530 selfdict = rettv->vval.v_dict;
15531 if (selfdict != NULL)
15532 ++selfdict->dv_refcount;
15533 }
15534 else
15535 selfdict = NULL;
15536 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15537 {
15538 clear_tv(rettv);
15539 ret = FAIL;
15540 }
15541 }
15542 }
15543 dict_unref(selfdict);
15544 return ret;
15545}
15546
15547/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015548 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15549 * value).
15550 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015551 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015552alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015553{
Bram Moolenaar33570922005-01-25 22:26:29 +000015554 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015555}
15556
15557/*
15558 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 * The string "s" must have been allocated, it is consumed.
15560 * Return NULL for out of memory, the variable otherwise.
15561 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015562 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015563alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564 char_u *s;
15565{
Bram Moolenaar33570922005-01-25 22:26:29 +000015566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015568 rettv = alloc_tv();
15569 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015571 rettv->v_type = VAR_STRING;
15572 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573 }
15574 else
15575 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015576 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577}
15578
15579/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015580 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581 */
15582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015583free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015584 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585{
15586 if (varp != NULL)
15587 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015588 switch (varp->v_type)
15589 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015590 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015591 func_unref(varp->vval.v_string);
15592 /*FALLTHROUGH*/
15593 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015594 vim_free(varp->vval.v_string);
15595 break;
15596 case VAR_LIST:
15597 list_unref(varp->vval.v_list);
15598 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015599 case VAR_DICT:
15600 dict_unref(varp->vval.v_dict);
15601 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015602 case VAR_NUMBER:
15603 case VAR_UNKNOWN:
15604 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015605 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015606 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015607 break;
15608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609 vim_free(varp);
15610 }
15611}
15612
15613/*
15614 * Free the memory for a variable value and set the value to NULL or 0.
15615 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015616 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015617clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015618 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619{
15620 if (varp != NULL)
15621 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015622 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015624 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015625 func_unref(varp->vval.v_string);
15626 /*FALLTHROUGH*/
15627 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015628 vim_free(varp->vval.v_string);
15629 varp->vval.v_string = NULL;
15630 break;
15631 case VAR_LIST:
15632 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015633 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015634 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015635 case VAR_DICT:
15636 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015637 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015638 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015639 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015640 varp->vval.v_number = 0;
15641 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015642 case VAR_UNKNOWN:
15643 break;
15644 default:
15645 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015646 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015647 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 }
15649}
15650
15651/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015652 * Set the value of a variable to NULL without freeing items.
15653 */
15654 static void
15655init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015656 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015657{
15658 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015659 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015660}
15661
15662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663 * Get the number value of a variable.
15664 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015665 * For incompatible types, return 0.
15666 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15667 * caller of incompatible types: it sets *denote to TRUE if "denote"
15668 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 */
15670 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015671get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015672 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015674 int error = FALSE;
15675
15676 return get_tv_number_chk(varp, &error); /* return 0L on error */
15677}
15678
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015679 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015680get_tv_number_chk(varp, denote)
15681 typval_T *varp;
15682 int *denote;
15683{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015684 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015686 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015688 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015689 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015690 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015691 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015692 break;
15693 case VAR_STRING:
15694 if (varp->vval.v_string != NULL)
15695 vim_str2nr(varp->vval.v_string, NULL, NULL,
15696 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015697 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015698 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015699 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015700 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015701 case VAR_DICT:
15702 EMSG(_("E728: Using a Dictionary as a number"));
15703 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015704 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015705 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015706 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015708 if (denote == NULL) /* useful for values that must be unsigned */
15709 n = -1;
15710 else
15711 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015712 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713}
15714
15715/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015716 * Get the lnum from the first argument.
15717 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015718 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719 */
15720 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015721get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015722 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723{
Bram Moolenaar33570922005-01-25 22:26:29 +000015724 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 linenr_T lnum;
15726
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015727 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 if (lnum == 0) /* no valid number, try using line() */
15729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015730 rettv.v_type = VAR_NUMBER;
15731 f_line(argvars, &rettv);
15732 lnum = rettv.vval.v_number;
15733 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734 }
15735 return lnum;
15736}
15737
15738/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015739 * Get the lnum from the first argument.
15740 * Also accepts "$", then "buf" is used.
15741 * Returns 0 on error.
15742 */
15743 static linenr_T
15744get_tv_lnum_buf(argvars, buf)
15745 typval_T *argvars;
15746 buf_T *buf;
15747{
15748 if (argvars[0].v_type == VAR_STRING
15749 && argvars[0].vval.v_string != NULL
15750 && argvars[0].vval.v_string[0] == '$'
15751 && buf != NULL)
15752 return buf->b_ml.ml_line_count;
15753 return get_tv_number_chk(&argvars[0], NULL);
15754}
15755
15756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 * Get the string value of a variable.
15758 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015759 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15760 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761 * If the String variable has never been set, return an empty string.
15762 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015763 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15764 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765 */
15766 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015767get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015768 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769{
15770 static char_u mybuf[NUMBUFLEN];
15771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015772 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773}
15774
15775 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015776get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015777 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015778 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015780 char_u *res = get_tv_string_buf_chk(varp, buf);
15781
15782 return res != NULL ? res : (char_u *)"";
15783}
15784
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015785 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015786get_tv_string_chk(varp)
15787 typval_T *varp;
15788{
15789 static char_u mybuf[NUMBUFLEN];
15790
15791 return get_tv_string_buf_chk(varp, mybuf);
15792}
15793
15794 static char_u *
15795get_tv_string_buf_chk(varp, buf)
15796 typval_T *varp;
15797 char_u *buf;
15798{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015799 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015801 case VAR_NUMBER:
15802 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15803 return buf;
15804 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015805 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015806 break;
15807 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015808 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015809 break;
15810 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015811 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015812 break;
15813 case VAR_STRING:
15814 if (varp->vval.v_string != NULL)
15815 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015816 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015817 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015818 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015819 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015820 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015821 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822}
15823
15824/*
15825 * Find variable "name" in the list of variables.
15826 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015827 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015828 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015829 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015830 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015831 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015832find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015834 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015836 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015837 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838
Bram Moolenaara7043832005-01-21 11:56:39 +000015839 ht = find_var_ht(name, &varname);
15840 if (htp != NULL)
15841 *htp = ht;
15842 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015844 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845}
15846
15847/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015848 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000015849 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015851 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015852find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000015853 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000015854 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015855 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000015856{
Bram Moolenaar33570922005-01-25 22:26:29 +000015857 hashitem_T *hi;
15858
15859 if (*varname == NUL)
15860 {
15861 /* Must be something like "s:", otherwise "ht" would be NULL. */
15862 switch (varname[-2])
15863 {
15864 case 's': return &SCRIPT_SV(current_SID).sv_var;
15865 case 'g': return &globvars_var;
15866 case 'v': return &vimvars_var;
15867 case 'b': return &curbuf->b_bufvar;
15868 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015869 case 'l': return current_funccal == NULL
15870 ? NULL : &current_funccal->l_vars_var;
15871 case 'a': return current_funccal == NULL
15872 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000015873 }
15874 return NULL;
15875 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015876
15877 hi = hash_find(ht, varname);
15878 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015879 {
15880 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015881 * worked find the variable again. Don't auto-load a script if it was
15882 * loaded already, otherwise it would be loaded every time when
15883 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015884 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015885 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015886 hi = hash_find(ht, varname);
15887 if (HASHITEM_EMPTY(hi))
15888 return NULL;
15889 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015890 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015891}
15892
15893/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015894 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015895 * Set "varname" to the start of name without ':'.
15896 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015897 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015898find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015899 char_u *name;
15900 char_u **varname;
15901{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015902 hashitem_T *hi;
15903
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904 if (name[1] != ':')
15905 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015906 /* The name must not start with a colon or #. */
15907 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908 return NULL;
15909 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015910
15911 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015912 hi = hash_find(&compat_hashtab, name);
15913 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000015914 return &compat_hashtab;
15915
Bram Moolenaar071d4272004-06-13 20:20:40 +000015916 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015917 return &globvarht; /* global variable */
15918 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015919 }
15920 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015921 if (*name == 'g') /* global variable */
15922 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015923 /* There must be no ':' or '#' in the rest of the name, unless g: is used
15924 */
15925 if (vim_strchr(name + 2, ':') != NULL
15926 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015927 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015929 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015931 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000015932 if (*name == 'v') /* v: variable */
15933 return &vimvarht;
15934 if (*name == 'a' && current_funccal != NULL) /* function argument */
15935 return &current_funccal->l_avars.dv_hashtab;
15936 if (*name == 'l' && current_funccal != NULL) /* local function variable */
15937 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938 if (*name == 's' /* script variable */
15939 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
15940 return &SCRIPT_VARS(current_SID);
15941 return NULL;
15942}
15943
15944/*
15945 * Get the string value of a (global/local) variable.
15946 * Returns NULL when it doesn't exist.
15947 */
15948 char_u *
15949get_var_value(name)
15950 char_u *name;
15951{
Bram Moolenaar33570922005-01-25 22:26:29 +000015952 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015953
Bram Moolenaara7043832005-01-21 11:56:39 +000015954 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015955 if (v == NULL)
15956 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015957 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015958}
15959
15960/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015961 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000015962 * sourcing this script and when executing functions defined in the script.
15963 */
15964 void
15965new_script_vars(id)
15966 scid_T id;
15967{
Bram Moolenaara7043832005-01-21 11:56:39 +000015968 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000015969 hashtab_T *ht;
15970 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000015971
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
15973 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015974 /* Re-allocating ga_data means that an ht_array pointing to
15975 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000015976 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000015977 for (i = 1; i <= ga_scripts.ga_len; ++i)
15978 {
15979 ht = &SCRIPT_VARS(i);
15980 if (ht->ht_mask == HT_INIT_SIZE - 1)
15981 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000015982 sv = &SCRIPT_SV(i);
15983 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000015984 }
15985
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986 while (ga_scripts.ga_len < id)
15987 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015988 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
15989 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015991 }
15992 }
15993}
15994
15995/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015996 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
15997 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998 */
15999 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016000init_var_dict(dict, dict_var)
16001 dict_T *dict;
16002 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003{
Bram Moolenaar33570922005-01-25 22:26:29 +000016004 hash_init(&dict->dv_hashtab);
16005 dict->dv_refcount = 99999;
16006 dict_var->di_tv.vval.v_dict = dict;
16007 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016008 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016009 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16010 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011}
16012
16013/*
16014 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016015 * Frees all allocated variables and the value they contain.
16016 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 */
16018 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016019vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016020 hashtab_T *ht;
16021{
16022 vars_clear_ext(ht, TRUE);
16023}
16024
16025/*
16026 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16027 */
16028 static void
16029vars_clear_ext(ht, free_val)
16030 hashtab_T *ht;
16031 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032{
Bram Moolenaara7043832005-01-21 11:56:39 +000016033 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016034 hashitem_T *hi;
16035 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036
Bram Moolenaar33570922005-01-25 22:26:29 +000016037 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016038 todo = ht->ht_used;
16039 for (hi = ht->ht_array; todo > 0; ++hi)
16040 {
16041 if (!HASHITEM_EMPTY(hi))
16042 {
16043 --todo;
16044
Bram Moolenaar33570922005-01-25 22:26:29 +000016045 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016046 * ht_array might change then. hash_clear() takes care of it
16047 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016048 v = HI2DI(hi);
16049 if (free_val)
16050 clear_tv(&v->di_tv);
16051 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16052 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016053 }
16054 }
16055 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016056 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057}
16058
Bram Moolenaara7043832005-01-21 11:56:39 +000016059/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016060 * Delete a variable from hashtab "ht" at item "hi".
16061 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016064delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016065 hashtab_T *ht;
16066 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016067{
Bram Moolenaar33570922005-01-25 22:26:29 +000016068 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016069
16070 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016071 clear_tv(&di->di_tv);
16072 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073}
16074
16075/*
16076 * List the value of one internal variable.
16077 */
16078 static void
16079list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016080 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 char_u *prefix;
16082{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016083 char_u *tofree;
16084 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016085 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016086
Bram Moolenaar33570922005-01-25 22:26:29 +000016087 s = echo_string(&v->di_tv, &tofree, numbuf);
16088 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016089 s == NULL ? (char_u *)"" : s);
16090 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091}
16092
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093 static void
16094list_one_var_a(prefix, name, type, string)
16095 char_u *prefix;
16096 char_u *name;
16097 int type;
16098 char_u *string;
16099{
16100 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16101 if (name != NULL) /* "a:" vars don't have a name stored */
16102 msg_puts(name);
16103 msg_putchar(' ');
16104 msg_advance(22);
16105 if (type == VAR_NUMBER)
16106 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016107 else if (type == VAR_FUNC)
16108 msg_putchar('*');
16109 else if (type == VAR_LIST)
16110 {
16111 msg_putchar('[');
16112 if (*string == '[')
16113 ++string;
16114 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016115 else if (type == VAR_DICT)
16116 {
16117 msg_putchar('{');
16118 if (*string == '{')
16119 ++string;
16120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121 else
16122 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016123
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016125
16126 if (type == VAR_FUNC)
16127 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128}
16129
16130/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016131 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132 * If the variable already exists, the value is updated.
16133 * Otherwise the variable is created.
16134 */
16135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016136set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016138 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016139 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140{
Bram Moolenaar33570922005-01-25 22:26:29 +000016141 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016143 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016144 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016146 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016147 {
16148 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16149 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16150 ? name[2] : name[0]))
16151 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016152 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016153 return;
16154 }
16155 if (function_exists(name))
16156 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016157 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016158 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016159 return;
16160 }
16161 }
16162
Bram Moolenaara7043832005-01-21 11:56:39 +000016163 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016164 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016165 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016166 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016167 return;
16168 }
16169
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016170 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016171 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016173 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016174 if (var_check_ro(v->di_flags, name)
16175 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016176 return;
16177 if (v->di_tv.v_type != tv->v_type
16178 && !((v->di_tv.v_type == VAR_STRING
16179 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016180 && (tv->v_type == VAR_STRING
16181 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016182 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016183 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016184 return;
16185 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016186
16187 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016188 * Handle setting internal v: variables separately: we don't change
16189 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016190 */
16191 if (ht == &vimvarht)
16192 {
16193 if (v->di_tv.v_type == VAR_STRING)
16194 {
16195 vim_free(v->di_tv.vval.v_string);
16196 if (copy || tv->v_type != VAR_STRING)
16197 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16198 else
16199 {
16200 /* Take over the string to avoid an extra alloc/free. */
16201 v->di_tv.vval.v_string = tv->vval.v_string;
16202 tv->vval.v_string = NULL;
16203 }
16204 }
16205 else if (v->di_tv.v_type != VAR_NUMBER)
16206 EMSG2(_(e_intern2), "set_var()");
16207 else
16208 v->di_tv.vval.v_number = get_tv_number(tv);
16209 return;
16210 }
16211
16212 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213 }
16214 else /* add a new variable */
16215 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016216 /* Make sure the variable name is valid. */
16217 for (p = varname; *p != NUL; ++p)
16218 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16219 {
16220 EMSG2(_(e_illvar), varname);
16221 return;
16222 }
16223
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016224 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16225 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016226 if (v == NULL)
16227 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016228 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016229 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016231 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232 return;
16233 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016234 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016237 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016238 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016239 else
16240 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016241 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016242 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016243 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245}
16246
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016247/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016248 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16249 * Also give an error message.
16250 */
16251 static int
16252var_check_ro(flags, name)
16253 int flags;
16254 char_u *name;
16255{
16256 if (flags & DI_FLAGS_RO)
16257 {
16258 EMSG2(_(e_readonlyvar), name);
16259 return TRUE;
16260 }
16261 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16262 {
16263 EMSG2(_(e_readonlysbx), name);
16264 return TRUE;
16265 }
16266 return FALSE;
16267}
16268
16269/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016270 * Return TRUE if typeval "tv" is set to be locked (immutable).
16271 * Also give an error message, using "name".
16272 */
16273 static int
16274tv_check_lock(lock, name)
16275 int lock;
16276 char_u *name;
16277{
16278 if (lock & VAR_LOCKED)
16279 {
16280 EMSG2(_("E741: Value is locked: %s"),
16281 name == NULL ? (char_u *)_("Unknown") : name);
16282 return TRUE;
16283 }
16284 if (lock & VAR_FIXED)
16285 {
16286 EMSG2(_("E742: Cannot change value of %s"),
16287 name == NULL ? (char_u *)_("Unknown") : name);
16288 return TRUE;
16289 }
16290 return FALSE;
16291}
16292
16293/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016294 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016295 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016296 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016299copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016300 typval_T *from;
16301 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016303 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016304 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016305 switch (from->v_type)
16306 {
16307 case VAR_NUMBER:
16308 to->vval.v_number = from->vval.v_number;
16309 break;
16310 case VAR_STRING:
16311 case VAR_FUNC:
16312 if (from->vval.v_string == NULL)
16313 to->vval.v_string = NULL;
16314 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016315 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016316 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016317 if (from->v_type == VAR_FUNC)
16318 func_ref(to->vval.v_string);
16319 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016320 break;
16321 case VAR_LIST:
16322 if (from->vval.v_list == NULL)
16323 to->vval.v_list = NULL;
16324 else
16325 {
16326 to->vval.v_list = from->vval.v_list;
16327 ++to->vval.v_list->lv_refcount;
16328 }
16329 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016330 case VAR_DICT:
16331 if (from->vval.v_dict == NULL)
16332 to->vval.v_dict = NULL;
16333 else
16334 {
16335 to->vval.v_dict = from->vval.v_dict;
16336 ++to->vval.v_dict->dv_refcount;
16337 }
16338 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016339 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016340 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016341 break;
16342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016343}
16344
16345/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016346 * Make a copy of an item.
16347 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016348 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16349 * reference to an already copied list/dict can be used.
16350 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016351 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016352 static int
16353item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016354 typval_T *from;
16355 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016356 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016357 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016358{
16359 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016360 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016361
Bram Moolenaar33570922005-01-25 22:26:29 +000016362 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016363 {
16364 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016365 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016366 }
16367 ++recurse;
16368
16369 switch (from->v_type)
16370 {
16371 case VAR_NUMBER:
16372 case VAR_STRING:
16373 case VAR_FUNC:
16374 copy_tv(from, to);
16375 break;
16376 case VAR_LIST:
16377 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016378 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016379 if (from->vval.v_list == NULL)
16380 to->vval.v_list = NULL;
16381 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16382 {
16383 /* use the copy made earlier */
16384 to->vval.v_list = from->vval.v_list->lv_copylist;
16385 ++to->vval.v_list->lv_refcount;
16386 }
16387 else
16388 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16389 if (to->vval.v_list == NULL)
16390 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016391 break;
16392 case VAR_DICT:
16393 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016394 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016395 if (from->vval.v_dict == NULL)
16396 to->vval.v_dict = NULL;
16397 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16398 {
16399 /* use the copy made earlier */
16400 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16401 ++to->vval.v_dict->dv_refcount;
16402 }
16403 else
16404 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16405 if (to->vval.v_dict == NULL)
16406 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016407 break;
16408 default:
16409 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016410 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016411 }
16412 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016413 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016414}
16415
16416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417 * ":echo expr1 ..." print each argument separated with a space, add a
16418 * newline at the end.
16419 * ":echon expr1 ..." print each argument plain.
16420 */
16421 void
16422ex_echo(eap)
16423 exarg_T *eap;
16424{
16425 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016426 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016427 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428 char_u *p;
16429 int needclr = TRUE;
16430 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016431 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432
16433 if (eap->skip)
16434 ++emsg_skip;
16435 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16436 {
16437 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016438 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016439 {
16440 /*
16441 * Report the invalid expression unless the expression evaluation
16442 * has been cancelled due to an aborting error, an interrupt, or an
16443 * exception.
16444 */
16445 if (!aborting())
16446 EMSG2(_(e_invexpr2), p);
16447 break;
16448 }
16449 if (!eap->skip)
16450 {
16451 if (atstart)
16452 {
16453 atstart = FALSE;
16454 /* Call msg_start() after eval1(), evaluating the expression
16455 * may cause a message to appear. */
16456 if (eap->cmdidx == CMD_echo)
16457 msg_start();
16458 }
16459 else if (eap->cmdidx == CMD_echo)
16460 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016461 p = echo_string(&rettv, &tofree, numbuf);
16462 if (p != NULL)
16463 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016465 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016467 if (*p != TAB && needclr)
16468 {
16469 /* remove any text still there from the command */
16470 msg_clr_eos();
16471 needclr = FALSE;
16472 }
16473 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474 }
16475 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016476 {
16477#ifdef FEAT_MBYTE
16478 if (has_mbyte)
16479 {
16480 int i = (*mb_ptr2len_check)(p);
16481
16482 (void)msg_outtrans_len_attr(p, i, echo_attr);
16483 p += i - 1;
16484 }
16485 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016487 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016489 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016490 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016492 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493 arg = skipwhite(arg);
16494 }
16495 eap->nextcmd = check_nextcmd(arg);
16496
16497 if (eap->skip)
16498 --emsg_skip;
16499 else
16500 {
16501 /* remove text that may still be there from the command */
16502 if (needclr)
16503 msg_clr_eos();
16504 if (eap->cmdidx == CMD_echo)
16505 msg_end();
16506 }
16507}
16508
16509/*
16510 * ":echohl {name}".
16511 */
16512 void
16513ex_echohl(eap)
16514 exarg_T *eap;
16515{
16516 int id;
16517
16518 id = syn_name2id(eap->arg);
16519 if (id == 0)
16520 echo_attr = 0;
16521 else
16522 echo_attr = syn_id2attr(id);
16523}
16524
16525/*
16526 * ":execute expr1 ..." execute the result of an expression.
16527 * ":echomsg expr1 ..." Print a message
16528 * ":echoerr expr1 ..." Print an error
16529 * Each gets spaces around each argument and a newline at the end for
16530 * echo commands
16531 */
16532 void
16533ex_execute(eap)
16534 exarg_T *eap;
16535{
16536 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016537 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538 int ret = OK;
16539 char_u *p;
16540 garray_T ga;
16541 int len;
16542 int save_did_emsg;
16543
16544 ga_init2(&ga, 1, 80);
16545
16546 if (eap->skip)
16547 ++emsg_skip;
16548 while (*arg != NUL && *arg != '|' && *arg != '\n')
16549 {
16550 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016551 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552 {
16553 /*
16554 * Report the invalid expression unless the expression evaluation
16555 * has been cancelled due to an aborting error, an interrupt, or an
16556 * exception.
16557 */
16558 if (!aborting())
16559 EMSG2(_(e_invexpr2), p);
16560 ret = FAIL;
16561 break;
16562 }
16563
16564 if (!eap->skip)
16565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016566 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 len = (int)STRLEN(p);
16568 if (ga_grow(&ga, len + 2) == FAIL)
16569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016570 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016571 ret = FAIL;
16572 break;
16573 }
16574 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016576 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016577 ga.ga_len += len;
16578 }
16579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016580 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581 arg = skipwhite(arg);
16582 }
16583
16584 if (ret != FAIL && ga.ga_data != NULL)
16585 {
16586 if (eap->cmdidx == CMD_echomsg)
16587 MSG_ATTR(ga.ga_data, echo_attr);
16588 else if (eap->cmdidx == CMD_echoerr)
16589 {
16590 /* We don't want to abort following commands, restore did_emsg. */
16591 save_did_emsg = did_emsg;
16592 EMSG((char_u *)ga.ga_data);
16593 if (!force_abort)
16594 did_emsg = save_did_emsg;
16595 }
16596 else if (eap->cmdidx == CMD_execute)
16597 do_cmdline((char_u *)ga.ga_data,
16598 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16599 }
16600
16601 ga_clear(&ga);
16602
16603 if (eap->skip)
16604 --emsg_skip;
16605
16606 eap->nextcmd = check_nextcmd(arg);
16607}
16608
16609/*
16610 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16611 * "arg" points to the "&" or '+' when called, to "option" when returning.
16612 * Returns NULL when no option name found. Otherwise pointer to the char
16613 * after the option name.
16614 */
16615 static char_u *
16616find_option_end(arg, opt_flags)
16617 char_u **arg;
16618 int *opt_flags;
16619{
16620 char_u *p = *arg;
16621
16622 ++p;
16623 if (*p == 'g' && p[1] == ':')
16624 {
16625 *opt_flags = OPT_GLOBAL;
16626 p += 2;
16627 }
16628 else if (*p == 'l' && p[1] == ':')
16629 {
16630 *opt_flags = OPT_LOCAL;
16631 p += 2;
16632 }
16633 else
16634 *opt_flags = 0;
16635
16636 if (!ASCII_ISALPHA(*p))
16637 return NULL;
16638 *arg = p;
16639
16640 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16641 p += 4; /* termcap option */
16642 else
16643 while (ASCII_ISALPHA(*p))
16644 ++p;
16645 return p;
16646}
16647
16648/*
16649 * ":function"
16650 */
16651 void
16652ex_function(eap)
16653 exarg_T *eap;
16654{
16655 char_u *theline;
16656 int j;
16657 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659 char_u *name = NULL;
16660 char_u *p;
16661 char_u *arg;
16662 garray_T newargs;
16663 garray_T newlines;
16664 int varargs = FALSE;
16665 int mustend = FALSE;
16666 int flags = 0;
16667 ufunc_T *fp;
16668 int indent;
16669 int nesting;
16670 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016671 dictitem_T *v;
16672 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016673 static int func_nr = 0; /* number for nameless function */
16674 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016675 hashtab_T *ht;
16676 int todo;
16677 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678
16679 /*
16680 * ":function" without argument: list functions.
16681 */
16682 if (ends_excmd(*eap->arg))
16683 {
16684 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016685 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016686 todo = func_hashtab.ht_used;
16687 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016688 {
16689 if (!HASHITEM_EMPTY(hi))
16690 {
16691 --todo;
16692 fp = HI2UF(hi);
16693 if (!isdigit(*fp->uf_name))
16694 list_func_head(fp, FALSE);
16695 }
16696 }
16697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016698 eap->nextcmd = check_nextcmd(eap->arg);
16699 return;
16700 }
16701
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016702 /*
16703 * Get the function name. There are these situations:
16704 * func normal function name
16705 * "name" == func, "fudi.fd_dict" == NULL
16706 * dict.func new dictionary entry
16707 * "name" == NULL, "fudi.fd_dict" set,
16708 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16709 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016710 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016711 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16712 * dict.func existing dict entry that's not a Funcref
16713 * "name" == NULL, "fudi.fd_dict" set,
16714 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016717 name = trans_function_name(&p, eap->skip, 0, &fudi);
16718 paren = (vim_strchr(p, '(') != NULL);
16719 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720 {
16721 /*
16722 * Return on an invalid expression in braces, unless the expression
16723 * evaluation has been cancelled due to an aborting error, an
16724 * interrupt, or an exception.
16725 */
16726 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016727 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016728 if (!eap->skip && fudi.fd_newkey != NULL)
16729 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016730 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733 else
16734 eap->skip = TRUE;
16735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736 /* An error in a function call during evaluation of an expression in magic
16737 * braces should not cause the function not to be defined. */
16738 saved_did_emsg = did_emsg;
16739 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740
16741 /*
16742 * ":function func" with only function name: list function.
16743 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016744 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 {
16746 if (!ends_excmd(*skipwhite(p)))
16747 {
16748 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016749 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750 }
16751 eap->nextcmd = check_nextcmd(p);
16752 if (eap->nextcmd != NULL)
16753 *p = NUL;
16754 if (!eap->skip && !got_int)
16755 {
16756 fp = find_func(name);
16757 if (fp != NULL)
16758 {
16759 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016760 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761 {
16762 msg_putchar('\n');
16763 msg_outnum((long)(j + 1));
16764 if (j < 9)
16765 msg_putchar(' ');
16766 if (j < 99)
16767 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016768 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769 out_flush(); /* show a line at a time */
16770 ui_breakcheck();
16771 }
16772 if (!got_int)
16773 {
16774 msg_putchar('\n');
16775 msg_puts((char_u *)" endfunction");
16776 }
16777 }
16778 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016779 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016780 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016781 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016782 }
16783
16784 /*
16785 * ":function name(arg1, arg2)" Define function.
16786 */
16787 p = skipwhite(p);
16788 if (*p != '(')
16789 {
16790 if (!eap->skip)
16791 {
16792 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016793 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794 }
16795 /* attempt to continue by skipping some text */
16796 if (vim_strchr(p, '(') != NULL)
16797 p = vim_strchr(p, '(');
16798 }
16799 p = skipwhite(p + 1);
16800
16801 ga_init2(&newargs, (int)sizeof(char_u *), 3);
16802 ga_init2(&newlines, (int)sizeof(char_u *), 3);
16803
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016804 if (!eap->skip)
16805 {
16806 /* Check the name of the function. */
16807 if (name != NULL)
16808 arg = name;
16809 else
16810 arg = fudi.fd_newkey;
16811 if (arg != NULL)
16812 {
16813 if (*arg == K_SPECIAL)
16814 j = 3;
16815 else
16816 j = 0;
16817 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
16818 : eval_isnamec(arg[j])))
16819 ++j;
16820 if (arg[j] != NUL)
16821 emsg_funcname(_(e_invarg2), arg);
16822 }
16823 }
16824
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 /*
16826 * Isolate the arguments: "arg1, arg2, ...)"
16827 */
16828 while (*p != ')')
16829 {
16830 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
16831 {
16832 varargs = TRUE;
16833 p += 3;
16834 mustend = TRUE;
16835 }
16836 else
16837 {
16838 arg = p;
16839 while (ASCII_ISALNUM(*p) || *p == '_')
16840 ++p;
16841 if (arg == p || isdigit(*arg)
16842 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
16843 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
16844 {
16845 if (!eap->skip)
16846 EMSG2(_("E125: Illegal argument: %s"), arg);
16847 break;
16848 }
16849 if (ga_grow(&newargs, 1) == FAIL)
16850 goto erret;
16851 c = *p;
16852 *p = NUL;
16853 arg = vim_strsave(arg);
16854 if (arg == NULL)
16855 goto erret;
16856 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
16857 *p = c;
16858 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859 if (*p == ',')
16860 ++p;
16861 else
16862 mustend = TRUE;
16863 }
16864 p = skipwhite(p);
16865 if (mustend && *p != ')')
16866 {
16867 if (!eap->skip)
16868 EMSG2(_(e_invarg2), eap->arg);
16869 break;
16870 }
16871 }
16872 ++p; /* skip the ')' */
16873
Bram Moolenaare9a41262005-01-15 22:18:47 +000016874 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 for (;;)
16876 {
16877 p = skipwhite(p);
16878 if (STRNCMP(p, "range", 5) == 0)
16879 {
16880 flags |= FC_RANGE;
16881 p += 5;
16882 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016883 else if (STRNCMP(p, "dict", 4) == 0)
16884 {
16885 flags |= FC_DICT;
16886 p += 4;
16887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016888 else if (STRNCMP(p, "abort", 5) == 0)
16889 {
16890 flags |= FC_ABORT;
16891 p += 5;
16892 }
16893 else
16894 break;
16895 }
16896
16897 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
16898 EMSG(_(e_trailing));
16899
16900 /*
16901 * Read the body of the function, until ":endfunction" is found.
16902 */
16903 if (KeyTyped)
16904 {
16905 /* Check if the function already exists, don't let the user type the
16906 * whole function before telling him it doesn't work! For a script we
16907 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016908 if (!eap->skip && !eap->forceit)
16909 {
16910 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
16911 EMSG(_(e_funcdict));
16912 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016913 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016916 if (!eap->skip && did_emsg)
16917 goto erret;
16918
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919 msg_putchar('\n'); /* don't overwrite the function name */
16920 cmdline_row = msg_row;
16921 }
16922
16923 indent = 2;
16924 nesting = 0;
16925 for (;;)
16926 {
16927 msg_scroll = TRUE;
16928 need_wait_return = FALSE;
16929 if (eap->getline == NULL)
16930 theline = getcmdline(':', 0L, indent);
16931 else
16932 theline = eap->getline(':', eap->cookie, indent);
16933 if (KeyTyped)
16934 lines_left = Rows - 1;
16935 if (theline == NULL)
16936 {
16937 EMSG(_("E126: Missing :endfunction"));
16938 goto erret;
16939 }
16940
16941 if (skip_until != NULL)
16942 {
16943 /* between ":append" and "." and between ":python <<EOF" and "EOF"
16944 * don't check for ":endfunc". */
16945 if (STRCMP(theline, skip_until) == 0)
16946 {
16947 vim_free(skip_until);
16948 skip_until = NULL;
16949 }
16950 }
16951 else
16952 {
16953 /* skip ':' and blanks*/
16954 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
16955 ;
16956
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016957 /* Check for "endfunction". */
16958 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959 {
16960 vim_free(theline);
16961 break;
16962 }
16963
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016964 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000016965 * at "end". */
16966 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
16967 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016968 else if (STRNCMP(p, "if", 2) == 0
16969 || STRNCMP(p, "wh", 2) == 0
16970 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971 || STRNCMP(p, "try", 3) == 0)
16972 indent += 2;
16973
16974 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016975 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016977 if (*p == '!')
16978 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 p += eval_fname_script(p);
16980 if (ASCII_ISALPHA(*p))
16981 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016982 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 if (*skipwhite(p) == '(')
16984 {
16985 ++nesting;
16986 indent += 2;
16987 }
16988 }
16989 }
16990
16991 /* Check for ":append" or ":insert". */
16992 p = skip_range(p, NULL);
16993 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
16994 || (p[0] == 'i'
16995 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
16996 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
16997 skip_until = vim_strsave((char_u *)".");
16998
16999 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17000 arg = skipwhite(skiptowhite(p));
17001 if (arg[0] == '<' && arg[1] =='<'
17002 && ((p[0] == 'p' && p[1] == 'y'
17003 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17004 || (p[0] == 'p' && p[1] == 'e'
17005 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17006 || (p[0] == 't' && p[1] == 'c'
17007 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17008 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17009 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017010 || (p[0] == 'm' && p[1] == 'z'
17011 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012 ))
17013 {
17014 /* ":python <<" continues until a dot, like ":append" */
17015 p = skipwhite(arg + 2);
17016 if (*p == NUL)
17017 skip_until = vim_strsave((char_u *)".");
17018 else
17019 skip_until = vim_strsave(p);
17020 }
17021 }
17022
17023 /* Add the line to the function. */
17024 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017025 {
17026 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017028 }
17029
17030 /* Copy the line to newly allocated memory. get_one_sourceline()
17031 * allocates 250 bytes per line, this saves 80% on average. The cost
17032 * is an extra alloc/free. */
17033 p = vim_strsave(theline);
17034 if (p != NULL)
17035 {
17036 vim_free(theline);
17037 theline = p;
17038 }
17039
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17041 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 }
17043
17044 /* Don't define the function when skipping commands or when an error was
17045 * detected. */
17046 if (eap->skip || did_emsg)
17047 goto erret;
17048
17049 /*
17050 * If there are no errors, add the function
17051 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017052 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017053 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017054 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017055 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017056 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017057 emsg_funcname("E707: Function name conflicts with variable: %s",
17058 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017059 goto erret;
17060 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017061
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017062 fp = find_func(name);
17063 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017065 if (!eap->forceit)
17066 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017067 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017068 goto erret;
17069 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017070 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017071 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017072 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017073 name);
17074 goto erret;
17075 }
17076 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017077 ga_clear_strings(&(fp->uf_args));
17078 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017079 vim_free(name);
17080 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082 }
17083 else
17084 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017085 char numbuf[20];
17086
17087 fp = NULL;
17088 if (fudi.fd_newkey == NULL && !eap->forceit)
17089 {
17090 EMSG(_(e_funcdict));
17091 goto erret;
17092 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017093 if (fudi.fd_di == NULL)
17094 {
17095 /* Can't add a function to a locked dictionary */
17096 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17097 goto erret;
17098 }
17099 /* Can't change an existing function if it is locked */
17100 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17101 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017102
17103 /* Give the function a sequential number. Can only be used with a
17104 * Funcref! */
17105 vim_free(name);
17106 sprintf(numbuf, "%d", ++func_nr);
17107 name = vim_strsave((char_u *)numbuf);
17108 if (name == NULL)
17109 goto erret;
17110 }
17111
17112 if (fp == NULL)
17113 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017114 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017115 {
17116 int slen, plen;
17117 char_u *scriptname;
17118
17119 /* Check that the autoload name matches the script name. */
17120 j = FAIL;
17121 if (sourcing_name != NULL)
17122 {
17123 scriptname = autoload_name(name);
17124 if (scriptname != NULL)
17125 {
17126 p = vim_strchr(scriptname, '/');
17127 plen = STRLEN(p);
17128 slen = STRLEN(sourcing_name);
17129 if (slen > plen && fnamecmp(p,
17130 sourcing_name + slen - plen) == 0)
17131 j = OK;
17132 vim_free(scriptname);
17133 }
17134 }
17135 if (j == FAIL)
17136 {
17137 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17138 goto erret;
17139 }
17140 }
17141
17142 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143 if (fp == NULL)
17144 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017145
17146 if (fudi.fd_dict != NULL)
17147 {
17148 if (fudi.fd_di == NULL)
17149 {
17150 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017151 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017152 if (fudi.fd_di == NULL)
17153 {
17154 vim_free(fp);
17155 goto erret;
17156 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017157 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17158 {
17159 vim_free(fudi.fd_di);
17160 goto erret;
17161 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017162 }
17163 else
17164 /* overwrite existing dict entry */
17165 clear_tv(&fudi.fd_di->di_tv);
17166 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017167 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017168 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017169 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017170 }
17171
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017173 STRCPY(fp->uf_name, name);
17174 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017176 fp->uf_args = newargs;
17177 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017178#ifdef FEAT_PROFILE
17179 fp->uf_tml_count = NULL;
17180 fp->uf_tml_total = NULL;
17181 fp->uf_tml_self = NULL;
17182 fp->uf_profiling = FALSE;
17183 if (prof_def_func())
17184 func_do_profile(fp);
17185#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017186 fp->uf_varargs = varargs;
17187 fp->uf_flags = flags;
17188 fp->uf_calls = 0;
17189 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017190 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191
17192erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 ga_clear_strings(&newargs);
17194 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017195ret_free:
17196 vim_free(skip_until);
17197 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200}
17201
17202/*
17203 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017204 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017206 * flags:
17207 * TFN_INT: internal function name OK
17208 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 * Advances "pp" to just after the function name (if no error).
17210 */
17211 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017212trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 char_u **pp;
17214 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017215 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017216 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017218 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219 char_u *start;
17220 char_u *end;
17221 int lead;
17222 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017224 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017225
17226 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017227 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017229
17230 /* Check for hard coded <SNR>: already translated function ID (from a user
17231 * command). */
17232 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17233 && (*pp)[2] == (int)KE_SNR)
17234 {
17235 *pp += 3;
17236 len = get_id_len(pp) + 3;
17237 return vim_strnsave(start, len);
17238 }
17239
17240 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17241 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017243 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017245
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017246 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17247 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017248 if (end == start)
17249 {
17250 if (!skip)
17251 EMSG(_("E129: Function name required"));
17252 goto theend;
17253 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017254 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017255 {
17256 /*
17257 * Report an invalid expression in braces, unless the expression
17258 * evaluation has been cancelled due to an aborting error, an
17259 * interrupt, or an exception.
17260 */
17261 if (!aborting())
17262 {
17263 if (end != NULL)
17264 EMSG2(_(e_invarg2), start);
17265 }
17266 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017267 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017268 goto theend;
17269 }
17270
17271 if (lv.ll_tv != NULL)
17272 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017273 if (fdp != NULL)
17274 {
17275 fdp->fd_dict = lv.ll_dict;
17276 fdp->fd_newkey = lv.ll_newkey;
17277 lv.ll_newkey = NULL;
17278 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017279 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017280 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17281 {
17282 name = vim_strsave(lv.ll_tv->vval.v_string);
17283 *pp = end;
17284 }
17285 else
17286 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017287 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17288 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017289 EMSG(_(e_funcref));
17290 else
17291 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017292 name = NULL;
17293 }
17294 goto theend;
17295 }
17296
17297 if (lv.ll_name == NULL)
17298 {
17299 /* Error found, but continue after the function name. */
17300 *pp = end;
17301 goto theend;
17302 }
17303
17304 if (lv.ll_exp_name != NULL)
17305 len = STRLEN(lv.ll_exp_name);
17306 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017307 {
17308 if (lead == 2) /* skip over "s:" */
17309 lv.ll_name += 2;
17310 len = (int)(end - lv.ll_name);
17311 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017312
17313 /*
17314 * Copy the function name to allocated memory.
17315 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17316 * Accept <SNR>123_name() outside a script.
17317 */
17318 if (skip)
17319 lead = 0; /* do nothing */
17320 else if (lead > 0)
17321 {
17322 lead = 3;
17323 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17324 {
17325 if (current_SID <= 0)
17326 {
17327 EMSG(_(e_usingsid));
17328 goto theend;
17329 }
17330 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17331 lead += (int)STRLEN(sid_buf);
17332 }
17333 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017334 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017335 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017336 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017337 goto theend;
17338 }
17339 name = alloc((unsigned)(len + lead + 1));
17340 if (name != NULL)
17341 {
17342 if (lead > 0)
17343 {
17344 name[0] = K_SPECIAL;
17345 name[1] = KS_EXTRA;
17346 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017347 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017348 STRCPY(name + 3, sid_buf);
17349 }
17350 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17351 name[len + lead] = NUL;
17352 }
17353 *pp = end;
17354
17355theend:
17356 clear_lval(&lv);
17357 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358}
17359
17360/*
17361 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17362 * Return 2 if "p" starts with "s:".
17363 * Return 0 otherwise.
17364 */
17365 static int
17366eval_fname_script(p)
17367 char_u *p;
17368{
17369 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17370 || STRNICMP(p + 1, "SNR>", 4) == 0))
17371 return 5;
17372 if (p[0] == 's' && p[1] == ':')
17373 return 2;
17374 return 0;
17375}
17376
17377/*
17378 * Return TRUE if "p" starts with "<SID>" or "s:".
17379 * Only works if eval_fname_script() returned non-zero for "p"!
17380 */
17381 static int
17382eval_fname_sid(p)
17383 char_u *p;
17384{
17385 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17386}
17387
17388/*
17389 * List the head of the function: "name(arg1, arg2)".
17390 */
17391 static void
17392list_func_head(fp, indent)
17393 ufunc_T *fp;
17394 int indent;
17395{
17396 int j;
17397
17398 msg_start();
17399 if (indent)
17400 MSG_PUTS(" ");
17401 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017402 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 {
17404 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017405 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 }
17407 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017408 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017410 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411 {
17412 if (j)
17413 MSG_PUTS(", ");
17414 msg_puts(FUNCARG(fp, j));
17415 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017416 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417 {
17418 if (j)
17419 MSG_PUTS(", ");
17420 MSG_PUTS("...");
17421 }
17422 msg_putchar(')');
17423}
17424
17425/*
17426 * Find a function by name, return pointer to it in ufuncs.
17427 * Return NULL for unknown function.
17428 */
17429 static ufunc_T *
17430find_func(name)
17431 char_u *name;
17432{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017433 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017435 hi = hash_find(&func_hashtab, name);
17436 if (!HASHITEM_EMPTY(hi))
17437 return HI2UF(hi);
17438 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439}
17440
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017441#if defined(EXITFREE) || defined(PROTO)
17442 void
17443free_all_functions()
17444{
17445 hashitem_T *hi;
17446
17447 /* Need to start all over every time, because func_free() may change the
17448 * hash table. */
17449 while (func_hashtab.ht_used > 0)
17450 for (hi = func_hashtab.ht_array; ; ++hi)
17451 if (!HASHITEM_EMPTY(hi))
17452 {
17453 func_free(HI2UF(hi));
17454 break;
17455 }
17456}
17457#endif
17458
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017459/*
17460 * Return TRUE if a function "name" exists.
17461 */
17462 static int
17463function_exists(name)
17464 char_u *name;
17465{
17466 char_u *p = name;
17467 int n = FALSE;
17468
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017469 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017470 if (p != NULL)
17471 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017472 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017473 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017474 else
17475 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017476 vim_free(p);
17477 }
17478 return n;
17479}
17480
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017481/*
17482 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017483 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017484 */
17485 static int
17486builtin_function(name)
17487 char_u *name;
17488{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017489 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17490 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017491}
17492
Bram Moolenaar05159a02005-02-26 23:04:13 +000017493#if defined(FEAT_PROFILE) || defined(PROTO)
17494/*
17495 * Start profiling function "fp".
17496 */
17497 static void
17498func_do_profile(fp)
17499 ufunc_T *fp;
17500{
17501 fp->uf_tm_count = 0;
17502 profile_zero(&fp->uf_tm_self);
17503 profile_zero(&fp->uf_tm_total);
17504 if (fp->uf_tml_count == NULL)
17505 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17506 (sizeof(int) * fp->uf_lines.ga_len));
17507 if (fp->uf_tml_total == NULL)
17508 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17509 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17510 if (fp->uf_tml_self == NULL)
17511 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17512 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17513 fp->uf_tml_idx = -1;
17514 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17515 || fp->uf_tml_self == NULL)
17516 return; /* out of memory */
17517
17518 fp->uf_profiling = TRUE;
17519}
17520
17521/*
17522 * Dump the profiling results for all functions in file "fd".
17523 */
17524 void
17525func_dump_profile(fd)
17526 FILE *fd;
17527{
17528 hashitem_T *hi;
17529 int todo;
17530 ufunc_T *fp;
17531 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017532 ufunc_T **sorttab;
17533 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017534
17535 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017536 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17537
Bram Moolenaar05159a02005-02-26 23:04:13 +000017538 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17539 {
17540 if (!HASHITEM_EMPTY(hi))
17541 {
17542 --todo;
17543 fp = HI2UF(hi);
17544 if (fp->uf_profiling)
17545 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017546 if (sorttab != NULL)
17547 sorttab[st_len++] = fp;
17548
Bram Moolenaar05159a02005-02-26 23:04:13 +000017549 if (fp->uf_name[0] == K_SPECIAL)
17550 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17551 else
17552 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17553 if (fp->uf_tm_count == 1)
17554 fprintf(fd, "Called 1 time\n");
17555 else
17556 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17557 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17558 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17559 fprintf(fd, "\n");
17560 fprintf(fd, "count total (s) self (s)\n");
17561
17562 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17563 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017564 prof_func_line(fd, fp->uf_tml_count[i],
17565 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017566 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17567 }
17568 fprintf(fd, "\n");
17569 }
17570 }
17571 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017572
17573 if (sorttab != NULL && st_len > 0)
17574 {
17575 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17576 prof_total_cmp);
17577 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17578 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17579 prof_self_cmp);
17580 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17581 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017582}
Bram Moolenaar73830342005-02-28 22:48:19 +000017583
17584 static void
17585prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17586 FILE *fd;
17587 ufunc_T **sorttab;
17588 int st_len;
17589 char *title;
17590 int prefer_self; /* when equal print only self time */
17591{
17592 int i;
17593 ufunc_T *fp;
17594
17595 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17596 fprintf(fd, "count total (s) self (s) function\n");
17597 for (i = 0; i < 20 && i < st_len; ++i)
17598 {
17599 fp = sorttab[i];
17600 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17601 prefer_self);
17602 if (fp->uf_name[0] == K_SPECIAL)
17603 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17604 else
17605 fprintf(fd, " %s()\n", fp->uf_name);
17606 }
17607 fprintf(fd, "\n");
17608}
17609
17610/*
17611 * Print the count and times for one function or function line.
17612 */
17613 static void
17614prof_func_line(fd, count, total, self, prefer_self)
17615 FILE *fd;
17616 int count;
17617 proftime_T *total;
17618 proftime_T *self;
17619 int prefer_self; /* when equal print only self time */
17620{
17621 if (count > 0)
17622 {
17623 fprintf(fd, "%5d ", count);
17624 if (prefer_self && profile_equal(total, self))
17625 fprintf(fd, " ");
17626 else
17627 fprintf(fd, "%s ", profile_msg(total));
17628 if (!prefer_self && profile_equal(total, self))
17629 fprintf(fd, " ");
17630 else
17631 fprintf(fd, "%s ", profile_msg(self));
17632 }
17633 else
17634 fprintf(fd, " ");
17635}
17636
17637/*
17638 * Compare function for total time sorting.
17639 */
17640 static int
17641#ifdef __BORLANDC__
17642_RTLENTRYF
17643#endif
17644prof_total_cmp(s1, s2)
17645 const void *s1;
17646 const void *s2;
17647{
17648 ufunc_T *p1, *p2;
17649
17650 p1 = *(ufunc_T **)s1;
17651 p2 = *(ufunc_T **)s2;
17652 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17653}
17654
17655/*
17656 * Compare function for self time sorting.
17657 */
17658 static int
17659#ifdef __BORLANDC__
17660_RTLENTRYF
17661#endif
17662prof_self_cmp(s1, s2)
17663 const void *s1;
17664 const void *s2;
17665{
17666 ufunc_T *p1, *p2;
17667
17668 p1 = *(ufunc_T **)s1;
17669 p2 = *(ufunc_T **)s2;
17670 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17671}
17672
Bram Moolenaar05159a02005-02-26 23:04:13 +000017673#endif
17674
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017675/* The names of packages that once were loaded is remembered. */
17676static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
17677
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017678/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017679 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017680 * Return TRUE if a package was loaded.
17681 */
17682 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017683script_autoload(name, reload)
17684 char_u *name;
17685 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017686{
17687 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017688 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017689 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017690 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017691
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017692 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017693 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017694 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017695 return FALSE;
17696
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017697 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017698
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017699 /* Find the name in the list of previously loaded package names. Skip
17700 * "autoload/", it's always the same. */
17701 for (i = 0; i < ga_loaded.ga_len; ++i)
17702 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
17703 break;
17704 if (!reload && i < ga_loaded.ga_len)
17705 ret = FALSE; /* was loaded already */
17706 else
17707 {
17708 /* Remember the name if it wasn't loaded already. */
17709 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
17710 {
17711 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
17712 tofree = NULL;
17713 }
17714
17715 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
17716 if (cmd_runtime(scriptname, FALSE) == OK)
17717 ret = TRUE;
17718 }
17719
17720 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017721 return ret;
17722}
17723
17724/*
17725 * Return the autoload script name for a function or variable name.
17726 * Returns NULL when out of memory.
17727 */
17728 static char_u *
17729autoload_name(name)
17730 char_u *name;
17731{
17732 char_u *p;
17733 char_u *scriptname;
17734
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017735 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017736 scriptname = alloc((unsigned)(STRLEN(name) + 14));
17737 if (scriptname == NULL)
17738 return FALSE;
17739 STRCPY(scriptname, "autoload/");
17740 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017741 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017742 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017743 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017744 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017745 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017746}
17747
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
17749
17750/*
17751 * Function given to ExpandGeneric() to obtain the list of user defined
17752 * function names.
17753 */
17754 char_u *
17755get_user_func_name(xp, idx)
17756 expand_T *xp;
17757 int idx;
17758{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017759 static long_u done;
17760 static hashitem_T *hi;
17761 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762
17763 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017765 done = 0;
17766 hi = func_hashtab.ht_array;
17767 }
17768 if (done < func_hashtab.ht_used)
17769 {
17770 if (done++ > 0)
17771 ++hi;
17772 while (HASHITEM_EMPTY(hi))
17773 ++hi;
17774 fp = HI2UF(hi);
17775
17776 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
17777 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778
17779 cat_func_name(IObuff, fp);
17780 if (xp->xp_context != EXPAND_USER_FUNC)
17781 {
17782 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017783 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784 STRCAT(IObuff, ")");
17785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786 return IObuff;
17787 }
17788 return NULL;
17789}
17790
17791#endif /* FEAT_CMDL_COMPL */
17792
17793/*
17794 * Copy the function name of "fp" to buffer "buf".
17795 * "buf" must be able to hold the function name plus three bytes.
17796 * Takes care of script-local function names.
17797 */
17798 static void
17799cat_func_name(buf, fp)
17800 char_u *buf;
17801 ufunc_T *fp;
17802{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017803 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804 {
17805 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017806 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807 }
17808 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017809 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810}
17811
17812/*
17813 * ":delfunction {name}"
17814 */
17815 void
17816ex_delfunction(eap)
17817 exarg_T *eap;
17818{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017819 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017820 char_u *p;
17821 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017822 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823
17824 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017825 name = trans_function_name(&p, eap->skip, 0, &fudi);
17826 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017828 {
17829 if (fudi.fd_dict != NULL && !eap->skip)
17830 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833 if (!ends_excmd(*skipwhite(p)))
17834 {
17835 vim_free(name);
17836 EMSG(_(e_trailing));
17837 return;
17838 }
17839 eap->nextcmd = check_nextcmd(p);
17840 if (eap->nextcmd != NULL)
17841 *p = NUL;
17842
17843 if (!eap->skip)
17844 fp = find_func(name);
17845 vim_free(name);
17846
17847 if (!eap->skip)
17848 {
17849 if (fp == NULL)
17850 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017851 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 return;
17853 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017854 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 {
17856 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
17857 return;
17858 }
17859
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017860 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017862 /* Delete the dict item that refers to the function, it will
17863 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017864 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017866 else
17867 func_free(fp);
17868 }
17869}
17870
17871/*
17872 * Free a function and remove it from the list of functions.
17873 */
17874 static void
17875func_free(fp)
17876 ufunc_T *fp;
17877{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017878 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017879
17880 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017881 ga_clear_strings(&(fp->uf_args));
17882 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000017883#ifdef FEAT_PROFILE
17884 vim_free(fp->uf_tml_count);
17885 vim_free(fp->uf_tml_total);
17886 vim_free(fp->uf_tml_self);
17887#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017888
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017889 /* remove the function from the function hashtable */
17890 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
17891 if (HASHITEM_EMPTY(hi))
17892 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017893 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017894 hash_remove(&func_hashtab, hi);
17895
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017896 vim_free(fp);
17897}
17898
17899/*
17900 * Unreference a Function: decrement the reference count and free it when it
17901 * becomes zero. Only for numbered functions.
17902 */
17903 static void
17904func_unref(name)
17905 char_u *name;
17906{
17907 ufunc_T *fp;
17908
17909 if (name != NULL && isdigit(*name))
17910 {
17911 fp = find_func(name);
17912 if (fp == NULL)
17913 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017914 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017915 {
17916 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017917 * when "uf_calls" becomes zero. */
17918 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017919 func_free(fp);
17920 }
17921 }
17922}
17923
17924/*
17925 * Count a reference to a Function.
17926 */
17927 static void
17928func_ref(name)
17929 char_u *name;
17930{
17931 ufunc_T *fp;
17932
17933 if (name != NULL && isdigit(*name))
17934 {
17935 fp = find_func(name);
17936 if (fp == NULL)
17937 EMSG2(_(e_intern2), "func_ref()");
17938 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017939 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940 }
17941}
17942
17943/*
17944 * Call a user function.
17945 */
17946 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000017947call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948 ufunc_T *fp; /* pointer to function */
17949 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000017950 typval_T *argvars; /* arguments */
17951 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952 linenr_T firstline; /* first line of range */
17953 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000017954 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955{
Bram Moolenaar33570922005-01-25 22:26:29 +000017956 char_u *save_sourcing_name;
17957 linenr_T save_sourcing_lnum;
17958 scid_T save_current_SID;
17959 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000017960 int save_did_emsg;
17961 static int depth = 0;
17962 dictitem_T *v;
17963 int fixvar_idx = 0; /* index in fixvar[] */
17964 int i;
17965 int ai;
17966 char_u numbuf[NUMBUFLEN];
17967 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017968#ifdef FEAT_PROFILE
17969 proftime_T wait_start;
17970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971
17972 /* If depth of calling is getting too high, don't execute the function */
17973 if (depth >= p_mfd)
17974 {
17975 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017976 rettv->v_type = VAR_NUMBER;
17977 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978 return;
17979 }
17980 ++depth;
17981
17982 line_breakcheck(); /* check for CTRL-C hit */
17983
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017984 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000017985 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017987 fc.rettv = rettv;
17988 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989 fc.linenr = 0;
17990 fc.returned = FALSE;
17991 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017993 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994 fc.dbg_tick = debug_tick;
17995
Bram Moolenaar33570922005-01-25 22:26:29 +000017996 /*
17997 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
17998 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
17999 * each argument variable and saves a lot of time.
18000 */
18001 /*
18002 * Init l: variables.
18003 */
18004 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018005 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018006 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018007 /* Set l:self to "selfdict". */
18008 v = &fc.fixvar[fixvar_idx++].var;
18009 STRCPY(v->di_key, "self");
18010 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18011 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18012 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018013 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018014 v->di_tv.vval.v_dict = selfdict;
18015 ++selfdict->dv_refcount;
18016 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018017
Bram Moolenaar33570922005-01-25 22:26:29 +000018018 /*
18019 * Init a: variables.
18020 * Set a:0 to "argcount".
18021 * Set a:000 to a list with room for the "..." arguments.
18022 */
18023 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18024 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018025 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018026 v = &fc.fixvar[fixvar_idx++].var;
18027 STRCPY(v->di_key, "000");
18028 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18029 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18030 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018031 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018032 v->di_tv.vval.v_list = &fc.l_varlist;
18033 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18034 fc.l_varlist.lv_refcount = 99999;
18035
18036 /*
18037 * Set a:firstline to "firstline" and a:lastline to "lastline".
18038 * Set a:name to named arguments.
18039 * Set a:N to the "..." arguments.
18040 */
18041 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18042 (varnumber_T)firstline);
18043 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18044 (varnumber_T)lastline);
18045 for (i = 0; i < argcount; ++i)
18046 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018047 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018048 if (ai < 0)
18049 /* named argument a:name */
18050 name = FUNCARG(fp, i);
18051 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018052 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018053 /* "..." argument a:1, a:2, etc. */
18054 sprintf((char *)numbuf, "%d", ai + 1);
18055 name = numbuf;
18056 }
18057 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18058 {
18059 v = &fc.fixvar[fixvar_idx++].var;
18060 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18061 }
18062 else
18063 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018064 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18065 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018066 if (v == NULL)
18067 break;
18068 v->di_flags = DI_FLAGS_RO;
18069 }
18070 STRCPY(v->di_key, name);
18071 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18072
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018073 /* Note: the values are copied directly to avoid alloc/free.
18074 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018075 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018076 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018077
18078 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18079 {
18080 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18081 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018082 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018083 }
18084 }
18085
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 /* Don't redraw while executing the function. */
18087 ++RedrawingDisabled;
18088 save_sourcing_name = sourcing_name;
18089 save_sourcing_lnum = sourcing_lnum;
18090 sourcing_lnum = 1;
18091 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018092 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093 if (sourcing_name != NULL)
18094 {
18095 if (save_sourcing_name != NULL
18096 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18097 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18098 else
18099 STRCPY(sourcing_name, "function ");
18100 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18101
18102 if (p_verbose >= 12)
18103 {
18104 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018105 verbose_enter_scroll();
18106
Bram Moolenaar555b2802005-05-19 21:08:39 +000018107 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 if (p_verbose >= 14)
18109 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018111 char_u numbuf[NUMBUFLEN];
18112 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113
18114 msg_puts((char_u *)"(");
18115 for (i = 0; i < argcount; ++i)
18116 {
18117 if (i > 0)
18118 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018119 if (argvars[i].v_type == VAR_NUMBER)
18120 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121 else
18122 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018123 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018124 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018126 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127 }
18128 }
18129 msg_puts((char_u *)")");
18130 }
18131 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018132
18133 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134 --no_wait_return;
18135 }
18136 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018137#ifdef FEAT_PROFILE
18138 if (do_profiling)
18139 {
18140 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18141 func_do_profile(fp);
18142 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018143 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018144 {
18145 ++fp->uf_tm_count;
18146 profile_start(&fp->uf_tm_start);
18147 profile_zero(&fp->uf_tm_children);
18148 }
18149 script_prof_save(&wait_start);
18150 }
18151#endif
18152
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018154 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155 save_did_emsg = did_emsg;
18156 did_emsg = FALSE;
18157
18158 /* call do_cmdline() to execute the lines */
18159 do_cmdline(NULL, get_func_line, (void *)&fc,
18160 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18161
18162 --RedrawingDisabled;
18163
18164 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018165 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018167 clear_tv(rettv);
18168 rettv->v_type = VAR_NUMBER;
18169 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 }
18171
Bram Moolenaar05159a02005-02-26 23:04:13 +000018172#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018173 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018174 {
18175 profile_end(&fp->uf_tm_start);
18176 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18177 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18178 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18179 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018180 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018181 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018182 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18183 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018184 }
18185 }
18186#endif
18187
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188 /* when being verbose, mention the return value */
18189 if (p_verbose >= 12)
18190 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018192 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018193
Bram Moolenaar071d4272004-06-13 20:20:40 +000018194 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018195 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018196 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018197 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18198 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018199 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018201 char_u buf[MSG_BUF_LEN];
18202 char_u numbuf[NUMBUFLEN];
18203 char_u *tofree;
18204
Bram Moolenaar555b2802005-05-19 21:08:39 +000018205 /* The value may be very long. Skip the middle part, so that we
18206 * have some idea how it starts and ends. smsg() would always
18207 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018208 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018209 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018210 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018211 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 }
18213 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018214
18215 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 --no_wait_return;
18217 }
18218
18219 vim_free(sourcing_name);
18220 sourcing_name = save_sourcing_name;
18221 sourcing_lnum = save_sourcing_lnum;
18222 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018223#ifdef FEAT_PROFILE
18224 if (do_profiling)
18225 script_prof_restore(&wait_start);
18226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227
18228 if (p_verbose >= 12 && sourcing_name != NULL)
18229 {
18230 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018231 verbose_enter_scroll();
18232
Bram Moolenaar555b2802005-05-19 21:08:39 +000018233 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018235
18236 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237 --no_wait_return;
18238 }
18239
18240 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018241 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242
Bram Moolenaar33570922005-01-25 22:26:29 +000018243 /* The a: variables typevals were not alloced, only free the allocated
18244 * variables. */
18245 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18246
18247 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248 --depth;
18249}
18250
18251/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018252 * Add a number variable "name" to dict "dp" with value "nr".
18253 */
18254 static void
18255add_nr_var(dp, v, name, nr)
18256 dict_T *dp;
18257 dictitem_T *v;
18258 char *name;
18259 varnumber_T nr;
18260{
18261 STRCPY(v->di_key, name);
18262 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18263 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18264 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018265 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018266 v->di_tv.vval.v_number = nr;
18267}
18268
18269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270 * ":return [expr]"
18271 */
18272 void
18273ex_return(eap)
18274 exarg_T *eap;
18275{
18276 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018277 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278 int returning = FALSE;
18279
18280 if (current_funccal == NULL)
18281 {
18282 EMSG(_("E133: :return not inside a function"));
18283 return;
18284 }
18285
18286 if (eap->skip)
18287 ++emsg_skip;
18288
18289 eap->nextcmd = NULL;
18290 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018291 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292 {
18293 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018294 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018296 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297 }
18298 /* It's safer to return also on error. */
18299 else if (!eap->skip)
18300 {
18301 /*
18302 * Return unless the expression evaluation has been cancelled due to an
18303 * aborting error, an interrupt, or an exception.
18304 */
18305 if (!aborting())
18306 returning = do_return(eap, FALSE, TRUE, NULL);
18307 }
18308
18309 /* When skipping or the return gets pending, advance to the next command
18310 * in this line (!returning). Otherwise, ignore the rest of the line.
18311 * Following lines will be ignored by get_func_line(). */
18312 if (returning)
18313 eap->nextcmd = NULL;
18314 else if (eap->nextcmd == NULL) /* no argument */
18315 eap->nextcmd = check_nextcmd(arg);
18316
18317 if (eap->skip)
18318 --emsg_skip;
18319}
18320
18321/*
18322 * Return from a function. Possibly makes the return pending. Also called
18323 * for a pending return at the ":endtry" or after returning from an extra
18324 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018325 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018326 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 * FALSE when the return gets pending.
18328 */
18329 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018330do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331 exarg_T *eap;
18332 int reanimate;
18333 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018334 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335{
18336 int idx;
18337 struct condstack *cstack = eap->cstack;
18338
18339 if (reanimate)
18340 /* Undo the return. */
18341 current_funccal->returned = FALSE;
18342
18343 /*
18344 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18345 * not in its finally clause (which then is to be executed next) is found.
18346 * In this case, make the ":return" pending for execution at the ":endtry".
18347 * Otherwise, return normally.
18348 */
18349 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18350 if (idx >= 0)
18351 {
18352 cstack->cs_pending[idx] = CSTP_RETURN;
18353
18354 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018355 /* A pending return again gets pending. "rettv" points to an
18356 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018358 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359 else
18360 {
18361 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018362 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018364 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018366 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367 {
18368 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018369 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018370 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018371 else
18372 EMSG(_(e_outofmem));
18373 }
18374 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018375 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018376
18377 if (reanimate)
18378 {
18379 /* The pending return value could be overwritten by a ":return"
18380 * without argument in a finally clause; reset the default
18381 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018382 current_funccal->rettv->v_type = VAR_NUMBER;
18383 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 }
18385 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018386 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387 }
18388 else
18389 {
18390 current_funccal->returned = TRUE;
18391
18392 /* If the return is carried out now, store the return value. For
18393 * a return immediately after reanimation, the value is already
18394 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018395 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018397 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018398 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018400 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 }
18402 }
18403
18404 return idx < 0;
18405}
18406
18407/*
18408 * Free the variable with a pending return value.
18409 */
18410 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018411discard_pending_return(rettv)
18412 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413{
Bram Moolenaar33570922005-01-25 22:26:29 +000018414 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415}
18416
18417/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018418 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 * is an allocated string. Used by report_pending() for verbose messages.
18420 */
18421 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018422get_return_cmd(rettv)
18423 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018425 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018426 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018427 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018428
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018429 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018430 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018431 if (s == NULL)
18432 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018433
18434 STRCPY(IObuff, ":return ");
18435 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18436 if (STRLEN(s) + 8 >= IOSIZE)
18437 STRCPY(IObuff + IOSIZE - 4, "...");
18438 vim_free(tofree);
18439 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440}
18441
18442/*
18443 * Get next function line.
18444 * Called by do_cmdline() to get the next line.
18445 * Returns allocated string, or NULL for end of function.
18446 */
18447/* ARGSUSED */
18448 char_u *
18449get_func_line(c, cookie, indent)
18450 int c; /* not used */
18451 void *cookie;
18452 int indent; /* not used */
18453{
Bram Moolenaar33570922005-01-25 22:26:29 +000018454 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018455 ufunc_T *fp = fcp->func;
18456 char_u *retval;
18457 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458
18459 /* If breakpoints have been added/deleted need to check for it. */
18460 if (fcp->dbg_tick != debug_tick)
18461 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018462 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463 sourcing_lnum);
18464 fcp->dbg_tick = debug_tick;
18465 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018466#ifdef FEAT_PROFILE
18467 if (do_profiling)
18468 func_line_end(cookie);
18469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470
Bram Moolenaar05159a02005-02-26 23:04:13 +000018471 gap = &fp->uf_lines;
18472 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473 retval = NULL;
18474 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18475 retval = NULL;
18476 else
18477 {
18478 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18479 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018480#ifdef FEAT_PROFILE
18481 if (do_profiling)
18482 func_line_start(cookie);
18483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484 }
18485
18486 /* Did we encounter a breakpoint? */
18487 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18488 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018489 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018491 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492 sourcing_lnum);
18493 fcp->dbg_tick = debug_tick;
18494 }
18495
18496 return retval;
18497}
18498
Bram Moolenaar05159a02005-02-26 23:04:13 +000018499#if defined(FEAT_PROFILE) || defined(PROTO)
18500/*
18501 * Called when starting to read a function line.
18502 * "sourcing_lnum" must be correct!
18503 * When skipping lines it may not actually be executed, but we won't find out
18504 * until later and we need to store the time now.
18505 */
18506 void
18507func_line_start(cookie)
18508 void *cookie;
18509{
18510 funccall_T *fcp = (funccall_T *)cookie;
18511 ufunc_T *fp = fcp->func;
18512
18513 if (fp->uf_profiling && sourcing_lnum >= 1
18514 && sourcing_lnum <= fp->uf_lines.ga_len)
18515 {
18516 fp->uf_tml_idx = sourcing_lnum - 1;
18517 fp->uf_tml_execed = FALSE;
18518 profile_start(&fp->uf_tml_start);
18519 profile_zero(&fp->uf_tml_children);
18520 profile_get_wait(&fp->uf_tml_wait);
18521 }
18522}
18523
18524/*
18525 * Called when actually executing a function line.
18526 */
18527 void
18528func_line_exec(cookie)
18529 void *cookie;
18530{
18531 funccall_T *fcp = (funccall_T *)cookie;
18532 ufunc_T *fp = fcp->func;
18533
18534 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18535 fp->uf_tml_execed = TRUE;
18536}
18537
18538/*
18539 * Called when done with a function line.
18540 */
18541 void
18542func_line_end(cookie)
18543 void *cookie;
18544{
18545 funccall_T *fcp = (funccall_T *)cookie;
18546 ufunc_T *fp = fcp->func;
18547
18548 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18549 {
18550 if (fp->uf_tml_execed)
18551 {
18552 ++fp->uf_tml_count[fp->uf_tml_idx];
18553 profile_end(&fp->uf_tml_start);
18554 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18555 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18556 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18557 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18558 }
18559 fp->uf_tml_idx = -1;
18560 }
18561}
18562#endif
18563
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564/*
18565 * Return TRUE if the currently active function should be ended, because a
18566 * return was encountered or an error occured. Used inside a ":while".
18567 */
18568 int
18569func_has_ended(cookie)
18570 void *cookie;
18571{
Bram Moolenaar33570922005-01-25 22:26:29 +000018572 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573
18574 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18575 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018576 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577 || fcp->returned);
18578}
18579
18580/*
18581 * return TRUE if cookie indicates a function which "abort"s on errors.
18582 */
18583 int
18584func_has_abort(cookie)
18585 void *cookie;
18586{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018587 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588}
18589
18590#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18591typedef enum
18592{
18593 VAR_FLAVOUR_DEFAULT,
18594 VAR_FLAVOUR_SESSION,
18595 VAR_FLAVOUR_VIMINFO
18596} var_flavour_T;
18597
18598static var_flavour_T var_flavour __ARGS((char_u *varname));
18599
18600 static var_flavour_T
18601var_flavour(varname)
18602 char_u *varname;
18603{
18604 char_u *p = varname;
18605
18606 if (ASCII_ISUPPER(*p))
18607 {
18608 while (*(++p))
18609 if (ASCII_ISLOWER(*p))
18610 return VAR_FLAVOUR_SESSION;
18611 return VAR_FLAVOUR_VIMINFO;
18612 }
18613 else
18614 return VAR_FLAVOUR_DEFAULT;
18615}
18616#endif
18617
18618#if defined(FEAT_VIMINFO) || defined(PROTO)
18619/*
18620 * Restore global vars that start with a capital from the viminfo file
18621 */
18622 int
18623read_viminfo_varlist(virp, writing)
18624 vir_T *virp;
18625 int writing;
18626{
18627 char_u *tab;
18628 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018629 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630
18631 if (!writing && (find_viminfo_parameter('!') != NULL))
18632 {
18633 tab = vim_strchr(virp->vir_line + 1, '\t');
18634 if (tab != NULL)
18635 {
18636 *tab++ = '\0'; /* isolate the variable name */
18637 if (*tab == 'S') /* string var */
18638 is_string = TRUE;
18639
18640 tab = vim_strchr(tab, '\t');
18641 if (tab != NULL)
18642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 if (is_string)
18644 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018645 tv.v_type = VAR_STRING;
18646 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648 }
18649 else
18650 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018651 tv.v_type = VAR_NUMBER;
18652 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018654 set_var(virp->vir_line + 1, &tv, FALSE);
18655 if (is_string)
18656 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 }
18658 }
18659 }
18660
18661 return viminfo_readline(virp);
18662}
18663
18664/*
18665 * Write global vars that start with a capital to the viminfo file
18666 */
18667 void
18668write_viminfo_varlist(fp)
18669 FILE *fp;
18670{
Bram Moolenaar33570922005-01-25 22:26:29 +000018671 hashitem_T *hi;
18672 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018673 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018674 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018675 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018676 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018677 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678
18679 if (find_viminfo_parameter('!') == NULL)
18680 return;
18681
18682 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018683
Bram Moolenaar33570922005-01-25 22:26:29 +000018684 todo = globvarht.ht_used;
18685 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018687 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018689 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018690 this_var = HI2DI(hi);
18691 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018692 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018693 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018694 {
18695 case VAR_STRING: s = "STR"; break;
18696 case VAR_NUMBER: s = "NUM"; break;
18697 default: continue;
18698 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018699 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018700 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18701 if (p != NULL)
18702 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018703 vim_free(tofree);
18704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 }
18706 }
18707}
18708#endif
18709
18710#if defined(FEAT_SESSION) || defined(PROTO)
18711 int
18712store_session_globals(fd)
18713 FILE *fd;
18714{
Bram Moolenaar33570922005-01-25 22:26:29 +000018715 hashitem_T *hi;
18716 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018717 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718 char_u *p, *t;
18719
Bram Moolenaar33570922005-01-25 22:26:29 +000018720 todo = globvarht.ht_used;
18721 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018723 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018725 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018726 this_var = HI2DI(hi);
18727 if ((this_var->di_tv.v_type == VAR_NUMBER
18728 || this_var->di_tv.v_type == VAR_STRING)
18729 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018730 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018731 /* Escape special characters with a backslash. Turn a LF and
18732 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018733 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018734 (char_u *)"\\\"\n\r");
18735 if (p == NULL) /* out of memory */
18736 break;
18737 for (t = p; *t != NUL; ++t)
18738 if (*t == '\n')
18739 *t = 'n';
18740 else if (*t == '\r')
18741 *t = 'r';
18742 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000018743 this_var->di_key,
18744 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18745 : ' ',
18746 p,
18747 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18748 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000018749 || put_eol(fd) == FAIL)
18750 {
18751 vim_free(p);
18752 return FAIL;
18753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 vim_free(p);
18755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 }
18757 }
18758 return OK;
18759}
18760#endif
18761
Bram Moolenaar661b1822005-07-28 22:36:45 +000018762/*
18763 * Display script name where an item was last set.
18764 * Should only be invoked when 'verbose' is non-zero.
18765 */
18766 void
18767last_set_msg(scriptID)
18768 scid_T scriptID;
18769{
18770 if (scriptID != 0)
18771 {
18772 verbose_enter();
18773 MSG_PUTS(_("\n\tLast set from "));
18774 MSG_PUTS(get_scriptname(scriptID));
18775 verbose_leave();
18776 }
18777}
18778
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779#endif /* FEAT_EVAL */
18780
18781#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
18782
18783
18784#ifdef WIN3264
18785/*
18786 * Functions for ":8" filename modifier: get 8.3 version of a filename.
18787 */
18788static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18789static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
18790static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18791
18792/*
18793 * Get the short pathname of a file.
18794 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
18795 */
18796 static int
18797get_short_pathname(fnamep, bufp, fnamelen)
18798 char_u **fnamep;
18799 char_u **bufp;
18800 int *fnamelen;
18801{
18802 int l,len;
18803 char_u *newbuf;
18804
18805 len = *fnamelen;
18806
18807 l = GetShortPathName(*fnamep, *fnamep, len);
18808 if (l > len - 1)
18809 {
18810 /* If that doesn't work (not enough space), then save the string
18811 * and try again with a new buffer big enough
18812 */
18813 newbuf = vim_strnsave(*fnamep, l);
18814 if (newbuf == NULL)
18815 return 0;
18816
18817 vim_free(*bufp);
18818 *fnamep = *bufp = newbuf;
18819
18820 l = GetShortPathName(*fnamep,*fnamep,l+1);
18821
18822 /* Really should always succeed, as the buffer is big enough */
18823 }
18824
18825 *fnamelen = l;
18826 return 1;
18827}
18828
18829/*
18830 * Create a short path name. Returns the length of the buffer it needs.
18831 * Doesn't copy over the end of the buffer passed in.
18832 */
18833 static int
18834shortpath_for_invalid_fname(fname, bufp, fnamelen)
18835 char_u **fname;
18836 char_u **bufp;
18837 int *fnamelen;
18838{
18839 char_u *s, *p, *pbuf2, *pbuf3;
18840 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018841 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842
18843 /* Make a copy */
18844 len2 = *fnamelen;
18845 pbuf2 = vim_strnsave(*fname, len2);
18846 pbuf3 = NULL;
18847
18848 s = pbuf2 + len2 - 1; /* Find the end */
18849 slen = 1;
18850 plen = len2;
18851
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018852 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853 {
18854 --s;
18855 ++slen;
18856 --plen;
18857 }
18858
18859 do
18860 {
18861 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018862 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863 {
18864 --s;
18865 ++slen;
18866 --plen;
18867 }
18868 if (s <= pbuf2)
18869 break;
18870
18871 /* Remeber the character that is about to be blatted */
18872 ch = *s;
18873 *s = 0; /* get_short_pathname requires a null-terminated string */
18874
18875 /* Try it in situ */
18876 p = pbuf2;
18877 if (!get_short_pathname(&p, &pbuf3, &plen))
18878 {
18879 vim_free(pbuf2);
18880 return -1;
18881 }
18882 *s = ch; /* Preserve the string */
18883 } while (plen == 0);
18884
18885 if (plen > 0)
18886 {
18887 /* Remeber the length of the new string. */
18888 *fnamelen = len = plen + slen;
18889 vim_free(*bufp);
18890 if (len > len2)
18891 {
18892 /* If there's not enough space in the currently allocated string,
18893 * then copy it to a buffer big enough.
18894 */
18895 *fname= *bufp = vim_strnsave(p, len);
18896 if (*fname == NULL)
18897 return -1;
18898 }
18899 else
18900 {
18901 /* Transfer pbuf2 to being the main buffer (it's big enough) */
18902 *fname = *bufp = pbuf2;
18903 if (p != pbuf2)
18904 strncpy(*fname, p, plen);
18905 pbuf2 = NULL;
18906 }
18907 /* Concat the next bit */
18908 strncpy(*fname + plen, s, slen);
18909 (*fname)[len] = '\0';
18910 }
18911 vim_free(pbuf3);
18912 vim_free(pbuf2);
18913 return 0;
18914}
18915
18916/*
18917 * Get a pathname for a partial path.
18918 */
18919 static int
18920shortpath_for_partial(fnamep, bufp, fnamelen)
18921 char_u **fnamep;
18922 char_u **bufp;
18923 int *fnamelen;
18924{
18925 int sepcount, len, tflen;
18926 char_u *p;
18927 char_u *pbuf, *tfname;
18928 int hasTilde;
18929
18930 /* Count up the path seperators from the RHS.. so we know which part
18931 * of the path to return.
18932 */
18933 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018934 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 if (vim_ispathsep(*p))
18936 ++sepcount;
18937
18938 /* Need full path first (use expand_env() to remove a "~/") */
18939 hasTilde = (**fnamep == '~');
18940 if (hasTilde)
18941 pbuf = tfname = expand_env_save(*fnamep);
18942 else
18943 pbuf = tfname = FullName_save(*fnamep, FALSE);
18944
18945 len = tflen = STRLEN(tfname);
18946
18947 if (!get_short_pathname(&tfname, &pbuf, &len))
18948 return -1;
18949
18950 if (len == 0)
18951 {
18952 /* Don't have a valid filename, so shorten the rest of the
18953 * path if we can. This CAN give us invalid 8.3 filenames, but
18954 * there's not a lot of point in guessing what it might be.
18955 */
18956 len = tflen;
18957 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
18958 return -1;
18959 }
18960
18961 /* Count the paths backward to find the beginning of the desired string. */
18962 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018963 {
18964#ifdef FEAT_MBYTE
18965 if (has_mbyte)
18966 p -= mb_head_off(tfname, p);
18967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968 if (vim_ispathsep(*p))
18969 {
18970 if (sepcount == 0 || (hasTilde && sepcount == 1))
18971 break;
18972 else
18973 sepcount --;
18974 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976 if (hasTilde)
18977 {
18978 --p;
18979 if (p >= tfname)
18980 *p = '~';
18981 else
18982 return -1;
18983 }
18984 else
18985 ++p;
18986
18987 /* Copy in the string - p indexes into tfname - allocated at pbuf */
18988 vim_free(*bufp);
18989 *fnamelen = (int)STRLEN(p);
18990 *bufp = pbuf;
18991 *fnamep = p;
18992
18993 return 0;
18994}
18995#endif /* WIN3264 */
18996
18997/*
18998 * Adjust a filename, according to a string of modifiers.
18999 * *fnamep must be NUL terminated when called. When returning, the length is
19000 * determined by *fnamelen.
19001 * Returns valid flags.
19002 * When there is an error, *fnamep is set to NULL.
19003 */
19004 int
19005modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19006 char_u *src; /* string with modifiers */
19007 int *usedlen; /* characters after src that are used */
19008 char_u **fnamep; /* file name so far */
19009 char_u **bufp; /* buffer for allocated file name or NULL */
19010 int *fnamelen; /* length of fnamep */
19011{
19012 int valid = 0;
19013 char_u *tail;
19014 char_u *s, *p, *pbuf;
19015 char_u dirname[MAXPATHL];
19016 int c;
19017 int has_fullname = 0;
19018#ifdef WIN3264
19019 int has_shortname = 0;
19020#endif
19021
19022repeat:
19023 /* ":p" - full path/file_name */
19024 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19025 {
19026 has_fullname = 1;
19027
19028 valid |= VALID_PATH;
19029 *usedlen += 2;
19030
19031 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19032 if ((*fnamep)[0] == '~'
19033#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19034 && ((*fnamep)[1] == '/'
19035# ifdef BACKSLASH_IN_FILENAME
19036 || (*fnamep)[1] == '\\'
19037# endif
19038 || (*fnamep)[1] == NUL)
19039
19040#endif
19041 )
19042 {
19043 *fnamep = expand_env_save(*fnamep);
19044 vim_free(*bufp); /* free any allocated file name */
19045 *bufp = *fnamep;
19046 if (*fnamep == NULL)
19047 return -1;
19048 }
19049
19050 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019051 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052 {
19053 if (vim_ispathsep(*p)
19054 && p[1] == '.'
19055 && (p[2] == NUL
19056 || vim_ispathsep(p[2])
19057 || (p[2] == '.'
19058 && (p[3] == NUL || vim_ispathsep(p[3])))))
19059 break;
19060 }
19061
19062 /* FullName_save() is slow, don't use it when not needed. */
19063 if (*p != NUL || !vim_isAbsName(*fnamep))
19064 {
19065 *fnamep = FullName_save(*fnamep, *p != NUL);
19066 vim_free(*bufp); /* free any allocated file name */
19067 *bufp = *fnamep;
19068 if (*fnamep == NULL)
19069 return -1;
19070 }
19071
19072 /* Append a path separator to a directory. */
19073 if (mch_isdir(*fnamep))
19074 {
19075 /* Make room for one or two extra characters. */
19076 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19077 vim_free(*bufp); /* free any allocated file name */
19078 *bufp = *fnamep;
19079 if (*fnamep == NULL)
19080 return -1;
19081 add_pathsep(*fnamep);
19082 }
19083 }
19084
19085 /* ":." - path relative to the current directory */
19086 /* ":~" - path relative to the home directory */
19087 /* ":8" - shortname path - postponed till after */
19088 while (src[*usedlen] == ':'
19089 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19090 {
19091 *usedlen += 2;
19092 if (c == '8')
19093 {
19094#ifdef WIN3264
19095 has_shortname = 1; /* Postpone this. */
19096#endif
19097 continue;
19098 }
19099 pbuf = NULL;
19100 /* Need full path first (use expand_env() to remove a "~/") */
19101 if (!has_fullname)
19102 {
19103 if (c == '.' && **fnamep == '~')
19104 p = pbuf = expand_env_save(*fnamep);
19105 else
19106 p = pbuf = FullName_save(*fnamep, FALSE);
19107 }
19108 else
19109 p = *fnamep;
19110
19111 has_fullname = 0;
19112
19113 if (p != NULL)
19114 {
19115 if (c == '.')
19116 {
19117 mch_dirname(dirname, MAXPATHL);
19118 s = shorten_fname(p, dirname);
19119 if (s != NULL)
19120 {
19121 *fnamep = s;
19122 if (pbuf != NULL)
19123 {
19124 vim_free(*bufp); /* free any allocated file name */
19125 *bufp = pbuf;
19126 pbuf = NULL;
19127 }
19128 }
19129 }
19130 else
19131 {
19132 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19133 /* Only replace it when it starts with '~' */
19134 if (*dirname == '~')
19135 {
19136 s = vim_strsave(dirname);
19137 if (s != NULL)
19138 {
19139 *fnamep = s;
19140 vim_free(*bufp);
19141 *bufp = s;
19142 }
19143 }
19144 }
19145 vim_free(pbuf);
19146 }
19147 }
19148
19149 tail = gettail(*fnamep);
19150 *fnamelen = (int)STRLEN(*fnamep);
19151
19152 /* ":h" - head, remove "/file_name", can be repeated */
19153 /* Don't remove the first "/" or "c:\" */
19154 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19155 {
19156 valid |= VALID_HEAD;
19157 *usedlen += 2;
19158 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019159 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 --tail;
19161 *fnamelen = (int)(tail - *fnamep);
19162#ifdef VMS
19163 if (*fnamelen > 0)
19164 *fnamelen += 1; /* the path separator is part of the path */
19165#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019166 while (tail > s && !after_pathsep(s, tail))
19167 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 }
19169
19170 /* ":8" - shortname */
19171 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19172 {
19173 *usedlen += 2;
19174#ifdef WIN3264
19175 has_shortname = 1;
19176#endif
19177 }
19178
19179#ifdef WIN3264
19180 /* Check shortname after we have done 'heads' and before we do 'tails'
19181 */
19182 if (has_shortname)
19183 {
19184 pbuf = NULL;
19185 /* Copy the string if it is shortened by :h */
19186 if (*fnamelen < (int)STRLEN(*fnamep))
19187 {
19188 p = vim_strnsave(*fnamep, *fnamelen);
19189 if (p == 0)
19190 return -1;
19191 vim_free(*bufp);
19192 *bufp = *fnamep = p;
19193 }
19194
19195 /* Split into two implementations - makes it easier. First is where
19196 * there isn't a full name already, second is where there is.
19197 */
19198 if (!has_fullname && !vim_isAbsName(*fnamep))
19199 {
19200 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19201 return -1;
19202 }
19203 else
19204 {
19205 int l;
19206
19207 /* Simple case, already have the full-name
19208 * Nearly always shorter, so try first time. */
19209 l = *fnamelen;
19210 if (!get_short_pathname(fnamep, bufp, &l))
19211 return -1;
19212
19213 if (l == 0)
19214 {
19215 /* Couldn't find the filename.. search the paths.
19216 */
19217 l = *fnamelen;
19218 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19219 return -1;
19220 }
19221 *fnamelen = l;
19222 }
19223 }
19224#endif /* WIN3264 */
19225
19226 /* ":t" - tail, just the basename */
19227 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19228 {
19229 *usedlen += 2;
19230 *fnamelen -= (int)(tail - *fnamep);
19231 *fnamep = tail;
19232 }
19233
19234 /* ":e" - extension, can be repeated */
19235 /* ":r" - root, without extension, can be repeated */
19236 while (src[*usedlen] == ':'
19237 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19238 {
19239 /* find a '.' in the tail:
19240 * - for second :e: before the current fname
19241 * - otherwise: The last '.'
19242 */
19243 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19244 s = *fnamep - 2;
19245 else
19246 s = *fnamep + *fnamelen - 1;
19247 for ( ; s > tail; --s)
19248 if (s[0] == '.')
19249 break;
19250 if (src[*usedlen + 1] == 'e') /* :e */
19251 {
19252 if (s > tail)
19253 {
19254 *fnamelen += (int)(*fnamep - (s + 1));
19255 *fnamep = s + 1;
19256#ifdef VMS
19257 /* cut version from the extension */
19258 s = *fnamep + *fnamelen - 1;
19259 for ( ; s > *fnamep; --s)
19260 if (s[0] == ';')
19261 break;
19262 if (s > *fnamep)
19263 *fnamelen = s - *fnamep;
19264#endif
19265 }
19266 else if (*fnamep <= tail)
19267 *fnamelen = 0;
19268 }
19269 else /* :r */
19270 {
19271 if (s > tail) /* remove one extension */
19272 *fnamelen = (int)(s - *fnamep);
19273 }
19274 *usedlen += 2;
19275 }
19276
19277 /* ":s?pat?foo?" - substitute */
19278 /* ":gs?pat?foo?" - global substitute */
19279 if (src[*usedlen] == ':'
19280 && (src[*usedlen + 1] == 's'
19281 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19282 {
19283 char_u *str;
19284 char_u *pat;
19285 char_u *sub;
19286 int sep;
19287 char_u *flags;
19288 int didit = FALSE;
19289
19290 flags = (char_u *)"";
19291 s = src + *usedlen + 2;
19292 if (src[*usedlen + 1] == 'g')
19293 {
19294 flags = (char_u *)"g";
19295 ++s;
19296 }
19297
19298 sep = *s++;
19299 if (sep)
19300 {
19301 /* find end of pattern */
19302 p = vim_strchr(s, sep);
19303 if (p != NULL)
19304 {
19305 pat = vim_strnsave(s, (int)(p - s));
19306 if (pat != NULL)
19307 {
19308 s = p + 1;
19309 /* find end of substitution */
19310 p = vim_strchr(s, sep);
19311 if (p != NULL)
19312 {
19313 sub = vim_strnsave(s, (int)(p - s));
19314 str = vim_strnsave(*fnamep, *fnamelen);
19315 if (sub != NULL && str != NULL)
19316 {
19317 *usedlen = (int)(p + 1 - src);
19318 s = do_string_sub(str, pat, sub, flags);
19319 if (s != NULL)
19320 {
19321 *fnamep = s;
19322 *fnamelen = (int)STRLEN(s);
19323 vim_free(*bufp);
19324 *bufp = s;
19325 didit = TRUE;
19326 }
19327 }
19328 vim_free(sub);
19329 vim_free(str);
19330 }
19331 vim_free(pat);
19332 }
19333 }
19334 /* after using ":s", repeat all the modifiers */
19335 if (didit)
19336 goto repeat;
19337 }
19338 }
19339
19340 return valid;
19341}
19342
19343/*
19344 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19345 * "flags" can be "g" to do a global substitute.
19346 * Returns an allocated string, NULL for error.
19347 */
19348 char_u *
19349do_string_sub(str, pat, sub, flags)
19350 char_u *str;
19351 char_u *pat;
19352 char_u *sub;
19353 char_u *flags;
19354{
19355 int sublen;
19356 regmatch_T regmatch;
19357 int i;
19358 int do_all;
19359 char_u *tail;
19360 garray_T ga;
19361 char_u *ret;
19362 char_u *save_cpo;
19363
19364 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19365 save_cpo = p_cpo;
19366 p_cpo = (char_u *)"";
19367
19368 ga_init2(&ga, 1, 200);
19369
19370 do_all = (flags[0] == 'g');
19371
19372 regmatch.rm_ic = p_ic;
19373 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19374 if (regmatch.regprog != NULL)
19375 {
19376 tail = str;
19377 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19378 {
19379 /*
19380 * Get some space for a temporary buffer to do the substitution
19381 * into. It will contain:
19382 * - The text up to where the match is.
19383 * - The substituted text.
19384 * - The text after the match.
19385 */
19386 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19387 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19388 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19389 {
19390 ga_clear(&ga);
19391 break;
19392 }
19393
19394 /* copy the text up to where the match is */
19395 i = (int)(regmatch.startp[0] - tail);
19396 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19397 /* add the substituted text */
19398 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19399 + ga.ga_len + i, TRUE, TRUE, FALSE);
19400 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 /* avoid getting stuck on a match with an empty string */
19402 if (tail == regmatch.endp[0])
19403 {
19404 if (*tail == NUL)
19405 break;
19406 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19407 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408 }
19409 else
19410 {
19411 tail = regmatch.endp[0];
19412 if (*tail == NUL)
19413 break;
19414 }
19415 if (!do_all)
19416 break;
19417 }
19418
19419 if (ga.ga_data != NULL)
19420 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19421
19422 vim_free(regmatch.regprog);
19423 }
19424
19425 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19426 ga_clear(&ga);
19427 p_cpo = save_cpo;
19428
19429 return ret;
19430}
19431
19432#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */