blob: f14a69b38c0743f4e3fa0ab251a2eb304d464457 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000340};
341
342/* shorthand */
343#define vv_type vv_di.di_tv.v_type
344#define vv_nr vv_di.di_tv.vval.v_number
345#define vv_str vv_di.di_tv.vval.v_string
346#define vv_tv vv_di.di_tv
347
348/*
349 * The v: variables are stored in dictionary "vimvardict".
350 * "vimvars_var" is the variable that is used for the "l:" scope.
351 */
352static dict_T vimvardict;
353static dictitem_T vimvars_var;
354#define vimvarht vimvardict.dv_hashtab
355
Bram Moolenaara40058a2005-07-11 22:42:07 +0000356static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
357static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
358#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
359static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
360#endif
361static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
362static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
363static char_u *skip_var_one __ARGS((char_u *arg));
364static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
365static void list_glob_vars __ARGS((void));
366static void list_buf_vars __ARGS((void));
367static void list_win_vars __ARGS((void));
368static void list_vim_vars __ARGS((void));
369static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
370static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
371static int check_changedtick __ARGS((char_u *arg));
372static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
373static void clear_lval __ARGS((lval_T *lp));
374static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
375static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
376static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
377static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
378static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
379static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
380static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
381static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
382static void item_lock __ARGS((typval_T *tv, int deep, int lock));
383static int tv_islocked __ARGS((typval_T *tv));
384
Bram Moolenaar33570922005-01-25 22:26:29 +0000385static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
386static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
387static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
388static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
389static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
390static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000394static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000395static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
397static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
399static list_T *list_alloc __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000400static void list_free __ARGS((list_T *l));
401static listitem_T *listitem_alloc __ARGS((void));
402static void listitem_free __ARGS((listitem_T *item));
403static void listitem_remove __ARGS((list_T *l, listitem_T *item));
404static long list_len __ARGS((list_T *l));
405static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
406static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
407static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000408static listitem_T *list_find __ARGS((list_T *l, long n));
409static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static void list_append __ARGS((list_T *l, listitem_T *item));
411static int list_append_tv __ARGS((list_T *l, typval_T *tv));
412static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
413static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
414static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000415static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
417static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000418static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000419static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
420static void set_ref_in_list __ARGS((list_T *l, int copyID));
421static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static void dict_unref __ARGS((dict_T *d));
423static void dict_free __ARGS((dict_T *d));
424static dictitem_T *dictitem_alloc __ARGS((char_u *key));
425static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
426static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
427static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000428static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static int dict_add __ARGS((dict_T *d, dictitem_T *item));
430static long dict_len __ARGS((dict_T *d));
431static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
432static char_u *dict2string __ARGS((typval_T *tv));
433static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
435static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
436static char_u *string_quote __ARGS((char_u *str, int function));
437static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
438static int find_internal_func __ARGS((char_u *name));
439static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
440static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
441static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000442static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443
444static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000463#if defined(FEAT_INS_EXPAND)
464static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
466#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000467static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
472static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000498static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000499static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000500static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000513static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000514static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000541static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000558static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000562#ifdef vim_mkdir
563static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
564#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000569static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000570static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000571static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000589static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000594static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000595static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
598#ifdef HAVE_STRFTIME
599static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
600#endif
601static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000613static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000615static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000630static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
633static int get_env_len __ARGS((char_u **arg));
634static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000635static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000636static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
637#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
638#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
639 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000640static 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 +0000641static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000642static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000643static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
644static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static typval_T *alloc_tv __ARGS((void));
646static typval_T *alloc_string_tv __ARGS((char_u *string));
647static void free_tv __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void init_tv __ARGS((typval_T *varp));
649static long get_tv_number __ARGS((typval_T *varp));
650static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000651static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static char_u *get_tv_string __ARGS((typval_T *varp));
653static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000654static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000656static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
658static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
659static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
660static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
661static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
662static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
663static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000664static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000665static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000666static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
668static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
669static int eval_fname_script __ARGS((char_u *p));
670static int eval_fname_sid __ARGS((char_u *p));
671static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static ufunc_T *find_func __ARGS((char_u *name));
673static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000674static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000675#ifdef FEAT_PROFILE
676static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000677static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
678static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
679static int
680# ifdef __BORLANDC__
681 _RTLENTRYF
682# endif
683 prof_total_cmp __ARGS((const void *s1, const void *s2));
684static int
685# ifdef __BORLANDC__
686 _RTLENTRYF
687# endif
688 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000689#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000690static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000691static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000692static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void func_free __ARGS((ufunc_T *fp));
694static void func_unref __ARGS((char_u *name));
695static void func_ref __ARGS((char_u *name));
696static 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));
697static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
698
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000699/* Character used as separated in autoload function/variable names. */
700#define AUTOLOAD_CHAR '#'
701
Bram Moolenaar33570922005-01-25 22:26:29 +0000702/*
703 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000704 */
705 void
706eval_init()
707{
Bram Moolenaar33570922005-01-25 22:26:29 +0000708 int i;
709 struct vimvar *p;
710
711 init_var_dict(&globvardict, &globvars_var);
712 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000713 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000714 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000715
716 for (i = 0; i < VV_LEN; ++i)
717 {
718 p = &vimvars[i];
719 STRCPY(p->vv_di.di_key, p->vv_name);
720 if (p->vv_flags & VV_RO)
721 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
722 else if (p->vv_flags & VV_RO_SBX)
723 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
724 else
725 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000726
727 /* add to v: scope dict, unless the value is not always available */
728 if (p->vv_type != VAR_UNKNOWN)
729 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000730 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000731 /* add to compat scope dict */
732 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000733 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000734}
735
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000736#if defined(EXITFREE) || defined(PROTO)
737 void
738eval_clear()
739{
740 int i;
741 struct vimvar *p;
742
743 for (i = 0; i < VV_LEN; ++i)
744 {
745 p = &vimvars[i];
746 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000747 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000748 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000749 p->vv_di.di_tv.vval.v_string = NULL;
750 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000751 }
752 hash_clear(&vimvarht);
753 hash_clear(&compat_hashtab);
754
755 /* script-local variables */
756 for (i = 1; i <= ga_scripts.ga_len; ++i)
757 vars_clear(&SCRIPT_VARS(i));
758 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000759 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000760
761 /* global variables */
762 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000763
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000764 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000765 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000766 hash_clear(&func_hashtab);
767
768 /* unreferenced lists and dicts */
769 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000770}
771#endif
772
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 * Return the name of the executed function.
775 */
776 char_u *
777func_name(cookie)
778 void *cookie;
779{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000780 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781}
782
783/*
784 * Return the address holding the next breakpoint line for a funccall cookie.
785 */
786 linenr_T *
787func_breakpoint(cookie)
788 void *cookie;
789{
Bram Moolenaar33570922005-01-25 22:26:29 +0000790 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791}
792
793/*
794 * Return the address holding the debug tick for a funccall cookie.
795 */
796 int *
797func_dbg_tick(cookie)
798 void *cookie;
799{
Bram Moolenaar33570922005-01-25 22:26:29 +0000800 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801}
802
803/*
804 * Return the nesting level for a funccall cookie.
805 */
806 int
807func_level(cookie)
808 void *cookie;
809{
Bram Moolenaar33570922005-01-25 22:26:29 +0000810 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811}
812
813/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000814funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
816/*
817 * Return TRUE when a function was ended by a ":return" command.
818 */
819 int
820current_func_returned()
821{
822 return current_funccal->returned;
823}
824
825
826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 * Set an internal variable to a string value. Creates the variable if it does
828 * not already exist.
829 */
830 void
831set_internal_string_var(name, value)
832 char_u *name;
833 char_u *value;
834{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000835 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000836 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
838 val = vim_strsave(value);
839 if (val != NULL)
840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000841 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000842 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000844 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000845 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 }
847 }
848}
849
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000850static lval_T *redir_lval = NULL;
851static char_u *redir_endp = NULL;
852static char_u *redir_varname = NULL;
853
854/*
855 * Start recording command output to a variable
856 * Returns OK if successfully completed the setup. FAIL otherwise.
857 */
858 int
859var_redir_start(name, append)
860 char_u *name;
861 int append; /* append to an existing variable */
862{
863 int save_emsg;
864 int err;
865 typval_T tv;
866
867 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000868 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000869 {
870 EMSG(_(e_invarg));
871 return FAIL;
872 }
873
874 redir_varname = vim_strsave(name);
875 if (redir_varname == NULL)
876 return FAIL;
877
878 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
879 if (redir_lval == NULL)
880 {
881 var_redir_stop();
882 return FAIL;
883 }
884
885 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000886 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
887 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000888 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
889 {
890 if (redir_endp != NULL && *redir_endp != NUL)
891 /* Trailing characters are present after the variable name */
892 EMSG(_(e_trailing));
893 else
894 EMSG(_(e_invarg));
895 var_redir_stop();
896 return FAIL;
897 }
898
899 /* check if we can write to the variable: set it to or append an empty
900 * string */
901 save_emsg = did_emsg;
902 did_emsg = FALSE;
903 tv.v_type = VAR_STRING;
904 tv.vval.v_string = (char_u *)"";
905 if (append)
906 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
907 else
908 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
909 err = did_emsg;
910 did_emsg += save_emsg;
911 if (err)
912 {
913 var_redir_stop();
914 return FAIL;
915 }
916 if (redir_lval->ll_newkey != NULL)
917 {
918 /* Dictionary item was created, don't do it again. */
919 vim_free(redir_lval->ll_newkey);
920 redir_lval->ll_newkey = NULL;
921 }
922
923 return OK;
924}
925
926/*
927 * Append "value[len]" to the variable set by var_redir_start().
928 */
929 void
930var_redir_str(value, len)
931 char_u *value;
932 int len;
933{
934 char_u *val;
935 typval_T tv;
936 int save_emsg;
937 int err;
938
939 if (redir_lval == NULL)
940 return;
941
942 if (len == -1)
943 /* Append the entire string */
944 val = vim_strsave(value);
945 else
946 /* Append only the specified number of characters */
947 val = vim_strnsave(value, len);
948 if (val == NULL)
949 return;
950
951 tv.v_type = VAR_STRING;
952 tv.vval.v_string = val;
953
954 save_emsg = did_emsg;
955 did_emsg = FALSE;
956 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
957 err = did_emsg;
958 did_emsg += save_emsg;
959 if (err)
960 var_redir_stop();
961
962 vim_free(tv.vval.v_string);
963}
964
965/*
966 * Stop redirecting command output to a variable.
967 */
968 void
969var_redir_stop()
970{
971 if (redir_lval != NULL)
972 {
973 clear_lval(redir_lval);
974 vim_free(redir_lval);
975 redir_lval = NULL;
976 }
977 vim_free(redir_varname);
978 redir_varname = NULL;
979}
980
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981# if defined(FEAT_MBYTE) || defined(PROTO)
982 int
983eval_charconvert(enc_from, enc_to, fname_from, fname_to)
984 char_u *enc_from;
985 char_u *enc_to;
986 char_u *fname_from;
987 char_u *fname_to;
988{
989 int err = FALSE;
990
991 set_vim_var_string(VV_CC_FROM, enc_from, -1);
992 set_vim_var_string(VV_CC_TO, enc_to, -1);
993 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
994 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
995 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
996 err = TRUE;
997 set_vim_var_string(VV_CC_FROM, NULL, -1);
998 set_vim_var_string(VV_CC_TO, NULL, -1);
999 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1000 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1001
1002 if (err)
1003 return FAIL;
1004 return OK;
1005}
1006# endif
1007
1008# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1009 int
1010eval_printexpr(fname, args)
1011 char_u *fname;
1012 char_u *args;
1013{
1014 int err = FALSE;
1015
1016 set_vim_var_string(VV_FNAME_IN, fname, -1);
1017 set_vim_var_string(VV_CMDARG, args, -1);
1018 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1019 err = TRUE;
1020 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1021 set_vim_var_string(VV_CMDARG, NULL, -1);
1022
1023 if (err)
1024 {
1025 mch_remove(fname);
1026 return FAIL;
1027 }
1028 return OK;
1029}
1030# endif
1031
1032# if defined(FEAT_DIFF) || defined(PROTO)
1033 void
1034eval_diff(origfile, newfile, outfile)
1035 char_u *origfile;
1036 char_u *newfile;
1037 char_u *outfile;
1038{
1039 int err = FALSE;
1040
1041 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1042 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1043 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1044 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1045 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1046 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1047 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1048}
1049
1050 void
1051eval_patch(origfile, difffile, outfile)
1052 char_u *origfile;
1053 char_u *difffile;
1054 char_u *outfile;
1055{
1056 int err;
1057
1058 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1059 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1060 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1061 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1062 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1063 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1064 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1065}
1066# endif
1067
1068/*
1069 * Top level evaluation function, returning a boolean.
1070 * Sets "error" to TRUE if there was an error.
1071 * Return TRUE or FALSE.
1072 */
1073 int
1074eval_to_bool(arg, error, nextcmd, skip)
1075 char_u *arg;
1076 int *error;
1077 char_u **nextcmd;
1078 int skip; /* only parse, don't execute */
1079{
Bram Moolenaar33570922005-01-25 22:26:29 +00001080 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 int retval = FALSE;
1082
1083 if (skip)
1084 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001085 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 else
1088 {
1089 *error = FALSE;
1090 if (!skip)
1091 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001092 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001093 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 }
1095 }
1096 if (skip)
1097 --emsg_skip;
1098
1099 return retval;
1100}
1101
1102/*
1103 * Top level evaluation function, returning a string. If "skip" is TRUE,
1104 * only parsing to "nextcmd" is done, without reporting errors. Return
1105 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1106 */
1107 char_u *
1108eval_to_string_skip(arg, nextcmd, skip)
1109 char_u *arg;
1110 char_u **nextcmd;
1111 int skip; /* only parse, don't execute */
1112{
Bram Moolenaar33570922005-01-25 22:26:29 +00001113 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 char_u *retval;
1115
1116 if (skip)
1117 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001118 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 retval = NULL;
1120 else
1121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001122 retval = vim_strsave(get_tv_string(&tv));
1123 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 }
1125 if (skip)
1126 --emsg_skip;
1127
1128 return retval;
1129}
1130
1131/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001132 * Skip over an expression at "*pp".
1133 * Return FAIL for an error, OK otherwise.
1134 */
1135 int
1136skip_expr(pp)
1137 char_u **pp;
1138{
Bram Moolenaar33570922005-01-25 22:26:29 +00001139 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001140
1141 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001142 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001143}
1144
1145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 * Top level evaluation function, returning a string.
1147 * Return pointer to allocated memory, or NULL for failure.
1148 */
1149 char_u *
1150eval_to_string(arg, nextcmd)
1151 char_u *arg;
1152 char_u **nextcmd;
1153{
Bram Moolenaar33570922005-01-25 22:26:29 +00001154 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 char_u *retval;
1156
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001157 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 retval = NULL;
1159 else
1160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001161 retval = vim_strsave(get_tv_string(&tv));
1162 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 }
1164
1165 return retval;
1166}
1167
1168/*
1169 * Call eval_to_string() with "sandbox" set and not using local variables.
1170 */
1171 char_u *
1172eval_to_string_safe(arg, nextcmd)
1173 char_u *arg;
1174 char_u **nextcmd;
1175{
1176 char_u *retval;
1177 void *save_funccalp;
1178
1179 save_funccalp = save_funccal();
1180 ++sandbox;
1181 retval = eval_to_string(arg, nextcmd);
1182 --sandbox;
1183 restore_funccal(save_funccalp);
1184 return retval;
1185}
1186
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187/*
1188 * Top level evaluation function, returning a number.
1189 * Evaluates "expr" silently.
1190 * Returns -1 for an error.
1191 */
1192 int
1193eval_to_number(expr)
1194 char_u *expr;
1195{
Bram Moolenaar33570922005-01-25 22:26:29 +00001196 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001198 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199
1200 ++emsg_off;
1201
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 retval = -1;
1204 else
1205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001206 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 }
1209 --emsg_off;
1210
1211 return retval;
1212}
1213
Bram Moolenaara40058a2005-07-11 22:42:07 +00001214/*
1215 * Prepare v: variable "idx" to be used.
1216 * Save the current typeval in "save_tv".
1217 * When not used yet add the variable to the v: hashtable.
1218 */
1219 static void
1220prepare_vimvar(idx, save_tv)
1221 int idx;
1222 typval_T *save_tv;
1223{
1224 *save_tv = vimvars[idx].vv_tv;
1225 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1226 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1227}
1228
1229/*
1230 * Restore v: variable "idx" to typeval "save_tv".
1231 * When no longer defined, remove the variable from the v: hashtable.
1232 */
1233 static void
1234restore_vimvar(idx, save_tv)
1235 int idx;
1236 typval_T *save_tv;
1237{
1238 hashitem_T *hi;
1239
1240 clear_tv(&vimvars[idx].vv_tv);
1241 vimvars[idx].vv_tv = *save_tv;
1242 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1243 {
1244 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1245 if (HASHITEM_EMPTY(hi))
1246 EMSG2(_(e_intern2), "restore_vimvar()");
1247 else
1248 hash_remove(&vimvarht, hi);
1249 }
1250}
1251
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001252#if defined(FEAT_SYN_HL) || defined(PROTO)
1253/*
1254 * Evaluate an expression to a list with suggestions.
1255 * For the "expr:" part of 'spellsuggest'.
1256 */
1257 list_T *
1258eval_spell_expr(badword, expr)
1259 char_u *badword;
1260 char_u *expr;
1261{
1262 typval_T save_val;
1263 typval_T rettv;
1264 list_T *list = NULL;
1265 char_u *p = skipwhite(expr);
1266
1267 /* Set "v:val" to the bad word. */
1268 prepare_vimvar(VV_VAL, &save_val);
1269 vimvars[VV_VAL].vv_type = VAR_STRING;
1270 vimvars[VV_VAL].vv_str = badword;
1271 if (p_verbose == 0)
1272 ++emsg_off;
1273
1274 if (eval1(&p, &rettv, TRUE) == OK)
1275 {
1276 if (rettv.v_type != VAR_LIST)
1277 clear_tv(&rettv);
1278 else
1279 list = rettv.vval.v_list;
1280 }
1281
1282 if (p_verbose == 0)
1283 --emsg_off;
1284 vimvars[VV_VAL].vv_str = NULL;
1285 restore_vimvar(VV_VAL, &save_val);
1286
1287 return list;
1288}
1289
1290/*
1291 * "list" is supposed to contain two items: a word and a number. Return the
1292 * word in "pp" and the number as the return value.
1293 * Return -1 if anything isn't right.
1294 * Used to get the good word and score from the eval_spell_expr() result.
1295 */
1296 int
1297get_spellword(list, pp)
1298 list_T *list;
1299 char_u **pp;
1300{
1301 listitem_T *li;
1302
1303 li = list->lv_first;
1304 if (li == NULL)
1305 return -1;
1306 *pp = get_tv_string(&li->li_tv);
1307
1308 li = li->li_next;
1309 if (li == NULL)
1310 return -1;
1311 return get_tv_number(&li->li_tv);
1312}
1313#endif
1314
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001315/*
1316 * Top level evaluation function,
1317 */
1318 typval_T *
1319eval_expr(arg, nextcmd)
1320 char_u *arg;
1321 char_u **nextcmd;
1322{
1323 typval_T *tv;
1324
1325 tv = (typval_T *)alloc(sizeof(typval_T));
1326 if (!tv)
1327 return NULL;
1328
1329 if (eval0(arg, tv, nextcmd, TRUE) == FAIL)
1330 {
1331 vim_free(tv);
1332 return NULL;
1333 }
1334
1335 return tv;
1336}
1337
1338
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1340/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001341 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001343 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001345 static int
1346call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 char_u *func;
1348 int argc;
1349 char_u **argv;
1350 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
Bram Moolenaar33570922005-01-25 22:26:29 +00001353 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 long n;
1355 int len;
1356 int i;
1357 int doesrange;
1358 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001359 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaar33570922005-01-25 22:26:29 +00001361 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001363 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364
1365 for (i = 0; i < argc; i++)
1366 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001367 /* Pass a NULL or empty argument as an empty string */
1368 if (argv[i] == NULL || *argv[i] == NUL)
1369 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001370 argvars[i].v_type = VAR_STRING;
1371 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001372 continue;
1373 }
1374
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 /* Recognize a number argument, the others must be strings. */
1376 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1377 if (len != 0 && len == (int)STRLEN(argv[i]))
1378 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001379 argvars[i].v_type = VAR_NUMBER;
1380 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382 else
1383 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001384 argvars[i].v_type = VAR_STRING;
1385 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
1387 }
1388
1389 if (safe)
1390 {
1391 save_funccalp = save_funccal();
1392 ++sandbox;
1393 }
1394
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001395 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1396 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001398 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 if (safe)
1400 {
1401 --sandbox;
1402 restore_funccal(save_funccalp);
1403 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001404 vim_free(argvars);
1405
1406 if (ret == FAIL)
1407 clear_tv(rettv);
1408
1409 return ret;
1410}
1411
1412/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001413 * Call vimL function "func" and return the result as a string.
1414 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001415 * Uses argv[argc] for the function arguments.
1416 */
1417 void *
1418call_func_retstr(func, argc, argv, safe)
1419 char_u *func;
1420 int argc;
1421 char_u **argv;
1422 int safe; /* use the sandbox */
1423{
1424 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001425 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001426
1427 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1428 return NULL;
1429
1430 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001431 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 return retval;
1433}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001434
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001435#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001436/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001437 * Call vimL function "func" and return the result as a number.
1438 * Returns -1 when calling the function fails.
1439 * Uses argv[argc] for the function arguments.
1440 */
1441 long
1442call_func_retnr(func, argc, argv, safe)
1443 char_u *func;
1444 int argc;
1445 char_u **argv;
1446 int safe; /* use the sandbox */
1447{
1448 typval_T rettv;
1449 long retval;
1450
1451 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1452 return -1;
1453
1454 retval = get_tv_number_chk(&rettv, NULL);
1455 clear_tv(&rettv);
1456 return retval;
1457}
1458#endif
1459
1460/*
1461 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001462 * Uses argv[argc] for the function arguments.
1463 */
1464 void *
1465call_func_retlist(func, argc, argv, safe)
1466 char_u *func;
1467 int argc;
1468 char_u **argv;
1469 int safe; /* use the sandbox */
1470{
1471 typval_T rettv;
1472
1473 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1474 return NULL;
1475
1476 if (rettv.v_type != VAR_LIST)
1477 {
1478 clear_tv(&rettv);
1479 return NULL;
1480 }
1481
1482 return rettv.vval.v_list;
1483}
1484
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485#endif
1486
1487/*
1488 * Save the current function call pointer, and set it to NULL.
1489 * Used when executing autocommands and for ":source".
1490 */
1491 void *
1492save_funccal()
1493{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001494 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 current_funccal = NULL;
1497 return (void *)fc;
1498}
1499
1500 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001501restore_funccal(vfc)
1502 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001504 funccall_T *fc = (funccall_T *)vfc;
1505
1506 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507}
1508
Bram Moolenaar05159a02005-02-26 23:04:13 +00001509#if defined(FEAT_PROFILE) || defined(PROTO)
1510/*
1511 * Prepare profiling for entering a child or something else that is not
1512 * counted for the script/function itself.
1513 * Should always be called in pair with prof_child_exit().
1514 */
1515 void
1516prof_child_enter(tm)
1517 proftime_T *tm; /* place to store waittime */
1518{
1519 funccall_T *fc = current_funccal;
1520
1521 if (fc != NULL && fc->func->uf_profiling)
1522 profile_start(&fc->prof_child);
1523 script_prof_save(tm);
1524}
1525
1526/*
1527 * Take care of time spent in a child.
1528 * Should always be called after prof_child_enter().
1529 */
1530 void
1531prof_child_exit(tm)
1532 proftime_T *tm; /* where waittime was stored */
1533{
1534 funccall_T *fc = current_funccal;
1535
1536 if (fc != NULL && fc->func->uf_profiling)
1537 {
1538 profile_end(&fc->prof_child);
1539 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1540 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1541 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1542 }
1543 script_prof_restore(tm);
1544}
1545#endif
1546
1547
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548#ifdef FEAT_FOLDING
1549/*
1550 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1551 * it in "*cp". Doesn't give error messages.
1552 */
1553 int
1554eval_foldexpr(arg, cp)
1555 char_u *arg;
1556 int *cp;
1557{
Bram Moolenaar33570922005-01-25 22:26:29 +00001558 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 int retval;
1560 char_u *s;
1561
1562 ++emsg_off;
1563 ++sandbox;
1564 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001565 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 retval = 0;
1567 else
1568 {
1569 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001570 if (tv.v_type == VAR_NUMBER)
1571 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001572 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 retval = 0;
1574 else
1575 {
1576 /* If the result is a string, check if there is a non-digit before
1577 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001578 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 if (!VIM_ISDIGIT(*s) && *s != '-')
1580 *cp = *s++;
1581 retval = atol((char *)s);
1582 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001583 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 }
1585 --emsg_off;
1586 --sandbox;
1587
1588 return retval;
1589}
1590#endif
1591
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001593 * ":let" list all variable values
1594 * ":let var1 var2" list variable values
1595 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001596 * ":let var += expr" assignment command.
1597 * ":let var -= expr" assignment command.
1598 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001599 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 */
1601 void
1602ex_let(eap)
1603 exarg_T *eap;
1604{
1605 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001606 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001607 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001609 int var_count = 0;
1610 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001611 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001613 expr = skip_var_list(arg, &var_count, &semicolon);
1614 if (expr == NULL)
1615 return;
1616 expr = vim_strchr(expr, '=');
1617 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001619 /*
1620 * ":let" without "=": list variables
1621 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001622 if (*arg == '[')
1623 EMSG(_(e_invarg));
1624 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001625 /* ":let var1 var2" */
1626 arg = list_arg_vars(eap, arg);
1627 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001629 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001630 list_glob_vars();
1631 list_buf_vars();
1632 list_win_vars();
1633 list_vim_vars();
1634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 eap->nextcmd = check_nextcmd(arg);
1636 }
1637 else
1638 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001639 op[0] = '=';
1640 op[1] = NUL;
1641 if (expr > arg)
1642 {
1643 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1644 op[0] = expr[-1]; /* +=, -= or .= */
1645 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001646 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001647
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 if (eap->skip)
1649 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001650 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (eap->skip)
1652 {
1653 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001654 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 --emsg_skip;
1656 }
1657 else if (i != FAIL)
1658 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001660 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001661 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 }
1664}
1665
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666/*
1667 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1668 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001669 * When "nextchars" is not NULL it points to a string with characters that
1670 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1671 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001672 * Returns OK or FAIL;
1673 */
1674 static int
1675ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1676 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001677 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001678 int copy; /* copy values from "tv", don't move */
1679 int semicolon; /* from skip_var_list() */
1680 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001681 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682{
1683 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001684 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001686 listitem_T *item;
1687 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688
1689 if (*arg != '[')
1690 {
1691 /*
1692 * ":let var = expr" or ":for var in list"
1693 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001694 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695 return FAIL;
1696 return OK;
1697 }
1698
1699 /*
1700 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1701 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001702 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001703 {
1704 EMSG(_(e_listreq));
1705 return FAIL;
1706 }
1707
1708 i = list_len(l);
1709 if (semicolon == 0 && var_count < i)
1710 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001711 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712 return FAIL;
1713 }
1714 if (var_count - semicolon > i)
1715 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001716 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717 return FAIL;
1718 }
1719
1720 item = l->lv_first;
1721 while (*arg != ']')
1722 {
1723 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001724 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 item = item->li_next;
1726 if (arg == NULL)
1727 return FAIL;
1728
1729 arg = skipwhite(arg);
1730 if (*arg == ';')
1731 {
1732 /* Put the rest of the list (may be empty) in the var after ';'.
1733 * Create a new list for this. */
1734 l = list_alloc();
1735 if (l == NULL)
1736 return FAIL;
1737 while (item != NULL)
1738 {
1739 list_append_tv(l, &item->li_tv);
1740 item = item->li_next;
1741 }
1742
1743 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001744 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001745 ltv.vval.v_list = l;
1746 l->lv_refcount = 1;
1747
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001748 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1749 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 clear_tv(&ltv);
1751 if (arg == NULL)
1752 return FAIL;
1753 break;
1754 }
1755 else if (*arg != ',' && *arg != ']')
1756 {
1757 EMSG2(_(e_intern2), "ex_let_vars()");
1758 return FAIL;
1759 }
1760 }
1761
1762 return OK;
1763}
1764
1765/*
1766 * Skip over assignable variable "var" or list of variables "[var, var]".
1767 * Used for ":let varvar = expr" and ":for varvar in expr".
1768 * For "[var, var]" increment "*var_count" for each variable.
1769 * for "[var, var; var]" set "semicolon".
1770 * Return NULL for an error.
1771 */
1772 static char_u *
1773skip_var_list(arg, var_count, semicolon)
1774 char_u *arg;
1775 int *var_count;
1776 int *semicolon;
1777{
1778 char_u *p, *s;
1779
1780 if (*arg == '[')
1781 {
1782 /* "[var, var]": find the matching ']'. */
1783 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001784 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785 {
1786 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1787 s = skip_var_one(p);
1788 if (s == p)
1789 {
1790 EMSG2(_(e_invarg2), p);
1791 return NULL;
1792 }
1793 ++*var_count;
1794
1795 p = skipwhite(s);
1796 if (*p == ']')
1797 break;
1798 else if (*p == ';')
1799 {
1800 if (*semicolon == 1)
1801 {
1802 EMSG(_("Double ; in list of variables"));
1803 return NULL;
1804 }
1805 *semicolon = 1;
1806 }
1807 else if (*p != ',')
1808 {
1809 EMSG2(_(e_invarg2), p);
1810 return NULL;
1811 }
1812 }
1813 return p + 1;
1814 }
1815 else
1816 return skip_var_one(arg);
1817}
1818
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001819/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001820 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1821 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001822 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823 static char_u *
1824skip_var_one(arg)
1825 char_u *arg;
1826{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001827 if (*arg == '@' && arg[1] != NUL)
1828 return arg + 2;
1829 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1830 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001831}
1832
Bram Moolenaara7043832005-01-21 11:56:39 +00001833/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001834 * List variables for hashtab "ht" with prefix "prefix".
1835 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001836 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001838list_hashtable_vars(ht, prefix, empty)
1839 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001840 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001841 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001842{
Bram Moolenaar33570922005-01-25 22:26:29 +00001843 hashitem_T *hi;
1844 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001845 int todo;
1846
1847 todo = ht->ht_used;
1848 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1849 {
1850 if (!HASHITEM_EMPTY(hi))
1851 {
1852 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001853 di = HI2DI(hi);
1854 if (empty || di->di_tv.v_type != VAR_STRING
1855 || di->di_tv.vval.v_string != NULL)
1856 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001857 }
1858 }
1859}
1860
1861/*
1862 * List global variables.
1863 */
1864 static void
1865list_glob_vars()
1866{
Bram Moolenaar33570922005-01-25 22:26:29 +00001867 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001868}
1869
1870/*
1871 * List buffer variables.
1872 */
1873 static void
1874list_buf_vars()
1875{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001876 char_u numbuf[NUMBUFLEN];
1877
Bram Moolenaar33570922005-01-25 22:26:29 +00001878 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001879
1880 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1881 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001882}
1883
1884/*
1885 * List window variables.
1886 */
1887 static void
1888list_win_vars()
1889{
Bram Moolenaar33570922005-01-25 22:26:29 +00001890 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001891}
1892
1893/*
1894 * List Vim variables.
1895 */
1896 static void
1897list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898{
Bram Moolenaar33570922005-01-25 22:26:29 +00001899 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900}
1901
1902/*
1903 * List variables in "arg".
1904 */
1905 static char_u *
1906list_arg_vars(eap, arg)
1907 exarg_T *eap;
1908 char_u *arg;
1909{
1910 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001911 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001912 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001913 char_u *name_start;
1914 char_u *arg_subsc;
1915 char_u *tofree;
1916 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917
1918 while (!ends_excmd(*arg) && !got_int)
1919 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001920 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001922 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001923 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1924 {
1925 emsg_severe = TRUE;
1926 EMSG(_(e_trailing));
1927 break;
1928 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001930 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001932 /* get_name_len() takes care of expanding curly braces */
1933 name_start = name = arg;
1934 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1935 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001937 /* This is mainly to keep test 49 working: when expanding
1938 * curly braces fails overrule the exception error message. */
1939 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001941 emsg_severe = TRUE;
1942 EMSG2(_(e_invarg2), arg);
1943 break;
1944 }
1945 error = TRUE;
1946 }
1947 else
1948 {
1949 if (tofree != NULL)
1950 name = tofree;
1951 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001952 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001953 else
1954 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001955 /* handle d.key, l[idx], f(expr) */
1956 arg_subsc = arg;
1957 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001958 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001960 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001961 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001962 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001963 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001964 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001965 case 'g': list_glob_vars(); break;
1966 case 'b': list_buf_vars(); break;
1967 case 'w': list_win_vars(); break;
1968 case 'v': list_vim_vars(); break;
1969 default:
1970 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001971 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001972 }
1973 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001974 {
1975 char_u numbuf[NUMBUFLEN];
1976 char_u *tf;
1977 int c;
1978 char_u *s;
1979
1980 s = echo_string(&tv, &tf, numbuf);
1981 c = *arg;
1982 *arg = NUL;
1983 list_one_var_a((char_u *)"",
1984 arg == arg_subsc ? name : name_start,
1985 tv.v_type, s == NULL ? (char_u *)"" : s);
1986 *arg = c;
1987 vim_free(tf);
1988 }
1989 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001990 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001991 }
1992 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001993
1994 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001995 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001996
1997 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001998 }
1999
2000 return arg;
2001}
2002
2003/*
2004 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2005 * Returns a pointer to the char just after the var name.
2006 * Returns NULL if there is an error.
2007 */
2008 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002009ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002010 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002011 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002014 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002015{
2016 int c1;
2017 char_u *name;
2018 char_u *p;
2019 char_u *arg_end = NULL;
2020 int len;
2021 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002022 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002023
2024 /*
2025 * ":let $VAR = expr": Set environment variable.
2026 */
2027 if (*arg == '$')
2028 {
2029 /* Find the end of the name. */
2030 ++arg;
2031 name = arg;
2032 len = get_env_len(&arg);
2033 if (len == 0)
2034 EMSG2(_(e_invarg2), name - 1);
2035 else
2036 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002037 if (op != NULL && (*op == '+' || *op == '-'))
2038 EMSG2(_(e_letwrong), op);
2039 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002040 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002041 EMSG(_(e_letunexp));
2042 else
2043 {
2044 c1 = name[len];
2045 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002046 p = get_tv_string_chk(tv);
2047 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002048 {
2049 int mustfree = FALSE;
2050 char_u *s = vim_getenv(name, &mustfree);
2051
2052 if (s != NULL)
2053 {
2054 p = tofree = concat_str(s, p);
2055 if (mustfree)
2056 vim_free(s);
2057 }
2058 }
2059 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002060 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002061 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002062 if (STRICMP(name, "HOME") == 0)
2063 init_homedir();
2064 else if (didset_vim && STRICMP(name, "VIM") == 0)
2065 didset_vim = FALSE;
2066 else if (didset_vimruntime
2067 && STRICMP(name, "VIMRUNTIME") == 0)
2068 didset_vimruntime = FALSE;
2069 arg_end = arg;
2070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002071 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002072 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073 }
2074 }
2075 }
2076
2077 /*
2078 * ":let &option = expr": Set option value.
2079 * ":let &l:option = expr": Set local option value.
2080 * ":let &g:option = expr": Set global option value.
2081 */
2082 else if (*arg == '&')
2083 {
2084 /* Find the end of the name. */
2085 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 if (p == NULL || (endchars != NULL
2087 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002088 EMSG(_(e_letunexp));
2089 else
2090 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002091 long n;
2092 int opt_type;
2093 long numval;
2094 char_u *stringval = NULL;
2095 char_u *s;
2096
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097 c1 = *p;
2098 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002099
2100 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002101 s = get_tv_string_chk(tv); /* != NULL if number or string */
2102 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002103 {
2104 opt_type = get_option_value(arg, &numval,
2105 &stringval, opt_flags);
2106 if ((opt_type == 1 && *op == '.')
2107 || (opt_type == 0 && *op != '.'))
2108 EMSG2(_(e_letwrong), op);
2109 else
2110 {
2111 if (opt_type == 1) /* number */
2112 {
2113 if (*op == '+')
2114 n = numval + n;
2115 else
2116 n = numval - n;
2117 }
2118 else if (opt_type == 0 && stringval != NULL) /* string */
2119 {
2120 s = concat_str(stringval, s);
2121 vim_free(stringval);
2122 stringval = s;
2123 }
2124 }
2125 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002126 if (s != NULL)
2127 {
2128 set_option_value(arg, n, s, opt_flags);
2129 arg_end = p;
2130 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002131 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002132 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002133 }
2134 }
2135
2136 /*
2137 * ":let @r = expr": Set register contents.
2138 */
2139 else if (*arg == '@')
2140 {
2141 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002142 if (op != NULL && (*op == '+' || *op == '-'))
2143 EMSG2(_(e_letwrong), op);
2144 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002145 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002146 EMSG(_(e_letunexp));
2147 else
2148 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002149 char_u *tofree = NULL;
2150 char_u *s;
2151
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002152 p = get_tv_string_chk(tv);
2153 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002154 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002155 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002156 if (s != NULL)
2157 {
2158 p = tofree = concat_str(s, p);
2159 vim_free(s);
2160 }
2161 }
2162 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002163 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002164 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002165 arg_end = arg + 1;
2166 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002167 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168 }
2169 }
2170
2171 /*
2172 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002173 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002175 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002176 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002177 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002179 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002180 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2183 EMSG(_(e_letunexp));
2184 else
2185 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002186 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 arg_end = p;
2188 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002190 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 }
2192
2193 else
2194 EMSG2(_(e_invarg2), arg);
2195
2196 return arg_end;
2197}
2198
2199/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002200 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2201 */
2202 static int
2203check_changedtick(arg)
2204 char_u *arg;
2205{
2206 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2207 {
2208 EMSG2(_(e_readonlyvar), arg);
2209 return TRUE;
2210 }
2211 return FALSE;
2212}
2213
2214/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 * Get an lval: variable, Dict item or List item that can be assigned a value
2216 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2217 * "name.key", "name.key[expr]" etc.
2218 * Indexing only works if "name" is an existing List or Dictionary.
2219 * "name" points to the start of the name.
2220 * If "rettv" is not NULL it points to the value to be assigned.
2221 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2222 * wrong; must end in space or cmd separator.
2223 *
2224 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002225 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002226 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 */
2228 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002229get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002231 typval_T *rettv;
2232 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002233 int unlet;
2234 int skip;
2235 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002236 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 char_u *expr_start, *expr_end;
2240 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002241 dictitem_T *v;
2242 typval_T var1;
2243 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002244 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002245 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002246 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002247 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002248 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002250 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002251 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252
2253 if (skip)
2254 {
2255 /* When skipping just find the end of the name. */
2256 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002257 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 }
2259
2260 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002261 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 if (expr_start != NULL)
2263 {
2264 /* Don't expand the name when we already know there is an error. */
2265 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2266 && *p != '[' && *p != '.')
2267 {
2268 EMSG(_(e_trailing));
2269 return NULL;
2270 }
2271
2272 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2273 if (lp->ll_exp_name == NULL)
2274 {
2275 /* Report an invalid expression in braces, unless the
2276 * expression evaluation has been cancelled due to an
2277 * aborting error, an interrupt, or an exception. */
2278 if (!aborting() && !quiet)
2279 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002280 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 EMSG2(_(e_invarg2), name);
2282 return NULL;
2283 }
2284 }
2285 lp->ll_name = lp->ll_exp_name;
2286 }
2287 else
2288 lp->ll_name = name;
2289
2290 /* Without [idx] or .key we are done. */
2291 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2292 return p;
2293
2294 cc = *p;
2295 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002297 if (v == NULL && !quiet)
2298 EMSG2(_(e_undefvar), lp->ll_name);
2299 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 if (v == NULL)
2301 return NULL;
2302
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002303 /*
2304 * Loop until no more [idx] or .key is following.
2305 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002306 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002309 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2310 && !(lp->ll_tv->v_type == VAR_DICT
2311 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 if (!quiet)
2314 EMSG(_("E689: Can only index a List or Dictionary"));
2315 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002317 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 if (!quiet)
2320 EMSG(_("E708: [:] must come last"));
2321 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002323
Bram Moolenaar8c711452005-01-14 21:53:12 +00002324 len = -1;
2325 if (*p == '.')
2326 {
2327 key = p + 1;
2328 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2329 ;
2330 if (len == 0)
2331 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 if (!quiet)
2333 EMSG(_(e_emptykey));
2334 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002335 }
2336 p = key + len;
2337 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002338 else
2339 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002340 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002341 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002342 if (*p == ':')
2343 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002344 else
2345 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002346 empty1 = FALSE;
2347 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002348 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002349 if (get_tv_string_chk(&var1) == NULL)
2350 {
2351 /* not a number or string */
2352 clear_tv(&var1);
2353 return NULL;
2354 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002355 }
2356
2357 /* Optionally get the second index [ :expr]. */
2358 if (*p == ':')
2359 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002360 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002361 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002362 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002363 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002364 if (!empty1)
2365 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002367 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002368 if (rettv != NULL && (rettv->v_type != VAR_LIST
2369 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002370 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002371 if (!quiet)
2372 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002373 if (!empty1)
2374 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002375 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002376 }
2377 p = skipwhite(p + 1);
2378 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002379 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002380 else
2381 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002383 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2384 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002385 if (!empty1)
2386 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002388 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 if (get_tv_string_chk(&var2) == NULL)
2390 {
2391 /* not a number or string */
2392 if (!empty1)
2393 clear_tv(&var1);
2394 clear_tv(&var2);
2395 return NULL;
2396 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002397 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002398 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002399 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002400 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002402
Bram Moolenaar8c711452005-01-14 21:53:12 +00002403 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002404 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 if (!quiet)
2406 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 if (!empty1)
2408 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002409 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002410 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002412 }
2413
2414 /* Skip to past ']'. */
2415 ++p;
2416 }
2417
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002418 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002419 {
2420 if (len == -1)
2421 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002423 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002424 if (*key == NUL)
2425 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002426 if (!quiet)
2427 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002428 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002430 }
2431 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 lp->ll_list = NULL;
2433 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002434 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002436 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002437 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002439 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002442 if (len == -1)
2443 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002445 }
2446 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002450 if (len == -1)
2451 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002452 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002453 p = NULL;
2454 break;
2455 }
2456 if (len == -1)
2457 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002459 }
2460 else
2461 {
2462 /*
2463 * Get the number and item for the only or first index of the List.
2464 */
2465 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002467 else
2468 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002470 clear_tv(&var1);
2471 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 lp->ll_list = lp->ll_tv->vval.v_list;
2474 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2475 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002476 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 if (!quiet)
2478 EMSGN(_(e_listidx), lp->ll_n1);
2479 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002480 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002482 }
2483
2484 /*
2485 * May need to find the item or absolute index for the second
2486 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 * When no index given: "lp->ll_empty2" is TRUE.
2488 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002489 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002491 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002493 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002497 if (ni == NULL)
2498 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 if (!quiet)
2500 EMSGN(_(e_listidx), lp->ll_n2);
2501 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 }
2505
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2507 if (lp->ll_n1 < 0)
2508 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2509 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002510 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 if (!quiet)
2512 EMSGN(_(e_listidx), lp->ll_n2);
2513 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002514 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002515 }
2516
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002518 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 }
2520
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 return p;
2522}
2523
2524/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002525 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 */
2527 static void
2528clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002529 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530{
2531 vim_free(lp->ll_exp_name);
2532 vim_free(lp->ll_newkey);
2533}
2534
2535/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002536 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002538 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 */
2540 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002541set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002542 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002546 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547{
2548 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002549 listitem_T *ni;
2550 listitem_T *ri;
2551 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552
2553 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 cc = *endp;
2558 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002559 if (op != NULL && *op != '=')
2560 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002562
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002563 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002564 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2565 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002566 {
2567 if (tv_op(&tv, rettv, op) == OK)
2568 set_var(lp->ll_name, &tv, FALSE);
2569 clear_tv(&tv);
2570 }
2571 }
2572 else
2573 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002575 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002577 else if (tv_check_lock(lp->ll_newkey == NULL
2578 ? lp->ll_tv->v_lock
2579 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2580 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 else if (lp->ll_range)
2582 {
2583 /*
2584 * Assign the List values to the list items.
2585 */
2586 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002587 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002588 if (op != NULL && *op != '=')
2589 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2590 else
2591 {
2592 clear_tv(&lp->ll_li->li_tv);
2593 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2594 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 ri = ri->li_next;
2596 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2597 break;
2598 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 /* Need to add an empty item. */
2601 ni = listitem_alloc();
2602 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002603 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 ri = NULL;
2605 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002606 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002608 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 ni->li_tv.vval.v_number = 0;
2610 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 lp->ll_li = lp->ll_li->li_next;
2613 ++lp->ll_n1;
2614 }
2615 if (ri != NULL)
2616 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002617 else if (lp->ll_empty2
2618 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 : lp->ll_n1 != lp->ll_n2)
2620 EMSG(_("E711: List value has not enough items"));
2621 }
2622 else
2623 {
2624 /*
2625 * Assign to a List or Dictionary item.
2626 */
2627 if (lp->ll_newkey != NULL)
2628 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002629 if (op != NULL && *op != '=')
2630 {
2631 EMSG2(_(e_letwrong), op);
2632 return;
2633 }
2634
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002636 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (di == NULL)
2638 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002639 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2640 {
2641 vim_free(di);
2642 return;
2643 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002645 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002646 else if (op != NULL && *op != '=')
2647 {
2648 tv_op(lp->ll_tv, rettv, op);
2649 return;
2650 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 /*
2655 * Assign the value to the variable or list item.
2656 */
2657 if (copy)
2658 copy_tv(rettv, lp->ll_tv);
2659 else
2660 {
2661 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002662 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002664 }
2665 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666}
2667
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002668/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002669 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2670 * Returns OK or FAIL.
2671 */
2672 static int
2673tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002674 typval_T *tv1;
2675 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002676 char_u *op;
2677{
2678 long n;
2679 char_u numbuf[NUMBUFLEN];
2680 char_u *s;
2681
2682 /* Can't do anything with a Funcref or a Dict on the right. */
2683 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2684 {
2685 switch (tv1->v_type)
2686 {
2687 case VAR_DICT:
2688 case VAR_FUNC:
2689 break;
2690
2691 case VAR_LIST:
2692 if (*op != '+' || tv2->v_type != VAR_LIST)
2693 break;
2694 /* List += List */
2695 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2696 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2697 return OK;
2698
2699 case VAR_NUMBER:
2700 case VAR_STRING:
2701 if (tv2->v_type == VAR_LIST)
2702 break;
2703 if (*op == '+' || *op == '-')
2704 {
2705 /* nr += nr or nr -= nr*/
2706 n = get_tv_number(tv1);
2707 if (*op == '+')
2708 n += get_tv_number(tv2);
2709 else
2710 n -= get_tv_number(tv2);
2711 clear_tv(tv1);
2712 tv1->v_type = VAR_NUMBER;
2713 tv1->vval.v_number = n;
2714 }
2715 else
2716 {
2717 /* str .= str */
2718 s = get_tv_string(tv1);
2719 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2720 clear_tv(tv1);
2721 tv1->v_type = VAR_STRING;
2722 tv1->vval.v_string = s;
2723 }
2724 return OK;
2725 }
2726 }
2727
2728 EMSG2(_(e_letwrong), op);
2729 return FAIL;
2730}
2731
2732/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002733 * Add a watcher to a list.
2734 */
2735 static void
2736list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002737 list_T *l;
2738 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002739{
2740 lw->lw_next = l->lv_watch;
2741 l->lv_watch = lw;
2742}
2743
2744/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002745 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002746 * No warning when it isn't found...
2747 */
2748 static void
2749list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002750 list_T *l;
2751 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002752{
Bram Moolenaar33570922005-01-25 22:26:29 +00002753 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002754
2755 lwp = &l->lv_watch;
2756 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2757 {
2758 if (lw == lwrem)
2759 {
2760 *lwp = lw->lw_next;
2761 break;
2762 }
2763 lwp = &lw->lw_next;
2764 }
2765}
2766
2767/*
2768 * Just before removing an item from a list: advance watchers to the next
2769 * item.
2770 */
2771 static void
2772list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002773 list_T *l;
2774 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002775{
Bram Moolenaar33570922005-01-25 22:26:29 +00002776 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002777
2778 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2779 if (lw->lw_item == item)
2780 lw->lw_item = item->li_next;
2781}
2782
2783/*
2784 * Evaluate the expression used in a ":for var in expr" command.
2785 * "arg" points to "var".
2786 * Set "*errp" to TRUE for an error, FALSE otherwise;
2787 * Return a pointer that holds the info. Null when there is an error.
2788 */
2789 void *
2790eval_for_line(arg, errp, nextcmdp, skip)
2791 char_u *arg;
2792 int *errp;
2793 char_u **nextcmdp;
2794 int skip;
2795{
Bram Moolenaar33570922005-01-25 22:26:29 +00002796 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002797 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002798 typval_T tv;
2799 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002800
2801 *errp = TRUE; /* default: there is an error */
2802
Bram Moolenaar33570922005-01-25 22:26:29 +00002803 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002804 if (fi == NULL)
2805 return NULL;
2806
2807 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2808 if (expr == NULL)
2809 return fi;
2810
2811 expr = skipwhite(expr);
2812 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2813 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002814 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002815 return fi;
2816 }
2817
2818 if (skip)
2819 ++emsg_skip;
2820 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2821 {
2822 *errp = FALSE;
2823 if (!skip)
2824 {
2825 l = tv.vval.v_list;
2826 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002827 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002828 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002829 clear_tv(&tv);
2830 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002831 else
2832 {
2833 fi->fi_list = l;
2834 list_add_watch(l, &fi->fi_lw);
2835 fi->fi_lw.lw_item = l->lv_first;
2836 }
2837 }
2838 }
2839 if (skip)
2840 --emsg_skip;
2841
2842 return fi;
2843}
2844
2845/*
2846 * Use the first item in a ":for" list. Advance to the next.
2847 * Assign the values to the variable (list). "arg" points to the first one.
2848 * Return TRUE when a valid item was found, FALSE when at end of list or
2849 * something wrong.
2850 */
2851 int
2852next_for_item(fi_void, arg)
2853 void *fi_void;
2854 char_u *arg;
2855{
Bram Moolenaar33570922005-01-25 22:26:29 +00002856 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002857 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002858 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002859
2860 item = fi->fi_lw.lw_item;
2861 if (item == NULL)
2862 result = FALSE;
2863 else
2864 {
2865 fi->fi_lw.lw_item = item->li_next;
2866 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2867 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2868 }
2869 return result;
2870}
2871
2872/*
2873 * Free the structure used to store info used by ":for".
2874 */
2875 void
2876free_for_info(fi_void)
2877 void *fi_void;
2878{
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002880
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002881 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002882 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002884 list_unref(fi->fi_list);
2885 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002886 vim_free(fi);
2887}
2888
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2890
2891 void
2892set_context_for_expression(xp, arg, cmdidx)
2893 expand_T *xp;
2894 char_u *arg;
2895 cmdidx_T cmdidx;
2896{
2897 int got_eq = FALSE;
2898 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002899 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002901 if (cmdidx == CMD_let)
2902 {
2903 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002904 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002905 {
2906 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002907 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002908 {
2909 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002911 if (vim_iswhite(*p))
2912 break;
2913 }
2914 return;
2915 }
2916 }
2917 else
2918 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2919 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 while ((xp->xp_pattern = vim_strpbrk(arg,
2921 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2922 {
2923 c = *xp->xp_pattern;
2924 if (c == '&')
2925 {
2926 c = xp->xp_pattern[1];
2927 if (c == '&')
2928 {
2929 ++xp->xp_pattern;
2930 xp->xp_context = cmdidx != CMD_let || got_eq
2931 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2932 }
2933 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002934 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002936 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2937 xp->xp_pattern += 2;
2938
2939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 }
2941 else if (c == '$')
2942 {
2943 /* environment variable */
2944 xp->xp_context = EXPAND_ENV_VARS;
2945 }
2946 else if (c == '=')
2947 {
2948 got_eq = TRUE;
2949 xp->xp_context = EXPAND_EXPRESSION;
2950 }
2951 else if (c == '<'
2952 && xp->xp_context == EXPAND_FUNCTIONS
2953 && vim_strchr(xp->xp_pattern, '(') == NULL)
2954 {
2955 /* Function name can start with "<SNR>" */
2956 break;
2957 }
2958 else if (cmdidx != CMD_let || got_eq)
2959 {
2960 if (c == '"') /* string */
2961 {
2962 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2963 if (c == '\\' && xp->xp_pattern[1] != NUL)
2964 ++xp->xp_pattern;
2965 xp->xp_context = EXPAND_NOTHING;
2966 }
2967 else if (c == '\'') /* literal string */
2968 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002969 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2971 /* skip */ ;
2972 xp->xp_context = EXPAND_NOTHING;
2973 }
2974 else if (c == '|')
2975 {
2976 if (xp->xp_pattern[1] == '|')
2977 {
2978 ++xp->xp_pattern;
2979 xp->xp_context = EXPAND_EXPRESSION;
2980 }
2981 else
2982 xp->xp_context = EXPAND_COMMANDS;
2983 }
2984 else
2985 xp->xp_context = EXPAND_EXPRESSION;
2986 }
2987 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002988 /* Doesn't look like something valid, expand as an expression
2989 * anyway. */
2990 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 arg = xp->xp_pattern;
2992 if (*arg != NUL)
2993 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2994 /* skip */ ;
2995 }
2996 xp->xp_pattern = arg;
2997}
2998
2999#endif /* FEAT_CMDL_COMPL */
3000
3001/*
3002 * ":1,25call func(arg1, arg2)" function call.
3003 */
3004 void
3005ex_call(eap)
3006 exarg_T *eap;
3007{
3008 char_u *arg = eap->arg;
3009 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003011 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003013 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 linenr_T lnum;
3015 int doesrange;
3016 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003017 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003019 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3020 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003021 if (tofree == NULL)
3022 return;
3023
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003024 /* Increase refcount on dictionary, it could get deleted when evaluating
3025 * the arguments. */
3026 if (fudi.fd_dict != NULL)
3027 ++fudi.fd_dict->dv_refcount;
3028
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003029 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3030 len = STRLEN(tofree);
3031 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032
Bram Moolenaar532c7802005-01-27 14:44:31 +00003033 /* Skip white space to allow ":call func ()". Not good, but required for
3034 * backward compatibility. */
3035 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003036 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
3038 if (*startarg != '(')
3039 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003040 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 goto end;
3042 }
3043
3044 /*
3045 * When skipping, evaluate the function once, to find the end of the
3046 * arguments.
3047 * When the function takes a range, this is discovered after the first
3048 * call, and the loop is broken.
3049 */
3050 if (eap->skip)
3051 {
3052 ++emsg_skip;
3053 lnum = eap->line2; /* do it once, also with an invalid range */
3054 }
3055 else
3056 lnum = eap->line1;
3057 for ( ; lnum <= eap->line2; ++lnum)
3058 {
3059 if (!eap->skip && eap->addr_count > 0)
3060 {
3061 curwin->w_cursor.lnum = lnum;
3062 curwin->w_cursor.col = 0;
3063 }
3064 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003065 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003066 eap->line1, eap->line2, &doesrange,
3067 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 {
3069 failed = TRUE;
3070 break;
3071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003072 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 if (doesrange || eap->skip)
3074 break;
3075 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003076 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003077 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003078 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 if (aborting())
3080 break;
3081 }
3082 if (eap->skip)
3083 --emsg_skip;
3084
3085 if (!failed)
3086 {
3087 /* Check for trailing illegal characters and a following command. */
3088 if (!ends_excmd(*arg))
3089 {
3090 emsg_severe = TRUE;
3091 EMSG(_(e_trailing));
3092 }
3093 else
3094 eap->nextcmd = check_nextcmd(arg);
3095 }
3096
3097end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003098 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100}
3101
3102/*
3103 * ":unlet[!] var1 ... " command.
3104 */
3105 void
3106ex_unlet(eap)
3107 exarg_T *eap;
3108{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003109 ex_unletlock(eap, eap->arg, 0);
3110}
3111
3112/*
3113 * ":lockvar" and ":unlockvar" commands
3114 */
3115 void
3116ex_lockvar(eap)
3117 exarg_T *eap;
3118{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003120 int deep = 2;
3121
3122 if (eap->forceit)
3123 deep = -1;
3124 else if (vim_isdigit(*arg))
3125 {
3126 deep = getdigits(&arg);
3127 arg = skipwhite(arg);
3128 }
3129
3130 ex_unletlock(eap, arg, deep);
3131}
3132
3133/*
3134 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3135 */
3136 static void
3137ex_unletlock(eap, argstart, deep)
3138 exarg_T *eap;
3139 char_u *argstart;
3140 int deep;
3141{
3142 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003145 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
3147 do
3148 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003149 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003150 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3151 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003152 if (lv.ll_name == NULL)
3153 error = TRUE; /* error but continue parsing */
3154 if (name_end == NULL || (!vim_iswhite(*name_end)
3155 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003157 if (name_end != NULL)
3158 {
3159 emsg_severe = TRUE;
3160 EMSG(_(e_trailing));
3161 }
3162 if (!(eap->skip || error))
3163 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 break;
3165 }
3166
3167 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003168 {
3169 if (eap->cmdidx == CMD_unlet)
3170 {
3171 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3172 error = TRUE;
3173 }
3174 else
3175 {
3176 if (do_lock_var(&lv, name_end, deep,
3177 eap->cmdidx == CMD_lockvar) == FAIL)
3178 error = TRUE;
3179 }
3180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003182 if (!eap->skip)
3183 clear_lval(&lv);
3184
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 arg = skipwhite(name_end);
3186 } while (!ends_excmd(*arg));
3187
3188 eap->nextcmd = check_nextcmd(arg);
3189}
3190
Bram Moolenaar8c711452005-01-14 21:53:12 +00003191 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003192do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003193 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003194 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003195 int forceit;
3196{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003197 int ret = OK;
3198 int cc;
3199
3200 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003201 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003202 cc = *name_end;
3203 *name_end = NUL;
3204
3205 /* Normal name or expanded name. */
3206 if (check_changedtick(lp->ll_name))
3207 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003208 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003209 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003210 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003211 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003212 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3213 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003214 else if (lp->ll_range)
3215 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003216 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003217
3218 /* Delete a range of List items. */
3219 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3220 {
3221 li = lp->ll_li->li_next;
3222 listitem_remove(lp->ll_list, lp->ll_li);
3223 lp->ll_li = li;
3224 ++lp->ll_n1;
3225 }
3226 }
3227 else
3228 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003229 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003230 /* unlet a List item. */
3231 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003232 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003233 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003234 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003235 }
3236
3237 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003238}
3239
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240/*
3241 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003242 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 */
3244 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003245do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003247 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248{
Bram Moolenaar33570922005-01-25 22:26:29 +00003249 hashtab_T *ht;
3250 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003251 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
Bram Moolenaar33570922005-01-25 22:26:29 +00003253 ht = find_var_ht(name, &varname);
3254 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003256 hi = hash_find(ht, varname);
3257 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003258 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003259 if (var_check_ro(HI2DI(hi)->di_flags, name))
3260 return FAIL;
3261 delete_var(ht, hi);
3262 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003265 if (forceit)
3266 return OK;
3267 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 return FAIL;
3269}
3270
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003271/*
3272 * Lock or unlock variable indicated by "lp".
3273 * "deep" is the levels to go (-1 for unlimited);
3274 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3275 */
3276 static int
3277do_lock_var(lp, name_end, deep, lock)
3278 lval_T *lp;
3279 char_u *name_end;
3280 int deep;
3281 int lock;
3282{
3283 int ret = OK;
3284 int cc;
3285 dictitem_T *di;
3286
3287 if (deep == 0) /* nothing to do */
3288 return OK;
3289
3290 if (lp->ll_tv == NULL)
3291 {
3292 cc = *name_end;
3293 *name_end = NUL;
3294
3295 /* Normal name or expanded name. */
3296 if (check_changedtick(lp->ll_name))
3297 ret = FAIL;
3298 else
3299 {
3300 di = find_var(lp->ll_name, NULL);
3301 if (di == NULL)
3302 ret = FAIL;
3303 else
3304 {
3305 if (lock)
3306 di->di_flags |= DI_FLAGS_LOCK;
3307 else
3308 di->di_flags &= ~DI_FLAGS_LOCK;
3309 item_lock(&di->di_tv, deep, lock);
3310 }
3311 }
3312 *name_end = cc;
3313 }
3314 else if (lp->ll_range)
3315 {
3316 listitem_T *li = lp->ll_li;
3317
3318 /* (un)lock a range of List items. */
3319 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3320 {
3321 item_lock(&li->li_tv, deep, lock);
3322 li = li->li_next;
3323 ++lp->ll_n1;
3324 }
3325 }
3326 else if (lp->ll_list != NULL)
3327 /* (un)lock a List item. */
3328 item_lock(&lp->ll_li->li_tv, deep, lock);
3329 else
3330 /* un(lock) a Dictionary item. */
3331 item_lock(&lp->ll_di->di_tv, deep, lock);
3332
3333 return ret;
3334}
3335
3336/*
3337 * Lock or unlock an item. "deep" is nr of levels to go.
3338 */
3339 static void
3340item_lock(tv, deep, lock)
3341 typval_T *tv;
3342 int deep;
3343 int lock;
3344{
3345 static int recurse = 0;
3346 list_T *l;
3347 listitem_T *li;
3348 dict_T *d;
3349 hashitem_T *hi;
3350 int todo;
3351
3352 if (recurse >= DICT_MAXNEST)
3353 {
3354 EMSG(_("E743: variable nested too deep for (un)lock"));
3355 return;
3356 }
3357 if (deep == 0)
3358 return;
3359 ++recurse;
3360
3361 /* lock/unlock the item itself */
3362 if (lock)
3363 tv->v_lock |= VAR_LOCKED;
3364 else
3365 tv->v_lock &= ~VAR_LOCKED;
3366
3367 switch (tv->v_type)
3368 {
3369 case VAR_LIST:
3370 if ((l = tv->vval.v_list) != NULL)
3371 {
3372 if (lock)
3373 l->lv_lock |= VAR_LOCKED;
3374 else
3375 l->lv_lock &= ~VAR_LOCKED;
3376 if (deep < 0 || deep > 1)
3377 /* recursive: lock/unlock the items the List contains */
3378 for (li = l->lv_first; li != NULL; li = li->li_next)
3379 item_lock(&li->li_tv, deep - 1, lock);
3380 }
3381 break;
3382 case VAR_DICT:
3383 if ((d = tv->vval.v_dict) != NULL)
3384 {
3385 if (lock)
3386 d->dv_lock |= VAR_LOCKED;
3387 else
3388 d->dv_lock &= ~VAR_LOCKED;
3389 if (deep < 0 || deep > 1)
3390 {
3391 /* recursive: lock/unlock the items the List contains */
3392 todo = d->dv_hashtab.ht_used;
3393 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3394 {
3395 if (!HASHITEM_EMPTY(hi))
3396 {
3397 --todo;
3398 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3399 }
3400 }
3401 }
3402 }
3403 }
3404 --recurse;
3405}
3406
Bram Moolenaara40058a2005-07-11 22:42:07 +00003407/*
3408 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3409 * it refers to a List or Dictionary that is locked.
3410 */
3411 static int
3412tv_islocked(tv)
3413 typval_T *tv;
3414{
3415 return (tv->v_lock & VAR_LOCKED)
3416 || (tv->v_type == VAR_LIST
3417 && tv->vval.v_list != NULL
3418 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3419 || (tv->v_type == VAR_DICT
3420 && tv->vval.v_dict != NULL
3421 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3422}
3423
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3425/*
3426 * Delete all "menutrans_" variables.
3427 */
3428 void
3429del_menutrans_vars()
3430{
Bram Moolenaar33570922005-01-25 22:26:29 +00003431 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003432 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
Bram Moolenaar33570922005-01-25 22:26:29 +00003434 hash_lock(&globvarht);
3435 todo = globvarht.ht_used;
3436 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003437 {
3438 if (!HASHITEM_EMPTY(hi))
3439 {
3440 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003441 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3442 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003443 }
3444 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003445 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446}
3447#endif
3448
3449#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3450
3451/*
3452 * Local string buffer for the next two functions to store a variable name
3453 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3454 * get_user_var_name().
3455 */
3456
3457static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3458
3459static char_u *varnamebuf = NULL;
3460static int varnamebuflen = 0;
3461
3462/*
3463 * Function to concatenate a prefix and a variable name.
3464 */
3465 static char_u *
3466cat_prefix_varname(prefix, name)
3467 int prefix;
3468 char_u *name;
3469{
3470 int len;
3471
3472 len = (int)STRLEN(name) + 3;
3473 if (len > varnamebuflen)
3474 {
3475 vim_free(varnamebuf);
3476 len += 10; /* some additional space */
3477 varnamebuf = alloc(len);
3478 if (varnamebuf == NULL)
3479 {
3480 varnamebuflen = 0;
3481 return NULL;
3482 }
3483 varnamebuflen = len;
3484 }
3485 *varnamebuf = prefix;
3486 varnamebuf[1] = ':';
3487 STRCPY(varnamebuf + 2, name);
3488 return varnamebuf;
3489}
3490
3491/*
3492 * Function given to ExpandGeneric() to obtain the list of user defined
3493 * (global/buffer/window/built-in) variable names.
3494 */
3495/*ARGSUSED*/
3496 char_u *
3497get_user_var_name(xp, idx)
3498 expand_T *xp;
3499 int idx;
3500{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003501 static long_u gdone;
3502 static long_u bdone;
3503 static long_u wdone;
3504 static int vidx;
3505 static hashitem_T *hi;
3506 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
3508 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003509 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003510
3511 /* Global variables */
3512 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003514 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003515 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003516 else
3517 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003518 while (HASHITEM_EMPTY(hi))
3519 ++hi;
3520 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3521 return cat_prefix_varname('g', hi->hi_key);
3522 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003524
3525 /* b: variables */
3526 ht = &curbuf->b_vars.dv_hashtab;
3527 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003529 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003530 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003531 else
3532 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003533 while (HASHITEM_EMPTY(hi))
3534 ++hi;
3535 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003537 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003539 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 return (char_u *)"b:changedtick";
3541 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003542
3543 /* w: variables */
3544 ht = &curwin->w_vars.dv_hashtab;
3545 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003547 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003548 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003549 else
3550 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003551 while (HASHITEM_EMPTY(hi))
3552 ++hi;
3553 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003555
3556 /* v: variables */
3557 if (vidx < VV_LEN)
3558 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559
3560 vim_free(varnamebuf);
3561 varnamebuf = NULL;
3562 varnamebuflen = 0;
3563 return NULL;
3564}
3565
3566#endif /* FEAT_CMDL_COMPL */
3567
3568/*
3569 * types for expressions.
3570 */
3571typedef enum
3572{
3573 TYPE_UNKNOWN = 0
3574 , TYPE_EQUAL /* == */
3575 , TYPE_NEQUAL /* != */
3576 , TYPE_GREATER /* > */
3577 , TYPE_GEQUAL /* >= */
3578 , TYPE_SMALLER /* < */
3579 , TYPE_SEQUAL /* <= */
3580 , TYPE_MATCH /* =~ */
3581 , TYPE_NOMATCH /* !~ */
3582} exptype_T;
3583
3584/*
3585 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003586 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3588 */
3589
3590/*
3591 * Handle zero level expression.
3592 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003593 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 * Return OK or FAIL.
3595 */
3596 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003597eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 char_u **nextcmd;
3601 int evaluate;
3602{
3603 int ret;
3604 char_u *p;
3605
3606 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003607 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 if (ret == FAIL || !ends_excmd(*p))
3609 {
3610 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003611 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 /*
3613 * Report the invalid expression unless the expression evaluation has
3614 * been cancelled due to an aborting error, an interrupt, or an
3615 * exception.
3616 */
3617 if (!aborting())
3618 EMSG2(_(e_invexpr2), arg);
3619 ret = FAIL;
3620 }
3621 if (nextcmd != NULL)
3622 *nextcmd = check_nextcmd(p);
3623
3624 return ret;
3625}
3626
3627/*
3628 * Handle top level expression:
3629 * expr1 ? expr0 : expr0
3630 *
3631 * "arg" must point to the first non-white of the expression.
3632 * "arg" is advanced to the next non-white after the recognized expression.
3633 *
3634 * Return OK or FAIL.
3635 */
3636 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003637eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 int evaluate;
3641{
3642 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003643 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 /*
3646 * Get the first variable.
3647 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003648 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 return FAIL;
3650
3651 if ((*arg)[0] == '?')
3652 {
3653 result = FALSE;
3654 if (evaluate)
3655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003656 int error = FALSE;
3657
3658 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003660 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003661 if (error)
3662 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
3664
3665 /*
3666 * Get the second variable.
3667 */
3668 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003669 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 return FAIL;
3671
3672 /*
3673 * Check for the ":".
3674 */
3675 if ((*arg)[0] != ':')
3676 {
3677 EMSG(_("E109: Missing ':' after '?'"));
3678 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003679 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 return FAIL;
3681 }
3682
3683 /*
3684 * Get the third variable.
3685 */
3686 *arg = skipwhite(*arg + 1);
3687 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3688 {
3689 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003690 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 return FAIL;
3692 }
3693 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003694 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696
3697 return OK;
3698}
3699
3700/*
3701 * Handle first level expression:
3702 * expr2 || expr2 || expr2 logical OR
3703 *
3704 * "arg" must point to the first non-white of the expression.
3705 * "arg" is advanced to the next non-white after the recognized expression.
3706 *
3707 * Return OK or FAIL.
3708 */
3709 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003710eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 int evaluate;
3714{
Bram Moolenaar33570922005-01-25 22:26:29 +00003715 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 long result;
3717 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003718 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719
3720 /*
3721 * Get the first variable.
3722 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003723 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 return FAIL;
3725
3726 /*
3727 * Repeat until there is no following "||".
3728 */
3729 first = TRUE;
3730 result = FALSE;
3731 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3732 {
3733 if (evaluate && first)
3734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003735 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003737 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003738 if (error)
3739 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 first = FALSE;
3741 }
3742
3743 /*
3744 * Get the second variable.
3745 */
3746 *arg = skipwhite(*arg + 2);
3747 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3748 return FAIL;
3749
3750 /*
3751 * Compute the result.
3752 */
3753 if (evaluate && !result)
3754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003755 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003757 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003758 if (error)
3759 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
3761 if (evaluate)
3762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003763 rettv->v_type = VAR_NUMBER;
3764 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
3766 }
3767
3768 return OK;
3769}
3770
3771/*
3772 * Handle second level expression:
3773 * expr3 && expr3 && expr3 logical AND
3774 *
3775 * "arg" must point to the first non-white of the expression.
3776 * "arg" is advanced to the next non-white after the recognized expression.
3777 *
3778 * Return OK or FAIL.
3779 */
3780 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003781eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 int evaluate;
3785{
Bram Moolenaar33570922005-01-25 22:26:29 +00003786 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 long result;
3788 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003789 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
3791 /*
3792 * Get the first variable.
3793 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003794 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 return FAIL;
3796
3797 /*
3798 * Repeat until there is no following "&&".
3799 */
3800 first = TRUE;
3801 result = TRUE;
3802 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3803 {
3804 if (evaluate && first)
3805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003806 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003808 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003809 if (error)
3810 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 first = FALSE;
3812 }
3813
3814 /*
3815 * Get the second variable.
3816 */
3817 *arg = skipwhite(*arg + 2);
3818 if (eval4(arg, &var2, evaluate && result) == FAIL)
3819 return FAIL;
3820
3821 /*
3822 * Compute the result.
3823 */
3824 if (evaluate && result)
3825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003826 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003828 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003829 if (error)
3830 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832 if (evaluate)
3833 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003834 rettv->v_type = VAR_NUMBER;
3835 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837 }
3838
3839 return OK;
3840}
3841
3842/*
3843 * Handle third level expression:
3844 * var1 == var2
3845 * var1 =~ var2
3846 * var1 != var2
3847 * var1 !~ var2
3848 * var1 > var2
3849 * var1 >= var2
3850 * var1 < var2
3851 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003852 * var1 is var2
3853 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 *
3855 * "arg" must point to the first non-white of the expression.
3856 * "arg" is advanced to the next non-white after the recognized expression.
3857 *
3858 * Return OK or FAIL.
3859 */
3860 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003861eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 int evaluate;
3865{
Bram Moolenaar33570922005-01-25 22:26:29 +00003866 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 char_u *p;
3868 int i;
3869 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003870 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 int len = 2;
3872 long n1, n2;
3873 char_u *s1, *s2;
3874 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3875 regmatch_T regmatch;
3876 int ic;
3877 char_u *save_cpo;
3878
3879 /*
3880 * Get the first variable.
3881 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003882 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 return FAIL;
3884
3885 p = *arg;
3886 switch (p[0])
3887 {
3888 case '=': if (p[1] == '=')
3889 type = TYPE_EQUAL;
3890 else if (p[1] == '~')
3891 type = TYPE_MATCH;
3892 break;
3893 case '!': if (p[1] == '=')
3894 type = TYPE_NEQUAL;
3895 else if (p[1] == '~')
3896 type = TYPE_NOMATCH;
3897 break;
3898 case '>': if (p[1] != '=')
3899 {
3900 type = TYPE_GREATER;
3901 len = 1;
3902 }
3903 else
3904 type = TYPE_GEQUAL;
3905 break;
3906 case '<': if (p[1] != '=')
3907 {
3908 type = TYPE_SMALLER;
3909 len = 1;
3910 }
3911 else
3912 type = TYPE_SEQUAL;
3913 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003914 case 'i': if (p[1] == 's')
3915 {
3916 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3917 len = 5;
3918 if (!vim_isIDc(p[len]))
3919 {
3920 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3921 type_is = TRUE;
3922 }
3923 }
3924 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 }
3926
3927 /*
3928 * If there is a comparitive operator, use it.
3929 */
3930 if (type != TYPE_UNKNOWN)
3931 {
3932 /* extra question mark appended: ignore case */
3933 if (p[len] == '?')
3934 {
3935 ic = TRUE;
3936 ++len;
3937 }
3938 /* extra '#' appended: match case */
3939 else if (p[len] == '#')
3940 {
3941 ic = FALSE;
3942 ++len;
3943 }
3944 /* nothing appened: use 'ignorecase' */
3945 else
3946 ic = p_ic;
3947
3948 /*
3949 * Get the second variable.
3950 */
3951 *arg = skipwhite(p + len);
3952 if (eval5(arg, &var2, evaluate) == FAIL)
3953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003954 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 return FAIL;
3956 }
3957
3958 if (evaluate)
3959 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003960 if (type_is && rettv->v_type != var2.v_type)
3961 {
3962 /* For "is" a different type always means FALSE, for "notis"
3963 * it means TRUE. */
3964 n1 = (type == TYPE_NEQUAL);
3965 }
3966 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3967 {
3968 if (type_is)
3969 {
3970 n1 = (rettv->v_type == var2.v_type
3971 && rettv->vval.v_list == var2.vval.v_list);
3972 if (type == TYPE_NEQUAL)
3973 n1 = !n1;
3974 }
3975 else if (rettv->v_type != var2.v_type
3976 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3977 {
3978 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003979 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003980 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003981 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003982 clear_tv(rettv);
3983 clear_tv(&var2);
3984 return FAIL;
3985 }
3986 else
3987 {
3988 /* Compare two Lists for being equal or unequal. */
3989 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3990 if (type == TYPE_NEQUAL)
3991 n1 = !n1;
3992 }
3993 }
3994
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003995 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3996 {
3997 if (type_is)
3998 {
3999 n1 = (rettv->v_type == var2.v_type
4000 && rettv->vval.v_dict == var2.vval.v_dict);
4001 if (type == TYPE_NEQUAL)
4002 n1 = !n1;
4003 }
4004 else if (rettv->v_type != var2.v_type
4005 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4006 {
4007 if (rettv->v_type != var2.v_type)
4008 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4009 else
4010 EMSG(_("E736: Invalid operation for Dictionary"));
4011 clear_tv(rettv);
4012 clear_tv(&var2);
4013 return FAIL;
4014 }
4015 else
4016 {
4017 /* Compare two Dictionaries for being equal or unequal. */
4018 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4019 if (type == TYPE_NEQUAL)
4020 n1 = !n1;
4021 }
4022 }
4023
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004024 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4025 {
4026 if (rettv->v_type != var2.v_type
4027 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4028 {
4029 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004030 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004031 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004032 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004033 clear_tv(rettv);
4034 clear_tv(&var2);
4035 return FAIL;
4036 }
4037 else
4038 {
4039 /* Compare two Funcrefs for being equal or unequal. */
4040 if (rettv->vval.v_string == NULL
4041 || var2.vval.v_string == NULL)
4042 n1 = FALSE;
4043 else
4044 n1 = STRCMP(rettv->vval.v_string,
4045 var2.vval.v_string) == 0;
4046 if (type == TYPE_NEQUAL)
4047 n1 = !n1;
4048 }
4049 }
4050
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 /*
4052 * If one of the two variables is a number, compare as a number.
4053 * When using "=~" or "!~", always compare as string.
4054 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004055 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004058 n1 = get_tv_number(rettv);
4059 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 switch (type)
4061 {
4062 case TYPE_EQUAL: n1 = (n1 == n2); break;
4063 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4064 case TYPE_GREATER: n1 = (n1 > n2); break;
4065 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4066 case TYPE_SMALLER: n1 = (n1 < n2); break;
4067 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4068 case TYPE_UNKNOWN:
4069 case TYPE_MATCH:
4070 case TYPE_NOMATCH: break; /* avoid gcc warning */
4071 }
4072 }
4073 else
4074 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004075 s1 = get_tv_string_buf(rettv, buf1);
4076 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4078 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4079 else
4080 i = 0;
4081 n1 = FALSE;
4082 switch (type)
4083 {
4084 case TYPE_EQUAL: n1 = (i == 0); break;
4085 case TYPE_NEQUAL: n1 = (i != 0); break;
4086 case TYPE_GREATER: n1 = (i > 0); break;
4087 case TYPE_GEQUAL: n1 = (i >= 0); break;
4088 case TYPE_SMALLER: n1 = (i < 0); break;
4089 case TYPE_SEQUAL: n1 = (i <= 0); break;
4090
4091 case TYPE_MATCH:
4092 case TYPE_NOMATCH:
4093 /* avoid 'l' flag in 'cpoptions' */
4094 save_cpo = p_cpo;
4095 p_cpo = (char_u *)"";
4096 regmatch.regprog = vim_regcomp(s2,
4097 RE_MAGIC + RE_STRING);
4098 regmatch.rm_ic = ic;
4099 if (regmatch.regprog != NULL)
4100 {
4101 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4102 vim_free(regmatch.regprog);
4103 if (type == TYPE_NOMATCH)
4104 n1 = !n1;
4105 }
4106 p_cpo = save_cpo;
4107 break;
4108
4109 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4110 }
4111 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 clear_tv(rettv);
4113 clear_tv(&var2);
4114 rettv->v_type = VAR_NUMBER;
4115 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117 }
4118
4119 return OK;
4120}
4121
4122/*
4123 * Handle fourth level expression:
4124 * + number addition
4125 * - number subtraction
4126 * . string concatenation
4127 *
4128 * "arg" must point to the first non-white of the expression.
4129 * "arg" is advanced to the next non-white after the recognized expression.
4130 *
4131 * Return OK or FAIL.
4132 */
4133 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004134eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 int evaluate;
4138{
Bram Moolenaar33570922005-01-25 22:26:29 +00004139 typval_T var2;
4140 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 int op;
4142 long n1, n2;
4143 char_u *s1, *s2;
4144 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4145 char_u *p;
4146
4147 /*
4148 * Get the first variable.
4149 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004150 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 return FAIL;
4152
4153 /*
4154 * Repeat computing, until no '+', '-' or '.' is following.
4155 */
4156 for (;;)
4157 {
4158 op = **arg;
4159 if (op != '+' && op != '-' && op != '.')
4160 break;
4161
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 if (op != '+' || rettv->v_type != VAR_LIST)
4163 {
4164 /* For "list + ...", an illegal use of the first operand as
4165 * a number cannot be determined before evaluating the 2nd
4166 * operand: if this is also a list, all is ok.
4167 * For "something . ...", "something - ..." or "non-list + ...",
4168 * we know that the first operand needs to be a string or number
4169 * without evaluating the 2nd operand. So check before to avoid
4170 * side effects after an error. */
4171 if (evaluate && get_tv_string_chk(rettv) == NULL)
4172 {
4173 clear_tv(rettv);
4174 return FAIL;
4175 }
4176 }
4177
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 /*
4179 * Get the second variable.
4180 */
4181 *arg = skipwhite(*arg + 1);
4182 if (eval6(arg, &var2, evaluate) == FAIL)
4183 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004184 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 return FAIL;
4186 }
4187
4188 if (evaluate)
4189 {
4190 /*
4191 * Compute the result.
4192 */
4193 if (op == '.')
4194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004195 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4196 s2 = get_tv_string_buf_chk(&var2, buf2);
4197 if (s2 == NULL) /* type error ? */
4198 {
4199 clear_tv(rettv);
4200 clear_tv(&var2);
4201 return FAIL;
4202 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004203 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 clear_tv(rettv);
4205 rettv->v_type = VAR_STRING;
4206 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004208 else if (op == '+' && rettv->v_type == VAR_LIST
4209 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004210 {
4211 /* concatenate Lists */
4212 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4213 &var3) == FAIL)
4214 {
4215 clear_tv(rettv);
4216 clear_tv(&var2);
4217 return FAIL;
4218 }
4219 clear_tv(rettv);
4220 *rettv = var3;
4221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 else
4223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004224 int error = FALSE;
4225
4226 n1 = get_tv_number_chk(rettv, &error);
4227 if (error)
4228 {
4229 /* This can only happen for "list + non-list".
4230 * For "non-list + ..." or "something - ...", we returned
4231 * before evaluating the 2nd operand. */
4232 clear_tv(rettv);
4233 return FAIL;
4234 }
4235 n2 = get_tv_number_chk(&var2, &error);
4236 if (error)
4237 {
4238 clear_tv(rettv);
4239 clear_tv(&var2);
4240 return FAIL;
4241 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004242 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 if (op == '+')
4244 n1 = n1 + n2;
4245 else
4246 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 rettv->v_type = VAR_NUMBER;
4248 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004250 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
4252 }
4253 return OK;
4254}
4255
4256/*
4257 * Handle fifth level expression:
4258 * * number multiplication
4259 * / number division
4260 * % number modulo
4261 *
4262 * "arg" must point to the first non-white of the expression.
4263 * "arg" is advanced to the next non-white after the recognized expression.
4264 *
4265 * Return OK or FAIL.
4266 */
4267 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 int evaluate;
4272{
Bram Moolenaar33570922005-01-25 22:26:29 +00004273 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 int op;
4275 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004276 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
4278 /*
4279 * Get the first variable.
4280 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 return FAIL;
4283
4284 /*
4285 * Repeat computing, until no '*', '/' or '%' is following.
4286 */
4287 for (;;)
4288 {
4289 op = **arg;
4290 if (op != '*' && op != '/' && op != '%')
4291 break;
4292
4293 if (evaluate)
4294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004295 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004296 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004297 if (error)
4298 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300 else
4301 n1 = 0;
4302
4303 /*
4304 * Get the second variable.
4305 */
4306 *arg = skipwhite(*arg + 1);
4307 if (eval7(arg, &var2, evaluate) == FAIL)
4308 return FAIL;
4309
4310 if (evaluate)
4311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004312 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004314 if (error)
4315 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316
4317 /*
4318 * Compute the result.
4319 */
4320 if (op == '*')
4321 n1 = n1 * n2;
4322 else if (op == '/')
4323 {
4324 if (n2 == 0) /* give an error message? */
4325 n1 = 0x7fffffffL;
4326 else
4327 n1 = n1 / n2;
4328 }
4329 else
4330 {
4331 if (n2 == 0) /* give an error message? */
4332 n1 = 0;
4333 else
4334 n1 = n1 % n2;
4335 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 rettv->v_type = VAR_NUMBER;
4337 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 }
4339 }
4340
4341 return OK;
4342}
4343
4344/*
4345 * Handle sixth level expression:
4346 * number number constant
4347 * "string" string contstant
4348 * 'string' literal string contstant
4349 * &option-name option value
4350 * @r register contents
4351 * identifier variable value
4352 * function() function call
4353 * $VAR environment variable
4354 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004355 * [expr, expr] List
4356 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 *
4358 * Also handle:
4359 * ! in front logical NOT
4360 * - in front unary minus
4361 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004362 * trailing [] subscript in String or List
4363 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 *
4365 * "arg" must point to the first non-white of the expression.
4366 * "arg" is advanced to the next non-white after the recognized expression.
4367 *
4368 * Return OK or FAIL.
4369 */
4370 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004371eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 int evaluate;
4375{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 long n;
4377 int len;
4378 char_u *s;
4379 int val;
4380 char_u *start_leader, *end_leader;
4381 int ret = OK;
4382 char_u *alias;
4383
4384 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004385 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004386 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004388 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
4390 /*
4391 * Skip '!' and '-' characters. They are handled later.
4392 */
4393 start_leader = *arg;
4394 while (**arg == '!' || **arg == '-' || **arg == '+')
4395 *arg = skipwhite(*arg + 1);
4396 end_leader = *arg;
4397
4398 switch (**arg)
4399 {
4400 /*
4401 * Number constant.
4402 */
4403 case '0':
4404 case '1':
4405 case '2':
4406 case '3':
4407 case '4':
4408 case '5':
4409 case '6':
4410 case '7':
4411 case '8':
4412 case '9':
4413 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4414 *arg += len;
4415 if (evaluate)
4416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004417 rettv->v_type = VAR_NUMBER;
4418 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 }
4420 break;
4421
4422 /*
4423 * String constant: "string".
4424 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004425 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 break;
4427
4428 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004429 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004431 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004432 break;
4433
4434 /*
4435 * List: [expr, expr]
4436 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004437 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 break;
4439
4440 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004441 * Dictionary: {key: val, key: val}
4442 */
4443 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4444 break;
4445
4446 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004447 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004449 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 break;
4451
4452 /*
4453 * Environment variable: $VAR.
4454 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004455 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 break;
4457
4458 /*
4459 * Register contents: @r.
4460 */
4461 case '@': ++*arg;
4462 if (evaluate)
4463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004464 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004465 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 }
4467 if (**arg != NUL)
4468 ++*arg;
4469 break;
4470
4471 /*
4472 * nested expression: (expression).
4473 */
4474 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004475 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 if (**arg == ')')
4477 ++*arg;
4478 else if (ret == OK)
4479 {
4480 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 ret = FAIL;
4483 }
4484 break;
4485
Bram Moolenaar8c711452005-01-14 21:53:12 +00004486 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 break;
4488 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004489
4490 if (ret == NOTDONE)
4491 {
4492 /*
4493 * Must be a variable or function name.
4494 * Can also be a curly-braces kind of name: {expr}.
4495 */
4496 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004497 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004498 if (alias != NULL)
4499 s = alias;
4500
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004501 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004502 ret = FAIL;
4503 else
4504 {
4505 if (**arg == '(') /* recursive! */
4506 {
4507 /* If "s" is the name of a variable of type VAR_FUNC
4508 * use its contents. */
4509 s = deref_func_name(s, &len);
4510
4511 /* Invoke the function. */
4512 ret = get_func_tv(s, len, rettv, arg,
4513 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004514 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004515 /* Stop the expression evaluation when immediately
4516 * aborting on error, or when an interrupt occurred or
4517 * an exception was thrown but not caught. */
4518 if (aborting())
4519 {
4520 if (ret == OK)
4521 clear_tv(rettv);
4522 ret = FAIL;
4523 }
4524 }
4525 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004526 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004527 else
4528 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004529 }
4530
4531 if (alias != NULL)
4532 vim_free(alias);
4533 }
4534
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 *arg = skipwhite(*arg);
4536
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004537 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4538 * expr(expr). */
4539 if (ret == OK)
4540 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541
4542 /*
4543 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4544 */
4545 if (ret == OK && evaluate && end_leader > start_leader)
4546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004547 int error = FALSE;
4548
4549 val = get_tv_number_chk(rettv, &error);
4550 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004552 clear_tv(rettv);
4553 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004555 else
4556 {
4557 while (end_leader > start_leader)
4558 {
4559 --end_leader;
4560 if (*end_leader == '!')
4561 val = !val;
4562 else if (*end_leader == '-')
4563 val = -val;
4564 }
4565 clear_tv(rettv);
4566 rettv->v_type = VAR_NUMBER;
4567 rettv->vval.v_number = val;
4568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
4570
4571 return ret;
4572}
4573
4574/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004575 * Evaluate an "[expr]" or "[expr:expr]" index.
4576 * "*arg" points to the '['.
4577 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4578 */
4579 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004580eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004581 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004582 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004584 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004585{
4586 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004587 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004588 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004589 long len = -1;
4590 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004591 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004592 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004594 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004595 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004596 if (verbose)
4597 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004598 return FAIL;
4599 }
4600
Bram Moolenaar8c711452005-01-14 21:53:12 +00004601 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004602 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004603 /*
4604 * dict.name
4605 */
4606 key = *arg + 1;
4607 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4608 ;
4609 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004610 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004611 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004612 }
4613 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004614 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004615 /*
4616 * something[idx]
4617 *
4618 * Get the (first) variable from inside the [].
4619 */
4620 *arg = skipwhite(*arg + 1);
4621 if (**arg == ':')
4622 empty1 = TRUE;
4623 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4624 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004625 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4626 {
4627 /* not a number or string */
4628 clear_tv(&var1);
4629 return FAIL;
4630 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004631
4632 /*
4633 * Get the second variable from inside the [:].
4634 */
4635 if (**arg == ':')
4636 {
4637 range = TRUE;
4638 *arg = skipwhite(*arg + 1);
4639 if (**arg == ']')
4640 empty2 = TRUE;
4641 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004643 if (!empty1)
4644 clear_tv(&var1);
4645 return FAIL;
4646 }
4647 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4648 {
4649 /* not a number or string */
4650 if (!empty1)
4651 clear_tv(&var1);
4652 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004653 return FAIL;
4654 }
4655 }
4656
4657 /* Check for the ']'. */
4658 if (**arg != ']')
4659 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004660 if (verbose)
4661 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004662 clear_tv(&var1);
4663 if (range)
4664 clear_tv(&var2);
4665 return FAIL;
4666 }
4667 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004668 }
4669
4670 if (evaluate)
4671 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004672 n1 = 0;
4673 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004675 n1 = get_tv_number(&var1);
4676 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004677 }
4678 if (range)
4679 {
4680 if (empty2)
4681 n2 = -1;
4682 else
4683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004684 n2 = get_tv_number(&var2);
4685 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004686 }
4687 }
4688
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004689 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004690 {
4691 case VAR_NUMBER:
4692 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004693 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004694 len = (long)STRLEN(s);
4695 if (range)
4696 {
4697 /* The resulting variable is a substring. If the indexes
4698 * are out of range the result is empty. */
4699 if (n1 < 0)
4700 {
4701 n1 = len + n1;
4702 if (n1 < 0)
4703 n1 = 0;
4704 }
4705 if (n2 < 0)
4706 n2 = len + n2;
4707 else if (n2 >= len)
4708 n2 = len;
4709 if (n1 >= len || n2 < 0 || n1 > n2)
4710 s = NULL;
4711 else
4712 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4713 }
4714 else
4715 {
4716 /* The resulting variable is a string of a single
4717 * character. If the index is too big or negative the
4718 * result is empty. */
4719 if (n1 >= len || n1 < 0)
4720 s = NULL;
4721 else
4722 s = vim_strnsave(s + n1, 1);
4723 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004724 clear_tv(rettv);
4725 rettv->v_type = VAR_STRING;
4726 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004727 break;
4728
4729 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004730 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004731 if (n1 < 0)
4732 n1 = len + n1;
4733 if (!empty1 && (n1 < 0 || n1 >= len))
4734 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004735 if (verbose)
4736 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004737 return FAIL;
4738 }
4739 if (range)
4740 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004741 list_T *l;
4742 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004743
4744 if (n2 < 0)
4745 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004746 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004748 if (verbose)
4749 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750 return FAIL;
4751 }
4752 l = list_alloc();
4753 if (l == NULL)
4754 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004755 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004756 n1 <= n2; ++n1)
4757 {
4758 if (list_append_tv(l, &item->li_tv) == FAIL)
4759 {
4760 list_free(l);
4761 return FAIL;
4762 }
4763 item = item->li_next;
4764 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004765 clear_tv(rettv);
4766 rettv->v_type = VAR_LIST;
4767 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004768 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769 }
4770 else
4771 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004772 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004773 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004774 clear_tv(rettv);
4775 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 }
4777 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004778
4779 case VAR_DICT:
4780 if (range)
4781 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004782 if (verbose)
4783 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004784 if (len == -1)
4785 clear_tv(&var1);
4786 return FAIL;
4787 }
4788 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004789 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790
4791 if (len == -1)
4792 {
4793 key = get_tv_string(&var1);
4794 if (*key == NUL)
4795 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004796 if (verbose)
4797 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004798 clear_tv(&var1);
4799 return FAIL;
4800 }
4801 }
4802
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004803 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004804
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004805 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004806 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004807 if (len == -1)
4808 clear_tv(&var1);
4809 if (item == NULL)
4810 return FAIL;
4811
4812 copy_tv(&item->di_tv, &var1);
4813 clear_tv(rettv);
4814 *rettv = var1;
4815 }
4816 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004817 }
4818 }
4819
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004820 return OK;
4821}
4822
4823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 * Get an option value.
4825 * "arg" points to the '&' or '+' before the option name.
4826 * "arg" is advanced to character after the option name.
4827 * Return OK or FAIL.
4828 */
4829 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004832 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 int evaluate;
4834{
4835 char_u *option_end;
4836 long numval;
4837 char_u *stringval;
4838 int opt_type;
4839 int c;
4840 int working = (**arg == '+'); /* has("+option") */
4841 int ret = OK;
4842 int opt_flags;
4843
4844 /*
4845 * Isolate the option name and find its value.
4846 */
4847 option_end = find_option_end(arg, &opt_flags);
4848 if (option_end == NULL)
4849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004850 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 EMSG2(_("E112: Option name missing: %s"), *arg);
4852 return FAIL;
4853 }
4854
4855 if (!evaluate)
4856 {
4857 *arg = option_end;
4858 return OK;
4859 }
4860
4861 c = *option_end;
4862 *option_end = NUL;
4863 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004864 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
4866 if (opt_type == -3) /* invalid name */
4867 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004868 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 EMSG2(_("E113: Unknown option: %s"), *arg);
4870 ret = FAIL;
4871 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004872 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 {
4874 if (opt_type == -2) /* hidden string option */
4875 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004876 rettv->v_type = VAR_STRING;
4877 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879 else if (opt_type == -1) /* hidden number option */
4880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004881 rettv->v_type = VAR_NUMBER;
4882 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
4884 else if (opt_type == 1) /* number option */
4885 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004886 rettv->v_type = VAR_NUMBER;
4887 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889 else /* string option */
4890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004891 rettv->v_type = VAR_STRING;
4892 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 }
4894 }
4895 else if (working && (opt_type == -2 || opt_type == -1))
4896 ret = FAIL;
4897
4898 *option_end = c; /* put back for error messages */
4899 *arg = option_end;
4900
4901 return ret;
4902}
4903
4904/*
4905 * Allocate a variable for a string constant.
4906 * Return OK or FAIL.
4907 */
4908 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004909get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 int evaluate;
4913{
4914 char_u *p;
4915 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 int extra = 0;
4917
4918 /*
4919 * Find the end of the string, skipping backslashed characters.
4920 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004921 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 {
4923 if (*p == '\\' && p[1] != NUL)
4924 {
4925 ++p;
4926 /* A "\<x>" form occupies at least 4 characters, and produces up
4927 * to 6 characters: reserve space for 2 extra */
4928 if (*p == '<')
4929 extra += 2;
4930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932
4933 if (*p != '"')
4934 {
4935 EMSG2(_("E114: Missing quote: %s"), *arg);
4936 return FAIL;
4937 }
4938
4939 /* If only parsing, set *arg and return here */
4940 if (!evaluate)
4941 {
4942 *arg = p + 1;
4943 return OK;
4944 }
4945
4946 /*
4947 * Copy the string into allocated memory, handling backslashed
4948 * characters.
4949 */
4950 name = alloc((unsigned)(p - *arg + extra));
4951 if (name == NULL)
4952 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004953 rettv->v_type = VAR_STRING;
4954 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955
Bram Moolenaar8c711452005-01-14 21:53:12 +00004956 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
4958 if (*p == '\\')
4959 {
4960 switch (*++p)
4961 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 case 'b': *name++ = BS; ++p; break;
4963 case 'e': *name++ = ESC; ++p; break;
4964 case 'f': *name++ = FF; ++p; break;
4965 case 'n': *name++ = NL; ++p; break;
4966 case 'r': *name++ = CAR; ++p; break;
4967 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
4969 case 'X': /* hex: "\x1", "\x12" */
4970 case 'x':
4971 case 'u': /* Unicode: "\u0023" */
4972 case 'U':
4973 if (vim_isxdigit(p[1]))
4974 {
4975 int n, nr;
4976 int c = toupper(*p);
4977
4978 if (c == 'X')
4979 n = 2;
4980 else
4981 n = 4;
4982 nr = 0;
4983 while (--n >= 0 && vim_isxdigit(p[1]))
4984 {
4985 ++p;
4986 nr = (nr << 4) + hex2nr(*p);
4987 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989#ifdef FEAT_MBYTE
4990 /* For "\u" store the number according to
4991 * 'encoding'. */
4992 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004993 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 else
4995#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004996 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 break;
4999
5000 /* octal: "\1", "\12", "\123" */
5001 case '0':
5002 case '1':
5003 case '2':
5004 case '3':
5005 case '4':
5006 case '5':
5007 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005008 case '7': *name = *p++ - '0';
5009 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005011 *name = (*name << 3) + *p++ - '0';
5012 if (*p >= '0' && *p <= '7')
5013 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005015 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 break;
5017
5018 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005019 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 if (extra != 0)
5021 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 break;
5024 }
5025 /* FALLTHROUGH */
5026
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 break;
5029 }
5030 }
5031 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 *arg = p + 1;
5037
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 return OK;
5039}
5040
5041/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 * Return OK or FAIL.
5044 */
5045 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005046get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 int evaluate;
5050{
5051 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005052 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005053 int reduce = 0;
5054
5055 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005057 */
5058 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5059 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005060 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005061 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005063 break;
5064 ++reduce;
5065 ++p;
5066 }
5067 }
5068
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005070 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005072 return FAIL;
5073 }
5074
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005076 if (!evaluate)
5077 {
5078 *arg = p + 1;
5079 return OK;
5080 }
5081
5082 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005083 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005084 */
5085 str = alloc((unsigned)((p - *arg) - reduce));
5086 if (str == NULL)
5087 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 rettv->v_type = VAR_STRING;
5089 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005090
Bram Moolenaar8c711452005-01-14 21:53:12 +00005091 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005092 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005094 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005096 break;
5097 ++p;
5098 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005100 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005102 *arg = p + 1;
5103
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005104 return OK;
5105}
5106
5107/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005108 * Allocate a variable for a List and fill it from "*arg".
5109 * Return OK or FAIL.
5110 */
5111 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005112get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005113 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005114 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005115 int evaluate;
5116{
Bram Moolenaar33570922005-01-25 22:26:29 +00005117 list_T *l = NULL;
5118 typval_T tv;
5119 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005120
5121 if (evaluate)
5122 {
5123 l = list_alloc();
5124 if (l == NULL)
5125 return FAIL;
5126 }
5127
5128 *arg = skipwhite(*arg + 1);
5129 while (**arg != ']' && **arg != NUL)
5130 {
5131 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5132 goto failret;
5133 if (evaluate)
5134 {
5135 item = listitem_alloc();
5136 if (item != NULL)
5137 {
5138 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005139 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005140 list_append(l, item);
5141 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005142 else
5143 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005144 }
5145
5146 if (**arg == ']')
5147 break;
5148 if (**arg != ',')
5149 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005151 goto failret;
5152 }
5153 *arg = skipwhite(*arg + 1);
5154 }
5155
5156 if (**arg != ']')
5157 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005159failret:
5160 if (evaluate)
5161 list_free(l);
5162 return FAIL;
5163 }
5164
5165 *arg = skipwhite(*arg + 1);
5166 if (evaluate)
5167 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005168 rettv->v_type = VAR_LIST;
5169 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005170 ++l->lv_refcount;
5171 }
5172
5173 return OK;
5174}
5175
5176/*
5177 * Allocate an empty header for a list.
5178 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005179 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005180list_alloc()
5181{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005182 list_T *l;
5183
5184 l = (list_T *)alloc_clear(sizeof(list_T));
5185 if (l != NULL)
5186 {
5187 /* Prepend the list to the list of lists for garbage collection. */
5188 if (first_list != NULL)
5189 first_list->lv_used_prev = l;
5190 l->lv_used_prev = NULL;
5191 l->lv_used_next = first_list;
5192 first_list = l;
5193 }
5194 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005195}
5196
5197/*
5198 * Unreference a list: decrement the reference count and free it when it
5199 * becomes zero.
5200 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005201 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005202list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005203 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005204{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005205 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5206 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005207}
5208
5209/*
5210 * Free a list, including all items it points to.
5211 * Ignores the reference count.
5212 */
5213 static void
5214list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005215 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005216{
Bram Moolenaar33570922005-01-25 22:26:29 +00005217 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005218
Bram Moolenaard9fba312005-06-26 22:34:35 +00005219 /* Avoid that recursive reference to the list frees us again. */
5220 l->lv_refcount = DEL_REFCOUNT;
5221
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005222 /* Remove the list from the list of lists for garbage collection. */
5223 if (l->lv_used_prev == NULL)
5224 first_list = l->lv_used_next;
5225 else
5226 l->lv_used_prev->lv_used_next = l->lv_used_next;
5227 if (l->lv_used_next != NULL)
5228 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5229
Bram Moolenaard9fba312005-06-26 22:34:35 +00005230 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005231 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005232 /* Remove the item before deleting it. */
5233 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005234 listitem_free(item);
5235 }
5236 vim_free(l);
5237}
5238
5239/*
5240 * Allocate a list item.
5241 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005242 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243listitem_alloc()
5244{
Bram Moolenaar33570922005-01-25 22:26:29 +00005245 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246}
5247
5248/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005249 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 */
5251 static void
5252listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005253 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005255 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 vim_free(item);
5257}
5258
5259/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005260 * Remove a list item from a List and free it. Also clears the value.
5261 */
5262 static void
5263listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005264 list_T *l;
5265 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005266{
5267 list_remove(l, item, item);
5268 listitem_free(item);
5269}
5270
5271/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 * Get the number of items in a list.
5273 */
5274 static long
5275list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005276 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 if (l == NULL)
5279 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005280 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281}
5282
5283/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005284 * Return TRUE when two lists have exactly the same values.
5285 */
5286 static int
5287list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005288 list_T *l1;
5289 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005290 int ic; /* ignore case for strings */
5291{
Bram Moolenaar33570922005-01-25 22:26:29 +00005292 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005293
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005294 if (list_len(l1) != list_len(l2))
5295 return FALSE;
5296
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005297 for (item1 = l1->lv_first, item2 = l2->lv_first;
5298 item1 != NULL && item2 != NULL;
5299 item1 = item1->li_next, item2 = item2->li_next)
5300 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5301 return FALSE;
5302 return item1 == NULL && item2 == NULL;
5303}
5304
5305/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005306 * Return TRUE when two dictionaries have exactly the same key/values.
5307 */
5308 static int
5309dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005310 dict_T *d1;
5311 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005312 int ic; /* ignore case for strings */
5313{
Bram Moolenaar33570922005-01-25 22:26:29 +00005314 hashitem_T *hi;
5315 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005316 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005317
5318 if (dict_len(d1) != dict_len(d2))
5319 return FALSE;
5320
Bram Moolenaar33570922005-01-25 22:26:29 +00005321 todo = d1->dv_hashtab.ht_used;
5322 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005323 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005324 if (!HASHITEM_EMPTY(hi))
5325 {
5326 item2 = dict_find(d2, hi->hi_key, -1);
5327 if (item2 == NULL)
5328 return FALSE;
5329 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5330 return FALSE;
5331 --todo;
5332 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005333 }
5334 return TRUE;
5335}
5336
5337/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005338 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005339 * Compares the items just like "==" would compare them, but strings and
5340 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005341 */
5342 static int
5343tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005344 typval_T *tv1;
5345 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005346 int ic; /* ignore case */
5347{
5348 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005349 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005350
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005351 if (tv1->v_type != tv2->v_type)
5352 return FALSE;
5353
5354 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005355 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005356 case VAR_LIST:
5357 /* recursive! */
5358 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5359
5360 case VAR_DICT:
5361 /* recursive! */
5362 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5363
5364 case VAR_FUNC:
5365 return (tv1->vval.v_string != NULL
5366 && tv2->vval.v_string != NULL
5367 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5368
5369 case VAR_NUMBER:
5370 return tv1->vval.v_number == tv2->vval.v_number;
5371
5372 case VAR_STRING:
5373 s1 = get_tv_string_buf(tv1, buf1);
5374 s2 = get_tv_string_buf(tv2, buf2);
5375 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005376 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005377
5378 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005379 return TRUE;
5380}
5381
5382/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 * Locate item with index "n" in list "l" and return it.
5384 * A negative index is counted from the end; -1 is the last item.
5385 * Returns NULL when "n" is out of range.
5386 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005387 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005389 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 long n;
5391{
Bram Moolenaar33570922005-01-25 22:26:29 +00005392 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 long idx;
5394
5395 if (l == NULL)
5396 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005397
5398 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005400 n = l->lv_len + n;
5401
5402 /* Check for index out of range. */
5403 if (n < 0 || n >= l->lv_len)
5404 return NULL;
5405
5406 /* When there is a cached index may start search from there. */
5407 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005409 if (n < l->lv_idx / 2)
5410 {
5411 /* closest to the start of the list */
5412 item = l->lv_first;
5413 idx = 0;
5414 }
5415 else if (n > (l->lv_idx + l->lv_len) / 2)
5416 {
5417 /* closest to the end of the list */
5418 item = l->lv_last;
5419 idx = l->lv_len - 1;
5420 }
5421 else
5422 {
5423 /* closest to the cached index */
5424 item = l->lv_idx_item;
5425 idx = l->lv_idx;
5426 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 }
5428 else
5429 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005430 if (n < l->lv_len / 2)
5431 {
5432 /* closest to the start of the list */
5433 item = l->lv_first;
5434 idx = 0;
5435 }
5436 else
5437 {
5438 /* closest to the end of the list */
5439 item = l->lv_last;
5440 idx = l->lv_len - 1;
5441 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005443
5444 while (n > idx)
5445 {
5446 /* search forward */
5447 item = item->li_next;
5448 ++idx;
5449 }
5450 while (n < idx)
5451 {
5452 /* search backward */
5453 item = item->li_prev;
5454 --idx;
5455 }
5456
5457 /* cache the used index */
5458 l->lv_idx = idx;
5459 l->lv_idx_item = item;
5460
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 return item;
5462}
5463
5464/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005465 * Locate "item" list "l" and return its index.
5466 * Returns -1 when "item" is not in the list.
5467 */
5468 static long
5469list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005470 list_T *l;
5471 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005472{
5473 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005474 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005475
5476 if (l == NULL)
5477 return -1;
5478 idx = 0;
5479 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5480 ++idx;
5481 if (li == NULL)
5482 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005483 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005484}
5485
5486/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 * Append item "item" to the end of list "l".
5488 */
5489 static void
5490list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005491 list_T *l;
5492 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493{
5494 if (l->lv_last == NULL)
5495 {
5496 /* empty list */
5497 l->lv_first = item;
5498 l->lv_last = item;
5499 item->li_prev = NULL;
5500 }
5501 else
5502 {
5503 l->lv_last->li_next = item;
5504 item->li_prev = l->lv_last;
5505 l->lv_last = item;
5506 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005507 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 item->li_next = NULL;
5509}
5510
5511/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005512 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005513 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005514 */
5515 static int
5516list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005517 list_T *l;
5518 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005520 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521
Bram Moolenaar05159a02005-02-26 23:04:13 +00005522 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005524 copy_tv(tv, &li->li_tv);
5525 list_append(l, li);
5526 return OK;
5527}
5528
5529/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005530 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005531 * Return FAIL when out of memory.
5532 */
5533 int
5534list_append_dict(list, dict)
5535 list_T *list;
5536 dict_T *dict;
5537{
5538 listitem_T *li = listitem_alloc();
5539
5540 if (li == NULL)
5541 return FAIL;
5542 li->li_tv.v_type = VAR_DICT;
5543 li->li_tv.v_lock = 0;
5544 li->li_tv.vval.v_dict = dict;
5545 list_append(list, li);
5546 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 return OK;
5548}
5549
5550/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005551 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005552 * If "item" is NULL append at the end.
5553 * Return FAIL when out of memory.
5554 */
5555 static int
5556list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005557 list_T *l;
5558 typval_T *tv;
5559 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005560{
Bram Moolenaar33570922005-01-25 22:26:29 +00005561 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005562
5563 if (ni == NULL)
5564 return FAIL;
5565 copy_tv(tv, &ni->li_tv);
5566 if (item == NULL)
5567 /* Append new item at end of list. */
5568 list_append(l, ni);
5569 else
5570 {
5571 /* Insert new item before existing item. */
5572 ni->li_prev = item->li_prev;
5573 ni->li_next = item;
5574 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005575 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005576 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005577 ++l->lv_idx;
5578 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005579 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005580 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005581 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005582 l->lv_idx_item = NULL;
5583 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005584 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005585 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005586 }
5587 return OK;
5588}
5589
5590/*
5591 * Extend "l1" with "l2".
5592 * If "bef" is NULL append at the end, otherwise insert before this item.
5593 * Returns FAIL when out of memory.
5594 */
5595 static int
5596list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005597 list_T *l1;
5598 list_T *l2;
5599 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005600{
Bram Moolenaar33570922005-01-25 22:26:29 +00005601 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005602
5603 for (item = l2->lv_first; item != NULL; item = item->li_next)
5604 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5605 return FAIL;
5606 return OK;
5607}
5608
5609/*
5610 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5611 * Return FAIL when out of memory.
5612 */
5613 static int
5614list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005615 list_T *l1;
5616 list_T *l2;
5617 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005618{
Bram Moolenaar33570922005-01-25 22:26:29 +00005619 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005620
5621 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005622 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005623 if (l == NULL)
5624 return FAIL;
5625 tv->v_type = VAR_LIST;
5626 tv->vval.v_list = l;
5627
5628 /* append all items from the second list */
5629 return list_extend(l, l2, NULL);
5630}
5631
5632/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005633 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005635 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005636 * Returns NULL when out of memory.
5637 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005638 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005639list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005640 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005641 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005642 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005643{
Bram Moolenaar33570922005-01-25 22:26:29 +00005644 list_T *copy;
5645 listitem_T *item;
5646 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005647
5648 if (orig == NULL)
5649 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005650
5651 copy = list_alloc();
5652 if (copy != NULL)
5653 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005654 if (copyID != 0)
5655 {
5656 /* Do this before adding the items, because one of the items may
5657 * refer back to this list. */
5658 orig->lv_copyID = copyID;
5659 orig->lv_copylist = copy;
5660 }
5661 for (item = orig->lv_first; item != NULL && !got_int;
5662 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005663 {
5664 ni = listitem_alloc();
5665 if (ni == NULL)
5666 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005667 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005668 {
5669 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5670 {
5671 vim_free(ni);
5672 break;
5673 }
5674 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005675 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005677 list_append(copy, ni);
5678 }
5679 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005680 if (item != NULL)
5681 {
5682 list_unref(copy);
5683 copy = NULL;
5684 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005685 }
5686
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005687 return copy;
5688}
5689
5690/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005691 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005692 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005693 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005694 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005695list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005696 list_T *l;
5697 listitem_T *item;
5698 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005699{
Bram Moolenaar33570922005-01-25 22:26:29 +00005700 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005701
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005702 /* notify watchers */
5703 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005704 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005705 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005706 list_fix_watch(l, ip);
5707 if (ip == item2)
5708 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005709 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005710
5711 if (item2->li_next == NULL)
5712 l->lv_last = item->li_prev;
5713 else
5714 item2->li_next->li_prev = item->li_prev;
5715 if (item->li_prev == NULL)
5716 l->lv_first = item2->li_next;
5717 else
5718 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005719 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005720}
5721
5722/*
5723 * Return an allocated string with the string representation of a list.
5724 * May return NULL.
5725 */
5726 static char_u *
5727list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005728 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005729{
5730 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005731
5732 if (tv->vval.v_list == NULL)
5733 return NULL;
5734 ga_init2(&ga, (int)sizeof(char), 80);
5735 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005736 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5737 {
5738 vim_free(ga.ga_data);
5739 return NULL;
5740 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005741 ga_append(&ga, ']');
5742 ga_append(&ga, NUL);
5743 return (char_u *)ga.ga_data;
5744}
5745
5746/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747 * Join list "l" into a string in "*gap", using separator "sep".
5748 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005749 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005750 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005751 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752list_join(gap, l, sep, echo)
5753 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005754 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 char_u *sep;
5756 int echo;
5757{
5758 int first = TRUE;
5759 char_u *tofree;
5760 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005761 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005762 char_u *s;
5763
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005764 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005765 {
5766 if (first)
5767 first = FALSE;
5768 else
5769 ga_concat(gap, sep);
5770
5771 if (echo)
5772 s = echo_string(&item->li_tv, &tofree, numbuf);
5773 else
5774 s = tv2string(&item->li_tv, &tofree, numbuf);
5775 if (s != NULL)
5776 ga_concat(gap, s);
5777 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005778 if (s == NULL)
5779 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005780 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005781 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782}
5783
5784/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005785 * Garbage collection for lists and dictionaries.
5786 *
5787 * We use reference counts to be able to free most items right away when they
5788 * are no longer used. But for composite items it's possible that it becomes
5789 * unused while the reference count is > 0: When there is a recursive
5790 * reference. Example:
5791 * :let l = [1, 2, 3]
5792 * :let d = {9: l}
5793 * :let l[1] = d
5794 *
5795 * Since this is quite unusual we handle this with garbage collection: every
5796 * once in a while find out which lists and dicts are not referenced from any
5797 * variable.
5798 *
5799 * Here is a good reference text about garbage collection (refers to Python
5800 * but it applies to all reference-counting mechanisms):
5801 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005802 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005803
5804/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005805 * Do garbage collection for lists and dicts.
5806 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005807 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005808 int
5809garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005810{
5811 dict_T *dd;
5812 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005813 int copyID = ++current_copyID;
5814 buf_T *buf;
5815 win_T *wp;
5816 int i;
5817 funccall_T *fc;
5818 int did_free = FALSE;
5819
5820 /*
5821 * 1. Go through all accessible variables and mark all lists and dicts
5822 * with copyID.
5823 */
5824 /* script-local variables */
5825 for (i = 1; i <= ga_scripts.ga_len; ++i)
5826 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5827
5828 /* buffer-local variables */
5829 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5830 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5831
5832 /* window-local variables */
5833 FOR_ALL_WINDOWS(wp)
5834 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5835
5836 /* global variables */
5837 set_ref_in_ht(&globvarht, copyID);
5838
5839 /* function-local variables */
5840 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5841 {
5842 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5843 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5844 }
5845
5846 /*
5847 * 2. Go through the list of dicts and free items without the copyID.
5848 */
5849 for (dd = first_dict; dd != NULL; )
5850 if (dd->dv_copyID != copyID)
5851 {
5852 dict_free(dd);
5853 did_free = TRUE;
5854
5855 /* restart, next dict may also have been freed */
5856 dd = first_dict;
5857 }
5858 else
5859 dd = dd->dv_used_next;
5860
5861 /*
5862 * 3. Go through the list of lists and free items without the copyID.
5863 */
5864 for (ll = first_list; ll != NULL; )
5865 if (ll->lv_copyID != copyID)
5866 {
5867 list_free(ll);
5868 did_free = TRUE;
5869
5870 /* restart, next dict may also have been freed */
5871 ll = first_list;
5872 }
5873 else
5874 ll = ll->lv_used_next;
5875
5876 return did_free;
5877}
5878
5879/*
5880 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5881 */
5882 static void
5883set_ref_in_ht(ht, copyID)
5884 hashtab_T *ht;
5885 int copyID;
5886{
5887 int todo;
5888 hashitem_T *hi;
5889
5890 todo = ht->ht_used;
5891 for (hi = ht->ht_array; todo > 0; ++hi)
5892 if (!HASHITEM_EMPTY(hi))
5893 {
5894 --todo;
5895 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5896 }
5897}
5898
5899/*
5900 * Mark all lists and dicts referenced through list "l" with "copyID".
5901 */
5902 static void
5903set_ref_in_list(l, copyID)
5904 list_T *l;
5905 int copyID;
5906{
5907 listitem_T *li;
5908
5909 for (li = l->lv_first; li != NULL; li = li->li_next)
5910 set_ref_in_item(&li->li_tv, copyID);
5911}
5912
5913/*
5914 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5915 */
5916 static void
5917set_ref_in_item(tv, copyID)
5918 typval_T *tv;
5919 int copyID;
5920{
5921 dict_T *dd;
5922 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005923
5924 switch (tv->v_type)
5925 {
5926 case VAR_DICT:
5927 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005928 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005929 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005930 /* Didn't see this dict yet. */
5931 dd->dv_copyID = copyID;
5932 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005933 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005934 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005935
5936 case VAR_LIST:
5937 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005938 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005939 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005940 /* Didn't see this list yet. */
5941 ll->lv_copyID = copyID;
5942 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005943 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005944 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005945 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005946 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005947}
5948
5949/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005950 * Allocate an empty header for a dictionary.
5951 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005952 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953dict_alloc()
5954{
Bram Moolenaar33570922005-01-25 22:26:29 +00005955 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005956
Bram Moolenaar33570922005-01-25 22:26:29 +00005957 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005958 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005959 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005960 /* Add the list to the hashtable for garbage collection. */
5961 if (first_dict != NULL)
5962 first_dict->dv_used_prev = d;
5963 d->dv_used_next = first_dict;
5964 d->dv_used_prev = NULL;
5965
Bram Moolenaar33570922005-01-25 22:26:29 +00005966 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005967 d->dv_lock = 0;
5968 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005969 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005970 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005971 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005972}
5973
5974/*
5975 * Unreference a Dictionary: decrement the reference count and free it when it
5976 * becomes zero.
5977 */
5978 static void
5979dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005980 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005981{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005982 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
5983 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005984}
5985
5986/*
5987 * Free a Dictionary, including all items it contains.
5988 * Ignores the reference count.
5989 */
5990 static void
5991dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005992 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005993{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005994 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005996 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005997
Bram Moolenaard9fba312005-06-26 22:34:35 +00005998 /* Avoid that recursive reference to the dict frees us again. */
5999 d->dv_refcount = DEL_REFCOUNT;
6000
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006001 /* Remove the dict from the list of dicts for garbage collection. */
6002 if (d->dv_used_prev == NULL)
6003 first_dict = d->dv_used_next;
6004 else
6005 d->dv_used_prev->dv_used_next = d->dv_used_next;
6006 if (d->dv_used_next != NULL)
6007 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6008
6009 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006010 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006011 todo = d->dv_hashtab.ht_used;
6012 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006013 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006014 if (!HASHITEM_EMPTY(hi))
6015 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006016 /* Remove the item before deleting it, just in case there is
6017 * something recursive causing trouble. */
6018 di = HI2DI(hi);
6019 hash_remove(&d->dv_hashtab, hi);
6020 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006021 --todo;
6022 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006023 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006024 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006025 vim_free(d);
6026}
6027
6028/*
6029 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006030 * The "key" is copied to the new item.
6031 * Note that the value of the item "di_tv" still needs to be initialized!
6032 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006033 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006035dictitem_alloc(key)
6036 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006037{
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006039
Bram Moolenaar33570922005-01-25 22:26:29 +00006040 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006041 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006042 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006043 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006044 di->di_flags = 0;
6045 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006046 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006047}
6048
6049/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006050 * Make a copy of a Dictionary item.
6051 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006053dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006054 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006055{
Bram Moolenaar33570922005-01-25 22:26:29 +00006056 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006057
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006059 if (di != NULL)
6060 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006061 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006062 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006063 copy_tv(&org->di_tv, &di->di_tv);
6064 }
6065 return di;
6066}
6067
6068/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006069 * Remove item "item" from Dictionary "dict" and free it.
6070 */
6071 static void
6072dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006073 dict_T *dict;
6074 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006075{
Bram Moolenaar33570922005-01-25 22:26:29 +00006076 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006077
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006079 if (HASHITEM_EMPTY(hi))
6080 EMSG2(_(e_intern2), "dictitem_remove()");
6081 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006082 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006083 dictitem_free(item);
6084}
6085
6086/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006087 * Free a dict item. Also clears the value.
6088 */
6089 static void
6090dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006091 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006092{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006093 clear_tv(&item->di_tv);
6094 vim_free(item);
6095}
6096
6097/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006098 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6099 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006100 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006101 * Returns NULL when out of memory.
6102 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006103 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006104dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006105 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006106 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006107 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006108{
Bram Moolenaar33570922005-01-25 22:26:29 +00006109 dict_T *copy;
6110 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006111 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006112 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006113
6114 if (orig == NULL)
6115 return NULL;
6116
6117 copy = dict_alloc();
6118 if (copy != NULL)
6119 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006120 if (copyID != 0)
6121 {
6122 orig->dv_copyID = copyID;
6123 orig->dv_copydict = copy;
6124 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006125 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006126 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006127 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006128 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006129 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006130 --todo;
6131
6132 di = dictitem_alloc(hi->hi_key);
6133 if (di == NULL)
6134 break;
6135 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006136 {
6137 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6138 copyID) == FAIL)
6139 {
6140 vim_free(di);
6141 break;
6142 }
6143 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006144 else
6145 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6146 if (dict_add(copy, di) == FAIL)
6147 {
6148 dictitem_free(di);
6149 break;
6150 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006151 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006152 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006153
Bram Moolenaare9a41262005-01-15 22:18:47 +00006154 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006155 if (todo > 0)
6156 {
6157 dict_unref(copy);
6158 copy = NULL;
6159 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006160 }
6161
6162 return copy;
6163}
6164
6165/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006166 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006167 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006168 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006169 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006170dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006171 dict_T *d;
6172 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006173{
Bram Moolenaar33570922005-01-25 22:26:29 +00006174 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006175}
6176
Bram Moolenaar8c711452005-01-14 21:53:12 +00006177/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006178 * Add a number or string entry to dictionary "d".
6179 * When "str" is NULL use number "nr", otherwise use "str".
6180 * Returns FAIL when out of memory and when key already exists.
6181 */
6182 int
6183dict_add_nr_str(d, key, nr, str)
6184 dict_T *d;
6185 char *key;
6186 long nr;
6187 char_u *str;
6188{
6189 dictitem_T *item;
6190
6191 item = dictitem_alloc((char_u *)key);
6192 if (item == NULL)
6193 return FAIL;
6194 item->di_tv.v_lock = 0;
6195 if (str == NULL)
6196 {
6197 item->di_tv.v_type = VAR_NUMBER;
6198 item->di_tv.vval.v_number = nr;
6199 }
6200 else
6201 {
6202 item->di_tv.v_type = VAR_STRING;
6203 item->di_tv.vval.v_string = vim_strsave(str);
6204 }
6205 if (dict_add(d, item) == FAIL)
6206 {
6207 dictitem_free(item);
6208 return FAIL;
6209 }
6210 return OK;
6211}
6212
6213/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006214 * Get the number of items in a Dictionary.
6215 */
6216 static long
6217dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006218 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006219{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006220 if (d == NULL)
6221 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006222 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006223}
6224
6225/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006226 * Find item "key[len]" in Dictionary "d".
6227 * If "len" is negative use strlen(key).
6228 * Returns NULL when not found.
6229 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006230 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006231dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006232 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006233 char_u *key;
6234 int len;
6235{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006236#define AKEYLEN 200
6237 char_u buf[AKEYLEN];
6238 char_u *akey;
6239 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006240 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006241
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006242 if (len < 0)
6243 akey = key;
6244 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006245 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006246 tofree = akey = vim_strnsave(key, len);
6247 if (akey == NULL)
6248 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006249 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006250 else
6251 {
6252 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006253 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006254 akey = buf;
6255 }
6256
Bram Moolenaar33570922005-01-25 22:26:29 +00006257 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006258 vim_free(tofree);
6259 if (HASHITEM_EMPTY(hi))
6260 return NULL;
6261 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006262}
6263
6264/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006265 * Get a string item from a dictionary in allocated memory.
6266 * Returns NULL if the entry doesn't exist or out of memory.
6267 */
6268 char_u *
6269get_dict_string(d, key)
6270 dict_T *d;
6271 char_u *key;
6272{
6273 dictitem_T *di;
6274
6275 di = dict_find(d, key, -1);
6276 if (di == NULL)
6277 return NULL;
6278 return vim_strsave(get_tv_string(&di->di_tv));
6279}
6280
6281/*
6282 * Get a number item from a dictionary.
6283 * Returns 0 if the entry doesn't exist or out of memory.
6284 */
6285 long
6286get_dict_number(d, key)
6287 dict_T *d;
6288 char_u *key;
6289{
6290 dictitem_T *di;
6291
6292 di = dict_find(d, key, -1);
6293 if (di == NULL)
6294 return 0;
6295 return get_tv_number(&di->di_tv);
6296}
6297
6298/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006299 * Return an allocated string with the string representation of a Dictionary.
6300 * May return NULL.
6301 */
6302 static char_u *
6303dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006304 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006305{
6306 garray_T ga;
6307 int first = TRUE;
6308 char_u *tofree;
6309 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006311 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006313 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006314
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006315 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006316 return NULL;
6317 ga_init2(&ga, (int)sizeof(char), 80);
6318 ga_append(&ga, '{');
6319
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006321 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006322 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006323 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006324 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006325 --todo;
6326
6327 if (first)
6328 first = FALSE;
6329 else
6330 ga_concat(&ga, (char_u *)", ");
6331
6332 tofree = string_quote(hi->hi_key, FALSE);
6333 if (tofree != NULL)
6334 {
6335 ga_concat(&ga, tofree);
6336 vim_free(tofree);
6337 }
6338 ga_concat(&ga, (char_u *)": ");
6339 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6340 if (s != NULL)
6341 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006342 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006343 if (s == NULL)
6344 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006345 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006346 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006347 if (todo > 0)
6348 {
6349 vim_free(ga.ga_data);
6350 return NULL;
6351 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006352
6353 ga_append(&ga, '}');
6354 ga_append(&ga, NUL);
6355 return (char_u *)ga.ga_data;
6356}
6357
6358/*
6359 * Allocate a variable for a Dictionary and fill it from "*arg".
6360 * Return OK or FAIL. Returns NOTDONE for {expr}.
6361 */
6362 static int
6363get_dict_tv(arg, rettv, evaluate)
6364 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006365 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006366 int evaluate;
6367{
Bram Moolenaar33570922005-01-25 22:26:29 +00006368 dict_T *d = NULL;
6369 typval_T tvkey;
6370 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006371 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006372 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006373 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006374 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006375
6376 /*
6377 * First check if it's not a curly-braces thing: {expr}.
6378 * Must do this without evaluating, otherwise a function may be called
6379 * twice. Unfortunately this means we need to call eval1() twice for the
6380 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006381 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006382 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006383 if (*start != '}')
6384 {
6385 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6386 return FAIL;
6387 if (*start == '}')
6388 return NOTDONE;
6389 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006390
6391 if (evaluate)
6392 {
6393 d = dict_alloc();
6394 if (d == NULL)
6395 return FAIL;
6396 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006397 tvkey.v_type = VAR_UNKNOWN;
6398 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006399
6400 *arg = skipwhite(*arg + 1);
6401 while (**arg != '}' && **arg != NUL)
6402 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006403 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006404 goto failret;
6405 if (**arg != ':')
6406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006407 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006408 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006409 goto failret;
6410 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006411 key = get_tv_string_buf_chk(&tvkey, buf);
6412 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006414 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6415 if (key != NULL)
6416 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006417 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006418 goto failret;
6419 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006420
6421 *arg = skipwhite(*arg + 1);
6422 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6423 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006424 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006425 goto failret;
6426 }
6427 if (evaluate)
6428 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006429 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006430 if (item != NULL)
6431 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006432 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006433 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006434 clear_tv(&tv);
6435 goto failret;
6436 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006437 item = dictitem_alloc(key);
6438 clear_tv(&tvkey);
6439 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006440 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006441 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006442 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006443 if (dict_add(d, item) == FAIL)
6444 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006445 }
6446 }
6447
6448 if (**arg == '}')
6449 break;
6450 if (**arg != ',')
6451 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006452 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006453 goto failret;
6454 }
6455 *arg = skipwhite(*arg + 1);
6456 }
6457
6458 if (**arg != '}')
6459 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006460 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006461failret:
6462 if (evaluate)
6463 dict_free(d);
6464 return FAIL;
6465 }
6466
6467 *arg = skipwhite(*arg + 1);
6468 if (evaluate)
6469 {
6470 rettv->v_type = VAR_DICT;
6471 rettv->vval.v_dict = d;
6472 ++d->dv_refcount;
6473 }
6474
6475 return OK;
6476}
6477
Bram Moolenaar8c711452005-01-14 21:53:12 +00006478/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006479 * Return a string with the string representation of a variable.
6480 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006481 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006482 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006483 * May return NULL;
6484 */
6485 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006486echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006488 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006489 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006491 static int recurse = 0;
6492 char_u *r = NULL;
6493
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006495 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006496 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006497 *tofree = NULL;
6498 return NULL;
6499 }
6500 ++recurse;
6501
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006502 switch (tv->v_type)
6503 {
6504 case VAR_FUNC:
6505 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006506 r = tv->vval.v_string;
6507 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006508 case VAR_LIST:
6509 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006510 r = *tofree;
6511 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006512 case VAR_DICT:
6513 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006514 r = *tofree;
6515 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006516 case VAR_STRING:
6517 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006518 *tofree = NULL;
6519 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006521 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006522 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006523 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006524 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006525
6526 --recurse;
6527 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006528}
6529
6530/*
6531 * Return a string with the string representation of a variable.
6532 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6533 * "numbuf" is used for a number.
6534 * Puts quotes around strings, so that they can be parsed back by eval().
6535 * May return NULL;
6536 */
6537 static char_u *
6538tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006539 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006540 char_u **tofree;
6541 char_u *numbuf;
6542{
6543 switch (tv->v_type)
6544 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006545 case VAR_FUNC:
6546 *tofree = string_quote(tv->vval.v_string, TRUE);
6547 return *tofree;
6548 case VAR_STRING:
6549 *tofree = string_quote(tv->vval.v_string, FALSE);
6550 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006551 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006552 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006553 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006555 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006556 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006558 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006559}
6560
6561/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006562 * Return string "str" in ' quotes, doubling ' characters.
6563 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006564 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006565 */
6566 static char_u *
6567string_quote(str, function)
6568 char_u *str;
6569 int function;
6570{
Bram Moolenaar33570922005-01-25 22:26:29 +00006571 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006572 char_u *p, *r, *s;
6573
Bram Moolenaar33570922005-01-25 22:26:29 +00006574 len = (function ? 13 : 3);
6575 if (str != NULL)
6576 {
6577 len += STRLEN(str);
6578 for (p = str; *p != NUL; mb_ptr_adv(p))
6579 if (*p == '\'')
6580 ++len;
6581 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006582 s = r = alloc(len);
6583 if (r != NULL)
6584 {
6585 if (function)
6586 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006587 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006588 r += 10;
6589 }
6590 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006591 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006592 if (str != NULL)
6593 for (p = str; *p != NUL; )
6594 {
6595 if (*p == '\'')
6596 *r++ = '\'';
6597 MB_COPY_CHAR(p, r);
6598 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006599 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006600 if (function)
6601 *r++ = ')';
6602 *r++ = NUL;
6603 }
6604 return s;
6605}
6606
6607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 * Get the value of an environment variable.
6609 * "arg" is pointing to the '$'. It is advanced to after the name.
6610 * If the environment variable was not set, silently assume it is empty.
6611 * Always return OK.
6612 */
6613 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006614get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 int evaluate;
6618{
6619 char_u *string = NULL;
6620 int len;
6621 int cc;
6622 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006623 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624
6625 ++*arg;
6626 name = *arg;
6627 len = get_env_len(arg);
6628 if (evaluate)
6629 {
6630 if (len != 0)
6631 {
6632 cc = name[len];
6633 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006634 /* first try vim_getenv(), fast for normal environment vars */
6635 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006637 {
6638 if (!mustfree)
6639 string = vim_strsave(string);
6640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 else
6642 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006643 if (mustfree)
6644 vim_free(string);
6645
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 /* next try expanding things like $VIM and ${HOME} */
6647 string = expand_env_save(name - 1);
6648 if (string != NULL && *string == '$')
6649 {
6650 vim_free(string);
6651 string = NULL;
6652 }
6653 }
6654 name[len] = cc;
6655 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006656 rettv->v_type = VAR_STRING;
6657 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 }
6659
6660 return OK;
6661}
6662
6663/*
6664 * Array with names and number of arguments of all internal functions
6665 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6666 */
6667static struct fst
6668{
6669 char *f_name; /* function name */
6670 char f_min_argc; /* minimal number of arguments */
6671 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006672 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006673 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674} functions[] =
6675{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006676 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 {"append", 2, 2, f_append},
6678 {"argc", 0, 0, f_argc},
6679 {"argidx", 0, 0, f_argidx},
6680 {"argv", 1, 1, f_argv},
6681 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006682 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 {"bufexists", 1, 1, f_bufexists},
6684 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6685 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6686 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6687 {"buflisted", 1, 1, f_buflisted},
6688 {"bufloaded", 1, 1, f_bufloaded},
6689 {"bufname", 1, 1, f_bufname},
6690 {"bufnr", 1, 1, f_bufnr},
6691 {"bufwinnr", 1, 1, f_bufwinnr},
6692 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006693 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006694 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 {"char2nr", 1, 1, f_char2nr},
6696 {"cindent", 1, 1, f_cindent},
6697 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006698#if defined(FEAT_INS_EXPAND)
6699 {"complete_add", 1, 1, f_complete_add},
6700 {"complete_check", 0, 0, f_complete_check},
6701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006703 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006704 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 {"cscope_connection",0,3, f_cscope_connection},
6706 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006707 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 {"delete", 1, 1, f_delete},
6709 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006710 {"diff_filler", 1, 1, f_diff_filler},
6711 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006712 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006714 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 {"eventhandler", 0, 0, f_eventhandler},
6716 {"executable", 1, 1, f_executable},
6717 {"exists", 1, 1, f_exists},
6718 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006719 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6721 {"filereadable", 1, 1, f_filereadable},
6722 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006723 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006724 {"finddir", 1, 3, f_finddir},
6725 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 {"fnamemodify", 2, 2, f_fnamemodify},
6727 {"foldclosed", 1, 1, f_foldclosed},
6728 {"foldclosedend", 1, 1, f_foldclosedend},
6729 {"foldlevel", 1, 1, f_foldlevel},
6730 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006731 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006733 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006734 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006735 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006736 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 {"getbufvar", 2, 2, f_getbufvar},
6738 {"getchar", 0, 1, f_getchar},
6739 {"getcharmod", 0, 0, f_getcharmod},
6740 {"getcmdline", 0, 0, f_getcmdline},
6741 {"getcmdpos", 0, 0, f_getcmdpos},
6742 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006743 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006744 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 {"getfsize", 1, 1, f_getfsize},
6746 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006747 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006748 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006749 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006750 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 {"getregtype", 0, 1, f_getregtype},
6752 {"getwinposx", 0, 0, f_getwinposx},
6753 {"getwinposy", 0, 0, f_getwinposy},
6754 {"getwinvar", 2, 2, f_getwinvar},
6755 {"glob", 1, 1, f_glob},
6756 {"globpath", 2, 2, f_globpath},
6757 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006758 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 {"hasmapto", 1, 2, f_hasmapto},
6760 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6761 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6762 {"histadd", 2, 2, f_histadd},
6763 {"histdel", 1, 2, f_histdel},
6764 {"histget", 1, 2, f_histget},
6765 {"histnr", 1, 1, f_histnr},
6766 {"hlID", 1, 1, f_hlID},
6767 {"hlexists", 1, 1, f_hlexists},
6768 {"hostname", 0, 0, f_hostname},
6769 {"iconv", 3, 3, f_iconv},
6770 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006771 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 {"input", 1, 2, f_input},
6773 {"inputdialog", 1, 3, f_inputdialog},
6774 {"inputrestore", 0, 0, f_inputrestore},
6775 {"inputsave", 0, 0, f_inputsave},
6776 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006777 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006779 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006780 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006781 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006782 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006784 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 {"libcall", 3, 3, f_libcall},
6786 {"libcallnr", 3, 3, f_libcallnr},
6787 {"line", 1, 1, f_line},
6788 {"line2byte", 1, 1, f_line2byte},
6789 {"lispindent", 1, 1, f_lispindent},
6790 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006791 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 {"maparg", 1, 2, f_maparg},
6793 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006794 {"match", 2, 4, f_match},
6795 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006796 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006797 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006798 {"max", 1, 1, f_max},
6799 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006800#ifdef vim_mkdir
6801 {"mkdir", 1, 3, f_mkdir},
6802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 {"mode", 0, 0, f_mode},
6804 {"nextnonblank", 1, 1, f_nextnonblank},
6805 {"nr2char", 1, 1, f_nr2char},
6806 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006807 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006808 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006809 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 {"remote_expr", 2, 3, f_remote_expr},
6811 {"remote_foreground", 1, 1, f_remote_foreground},
6812 {"remote_peek", 1, 2, f_remote_peek},
6813 {"remote_read", 1, 1, f_remote_read},
6814 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006815 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006817 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006819 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 {"search", 1, 2, f_search},
6821 {"searchpair", 3, 5, f_searchpair},
6822 {"server2client", 2, 2, f_server2client},
6823 {"serverlist", 0, 0, f_serverlist},
6824 {"setbufvar", 3, 3, f_setbufvar},
6825 {"setcmdpos", 1, 1, f_setcmdpos},
6826 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006827 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 {"setreg", 2, 3, f_setreg},
6829 {"setwinvar", 3, 3, f_setwinvar},
6830 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006831 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006832 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006833 {"spellbadword", 0, 0, f_spellbadword},
6834 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006835 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836#ifdef HAVE_STRFTIME
6837 {"strftime", 1, 2, f_strftime},
6838#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006839 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006840 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 {"strlen", 1, 1, f_strlen},
6842 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006843 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 {"strtrans", 1, 1, f_strtrans},
6845 {"submatch", 1, 1, f_submatch},
6846 {"substitute", 4, 4, f_substitute},
6847 {"synID", 3, 3, f_synID},
6848 {"synIDattr", 2, 3, f_synIDattr},
6849 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006850 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006851 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006853 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 {"tolower", 1, 1, f_tolower},
6855 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006856 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006858 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {"virtcol", 1, 1, f_virtcol},
6860 {"visualmode", 0, 1, f_visualmode},
6861 {"winbufnr", 1, 1, f_winbufnr},
6862 {"wincol", 0, 0, f_wincol},
6863 {"winheight", 1, 1, f_winheight},
6864 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006865 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 {"winrestcmd", 0, 0, f_winrestcmd},
6867 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006868 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869};
6870
6871#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6872
6873/*
6874 * Function given to ExpandGeneric() to obtain the list of internal
6875 * or user defined function names.
6876 */
6877 char_u *
6878get_function_name(xp, idx)
6879 expand_T *xp;
6880 int idx;
6881{
6882 static int intidx = -1;
6883 char_u *name;
6884
6885 if (idx == 0)
6886 intidx = -1;
6887 if (intidx < 0)
6888 {
6889 name = get_user_func_name(xp, idx);
6890 if (name != NULL)
6891 return name;
6892 }
6893 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6894 {
6895 STRCPY(IObuff, functions[intidx].f_name);
6896 STRCAT(IObuff, "(");
6897 if (functions[intidx].f_max_argc == 0)
6898 STRCAT(IObuff, ")");
6899 return IObuff;
6900 }
6901
6902 return NULL;
6903}
6904
6905/*
6906 * Function given to ExpandGeneric() to obtain the list of internal or
6907 * user defined variable or function names.
6908 */
6909/*ARGSUSED*/
6910 char_u *
6911get_expr_name(xp, idx)
6912 expand_T *xp;
6913 int idx;
6914{
6915 static int intidx = -1;
6916 char_u *name;
6917
6918 if (idx == 0)
6919 intidx = -1;
6920 if (intidx < 0)
6921 {
6922 name = get_function_name(xp, idx);
6923 if (name != NULL)
6924 return name;
6925 }
6926 return get_user_var_name(xp, ++intidx);
6927}
6928
6929#endif /* FEAT_CMDL_COMPL */
6930
6931/*
6932 * Find internal function in table above.
6933 * Return index, or -1 if not found
6934 */
6935 static int
6936find_internal_func(name)
6937 char_u *name; /* name of the function */
6938{
6939 int first = 0;
6940 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6941 int cmp;
6942 int x;
6943
6944 /*
6945 * Find the function name in the table. Binary search.
6946 */
6947 while (first <= last)
6948 {
6949 x = first + ((unsigned)(last - first) >> 1);
6950 cmp = STRCMP(name, functions[x].f_name);
6951 if (cmp < 0)
6952 last = x - 1;
6953 else if (cmp > 0)
6954 first = x + 1;
6955 else
6956 return x;
6957 }
6958 return -1;
6959}
6960
6961/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006962 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6963 * name it contains, otherwise return "name".
6964 */
6965 static char_u *
6966deref_func_name(name, lenp)
6967 char_u *name;
6968 int *lenp;
6969{
Bram Moolenaar33570922005-01-25 22:26:29 +00006970 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006971 int cc;
6972
6973 cc = name[*lenp];
6974 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006975 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006976 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006978 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006979 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006980 {
6981 *lenp = 0;
6982 return (char_u *)""; /* just in case */
6983 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006984 *lenp = STRLEN(v->di_tv.vval.v_string);
6985 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006986 }
6987
6988 return name;
6989}
6990
6991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 * Allocate a variable for the result of a function.
6993 * Return OK or FAIL.
6994 */
6995 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006996get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6997 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 char_u *name; /* name of the function */
6999 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 char_u **arg; /* argument, pointing to the '(' */
7002 linenr_T firstline; /* first line of range */
7003 linenr_T lastline; /* last line of range */
7004 int *doesrange; /* return: function handled range */
7005 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007006 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007{
7008 char_u *argp;
7009 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007010 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 int argcount = 0; /* number of arguments found */
7012
7013 /*
7014 * Get the arguments.
7015 */
7016 argp = *arg;
7017 while (argcount < MAX_FUNC_ARGS)
7018 {
7019 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7020 if (*argp == ')' || *argp == ',' || *argp == NUL)
7021 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7023 {
7024 ret = FAIL;
7025 break;
7026 }
7027 ++argcount;
7028 if (*argp != ',')
7029 break;
7030 }
7031 if (*argp == ')')
7032 ++argp;
7033 else
7034 ret = FAIL;
7035
7036 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007037 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007038 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007040 {
7041 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007042 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007043 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007044 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046
7047 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007048 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049
7050 *arg = skipwhite(argp);
7051 return ret;
7052}
7053
7054
7055/*
7056 * Call a function with its resolved parameters
7057 * Return OK or FAIL.
7058 */
7059 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007060call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007061 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 char_u *name; /* name of the function */
7063 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007064 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007066 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 linenr_T firstline; /* first line of range */
7068 linenr_T lastline; /* last line of range */
7069 int *doesrange; /* return: function handled range */
7070 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007071 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072{
7073 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074#define ERROR_UNKNOWN 0
7075#define ERROR_TOOMANY 1
7076#define ERROR_TOOFEW 2
7077#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007078#define ERROR_DICT 4
7079#define ERROR_NONE 5
7080#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 int error = ERROR_NONE;
7082 int i;
7083 int llen;
7084 ufunc_T *fp;
7085 int cc;
7086#define FLEN_FIXED 40
7087 char_u fname_buf[FLEN_FIXED + 1];
7088 char_u *fname;
7089
7090 /*
7091 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7092 * Change <SNR>123_name() to K_SNR 123_name().
7093 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7094 */
7095 cc = name[len];
7096 name[len] = NUL;
7097 llen = eval_fname_script(name);
7098 if (llen > 0)
7099 {
7100 fname_buf[0] = K_SPECIAL;
7101 fname_buf[1] = KS_EXTRA;
7102 fname_buf[2] = (int)KE_SNR;
7103 i = 3;
7104 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7105 {
7106 if (current_SID <= 0)
7107 error = ERROR_SCRIPT;
7108 else
7109 {
7110 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7111 i = (int)STRLEN(fname_buf);
7112 }
7113 }
7114 if (i + STRLEN(name + llen) < FLEN_FIXED)
7115 {
7116 STRCPY(fname_buf + i, name + llen);
7117 fname = fname_buf;
7118 }
7119 else
7120 {
7121 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7122 if (fname == NULL)
7123 error = ERROR_OTHER;
7124 else
7125 {
7126 mch_memmove(fname, fname_buf, (size_t)i);
7127 STRCPY(fname + i, name + llen);
7128 }
7129 }
7130 }
7131 else
7132 fname = name;
7133
7134 *doesrange = FALSE;
7135
7136
7137 /* execute the function if no errors detected and executing */
7138 if (evaluate && error == ERROR_NONE)
7139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007140 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 error = ERROR_UNKNOWN;
7142
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007143 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 {
7145 /*
7146 * User defined function.
7147 */
7148 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007149
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007151 /* Trigger FuncUndefined event, may load the function. */
7152 if (fp == NULL
7153 && apply_autocmds(EVENT_FUNCUNDEFINED,
7154 fname, fname, TRUE, NULL)
7155 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007157 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 fp = find_func(fname);
7159 }
7160#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007161 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007162 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007163 {
7164 /* loaded a package, search for the function again */
7165 fp = find_func(fname);
7166 }
7167
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 if (fp != NULL)
7169 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007170 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007172 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007174 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007176 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007177 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 else
7179 {
7180 /*
7181 * Call the user function.
7182 * Save and restore search patterns, script variables and
7183 * redo buffer.
7184 */
7185 save_search_patterns();
7186 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007187 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007188 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007189 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007190 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7191 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7192 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007193 /* Function was unreferenced while being used, free it
7194 * now. */
7195 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 restoreRedobuff();
7197 restore_search_patterns();
7198 error = ERROR_NONE;
7199 }
7200 }
7201 }
7202 else
7203 {
7204 /*
7205 * Find the function name in the table, call its implementation.
7206 */
7207 i = find_internal_func(fname);
7208 if (i >= 0)
7209 {
7210 if (argcount < functions[i].f_min_argc)
7211 error = ERROR_TOOFEW;
7212 else if (argcount > functions[i].f_max_argc)
7213 error = ERROR_TOOMANY;
7214 else
7215 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007216 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007217 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 error = ERROR_NONE;
7219 }
7220 }
7221 }
7222 /*
7223 * The function call (or "FuncUndefined" autocommand sequence) might
7224 * have been aborted by an error, an interrupt, or an explicitly thrown
7225 * exception that has not been caught so far. This situation can be
7226 * tested for by calling aborting(). For an error in an internal
7227 * function or for the "E132" error in call_user_func(), however, the
7228 * throw point at which the "force_abort" flag (temporarily reset by
7229 * emsg()) is normally updated has not been reached yet. We need to
7230 * update that flag first to make aborting() reliable.
7231 */
7232 update_force_abort();
7233 }
7234 if (error == ERROR_NONE)
7235 ret = OK;
7236
7237 /*
7238 * Report an error unless the argument evaluation or function call has been
7239 * cancelled due to an aborting error, an interrupt, or an exception.
7240 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007241 if (!aborting())
7242 {
7243 switch (error)
7244 {
7245 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007246 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247 break;
7248 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007249 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 break;
7251 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007252 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253 name);
7254 break;
7255 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007256 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257 name);
7258 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007259 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007260 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007261 name);
7262 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263 }
7264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265
7266 name[len] = cc;
7267 if (fname != name && fname != fname_buf)
7268 vim_free(fname);
7269
7270 return ret;
7271}
7272
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007273/*
7274 * Give an error message with a function name. Handle <SNR> things.
7275 */
7276 static void
7277emsg_funcname(msg, name)
7278 char *msg;
7279 char_u *name;
7280{
7281 char_u *p;
7282
7283 if (*name == K_SPECIAL)
7284 p = concat_str((char_u *)"<SNR>", name + 3);
7285 else
7286 p = name;
7287 EMSG2(_(msg), p);
7288 if (p != name)
7289 vim_free(p);
7290}
7291
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292/*********************************************
7293 * Implementation of the built-in functions
7294 */
7295
7296/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007297 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 */
7299 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007300f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 typval_T *argvars;
7302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007306 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007307 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007309 if ((l = argvars[0].vval.v_list) != NULL
7310 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7311 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007312 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007313 }
7314 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007315 EMSG(_(e_listreq));
7316}
7317
7318/*
7319 * "append(lnum, string/list)" function
7320 */
7321 static void
7322f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 typval_T *argvars;
7324 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007325{
7326 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007327 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 list_T *l = NULL;
7329 listitem_T *li = NULL;
7330 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007331 long added = 0;
7332
Bram Moolenaar0d660222005-01-07 21:51:51 +00007333 lnum = get_tv_lnum(argvars);
7334 if (lnum >= 0
7335 && lnum <= curbuf->b_ml.ml_line_count
7336 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007337 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007338 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007339 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007340 l = argvars[1].vval.v_list;
7341 if (l == NULL)
7342 return;
7343 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007344 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007345 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007346 for (;;)
7347 {
7348 if (l == NULL)
7349 tv = &argvars[1]; /* append a string */
7350 else if (li == NULL)
7351 break; /* end of list */
7352 else
7353 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007354 line = get_tv_string_chk(tv);
7355 if (line == NULL) /* type error */
7356 {
7357 rettv->vval.v_number = 1; /* Failed */
7358 break;
7359 }
7360 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007361 ++added;
7362 if (l == NULL)
7363 break;
7364 li = li->li_next;
7365 }
7366
7367 appended_lines_mark(lnum, added);
7368 if (curwin->w_cursor.lnum > lnum)
7369 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007371 else
7372 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373}
7374
7375/*
7376 * "argc()" function
7377 */
7378/* ARGSUSED */
7379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007380f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007381 typval_T *argvars;
7382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007384 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385}
7386
7387/*
7388 * "argidx()" function
7389 */
7390/* ARGSUSED */
7391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007392f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 typval_T *argvars;
7394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007396 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397}
7398
7399/*
7400 * "argv(nr)" function
7401 */
7402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007403f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007404 typval_T *argvars;
7405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406{
7407 int idx;
7408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007409 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007411 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007413 rettv->vval.v_string = NULL;
7414 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415}
7416
7417/*
7418 * "browse(save, title, initdir, default)" function
7419 */
7420/* ARGSUSED */
7421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007422f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007423 typval_T *argvars;
7424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425{
7426#ifdef FEAT_BROWSE
7427 int save;
7428 char_u *title;
7429 char_u *initdir;
7430 char_u *defname;
7431 char_u buf[NUMBUFLEN];
7432 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007433 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007435 save = get_tv_number_chk(&argvars[0], &error);
7436 title = get_tv_string_chk(&argvars[1]);
7437 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7438 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007440 if (error || title == NULL || initdir == NULL || defname == NULL)
7441 rettv->vval.v_string = NULL;
7442 else
7443 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007444 do_browse(save ? BROWSE_SAVE : 0,
7445 title, defname, NULL, initdir, NULL, curbuf);
7446#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007447 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007448#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007449 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007450}
7451
7452/*
7453 * "browsedir(title, initdir)" function
7454 */
7455/* ARGSUSED */
7456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007457f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007458 typval_T *argvars;
7459 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007460{
7461#ifdef FEAT_BROWSE
7462 char_u *title;
7463 char_u *initdir;
7464 char_u buf[NUMBUFLEN];
7465
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007466 title = get_tv_string_chk(&argvars[0]);
7467 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007468
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007469 if (title == NULL || initdir == NULL)
7470 rettv->vval.v_string = NULL;
7471 else
7472 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007473 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007475 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007477 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478}
7479
Bram Moolenaar33570922005-01-25 22:26:29 +00007480static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007481
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482/*
7483 * Find a buffer by number or exact name.
7484 */
7485 static buf_T *
7486find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007487 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488{
7489 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007491 if (avar->v_type == VAR_NUMBER)
7492 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007493 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007495 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007496 if (buf == NULL)
7497 {
7498 /* No full path name match, try a match with a URL or a "nofile"
7499 * buffer, these don't use the full path. */
7500 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7501 if (buf->b_fname != NULL
7502 && (path_with_url(buf->b_fname)
7503#ifdef FEAT_QUICKFIX
7504 || bt_nofile(buf)
7505#endif
7506 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007507 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007508 break;
7509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 }
7511 return buf;
7512}
7513
7514/*
7515 * "bufexists(expr)" function
7516 */
7517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007518f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007519 typval_T *argvars;
7520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007522 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523}
7524
7525/*
7526 * "buflisted(expr)" function
7527 */
7528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007529f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007530 typval_T *argvars;
7531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532{
7533 buf_T *buf;
7534
7535 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007536 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537}
7538
7539/*
7540 * "bufloaded(expr)" function
7541 */
7542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007543f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 typval_T *argvars;
7545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546{
7547 buf_T *buf;
7548
7549 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007550 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551}
7552
Bram Moolenaar33570922005-01-25 22:26:29 +00007553static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007554
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555/*
7556 * Get buffer by number or pattern.
7557 */
7558 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007559get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007560 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007562 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 int save_magic;
7564 char_u *save_cpo;
7565 buf_T *buf;
7566
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007567 if (tv->v_type == VAR_NUMBER)
7568 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007569 if (tv->v_type != VAR_STRING)
7570 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 if (name == NULL || *name == NUL)
7572 return curbuf;
7573 if (name[0] == '$' && name[1] == NUL)
7574 return lastbuf;
7575
7576 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7577 save_magic = p_magic;
7578 p_magic = TRUE;
7579 save_cpo = p_cpo;
7580 p_cpo = (char_u *)"";
7581
7582 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7583 TRUE, FALSE));
7584
7585 p_magic = save_magic;
7586 p_cpo = save_cpo;
7587
7588 /* If not found, try expanding the name, like done for bufexists(). */
7589 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007590 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591
7592 return buf;
7593}
7594
7595/*
7596 * "bufname(expr)" function
7597 */
7598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007599f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007600 typval_T *argvars;
7601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602{
7603 buf_T *buf;
7604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007605 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007607 buf = get_buf_tv(&argvars[0]);
7608 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007610 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007612 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 --emsg_off;
7614}
7615
7616/*
7617 * "bufnr(expr)" function
7618 */
7619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007620f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 typval_T *argvars;
7622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623{
7624 buf_T *buf;
7625
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007626 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007628 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007630 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007632 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 --emsg_off;
7634}
7635
7636/*
7637 * "bufwinnr(nr)" function
7638 */
7639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007640f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007641 typval_T *argvars;
7642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643{
7644#ifdef FEAT_WINDOWS
7645 win_T *wp;
7646 int winnr = 0;
7647#endif
7648 buf_T *buf;
7649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007650 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007652 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653#ifdef FEAT_WINDOWS
7654 for (wp = firstwin; wp; wp = wp->w_next)
7655 {
7656 ++winnr;
7657 if (wp->w_buffer == buf)
7658 break;
7659 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007660 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007662 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663#endif
7664 --emsg_off;
7665}
7666
7667/*
7668 * "byte2line(byte)" function
7669 */
7670/*ARGSUSED*/
7671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007672f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 typval_T *argvars;
7674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675{
7676#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007677 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678#else
7679 long boff = 0;
7680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007681 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007683 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 (linenr_T)0, &boff);
7687#endif
7688}
7689
7690/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007691 * "byteidx()" function
7692 */
7693/*ARGSUSED*/
7694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007695f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007696 typval_T *argvars;
7697 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007698{
7699#ifdef FEAT_MBYTE
7700 char_u *t;
7701#endif
7702 char_u *str;
7703 long idx;
7704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007705 str = get_tv_string_chk(&argvars[0]);
7706 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007707 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007708 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007709 return;
7710
7711#ifdef FEAT_MBYTE
7712 t = str;
7713 for ( ; idx > 0; idx--)
7714 {
7715 if (*t == NUL) /* EOL reached */
7716 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007717 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007718 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007719 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007720#else
7721 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007722 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007723#endif
7724}
7725
7726/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007727 * "call(func, arglist)" function
7728 */
7729 static void
7730f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007731 typval_T *argvars;
7732 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007733{
7734 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007735 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007736 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007738 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007739 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007740
7741 rettv->vval.v_number = 0;
7742 if (argvars[1].v_type != VAR_LIST)
7743 {
7744 EMSG(_(e_listreq));
7745 return;
7746 }
7747 if (argvars[1].vval.v_list == NULL)
7748 return;
7749
7750 if (argvars[0].v_type == VAR_FUNC)
7751 func = argvars[0].vval.v_string;
7752 else
7753 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007754 if (*func == NUL)
7755 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007756
Bram Moolenaare9a41262005-01-15 22:18:47 +00007757 if (argvars[2].v_type != VAR_UNKNOWN)
7758 {
7759 if (argvars[2].v_type != VAR_DICT)
7760 {
7761 EMSG(_(e_dictreq));
7762 return;
7763 }
7764 selfdict = argvars[2].vval.v_dict;
7765 }
7766
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007767 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7768 item = item->li_next)
7769 {
7770 if (argc == MAX_FUNC_ARGS)
7771 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007772 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007773 break;
7774 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007775 /* Make a copy of each argument. This is needed to be able to set
7776 * v_lock to VAR_FIXED in the copy without changing the original list.
7777 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007778 copy_tv(&item->li_tv, &argv[argc++]);
7779 }
7780
7781 if (item == NULL)
7782 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007783 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7784 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007785
7786 /* Free the arguments. */
7787 while (argc > 0)
7788 clear_tv(&argv[--argc]);
7789}
7790
7791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 * "char2nr(string)" function
7793 */
7794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007795f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 typval_T *argvars;
7797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798{
7799#ifdef FEAT_MBYTE
7800 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007801 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 else
7803#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805}
7806
7807/*
7808 * "cindent(lnum)" function
7809 */
7810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007811f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007812 typval_T *argvars;
7813 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814{
7815#ifdef FEAT_CINDENT
7816 pos_T pos;
7817 linenr_T lnum;
7818
7819 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007820 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7822 {
7823 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007824 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 curwin->w_cursor = pos;
7826 }
7827 else
7828#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007829 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830}
7831
7832/*
7833 * "col(string)" function
7834 */
7835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007836f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007837 typval_T *argvars;
7838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839{
7840 colnr_T col = 0;
7841 pos_T *fp;
7842
7843 fp = var2fpos(&argvars[0], FALSE);
7844 if (fp != NULL)
7845 {
7846 if (fp->col == MAXCOL)
7847 {
7848 /* '> can be MAXCOL, get the length of the line then */
7849 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7850 col = STRLEN(ml_get(fp->lnum)) + 1;
7851 else
7852 col = MAXCOL;
7853 }
7854 else
7855 {
7856 col = fp->col + 1;
7857#ifdef FEAT_VIRTUALEDIT
7858 /* col(".") when the cursor is on the NUL at the end of the line
7859 * because of "coladd" can be seen as an extra column. */
7860 if (virtual_active() && fp == &curwin->w_cursor)
7861 {
7862 char_u *p = ml_get_cursor();
7863
7864 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7865 curwin->w_virtcol - curwin->w_cursor.coladd))
7866 {
7867# ifdef FEAT_MBYTE
7868 int l;
7869
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007870 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 col += l;
7872# else
7873 if (*p != NUL && p[1] == NUL)
7874 ++col;
7875# endif
7876 }
7877 }
7878#endif
7879 }
7880 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007881 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882}
7883
Bram Moolenaar572cb562005-08-05 21:35:02 +00007884#if defined(FEAT_INS_EXPAND)
7885/*
7886 * "complete_add()" function
7887 */
7888/*ARGSUSED*/
7889 static void
7890f_complete_add(argvars, rettv)
7891 typval_T *argvars;
7892 typval_T *rettv;
7893{
7894 char_u *s;
7895
7896 s = get_tv_string_chk(&argvars[0]);
7897 if (s != NULL)
7898 rettv->vval.v_number = ins_compl_add(s, -1, NULL, FORWARD, 0);
7899}
7900
7901/*
7902 * "complete_check()" function
7903 */
7904/*ARGSUSED*/
7905 static void
7906f_complete_check(argvars, rettv)
7907 typval_T *argvars;
7908 typval_T *rettv;
7909{
7910 int saved = RedrawingDisabled;
7911
7912 RedrawingDisabled = 0;
7913 ins_compl_check_keys(0);
7914 rettv->vval.v_number = compl_interrupted;
7915 RedrawingDisabled = saved;
7916}
7917#endif
7918
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919/*
7920 * "confirm(message, buttons[, default [, type]])" function
7921 */
7922/*ARGSUSED*/
7923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007924f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007925 typval_T *argvars;
7926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927{
7928#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7929 char_u *message;
7930 char_u *buttons = NULL;
7931 char_u buf[NUMBUFLEN];
7932 char_u buf2[NUMBUFLEN];
7933 int def = 1;
7934 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007935 char_u *typestr;
7936 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007938 message = get_tv_string_chk(&argvars[0]);
7939 if (message == NULL)
7940 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007941 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007943 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7944 if (buttons == NULL)
7945 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007946 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007948 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007949 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007951 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7952 if (typestr == NULL)
7953 error = TRUE;
7954 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007956 switch (TOUPPER_ASC(*typestr))
7957 {
7958 case 'E': type = VIM_ERROR; break;
7959 case 'Q': type = VIM_QUESTION; break;
7960 case 'I': type = VIM_INFO; break;
7961 case 'W': type = VIM_WARNING; break;
7962 case 'G': type = VIM_GENERIC; break;
7963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 }
7965 }
7966 }
7967 }
7968
7969 if (buttons == NULL || *buttons == NUL)
7970 buttons = (char_u *)_("&Ok");
7971
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007972 if (error)
7973 rettv->vval.v_number = 0;
7974 else
7975 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 def, NULL);
7977#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007978 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979#endif
7980}
7981
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007982/*
7983 * "copy()" function
7984 */
7985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007986f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007987 typval_T *argvars;
7988 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007989{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007990 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007991}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992
7993/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007994 * "count()" function
7995 */
7996 static void
7997f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007998 typval_T *argvars;
7999 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008000{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008001 long n = 0;
8002 int ic = FALSE;
8003
Bram Moolenaare9a41262005-01-15 22:18:47 +00008004 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008005 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008006 listitem_T *li;
8007 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008008 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008009
Bram Moolenaare9a41262005-01-15 22:18:47 +00008010 if ((l = argvars[0].vval.v_list) != NULL)
8011 {
8012 li = l->lv_first;
8013 if (argvars[2].v_type != VAR_UNKNOWN)
8014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008015 int error = FALSE;
8016
8017 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008018 if (argvars[3].v_type != VAR_UNKNOWN)
8019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008020 idx = get_tv_number_chk(&argvars[3], &error);
8021 if (!error)
8022 {
8023 li = list_find(l, idx);
8024 if (li == NULL)
8025 EMSGN(_(e_listidx), idx);
8026 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008027 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008028 if (error)
8029 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008030 }
8031
8032 for ( ; li != NULL; li = li->li_next)
8033 if (tv_equal(&li->li_tv, &argvars[1], ic))
8034 ++n;
8035 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008036 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008037 else if (argvars[0].v_type == VAR_DICT)
8038 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008039 int todo;
8040 dict_T *d;
8041 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008042
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008043 if ((d = argvars[0].vval.v_dict) != NULL)
8044 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008045 int error = FALSE;
8046
Bram Moolenaare9a41262005-01-15 22:18:47 +00008047 if (argvars[2].v_type != VAR_UNKNOWN)
8048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008049 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008050 if (argvars[3].v_type != VAR_UNKNOWN)
8051 EMSG(_(e_invarg));
8052 }
8053
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008054 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008055 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008056 {
8057 if (!HASHITEM_EMPTY(hi))
8058 {
8059 --todo;
8060 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8061 ++n;
8062 }
8063 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008064 }
8065 }
8066 else
8067 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008068 rettv->vval.v_number = n;
8069}
8070
8071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8073 *
8074 * Checks the existence of a cscope connection.
8075 */
8076/*ARGSUSED*/
8077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008078f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008079 typval_T *argvars;
8080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081{
8082#ifdef FEAT_CSCOPE
8083 int num = 0;
8084 char_u *dbpath = NULL;
8085 char_u *prepend = NULL;
8086 char_u buf[NUMBUFLEN];
8087
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008088 if (argvars[0].v_type != VAR_UNKNOWN
8089 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008091 num = (int)get_tv_number(&argvars[0]);
8092 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008093 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008094 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 }
8096
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008097 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008099 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100#endif
8101}
8102
8103/*
8104 * "cursor(lnum, col)" function
8105 *
8106 * Moves the cursor to the specified line and column
8107 */
8108/*ARGSUSED*/
8109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008110f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008111 typval_T *argvars;
8112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113{
8114 long line, col;
8115
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008116 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008117 col = get_tv_number_chk(&argvars[1], NULL);
8118 if (line < 0 || col < 0)
8119 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 if (line > 0)
8121 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 if (col > 0)
8123 curwin->w_cursor.col = col - 1;
8124#ifdef FEAT_VIRTUALEDIT
8125 curwin->w_cursor.coladd = 0;
8126#endif
8127
8128 /* Make sure the cursor is in a valid position. */
8129 check_cursor();
8130#ifdef FEAT_MBYTE
8131 /* Correct cursor for multi-byte character. */
8132 if (has_mbyte)
8133 mb_adjust_cursor();
8134#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008135
8136 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137}
8138
8139/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008140 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 */
8142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008143f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008144 typval_T *argvars;
8145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008147 int noref = 0;
8148
8149 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008150 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008151 if (noref < 0 || noref > 1)
8152 EMSG(_(e_invarg));
8153 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008154 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155}
8156
8157/*
8158 * "delete()" function
8159 */
8160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008161f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008162 typval_T *argvars;
8163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164{
8165 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008166 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008168 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169}
8170
8171/*
8172 * "did_filetype()" function
8173 */
8174/*ARGSUSED*/
8175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008176f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008177 typval_T *argvars;
8178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179{
8180#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008181 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008183 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184#endif
8185}
8186
8187/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008188 * "diff_filler()" function
8189 */
8190/*ARGSUSED*/
8191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008192f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008193 typval_T *argvars;
8194 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008195{
8196#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008197 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008198#endif
8199}
8200
8201/*
8202 * "diff_hlID()" function
8203 */
8204/*ARGSUSED*/
8205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008206f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008207 typval_T *argvars;
8208 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008209{
8210#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008211 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008212 static linenr_T prev_lnum = 0;
8213 static int changedtick = 0;
8214 static int fnum = 0;
8215 static int change_start = 0;
8216 static int change_end = 0;
8217 static enum hlf_value hlID = 0;
8218 int filler_lines;
8219 int col;
8220
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008221 if (lnum < 0) /* ignore type error in {lnum} arg */
8222 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008223 if (lnum != prev_lnum
8224 || changedtick != curbuf->b_changedtick
8225 || fnum != curbuf->b_fnum)
8226 {
8227 /* New line, buffer, change: need to get the values. */
8228 filler_lines = diff_check(curwin, lnum);
8229 if (filler_lines < 0)
8230 {
8231 if (filler_lines == -1)
8232 {
8233 change_start = MAXCOL;
8234 change_end = -1;
8235 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8236 hlID = HLF_ADD; /* added line */
8237 else
8238 hlID = HLF_CHD; /* changed line */
8239 }
8240 else
8241 hlID = HLF_ADD; /* added line */
8242 }
8243 else
8244 hlID = (enum hlf_value)0;
8245 prev_lnum = lnum;
8246 changedtick = curbuf->b_changedtick;
8247 fnum = curbuf->b_fnum;
8248 }
8249
8250 if (hlID == HLF_CHD || hlID == HLF_TXD)
8251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008252 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008253 if (col >= change_start && col <= change_end)
8254 hlID = HLF_TXD; /* changed text */
8255 else
8256 hlID = HLF_CHD; /* changed line */
8257 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008258 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008259#endif
8260}
8261
8262/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008263 * "empty({expr})" function
8264 */
8265 static void
8266f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008267 typval_T *argvars;
8268 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008269{
8270 int n;
8271
8272 switch (argvars[0].v_type)
8273 {
8274 case VAR_STRING:
8275 case VAR_FUNC:
8276 n = argvars[0].vval.v_string == NULL
8277 || *argvars[0].vval.v_string == NUL;
8278 break;
8279 case VAR_NUMBER:
8280 n = argvars[0].vval.v_number == 0;
8281 break;
8282 case VAR_LIST:
8283 n = argvars[0].vval.v_list == NULL
8284 || argvars[0].vval.v_list->lv_first == NULL;
8285 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008286 case VAR_DICT:
8287 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008288 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008289 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008290 default:
8291 EMSG2(_(e_intern2), "f_empty()");
8292 n = 0;
8293 }
8294
8295 rettv->vval.v_number = n;
8296}
8297
8298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 * "escape({string}, {chars})" function
8300 */
8301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008302f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008303 typval_T *argvars;
8304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305{
8306 char_u buf[NUMBUFLEN];
8307
Bram Moolenaar758711c2005-02-02 23:11:38 +00008308 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8309 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008310 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311}
8312
8313/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008314 * "eval()" function
8315 */
8316/*ARGSUSED*/
8317 static void
8318f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008319 typval_T *argvars;
8320 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008321{
8322 char_u *s;
8323
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008324 s = get_tv_string_chk(&argvars[0]);
8325 if (s != NULL)
8326 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008327
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008328 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8329 {
8330 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008331 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008332 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008333 else if (*s != NUL)
8334 EMSG(_(e_trailing));
8335}
8336
8337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 * "eventhandler()" function
8339 */
8340/*ARGSUSED*/
8341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008342f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008343 typval_T *argvars;
8344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008346 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347}
8348
8349/*
8350 * "executable()" function
8351 */
8352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008353f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008354 typval_T *argvars;
8355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008357 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358}
8359
8360/*
8361 * "exists()" function
8362 */
8363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008364f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008365 typval_T *argvars;
8366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367{
8368 char_u *p;
8369 char_u *name;
8370 int n = FALSE;
8371 int len = 0;
8372
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008373 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 if (*p == '$') /* environment variable */
8375 {
8376 /* first try "normal" environment variables (fast) */
8377 if (mch_getenv(p + 1) != NULL)
8378 n = TRUE;
8379 else
8380 {
8381 /* try expanding things like $VIM and ${HOME} */
8382 p = expand_env_save(p);
8383 if (p != NULL && *p != '$')
8384 n = TRUE;
8385 vim_free(p);
8386 }
8387 }
8388 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008389 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 else if (*p == '*') /* internal or user defined function */
8391 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008392 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 }
8394 else if (*p == ':')
8395 {
8396 n = cmd_exists(p + 1);
8397 }
8398 else if (*p == '#')
8399 {
8400#ifdef FEAT_AUTOCMD
8401 name = p + 1;
8402 p = vim_strchr(name, '#');
8403 if (p != NULL)
8404 n = au_exists(name, p, p + 1);
8405 else
8406 n = au_exists(name, name + STRLEN(name), NULL);
8407#endif
8408 }
8409 else /* internal variable */
8410 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008411 char_u *tofree;
8412 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008414 /* get_name_len() takes care of expanding curly braces */
8415 name = p;
8416 len = get_name_len(&p, &tofree, TRUE, FALSE);
8417 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008419 if (tofree != NULL)
8420 name = tofree;
8421 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8422 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008424 /* handle d.key, l[idx], f(expr) */
8425 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8426 if (n)
8427 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 }
8429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008431 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 }
8433
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008434 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435}
8436
8437/*
8438 * "expand()" function
8439 */
8440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008441f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008442 typval_T *argvars;
8443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444{
8445 char_u *s;
8446 int len;
8447 char_u *errormsg;
8448 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8449 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008450 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008452 rettv->v_type = VAR_STRING;
8453 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 if (*s == '%' || *s == '#' || *s == '<')
8455 {
8456 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008457 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 --emsg_off;
8459 }
8460 else
8461 {
8462 /* When the optional second argument is non-zero, don't remove matches
8463 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008464 if (argvars[1].v_type != VAR_UNKNOWN
8465 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008467 if (!error)
8468 {
8469 ExpandInit(&xpc);
8470 xpc.xp_context = EXPAND_FILES;
8471 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8472 ExpandCleanup(&xpc);
8473 }
8474 else
8475 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 }
8477}
8478
8479/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008480 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008481 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008482 */
8483 static void
8484f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008485 typval_T *argvars;
8486 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008487{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008488 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008489 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008490 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008491 list_T *l1, *l2;
8492 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008493 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008494 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008495
Bram Moolenaare9a41262005-01-15 22:18:47 +00008496 l1 = argvars[0].vval.v_list;
8497 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008498 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8499 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008500 {
8501 if (argvars[2].v_type != VAR_UNKNOWN)
8502 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008503 before = get_tv_number_chk(&argvars[2], &error);
8504 if (error)
8505 return; /* type error; errmsg already given */
8506
Bram Moolenaar758711c2005-02-02 23:11:38 +00008507 if (before == l1->lv_len)
8508 item = NULL;
8509 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008510 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008511 item = list_find(l1, before);
8512 if (item == NULL)
8513 {
8514 EMSGN(_(e_listidx), before);
8515 return;
8516 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008517 }
8518 }
8519 else
8520 item = NULL;
8521 list_extend(l1, l2, item);
8522
Bram Moolenaare9a41262005-01-15 22:18:47 +00008523 copy_tv(&argvars[0], rettv);
8524 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008525 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008526 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8527 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008528 dict_T *d1, *d2;
8529 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008530 char_u *action;
8531 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008532 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008533 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008534
8535 d1 = argvars[0].vval.v_dict;
8536 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008537 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8538 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008539 {
8540 /* Check the third argument. */
8541 if (argvars[2].v_type != VAR_UNKNOWN)
8542 {
8543 static char *(av[]) = {"keep", "force", "error"};
8544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008545 action = get_tv_string_chk(&argvars[2]);
8546 if (action == NULL)
8547 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008548 for (i = 0; i < 3; ++i)
8549 if (STRCMP(action, av[i]) == 0)
8550 break;
8551 if (i == 3)
8552 {
8553 EMSGN(_(e_invarg2), action);
8554 return;
8555 }
8556 }
8557 else
8558 action = (char_u *)"force";
8559
8560 /* Go over all entries in the second dict and add them to the
8561 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008562 todo = d2->dv_hashtab.ht_used;
8563 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008564 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008565 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008566 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008567 --todo;
8568 di1 = dict_find(d1, hi2->hi_key, -1);
8569 if (di1 == NULL)
8570 {
8571 di1 = dictitem_copy(HI2DI(hi2));
8572 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8573 dictitem_free(di1);
8574 }
8575 else if (*action == 'e')
8576 {
8577 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8578 break;
8579 }
8580 else if (*action == 'f')
8581 {
8582 clear_tv(&di1->di_tv);
8583 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8584 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008585 }
8586 }
8587
Bram Moolenaare9a41262005-01-15 22:18:47 +00008588 copy_tv(&argvars[0], rettv);
8589 }
8590 }
8591 else
8592 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008593}
8594
8595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 * "filereadable()" function
8597 */
8598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008599f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008600 typval_T *argvars;
8601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
8603 FILE *fd;
8604 char_u *p;
8605 int n;
8606
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008607 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8609 {
8610 n = TRUE;
8611 fclose(fd);
8612 }
8613 else
8614 n = FALSE;
8615
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008616 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617}
8618
8619/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008620 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 * rights to write into.
8622 */
8623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008624f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008625 typval_T *argvars;
8626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008628 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629}
8630
Bram Moolenaar33570922005-01-25 22:26:29 +00008631static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008632
8633 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008634findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008635 typval_T *argvars;
8636 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008637 int dir;
8638{
8639#ifdef FEAT_SEARCHPATH
8640 char_u *fname;
8641 char_u *fresult = NULL;
8642 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8643 char_u *p;
8644 char_u pathbuf[NUMBUFLEN];
8645 int count = 1;
8646 int first = TRUE;
8647
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008648 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008649
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008650 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008651 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008652 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8653 if (p == NULL)
8654 count = -1; /* error */
8655 else
8656 {
8657 if (*p != NUL)
8658 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008659
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008660 if (argvars[2].v_type != VAR_UNKNOWN)
8661 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8662 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008663 }
8664
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008665 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008667 do
8668 {
8669 vim_free(fresult);
8670 fresult = find_file_in_path_option(first ? fname : NULL,
8671 first ? (int)STRLEN(fname) : 0,
8672 0, first, path, dir, NULL);
8673 first = FALSE;
8674 } while (--count > 0 && fresult != NULL);
8675 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008676
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008677 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008678#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008679 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008680#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008681 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008682}
8683
Bram Moolenaar33570922005-01-25 22:26:29 +00008684static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8685static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008686
8687/*
8688 * Implementation of map() and filter().
8689 */
8690 static void
8691filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008692 typval_T *argvars;
8693 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008694 int map;
8695{
8696 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008697 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008698 listitem_T *li, *nli;
8699 list_T *l = NULL;
8700 dictitem_T *di;
8701 hashtab_T *ht;
8702 hashitem_T *hi;
8703 dict_T *d = NULL;
8704 typval_T save_val;
8705 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008706 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008707 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008708 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8709
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008710
8711 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008712 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008713 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008714 if ((l = argvars[0].vval.v_list) == NULL
8715 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008716 return;
8717 }
8718 else if (argvars[0].v_type == VAR_DICT)
8719 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008720 if ((d = argvars[0].vval.v_dict) == NULL
8721 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008722 return;
8723 }
8724 else
8725 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008726 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008727 return;
8728 }
8729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008730 expr = get_tv_string_buf_chk(&argvars[1], buf);
8731 /* On type errors, the preceding call has already displayed an error
8732 * message. Avoid a misleading error message for an empty string that
8733 * was not passed as argument. */
8734 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008736 prepare_vimvar(VV_VAL, &save_val);
8737 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008739 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008741 prepare_vimvar(VV_KEY, &save_key);
8742 vimvars[VV_KEY].vv_type = VAR_STRING;
8743
8744 ht = &d->dv_hashtab;
8745 hash_lock(ht);
8746 todo = ht->ht_used;
8747 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008749 if (!HASHITEM_EMPTY(hi))
8750 {
8751 --todo;
8752 di = HI2DI(hi);
8753 if (tv_check_lock(di->di_tv.v_lock, msg))
8754 break;
8755 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8756 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8757 break;
8758 if (!map && rem)
8759 dictitem_remove(d, di);
8760 clear_tv(&vimvars[VV_KEY].vv_tv);
8761 }
8762 }
8763 hash_unlock(ht);
8764
8765 restore_vimvar(VV_KEY, &save_key);
8766 }
8767 else
8768 {
8769 for (li = l->lv_first; li != NULL; li = nli)
8770 {
8771 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008772 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008773 nli = li->li_next;
8774 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008775 break;
8776 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008777 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008778 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008779 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008781 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008782 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008783
8784 copy_tv(&argvars[0], rettv);
8785}
8786
8787 static int
8788filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008789 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008790 char_u *expr;
8791 int map;
8792 int *remp;
8793{
Bram Moolenaar33570922005-01-25 22:26:29 +00008794 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008795 char_u *s;
8796
Bram Moolenaar33570922005-01-25 22:26:29 +00008797 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008798 s = expr;
8799 if (eval1(&s, &rettv, TRUE) == FAIL)
8800 return FAIL;
8801 if (*s != NUL) /* check for trailing chars after expr */
8802 {
8803 EMSG2(_(e_invexpr2), s);
8804 return FAIL;
8805 }
8806 if (map)
8807 {
8808 /* map(): replace the list item value */
8809 clear_tv(tv);
8810 *tv = rettv;
8811 }
8812 else
8813 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008814 int error = FALSE;
8815
Bram Moolenaare9a41262005-01-15 22:18:47 +00008816 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008817 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008818 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008819 /* On type error, nothing has been removed; return FAIL to stop the
8820 * loop. The error message was given by get_tv_number_chk(). */
8821 if (error)
8822 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008823 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008824 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008825 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008826}
8827
8828/*
8829 * "filter()" function
8830 */
8831 static void
8832f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008833 typval_T *argvars;
8834 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008835{
8836 filter_map(argvars, rettv, FALSE);
8837}
8838
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008839/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008840 * "finddir({fname}[, {path}[, {count}]])" function
8841 */
8842 static void
8843f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008844 typval_T *argvars;
8845 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008846{
8847 findfilendir(argvars, rettv, TRUE);
8848}
8849
8850/*
8851 * "findfile({fname}[, {path}[, {count}]])" function
8852 */
8853 static void
8854f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008855 typval_T *argvars;
8856 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008857{
8858 findfilendir(argvars, rettv, FALSE);
8859}
8860
8861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 * "fnamemodify({fname}, {mods})" function
8863 */
8864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008866 typval_T *argvars;
8867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868{
8869 char_u *fname;
8870 char_u *mods;
8871 int usedlen = 0;
8872 int len;
8873 char_u *fbuf = NULL;
8874 char_u buf[NUMBUFLEN];
8875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008876 fname = get_tv_string_chk(&argvars[0]);
8877 mods = get_tv_string_buf_chk(&argvars[1], buf);
8878 if (fname == NULL || mods == NULL)
8879 fname = NULL;
8880 else
8881 {
8882 len = (int)STRLEN(fname);
8883 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008886 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008888 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008890 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 vim_free(fbuf);
8892}
8893
Bram Moolenaar33570922005-01-25 22:26:29 +00008894static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895
8896/*
8897 * "foldclosed()" function
8898 */
8899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008900foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008901 typval_T *argvars;
8902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 int end;
8904{
8905#ifdef FEAT_FOLDING
8906 linenr_T lnum;
8907 linenr_T first, last;
8908
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008909 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8911 {
8912 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8913 {
8914 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008917 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 return;
8919 }
8920 }
8921#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008922 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923}
8924
8925/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008926 * "foldclosed()" function
8927 */
8928 static void
8929f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008930 typval_T *argvars;
8931 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008932{
8933 foldclosed_both(argvars, rettv, FALSE);
8934}
8935
8936/*
8937 * "foldclosedend()" function
8938 */
8939 static void
8940f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008941 typval_T *argvars;
8942 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008943{
8944 foldclosed_both(argvars, rettv, TRUE);
8945}
8946
8947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 * "foldlevel()" function
8949 */
8950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008951f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008952 typval_T *argvars;
8953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954{
8955#ifdef FEAT_FOLDING
8956 linenr_T lnum;
8957
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 else
8962#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008963 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964}
8965
8966/*
8967 * "foldtext()" function
8968 */
8969/*ARGSUSED*/
8970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008971f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008972 typval_T *argvars;
8973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974{
8975#ifdef FEAT_FOLDING
8976 linenr_T lnum;
8977 char_u *s;
8978 char_u *r;
8979 int len;
8980 char *txt;
8981#endif
8982
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008983 rettv->v_type = VAR_STRING;
8984 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008986 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8987 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8988 <= curbuf->b_ml.ml_line_count
8989 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 {
8991 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008992 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8993 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 {
8995 if (!linewhite(lnum))
8996 break;
8997 ++lnum;
8998 }
8999
9000 /* Find interesting text in this line. */
9001 s = skipwhite(ml_get(lnum));
9002 /* skip C comment-start */
9003 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009004 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009006 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009007 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009008 {
9009 s = skipwhite(ml_get(lnum + 1));
9010 if (*s == '*')
9011 s = skipwhite(s + 1);
9012 }
9013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 txt = _("+-%s%3ld lines: ");
9015 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009016 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 + 20 /* for %3ld */
9018 + STRLEN(s))); /* concatenated */
9019 if (r != NULL)
9020 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009021 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9022 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9023 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 len = (int)STRLEN(r);
9025 STRCAT(r, s);
9026 /* remove 'foldmarker' and 'commentstring' */
9027 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009028 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 }
9030 }
9031#endif
9032}
9033
9034/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009035 * "foldtextresult(lnum)" function
9036 */
9037/*ARGSUSED*/
9038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009040 typval_T *argvars;
9041 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009042{
9043#ifdef FEAT_FOLDING
9044 linenr_T lnum;
9045 char_u *text;
9046 char_u buf[51];
9047 foldinfo_T foldinfo;
9048 int fold_count;
9049#endif
9050
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009051 rettv->v_type = VAR_STRING;
9052 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009053#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009054 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009055 /* treat illegal types and illegal string values for {lnum} the same */
9056 if (lnum < 0)
9057 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009058 fold_count = foldedCount(curwin, lnum, &foldinfo);
9059 if (fold_count > 0)
9060 {
9061 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9062 &foldinfo, buf);
9063 if (text == buf)
9064 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009065 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009066 }
9067#endif
9068}
9069
9070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 * "foreground()" function
9072 */
9073/*ARGSUSED*/
9074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009075f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009076 typval_T *argvars;
9077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009079 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080#ifdef FEAT_GUI
9081 if (gui.in_use)
9082 gui_mch_set_foreground();
9083#else
9084# ifdef WIN32
9085 win32_set_foreground();
9086# endif
9087#endif
9088}
9089
9090/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009091 * "function()" function
9092 */
9093/*ARGSUSED*/
9094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009095f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009096 typval_T *argvars;
9097 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009098{
9099 char_u *s;
9100
Bram Moolenaara7043832005-01-21 11:56:39 +00009101 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009102 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009103 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009104 EMSG2(_(e_invarg2), s);
9105 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009106 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009107 else
9108 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109 rettv->vval.v_string = vim_strsave(s);
9110 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009111 }
9112}
9113
9114/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009115 * "garbagecollect()" function
9116 */
9117/*ARGSUSED*/
9118 static void
9119f_garbagecollect(argvars, rettv)
9120 typval_T *argvars;
9121 typval_T *rettv;
9122{
9123 garbage_collect();
9124}
9125
9126/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009127 * "get()" function
9128 */
9129 static void
9130f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009131 typval_T *argvars;
9132 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009133{
Bram Moolenaar33570922005-01-25 22:26:29 +00009134 listitem_T *li;
9135 list_T *l;
9136 dictitem_T *di;
9137 dict_T *d;
9138 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009139
Bram Moolenaare9a41262005-01-15 22:18:47 +00009140 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009141 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009142 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009144 int error = FALSE;
9145
9146 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9147 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009148 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009149 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009150 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009151 else if (argvars[0].v_type == VAR_DICT)
9152 {
9153 if ((d = argvars[0].vval.v_dict) != NULL)
9154 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009155 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009156 if (di != NULL)
9157 tv = &di->di_tv;
9158 }
9159 }
9160 else
9161 EMSG2(_(e_listdictarg), "get()");
9162
9163 if (tv == NULL)
9164 {
9165 if (argvars[2].v_type == VAR_UNKNOWN)
9166 rettv->vval.v_number = 0;
9167 else
9168 copy_tv(&argvars[2], rettv);
9169 }
9170 else
9171 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009172}
9173
Bram Moolenaar342337a2005-07-21 21:11:17 +00009174static 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 +00009175
9176/*
9177 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009178 * Return a range (from start to end) of lines in rettv from the specified
9179 * buffer.
9180 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009181 */
9182 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009183get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009184 buf_T *buf;
9185 linenr_T start;
9186 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009187 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009188 typval_T *rettv;
9189{
9190 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009191 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009192 listitem_T *li;
9193
Bram Moolenaar342337a2005-07-21 21:11:17 +00009194 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009195 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009196 l = list_alloc();
9197 if (l == NULL)
9198 return;
9199
9200 rettv->vval.v_list = l;
9201 rettv->v_type = VAR_LIST;
9202 ++l->lv_refcount;
9203 }
9204 else
9205 rettv->vval.v_number = 0;
9206
9207 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9208 return;
9209
9210 if (!retlist)
9211 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009212 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9213 p = ml_get_buf(buf, start, FALSE);
9214 else
9215 p = (char_u *)"";
9216
9217 rettv->v_type = VAR_STRING;
9218 rettv->vval.v_string = vim_strsave(p);
9219 }
9220 else
9221 {
9222 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009223 return;
9224
9225 if (start < 1)
9226 start = 1;
9227 if (end > buf->b_ml.ml_line_count)
9228 end = buf->b_ml.ml_line_count;
9229 while (start <= end)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009230 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009231 li = listitem_alloc();
9232 if (li == NULL)
9233 break;
9234 list_append(l, li);
9235 li->li_tv.v_type = VAR_STRING;
9236 li->li_tv.v_lock = 0;
9237 li->li_tv.vval.v_string =
9238 vim_strsave(ml_get_buf(buf, start++, FALSE));
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009239 }
9240 }
9241}
9242
9243/*
9244 * "getbufline()" function
9245 */
9246 static void
9247f_getbufline(argvars, rettv)
9248 typval_T *argvars;
9249 typval_T *rettv;
9250{
9251 linenr_T lnum;
9252 linenr_T end;
9253 buf_T *buf;
9254
9255 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9256 ++emsg_off;
9257 buf = get_buf_tv(&argvars[0]);
9258 --emsg_off;
9259
Bram Moolenaar661b1822005-07-28 22:36:45 +00009260 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009261 if (argvars[2].v_type == VAR_UNKNOWN)
9262 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009263 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009264 end = get_tv_lnum_buf(&argvars[2], buf);
9265
Bram Moolenaar342337a2005-07-21 21:11:17 +00009266 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009267}
9268
Bram Moolenaar0d660222005-01-07 21:51:51 +00009269/*
9270 * "getbufvar()" function
9271 */
9272 static void
9273f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009274 typval_T *argvars;
9275 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009276{
9277 buf_T *buf;
9278 buf_T *save_curbuf;
9279 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009280 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009282 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9283 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009284 ++emsg_off;
9285 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009286
9287 rettv->v_type = VAR_STRING;
9288 rettv->vval.v_string = NULL;
9289
9290 if (buf != NULL && varname != NULL)
9291 {
9292 if (*varname == '&') /* buffer-local-option */
9293 {
9294 /* set curbuf to be our buf, temporarily */
9295 save_curbuf = curbuf;
9296 curbuf = buf;
9297
9298 get_option_tv(&varname, rettv, TRUE);
9299
9300 /* restore previous notion of curbuf */
9301 curbuf = save_curbuf;
9302 }
9303 else
9304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009305 if (*varname == NUL)
9306 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9307 * scope prefix before the NUL byte is required by
9308 * find_var_in_ht(). */
9309 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009310 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009311 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009312 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009313 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009314 }
9315 }
9316
9317 --emsg_off;
9318}
9319
9320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 * "getchar()" function
9322 */
9323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009324f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009325 typval_T *argvars;
9326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327{
9328 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330
9331 ++no_mapping;
9332 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009333 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 /* getchar(): blocking wait. */
9335 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009336 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 /* getchar(1): only check if char avail */
9338 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009339 else if (error || vpeekc() == NUL)
9340 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 n = 0;
9342 else
9343 /* getchar(0) and char avail: return char */
9344 n = safe_vgetc();
9345 --no_mapping;
9346 --allow_keys;
9347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009348 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 if (IS_SPECIAL(n) || mod_mask != 0)
9350 {
9351 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9352 int i = 0;
9353
9354 /* Turn a special key into three bytes, plus modifier. */
9355 if (mod_mask != 0)
9356 {
9357 temp[i++] = K_SPECIAL;
9358 temp[i++] = KS_MODIFIER;
9359 temp[i++] = mod_mask;
9360 }
9361 if (IS_SPECIAL(n))
9362 {
9363 temp[i++] = K_SPECIAL;
9364 temp[i++] = K_SECOND(n);
9365 temp[i++] = K_THIRD(n);
9366 }
9367#ifdef FEAT_MBYTE
9368 else if (has_mbyte)
9369 i += (*mb_char2bytes)(n, temp + i);
9370#endif
9371 else
9372 temp[i++] = n;
9373 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009374 rettv->v_type = VAR_STRING;
9375 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 }
9377}
9378
9379/*
9380 * "getcharmod()" function
9381 */
9382/*ARGSUSED*/
9383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009384f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009385 typval_T *argvars;
9386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009388 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389}
9390
9391/*
9392 * "getcmdline()" function
9393 */
9394/*ARGSUSED*/
9395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009396f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009397 typval_T *argvars;
9398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400 rettv->v_type = VAR_STRING;
9401 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402}
9403
9404/*
9405 * "getcmdpos()" function
9406 */
9407/*ARGSUSED*/
9408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009409f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009410 typval_T *argvars;
9411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414}
9415
9416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 * "getcwd()" function
9418 */
9419/*ARGSUSED*/
9420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009421f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009422 typval_T *argvars;
9423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424{
9425 char_u cwd[MAXPATHL];
9426
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 else
9431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009434 if (rettv->vval.v_string != NULL)
9435 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436#endif
9437 }
9438}
9439
9440/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009441 * "getfontname()" function
9442 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009443/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009446 typval_T *argvars;
9447 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009448{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449 rettv->v_type = VAR_STRING;
9450 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009451#ifdef FEAT_GUI
9452 if (gui.in_use)
9453 {
9454 GuiFont font;
9455 char_u *name = NULL;
9456
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009457 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009458 {
9459 /* Get the "Normal" font. Either the name saved by
9460 * hl_set_font_name() or from the font ID. */
9461 font = gui.norm_font;
9462 name = hl_get_font_name();
9463 }
9464 else
9465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009467 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9468 return;
9469 font = gui_mch_get_font(name, FALSE);
9470 if (font == NOFONT)
9471 return; /* Invalid font name, return empty string. */
9472 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009473 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009474 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009475 gui_mch_free_font(font);
9476 }
9477#endif
9478}
9479
9480/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009481 * "getfperm({fname})" function
9482 */
9483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009485 typval_T *argvars;
9486 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009487{
9488 char_u *fname;
9489 struct stat st;
9490 char_u *perm = NULL;
9491 char_u flags[] = "rwx";
9492 int i;
9493
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009494 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009495
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009497 if (mch_stat((char *)fname, &st) >= 0)
9498 {
9499 perm = vim_strsave((char_u *)"---------");
9500 if (perm != NULL)
9501 {
9502 for (i = 0; i < 9; i++)
9503 {
9504 if (st.st_mode & (1 << (8 - i)))
9505 perm[i] = flags[i % 3];
9506 }
9507 }
9508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009509 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009510}
9511
9512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 * "getfsize({fname})" function
9514 */
9515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009517 typval_T *argvars;
9518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519{
9520 char_u *fname;
9521 struct stat st;
9522
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009523 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009525 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526
9527 if (mch_stat((char *)fname, &st) >= 0)
9528 {
9529 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009530 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009532 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 }
9534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536}
9537
9538/*
9539 * "getftime({fname})" function
9540 */
9541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009542f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009543 typval_T *argvars;
9544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
9546 char_u *fname;
9547 struct stat st;
9548
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009549 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550
9551 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555}
9556
9557/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009558 * "getftype({fname})" function
9559 */
9560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009561f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009562 typval_T *argvars;
9563 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009564{
9565 char_u *fname;
9566 struct stat st;
9567 char_u *type = NULL;
9568 char *t;
9569
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009570 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009571
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009573 if (mch_lstat((char *)fname, &st) >= 0)
9574 {
9575#ifdef S_ISREG
9576 if (S_ISREG(st.st_mode))
9577 t = "file";
9578 else if (S_ISDIR(st.st_mode))
9579 t = "dir";
9580# ifdef S_ISLNK
9581 else if (S_ISLNK(st.st_mode))
9582 t = "link";
9583# endif
9584# ifdef S_ISBLK
9585 else if (S_ISBLK(st.st_mode))
9586 t = "bdev";
9587# endif
9588# ifdef S_ISCHR
9589 else if (S_ISCHR(st.st_mode))
9590 t = "cdev";
9591# endif
9592# ifdef S_ISFIFO
9593 else if (S_ISFIFO(st.st_mode))
9594 t = "fifo";
9595# endif
9596# ifdef S_ISSOCK
9597 else if (S_ISSOCK(st.st_mode))
9598 t = "fifo";
9599# endif
9600 else
9601 t = "other";
9602#else
9603# ifdef S_IFMT
9604 switch (st.st_mode & S_IFMT)
9605 {
9606 case S_IFREG: t = "file"; break;
9607 case S_IFDIR: t = "dir"; break;
9608# ifdef S_IFLNK
9609 case S_IFLNK: t = "link"; break;
9610# endif
9611# ifdef S_IFBLK
9612 case S_IFBLK: t = "bdev"; break;
9613# endif
9614# ifdef S_IFCHR
9615 case S_IFCHR: t = "cdev"; break;
9616# endif
9617# ifdef S_IFIFO
9618 case S_IFIFO: t = "fifo"; break;
9619# endif
9620# ifdef S_IFSOCK
9621 case S_IFSOCK: t = "socket"; break;
9622# endif
9623 default: t = "other";
9624 }
9625# else
9626 if (mch_isdir(fname))
9627 t = "dir";
9628 else
9629 t = "file";
9630# endif
9631#endif
9632 type = vim_strsave((char_u *)t);
9633 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009635}
9636
9637/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009638 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009639 */
9640 static void
9641f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009642 typval_T *argvars;
9643 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009644{
9645 linenr_T lnum;
9646 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009647 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009648
9649 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009650 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009651 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009652 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009653 retlist = FALSE;
9654 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009655 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009656 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009657 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009658 retlist = TRUE;
9659 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009660
Bram Moolenaar342337a2005-07-21 21:11:17 +00009661 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009662}
9663
9664/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009665 * "getqflist()" function
9666 */
9667/*ARGSUSED*/
9668 static void
9669f_getqflist(argvars, rettv)
9670 typval_T *argvars;
9671 typval_T *rettv;
9672{
9673#ifdef FEAT_QUICKFIX
9674 list_T *l;
9675#endif
9676
9677 rettv->vval.v_number = FALSE;
9678#ifdef FEAT_QUICKFIX
9679 l = list_alloc();
9680 if (l != NULL)
9681 {
9682 if (get_errorlist(l) != FAIL)
9683 {
9684 rettv->vval.v_list = l;
9685 rettv->v_type = VAR_LIST;
9686 ++l->lv_refcount;
9687 }
9688 else
9689 list_free(l);
9690 }
9691#endif
9692}
9693
9694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 * "getreg()" function
9696 */
9697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009698f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009699 typval_T *argvars;
9700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701{
9702 char_u *strregname;
9703 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009704 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009705 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009707 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009709 strregname = get_tv_string_chk(&argvars[0]);
9710 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009711 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009712 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009715 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 regname = (strregname == NULL ? '"' : *strregname);
9717 if (regname == 0)
9718 regname = '"';
9719
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009721 rettv->vval.v_string = error ? NULL :
9722 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723}
9724
9725/*
9726 * "getregtype()" function
9727 */
9728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009729f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009730 typval_T *argvars;
9731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732{
9733 char_u *strregname;
9734 int regname;
9735 char_u buf[NUMBUFLEN + 2];
9736 long reglen = 0;
9737
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009738 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009739 {
9740 strregname = get_tv_string_chk(&argvars[0]);
9741 if (strregname == NULL) /* type error; errmsg already given */
9742 {
9743 rettv->v_type = VAR_STRING;
9744 rettv->vval.v_string = NULL;
9745 return;
9746 }
9747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 else
9749 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009750 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751
9752 regname = (strregname == NULL ? '"' : *strregname);
9753 if (regname == 0)
9754 regname = '"';
9755
9756 buf[0] = NUL;
9757 buf[1] = NUL;
9758 switch (get_reg_type(regname, &reglen))
9759 {
9760 case MLINE: buf[0] = 'V'; break;
9761 case MCHAR: buf[0] = 'v'; break;
9762#ifdef FEAT_VISUAL
9763 case MBLOCK:
9764 buf[0] = Ctrl_V;
9765 sprintf((char *)buf + 1, "%ld", reglen + 1);
9766 break;
9767#endif
9768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009769 rettv->v_type = VAR_STRING;
9770 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771}
9772
9773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 * "getwinposx()" function
9775 */
9776/*ARGSUSED*/
9777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009778f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009779 typval_T *argvars;
9780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783#ifdef FEAT_GUI
9784 if (gui.in_use)
9785 {
9786 int x, y;
9787
9788 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009789 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 }
9791#endif
9792}
9793
9794/*
9795 * "getwinposy()" function
9796 */
9797/*ARGSUSED*/
9798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009799f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009800 typval_T *argvars;
9801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804#ifdef FEAT_GUI
9805 if (gui.in_use)
9806 {
9807 int x, y;
9808
9809 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009810 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 }
9812#endif
9813}
9814
Bram Moolenaara40058a2005-07-11 22:42:07 +00009815static win_T *find_win_by_nr __ARGS((typval_T *vp));
9816
9817 static win_T *
9818find_win_by_nr(vp)
9819 typval_T *vp;
9820{
9821#ifdef FEAT_WINDOWS
9822 win_T *wp;
9823#endif
9824 int nr;
9825
9826 nr = get_tv_number_chk(vp, NULL);
9827
9828#ifdef FEAT_WINDOWS
9829 if (nr < 0)
9830 return NULL;
9831 if (nr == 0)
9832 return curwin;
9833
9834 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9835 if (--nr <= 0)
9836 break;
9837 return wp;
9838#else
9839 if (nr == 0 || nr == 1)
9840 return curwin;
9841 return NULL;
9842#endif
9843}
9844
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845/*
9846 * "getwinvar()" function
9847 */
9848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009849f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009850 typval_T *argvars;
9851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852{
9853 win_T *win, *oldcurwin;
9854 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009855 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009858 varname = get_tv_string_chk(&argvars[1]);
9859 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009861 rettv->v_type = VAR_STRING;
9862 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863
9864 if (win != NULL && varname != NULL)
9865 {
9866 if (*varname == '&') /* window-local-option */
9867 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009868 /* Set curwin to be our win, temporarily. Also set curbuf, so
9869 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 oldcurwin = curwin;
9871 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009872 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009874 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875
9876 /* restore previous notion of curwin */
9877 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009878 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 }
9880 else
9881 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009882 if (*varname == NUL)
9883 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9884 * scope prefix before the NUL byte is required by
9885 * find_var_in_ht(). */
9886 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009888 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009890 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 }
9892 }
9893
9894 --emsg_off;
9895}
9896
9897/*
9898 * "glob()" function
9899 */
9900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009902 typval_T *argvars;
9903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904{
9905 expand_T xpc;
9906
9907 ExpandInit(&xpc);
9908 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009909 rettv->v_type = VAR_STRING;
9910 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9912 ExpandCleanup(&xpc);
9913}
9914
9915/*
9916 * "globpath()" function
9917 */
9918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009919f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009920 typval_T *argvars;
9921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922{
9923 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009924 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009926 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009927 if (file == NULL)
9928 rettv->vval.v_string = NULL;
9929 else
9930 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931}
9932
9933/*
9934 * "has()" function
9935 */
9936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009938 typval_T *argvars;
9939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940{
9941 int i;
9942 char_u *name;
9943 int n = FALSE;
9944 static char *(has_list[]) =
9945 {
9946#ifdef AMIGA
9947 "amiga",
9948# ifdef FEAT_ARP
9949 "arp",
9950# endif
9951#endif
9952#ifdef __BEOS__
9953 "beos",
9954#endif
9955#ifdef MSDOS
9956# ifdef DJGPP
9957 "dos32",
9958# else
9959 "dos16",
9960# endif
9961#endif
9962#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9963 "mac",
9964#endif
9965#if defined(MACOS_X_UNIX)
9966 "macunix",
9967#endif
9968#ifdef OS2
9969 "os2",
9970#endif
9971#ifdef __QNX__
9972 "qnx",
9973#endif
9974#ifdef RISCOS
9975 "riscos",
9976#endif
9977#ifdef UNIX
9978 "unix",
9979#endif
9980#ifdef VMS
9981 "vms",
9982#endif
9983#ifdef WIN16
9984 "win16",
9985#endif
9986#ifdef WIN32
9987 "win32",
9988#endif
9989#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9990 "win32unix",
9991#endif
9992#ifdef WIN64
9993 "win64",
9994#endif
9995#ifdef EBCDIC
9996 "ebcdic",
9997#endif
9998#ifndef CASE_INSENSITIVE_FILENAME
9999 "fname_case",
10000#endif
10001#ifdef FEAT_ARABIC
10002 "arabic",
10003#endif
10004#ifdef FEAT_AUTOCMD
10005 "autocmd",
10006#endif
10007#ifdef FEAT_BEVAL
10008 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010009# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10010 "balloon_multiline",
10011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012#endif
10013#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10014 "builtin_terms",
10015# ifdef ALL_BUILTIN_TCAPS
10016 "all_builtin_terms",
10017# endif
10018#endif
10019#ifdef FEAT_BYTEOFF
10020 "byte_offset",
10021#endif
10022#ifdef FEAT_CINDENT
10023 "cindent",
10024#endif
10025#ifdef FEAT_CLIENTSERVER
10026 "clientserver",
10027#endif
10028#ifdef FEAT_CLIPBOARD
10029 "clipboard",
10030#endif
10031#ifdef FEAT_CMDL_COMPL
10032 "cmdline_compl",
10033#endif
10034#ifdef FEAT_CMDHIST
10035 "cmdline_hist",
10036#endif
10037#ifdef FEAT_COMMENTS
10038 "comments",
10039#endif
10040#ifdef FEAT_CRYPT
10041 "cryptv",
10042#endif
10043#ifdef FEAT_CSCOPE
10044 "cscope",
10045#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010046#ifdef CURSOR_SHAPE
10047 "cursorshape",
10048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049#ifdef DEBUG
10050 "debug",
10051#endif
10052#ifdef FEAT_CON_DIALOG
10053 "dialog_con",
10054#endif
10055#ifdef FEAT_GUI_DIALOG
10056 "dialog_gui",
10057#endif
10058#ifdef FEAT_DIFF
10059 "diff",
10060#endif
10061#ifdef FEAT_DIGRAPHS
10062 "digraphs",
10063#endif
10064#ifdef FEAT_DND
10065 "dnd",
10066#endif
10067#ifdef FEAT_EMACS_TAGS
10068 "emacs_tags",
10069#endif
10070 "eval", /* always present, of course! */
10071#ifdef FEAT_EX_EXTRA
10072 "ex_extra",
10073#endif
10074#ifdef FEAT_SEARCH_EXTRA
10075 "extra_search",
10076#endif
10077#ifdef FEAT_FKMAP
10078 "farsi",
10079#endif
10080#ifdef FEAT_SEARCHPATH
10081 "file_in_path",
10082#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010083#if defined(UNIX) && !defined(USE_SYSTEM)
10084 "filterpipe",
10085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086#ifdef FEAT_FIND_ID
10087 "find_in_path",
10088#endif
10089#ifdef FEAT_FOLDING
10090 "folding",
10091#endif
10092#ifdef FEAT_FOOTER
10093 "footer",
10094#endif
10095#if !defined(USE_SYSTEM) && defined(UNIX)
10096 "fork",
10097#endif
10098#ifdef FEAT_GETTEXT
10099 "gettext",
10100#endif
10101#ifdef FEAT_GUI
10102 "gui",
10103#endif
10104#ifdef FEAT_GUI_ATHENA
10105# ifdef FEAT_GUI_NEXTAW
10106 "gui_neXtaw",
10107# else
10108 "gui_athena",
10109# endif
10110#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +000010111#ifdef FEAT_GUI_KDE
10112 "gui_kde",
10113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114#ifdef FEAT_GUI_GTK
10115 "gui_gtk",
10116# ifdef HAVE_GTK2
10117 "gui_gtk2",
10118# endif
10119#endif
10120#ifdef FEAT_GUI_MAC
10121 "gui_mac",
10122#endif
10123#ifdef FEAT_GUI_MOTIF
10124 "gui_motif",
10125#endif
10126#ifdef FEAT_GUI_PHOTON
10127 "gui_photon",
10128#endif
10129#ifdef FEAT_GUI_W16
10130 "gui_win16",
10131#endif
10132#ifdef FEAT_GUI_W32
10133 "gui_win32",
10134#endif
10135#ifdef FEAT_HANGULIN
10136 "hangul_input",
10137#endif
10138#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10139 "iconv",
10140#endif
10141#ifdef FEAT_INS_EXPAND
10142 "insert_expand",
10143#endif
10144#ifdef FEAT_JUMPLIST
10145 "jumplist",
10146#endif
10147#ifdef FEAT_KEYMAP
10148 "keymap",
10149#endif
10150#ifdef FEAT_LANGMAP
10151 "langmap",
10152#endif
10153#ifdef FEAT_LIBCALL
10154 "libcall",
10155#endif
10156#ifdef FEAT_LINEBREAK
10157 "linebreak",
10158#endif
10159#ifdef FEAT_LISP
10160 "lispindent",
10161#endif
10162#ifdef FEAT_LISTCMDS
10163 "listcmds",
10164#endif
10165#ifdef FEAT_LOCALMAP
10166 "localmap",
10167#endif
10168#ifdef FEAT_MENU
10169 "menu",
10170#endif
10171#ifdef FEAT_SESSION
10172 "mksession",
10173#endif
10174#ifdef FEAT_MODIFY_FNAME
10175 "modify_fname",
10176#endif
10177#ifdef FEAT_MOUSE
10178 "mouse",
10179#endif
10180#ifdef FEAT_MOUSESHAPE
10181 "mouseshape",
10182#endif
10183#if defined(UNIX) || defined(VMS)
10184# ifdef FEAT_MOUSE_DEC
10185 "mouse_dec",
10186# endif
10187# ifdef FEAT_MOUSE_GPM
10188 "mouse_gpm",
10189# endif
10190# ifdef FEAT_MOUSE_JSB
10191 "mouse_jsbterm",
10192# endif
10193# ifdef FEAT_MOUSE_NET
10194 "mouse_netterm",
10195# endif
10196# ifdef FEAT_MOUSE_PTERM
10197 "mouse_pterm",
10198# endif
10199# ifdef FEAT_MOUSE_XTERM
10200 "mouse_xterm",
10201# endif
10202#endif
10203#ifdef FEAT_MBYTE
10204 "multi_byte",
10205#endif
10206#ifdef FEAT_MBYTE_IME
10207 "multi_byte_ime",
10208#endif
10209#ifdef FEAT_MULTI_LANG
10210 "multi_lang",
10211#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010212#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010213#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010214 "mzscheme",
10215#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217#ifdef FEAT_OLE
10218 "ole",
10219#endif
10220#ifdef FEAT_OSFILETYPE
10221 "osfiletype",
10222#endif
10223#ifdef FEAT_PATH_EXTRA
10224 "path_extra",
10225#endif
10226#ifdef FEAT_PERL
10227#ifndef DYNAMIC_PERL
10228 "perl",
10229#endif
10230#endif
10231#ifdef FEAT_PYTHON
10232#ifndef DYNAMIC_PYTHON
10233 "python",
10234#endif
10235#endif
10236#ifdef FEAT_POSTSCRIPT
10237 "postscript",
10238#endif
10239#ifdef FEAT_PRINTER
10240 "printer",
10241#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010242#ifdef FEAT_PROFILE
10243 "profile",
10244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245#ifdef FEAT_QUICKFIX
10246 "quickfix",
10247#endif
10248#ifdef FEAT_RIGHTLEFT
10249 "rightleft",
10250#endif
10251#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10252 "ruby",
10253#endif
10254#ifdef FEAT_SCROLLBIND
10255 "scrollbind",
10256#endif
10257#ifdef FEAT_CMDL_INFO
10258 "showcmd",
10259 "cmdline_info",
10260#endif
10261#ifdef FEAT_SIGNS
10262 "signs",
10263#endif
10264#ifdef FEAT_SMARTINDENT
10265 "smartindent",
10266#endif
10267#ifdef FEAT_SNIFF
10268 "sniff",
10269#endif
10270#ifdef FEAT_STL_OPT
10271 "statusline",
10272#endif
10273#ifdef FEAT_SUN_WORKSHOP
10274 "sun_workshop",
10275#endif
10276#ifdef FEAT_NETBEANS_INTG
10277 "netbeans_intg",
10278#endif
10279#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010280 "spell",
10281#endif
10282#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 "syntax",
10284#endif
10285#if defined(USE_SYSTEM) || !defined(UNIX)
10286 "system",
10287#endif
10288#ifdef FEAT_TAG_BINS
10289 "tag_binary",
10290#endif
10291#ifdef FEAT_TAG_OLDSTATIC
10292 "tag_old_static",
10293#endif
10294#ifdef FEAT_TAG_ANYWHITE
10295 "tag_any_white",
10296#endif
10297#ifdef FEAT_TCL
10298# ifndef DYNAMIC_TCL
10299 "tcl",
10300# endif
10301#endif
10302#ifdef TERMINFO
10303 "terminfo",
10304#endif
10305#ifdef FEAT_TERMRESPONSE
10306 "termresponse",
10307#endif
10308#ifdef FEAT_TEXTOBJ
10309 "textobjects",
10310#endif
10311#ifdef HAVE_TGETENT
10312 "tgetent",
10313#endif
10314#ifdef FEAT_TITLE
10315 "title",
10316#endif
10317#ifdef FEAT_TOOLBAR
10318 "toolbar",
10319#endif
10320#ifdef FEAT_USR_CMDS
10321 "user-commands", /* was accidentally included in 5.4 */
10322 "user_commands",
10323#endif
10324#ifdef FEAT_VIMINFO
10325 "viminfo",
10326#endif
10327#ifdef FEAT_VERTSPLIT
10328 "vertsplit",
10329#endif
10330#ifdef FEAT_VIRTUALEDIT
10331 "virtualedit",
10332#endif
10333#ifdef FEAT_VISUAL
10334 "visual",
10335#endif
10336#ifdef FEAT_VISUALEXTRA
10337 "visualextra",
10338#endif
10339#ifdef FEAT_VREPLACE
10340 "vreplace",
10341#endif
10342#ifdef FEAT_WILDIGN
10343 "wildignore",
10344#endif
10345#ifdef FEAT_WILDMENU
10346 "wildmenu",
10347#endif
10348#ifdef FEAT_WINDOWS
10349 "windows",
10350#endif
10351#ifdef FEAT_WAK
10352 "winaltkeys",
10353#endif
10354#ifdef FEAT_WRITEBACKUP
10355 "writebackup",
10356#endif
10357#ifdef FEAT_XIM
10358 "xim",
10359#endif
10360#ifdef FEAT_XFONTSET
10361 "xfontset",
10362#endif
10363#ifdef USE_XSMP
10364 "xsmp",
10365#endif
10366#ifdef USE_XSMP_INTERACT
10367 "xsmp_interact",
10368#endif
10369#ifdef FEAT_XCLIPBOARD
10370 "xterm_clipboard",
10371#endif
10372#ifdef FEAT_XTERM_SAVE
10373 "xterm_save",
10374#endif
10375#if defined(UNIX) && defined(FEAT_X11)
10376 "X11",
10377#endif
10378 NULL
10379 };
10380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010381 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 for (i = 0; has_list[i] != NULL; ++i)
10383 if (STRICMP(name, has_list[i]) == 0)
10384 {
10385 n = TRUE;
10386 break;
10387 }
10388
10389 if (n == FALSE)
10390 {
10391 if (STRNICMP(name, "patch", 5) == 0)
10392 n = has_patch(atoi((char *)name + 5));
10393 else if (STRICMP(name, "vim_starting") == 0)
10394 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010395#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10396 else if (STRICMP(name, "balloon_multiline") == 0)
10397 n = multiline_balloon_available();
10398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399#ifdef DYNAMIC_TCL
10400 else if (STRICMP(name, "tcl") == 0)
10401 n = tcl_enabled(FALSE);
10402#endif
10403#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10404 else if (STRICMP(name, "iconv") == 0)
10405 n = iconv_enabled(FALSE);
10406#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010407#ifdef DYNAMIC_MZSCHEME
10408 else if (STRICMP(name, "mzscheme") == 0)
10409 n = mzscheme_enabled(FALSE);
10410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411#ifdef DYNAMIC_RUBY
10412 else if (STRICMP(name, "ruby") == 0)
10413 n = ruby_enabled(FALSE);
10414#endif
10415#ifdef DYNAMIC_PYTHON
10416 else if (STRICMP(name, "python") == 0)
10417 n = python_enabled(FALSE);
10418#endif
10419#ifdef DYNAMIC_PERL
10420 else if (STRICMP(name, "perl") == 0)
10421 n = perl_enabled(FALSE);
10422#endif
10423#ifdef FEAT_GUI
10424 else if (STRICMP(name, "gui_running") == 0)
10425 n = (gui.in_use || gui.starting);
10426# ifdef FEAT_GUI_W32
10427 else if (STRICMP(name, "gui_win32s") == 0)
10428 n = gui_is_win32s();
10429# endif
10430# ifdef FEAT_BROWSE
10431 else if (STRICMP(name, "browse") == 0)
10432 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10433# endif
10434#endif
10435#ifdef FEAT_SYN_HL
10436 else if (STRICMP(name, "syntax_items") == 0)
10437 n = syntax_present(curbuf);
10438#endif
10439#if defined(WIN3264)
10440 else if (STRICMP(name, "win95") == 0)
10441 n = mch_windows95();
10442#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010443#ifdef FEAT_NETBEANS_INTG
10444 else if (STRICMP(name, "netbeans_enabled") == 0)
10445 n = usingNetbeans;
10446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 }
10448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010449 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450}
10451
10452/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010453 * "has_key()" function
10454 */
10455 static void
10456f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010457 typval_T *argvars;
10458 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010459{
10460 rettv->vval.v_number = 0;
10461 if (argvars[0].v_type != VAR_DICT)
10462 {
10463 EMSG(_(e_dictreq));
10464 return;
10465 }
10466 if (argvars[0].vval.v_dict == NULL)
10467 return;
10468
10469 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010470 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010471}
10472
10473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 * "hasmapto()" function
10475 */
10476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010477f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010478 typval_T *argvars;
10479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480{
10481 char_u *name;
10482 char_u *mode;
10483 char_u buf[NUMBUFLEN];
10484
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010485 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010486 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 mode = (char_u *)"nvo";
10488 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010489 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490
10491 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010492 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010494 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495}
10496
10497/*
10498 * "histadd()" function
10499 */
10500/*ARGSUSED*/
10501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010502f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010503 typval_T *argvars;
10504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505{
10506#ifdef FEAT_CMDHIST
10507 int histype;
10508 char_u *str;
10509 char_u buf[NUMBUFLEN];
10510#endif
10511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010512 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 if (check_restricted() || check_secure())
10514 return;
10515#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10517 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 if (histype >= 0)
10519 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010520 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 if (*str != NUL)
10522 {
10523 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010524 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 return;
10526 }
10527 }
10528#endif
10529}
10530
10531/*
10532 * "histdel()" function
10533 */
10534/*ARGSUSED*/
10535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010536f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010537 typval_T *argvars;
10538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539{
10540#ifdef FEAT_CMDHIST
10541 int n;
10542 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010543 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010545 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10546 if (str == NULL)
10547 n = 0;
10548 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010550 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010551 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010553 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010554 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 else
10556 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010557 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010558 get_tv_string_buf(&argvars[1], buf));
10559 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010561 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562#endif
10563}
10564
10565/*
10566 * "histget()" function
10567 */
10568/*ARGSUSED*/
10569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010570f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010571 typval_T *argvars;
10572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573{
10574#ifdef FEAT_CMDHIST
10575 int type;
10576 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010577 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010579 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10580 if (str == NULL)
10581 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010583 {
10584 type = get_histtype(str);
10585 if (argvars[1].v_type == VAR_UNKNOWN)
10586 idx = get_history_idx(type);
10587 else
10588 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10589 /* -1 on type error */
10590 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010593 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010595 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596}
10597
10598/*
10599 * "histnr()" function
10600 */
10601/*ARGSUSED*/
10602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010603f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010604 typval_T *argvars;
10605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606{
10607 int i;
10608
10609#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010610 char_u *history = get_tv_string_chk(&argvars[0]);
10611
10612 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 if (i >= HIST_CMD && i < HIST_COUNT)
10614 i = get_history_idx(i);
10615 else
10616#endif
10617 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010618 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619}
10620
10621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 * "highlightID(name)" function
10623 */
10624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010625f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010626 typval_T *argvars;
10627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010629 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630}
10631
10632/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010633 * "highlight_exists()" function
10634 */
10635 static void
10636f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010637 typval_T *argvars;
10638 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010639{
10640 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10641}
10642
10643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 * "hostname()" function
10645 */
10646/*ARGSUSED*/
10647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010648f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010649 typval_T *argvars;
10650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651{
10652 char_u hostname[256];
10653
10654 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010655 rettv->v_type = VAR_STRING;
10656 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657}
10658
10659/*
10660 * iconv() function
10661 */
10662/*ARGSUSED*/
10663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010664f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010665 typval_T *argvars;
10666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667{
10668#ifdef FEAT_MBYTE
10669 char_u buf1[NUMBUFLEN];
10670 char_u buf2[NUMBUFLEN];
10671 char_u *from, *to, *str;
10672 vimconv_T vimconv;
10673#endif
10674
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010675 rettv->v_type = VAR_STRING;
10676 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677
10678#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010679 str = get_tv_string(&argvars[0]);
10680 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10681 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 vimconv.vc_type = CONV_NONE;
10683 convert_setup(&vimconv, from, to);
10684
10685 /* If the encodings are equal, no conversion needed. */
10686 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010687 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010689 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690
10691 convert_setup(&vimconv, NULL, NULL);
10692 vim_free(from);
10693 vim_free(to);
10694#endif
10695}
10696
10697/*
10698 * "indent()" function
10699 */
10700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010701f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010702 typval_T *argvars;
10703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704{
10705 linenr_T lnum;
10706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010707 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010709 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010711 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712}
10713
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010714/*
10715 * "index()" function
10716 */
10717 static void
10718f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010719 typval_T *argvars;
10720 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010721{
Bram Moolenaar33570922005-01-25 22:26:29 +000010722 list_T *l;
10723 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010724 long idx = 0;
10725 int ic = FALSE;
10726
10727 rettv->vval.v_number = -1;
10728 if (argvars[0].v_type != VAR_LIST)
10729 {
10730 EMSG(_(e_listreq));
10731 return;
10732 }
10733 l = argvars[0].vval.v_list;
10734 if (l != NULL)
10735 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010736 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010737 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010738 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010739 int error = FALSE;
10740
Bram Moolenaar758711c2005-02-02 23:11:38 +000010741 /* Start at specified item. Use the cached index that list_find()
10742 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010743 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010744 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010745 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010746 ic = get_tv_number_chk(&argvars[3], &error);
10747 if (error)
10748 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010749 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010750
Bram Moolenaar758711c2005-02-02 23:11:38 +000010751 for ( ; item != NULL; item = item->li_next, ++idx)
10752 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010753 {
10754 rettv->vval.v_number = idx;
10755 break;
10756 }
10757 }
10758}
10759
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760static int inputsecret_flag = 0;
10761
10762/*
10763 * "input()" function
10764 * Also handles inputsecret() when inputsecret is set.
10765 */
10766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010767f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010768 typval_T *argvars;
10769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010771 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 char_u *p = NULL;
10773 int c;
10774 char_u buf[NUMBUFLEN];
10775 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010776 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010778 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779
10780#ifdef NO_CONSOLE_INPUT
10781 /* While starting up, there is no place to enter text. */
10782 if (no_console_input())
10783 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010784 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 return;
10786 }
10787#endif
10788
10789 cmd_silent = FALSE; /* Want to see the prompt. */
10790 if (prompt != NULL)
10791 {
10792 /* Only the part of the message after the last NL is considered as
10793 * prompt for the command line */
10794 p = vim_strrchr(prompt, '\n');
10795 if (p == NULL)
10796 p = prompt;
10797 else
10798 {
10799 ++p;
10800 c = *p;
10801 *p = NUL;
10802 msg_start();
10803 msg_clr_eos();
10804 msg_puts_attr(prompt, echo_attr);
10805 msg_didout = FALSE;
10806 msg_starthere();
10807 *p = c;
10808 }
10809 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010811 if (argvars[1].v_type != VAR_UNKNOWN)
10812 {
10813 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10814 if (defstr != NULL)
10815 stuffReadbuffSpec(defstr);
10816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010818 if (defstr != NULL)
10819 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010822 /* since the user typed this, no need to wait for return */
10823 need_wait_return = FALSE;
10824 msg_didout = FALSE;
10825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 cmd_silent = cmd_silent_save;
10827}
10828
10829/*
10830 * "inputdialog()" function
10831 */
10832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010833f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010834 typval_T *argvars;
10835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836{
10837#if defined(FEAT_GUI_TEXTDIALOG)
10838 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10839 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10840 {
10841 char_u *message;
10842 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010843 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010845 message = get_tv_string_chk(&argvars[0]);
10846 if (argvars[1].v_type != VAR_UNKNOWN
10847 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010848 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 else
10850 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010851 if (message != NULL && defstr != NULL
10852 && do_dialog(VIM_QUESTION, NULL, message,
10853 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010854 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 else
10856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010857 if (message != NULL && defstr != NULL
10858 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010859 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010860 rettv->vval.v_string = vim_strsave(
10861 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010863 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010865 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 }
10867 else
10868#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870}
10871
10872static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10873
10874/*
10875 * "inputrestore()" function
10876 */
10877/*ARGSUSED*/
10878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010879f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010880 typval_T *argvars;
10881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882{
10883 if (ga_userinput.ga_len > 0)
10884 {
10885 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10887 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010888 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 }
10890 else if (p_verbose > 1)
10891 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010892 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010893 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 }
10895}
10896
10897/*
10898 * "inputsave()" function
10899 */
10900/*ARGSUSED*/
10901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010902f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010903 typval_T *argvars;
10904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905{
10906 /* Add an entry to the stack of typehead storage. */
10907 if (ga_grow(&ga_userinput, 1) == OK)
10908 {
10909 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10910 + ga_userinput.ga_len);
10911 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 }
10914 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916}
10917
10918/*
10919 * "inputsecret()" function
10920 */
10921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010922f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010923 typval_T *argvars;
10924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925{
10926 ++cmdline_star;
10927 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010928 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 --cmdline_star;
10930 --inputsecret_flag;
10931}
10932
10933/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010934 * "insert()" function
10935 */
10936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010938 typval_T *argvars;
10939 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010940{
10941 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010942 listitem_T *item;
10943 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010944 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010945
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010946 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010947 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010948 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010949 else if ((l = argvars[0].vval.v_list) != NULL
10950 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010951 {
10952 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010953 before = get_tv_number_chk(&argvars[2], &error);
10954 if (error)
10955 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010956
Bram Moolenaar758711c2005-02-02 23:11:38 +000010957 if (before == l->lv_len)
10958 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010959 else
10960 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010961 item = list_find(l, before);
10962 if (item == NULL)
10963 {
10964 EMSGN(_(e_listidx), before);
10965 l = NULL;
10966 }
10967 }
10968 if (l != NULL)
10969 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010970 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010971 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010972 }
10973 }
10974}
10975
10976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 * "isdirectory()" function
10978 */
10979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010980f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010981 typval_T *argvars;
10982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010984 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985}
10986
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010987/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010988 * "islocked()" function
10989 */
10990 static void
10991f_islocked(argvars, rettv)
10992 typval_T *argvars;
10993 typval_T *rettv;
10994{
10995 lval_T lv;
10996 char_u *end;
10997 dictitem_T *di;
10998
10999 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011000 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11001 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011002 if (end != NULL && lv.ll_name != NULL)
11003 {
11004 if (*end != NUL)
11005 EMSG(_(e_trailing));
11006 else
11007 {
11008 if (lv.ll_tv == NULL)
11009 {
11010 if (check_changedtick(lv.ll_name))
11011 rettv->vval.v_number = 1; /* always locked */
11012 else
11013 {
11014 di = find_var(lv.ll_name, NULL);
11015 if (di != NULL)
11016 {
11017 /* Consider a variable locked when:
11018 * 1. the variable itself is locked
11019 * 2. the value of the variable is locked.
11020 * 3. the List or Dict value is locked.
11021 */
11022 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11023 || tv_islocked(&di->di_tv));
11024 }
11025 }
11026 }
11027 else if (lv.ll_range)
11028 EMSG(_("E745: Range not allowed"));
11029 else if (lv.ll_newkey != NULL)
11030 EMSG2(_(e_dictkey), lv.ll_newkey);
11031 else if (lv.ll_list != NULL)
11032 /* List item. */
11033 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11034 else
11035 /* Dictionary item. */
11036 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11037 }
11038 }
11039
11040 clear_lval(&lv);
11041}
11042
Bram Moolenaar33570922005-01-25 22:26:29 +000011043static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011044
11045/*
11046 * Turn a dict into a list:
11047 * "what" == 0: list of keys
11048 * "what" == 1: list of values
11049 * "what" == 2: list of items
11050 */
11051 static void
11052dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011053 typval_T *argvars;
11054 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011055 int what;
11056{
Bram Moolenaar33570922005-01-25 22:26:29 +000011057 list_T *l;
11058 list_T *l2;
11059 dictitem_T *di;
11060 hashitem_T *hi;
11061 listitem_T *li;
11062 listitem_T *li2;
11063 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011064 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011065
11066 rettv->vval.v_number = 0;
11067 if (argvars[0].v_type != VAR_DICT)
11068 {
11069 EMSG(_(e_dictreq));
11070 return;
11071 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011072 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011073 return;
11074
11075 l = list_alloc();
11076 if (l == NULL)
11077 return;
11078 rettv->v_type = VAR_LIST;
11079 rettv->vval.v_list = l;
11080 ++l->lv_refcount;
11081
Bram Moolenaar33570922005-01-25 22:26:29 +000011082 todo = d->dv_hashtab.ht_used;
11083 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011084 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011085 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011086 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011087 --todo;
11088 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011089
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011090 li = listitem_alloc();
11091 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011092 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011093 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011094
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011095 if (what == 0)
11096 {
11097 /* keys() */
11098 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011099 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011100 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11101 }
11102 else if (what == 1)
11103 {
11104 /* values() */
11105 copy_tv(&di->di_tv, &li->li_tv);
11106 }
11107 else
11108 {
11109 /* items() */
11110 l2 = list_alloc();
11111 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011112 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011113 li->li_tv.vval.v_list = l2;
11114 if (l2 == NULL)
11115 break;
11116 ++l2->lv_refcount;
11117
11118 li2 = listitem_alloc();
11119 if (li2 == NULL)
11120 break;
11121 list_append(l2, li2);
11122 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011123 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011124 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11125
11126 li2 = listitem_alloc();
11127 if (li2 == NULL)
11128 break;
11129 list_append(l2, li2);
11130 copy_tv(&di->di_tv, &li2->li_tv);
11131 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011132 }
11133 }
11134}
11135
11136/*
11137 * "items(dict)" function
11138 */
11139 static void
11140f_items(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, 2);
11145}
11146
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011148 * "join()" function
11149 */
11150 static void
11151f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011152 typval_T *argvars;
11153 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011154{
11155 garray_T ga;
11156 char_u *sep;
11157
11158 rettv->vval.v_number = 0;
11159 if (argvars[0].v_type != VAR_LIST)
11160 {
11161 EMSG(_(e_listreq));
11162 return;
11163 }
11164 if (argvars[0].vval.v_list == NULL)
11165 return;
11166 if (argvars[1].v_type == VAR_UNKNOWN)
11167 sep = (char_u *)" ";
11168 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011169 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011170
11171 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011172
11173 if (sep != NULL)
11174 {
11175 ga_init2(&ga, (int)sizeof(char), 80);
11176 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11177 ga_append(&ga, NUL);
11178 rettv->vval.v_string = (char_u *)ga.ga_data;
11179 }
11180 else
11181 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011182}
11183
11184/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011185 * "keys()" function
11186 */
11187 static void
11188f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011189 typval_T *argvars;
11190 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011191{
11192 dict_list(argvars, rettv, 0);
11193}
11194
11195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196 * "last_buffer_nr()" function.
11197 */
11198/*ARGSUSED*/
11199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011200f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011201 typval_T *argvars;
11202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203{
11204 int n = 0;
11205 buf_T *buf;
11206
11207 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11208 if (n < buf->b_fnum)
11209 n = buf->b_fnum;
11210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011211 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011212}
11213
11214/*
11215 * "len()" function
11216 */
11217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011219 typval_T *argvars;
11220 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011221{
11222 switch (argvars[0].v_type)
11223 {
11224 case VAR_STRING:
11225 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 rettv->vval.v_number = (varnumber_T)STRLEN(
11227 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011228 break;
11229 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011230 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011231 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011232 case VAR_DICT:
11233 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11234 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011235 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011236 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011237 break;
11238 }
11239}
11240
Bram Moolenaar33570922005-01-25 22:26:29 +000011241static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011242
11243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011244libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011245 typval_T *argvars;
11246 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011247 int type;
11248{
11249#ifdef FEAT_LIBCALL
11250 char_u *string_in;
11251 char_u **string_result;
11252 int nr_result;
11253#endif
11254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011256 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011257 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011258 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011260
11261 if (check_restricted() || check_secure())
11262 return;
11263
11264#ifdef FEAT_LIBCALL
11265 /* The first two args must be strings, otherwise its meaningless */
11266 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11267 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011268 string_in = NULL;
11269 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011270 string_in = argvars[2].vval.v_string;
11271 if (type == VAR_NUMBER)
11272 string_result = NULL;
11273 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011274 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011275 if (mch_libcall(argvars[0].vval.v_string,
11276 argvars[1].vval.v_string,
11277 string_in,
11278 argvars[2].vval.v_number,
11279 string_result,
11280 &nr_result) == OK
11281 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011283 }
11284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285}
11286
11287/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011288 * "libcall()" function
11289 */
11290 static void
11291f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 typval_T *argvars;
11293 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011294{
11295 libcall_common(argvars, rettv, VAR_STRING);
11296}
11297
11298/*
11299 * "libcallnr()" function
11300 */
11301 static void
11302f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011303 typval_T *argvars;
11304 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011305{
11306 libcall_common(argvars, rettv, VAR_NUMBER);
11307}
11308
11309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 * "line(string)" function
11311 */
11312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011313f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011314 typval_T *argvars;
11315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316{
11317 linenr_T lnum = 0;
11318 pos_T *fp;
11319
11320 fp = var2fpos(&argvars[0], TRUE);
11321 if (fp != NULL)
11322 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324}
11325
11326/*
11327 * "line2byte(lnum)" function
11328 */
11329/*ARGSUSED*/
11330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011332 typval_T *argvars;
11333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334{
11335#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337#else
11338 linenr_T lnum;
11339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011344 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11345 if (rettv->vval.v_number >= 0)
11346 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347#endif
11348}
11349
11350/*
11351 * "lispindent(lnum)" function
11352 */
11353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011354f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011355 typval_T *argvars;
11356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357{
11358#ifdef FEAT_LISP
11359 pos_T pos;
11360 linenr_T lnum;
11361
11362 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011363 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11365 {
11366 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011367 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 curwin->w_cursor = pos;
11369 }
11370 else
11371#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373}
11374
11375/*
11376 * "localtime()" function
11377 */
11378/*ARGSUSED*/
11379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011380f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011381 typval_T *argvars;
11382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011384 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385}
11386
Bram Moolenaar33570922005-01-25 22:26:29 +000011387static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388
11389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011391 typval_T *argvars;
11392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 int exact;
11394{
11395 char_u *keys;
11396 char_u *which;
11397 char_u buf[NUMBUFLEN];
11398 char_u *keys_buf = NULL;
11399 char_u *rhs;
11400 int mode;
11401 garray_T ga;
11402
11403 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404 rettv->v_type = VAR_STRING;
11405 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011407 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 if (*keys == NUL)
11409 return;
11410
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011411 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011412 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 else
11414 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011415 if (which == NULL)
11416 return;
11417
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 mode = get_map_mode(&which, 0);
11419
11420 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011421 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 vim_free(keys_buf);
11423 if (rhs != NULL)
11424 {
11425 ga_init(&ga);
11426 ga.ga_itemsize = 1;
11427 ga.ga_growsize = 40;
11428
11429 while (*rhs != NUL)
11430 ga_concat(&ga, str2special(&rhs, FALSE));
11431
11432 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011433 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 }
11435}
11436
11437/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011438 * "map()" function
11439 */
11440 static void
11441f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011442 typval_T *argvars;
11443 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011444{
11445 filter_map(argvars, rettv, TRUE);
11446}
11447
11448/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011449 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 */
11451 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011452f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011453 typval_T *argvars;
11454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011456 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457}
11458
11459/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011460 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461 */
11462 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011463f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011464 typval_T *argvars;
11465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011467 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468}
11469
Bram Moolenaar33570922005-01-25 22:26:29 +000011470static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471
11472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011474 typval_T *argvars;
11475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476 int type;
11477{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011478 char_u *str = NULL;
11479 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 char_u *pat;
11481 regmatch_T regmatch;
11482 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011483 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484 char_u *save_cpo;
11485 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011486 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011487 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011488 list_T *l = NULL;
11489 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011490 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011491 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492
11493 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11494 save_cpo = p_cpo;
11495 p_cpo = (char_u *)"";
11496
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011497 rettv->vval.v_number = -1;
11498 if (type == 3)
11499 {
11500 /* return empty list when there are no matches */
11501 if ((rettv->vval.v_list = list_alloc()) == NULL)
11502 goto theend;
11503 rettv->v_type = VAR_LIST;
11504 ++rettv->vval.v_list->lv_refcount;
11505 }
11506 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011508 rettv->v_type = VAR_STRING;
11509 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011512 if (argvars[0].v_type == VAR_LIST)
11513 {
11514 if ((l = argvars[0].vval.v_list) == NULL)
11515 goto theend;
11516 li = l->lv_first;
11517 }
11518 else
11519 expr = str = get_tv_string(&argvars[0]);
11520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011521 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11522 if (pat == NULL)
11523 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011524
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011525 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011527 int error = FALSE;
11528
11529 start = get_tv_number_chk(&argvars[2], &error);
11530 if (error)
11531 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011532 if (l != NULL)
11533 {
11534 li = list_find(l, start);
11535 if (li == NULL)
11536 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011537 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011538 }
11539 else
11540 {
11541 if (start < 0)
11542 start = 0;
11543 if (start > (long)STRLEN(str))
11544 goto theend;
11545 str += start;
11546 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011547
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011548 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011549 nth = get_tv_number_chk(&argvars[3], &error);
11550 if (error)
11551 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552 }
11553
11554 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11555 if (regmatch.regprog != NULL)
11556 {
11557 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011558
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011559 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011560 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011561 if (l != NULL)
11562 {
11563 if (li == NULL)
11564 {
11565 match = FALSE;
11566 break;
11567 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011568 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011569 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011570 if (str == NULL)
11571 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011572 }
11573
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011574 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011575
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011576 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011577 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011578 if (l == NULL && !match)
11579 break;
11580
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011581 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011582 if (l != NULL)
11583 {
11584 li = li->li_next;
11585 ++idx;
11586 }
11587 else
11588 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011589#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011590 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011591#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011592 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011593#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011594 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011595 }
11596
11597 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011599 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011600 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011601 int i;
11602
11603 /* return list with matched string and submatches */
11604 for (i = 0; i < NSUBEXP; ++i)
11605 {
11606 if (regmatch.endp[i] == NULL)
11607 break;
11608 li = listitem_alloc();
11609 if (li == NULL)
11610 break;
11611 li->li_tv.v_type = VAR_STRING;
11612 li->li_tv.v_lock = 0;
11613 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11614 (int)(regmatch.endp[i] - regmatch.startp[i]));
11615 list_append(rettv->vval.v_list, li);
11616 }
11617 }
11618 else if (type == 2)
11619 {
11620 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011621 if (l != NULL)
11622 copy_tv(&li->li_tv, rettv);
11623 else
11624 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011626 }
11627 else if (l != NULL)
11628 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629 else
11630 {
11631 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011632 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633 (varnumber_T)(regmatch.startp[0] - str);
11634 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011635 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011637 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638 }
11639 }
11640 vim_free(regmatch.regprog);
11641 }
11642
11643theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011644 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645 p_cpo = save_cpo;
11646}
11647
11648/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011649 * "match()" function
11650 */
11651 static void
11652f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011653 typval_T *argvars;
11654 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011655{
11656 find_some_match(argvars, rettv, 1);
11657}
11658
11659/*
11660 * "matchend()" function
11661 */
11662 static void
11663f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011664 typval_T *argvars;
11665 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011666{
11667 find_some_match(argvars, rettv, 0);
11668}
11669
11670/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011671 * "matchlist()" function
11672 */
11673 static void
11674f_matchlist(argvars, rettv)
11675 typval_T *argvars;
11676 typval_T *rettv;
11677{
11678 find_some_match(argvars, rettv, 3);
11679}
11680
11681/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011682 * "matchstr()" function
11683 */
11684 static void
11685f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011686 typval_T *argvars;
11687 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011688{
11689 find_some_match(argvars, rettv, 2);
11690}
11691
Bram Moolenaar33570922005-01-25 22:26:29 +000011692static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011693
11694 static void
11695max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011696 typval_T *argvars;
11697 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011698 int domax;
11699{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011700 long n = 0;
11701 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011702 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011703
11704 if (argvars[0].v_type == VAR_LIST)
11705 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011706 list_T *l;
11707 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011708
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011709 l = argvars[0].vval.v_list;
11710 if (l != NULL)
11711 {
11712 li = l->lv_first;
11713 if (li != NULL)
11714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011715 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011716 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011717 {
11718 li = li->li_next;
11719 if (li == NULL)
11720 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011721 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011722 if (domax ? i > n : i < n)
11723 n = i;
11724 }
11725 }
11726 }
11727 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011728 else if (argvars[0].v_type == VAR_DICT)
11729 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011730 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011731 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011732 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011733 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011734
11735 d = argvars[0].vval.v_dict;
11736 if (d != NULL)
11737 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011738 todo = d->dv_hashtab.ht_used;
11739 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011740 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011741 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011742 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011743 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011744 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011745 if (first)
11746 {
11747 n = i;
11748 first = FALSE;
11749 }
11750 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011751 n = i;
11752 }
11753 }
11754 }
11755 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011756 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011757 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011758 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011759}
11760
11761/*
11762 * "max()" function
11763 */
11764 static void
11765f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011766 typval_T *argvars;
11767 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011768{
11769 max_min(argvars, rettv, TRUE);
11770}
11771
11772/*
11773 * "min()" function
11774 */
11775 static void
11776f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011777 typval_T *argvars;
11778 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011779{
11780 max_min(argvars, rettv, FALSE);
11781}
11782
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011783static int mkdir_recurse __ARGS((char_u *dir, int prot));
11784
11785/*
11786 * Create the directory in which "dir" is located, and higher levels when
11787 * needed.
11788 */
11789 static int
11790mkdir_recurse(dir, prot)
11791 char_u *dir;
11792 int prot;
11793{
11794 char_u *p;
11795 char_u *updir;
11796 int r = FAIL;
11797
11798 /* Get end of directory name in "dir".
11799 * We're done when it's "/" or "c:/". */
11800 p = gettail_sep(dir);
11801 if (p <= get_past_head(dir))
11802 return OK;
11803
11804 /* If the directory exists we're done. Otherwise: create it.*/
11805 updir = vim_strnsave(dir, (int)(p - dir));
11806 if (updir == NULL)
11807 return FAIL;
11808 if (mch_isdir(updir))
11809 r = OK;
11810 else if (mkdir_recurse(updir, prot) == OK)
11811 r = vim_mkdir_emsg(updir, prot);
11812 vim_free(updir);
11813 return r;
11814}
11815
11816#ifdef vim_mkdir
11817/*
11818 * "mkdir()" function
11819 */
11820 static void
11821f_mkdir(argvars, rettv)
11822 typval_T *argvars;
11823 typval_T *rettv;
11824{
11825 char_u *dir;
11826 char_u buf[NUMBUFLEN];
11827 int prot = 0755;
11828
11829 rettv->vval.v_number = FAIL;
11830 if (check_restricted() || check_secure())
11831 return;
11832
11833 dir = get_tv_string_buf(&argvars[0], buf);
11834 if (argvars[1].v_type != VAR_UNKNOWN)
11835 {
11836 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011837 prot = get_tv_number_chk(&argvars[2], NULL);
11838 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011839 mkdir_recurse(dir, prot);
11840 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011841 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011842}
11843#endif
11844
Bram Moolenaar0d660222005-01-07 21:51:51 +000011845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846 * "mode()" function
11847 */
11848/*ARGSUSED*/
11849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011850f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011851 typval_T *argvars;
11852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853{
11854 char_u buf[2];
11855
11856#ifdef FEAT_VISUAL
11857 if (VIsual_active)
11858 {
11859 if (VIsual_select)
11860 buf[0] = VIsual_mode + 's' - 'v';
11861 else
11862 buf[0] = VIsual_mode;
11863 }
11864 else
11865#endif
11866 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11867 buf[0] = 'r';
11868 else if (State & INSERT)
11869 {
11870 if (State & REPLACE_FLAG)
11871 buf[0] = 'R';
11872 else
11873 buf[0] = 'i';
11874 }
11875 else if (State & CMDLINE)
11876 buf[0] = 'c';
11877 else
11878 buf[0] = 'n';
11879
11880 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011881 rettv->vval.v_string = vim_strsave(buf);
11882 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011883}
11884
11885/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011886 * "nextnonblank()" function
11887 */
11888 static void
11889f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011890 typval_T *argvars;
11891 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011892{
11893 linenr_T lnum;
11894
11895 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011897 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011898 {
11899 lnum = 0;
11900 break;
11901 }
11902 if (*skipwhite(ml_get(lnum)) != NUL)
11903 break;
11904 }
11905 rettv->vval.v_number = lnum;
11906}
11907
11908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909 * "nr2char()" function
11910 */
11911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011912f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011913 typval_T *argvars;
11914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915{
11916 char_u buf[NUMBUFLEN];
11917
11918#ifdef FEAT_MBYTE
11919 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011920 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 else
11922#endif
11923 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011924 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925 buf[1] = NUL;
11926 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011927 rettv->v_type = VAR_STRING;
11928 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011929}
11930
11931/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011932 * "prevnonblank()" function
11933 */
11934 static void
11935f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011936 typval_T *argvars;
11937 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011938{
11939 linenr_T lnum;
11940
11941 lnum = get_tv_lnum(argvars);
11942 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11943 lnum = 0;
11944 else
11945 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11946 --lnum;
11947 rettv->vval.v_number = lnum;
11948}
11949
Bram Moolenaara6c840d2005-08-22 22:59:46 +000011950#ifdef HAVE_STDARG_H
11951/* This dummy va_list is here because:
11952 * - passing a NULL pointer doesn't work when va_list isn't a pointer
11953 * - locally in the function results in a "used before set" warning
11954 * - using va_start() to initialize it gives "function with fixed args" error */
11955static va_list ap;
11956#endif
11957
Bram Moolenaar8c711452005-01-14 21:53:12 +000011958/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000011959 * "printf()" function
11960 */
11961 static void
11962f_printf(argvars, rettv)
11963 typval_T *argvars;
11964 typval_T *rettv;
11965{
11966 rettv->v_type = VAR_STRING;
11967 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000011968#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000011969 {
11970 char_u buf[NUMBUFLEN];
11971 int len;
11972 char_u *s;
11973 int saved_did_emsg = did_emsg;
11974 char *fmt;
11975
11976 /* Get the required length, allocate the buffer and do it for real. */
11977 did_emsg = FALSE;
11978 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011979 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000011980 if (!did_emsg)
11981 {
11982 s = alloc(len + 1);
11983 if (s != NULL)
11984 {
11985 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011986 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000011987 }
11988 }
11989 did_emsg |= saved_did_emsg;
11990 }
11991#endif
11992}
11993
11994/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011995 * "range()" function
11996 */
11997 static void
11998f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011999 typval_T *argvars;
12000 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012001{
12002 long start;
12003 long end;
12004 long stride = 1;
12005 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012006 list_T *l;
12007 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012008 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012010 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012011 if (argvars[1].v_type == VAR_UNKNOWN)
12012 {
12013 end = start - 1;
12014 start = 0;
12015 }
12016 else
12017 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012018 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012019 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012020 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012021 }
12022
12023 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012024 if (error)
12025 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012026 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012027 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012028 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012029 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012030 else
12031 {
12032 l = list_alloc();
12033 if (l != NULL)
12034 {
12035 rettv->v_type = VAR_LIST;
12036 rettv->vval.v_list = l;
12037 ++l->lv_refcount;
12038
12039 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
12040 {
12041 li = listitem_alloc();
12042 if (li == NULL)
12043 break;
12044 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012045 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012046 li->li_tv.vval.v_number = i;
12047 list_append(l, li);
12048 }
12049 }
12050 }
12051}
12052
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012053/*
12054 * "readfile()" function
12055 */
12056 static void
12057f_readfile(argvars, rettv)
12058 typval_T *argvars;
12059 typval_T *rettv;
12060{
12061 int binary = FALSE;
12062 char_u *fname;
12063 FILE *fd;
12064 list_T *l;
12065 listitem_T *li;
12066#define FREAD_SIZE 200 /* optimized for text lines */
12067 char_u buf[FREAD_SIZE];
12068 int readlen; /* size of last fread() */
12069 int buflen; /* nr of valid chars in buf[] */
12070 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12071 int tolist; /* first byte in buf[] still to be put in list */
12072 int chop; /* how many CR to chop off */
12073 char_u *prev = NULL; /* previously read bytes, if any */
12074 int prevlen = 0; /* length of "prev" if not NULL */
12075 char_u *s;
12076 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012077 long maxline = MAXLNUM;
12078 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012079
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012080 if (argvars[1].v_type != VAR_UNKNOWN)
12081 {
12082 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12083 binary = TRUE;
12084 if (argvars[2].v_type != VAR_UNKNOWN)
12085 maxline = get_tv_number(&argvars[2]);
12086 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012087
12088 l = list_alloc();
12089 if (l == NULL)
12090 return;
12091 rettv->v_type = VAR_LIST;
12092 rettv->vval.v_list = l;
12093 l->lv_refcount = 1;
12094
12095 /* Always open the file in binary mode, library functions have a mind of
12096 * their own about CR-LF conversion. */
12097 fname = get_tv_string(&argvars[0]);
12098 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12099 {
12100 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12101 return;
12102 }
12103
12104 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012105 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012106 {
12107 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12108 buflen = filtd + readlen;
12109 tolist = 0;
12110 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12111 {
12112 if (buf[filtd] == '\n' || readlen <= 0)
12113 {
12114 /* Only when in binary mode add an empty list item when the
12115 * last line ends in a '\n'. */
12116 if (!binary && readlen == 0 && filtd == 0)
12117 break;
12118
12119 /* Found end-of-line or end-of-file: add a text line to the
12120 * list. */
12121 chop = 0;
12122 if (!binary)
12123 while (filtd - chop - 1 >= tolist
12124 && buf[filtd - chop - 1] == '\r')
12125 ++chop;
12126 len = filtd - tolist - chop;
12127 if (prev == NULL)
12128 s = vim_strnsave(buf + tolist, len);
12129 else
12130 {
12131 s = alloc((unsigned)(prevlen + len + 1));
12132 if (s != NULL)
12133 {
12134 mch_memmove(s, prev, prevlen);
12135 vim_free(prev);
12136 prev = NULL;
12137 mch_memmove(s + prevlen, buf + tolist, len);
12138 s[prevlen + len] = NUL;
12139 }
12140 }
12141 tolist = filtd + 1;
12142
12143 li = listitem_alloc();
12144 if (li == NULL)
12145 {
12146 vim_free(s);
12147 break;
12148 }
12149 li->li_tv.v_type = VAR_STRING;
12150 li->li_tv.v_lock = 0;
12151 li->li_tv.vval.v_string = s;
12152 list_append(l, li);
12153
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012154 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012155 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012156 if (readlen <= 0)
12157 break;
12158 }
12159 else if (buf[filtd] == NUL)
12160 buf[filtd] = '\n';
12161 }
12162 if (readlen <= 0)
12163 break;
12164
12165 if (tolist == 0)
12166 {
12167 /* "buf" is full, need to move text to an allocated buffer */
12168 if (prev == NULL)
12169 {
12170 prev = vim_strnsave(buf, buflen);
12171 prevlen = buflen;
12172 }
12173 else
12174 {
12175 s = alloc((unsigned)(prevlen + buflen));
12176 if (s != NULL)
12177 {
12178 mch_memmove(s, prev, prevlen);
12179 mch_memmove(s + prevlen, buf, buflen);
12180 vim_free(prev);
12181 prev = s;
12182 prevlen += buflen;
12183 }
12184 }
12185 filtd = 0;
12186 }
12187 else
12188 {
12189 mch_memmove(buf, buf + tolist, buflen - tolist);
12190 filtd -= tolist;
12191 }
12192 }
12193
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012194 /*
12195 * For a negative line count use only the lines at the end of the file,
12196 * free the rest.
12197 */
12198 if (maxline < 0)
12199 while (cnt > -maxline)
12200 {
12201 listitem_remove(l, l->lv_first);
12202 --cnt;
12203 }
12204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012205 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012206 fclose(fd);
12207}
12208
12209
Bram Moolenaar0d660222005-01-07 21:51:51 +000012210#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12211static void make_connection __ARGS((void));
12212static int check_connection __ARGS((void));
12213
12214 static void
12215make_connection()
12216{
12217 if (X_DISPLAY == NULL
12218# ifdef FEAT_GUI
12219 && !gui.in_use
12220# endif
12221 )
12222 {
12223 x_force_connect = TRUE;
12224 setup_term_clip();
12225 x_force_connect = FALSE;
12226 }
12227}
12228
12229 static int
12230check_connection()
12231{
12232 make_connection();
12233 if (X_DISPLAY == NULL)
12234 {
12235 EMSG(_("E240: No connection to Vim server"));
12236 return FAIL;
12237 }
12238 return OK;
12239}
12240#endif
12241
12242#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012243static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012244
12245 static void
12246remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012247 typval_T *argvars;
12248 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012249 int expr;
12250{
12251 char_u *server_name;
12252 char_u *keys;
12253 char_u *r = NULL;
12254 char_u buf[NUMBUFLEN];
12255# ifdef WIN32
12256 HWND w;
12257# else
12258 Window w;
12259# endif
12260
12261 if (check_restricted() || check_secure())
12262 return;
12263
12264# ifdef FEAT_X11
12265 if (check_connection() == FAIL)
12266 return;
12267# endif
12268
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012269 server_name = get_tv_string_chk(&argvars[0]);
12270 if (server_name == NULL)
12271 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012272 keys = get_tv_string_buf(&argvars[1], buf);
12273# ifdef WIN32
12274 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12275# else
12276 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12277 < 0)
12278# endif
12279 {
12280 if (r != NULL)
12281 EMSG(r); /* sending worked but evaluation failed */
12282 else
12283 EMSG2(_("E241: Unable to send to %s"), server_name);
12284 return;
12285 }
12286
12287 rettv->vval.v_string = r;
12288
12289 if (argvars[2].v_type != VAR_UNKNOWN)
12290 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012291 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012292 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012293 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012294
12295 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012296 v.di_tv.v_type = VAR_STRING;
12297 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012298 idvar = get_tv_string_chk(&argvars[2]);
12299 if (idvar != NULL)
12300 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012301 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012302 }
12303}
12304#endif
12305
12306/*
12307 * "remote_expr()" function
12308 */
12309/*ARGSUSED*/
12310 static void
12311f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012312 typval_T *argvars;
12313 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012314{
12315 rettv->v_type = VAR_STRING;
12316 rettv->vval.v_string = NULL;
12317#ifdef FEAT_CLIENTSERVER
12318 remote_common(argvars, rettv, TRUE);
12319#endif
12320}
12321
12322/*
12323 * "remote_foreground()" function
12324 */
12325/*ARGSUSED*/
12326 static void
12327f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012328 typval_T *argvars;
12329 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012330{
12331 rettv->vval.v_number = 0;
12332#ifdef FEAT_CLIENTSERVER
12333# ifdef WIN32
12334 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012335 {
12336 char_u *server_name = get_tv_string_chk(&argvars[0]);
12337
12338 if (server_name != NULL)
12339 serverForeground(server_name);
12340 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012341# else
12342 /* Send a foreground() expression to the server. */
12343 argvars[1].v_type = VAR_STRING;
12344 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12345 argvars[2].v_type = VAR_UNKNOWN;
12346 remote_common(argvars, rettv, TRUE);
12347 vim_free(argvars[1].vval.v_string);
12348# endif
12349#endif
12350}
12351
12352/*ARGSUSED*/
12353 static void
12354f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012355 typval_T *argvars;
12356 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012357{
12358#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012359 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012360 char_u *s = NULL;
12361# ifdef WIN32
12362 int n = 0;
12363# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012364 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012365
12366 if (check_restricted() || check_secure())
12367 {
12368 rettv->vval.v_number = -1;
12369 return;
12370 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012371 serverid = get_tv_string_chk(&argvars[0]);
12372 if (serverid == NULL)
12373 {
12374 rettv->vval.v_number = -1;
12375 return; /* type error; errmsg already given */
12376 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012377# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012378 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012379 if (n == 0)
12380 rettv->vval.v_number = -1;
12381 else
12382 {
12383 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12384 rettv->vval.v_number = (s != NULL);
12385 }
12386# else
12387 rettv->vval.v_number = 0;
12388 if (check_connection() == FAIL)
12389 return;
12390
12391 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012392 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012393# endif
12394
12395 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12396 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012397 char_u *retvar;
12398
Bram Moolenaar33570922005-01-25 22:26:29 +000012399 v.di_tv.v_type = VAR_STRING;
12400 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012401 retvar = get_tv_string_chk(&argvars[1]);
12402 if (retvar != NULL)
12403 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012404 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012405 }
12406#else
12407 rettv->vval.v_number = -1;
12408#endif
12409}
12410
12411/*ARGSUSED*/
12412 static void
12413f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012414 typval_T *argvars;
12415 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012416{
12417 char_u *r = NULL;
12418
12419#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012420 char_u *serverid = get_tv_string_chk(&argvars[0]);
12421
12422 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012423 {
12424# ifdef WIN32
12425 /* The server's HWND is encoded in the 'id' parameter */
12426 int n = 0;
12427
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012428 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012429 if (n != 0)
12430 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12431 if (r == NULL)
12432# else
12433 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012434 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012435# endif
12436 EMSG(_("E277: Unable to read a server reply"));
12437 }
12438#endif
12439 rettv->v_type = VAR_STRING;
12440 rettv->vval.v_string = r;
12441}
12442
12443/*
12444 * "remote_send()" function
12445 */
12446/*ARGSUSED*/
12447 static void
12448f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012449 typval_T *argvars;
12450 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012451{
12452 rettv->v_type = VAR_STRING;
12453 rettv->vval.v_string = NULL;
12454#ifdef FEAT_CLIENTSERVER
12455 remote_common(argvars, rettv, FALSE);
12456#endif
12457}
12458
12459/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012460 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012461 */
12462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012463f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012464 typval_T *argvars;
12465 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012466{
Bram Moolenaar33570922005-01-25 22:26:29 +000012467 list_T *l;
12468 listitem_T *item, *item2;
12469 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012470 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012471 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012472 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012473 dict_T *d;
12474 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012475
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012476 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012477 if (argvars[0].v_type == VAR_DICT)
12478 {
12479 if (argvars[2].v_type != VAR_UNKNOWN)
12480 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012481 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012482 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012484 key = get_tv_string_chk(&argvars[1]);
12485 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012487 di = dict_find(d, key, -1);
12488 if (di == NULL)
12489 EMSG2(_(e_dictkey), key);
12490 else
12491 {
12492 *rettv = di->di_tv;
12493 init_tv(&di->di_tv);
12494 dictitem_remove(d, di);
12495 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012496 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012497 }
12498 }
12499 else if (argvars[0].v_type != VAR_LIST)
12500 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012501 else if ((l = argvars[0].vval.v_list) != NULL
12502 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012503 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012504 int error = FALSE;
12505
12506 idx = get_tv_number_chk(&argvars[1], &error);
12507 if (error)
12508 ; /* type error: do nothing, errmsg already given */
12509 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012510 EMSGN(_(e_listidx), idx);
12511 else
12512 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012513 if (argvars[2].v_type == VAR_UNKNOWN)
12514 {
12515 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012516 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012517 *rettv = item->li_tv;
12518 vim_free(item);
12519 }
12520 else
12521 {
12522 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012523 end = get_tv_number_chk(&argvars[2], &error);
12524 if (error)
12525 ; /* type error: do nothing */
12526 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012527 EMSGN(_(e_listidx), end);
12528 else
12529 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012530 int cnt = 0;
12531
12532 for (li = item; li != NULL; li = li->li_next)
12533 {
12534 ++cnt;
12535 if (li == item2)
12536 break;
12537 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012538 if (li == NULL) /* didn't find "item2" after "item" */
12539 EMSG(_(e_invrange));
12540 else
12541 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012542 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012543 l = list_alloc();
12544 if (l != NULL)
12545 {
12546 rettv->v_type = VAR_LIST;
12547 rettv->vval.v_list = l;
12548 l->lv_first = item;
12549 l->lv_last = item2;
12550 l->lv_refcount = 1;
12551 item->li_prev = NULL;
12552 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012553 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012554 }
12555 }
12556 }
12557 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012558 }
12559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560}
12561
12562/*
12563 * "rename({from}, {to})" function
12564 */
12565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012566f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012567 typval_T *argvars;
12568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569{
12570 char_u buf[NUMBUFLEN];
12571
12572 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012573 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012575 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12576 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577}
12578
12579/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012580 * "repeat()" function
12581 */
12582/*ARGSUSED*/
12583 static void
12584f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012585 typval_T *argvars;
12586 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012587{
12588 char_u *p;
12589 int n;
12590 int slen;
12591 int len;
12592 char_u *r;
12593 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012594 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012595
12596 n = get_tv_number(&argvars[1]);
12597 if (argvars[0].v_type == VAR_LIST)
12598 {
12599 l = list_alloc();
12600 if (l != NULL && argvars[0].vval.v_list != NULL)
12601 {
12602 l->lv_refcount = 1;
12603 while (n-- > 0)
12604 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12605 break;
12606 }
12607 rettv->v_type = VAR_LIST;
12608 rettv->vval.v_list = l;
12609 }
12610 else
12611 {
12612 p = get_tv_string(&argvars[0]);
12613 rettv->v_type = VAR_STRING;
12614 rettv->vval.v_string = NULL;
12615
12616 slen = (int)STRLEN(p);
12617 len = slen * n;
12618 if (len <= 0)
12619 return;
12620
12621 r = alloc(len + 1);
12622 if (r != NULL)
12623 {
12624 for (i = 0; i < n; i++)
12625 mch_memmove(r + i * slen, p, (size_t)slen);
12626 r[len] = NUL;
12627 }
12628
12629 rettv->vval.v_string = r;
12630 }
12631}
12632
12633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634 * "resolve()" function
12635 */
12636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012637f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012638 typval_T *argvars;
12639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640{
12641 char_u *p;
12642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644#ifdef FEAT_SHORTCUT
12645 {
12646 char_u *v = NULL;
12647
12648 v = mch_resolve_shortcut(p);
12649 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012650 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012652 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653 }
12654#else
12655# ifdef HAVE_READLINK
12656 {
12657 char_u buf[MAXPATHL + 1];
12658 char_u *cpy;
12659 int len;
12660 char_u *remain = NULL;
12661 char_u *q;
12662 int is_relative_to_current = FALSE;
12663 int has_trailing_pathsep = FALSE;
12664 int limit = 100;
12665
12666 p = vim_strsave(p);
12667
12668 if (p[0] == '.' && (vim_ispathsep(p[1])
12669 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12670 is_relative_to_current = TRUE;
12671
12672 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012673 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 has_trailing_pathsep = TRUE;
12675
12676 q = getnextcomp(p);
12677 if (*q != NUL)
12678 {
12679 /* Separate the first path component in "p", and keep the
12680 * remainder (beginning with the path separator). */
12681 remain = vim_strsave(q - 1);
12682 q[-1] = NUL;
12683 }
12684
12685 for (;;)
12686 {
12687 for (;;)
12688 {
12689 len = readlink((char *)p, (char *)buf, MAXPATHL);
12690 if (len <= 0)
12691 break;
12692 buf[len] = NUL;
12693
12694 if (limit-- == 0)
12695 {
12696 vim_free(p);
12697 vim_free(remain);
12698 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012699 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700 goto fail;
12701 }
12702
12703 /* Ensure that the result will have a trailing path separator
12704 * if the argument has one. */
12705 if (remain == NULL && has_trailing_pathsep)
12706 add_pathsep(buf);
12707
12708 /* Separate the first path component in the link value and
12709 * concatenate the remainders. */
12710 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12711 if (*q != NUL)
12712 {
12713 if (remain == NULL)
12714 remain = vim_strsave(q - 1);
12715 else
12716 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012717 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718 if (cpy != NULL)
12719 {
12720 STRCAT(cpy, remain);
12721 vim_free(remain);
12722 remain = cpy;
12723 }
12724 }
12725 q[-1] = NUL;
12726 }
12727
12728 q = gettail(p);
12729 if (q > p && *q == NUL)
12730 {
12731 /* Ignore trailing path separator. */
12732 q[-1] = NUL;
12733 q = gettail(p);
12734 }
12735 if (q > p && !mch_isFullName(buf))
12736 {
12737 /* symlink is relative to directory of argument */
12738 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12739 if (cpy != NULL)
12740 {
12741 STRCPY(cpy, p);
12742 STRCPY(gettail(cpy), buf);
12743 vim_free(p);
12744 p = cpy;
12745 }
12746 }
12747 else
12748 {
12749 vim_free(p);
12750 p = vim_strsave(buf);
12751 }
12752 }
12753
12754 if (remain == NULL)
12755 break;
12756
12757 /* Append the first path component of "remain" to "p". */
12758 q = getnextcomp(remain + 1);
12759 len = q - remain - (*q != NUL);
12760 cpy = vim_strnsave(p, STRLEN(p) + len);
12761 if (cpy != NULL)
12762 {
12763 STRNCAT(cpy, remain, len);
12764 vim_free(p);
12765 p = cpy;
12766 }
12767 /* Shorten "remain". */
12768 if (*q != NUL)
12769 STRCPY(remain, q - 1);
12770 else
12771 {
12772 vim_free(remain);
12773 remain = NULL;
12774 }
12775 }
12776
12777 /* If the result is a relative path name, make it explicitly relative to
12778 * the current directory if and only if the argument had this form. */
12779 if (!vim_ispathsep(*p))
12780 {
12781 if (is_relative_to_current
12782 && *p != NUL
12783 && !(p[0] == '.'
12784 && (p[1] == NUL
12785 || vim_ispathsep(p[1])
12786 || (p[1] == '.'
12787 && (p[2] == NUL
12788 || vim_ispathsep(p[2]))))))
12789 {
12790 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012791 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 if (cpy != NULL)
12793 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794 vim_free(p);
12795 p = cpy;
12796 }
12797 }
12798 else if (!is_relative_to_current)
12799 {
12800 /* Strip leading "./". */
12801 q = p;
12802 while (q[0] == '.' && vim_ispathsep(q[1]))
12803 q += 2;
12804 if (q > p)
12805 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12806 }
12807 }
12808
12809 /* Ensure that the result will have no trailing path separator
12810 * if the argument had none. But keep "/" or "//". */
12811 if (!has_trailing_pathsep)
12812 {
12813 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012814 if (after_pathsep(p, q))
12815 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 }
12817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 }
12820# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012821 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822# endif
12823#endif
12824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012825 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826
12827#ifdef HAVE_READLINK
12828fail:
12829#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831}
12832
12833/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012834 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835 */
12836 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012837f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012838 typval_T *argvars;
12839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840{
Bram Moolenaar33570922005-01-25 22:26:29 +000012841 list_T *l;
12842 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843
Bram Moolenaar0d660222005-01-07 21:51:51 +000012844 rettv->vval.v_number = 0;
12845 if (argvars[0].v_type != VAR_LIST)
12846 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012847 else if ((l = argvars[0].vval.v_list) != NULL
12848 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012849 {
12850 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012851 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012852 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012853 while (li != NULL)
12854 {
12855 ni = li->li_prev;
12856 list_append(l, li);
12857 li = ni;
12858 }
12859 rettv->vval.v_list = l;
12860 rettv->v_type = VAR_LIST;
12861 ++l->lv_refcount;
12862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863}
12864
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012865#define SP_NOMOVE 1 /* don't move cursor */
12866#define SP_REPEAT 2 /* repeat to find outer pair */
12867#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000012868#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012869
Bram Moolenaar33570922005-01-25 22:26:29 +000012870static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012871
12872/*
12873 * Get flags for a search function.
12874 * Possibly sets "p_ws".
12875 * Returns BACKWARD, FORWARD or zero (for an error).
12876 */
12877 static int
12878get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012879 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012880 int *flagsp;
12881{
12882 int dir = FORWARD;
12883 char_u *flags;
12884 char_u nbuf[NUMBUFLEN];
12885 int mask;
12886
12887 if (varp->v_type != VAR_UNKNOWN)
12888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012889 flags = get_tv_string_buf_chk(varp, nbuf);
12890 if (flags == NULL)
12891 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012892 while (*flags != NUL)
12893 {
12894 switch (*flags)
12895 {
12896 case 'b': dir = BACKWARD; break;
12897 case 'w': p_ws = TRUE; break;
12898 case 'W': p_ws = FALSE; break;
12899 default: mask = 0;
12900 if (flagsp != NULL)
12901 switch (*flags)
12902 {
12903 case 'n': mask = SP_NOMOVE; break;
12904 case 'r': mask = SP_REPEAT; break;
12905 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012906 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012907 }
12908 if (mask == 0)
12909 {
12910 EMSG2(_(e_invarg2), flags);
12911 dir = 0;
12912 }
12913 else
12914 *flagsp |= mask;
12915 }
12916 if (dir == 0)
12917 break;
12918 ++flags;
12919 }
12920 }
12921 return dir;
12922}
12923
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924/*
12925 * "search()" function
12926 */
12927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012928f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012929 typval_T *argvars;
12930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931{
12932 char_u *pat;
12933 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012934 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 int save_p_ws = p_ws;
12936 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012937 int flags = 0;
12938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012941 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012942 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12943 if (dir == 0)
12944 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012945 /*
12946 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
12947 * Check to make sure only those flags are set.
12948 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
12949 * flags cannot be set. Check for that condition also.
12950 */
12951 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
12952 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012954 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012955 goto theend;
12956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012958 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12960 SEARCH_KEEP, RE_SEARCH) != FAIL)
12961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012962 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012963 if (flags & SP_SETPCMARK)
12964 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965 curwin->w_cursor = pos;
12966 /* "/$" will put the cursor after the end of the line, may need to
12967 * correct that here */
12968 check_cursor();
12969 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012970
12971 /* If 'n' flag is used: restore cursor position. */
12972 if (flags & SP_NOMOVE)
12973 curwin->w_cursor = save_cursor;
12974theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975 p_ws = save_p_ws;
12976}
12977
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978/*
12979 * "searchpair()" function
12980 */
12981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012982f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012983 typval_T *argvars;
12984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985{
12986 char_u *spat, *mpat, *epat;
12987 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989 int dir;
12990 int flags = 0;
12991 char_u nbuf1[NUMBUFLEN];
12992 char_u nbuf2[NUMBUFLEN];
12993 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012995 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012998 spat = get_tv_string_chk(&argvars[0]);
12999 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13000 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13001 if (spat == NULL || mpat == NULL || epat == NULL)
13002 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004 /* Handle the optional fourth argument: flags */
13005 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013006 if (dir == 0)
13007 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013008 /*
13009 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13010 */
13011 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13012 {
13013 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13014 goto theend;
13015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016
13017 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013018 if (argvars[3].v_type == VAR_UNKNOWN
13019 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020 skip = (char_u *)"";
13021 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013022 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13023 if (skip == NULL)
13024 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013026 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
13027
13028theend:
13029 p_ws = save_p_ws;
13030}
13031
13032/*
13033 * Search for a start/middle/end thing.
13034 * Used by searchpair(), see its documentation for the details.
13035 * Returns 0 or -1 for no match,
13036 */
13037 long
13038do_searchpair(spat, mpat, epat, dir, skip, flags)
13039 char_u *spat; /* start pattern */
13040 char_u *mpat; /* middle pattern */
13041 char_u *epat; /* end pattern */
13042 int dir; /* BACKWARD or FORWARD */
13043 char_u *skip; /* skip expression */
13044 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
13045{
13046 char_u *save_cpo;
13047 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13048 long retval = 0;
13049 pos_T pos;
13050 pos_T firstpos;
13051 pos_T foundpos;
13052 pos_T save_cursor;
13053 pos_T save_pos;
13054 int n;
13055 int r;
13056 int nest = 1;
13057 int err;
13058
13059 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13060 save_cpo = p_cpo;
13061 p_cpo = (char_u *)"";
13062
13063 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13064 * start/middle/end (pat3, for the top pair). */
13065 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13066 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13067 if (pat2 == NULL || pat3 == NULL)
13068 goto theend;
13069 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13070 if (*mpat == NUL)
13071 STRCPY(pat3, pat2);
13072 else
13073 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13074 spat, epat, mpat);
13075
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076 save_cursor = curwin->w_cursor;
13077 pos = curwin->w_cursor;
13078 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013079 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013080 pat = pat3;
13081 for (;;)
13082 {
13083 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13084 SEARCH_KEEP, RE_SEARCH);
13085 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13086 /* didn't find it or found the first match again: FAIL */
13087 break;
13088
13089 if (firstpos.lnum == 0)
13090 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013091 if (equalpos(pos, foundpos))
13092 {
13093 /* Found the same position again. Can happen with a pattern that
13094 * has "\zs" at the end and searching backwards. Advance one
13095 * character and try again. */
13096 if (dir == BACKWARD)
13097 decl(&pos);
13098 else
13099 incl(&pos);
13100 }
13101 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102
13103 /* If the skip pattern matches, ignore this match. */
13104 if (*skip != NUL)
13105 {
13106 save_pos = curwin->w_cursor;
13107 curwin->w_cursor = pos;
13108 r = eval_to_bool(skip, &err, NULL, FALSE);
13109 curwin->w_cursor = save_pos;
13110 if (err)
13111 {
13112 /* Evaluating {skip} caused an error, break here. */
13113 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013114 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115 break;
13116 }
13117 if (r)
13118 continue;
13119 }
13120
13121 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13122 {
13123 /* Found end when searching backwards or start when searching
13124 * forward: nested pair. */
13125 ++nest;
13126 pat = pat2; /* nested, don't search for middle */
13127 }
13128 else
13129 {
13130 /* Found end when searching forward or start when searching
13131 * backward: end of (nested) pair; or found middle in outer pair. */
13132 if (--nest == 1)
13133 pat = pat3; /* outer level, search for middle */
13134 }
13135
13136 if (nest == 0)
13137 {
13138 /* Found the match: return matchcount or line number. */
13139 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013140 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013142 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013143 if (flags & SP_SETPCMARK)
13144 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145 curwin->w_cursor = pos;
13146 if (!(flags & SP_REPEAT))
13147 break;
13148 nest = 1; /* search for next unmatched */
13149 }
13150 }
13151
13152 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013153 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154 curwin->w_cursor = save_cursor;
13155
13156theend:
13157 vim_free(pat2);
13158 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013160
13161 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162}
13163
Bram Moolenaar0d660222005-01-07 21:51:51 +000013164/*ARGSUSED*/
13165 static void
13166f_server2client(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{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013170#ifdef FEAT_CLIENTSERVER
13171 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013172 char_u *server = get_tv_string_chk(&argvars[0]);
13173 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174
Bram Moolenaar0d660222005-01-07 21:51:51 +000013175 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013176 if (server == NULL || reply == NULL)
13177 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013178 if (check_restricted() || check_secure())
13179 return;
13180# ifdef FEAT_X11
13181 if (check_connection() == FAIL)
13182 return;
13183# endif
13184
13185 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013187 EMSG(_("E258: Unable to send to client"));
13188 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013190 rettv->vval.v_number = 0;
13191#else
13192 rettv->vval.v_number = -1;
13193#endif
13194}
13195
13196/*ARGSUSED*/
13197 static void
13198f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013199 typval_T *argvars;
13200 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013201{
13202 char_u *r = NULL;
13203
13204#ifdef FEAT_CLIENTSERVER
13205# ifdef WIN32
13206 r = serverGetVimNames();
13207# else
13208 make_connection();
13209 if (X_DISPLAY != NULL)
13210 r = serverGetVimNames(X_DISPLAY);
13211# endif
13212#endif
13213 rettv->v_type = VAR_STRING;
13214 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215}
13216
13217/*
13218 * "setbufvar()" function
13219 */
13220/*ARGSUSED*/
13221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013222f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013223 typval_T *argvars;
13224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225{
13226 buf_T *buf;
13227#ifdef FEAT_AUTOCMD
13228 aco_save_T aco;
13229#else
13230 buf_T *save_curbuf;
13231#endif
13232 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013233 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234 char_u nbuf[NUMBUFLEN];
13235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013236 rettv->vval.v_number = 0;
13237
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238 if (check_restricted() || check_secure())
13239 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013240 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13241 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013242 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243 varp = &argvars[2];
13244
13245 if (buf != NULL && varname != NULL && varp != NULL)
13246 {
13247 /* set curbuf to be our buf, temporarily */
13248#ifdef FEAT_AUTOCMD
13249 aucmd_prepbuf(&aco, buf);
13250#else
13251 save_curbuf = curbuf;
13252 curbuf = buf;
13253#endif
13254
13255 if (*varname == '&')
13256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013257 long numval;
13258 char_u *strval;
13259 int error = FALSE;
13260
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013262 numval = get_tv_number_chk(varp, &error);
13263 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013264 if (!error && strval != NULL)
13265 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266 }
13267 else
13268 {
13269 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13270 if (bufvarname != NULL)
13271 {
13272 STRCPY(bufvarname, "b:");
13273 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013274 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275 vim_free(bufvarname);
13276 }
13277 }
13278
13279 /* reset notion of buffer */
13280#ifdef FEAT_AUTOCMD
13281 aucmd_restbuf(&aco);
13282#else
13283 curbuf = save_curbuf;
13284#endif
13285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286}
13287
13288/*
13289 * "setcmdpos()" function
13290 */
13291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013292f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013293 typval_T *argvars;
13294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013296 int pos = (int)get_tv_number(&argvars[0]) - 1;
13297
13298 if (pos >= 0)
13299 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300}
13301
13302/*
13303 * "setline()" function
13304 */
13305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013306f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013307 typval_T *argvars;
13308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309{
13310 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013311 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013312 list_T *l = NULL;
13313 listitem_T *li = NULL;
13314 long added = 0;
13315 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013317 lnum = get_tv_lnum(&argvars[0]);
13318 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013320 l = argvars[1].vval.v_list;
13321 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013323 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013324 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013325
13326 rettv->vval.v_number = 0; /* OK */
13327 for (;;)
13328 {
13329 if (l != NULL)
13330 {
13331 /* list argument, get next string */
13332 if (li == NULL)
13333 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013334 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013335 li = li->li_next;
13336 }
13337
13338 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013339 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013340 break;
13341 if (lnum <= curbuf->b_ml.ml_line_count)
13342 {
13343 /* existing line, replace it */
13344 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13345 {
13346 changed_bytes(lnum, 0);
13347 check_cursor_col();
13348 rettv->vval.v_number = 0; /* OK */
13349 }
13350 }
13351 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13352 {
13353 /* lnum is one past the last line, append the line */
13354 ++added;
13355 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13356 rettv->vval.v_number = 0; /* OK */
13357 }
13358
13359 if (l == NULL) /* only one string argument */
13360 break;
13361 ++lnum;
13362 }
13363
13364 if (added > 0)
13365 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366}
13367
13368/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013369 * "setqflist()" function
13370 */
13371/*ARGSUSED*/
13372 static void
13373f_setqflist(argvars, rettv)
13374 typval_T *argvars;
13375 typval_T *rettv;
13376{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013377 char_u *act;
13378 int action = ' ';
13379
Bram Moolenaar2641f772005-03-25 21:58:17 +000013380 rettv->vval.v_number = -1;
13381
13382#ifdef FEAT_QUICKFIX
13383 if (argvars[0].v_type != VAR_LIST)
13384 EMSG(_(e_listreq));
13385 else
13386 {
13387 list_T *l = argvars[0].vval.v_list;
13388
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013389 if (argvars[1].v_type == VAR_STRING)
13390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013391 act = get_tv_string_chk(&argvars[1]);
13392 if (act == NULL)
13393 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013394 if (*act == 'a' || *act == 'r')
13395 action = *act;
13396 }
13397
13398 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013399 rettv->vval.v_number = 0;
13400 }
13401#endif
13402}
13403
13404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405 * "setreg()" function
13406 */
13407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013408f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013409 typval_T *argvars;
13410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411{
13412 int regname;
13413 char_u *strregname;
13414 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013415 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 int append;
13417 char_u yank_type;
13418 long block_len;
13419
13420 block_len = -1;
13421 yank_type = MAUTO;
13422 append = FALSE;
13423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013424 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013425 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013427 if (strregname == NULL)
13428 return; /* type error; errmsg already given */
13429 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 if (regname == 0 || regname == '@')
13431 regname = '"';
13432 else if (regname == '=')
13433 return;
13434
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013435 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013437 stropt = get_tv_string_chk(&argvars[2]);
13438 if (stropt == NULL)
13439 return; /* type error */
13440 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441 switch (*stropt)
13442 {
13443 case 'a': case 'A': /* append */
13444 append = TRUE;
13445 break;
13446 case 'v': case 'c': /* character-wise selection */
13447 yank_type = MCHAR;
13448 break;
13449 case 'V': case 'l': /* line-wise selection */
13450 yank_type = MLINE;
13451 break;
13452#ifdef FEAT_VISUAL
13453 case 'b': case Ctrl_V: /* block-wise selection */
13454 yank_type = MBLOCK;
13455 if (VIM_ISDIGIT(stropt[1]))
13456 {
13457 ++stropt;
13458 block_len = getdigits(&stropt) - 1;
13459 --stropt;
13460 }
13461 break;
13462#endif
13463 }
13464 }
13465
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013466 strval = get_tv_string_chk(&argvars[1]);
13467 if (strval != NULL)
13468 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013470 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471}
13472
13473
13474/*
13475 * "setwinvar(expr)" function
13476 */
13477/*ARGSUSED*/
13478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013479f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013480 typval_T *argvars;
13481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482{
13483 win_T *win;
13484#ifdef FEAT_WINDOWS
13485 win_T *save_curwin;
13486#endif
13487 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013488 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489 char_u nbuf[NUMBUFLEN];
13490
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013491 rettv->vval.v_number = 0;
13492
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493 if (check_restricted() || check_secure())
13494 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013496 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497 varp = &argvars[2];
13498
13499 if (win != NULL && varname != NULL && varp != NULL)
13500 {
13501#ifdef FEAT_WINDOWS
13502 /* set curwin to be our win, temporarily */
13503 save_curwin = curwin;
13504 curwin = win;
13505 curbuf = curwin->w_buffer;
13506#endif
13507
13508 if (*varname == '&')
13509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013510 long numval;
13511 char_u *strval;
13512 int error = FALSE;
13513
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013515 numval = get_tv_number_chk(varp, &error);
13516 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013517 if (!error && strval != NULL)
13518 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519 }
13520 else
13521 {
13522 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13523 if (winvarname != NULL)
13524 {
13525 STRCPY(winvarname, "w:");
13526 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013527 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528 vim_free(winvarname);
13529 }
13530 }
13531
13532#ifdef FEAT_WINDOWS
13533 /* Restore current window, if it's still valid (autocomands can make
13534 * it invalid). */
13535 if (win_valid(save_curwin))
13536 {
13537 curwin = save_curwin;
13538 curbuf = curwin->w_buffer;
13539 }
13540#endif
13541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542}
13543
13544/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013545 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546 */
13547 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013548f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013549 typval_T *argvars;
13550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013552 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553
Bram Moolenaar0d660222005-01-07 21:51:51 +000013554 p = get_tv_string(&argvars[0]);
13555 rettv->vval.v_string = vim_strsave(p);
13556 simplify_filename(rettv->vval.v_string); /* simplify in place */
13557 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558}
13559
Bram Moolenaar0d660222005-01-07 21:51:51 +000013560static int
13561#ifdef __BORLANDC__
13562 _RTLENTRYF
13563#endif
13564 item_compare __ARGS((const void *s1, const void *s2));
13565static int
13566#ifdef __BORLANDC__
13567 _RTLENTRYF
13568#endif
13569 item_compare2 __ARGS((const void *s1, const void *s2));
13570
13571static int item_compare_ic;
13572static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013573static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013574#define ITEM_COMPARE_FAIL 999
13575
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013577 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013579 static int
13580#ifdef __BORLANDC__
13581_RTLENTRYF
13582#endif
13583item_compare(s1, s2)
13584 const void *s1;
13585 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013587 char_u *p1, *p2;
13588 char_u *tofree1, *tofree2;
13589 int res;
13590 char_u numbuf1[NUMBUFLEN];
13591 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592
Bram Moolenaar33570922005-01-25 22:26:29 +000013593 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13594 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013595 if (item_compare_ic)
13596 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013598 res = STRCMP(p1, p2);
13599 vim_free(tofree1);
13600 vim_free(tofree2);
13601 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602}
13603
13604 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013605#ifdef __BORLANDC__
13606_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013608item_compare2(s1, s2)
13609 const void *s1;
13610 const void *s2;
13611{
13612 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013613 typval_T rettv;
13614 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013615 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013617 /* shortcut after failure in previous call; compare all items equal */
13618 if (item_compare_func_err)
13619 return 0;
13620
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013621 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13622 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013623 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13624 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013625
13626 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13627 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013628 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013629 clear_tv(&argv[0]);
13630 clear_tv(&argv[1]);
13631
13632 if (res == FAIL)
13633 res = ITEM_COMPARE_FAIL;
13634 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013635 /* return value has wrong type */
13636 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13637 if (item_compare_func_err)
13638 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013639 clear_tv(&rettv);
13640 return res;
13641}
13642
13643/*
13644 * "sort({list})" function
13645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013647f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013648 typval_T *argvars;
13649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650{
Bram Moolenaar33570922005-01-25 22:26:29 +000013651 list_T *l;
13652 listitem_T *li;
13653 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013654 long len;
13655 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656
Bram Moolenaar0d660222005-01-07 21:51:51 +000013657 rettv->vval.v_number = 0;
13658 if (argvars[0].v_type != VAR_LIST)
13659 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660 else
13661 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013662 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013663 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013664 return;
13665 rettv->vval.v_list = l;
13666 rettv->v_type = VAR_LIST;
13667 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668
Bram Moolenaar0d660222005-01-07 21:51:51 +000013669 len = list_len(l);
13670 if (len <= 1)
13671 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672
Bram Moolenaar0d660222005-01-07 21:51:51 +000013673 item_compare_ic = FALSE;
13674 item_compare_func = NULL;
13675 if (argvars[1].v_type != VAR_UNKNOWN)
13676 {
13677 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013678 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013679 else
13680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013681 int error = FALSE;
13682
13683 i = get_tv_number_chk(&argvars[1], &error);
13684 if (error)
13685 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013686 if (i == 1)
13687 item_compare_ic = TRUE;
13688 else
13689 item_compare_func = get_tv_string(&argvars[1]);
13690 }
13691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692
Bram Moolenaar0d660222005-01-07 21:51:51 +000013693 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013694 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013695 if (ptrs == NULL)
13696 return;
13697 i = 0;
13698 for (li = l->lv_first; li != NULL; li = li->li_next)
13699 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013701 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013702 /* test the compare function */
13703 if (item_compare_func != NULL
13704 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13705 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013706 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013708 {
13709 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013710 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013711 item_compare_func == NULL ? item_compare : item_compare2);
13712
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013713 if (!item_compare_func_err)
13714 {
13715 /* Clear the List and append the items in the sorted order. */
13716 l->lv_first = l->lv_last = NULL;
13717 l->lv_len = 0;
13718 for (i = 0; i < len; ++i)
13719 list_append(l, ptrs[i]);
13720 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013721 }
13722
13723 vim_free(ptrs);
13724 }
13725}
13726
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013727/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013728 * "soundfold({word})" function
13729 */
13730 static void
13731f_soundfold(argvars, rettv)
13732 typval_T *argvars;
13733 typval_T *rettv;
13734{
13735 char_u *s;
13736
13737 rettv->v_type = VAR_STRING;
13738 s = get_tv_string(&argvars[0]);
13739#ifdef FEAT_SYN_HL
13740 rettv->vval.v_string = eval_soundfold(s);
13741#else
13742 rettv->vval.v_string = vim_strsave(s);
13743#endif
13744}
13745
13746/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013747 * "spellbadword()" function
13748 */
13749/* ARGSUSED */
13750 static void
13751f_spellbadword(argvars, rettv)
13752 typval_T *argvars;
13753 typval_T *rettv;
13754{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013755 int len;
13756
13757 rettv->vval.v_string = NULL;
13758 rettv->v_type = VAR_STRING;
13759
13760#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +000013761 /* Find the start and length of the badly spelled word. */
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000013762 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +000013763 if (len != 0)
13764 rettv->vval.v_string = vim_strnsave(ml_get_cursor(), len);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013765#endif
13766}
13767
13768/*
13769 * "spellsuggest()" function
13770 */
13771 static void
13772f_spellsuggest(argvars, rettv)
13773 typval_T *argvars;
13774 typval_T *rettv;
13775{
13776 char_u *str;
13777 int maxcount;
13778 garray_T ga;
13779 list_T *l;
13780 listitem_T *li;
13781 int i;
13782
13783 l = list_alloc();
13784 if (l == NULL)
13785 return;
13786 rettv->v_type = VAR_LIST;
13787 rettv->vval.v_list = l;
13788 ++l->lv_refcount;
13789
13790#ifdef FEAT_SYN_HL
13791 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13792 {
13793 str = get_tv_string(&argvars[0]);
13794 if (argvars[1].v_type != VAR_UNKNOWN)
13795 {
13796 maxcount = get_tv_number(&argvars[1]);
13797 if (maxcount <= 0)
13798 return;
13799 }
13800 else
13801 maxcount = 25;
13802
Bram Moolenaar8c45cdf2005-08-11 20:11:38 +000013803 spell_suggest_list(&ga, str, maxcount, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013804
13805 for (i = 0; i < ga.ga_len; ++i)
13806 {
13807 str = ((char_u **)ga.ga_data)[i];
13808
13809 li = listitem_alloc();
13810 if (li == NULL)
13811 vim_free(str);
13812 else
13813 {
13814 li->li_tv.v_type = VAR_STRING;
13815 li->li_tv.v_lock = 0;
13816 li->li_tv.vval.v_string = str;
13817 list_append(l, li);
13818 }
13819 }
13820 ga_clear(&ga);
13821 }
13822#endif
13823}
13824
Bram Moolenaar0d660222005-01-07 21:51:51 +000013825 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013826f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013827 typval_T *argvars;
13828 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013829{
13830 char_u *str;
13831 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013832 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013833 regmatch_T regmatch;
13834 char_u patbuf[NUMBUFLEN];
13835 char_u *save_cpo;
13836 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013837 listitem_T *ni;
13838 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013839 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013840 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013841 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013842
13843 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13844 save_cpo = p_cpo;
13845 p_cpo = (char_u *)"";
13846
13847 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013848 if (argvars[1].v_type != VAR_UNKNOWN)
13849 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013850 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13851 if (pat == NULL)
13852 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013853 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013854 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013855 }
13856 if (pat == NULL || *pat == NUL)
13857 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013858
13859 l = list_alloc();
13860 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013862 rettv->v_type = VAR_LIST;
13863 rettv->vval.v_list = l;
13864 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013865 if (typeerr)
13866 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867
Bram Moolenaar0d660222005-01-07 21:51:51 +000013868 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13869 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013871 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013872 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013873 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013874 if (*str == NUL)
13875 match = FALSE; /* empty item at the end */
13876 else
13877 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013878 if (match)
13879 end = regmatch.startp[0];
13880 else
13881 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013882 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13883 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013884 {
13885 ni = listitem_alloc();
13886 if (ni == NULL)
13887 break;
13888 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013889 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013890 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13891 list_append(l, ni);
13892 }
13893 if (!match)
13894 break;
13895 /* Advance to just after the match. */
13896 if (regmatch.endp[0] > str)
13897 col = 0;
13898 else
13899 {
13900 /* Don't get stuck at the same match. */
13901#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013902 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013903#else
13904 col = 1;
13905#endif
13906 }
13907 str = regmatch.endp[0];
13908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909
Bram Moolenaar0d660222005-01-07 21:51:51 +000013910 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912
Bram Moolenaar0d660222005-01-07 21:51:51 +000013913 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914}
13915
13916#ifdef HAVE_STRFTIME
13917/*
13918 * "strftime({format}[, {time}])" function
13919 */
13920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013921f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013922 typval_T *argvars;
13923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924{
13925 char_u result_buf[256];
13926 struct tm *curtime;
13927 time_t seconds;
13928 char_u *p;
13929
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013930 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013932 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013933 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934 seconds = time(NULL);
13935 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013936 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937 curtime = localtime(&seconds);
13938 /* MSVC returns NULL for an invalid value of seconds. */
13939 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013940 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 else
13942 {
13943# ifdef FEAT_MBYTE
13944 vimconv_T conv;
13945 char_u *enc;
13946
13947 conv.vc_type = CONV_NONE;
13948 enc = enc_locale();
13949 convert_setup(&conv, p_enc, enc);
13950 if (conv.vc_type != CONV_NONE)
13951 p = string_convert(&conv, p, NULL);
13952# endif
13953 if (p != NULL)
13954 (void)strftime((char *)result_buf, sizeof(result_buf),
13955 (char *)p, curtime);
13956 else
13957 result_buf[0] = NUL;
13958
13959# ifdef FEAT_MBYTE
13960 if (conv.vc_type != CONV_NONE)
13961 vim_free(p);
13962 convert_setup(&conv, enc, p_enc);
13963 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013964 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 else
13966# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013967 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968
13969# ifdef FEAT_MBYTE
13970 /* Release conversion descriptors */
13971 convert_setup(&conv, NULL, NULL);
13972 vim_free(enc);
13973# endif
13974 }
13975}
13976#endif
13977
13978/*
13979 * "stridx()" function
13980 */
13981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013982f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013983 typval_T *argvars;
13984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985{
13986 char_u buf[NUMBUFLEN];
13987 char_u *needle;
13988 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000013989 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000013991 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013993 needle = get_tv_string_chk(&argvars[1]);
13994 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000013995 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013996 if (needle == NULL || haystack == NULL)
13997 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998
Bram Moolenaar33570922005-01-25 22:26:29 +000013999 if (argvars[2].v_type != VAR_UNKNOWN)
14000 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014001 int error = FALSE;
14002
14003 start_idx = get_tv_number_chk(&argvars[2], &error);
14004 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014005 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014006 if (start_idx >= 0)
14007 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014008 }
14009
14010 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14011 if (pos != NULL)
14012 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013}
14014
14015/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014016 * "string()" function
14017 */
14018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014019f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014020 typval_T *argvars;
14021 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014022{
14023 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014024 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014026 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014027 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014028 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014029 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030}
14031
14032/*
14033 * "strlen()" function
14034 */
14035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014036f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014037 typval_T *argvars;
14038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014040 rettv->vval.v_number = (varnumber_T)(STRLEN(
14041 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042}
14043
14044/*
14045 * "strpart()" function
14046 */
14047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014048f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014049 typval_T *argvars;
14050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051{
14052 char_u *p;
14053 int n;
14054 int len;
14055 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014056 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014058 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059 slen = (int)STRLEN(p);
14060
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014061 n = get_tv_number_chk(&argvars[1], &error);
14062 if (error)
14063 len = 0;
14064 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014065 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066 else
14067 len = slen - n; /* default len: all bytes that are available. */
14068
14069 /*
14070 * Only return the overlap between the specified part and the actual
14071 * string.
14072 */
14073 if (n < 0)
14074 {
14075 len += n;
14076 n = 0;
14077 }
14078 else if (n > slen)
14079 n = slen;
14080 if (len < 0)
14081 len = 0;
14082 else if (n + len > slen)
14083 len = slen - n;
14084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014085 rettv->v_type = VAR_STRING;
14086 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087}
14088
14089/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014090 * "strridx()" function
14091 */
14092 static void
14093f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014094 typval_T *argvars;
14095 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014096{
14097 char_u buf[NUMBUFLEN];
14098 char_u *needle;
14099 char_u *haystack;
14100 char_u *rest;
14101 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014102 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014103
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014104 needle = get_tv_string_chk(&argvars[1]);
14105 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014106 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014107
14108 rettv->vval.v_number = -1;
14109 if (needle == NULL || haystack == NULL)
14110 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014111 if (argvars[2].v_type != VAR_UNKNOWN)
14112 {
14113 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014114 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014115 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014116 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014117 }
14118 else
14119 end_idx = haystack_len;
14120
Bram Moolenaar0d660222005-01-07 21:51:51 +000014121 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014122 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014123 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014124 lastmatch = haystack + end_idx;
14125 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014126 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014127 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014128 for (rest = haystack; *rest != '\0'; ++rest)
14129 {
14130 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014131 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014132 break;
14133 lastmatch = rest;
14134 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014135 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014136
14137 if (lastmatch == NULL)
14138 rettv->vval.v_number = -1;
14139 else
14140 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14141}
14142
14143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144 * "strtrans()" function
14145 */
14146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014147f_strtrans(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014151 rettv->v_type = VAR_STRING;
14152 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153}
14154
14155/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014156 * "submatch()" function
14157 */
14158 static void
14159f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014160 typval_T *argvars;
14161 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014162{
14163 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014164 rettv->vval.v_string =
14165 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014166}
14167
14168/*
14169 * "substitute()" function
14170 */
14171 static void
14172f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014173 typval_T *argvars;
14174 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014175{
14176 char_u patbuf[NUMBUFLEN];
14177 char_u subbuf[NUMBUFLEN];
14178 char_u flagsbuf[NUMBUFLEN];
14179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180 char_u *str = get_tv_string_chk(&argvars[0]);
14181 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14182 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14183 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14184
Bram Moolenaar0d660222005-01-07 21:51:51 +000014185 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014186 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14187 rettv->vval.v_string = NULL;
14188 else
14189 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014190}
14191
14192/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014193 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 */
14195/*ARGSUSED*/
14196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014197f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014198 typval_T *argvars;
14199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200{
14201 int id = 0;
14202#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014203 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014204 long col;
14205 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014206 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208 lnum = get_tv_lnum(argvars); /* -1 on type error */
14209 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14210 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014212 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014213 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014214 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215#endif
14216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014217 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218}
14219
14220/*
14221 * "synIDattr(id, what [, mode])" function
14222 */
14223/*ARGSUSED*/
14224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014225f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014226 typval_T *argvars;
14227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228{
14229 char_u *p = NULL;
14230#ifdef FEAT_SYN_HL
14231 int id;
14232 char_u *what;
14233 char_u *mode;
14234 char_u modebuf[NUMBUFLEN];
14235 int modec;
14236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014237 id = get_tv_number(&argvars[0]);
14238 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014239 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014241 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 modec = TOLOWER_ASC(mode[0]);
14243 if (modec != 't' && modec != 'c'
14244#ifdef FEAT_GUI
14245 && modec != 'g'
14246#endif
14247 )
14248 modec = 0; /* replace invalid with current */
14249 }
14250 else
14251 {
14252#ifdef FEAT_GUI
14253 if (gui.in_use)
14254 modec = 'g';
14255 else
14256#endif
14257 if (t_colors > 1)
14258 modec = 'c';
14259 else
14260 modec = 't';
14261 }
14262
14263
14264 switch (TOLOWER_ASC(what[0]))
14265 {
14266 case 'b':
14267 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14268 p = highlight_color(id, what, modec);
14269 else /* bold */
14270 p = highlight_has_attr(id, HL_BOLD, modec);
14271 break;
14272
14273 case 'f': /* fg[#] */
14274 p = highlight_color(id, what, modec);
14275 break;
14276
14277 case 'i':
14278 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14279 p = highlight_has_attr(id, HL_INVERSE, modec);
14280 else /* italic */
14281 p = highlight_has_attr(id, HL_ITALIC, modec);
14282 break;
14283
14284 case 'n': /* name */
14285 p = get_highlight_name(NULL, id - 1);
14286 break;
14287
14288 case 'r': /* reverse */
14289 p = highlight_has_attr(id, HL_INVERSE, modec);
14290 break;
14291
14292 case 's': /* standout */
14293 p = highlight_has_attr(id, HL_STANDOUT, modec);
14294 break;
14295
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014296 case 'u':
14297 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14298 /* underline */
14299 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14300 else
14301 /* undercurl */
14302 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303 break;
14304 }
14305
14306 if (p != NULL)
14307 p = vim_strsave(p);
14308#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014309 rettv->v_type = VAR_STRING;
14310 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311}
14312
14313/*
14314 * "synIDtrans(id)" function
14315 */
14316/*ARGSUSED*/
14317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014318f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014319 typval_T *argvars;
14320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321{
14322 int id;
14323
14324#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014325 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326
14327 if (id > 0)
14328 id = syn_get_final_id(id);
14329 else
14330#endif
14331 id = 0;
14332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014333 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334}
14335
14336/*
14337 * "system()" function
14338 */
14339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014340f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014341 typval_T *argvars;
14342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014344 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014346 char_u *infile = NULL;
14347 char_u buf[NUMBUFLEN];
14348 int err = FALSE;
14349 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014351 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014352 {
14353 /*
14354 * Write the string to a temp file, to be used for input of the shell
14355 * command.
14356 */
14357 if ((infile = vim_tempname('i')) == NULL)
14358 {
14359 EMSG(_(e_notmp));
14360 return;
14361 }
14362
14363 fd = mch_fopen((char *)infile, WRITEBIN);
14364 if (fd == NULL)
14365 {
14366 EMSG2(_(e_notopen), infile);
14367 goto done;
14368 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014369 p = get_tv_string_buf_chk(&argvars[1], buf);
14370 if (p == NULL)
14371 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014372 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14373 err = TRUE;
14374 if (fclose(fd) != 0)
14375 err = TRUE;
14376 if (err)
14377 {
14378 EMSG(_("E677: Error writing temp file"));
14379 goto done;
14380 }
14381 }
14382
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014383 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014384
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385#ifdef USE_CR
14386 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014387 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 {
14389 char_u *s;
14390
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014391 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 {
14393 if (*s == CAR)
14394 *s = NL;
14395 }
14396 }
14397#else
14398# ifdef USE_CRNL
14399 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014400 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401 {
14402 char_u *s, *d;
14403
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014404 d = res;
14405 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406 {
14407 if (s[0] == CAR && s[1] == NL)
14408 ++s;
14409 *d++ = *s;
14410 }
14411 *d = NUL;
14412 }
14413# endif
14414#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014415
14416done:
14417 if (infile != NULL)
14418 {
14419 mch_remove(infile);
14420 vim_free(infile);
14421 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014422 rettv->v_type = VAR_STRING;
14423 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424}
14425
14426/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014427 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014428 */
14429 static void
14430f_taglist(argvars, rettv)
14431 typval_T *argvars;
14432 typval_T *rettv;
14433{
14434 char_u *tag_pattern;
14435 list_T *l;
14436
14437 tag_pattern = get_tv_string(&argvars[0]);
14438
14439 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014440 if (*tag_pattern == NUL)
14441 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014442
14443 l = list_alloc();
14444 if (l != NULL)
14445 {
14446 if (get_tags(l, tag_pattern) != FAIL)
14447 {
14448 rettv->vval.v_list = l;
14449 rettv->v_type = VAR_LIST;
14450 ++l->lv_refcount;
14451 }
14452 else
14453 list_free(l);
14454 }
14455}
14456
14457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458 * "tempname()" function
14459 */
14460/*ARGSUSED*/
14461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014462f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014463 typval_T *argvars;
14464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465{
14466 static int x = 'A';
14467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014468 rettv->v_type = VAR_STRING;
14469 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470
14471 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14472 * names. Skip 'I' and 'O', they are used for shell redirection. */
14473 do
14474 {
14475 if (x == 'Z')
14476 x = '0';
14477 else if (x == '9')
14478 x = 'A';
14479 else
14480 {
14481#ifdef EBCDIC
14482 if (x == 'I')
14483 x = 'J';
14484 else if (x == 'R')
14485 x = 'S';
14486 else
14487#endif
14488 ++x;
14489 }
14490 } while (x == 'I' || x == 'O');
14491}
14492
14493/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000014494 * "test(list)" function: Just checking the walls...
14495 */
14496/*ARGSUSED*/
14497 static void
14498f_test(argvars, rettv)
14499 typval_T *argvars;
14500 typval_T *rettv;
14501{
14502 /* Used for unit testing. Change the code below to your liking. */
14503#if 0
14504 listitem_T *li;
14505 list_T *l;
14506 char_u *bad, *good;
14507
14508 if (argvars[0].v_type != VAR_LIST)
14509 return;
14510 l = argvars[0].vval.v_list;
14511 if (l == NULL)
14512 return;
14513 li = l->lv_first;
14514 if (li == NULL)
14515 return;
14516 bad = get_tv_string(&li->li_tv);
14517 li = li->li_next;
14518 if (li == NULL)
14519 return;
14520 good = get_tv_string(&li->li_tv);
14521 rettv->vval.v_number = test_edit_score(bad, good);
14522#endif
14523}
14524
14525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526 * "tolower(string)" function
14527 */
14528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014529f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014530 typval_T *argvars;
14531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532{
14533 char_u *p;
14534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014535 p = vim_strsave(get_tv_string(&argvars[0]));
14536 rettv->v_type = VAR_STRING;
14537 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538
14539 if (p != NULL)
14540 while (*p != NUL)
14541 {
14542#ifdef FEAT_MBYTE
14543 int l;
14544
14545 if (enc_utf8)
14546 {
14547 int c, lc;
14548
14549 c = utf_ptr2char(p);
14550 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014551 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 /* TODO: reallocate string when byte count changes. */
14553 if (utf_char2len(lc) == l)
14554 utf_char2bytes(lc, p);
14555 p += l;
14556 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014557 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558 p += l; /* skip multi-byte character */
14559 else
14560#endif
14561 {
14562 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14563 ++p;
14564 }
14565 }
14566}
14567
14568/*
14569 * "toupper(string)" function
14570 */
14571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014572f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014573 typval_T *argvars;
14574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014576 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014577 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578}
14579
14580/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014581 * "tr(string, fromstr, tostr)" function
14582 */
14583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014584f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014585 typval_T *argvars;
14586 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014587{
14588 char_u *instr;
14589 char_u *fromstr;
14590 char_u *tostr;
14591 char_u *p;
14592#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014593 int inlen;
14594 int fromlen;
14595 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014596 int idx;
14597 char_u *cpstr;
14598 int cplen;
14599 int first = TRUE;
14600#endif
14601 char_u buf[NUMBUFLEN];
14602 char_u buf2[NUMBUFLEN];
14603 garray_T ga;
14604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014605 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014606 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14607 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014608
14609 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014610 rettv->v_type = VAR_STRING;
14611 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014612 if (fromstr == NULL || tostr == NULL)
14613 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014614 ga_init2(&ga, (int)sizeof(char), 80);
14615
14616#ifdef FEAT_MBYTE
14617 if (!has_mbyte)
14618#endif
14619 /* not multi-byte: fromstr and tostr must be the same length */
14620 if (STRLEN(fromstr) != STRLEN(tostr))
14621 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014622#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014623error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014624#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014625 EMSG2(_(e_invarg2), fromstr);
14626 ga_clear(&ga);
14627 return;
14628 }
14629
14630 /* fromstr and tostr have to contain the same number of chars */
14631 while (*instr != NUL)
14632 {
14633#ifdef FEAT_MBYTE
14634 if (has_mbyte)
14635 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014636 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014637 cpstr = instr;
14638 cplen = inlen;
14639 idx = 0;
14640 for (p = fromstr; *p != NUL; p += fromlen)
14641 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014642 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014643 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14644 {
14645 for (p = tostr; *p != NUL; p += tolen)
14646 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014647 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014648 if (idx-- == 0)
14649 {
14650 cplen = tolen;
14651 cpstr = p;
14652 break;
14653 }
14654 }
14655 if (*p == NUL) /* tostr is shorter than fromstr */
14656 goto error;
14657 break;
14658 }
14659 ++idx;
14660 }
14661
14662 if (first && cpstr == instr)
14663 {
14664 /* Check that fromstr and tostr have the same number of
14665 * (multi-byte) characters. Done only once when a character
14666 * of instr doesn't appear in fromstr. */
14667 first = FALSE;
14668 for (p = tostr; *p != NUL; p += tolen)
14669 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014670 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014671 --idx;
14672 }
14673 if (idx != 0)
14674 goto error;
14675 }
14676
14677 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014678 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014679 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014680
14681 instr += inlen;
14682 }
14683 else
14684#endif
14685 {
14686 /* When not using multi-byte chars we can do it faster. */
14687 p = vim_strchr(fromstr, *instr);
14688 if (p != NULL)
14689 ga_append(&ga, tostr[p - fromstr]);
14690 else
14691 ga_append(&ga, *instr);
14692 ++instr;
14693 }
14694 }
14695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014696 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014697}
14698
14699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700 * "type(expr)" function
14701 */
14702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014703f_type(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{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014707 int n;
14708
14709 switch (argvars[0].v_type)
14710 {
14711 case VAR_NUMBER: n = 0; break;
14712 case VAR_STRING: n = 1; break;
14713 case VAR_FUNC: n = 2; break;
14714 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014715 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014716 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14717 }
14718 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719}
14720
14721/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014722 * "values(dict)" function
14723 */
14724 static void
14725f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014726 typval_T *argvars;
14727 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014728{
14729 dict_list(argvars, rettv, 1);
14730}
14731
14732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733 * "virtcol(string)" function
14734 */
14735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014736f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014737 typval_T *argvars;
14738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739{
14740 colnr_T vcol = 0;
14741 pos_T *fp;
14742
14743 fp = var2fpos(&argvars[0], FALSE);
14744 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14745 {
14746 getvvcol(curwin, fp, NULL, NULL, &vcol);
14747 ++vcol;
14748 }
14749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014750 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751}
14752
14753/*
14754 * "visualmode()" function
14755 */
14756/*ARGSUSED*/
14757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014758f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014759 typval_T *argvars;
14760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761{
14762#ifdef FEAT_VISUAL
14763 char_u str[2];
14764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014765 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014766 str[0] = curbuf->b_visual_mode_eval;
14767 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014768 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769
14770 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014771 if ((argvars[0].v_type == VAR_NUMBER
14772 && argvars[0].vval.v_number != 0)
14773 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014774 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775 curbuf->b_visual_mode_eval = NUL;
14776#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014777 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014778#endif
14779}
14780
14781/*
14782 * "winbufnr(nr)" function
14783 */
14784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014785f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014786 typval_T *argvars;
14787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788{
14789 win_T *wp;
14790
14791 wp = find_win_by_nr(&argvars[0]);
14792 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014793 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014795 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796}
14797
14798/*
14799 * "wincol()" function
14800 */
14801/*ARGSUSED*/
14802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014803f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014804 typval_T *argvars;
14805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806{
14807 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014808 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809}
14810
14811/*
14812 * "winheight(nr)" function
14813 */
14814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014815f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014816 typval_T *argvars;
14817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818{
14819 win_T *wp;
14820
14821 wp = find_win_by_nr(&argvars[0]);
14822 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014823 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014825 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826}
14827
14828/*
14829 * "winline()" function
14830 */
14831/*ARGSUSED*/
14832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014833f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014834 typval_T *argvars;
14835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836{
14837 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014838 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839}
14840
14841/*
14842 * "winnr()" function
14843 */
14844/* ARGSUSED */
14845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014846f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014847 typval_T *argvars;
14848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849{
14850 int nr = 1;
14851#ifdef FEAT_WINDOWS
14852 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014853 win_T *twin = curwin;
14854 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014856 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014858 arg = get_tv_string_chk(&argvars[0]);
14859 if (arg == NULL)
14860 nr = 0; /* type error; errmsg already given */
14861 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014862 twin = lastwin;
14863 else if (STRCMP(arg, "#") == 0)
14864 {
14865 twin = prevwin;
14866 if (prevwin == NULL)
14867 nr = 0;
14868 }
14869 else
14870 {
14871 EMSG2(_(e_invexpr2), arg);
14872 nr = 0;
14873 }
14874 }
14875
14876 if (nr > 0)
14877 for (wp = firstwin; wp != twin; wp = wp->w_next)
14878 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014880 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881}
14882
14883/*
14884 * "winrestcmd()" function
14885 */
14886/* ARGSUSED */
14887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014888f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014889 typval_T *argvars;
14890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891{
14892#ifdef FEAT_WINDOWS
14893 win_T *wp;
14894 int winnr = 1;
14895 garray_T ga;
14896 char_u buf[50];
14897
14898 ga_init2(&ga, (int)sizeof(char), 70);
14899 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14900 {
14901 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14902 ga_concat(&ga, buf);
14903# ifdef FEAT_VERTSPLIT
14904 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14905 ga_concat(&ga, buf);
14906# endif
14907 ++winnr;
14908 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014909 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014911 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014913 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014915 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916}
14917
14918/*
14919 * "winwidth(nr)" function
14920 */
14921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014922f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014923 typval_T *argvars;
14924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925{
14926 win_T *wp;
14927
14928 wp = find_win_by_nr(&argvars[0]);
14929 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014930 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931 else
14932#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014933 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014934#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014935 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936#endif
14937}
14938
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014940 * "writefile()" function
14941 */
14942 static void
14943f_writefile(argvars, rettv)
14944 typval_T *argvars;
14945 typval_T *rettv;
14946{
14947 int binary = FALSE;
14948 char_u *fname;
14949 FILE *fd;
14950 listitem_T *li;
14951 char_u *s;
14952 int ret = 0;
14953 int c;
14954
14955 if (argvars[0].v_type != VAR_LIST)
14956 {
14957 EMSG2(_(e_listarg), "writefile()");
14958 return;
14959 }
14960 if (argvars[0].vval.v_list == NULL)
14961 return;
14962
14963 if (argvars[2].v_type != VAR_UNKNOWN
14964 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
14965 binary = TRUE;
14966
14967 /* Always open the file in binary mode, library functions have a mind of
14968 * their own about CR-LF conversion. */
14969 fname = get_tv_string(&argvars[1]);
14970 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
14971 {
14972 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14973 ret = -1;
14974 }
14975 else
14976 {
14977 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
14978 li = li->li_next)
14979 {
14980 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
14981 {
14982 if (*s == '\n')
14983 c = putc(NUL, fd);
14984 else
14985 c = putc(*s, fd);
14986 if (c == EOF)
14987 {
14988 ret = -1;
14989 break;
14990 }
14991 }
14992 if (!binary || li->li_next != NULL)
14993 if (putc('\n', fd) == EOF)
14994 {
14995 ret = -1;
14996 break;
14997 }
14998 if (ret < 0)
14999 {
15000 EMSG(_(e_write));
15001 break;
15002 }
15003 }
15004 fclose(fd);
15005 }
15006
15007 rettv->vval.v_number = ret;
15008}
15009
15010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011 * Translate a String variable into a position.
15012 */
15013 static pos_T *
15014var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015015 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 int lnum; /* TRUE when $ is last line */
15017{
15018 char_u *name;
15019 static pos_T pos;
15020 pos_T *pp;
15021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015022 name = get_tv_string_chk(varp);
15023 if (name == NULL)
15024 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025 if (name[0] == '.') /* cursor */
15026 return &curwin->w_cursor;
15027 if (name[0] == '\'') /* mark */
15028 {
15029 pp = getmark(name[1], FALSE);
15030 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15031 return NULL;
15032 return pp;
15033 }
15034 if (name[0] == '$') /* last column or line */
15035 {
15036 if (lnum)
15037 {
15038 pos.lnum = curbuf->b_ml.ml_line_count;
15039 pos.col = 0;
15040 }
15041 else
15042 {
15043 pos.lnum = curwin->w_cursor.lnum;
15044 pos.col = (colnr_T)STRLEN(ml_get_curline());
15045 }
15046 return &pos;
15047 }
15048 return NULL;
15049}
15050
15051/*
15052 * Get the length of an environment variable name.
15053 * Advance "arg" to the first character after the name.
15054 * Return 0 for error.
15055 */
15056 static int
15057get_env_len(arg)
15058 char_u **arg;
15059{
15060 char_u *p;
15061 int len;
15062
15063 for (p = *arg; vim_isIDc(*p); ++p)
15064 ;
15065 if (p == *arg) /* no name found */
15066 return 0;
15067
15068 len = (int)(p - *arg);
15069 *arg = p;
15070 return len;
15071}
15072
15073/*
15074 * Get the length of the name of a function or internal variable.
15075 * "arg" is advanced to the first non-white character after the name.
15076 * Return 0 if something is wrong.
15077 */
15078 static int
15079get_id_len(arg)
15080 char_u **arg;
15081{
15082 char_u *p;
15083 int len;
15084
15085 /* Find the end of the name. */
15086 for (p = *arg; eval_isnamec(*p); ++p)
15087 ;
15088 if (p == *arg) /* no name found */
15089 return 0;
15090
15091 len = (int)(p - *arg);
15092 *arg = skipwhite(p);
15093
15094 return len;
15095}
15096
15097/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015098 * Get the length of the name of a variable or function.
15099 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015101 * Return -1 if curly braces expansion failed.
15102 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103 * If the name contains 'magic' {}'s, expand them and return the
15104 * expanded name in an allocated string via 'alias' - caller must free.
15105 */
15106 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015107get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108 char_u **arg;
15109 char_u **alias;
15110 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015111 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112{
15113 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114 char_u *p;
15115 char_u *expr_start;
15116 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117
15118 *alias = NULL; /* default to no alias */
15119
15120 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15121 && (*arg)[2] == (int)KE_SNR)
15122 {
15123 /* hard coded <SNR>, already translated */
15124 *arg += 3;
15125 return get_id_len(arg) + 3;
15126 }
15127 len = eval_fname_script(*arg);
15128 if (len > 0)
15129 {
15130 /* literal "<SID>", "s:" or "<SNR>" */
15131 *arg += len;
15132 }
15133
Bram Moolenaar071d4272004-06-13 20:20:40 +000015134 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015135 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015137 p = find_name_end(*arg, &expr_start, &expr_end,
15138 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015139 if (expr_start != NULL)
15140 {
15141 char_u *temp_string;
15142
15143 if (!evaluate)
15144 {
15145 len += (int)(p - *arg);
15146 *arg = skipwhite(p);
15147 return len;
15148 }
15149
15150 /*
15151 * Include any <SID> etc in the expanded string:
15152 * Thus the -len here.
15153 */
15154 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15155 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015156 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157 *alias = temp_string;
15158 *arg = skipwhite(p);
15159 return (int)STRLEN(temp_string);
15160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161
15162 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015163 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164 EMSG2(_(e_invexpr2), *arg);
15165
15166 return len;
15167}
15168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015169/*
15170 * Find the end of a variable or function name, taking care of magic braces.
15171 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15172 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015173 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015174 * Return a pointer to just after the name. Equal to "arg" if there is no
15175 * valid name.
15176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015178find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179 char_u *arg;
15180 char_u **expr_start;
15181 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015182 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015183{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015184 int mb_nest = 0;
15185 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186 char_u *p;
15187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015188 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015190 *expr_start = NULL;
15191 *expr_end = NULL;
15192 }
15193
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015194 /* Quick check for valid starting character. */
15195 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15196 return arg;
15197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015198 for (p = arg; *p != NUL
15199 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015200 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015201 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015202 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015203 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015204 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015205 if (*p == '\'')
15206 {
15207 /* skip over 'string' to avoid counting [ and ] inside it. */
15208 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15209 ;
15210 if (*p == NUL)
15211 break;
15212 }
15213 else if (*p == '"')
15214 {
15215 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15216 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15217 if (*p == '\\' && p[1] != NUL)
15218 ++p;
15219 if (*p == NUL)
15220 break;
15221 }
15222
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015223 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015224 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015225 if (*p == '[')
15226 ++br_nest;
15227 else if (*p == ']')
15228 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015229 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015231 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015233 if (*p == '{')
15234 {
15235 mb_nest++;
15236 if (expr_start != NULL && *expr_start == NULL)
15237 *expr_start = p;
15238 }
15239 else if (*p == '}')
15240 {
15241 mb_nest--;
15242 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15243 *expr_end = p;
15244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015246 }
15247
15248 return p;
15249}
15250
15251/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015252 * Expands out the 'magic' {}'s in a variable/function name.
15253 * Note that this can call itself recursively, to deal with
15254 * constructs like foo{bar}{baz}{bam}
15255 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15256 * "in_start" ^
15257 * "expr_start" ^
15258 * "expr_end" ^
15259 * "in_end" ^
15260 *
15261 * Returns a new allocated string, which the caller must free.
15262 * Returns NULL for failure.
15263 */
15264 static char_u *
15265make_expanded_name(in_start, expr_start, expr_end, in_end)
15266 char_u *in_start;
15267 char_u *expr_start;
15268 char_u *expr_end;
15269 char_u *in_end;
15270{
15271 char_u c1;
15272 char_u *retval = NULL;
15273 char_u *temp_result;
15274 char_u *nextcmd = NULL;
15275
15276 if (expr_end == NULL || in_end == NULL)
15277 return NULL;
15278 *expr_start = NUL;
15279 *expr_end = NUL;
15280 c1 = *in_end;
15281 *in_end = NUL;
15282
15283 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15284 if (temp_result != NULL && nextcmd == NULL)
15285 {
15286 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15287 + (in_end - expr_end) + 1));
15288 if (retval != NULL)
15289 {
15290 STRCPY(retval, in_start);
15291 STRCAT(retval, temp_result);
15292 STRCAT(retval, expr_end + 1);
15293 }
15294 }
15295 vim_free(temp_result);
15296
15297 *in_end = c1; /* put char back for error messages */
15298 *expr_start = '{';
15299 *expr_end = '}';
15300
15301 if (retval != NULL)
15302 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015303 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015304 if (expr_start != NULL)
15305 {
15306 /* Further expansion! */
15307 temp_result = make_expanded_name(retval, expr_start,
15308 expr_end, temp_result);
15309 vim_free(retval);
15310 retval = temp_result;
15311 }
15312 }
15313
15314 return retval;
15315}
15316
15317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015319 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320 */
15321 static int
15322eval_isnamec(c)
15323 int c;
15324{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015325 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15326}
15327
15328/*
15329 * Return TRUE if character "c" can be used as the first character in a
15330 * variable or function name (excluding '{' and '}').
15331 */
15332 static int
15333eval_isnamec1(c)
15334 int c;
15335{
15336 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015337}
15338
15339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340 * Set number v: variable to "val".
15341 */
15342 void
15343set_vim_var_nr(idx, val)
15344 int idx;
15345 long val;
15346{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015347 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348}
15349
15350/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015351 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 */
15353 long
15354get_vim_var_nr(idx)
15355 int idx;
15356{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015357 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358}
15359
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015360#if defined(FEAT_AUTOCMD) || defined(PROTO)
15361/*
15362 * Get string v: variable value. Uses a static buffer, can only be used once.
15363 */
15364 char_u *
15365get_vim_var_str(idx)
15366 int idx;
15367{
15368 return get_tv_string(&vimvars[idx].vv_tv);
15369}
15370#endif
15371
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372/*
15373 * Set v:count, v:count1 and v:prevcount.
15374 */
15375 void
15376set_vcount(count, count1)
15377 long count;
15378 long count1;
15379{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015380 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15381 vimvars[VV_COUNT].vv_nr = count;
15382 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383}
15384
15385/*
15386 * Set string v: variable to a copy of "val".
15387 */
15388 void
15389set_vim_var_string(idx, val, len)
15390 int idx;
15391 char_u *val;
15392 int len; /* length of "val" to use or -1 (whole string) */
15393{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015394 /* Need to do this (at least) once, since we can't initialize a union.
15395 * Will always be invoked when "v:progname" is set. */
15396 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15397
Bram Moolenaare9a41262005-01-15 22:18:47 +000015398 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015400 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015402 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015404 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405}
15406
15407/*
15408 * Set v:register if needed.
15409 */
15410 void
15411set_reg_var(c)
15412 int c;
15413{
15414 char_u regname;
15415
15416 if (c == 0 || c == ' ')
15417 regname = '"';
15418 else
15419 regname = c;
15420 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015421 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422 set_vim_var_string(VV_REG, &regname, 1);
15423}
15424
15425/*
15426 * Get or set v:exception. If "oldval" == NULL, return the current value.
15427 * Otherwise, restore the value to "oldval" and return NULL.
15428 * Must always be called in pairs to save and restore v:exception! Does not
15429 * take care of memory allocations.
15430 */
15431 char_u *
15432v_exception(oldval)
15433 char_u *oldval;
15434{
15435 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015436 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437
Bram Moolenaare9a41262005-01-15 22:18:47 +000015438 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 return NULL;
15440}
15441
15442/*
15443 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15444 * Otherwise, restore the value to "oldval" and return NULL.
15445 * Must always be called in pairs to save and restore v:throwpoint! Does not
15446 * take care of memory allocations.
15447 */
15448 char_u *
15449v_throwpoint(oldval)
15450 char_u *oldval;
15451{
15452 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015453 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454
Bram Moolenaare9a41262005-01-15 22:18:47 +000015455 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015456 return NULL;
15457}
15458
15459#if defined(FEAT_AUTOCMD) || defined(PROTO)
15460/*
15461 * Set v:cmdarg.
15462 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15463 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15464 * Must always be called in pairs!
15465 */
15466 char_u *
15467set_cmdarg(eap, oldarg)
15468 exarg_T *eap;
15469 char_u *oldarg;
15470{
15471 char_u *oldval;
15472 char_u *newval;
15473 unsigned len;
15474
Bram Moolenaare9a41262005-01-15 22:18:47 +000015475 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015476 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015478 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015479 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015480 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481 }
15482
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015483 if (eap->force_bin == FORCE_BIN)
15484 len = 6;
15485 else if (eap->force_bin == FORCE_NOBIN)
15486 len = 8;
15487 else
15488 len = 0;
15489 if (eap->force_ff != 0)
15490 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15491# ifdef FEAT_MBYTE
15492 if (eap->force_enc != 0)
15493 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15494# endif
15495
15496 newval = alloc(len + 1);
15497 if (newval == NULL)
15498 return NULL;
15499
15500 if (eap->force_bin == FORCE_BIN)
15501 sprintf((char *)newval, " ++bin");
15502 else if (eap->force_bin == FORCE_NOBIN)
15503 sprintf((char *)newval, " ++nobin");
15504 else
15505 *newval = NUL;
15506 if (eap->force_ff != 0)
15507 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15508 eap->cmd + eap->force_ff);
15509# ifdef FEAT_MBYTE
15510 if (eap->force_enc != 0)
15511 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15512 eap->cmd + eap->force_enc);
15513# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015514 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015515 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516}
15517#endif
15518
15519/*
15520 * Get the value of internal variable "name".
15521 * Return OK or FAIL.
15522 */
15523 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015524get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 char_u *name;
15526 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015527 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015528 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529{
15530 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015531 typval_T *tv = NULL;
15532 typval_T atv;
15533 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535
15536 /* truncate the name, so that we can use strcmp() */
15537 cc = name[len];
15538 name[len] = NUL;
15539
15540 /*
15541 * Check for "b:changedtick".
15542 */
15543 if (STRCMP(name, "b:changedtick") == 0)
15544 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015545 atv.v_type = VAR_NUMBER;
15546 atv.vval.v_number = curbuf->b_changedtick;
15547 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548 }
15549
15550 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551 * Check for user-defined variables.
15552 */
15553 else
15554 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015555 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015557 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 }
15559
Bram Moolenaare9a41262005-01-15 22:18:47 +000015560 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015562 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015563 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564 ret = FAIL;
15565 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015566 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015567 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568
15569 name[len] = cc;
15570
15571 return ret;
15572}
15573
15574/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015575 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15576 * Also handle function call with Funcref variable: func(expr)
15577 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15578 */
15579 static int
15580handle_subscript(arg, rettv, evaluate, verbose)
15581 char_u **arg;
15582 typval_T *rettv;
15583 int evaluate; /* do more than finding the end */
15584 int verbose; /* give error messages */
15585{
15586 int ret = OK;
15587 dict_T *selfdict = NULL;
15588 char_u *s;
15589 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015590 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015591
15592 while (ret == OK
15593 && (**arg == '['
15594 || (**arg == '.' && rettv->v_type == VAR_DICT)
15595 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15596 && !vim_iswhite(*(*arg - 1)))
15597 {
15598 if (**arg == '(')
15599 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015600 /* need to copy the funcref so that we can clear rettv */
15601 functv = *rettv;
15602 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015603
15604 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015605 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015606 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015607 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15608 &len, evaluate, selfdict);
15609
15610 /* Clear the funcref afterwards, so that deleting it while
15611 * evaluating the arguments is possible (see test55). */
15612 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015613
15614 /* Stop the expression evaluation when immediately aborting on
15615 * error, or when an interrupt occurred or an exception was thrown
15616 * but not caught. */
15617 if (aborting())
15618 {
15619 if (ret == OK)
15620 clear_tv(rettv);
15621 ret = FAIL;
15622 }
15623 dict_unref(selfdict);
15624 selfdict = NULL;
15625 }
15626 else /* **arg == '[' || **arg == '.' */
15627 {
15628 dict_unref(selfdict);
15629 if (rettv->v_type == VAR_DICT)
15630 {
15631 selfdict = rettv->vval.v_dict;
15632 if (selfdict != NULL)
15633 ++selfdict->dv_refcount;
15634 }
15635 else
15636 selfdict = NULL;
15637 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15638 {
15639 clear_tv(rettv);
15640 ret = FAIL;
15641 }
15642 }
15643 }
15644 dict_unref(selfdict);
15645 return ret;
15646}
15647
15648/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015649 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15650 * value).
15651 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015652 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015653alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015654{
Bram Moolenaar33570922005-01-25 22:26:29 +000015655 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015656}
15657
15658/*
15659 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660 * The string "s" must have been allocated, it is consumed.
15661 * Return NULL for out of memory, the variable otherwise.
15662 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015663 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015664alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 char_u *s;
15666{
Bram Moolenaar33570922005-01-25 22:26:29 +000015667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015669 rettv = alloc_tv();
15670 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015672 rettv->v_type = VAR_STRING;
15673 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674 }
15675 else
15676 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015677 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678}
15679
15680/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015681 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682 */
15683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015684free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015685 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015686{
15687 if (varp != NULL)
15688 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015689 switch (varp->v_type)
15690 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015691 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015692 func_unref(varp->vval.v_string);
15693 /*FALLTHROUGH*/
15694 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015695 vim_free(varp->vval.v_string);
15696 break;
15697 case VAR_LIST:
15698 list_unref(varp->vval.v_list);
15699 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015700 case VAR_DICT:
15701 dict_unref(varp->vval.v_dict);
15702 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015703 case VAR_NUMBER:
15704 case VAR_UNKNOWN:
15705 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015706 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015707 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015708 break;
15709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710 vim_free(varp);
15711 }
15712}
15713
15714/*
15715 * Free the memory for a variable value and set the value to NULL or 0.
15716 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015717 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015718clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015719 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720{
15721 if (varp != NULL)
15722 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015723 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015725 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015726 func_unref(varp->vval.v_string);
15727 /*FALLTHROUGH*/
15728 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015729 vim_free(varp->vval.v_string);
15730 varp->vval.v_string = NULL;
15731 break;
15732 case VAR_LIST:
15733 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015734 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015735 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015736 case VAR_DICT:
15737 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015738 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015739 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015740 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015741 varp->vval.v_number = 0;
15742 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015743 case VAR_UNKNOWN:
15744 break;
15745 default:
15746 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015748 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749 }
15750}
15751
15752/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015753 * Set the value of a variable to NULL without freeing items.
15754 */
15755 static void
15756init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015757 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015758{
15759 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015760 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015761}
15762
15763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764 * Get the number value of a variable.
15765 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015766 * For incompatible types, return 0.
15767 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15768 * caller of incompatible types: it sets *denote to TRUE if "denote"
15769 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 */
15771 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015772get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015773 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015775 int error = FALSE;
15776
15777 return get_tv_number_chk(varp, &error); /* return 0L on error */
15778}
15779
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015780 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015781get_tv_number_chk(varp, denote)
15782 typval_T *varp;
15783 int *denote;
15784{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015785 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015787 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015789 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015790 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015791 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015792 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015793 break;
15794 case VAR_STRING:
15795 if (varp->vval.v_string != NULL)
15796 vim_str2nr(varp->vval.v_string, NULL, NULL,
15797 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015798 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015799 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015800 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015801 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015802 case VAR_DICT:
15803 EMSG(_("E728: Using a Dictionary as a number"));
15804 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015805 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015806 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015807 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015808 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015809 if (denote == NULL) /* useful for values that must be unsigned */
15810 n = -1;
15811 else
15812 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015813 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814}
15815
15816/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015817 * Get the lnum from the first argument.
15818 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015819 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015820 */
15821 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015822get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015823 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824{
Bram Moolenaar33570922005-01-25 22:26:29 +000015825 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826 linenr_T lnum;
15827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015828 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829 if (lnum == 0) /* no valid number, try using line() */
15830 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015831 rettv.v_type = VAR_NUMBER;
15832 f_line(argvars, &rettv);
15833 lnum = rettv.vval.v_number;
15834 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835 }
15836 return lnum;
15837}
15838
15839/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015840 * Get the lnum from the first argument.
15841 * Also accepts "$", then "buf" is used.
15842 * Returns 0 on error.
15843 */
15844 static linenr_T
15845get_tv_lnum_buf(argvars, buf)
15846 typval_T *argvars;
15847 buf_T *buf;
15848{
15849 if (argvars[0].v_type == VAR_STRING
15850 && argvars[0].vval.v_string != NULL
15851 && argvars[0].vval.v_string[0] == '$'
15852 && buf != NULL)
15853 return buf->b_ml.ml_line_count;
15854 return get_tv_number_chk(&argvars[0], NULL);
15855}
15856
15857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858 * Get the string value of a variable.
15859 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015860 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15861 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862 * If the String variable has never been set, return an empty string.
15863 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015864 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15865 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866 */
15867 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015868get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015869 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870{
15871 static char_u mybuf[NUMBUFLEN];
15872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015873 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874}
15875
15876 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015877get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015878 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015879 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015881 char_u *res = get_tv_string_buf_chk(varp, buf);
15882
15883 return res != NULL ? res : (char_u *)"";
15884}
15885
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015886 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015887get_tv_string_chk(varp)
15888 typval_T *varp;
15889{
15890 static char_u mybuf[NUMBUFLEN];
15891
15892 return get_tv_string_buf_chk(varp, mybuf);
15893}
15894
15895 static char_u *
15896get_tv_string_buf_chk(varp, buf)
15897 typval_T *varp;
15898 char_u *buf;
15899{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015900 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015902 case VAR_NUMBER:
15903 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15904 return buf;
15905 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015906 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015907 break;
15908 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015909 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015910 break;
15911 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015912 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015913 break;
15914 case VAR_STRING:
15915 if (varp->vval.v_string != NULL)
15916 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015917 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015918 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015919 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015920 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015921 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015922 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015923}
15924
15925/*
15926 * Find variable "name" in the list of variables.
15927 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015928 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015929 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015930 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015932 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015933find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015934 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015935 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015936{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015937 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015938 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939
Bram Moolenaara7043832005-01-21 11:56:39 +000015940 ht = find_var_ht(name, &varname);
15941 if (htp != NULL)
15942 *htp = ht;
15943 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015944 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015945 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015946}
15947
15948/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015949 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000015950 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015952 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015953find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000015954 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000015955 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015956 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000015957{
Bram Moolenaar33570922005-01-25 22:26:29 +000015958 hashitem_T *hi;
15959
15960 if (*varname == NUL)
15961 {
15962 /* Must be something like "s:", otherwise "ht" would be NULL. */
15963 switch (varname[-2])
15964 {
15965 case 's': return &SCRIPT_SV(current_SID).sv_var;
15966 case 'g': return &globvars_var;
15967 case 'v': return &vimvars_var;
15968 case 'b': return &curbuf->b_bufvar;
15969 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015970 case 'l': return current_funccal == NULL
15971 ? NULL : &current_funccal->l_vars_var;
15972 case 'a': return current_funccal == NULL
15973 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000015974 }
15975 return NULL;
15976 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015977
15978 hi = hash_find(ht, varname);
15979 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015980 {
15981 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015982 * worked find the variable again. Don't auto-load a script if it was
15983 * loaded already, otherwise it would be loaded every time when
15984 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015985 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015986 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015987 hi = hash_find(ht, varname);
15988 if (HASHITEM_EMPTY(hi))
15989 return NULL;
15990 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015991 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015992}
15993
15994/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015995 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015996 * Set "varname" to the start of name without ':'.
15997 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015998 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015999find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000 char_u *name;
16001 char_u **varname;
16002{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016003 hashitem_T *hi;
16004
Bram Moolenaar071d4272004-06-13 20:20:40 +000016005 if (name[1] != ':')
16006 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016007 /* The name must not start with a colon or #. */
16008 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016009 return NULL;
16010 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016011
16012 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016013 hi = hash_find(&compat_hashtab, name);
16014 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016015 return &compat_hashtab;
16016
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016018 return &globvarht; /* global variable */
16019 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020 }
16021 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016022 if (*name == 'g') /* global variable */
16023 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016024 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16025 */
16026 if (vim_strchr(name + 2, ':') != NULL
16027 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016028 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016030 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016032 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016033 if (*name == 'v') /* v: variable */
16034 return &vimvarht;
16035 if (*name == 'a' && current_funccal != NULL) /* function argument */
16036 return &current_funccal->l_avars.dv_hashtab;
16037 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16038 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039 if (*name == 's' /* script variable */
16040 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16041 return &SCRIPT_VARS(current_SID);
16042 return NULL;
16043}
16044
16045/*
16046 * Get the string value of a (global/local) variable.
16047 * Returns NULL when it doesn't exist.
16048 */
16049 char_u *
16050get_var_value(name)
16051 char_u *name;
16052{
Bram Moolenaar33570922005-01-25 22:26:29 +000016053 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054
Bram Moolenaara7043832005-01-21 11:56:39 +000016055 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056 if (v == NULL)
16057 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016058 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059}
16060
16061/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016062 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063 * sourcing this script and when executing functions defined in the script.
16064 */
16065 void
16066new_script_vars(id)
16067 scid_T id;
16068{
Bram Moolenaara7043832005-01-21 11:56:39 +000016069 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016070 hashtab_T *ht;
16071 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016072
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16074 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016075 /* Re-allocating ga_data means that an ht_array pointing to
16076 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016077 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016078 for (i = 1; i <= ga_scripts.ga_len; ++i)
16079 {
16080 ht = &SCRIPT_VARS(i);
16081 if (ht->ht_mask == HT_INIT_SIZE - 1)
16082 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016083 sv = &SCRIPT_SV(i);
16084 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016085 }
16086
Bram Moolenaar071d4272004-06-13 20:20:40 +000016087 while (ga_scripts.ga_len < id)
16088 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016089 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16090 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016092 }
16093 }
16094}
16095
16096/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016097 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16098 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099 */
16100 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016101init_var_dict(dict, dict_var)
16102 dict_T *dict;
16103 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104{
Bram Moolenaar33570922005-01-25 22:26:29 +000016105 hash_init(&dict->dv_hashtab);
16106 dict->dv_refcount = 99999;
16107 dict_var->di_tv.vval.v_dict = dict;
16108 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016109 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016110 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16111 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112}
16113
16114/*
16115 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016116 * Frees all allocated variables and the value they contain.
16117 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118 */
16119 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016120vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016121 hashtab_T *ht;
16122{
16123 vars_clear_ext(ht, TRUE);
16124}
16125
16126/*
16127 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16128 */
16129 static void
16130vars_clear_ext(ht, free_val)
16131 hashtab_T *ht;
16132 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133{
Bram Moolenaara7043832005-01-21 11:56:39 +000016134 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016135 hashitem_T *hi;
16136 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137
Bram Moolenaar33570922005-01-25 22:26:29 +000016138 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016139 todo = ht->ht_used;
16140 for (hi = ht->ht_array; todo > 0; ++hi)
16141 {
16142 if (!HASHITEM_EMPTY(hi))
16143 {
16144 --todo;
16145
Bram Moolenaar33570922005-01-25 22:26:29 +000016146 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016147 * ht_array might change then. hash_clear() takes care of it
16148 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016149 v = HI2DI(hi);
16150 if (free_val)
16151 clear_tv(&v->di_tv);
16152 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16153 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016154 }
16155 }
16156 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016157 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158}
16159
Bram Moolenaara7043832005-01-21 11:56:39 +000016160/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016161 * Delete a variable from hashtab "ht" at item "hi".
16162 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016165delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016166 hashtab_T *ht;
16167 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168{
Bram Moolenaar33570922005-01-25 22:26:29 +000016169 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016170
16171 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016172 clear_tv(&di->di_tv);
16173 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174}
16175
16176/*
16177 * List the value of one internal variable.
16178 */
16179 static void
16180list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016181 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182 char_u *prefix;
16183{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016184 char_u *tofree;
16185 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016186 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016187
Bram Moolenaar33570922005-01-25 22:26:29 +000016188 s = echo_string(&v->di_tv, &tofree, numbuf);
16189 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016190 s == NULL ? (char_u *)"" : s);
16191 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192}
16193
Bram Moolenaar071d4272004-06-13 20:20:40 +000016194 static void
16195list_one_var_a(prefix, name, type, string)
16196 char_u *prefix;
16197 char_u *name;
16198 int type;
16199 char_u *string;
16200{
16201 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16202 if (name != NULL) /* "a:" vars don't have a name stored */
16203 msg_puts(name);
16204 msg_putchar(' ');
16205 msg_advance(22);
16206 if (type == VAR_NUMBER)
16207 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016208 else if (type == VAR_FUNC)
16209 msg_putchar('*');
16210 else if (type == VAR_LIST)
16211 {
16212 msg_putchar('[');
16213 if (*string == '[')
16214 ++string;
16215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016216 else if (type == VAR_DICT)
16217 {
16218 msg_putchar('{');
16219 if (*string == '{')
16220 ++string;
16221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 else
16223 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016224
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016226
16227 if (type == VAR_FUNC)
16228 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229}
16230
16231/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016232 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 * If the variable already exists, the value is updated.
16234 * Otherwise the variable is created.
16235 */
16236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016237set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016239 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016240 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241{
Bram Moolenaar33570922005-01-25 22:26:29 +000016242 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016244 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016245 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016247 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016248 {
16249 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16250 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16251 ? name[2] : name[0]))
16252 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016253 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016254 return;
16255 }
16256 if (function_exists(name))
16257 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016258 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016259 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016260 return;
16261 }
16262 }
16263
Bram Moolenaara7043832005-01-21 11:56:39 +000016264 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016265 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016266 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016267 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016268 return;
16269 }
16270
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016271 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016272 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016274 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016275 if (var_check_ro(v->di_flags, name)
16276 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016277 return;
16278 if (v->di_tv.v_type != tv->v_type
16279 && !((v->di_tv.v_type == VAR_STRING
16280 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016281 && (tv->v_type == VAR_STRING
16282 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016283 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016284 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016285 return;
16286 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016287
16288 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016289 * Handle setting internal v: variables separately: we don't change
16290 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016291 */
16292 if (ht == &vimvarht)
16293 {
16294 if (v->di_tv.v_type == VAR_STRING)
16295 {
16296 vim_free(v->di_tv.vval.v_string);
16297 if (copy || tv->v_type != VAR_STRING)
16298 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16299 else
16300 {
16301 /* Take over the string to avoid an extra alloc/free. */
16302 v->di_tv.vval.v_string = tv->vval.v_string;
16303 tv->vval.v_string = NULL;
16304 }
16305 }
16306 else if (v->di_tv.v_type != VAR_NUMBER)
16307 EMSG2(_(e_intern2), "set_var()");
16308 else
16309 v->di_tv.vval.v_number = get_tv_number(tv);
16310 return;
16311 }
16312
16313 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314 }
16315 else /* add a new variable */
16316 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016317 /* Make sure the variable name is valid. */
16318 for (p = varname; *p != NUL; ++p)
16319 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16320 {
16321 EMSG2(_(e_illvar), varname);
16322 return;
16323 }
16324
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016325 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16326 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016327 if (v == NULL)
16328 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016329 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016330 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016332 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333 return;
16334 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016335 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016338 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016339 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016340 else
16341 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016342 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016343 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016344 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016346}
16347
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016348/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016349 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16350 * Also give an error message.
16351 */
16352 static int
16353var_check_ro(flags, name)
16354 int flags;
16355 char_u *name;
16356{
16357 if (flags & DI_FLAGS_RO)
16358 {
16359 EMSG2(_(e_readonlyvar), name);
16360 return TRUE;
16361 }
16362 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16363 {
16364 EMSG2(_(e_readonlysbx), name);
16365 return TRUE;
16366 }
16367 return FALSE;
16368}
16369
16370/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016371 * Return TRUE if typeval "tv" is set to be locked (immutable).
16372 * Also give an error message, using "name".
16373 */
16374 static int
16375tv_check_lock(lock, name)
16376 int lock;
16377 char_u *name;
16378{
16379 if (lock & VAR_LOCKED)
16380 {
16381 EMSG2(_("E741: Value is locked: %s"),
16382 name == NULL ? (char_u *)_("Unknown") : name);
16383 return TRUE;
16384 }
16385 if (lock & VAR_FIXED)
16386 {
16387 EMSG2(_("E742: Cannot change value of %s"),
16388 name == NULL ? (char_u *)_("Unknown") : name);
16389 return TRUE;
16390 }
16391 return FALSE;
16392}
16393
16394/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016395 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016396 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016397 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016400copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016401 typval_T *from;
16402 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016404 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016405 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016406 switch (from->v_type)
16407 {
16408 case VAR_NUMBER:
16409 to->vval.v_number = from->vval.v_number;
16410 break;
16411 case VAR_STRING:
16412 case VAR_FUNC:
16413 if (from->vval.v_string == NULL)
16414 to->vval.v_string = NULL;
16415 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016416 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016417 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016418 if (from->v_type == VAR_FUNC)
16419 func_ref(to->vval.v_string);
16420 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016421 break;
16422 case VAR_LIST:
16423 if (from->vval.v_list == NULL)
16424 to->vval.v_list = NULL;
16425 else
16426 {
16427 to->vval.v_list = from->vval.v_list;
16428 ++to->vval.v_list->lv_refcount;
16429 }
16430 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016431 case VAR_DICT:
16432 if (from->vval.v_dict == NULL)
16433 to->vval.v_dict = NULL;
16434 else
16435 {
16436 to->vval.v_dict = from->vval.v_dict;
16437 ++to->vval.v_dict->dv_refcount;
16438 }
16439 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016440 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016441 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016442 break;
16443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444}
16445
16446/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016447 * Make a copy of an item.
16448 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016449 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16450 * reference to an already copied list/dict can be used.
16451 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016452 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016453 static int
16454item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016455 typval_T *from;
16456 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016457 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016458 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016459{
16460 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016461 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016462
Bram Moolenaar33570922005-01-25 22:26:29 +000016463 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016464 {
16465 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016466 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016467 }
16468 ++recurse;
16469
16470 switch (from->v_type)
16471 {
16472 case VAR_NUMBER:
16473 case VAR_STRING:
16474 case VAR_FUNC:
16475 copy_tv(from, to);
16476 break;
16477 case VAR_LIST:
16478 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016479 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016480 if (from->vval.v_list == NULL)
16481 to->vval.v_list = NULL;
16482 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16483 {
16484 /* use the copy made earlier */
16485 to->vval.v_list = from->vval.v_list->lv_copylist;
16486 ++to->vval.v_list->lv_refcount;
16487 }
16488 else
16489 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16490 if (to->vval.v_list == NULL)
16491 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016492 break;
16493 case VAR_DICT:
16494 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016495 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016496 if (from->vval.v_dict == NULL)
16497 to->vval.v_dict = NULL;
16498 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16499 {
16500 /* use the copy made earlier */
16501 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16502 ++to->vval.v_dict->dv_refcount;
16503 }
16504 else
16505 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16506 if (to->vval.v_dict == NULL)
16507 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016508 break;
16509 default:
16510 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016511 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016512 }
16513 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016514 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016515}
16516
16517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518 * ":echo expr1 ..." print each argument separated with a space, add a
16519 * newline at the end.
16520 * ":echon expr1 ..." print each argument plain.
16521 */
16522 void
16523ex_echo(eap)
16524 exarg_T *eap;
16525{
16526 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016527 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016528 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 char_u *p;
16530 int needclr = TRUE;
16531 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016532 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533
16534 if (eap->skip)
16535 ++emsg_skip;
16536 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16537 {
16538 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016539 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540 {
16541 /*
16542 * Report the invalid expression unless the expression evaluation
16543 * has been cancelled due to an aborting error, an interrupt, or an
16544 * exception.
16545 */
16546 if (!aborting())
16547 EMSG2(_(e_invexpr2), p);
16548 break;
16549 }
16550 if (!eap->skip)
16551 {
16552 if (atstart)
16553 {
16554 atstart = FALSE;
16555 /* Call msg_start() after eval1(), evaluating the expression
16556 * may cause a message to appear. */
16557 if (eap->cmdidx == CMD_echo)
16558 msg_start();
16559 }
16560 else if (eap->cmdidx == CMD_echo)
16561 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016562 p = echo_string(&rettv, &tofree, numbuf);
16563 if (p != NULL)
16564 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016566 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016568 if (*p != TAB && needclr)
16569 {
16570 /* remove any text still there from the command */
16571 msg_clr_eos();
16572 needclr = FALSE;
16573 }
16574 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 }
16576 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016577 {
16578#ifdef FEAT_MBYTE
16579 if (has_mbyte)
16580 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016581 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016582
16583 (void)msg_outtrans_len_attr(p, i, echo_attr);
16584 p += i - 1;
16585 }
16586 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016588 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016591 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016593 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594 arg = skipwhite(arg);
16595 }
16596 eap->nextcmd = check_nextcmd(arg);
16597
16598 if (eap->skip)
16599 --emsg_skip;
16600 else
16601 {
16602 /* remove text that may still be there from the command */
16603 if (needclr)
16604 msg_clr_eos();
16605 if (eap->cmdidx == CMD_echo)
16606 msg_end();
16607 }
16608}
16609
16610/*
16611 * ":echohl {name}".
16612 */
16613 void
16614ex_echohl(eap)
16615 exarg_T *eap;
16616{
16617 int id;
16618
16619 id = syn_name2id(eap->arg);
16620 if (id == 0)
16621 echo_attr = 0;
16622 else
16623 echo_attr = syn_id2attr(id);
16624}
16625
16626/*
16627 * ":execute expr1 ..." execute the result of an expression.
16628 * ":echomsg expr1 ..." Print a message
16629 * ":echoerr expr1 ..." Print an error
16630 * Each gets spaces around each argument and a newline at the end for
16631 * echo commands
16632 */
16633 void
16634ex_execute(eap)
16635 exarg_T *eap;
16636{
16637 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016638 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639 int ret = OK;
16640 char_u *p;
16641 garray_T ga;
16642 int len;
16643 int save_did_emsg;
16644
16645 ga_init2(&ga, 1, 80);
16646
16647 if (eap->skip)
16648 ++emsg_skip;
16649 while (*arg != NUL && *arg != '|' && *arg != '\n')
16650 {
16651 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016652 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653 {
16654 /*
16655 * Report the invalid expression unless the expression evaluation
16656 * has been cancelled due to an aborting error, an interrupt, or an
16657 * exception.
16658 */
16659 if (!aborting())
16660 EMSG2(_(e_invexpr2), p);
16661 ret = FAIL;
16662 break;
16663 }
16664
16665 if (!eap->skip)
16666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016667 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668 len = (int)STRLEN(p);
16669 if (ga_grow(&ga, len + 2) == FAIL)
16670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016671 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672 ret = FAIL;
16673 break;
16674 }
16675 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678 ga.ga_len += len;
16679 }
16680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016681 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682 arg = skipwhite(arg);
16683 }
16684
16685 if (ret != FAIL && ga.ga_data != NULL)
16686 {
16687 if (eap->cmdidx == CMD_echomsg)
16688 MSG_ATTR(ga.ga_data, echo_attr);
16689 else if (eap->cmdidx == CMD_echoerr)
16690 {
16691 /* We don't want to abort following commands, restore did_emsg. */
16692 save_did_emsg = did_emsg;
16693 EMSG((char_u *)ga.ga_data);
16694 if (!force_abort)
16695 did_emsg = save_did_emsg;
16696 }
16697 else if (eap->cmdidx == CMD_execute)
16698 do_cmdline((char_u *)ga.ga_data,
16699 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16700 }
16701
16702 ga_clear(&ga);
16703
16704 if (eap->skip)
16705 --emsg_skip;
16706
16707 eap->nextcmd = check_nextcmd(arg);
16708}
16709
16710/*
16711 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16712 * "arg" points to the "&" or '+' when called, to "option" when returning.
16713 * Returns NULL when no option name found. Otherwise pointer to the char
16714 * after the option name.
16715 */
16716 static char_u *
16717find_option_end(arg, opt_flags)
16718 char_u **arg;
16719 int *opt_flags;
16720{
16721 char_u *p = *arg;
16722
16723 ++p;
16724 if (*p == 'g' && p[1] == ':')
16725 {
16726 *opt_flags = OPT_GLOBAL;
16727 p += 2;
16728 }
16729 else if (*p == 'l' && p[1] == ':')
16730 {
16731 *opt_flags = OPT_LOCAL;
16732 p += 2;
16733 }
16734 else
16735 *opt_flags = 0;
16736
16737 if (!ASCII_ISALPHA(*p))
16738 return NULL;
16739 *arg = p;
16740
16741 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16742 p += 4; /* termcap option */
16743 else
16744 while (ASCII_ISALPHA(*p))
16745 ++p;
16746 return p;
16747}
16748
16749/*
16750 * ":function"
16751 */
16752 void
16753ex_function(eap)
16754 exarg_T *eap;
16755{
16756 char_u *theline;
16757 int j;
16758 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 char_u *name = NULL;
16761 char_u *p;
16762 char_u *arg;
16763 garray_T newargs;
16764 garray_T newlines;
16765 int varargs = FALSE;
16766 int mustend = FALSE;
16767 int flags = 0;
16768 ufunc_T *fp;
16769 int indent;
16770 int nesting;
16771 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016772 dictitem_T *v;
16773 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016774 static int func_nr = 0; /* number for nameless function */
16775 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016776 hashtab_T *ht;
16777 int todo;
16778 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779
16780 /*
16781 * ":function" without argument: list functions.
16782 */
16783 if (ends_excmd(*eap->arg))
16784 {
16785 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016786 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016787 todo = func_hashtab.ht_used;
16788 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016789 {
16790 if (!HASHITEM_EMPTY(hi))
16791 {
16792 --todo;
16793 fp = HI2UF(hi);
16794 if (!isdigit(*fp->uf_name))
16795 list_func_head(fp, FALSE);
16796 }
16797 }
16798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 eap->nextcmd = check_nextcmd(eap->arg);
16800 return;
16801 }
16802
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016803 /*
16804 * Get the function name. There are these situations:
16805 * func normal function name
16806 * "name" == func, "fudi.fd_dict" == NULL
16807 * dict.func new dictionary entry
16808 * "name" == NULL, "fudi.fd_dict" set,
16809 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16810 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016811 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016812 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16813 * dict.func existing dict entry that's not a Funcref
16814 * "name" == NULL, "fudi.fd_dict" set,
16815 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016818 name = trans_function_name(&p, eap->skip, 0, &fudi);
16819 paren = (vim_strchr(p, '(') != NULL);
16820 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821 {
16822 /*
16823 * Return on an invalid expression in braces, unless the expression
16824 * evaluation has been cancelled due to an aborting error, an
16825 * interrupt, or an exception.
16826 */
16827 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016828 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016829 if (!eap->skip && fudi.fd_newkey != NULL)
16830 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016831 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834 else
16835 eap->skip = TRUE;
16836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016837 /* An error in a function call during evaluation of an expression in magic
16838 * braces should not cause the function not to be defined. */
16839 saved_did_emsg = did_emsg;
16840 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841
16842 /*
16843 * ":function func" with only function name: list function.
16844 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016845 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846 {
16847 if (!ends_excmd(*skipwhite(p)))
16848 {
16849 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016850 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016851 }
16852 eap->nextcmd = check_nextcmd(p);
16853 if (eap->nextcmd != NULL)
16854 *p = NUL;
16855 if (!eap->skip && !got_int)
16856 {
16857 fp = find_func(name);
16858 if (fp != NULL)
16859 {
16860 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016861 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862 {
16863 msg_putchar('\n');
16864 msg_outnum((long)(j + 1));
16865 if (j < 9)
16866 msg_putchar(' ');
16867 if (j < 99)
16868 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016869 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870 out_flush(); /* show a line at a time */
16871 ui_breakcheck();
16872 }
16873 if (!got_int)
16874 {
16875 msg_putchar('\n');
16876 msg_puts((char_u *)" endfunction");
16877 }
16878 }
16879 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016880 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016882 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883 }
16884
16885 /*
16886 * ":function name(arg1, arg2)" Define function.
16887 */
16888 p = skipwhite(p);
16889 if (*p != '(')
16890 {
16891 if (!eap->skip)
16892 {
16893 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016894 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016895 }
16896 /* attempt to continue by skipping some text */
16897 if (vim_strchr(p, '(') != NULL)
16898 p = vim_strchr(p, '(');
16899 }
16900 p = skipwhite(p + 1);
16901
16902 ga_init2(&newargs, (int)sizeof(char_u *), 3);
16903 ga_init2(&newlines, (int)sizeof(char_u *), 3);
16904
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016905 if (!eap->skip)
16906 {
16907 /* Check the name of the function. */
16908 if (name != NULL)
16909 arg = name;
16910 else
16911 arg = fudi.fd_newkey;
16912 if (arg != NULL)
16913 {
16914 if (*arg == K_SPECIAL)
16915 j = 3;
16916 else
16917 j = 0;
16918 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
16919 : eval_isnamec(arg[j])))
16920 ++j;
16921 if (arg[j] != NUL)
16922 emsg_funcname(_(e_invarg2), arg);
16923 }
16924 }
16925
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926 /*
16927 * Isolate the arguments: "arg1, arg2, ...)"
16928 */
16929 while (*p != ')')
16930 {
16931 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
16932 {
16933 varargs = TRUE;
16934 p += 3;
16935 mustend = TRUE;
16936 }
16937 else
16938 {
16939 arg = p;
16940 while (ASCII_ISALNUM(*p) || *p == '_')
16941 ++p;
16942 if (arg == p || isdigit(*arg)
16943 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
16944 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
16945 {
16946 if (!eap->skip)
16947 EMSG2(_("E125: Illegal argument: %s"), arg);
16948 break;
16949 }
16950 if (ga_grow(&newargs, 1) == FAIL)
16951 goto erret;
16952 c = *p;
16953 *p = NUL;
16954 arg = vim_strsave(arg);
16955 if (arg == NULL)
16956 goto erret;
16957 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
16958 *p = c;
16959 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960 if (*p == ',')
16961 ++p;
16962 else
16963 mustend = TRUE;
16964 }
16965 p = skipwhite(p);
16966 if (mustend && *p != ')')
16967 {
16968 if (!eap->skip)
16969 EMSG2(_(e_invarg2), eap->arg);
16970 break;
16971 }
16972 }
16973 ++p; /* skip the ')' */
16974
Bram Moolenaare9a41262005-01-15 22:18:47 +000016975 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976 for (;;)
16977 {
16978 p = skipwhite(p);
16979 if (STRNCMP(p, "range", 5) == 0)
16980 {
16981 flags |= FC_RANGE;
16982 p += 5;
16983 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016984 else if (STRNCMP(p, "dict", 4) == 0)
16985 {
16986 flags |= FC_DICT;
16987 p += 4;
16988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989 else if (STRNCMP(p, "abort", 5) == 0)
16990 {
16991 flags |= FC_ABORT;
16992 p += 5;
16993 }
16994 else
16995 break;
16996 }
16997
16998 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
16999 EMSG(_(e_trailing));
17000
17001 /*
17002 * Read the body of the function, until ":endfunction" is found.
17003 */
17004 if (KeyTyped)
17005 {
17006 /* Check if the function already exists, don't let the user type the
17007 * whole function before telling him it doesn't work! For a script we
17008 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017009 if (!eap->skip && !eap->forceit)
17010 {
17011 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17012 EMSG(_(e_funcdict));
17013 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017014 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017017 if (!eap->skip && did_emsg)
17018 goto erret;
17019
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 msg_putchar('\n'); /* don't overwrite the function name */
17021 cmdline_row = msg_row;
17022 }
17023
17024 indent = 2;
17025 nesting = 0;
17026 for (;;)
17027 {
17028 msg_scroll = TRUE;
17029 need_wait_return = FALSE;
17030 if (eap->getline == NULL)
17031 theline = getcmdline(':', 0L, indent);
17032 else
17033 theline = eap->getline(':', eap->cookie, indent);
17034 if (KeyTyped)
17035 lines_left = Rows - 1;
17036 if (theline == NULL)
17037 {
17038 EMSG(_("E126: Missing :endfunction"));
17039 goto erret;
17040 }
17041
17042 if (skip_until != NULL)
17043 {
17044 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17045 * don't check for ":endfunc". */
17046 if (STRCMP(theline, skip_until) == 0)
17047 {
17048 vim_free(skip_until);
17049 skip_until = NULL;
17050 }
17051 }
17052 else
17053 {
17054 /* skip ':' and blanks*/
17055 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17056 ;
17057
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017058 /* Check for "endfunction". */
17059 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060 {
17061 vim_free(theline);
17062 break;
17063 }
17064
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017065 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066 * at "end". */
17067 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17068 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017069 else if (STRNCMP(p, "if", 2) == 0
17070 || STRNCMP(p, "wh", 2) == 0
17071 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072 || STRNCMP(p, "try", 3) == 0)
17073 indent += 2;
17074
17075 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017076 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017077 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017078 if (*p == '!')
17079 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017080 p += eval_fname_script(p);
17081 if (ASCII_ISALPHA(*p))
17082 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017083 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 if (*skipwhite(p) == '(')
17085 {
17086 ++nesting;
17087 indent += 2;
17088 }
17089 }
17090 }
17091
17092 /* Check for ":append" or ":insert". */
17093 p = skip_range(p, NULL);
17094 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17095 || (p[0] == 'i'
17096 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17097 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17098 skip_until = vim_strsave((char_u *)".");
17099
17100 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17101 arg = skipwhite(skiptowhite(p));
17102 if (arg[0] == '<' && arg[1] =='<'
17103 && ((p[0] == 'p' && p[1] == 'y'
17104 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17105 || (p[0] == 'p' && p[1] == 'e'
17106 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17107 || (p[0] == 't' && p[1] == 'c'
17108 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17109 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17110 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017111 || (p[0] == 'm' && p[1] == 'z'
17112 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113 ))
17114 {
17115 /* ":python <<" continues until a dot, like ":append" */
17116 p = skipwhite(arg + 2);
17117 if (*p == NUL)
17118 skip_until = vim_strsave((char_u *)".");
17119 else
17120 skip_until = vim_strsave(p);
17121 }
17122 }
17123
17124 /* Add the line to the function. */
17125 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017126 {
17127 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017129 }
17130
17131 /* Copy the line to newly allocated memory. get_one_sourceline()
17132 * allocates 250 bytes per line, this saves 80% on average. The cost
17133 * is an extra alloc/free. */
17134 p = vim_strsave(theline);
17135 if (p != NULL)
17136 {
17137 vim_free(theline);
17138 theline = p;
17139 }
17140
Bram Moolenaar071d4272004-06-13 20:20:40 +000017141 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17142 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143 }
17144
17145 /* Don't define the function when skipping commands or when an error was
17146 * detected. */
17147 if (eap->skip || did_emsg)
17148 goto erret;
17149
17150 /*
17151 * If there are no errors, add the function
17152 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017153 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017154 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017155 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017156 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017157 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017158 emsg_funcname("E707: Function name conflicts with variable: %s",
17159 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017160 goto erret;
17161 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017162
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017163 fp = find_func(name);
17164 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017166 if (!eap->forceit)
17167 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017168 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017169 goto erret;
17170 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017171 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017172 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017173 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017174 name);
17175 goto erret;
17176 }
17177 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017178 ga_clear_strings(&(fp->uf_args));
17179 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017180 vim_free(name);
17181 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183 }
17184 else
17185 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017186 char numbuf[20];
17187
17188 fp = NULL;
17189 if (fudi.fd_newkey == NULL && !eap->forceit)
17190 {
17191 EMSG(_(e_funcdict));
17192 goto erret;
17193 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017194 if (fudi.fd_di == NULL)
17195 {
17196 /* Can't add a function to a locked dictionary */
17197 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17198 goto erret;
17199 }
17200 /* Can't change an existing function if it is locked */
17201 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17202 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017203
17204 /* Give the function a sequential number. Can only be used with a
17205 * Funcref! */
17206 vim_free(name);
17207 sprintf(numbuf, "%d", ++func_nr);
17208 name = vim_strsave((char_u *)numbuf);
17209 if (name == NULL)
17210 goto erret;
17211 }
17212
17213 if (fp == NULL)
17214 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017215 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017216 {
17217 int slen, plen;
17218 char_u *scriptname;
17219
17220 /* Check that the autoload name matches the script name. */
17221 j = FAIL;
17222 if (sourcing_name != NULL)
17223 {
17224 scriptname = autoload_name(name);
17225 if (scriptname != NULL)
17226 {
17227 p = vim_strchr(scriptname, '/');
17228 plen = STRLEN(p);
17229 slen = STRLEN(sourcing_name);
17230 if (slen > plen && fnamecmp(p,
17231 sourcing_name + slen - plen) == 0)
17232 j = OK;
17233 vim_free(scriptname);
17234 }
17235 }
17236 if (j == FAIL)
17237 {
17238 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17239 goto erret;
17240 }
17241 }
17242
17243 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244 if (fp == NULL)
17245 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017246
17247 if (fudi.fd_dict != NULL)
17248 {
17249 if (fudi.fd_di == NULL)
17250 {
17251 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017252 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017253 if (fudi.fd_di == NULL)
17254 {
17255 vim_free(fp);
17256 goto erret;
17257 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017258 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17259 {
17260 vim_free(fudi.fd_di);
17261 goto erret;
17262 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017263 }
17264 else
17265 /* overwrite existing dict entry */
17266 clear_tv(&fudi.fd_di->di_tv);
17267 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017268 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017269 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017270 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017271 }
17272
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017274 STRCPY(fp->uf_name, name);
17275 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017277 fp->uf_args = newargs;
17278 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017279#ifdef FEAT_PROFILE
17280 fp->uf_tml_count = NULL;
17281 fp->uf_tml_total = NULL;
17282 fp->uf_tml_self = NULL;
17283 fp->uf_profiling = FALSE;
17284 if (prof_def_func())
17285 func_do_profile(fp);
17286#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017287 fp->uf_varargs = varargs;
17288 fp->uf_flags = flags;
17289 fp->uf_calls = 0;
17290 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017291 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292
17293erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294 ga_clear_strings(&newargs);
17295 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017296ret_free:
17297 vim_free(skip_until);
17298 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301}
17302
17303/*
17304 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017305 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017307 * flags:
17308 * TFN_INT: internal function name OK
17309 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310 * Advances "pp" to just after the function name (if no error).
17311 */
17312 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017313trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 char_u **pp;
17315 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017316 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017317 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017319 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 char_u *start;
17321 char_u *end;
17322 int lead;
17323 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017325 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017326
17327 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017328 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017330
17331 /* Check for hard coded <SNR>: already translated function ID (from a user
17332 * command). */
17333 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17334 && (*pp)[2] == (int)KE_SNR)
17335 {
17336 *pp += 3;
17337 len = get_id_len(pp) + 3;
17338 return vim_strnsave(start, len);
17339 }
17340
17341 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17342 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017344 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017346
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017347 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17348 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017349 if (end == start)
17350 {
17351 if (!skip)
17352 EMSG(_("E129: Function name required"));
17353 goto theend;
17354 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017355 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017356 {
17357 /*
17358 * Report an invalid expression in braces, unless the expression
17359 * evaluation has been cancelled due to an aborting error, an
17360 * interrupt, or an exception.
17361 */
17362 if (!aborting())
17363 {
17364 if (end != NULL)
17365 EMSG2(_(e_invarg2), start);
17366 }
17367 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017368 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017369 goto theend;
17370 }
17371
17372 if (lv.ll_tv != NULL)
17373 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017374 if (fdp != NULL)
17375 {
17376 fdp->fd_dict = lv.ll_dict;
17377 fdp->fd_newkey = lv.ll_newkey;
17378 lv.ll_newkey = NULL;
17379 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017380 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017381 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17382 {
17383 name = vim_strsave(lv.ll_tv->vval.v_string);
17384 *pp = end;
17385 }
17386 else
17387 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017388 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17389 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017390 EMSG(_(e_funcref));
17391 else
17392 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017393 name = NULL;
17394 }
17395 goto theend;
17396 }
17397
17398 if (lv.ll_name == NULL)
17399 {
17400 /* Error found, but continue after the function name. */
17401 *pp = end;
17402 goto theend;
17403 }
17404
17405 if (lv.ll_exp_name != NULL)
17406 len = STRLEN(lv.ll_exp_name);
17407 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017408 {
17409 if (lead == 2) /* skip over "s:" */
17410 lv.ll_name += 2;
17411 len = (int)(end - lv.ll_name);
17412 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017413
17414 /*
17415 * Copy the function name to allocated memory.
17416 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17417 * Accept <SNR>123_name() outside a script.
17418 */
17419 if (skip)
17420 lead = 0; /* do nothing */
17421 else if (lead > 0)
17422 {
17423 lead = 3;
17424 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17425 {
17426 if (current_SID <= 0)
17427 {
17428 EMSG(_(e_usingsid));
17429 goto theend;
17430 }
17431 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17432 lead += (int)STRLEN(sid_buf);
17433 }
17434 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017435 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017436 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017437 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017438 goto theend;
17439 }
17440 name = alloc((unsigned)(len + lead + 1));
17441 if (name != NULL)
17442 {
17443 if (lead > 0)
17444 {
17445 name[0] = K_SPECIAL;
17446 name[1] = KS_EXTRA;
17447 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017448 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017449 STRCPY(name + 3, sid_buf);
17450 }
17451 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17452 name[len + lead] = NUL;
17453 }
17454 *pp = end;
17455
17456theend:
17457 clear_lval(&lv);
17458 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459}
17460
17461/*
17462 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17463 * Return 2 if "p" starts with "s:".
17464 * Return 0 otherwise.
17465 */
17466 static int
17467eval_fname_script(p)
17468 char_u *p;
17469{
17470 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17471 || STRNICMP(p + 1, "SNR>", 4) == 0))
17472 return 5;
17473 if (p[0] == 's' && p[1] == ':')
17474 return 2;
17475 return 0;
17476}
17477
17478/*
17479 * Return TRUE if "p" starts with "<SID>" or "s:".
17480 * Only works if eval_fname_script() returned non-zero for "p"!
17481 */
17482 static int
17483eval_fname_sid(p)
17484 char_u *p;
17485{
17486 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17487}
17488
17489/*
17490 * List the head of the function: "name(arg1, arg2)".
17491 */
17492 static void
17493list_func_head(fp, indent)
17494 ufunc_T *fp;
17495 int indent;
17496{
17497 int j;
17498
17499 msg_start();
17500 if (indent)
17501 MSG_PUTS(" ");
17502 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017503 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504 {
17505 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017506 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507 }
17508 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017509 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017511 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 {
17513 if (j)
17514 MSG_PUTS(", ");
17515 msg_puts(FUNCARG(fp, j));
17516 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017517 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518 {
17519 if (j)
17520 MSG_PUTS(", ");
17521 MSG_PUTS("...");
17522 }
17523 msg_putchar(')');
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000017524#ifdef FEAT_EVAL
17525 if (p_verbose > 0)
17526 last_set_msg(fp->uf_script_ID);
17527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528}
17529
17530/*
17531 * Find a function by name, return pointer to it in ufuncs.
17532 * Return NULL for unknown function.
17533 */
17534 static ufunc_T *
17535find_func(name)
17536 char_u *name;
17537{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017538 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017540 hi = hash_find(&func_hashtab, name);
17541 if (!HASHITEM_EMPTY(hi))
17542 return HI2UF(hi);
17543 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017544}
17545
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017546#if defined(EXITFREE) || defined(PROTO)
17547 void
17548free_all_functions()
17549{
17550 hashitem_T *hi;
17551
17552 /* Need to start all over every time, because func_free() may change the
17553 * hash table. */
17554 while (func_hashtab.ht_used > 0)
17555 for (hi = func_hashtab.ht_array; ; ++hi)
17556 if (!HASHITEM_EMPTY(hi))
17557 {
17558 func_free(HI2UF(hi));
17559 break;
17560 }
17561}
17562#endif
17563
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017564/*
17565 * Return TRUE if a function "name" exists.
17566 */
17567 static int
17568function_exists(name)
17569 char_u *name;
17570{
17571 char_u *p = name;
17572 int n = FALSE;
17573
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017574 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017575 if (p != NULL)
17576 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017577 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017578 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017579 else
17580 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017581 vim_free(p);
17582 }
17583 return n;
17584}
17585
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017586/*
17587 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017588 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017589 */
17590 static int
17591builtin_function(name)
17592 char_u *name;
17593{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017594 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17595 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017596}
17597
Bram Moolenaar05159a02005-02-26 23:04:13 +000017598#if defined(FEAT_PROFILE) || defined(PROTO)
17599/*
17600 * Start profiling function "fp".
17601 */
17602 static void
17603func_do_profile(fp)
17604 ufunc_T *fp;
17605{
17606 fp->uf_tm_count = 0;
17607 profile_zero(&fp->uf_tm_self);
17608 profile_zero(&fp->uf_tm_total);
17609 if (fp->uf_tml_count == NULL)
17610 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17611 (sizeof(int) * fp->uf_lines.ga_len));
17612 if (fp->uf_tml_total == NULL)
17613 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17614 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17615 if (fp->uf_tml_self == NULL)
17616 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17617 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17618 fp->uf_tml_idx = -1;
17619 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17620 || fp->uf_tml_self == NULL)
17621 return; /* out of memory */
17622
17623 fp->uf_profiling = TRUE;
17624}
17625
17626/*
17627 * Dump the profiling results for all functions in file "fd".
17628 */
17629 void
17630func_dump_profile(fd)
17631 FILE *fd;
17632{
17633 hashitem_T *hi;
17634 int todo;
17635 ufunc_T *fp;
17636 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017637 ufunc_T **sorttab;
17638 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017639
17640 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017641 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17642
Bram Moolenaar05159a02005-02-26 23:04:13 +000017643 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17644 {
17645 if (!HASHITEM_EMPTY(hi))
17646 {
17647 --todo;
17648 fp = HI2UF(hi);
17649 if (fp->uf_profiling)
17650 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017651 if (sorttab != NULL)
17652 sorttab[st_len++] = fp;
17653
Bram Moolenaar05159a02005-02-26 23:04:13 +000017654 if (fp->uf_name[0] == K_SPECIAL)
17655 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17656 else
17657 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17658 if (fp->uf_tm_count == 1)
17659 fprintf(fd, "Called 1 time\n");
17660 else
17661 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17662 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17663 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17664 fprintf(fd, "\n");
17665 fprintf(fd, "count total (s) self (s)\n");
17666
17667 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17668 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017669 prof_func_line(fd, fp->uf_tml_count[i],
17670 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017671 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17672 }
17673 fprintf(fd, "\n");
17674 }
17675 }
17676 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017677
17678 if (sorttab != NULL && st_len > 0)
17679 {
17680 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17681 prof_total_cmp);
17682 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17683 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17684 prof_self_cmp);
17685 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17686 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017687}
Bram Moolenaar73830342005-02-28 22:48:19 +000017688
17689 static void
17690prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17691 FILE *fd;
17692 ufunc_T **sorttab;
17693 int st_len;
17694 char *title;
17695 int prefer_self; /* when equal print only self time */
17696{
17697 int i;
17698 ufunc_T *fp;
17699
17700 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17701 fprintf(fd, "count total (s) self (s) function\n");
17702 for (i = 0; i < 20 && i < st_len; ++i)
17703 {
17704 fp = sorttab[i];
17705 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17706 prefer_self);
17707 if (fp->uf_name[0] == K_SPECIAL)
17708 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17709 else
17710 fprintf(fd, " %s()\n", fp->uf_name);
17711 }
17712 fprintf(fd, "\n");
17713}
17714
17715/*
17716 * Print the count and times for one function or function line.
17717 */
17718 static void
17719prof_func_line(fd, count, total, self, prefer_self)
17720 FILE *fd;
17721 int count;
17722 proftime_T *total;
17723 proftime_T *self;
17724 int prefer_self; /* when equal print only self time */
17725{
17726 if (count > 0)
17727 {
17728 fprintf(fd, "%5d ", count);
17729 if (prefer_self && profile_equal(total, self))
17730 fprintf(fd, " ");
17731 else
17732 fprintf(fd, "%s ", profile_msg(total));
17733 if (!prefer_self && profile_equal(total, self))
17734 fprintf(fd, " ");
17735 else
17736 fprintf(fd, "%s ", profile_msg(self));
17737 }
17738 else
17739 fprintf(fd, " ");
17740}
17741
17742/*
17743 * Compare function for total time sorting.
17744 */
17745 static int
17746#ifdef __BORLANDC__
17747_RTLENTRYF
17748#endif
17749prof_total_cmp(s1, s2)
17750 const void *s1;
17751 const void *s2;
17752{
17753 ufunc_T *p1, *p2;
17754
17755 p1 = *(ufunc_T **)s1;
17756 p2 = *(ufunc_T **)s2;
17757 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17758}
17759
17760/*
17761 * Compare function for self time sorting.
17762 */
17763 static int
17764#ifdef __BORLANDC__
17765_RTLENTRYF
17766#endif
17767prof_self_cmp(s1, s2)
17768 const void *s1;
17769 const void *s2;
17770{
17771 ufunc_T *p1, *p2;
17772
17773 p1 = *(ufunc_T **)s1;
17774 p2 = *(ufunc_T **)s2;
17775 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17776}
17777
Bram Moolenaar05159a02005-02-26 23:04:13 +000017778#endif
17779
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017780/* The names of packages that once were loaded is remembered. */
17781static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
17782
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017783/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017784 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017785 * Return TRUE if a package was loaded.
17786 */
17787 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017788script_autoload(name, reload)
17789 char_u *name;
17790 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017791{
17792 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017793 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017794 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017795 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017796
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017797 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017798 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017799 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017800 return FALSE;
17801
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017802 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017803
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017804 /* Find the name in the list of previously loaded package names. Skip
17805 * "autoload/", it's always the same. */
17806 for (i = 0; i < ga_loaded.ga_len; ++i)
17807 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
17808 break;
17809 if (!reload && i < ga_loaded.ga_len)
17810 ret = FALSE; /* was loaded already */
17811 else
17812 {
17813 /* Remember the name if it wasn't loaded already. */
17814 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
17815 {
17816 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
17817 tofree = NULL;
17818 }
17819
17820 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000017821 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017822 ret = TRUE;
17823 }
17824
17825 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017826 return ret;
17827}
17828
17829/*
17830 * Return the autoload script name for a function or variable name.
17831 * Returns NULL when out of memory.
17832 */
17833 static char_u *
17834autoload_name(name)
17835 char_u *name;
17836{
17837 char_u *p;
17838 char_u *scriptname;
17839
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017840 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017841 scriptname = alloc((unsigned)(STRLEN(name) + 14));
17842 if (scriptname == NULL)
17843 return FALSE;
17844 STRCPY(scriptname, "autoload/");
17845 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017846 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017847 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017848 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017849 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017850 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017851}
17852
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
17854
17855/*
17856 * Function given to ExpandGeneric() to obtain the list of user defined
17857 * function names.
17858 */
17859 char_u *
17860get_user_func_name(xp, idx)
17861 expand_T *xp;
17862 int idx;
17863{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017864 static long_u done;
17865 static hashitem_T *hi;
17866 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867
17868 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017870 done = 0;
17871 hi = func_hashtab.ht_array;
17872 }
17873 if (done < func_hashtab.ht_used)
17874 {
17875 if (done++ > 0)
17876 ++hi;
17877 while (HASHITEM_EMPTY(hi))
17878 ++hi;
17879 fp = HI2UF(hi);
17880
17881 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
17882 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883
17884 cat_func_name(IObuff, fp);
17885 if (xp->xp_context != EXPAND_USER_FUNC)
17886 {
17887 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017888 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889 STRCAT(IObuff, ")");
17890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891 return IObuff;
17892 }
17893 return NULL;
17894}
17895
17896#endif /* FEAT_CMDL_COMPL */
17897
17898/*
17899 * Copy the function name of "fp" to buffer "buf".
17900 * "buf" must be able to hold the function name plus three bytes.
17901 * Takes care of script-local function names.
17902 */
17903 static void
17904cat_func_name(buf, fp)
17905 char_u *buf;
17906 ufunc_T *fp;
17907{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017908 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909 {
17910 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017911 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912 }
17913 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017914 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915}
17916
17917/*
17918 * ":delfunction {name}"
17919 */
17920 void
17921ex_delfunction(eap)
17922 exarg_T *eap;
17923{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017924 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925 char_u *p;
17926 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017927 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928
17929 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017930 name = trans_function_name(&p, eap->skip, 0, &fudi);
17931 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017933 {
17934 if (fudi.fd_dict != NULL && !eap->skip)
17935 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938 if (!ends_excmd(*skipwhite(p)))
17939 {
17940 vim_free(name);
17941 EMSG(_(e_trailing));
17942 return;
17943 }
17944 eap->nextcmd = check_nextcmd(p);
17945 if (eap->nextcmd != NULL)
17946 *p = NUL;
17947
17948 if (!eap->skip)
17949 fp = find_func(name);
17950 vim_free(name);
17951
17952 if (!eap->skip)
17953 {
17954 if (fp == NULL)
17955 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017956 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957 return;
17958 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017959 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960 {
17961 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
17962 return;
17963 }
17964
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017965 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017966 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017967 /* Delete the dict item that refers to the function, it will
17968 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017969 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017971 else
17972 func_free(fp);
17973 }
17974}
17975
17976/*
17977 * Free a function and remove it from the list of functions.
17978 */
17979 static void
17980func_free(fp)
17981 ufunc_T *fp;
17982{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017983 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017984
17985 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017986 ga_clear_strings(&(fp->uf_args));
17987 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000017988#ifdef FEAT_PROFILE
17989 vim_free(fp->uf_tml_count);
17990 vim_free(fp->uf_tml_total);
17991 vim_free(fp->uf_tml_self);
17992#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017993
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017994 /* remove the function from the function hashtable */
17995 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
17996 if (HASHITEM_EMPTY(hi))
17997 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017998 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017999 hash_remove(&func_hashtab, hi);
18000
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018001 vim_free(fp);
18002}
18003
18004/*
18005 * Unreference a Function: decrement the reference count and free it when it
18006 * becomes zero. Only for numbered functions.
18007 */
18008 static void
18009func_unref(name)
18010 char_u *name;
18011{
18012 ufunc_T *fp;
18013
18014 if (name != NULL && isdigit(*name))
18015 {
18016 fp = find_func(name);
18017 if (fp == NULL)
18018 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018019 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018020 {
18021 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018022 * when "uf_calls" becomes zero. */
18023 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018024 func_free(fp);
18025 }
18026 }
18027}
18028
18029/*
18030 * Count a reference to a Function.
18031 */
18032 static void
18033func_ref(name)
18034 char_u *name;
18035{
18036 ufunc_T *fp;
18037
18038 if (name != NULL && isdigit(*name))
18039 {
18040 fp = find_func(name);
18041 if (fp == NULL)
18042 EMSG2(_(e_intern2), "func_ref()");
18043 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018044 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045 }
18046}
18047
18048/*
18049 * Call a user function.
18050 */
18051 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018052call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 ufunc_T *fp; /* pointer to function */
18054 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018055 typval_T *argvars; /* arguments */
18056 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018057 linenr_T firstline; /* first line of range */
18058 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018059 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060{
Bram Moolenaar33570922005-01-25 22:26:29 +000018061 char_u *save_sourcing_name;
18062 linenr_T save_sourcing_lnum;
18063 scid_T save_current_SID;
18064 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018065 int save_did_emsg;
18066 static int depth = 0;
18067 dictitem_T *v;
18068 int fixvar_idx = 0; /* index in fixvar[] */
18069 int i;
18070 int ai;
18071 char_u numbuf[NUMBUFLEN];
18072 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018073#ifdef FEAT_PROFILE
18074 proftime_T wait_start;
18075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076
18077 /* If depth of calling is getting too high, don't execute the function */
18078 if (depth >= p_mfd)
18079 {
18080 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018081 rettv->v_type = VAR_NUMBER;
18082 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083 return;
18084 }
18085 ++depth;
18086
18087 line_breakcheck(); /* check for CTRL-C hit */
18088
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018089 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018090 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018092 fc.rettv = rettv;
18093 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 fc.linenr = 0;
18095 fc.returned = FALSE;
18096 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018098 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 fc.dbg_tick = debug_tick;
18100
Bram Moolenaar33570922005-01-25 22:26:29 +000018101 /*
18102 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18103 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18104 * each argument variable and saves a lot of time.
18105 */
18106 /*
18107 * Init l: variables.
18108 */
18109 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018110 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018111 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018112 /* Set l:self to "selfdict". */
18113 v = &fc.fixvar[fixvar_idx++].var;
18114 STRCPY(v->di_key, "self");
18115 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18116 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18117 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018118 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018119 v->di_tv.vval.v_dict = selfdict;
18120 ++selfdict->dv_refcount;
18121 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018122
Bram Moolenaar33570922005-01-25 22:26:29 +000018123 /*
18124 * Init a: variables.
18125 * Set a:0 to "argcount".
18126 * Set a:000 to a list with room for the "..." arguments.
18127 */
18128 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18129 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018130 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018131 v = &fc.fixvar[fixvar_idx++].var;
18132 STRCPY(v->di_key, "000");
18133 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18134 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18135 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018136 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018137 v->di_tv.vval.v_list = &fc.l_varlist;
18138 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18139 fc.l_varlist.lv_refcount = 99999;
18140
18141 /*
18142 * Set a:firstline to "firstline" and a:lastline to "lastline".
18143 * Set a:name to named arguments.
18144 * Set a:N to the "..." arguments.
18145 */
18146 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18147 (varnumber_T)firstline);
18148 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18149 (varnumber_T)lastline);
18150 for (i = 0; i < argcount; ++i)
18151 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018152 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018153 if (ai < 0)
18154 /* named argument a:name */
18155 name = FUNCARG(fp, i);
18156 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018157 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018158 /* "..." argument a:1, a:2, etc. */
18159 sprintf((char *)numbuf, "%d", ai + 1);
18160 name = numbuf;
18161 }
18162 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18163 {
18164 v = &fc.fixvar[fixvar_idx++].var;
18165 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18166 }
18167 else
18168 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018169 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18170 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018171 if (v == NULL)
18172 break;
18173 v->di_flags = DI_FLAGS_RO;
18174 }
18175 STRCPY(v->di_key, name);
18176 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18177
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018178 /* Note: the values are copied directly to avoid alloc/free.
18179 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018180 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018181 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018182
18183 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18184 {
18185 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18186 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018187 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018188 }
18189 }
18190
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191 /* Don't redraw while executing the function. */
18192 ++RedrawingDisabled;
18193 save_sourcing_name = sourcing_name;
18194 save_sourcing_lnum = sourcing_lnum;
18195 sourcing_lnum = 1;
18196 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018197 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198 if (sourcing_name != NULL)
18199 {
18200 if (save_sourcing_name != NULL
18201 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18202 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18203 else
18204 STRCPY(sourcing_name, "function ");
18205 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18206
18207 if (p_verbose >= 12)
18208 {
18209 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018210 verbose_enter_scroll();
18211
Bram Moolenaar555b2802005-05-19 21:08:39 +000018212 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213 if (p_verbose >= 14)
18214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018216 char_u numbuf[NUMBUFLEN];
18217 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218
18219 msg_puts((char_u *)"(");
18220 for (i = 0; i < argcount; ++i)
18221 {
18222 if (i > 0)
18223 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018224 if (argvars[i].v_type == VAR_NUMBER)
18225 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226 else
18227 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018228 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018229 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018231 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232 }
18233 }
18234 msg_puts((char_u *)")");
18235 }
18236 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018237
18238 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239 --no_wait_return;
18240 }
18241 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018242#ifdef FEAT_PROFILE
18243 if (do_profiling)
18244 {
18245 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18246 func_do_profile(fp);
18247 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018248 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018249 {
18250 ++fp->uf_tm_count;
18251 profile_start(&fp->uf_tm_start);
18252 profile_zero(&fp->uf_tm_children);
18253 }
18254 script_prof_save(&wait_start);
18255 }
18256#endif
18257
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018259 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260 save_did_emsg = did_emsg;
18261 did_emsg = FALSE;
18262
18263 /* call do_cmdline() to execute the lines */
18264 do_cmdline(NULL, get_func_line, (void *)&fc,
18265 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18266
18267 --RedrawingDisabled;
18268
18269 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018270 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018272 clear_tv(rettv);
18273 rettv->v_type = VAR_NUMBER;
18274 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275 }
18276
Bram Moolenaar05159a02005-02-26 23:04:13 +000018277#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018278 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018279 {
18280 profile_end(&fp->uf_tm_start);
18281 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18282 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18283 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18284 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018285 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018286 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018287 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18288 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018289 }
18290 }
18291#endif
18292
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293 /* when being verbose, mention the return value */
18294 if (p_verbose >= 12)
18295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018297 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018300 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018301 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018302 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18303 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018304 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018306 char_u buf[MSG_BUF_LEN];
18307 char_u numbuf[NUMBUFLEN];
18308 char_u *tofree;
18309
Bram Moolenaar555b2802005-05-19 21:08:39 +000018310 /* The value may be very long. Skip the middle part, so that we
18311 * have some idea how it starts and ends. smsg() would always
18312 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018313 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018314 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018315 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018316 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317 }
18318 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018319
18320 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321 --no_wait_return;
18322 }
18323
18324 vim_free(sourcing_name);
18325 sourcing_name = save_sourcing_name;
18326 sourcing_lnum = save_sourcing_lnum;
18327 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018328#ifdef FEAT_PROFILE
18329 if (do_profiling)
18330 script_prof_restore(&wait_start);
18331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332
18333 if (p_verbose >= 12 && sourcing_name != NULL)
18334 {
18335 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018336 verbose_enter_scroll();
18337
Bram Moolenaar555b2802005-05-19 21:08:39 +000018338 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018340
18341 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 --no_wait_return;
18343 }
18344
18345 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018346 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347
Bram Moolenaar33570922005-01-25 22:26:29 +000018348 /* The a: variables typevals were not alloced, only free the allocated
18349 * variables. */
18350 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18351
18352 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 --depth;
18354}
18355
18356/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018357 * Add a number variable "name" to dict "dp" with value "nr".
18358 */
18359 static void
18360add_nr_var(dp, v, name, nr)
18361 dict_T *dp;
18362 dictitem_T *v;
18363 char *name;
18364 varnumber_T nr;
18365{
18366 STRCPY(v->di_key, name);
18367 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18368 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18369 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018370 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018371 v->di_tv.vval.v_number = nr;
18372}
18373
18374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 * ":return [expr]"
18376 */
18377 void
18378ex_return(eap)
18379 exarg_T *eap;
18380{
18381 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018382 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383 int returning = FALSE;
18384
18385 if (current_funccal == NULL)
18386 {
18387 EMSG(_("E133: :return not inside a function"));
18388 return;
18389 }
18390
18391 if (eap->skip)
18392 ++emsg_skip;
18393
18394 eap->nextcmd = NULL;
18395 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018396 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397 {
18398 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018399 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018401 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402 }
18403 /* It's safer to return also on error. */
18404 else if (!eap->skip)
18405 {
18406 /*
18407 * Return unless the expression evaluation has been cancelled due to an
18408 * aborting error, an interrupt, or an exception.
18409 */
18410 if (!aborting())
18411 returning = do_return(eap, FALSE, TRUE, NULL);
18412 }
18413
18414 /* When skipping or the return gets pending, advance to the next command
18415 * in this line (!returning). Otherwise, ignore the rest of the line.
18416 * Following lines will be ignored by get_func_line(). */
18417 if (returning)
18418 eap->nextcmd = NULL;
18419 else if (eap->nextcmd == NULL) /* no argument */
18420 eap->nextcmd = check_nextcmd(arg);
18421
18422 if (eap->skip)
18423 --emsg_skip;
18424}
18425
18426/*
18427 * Return from a function. Possibly makes the return pending. Also called
18428 * for a pending return at the ":endtry" or after returning from an extra
18429 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018430 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018431 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018432 * FALSE when the return gets pending.
18433 */
18434 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018435do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018436 exarg_T *eap;
18437 int reanimate;
18438 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018439 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440{
18441 int idx;
18442 struct condstack *cstack = eap->cstack;
18443
18444 if (reanimate)
18445 /* Undo the return. */
18446 current_funccal->returned = FALSE;
18447
18448 /*
18449 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18450 * not in its finally clause (which then is to be executed next) is found.
18451 * In this case, make the ":return" pending for execution at the ":endtry".
18452 * Otherwise, return normally.
18453 */
18454 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18455 if (idx >= 0)
18456 {
18457 cstack->cs_pending[idx] = CSTP_RETURN;
18458
18459 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018460 /* A pending return again gets pending. "rettv" points to an
18461 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018463 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464 else
18465 {
18466 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018467 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018469 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018471 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472 {
18473 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018474 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018475 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 else
18477 EMSG(_(e_outofmem));
18478 }
18479 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018480 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481
18482 if (reanimate)
18483 {
18484 /* The pending return value could be overwritten by a ":return"
18485 * without argument in a finally clause; reset the default
18486 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018487 current_funccal->rettv->v_type = VAR_NUMBER;
18488 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489 }
18490 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018491 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492 }
18493 else
18494 {
18495 current_funccal->returned = TRUE;
18496
18497 /* If the return is carried out now, store the return value. For
18498 * a return immediately after reanimation, the value is already
18499 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018500 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018502 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018503 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018504 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018505 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018506 }
18507 }
18508
18509 return idx < 0;
18510}
18511
18512/*
18513 * Free the variable with a pending return value.
18514 */
18515 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018516discard_pending_return(rettv)
18517 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518{
Bram Moolenaar33570922005-01-25 22:26:29 +000018519 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520}
18521
18522/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018523 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524 * is an allocated string. Used by report_pending() for verbose messages.
18525 */
18526 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018527get_return_cmd(rettv)
18528 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018530 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018531 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018532 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018534 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018535 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018536 if (s == NULL)
18537 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018538
18539 STRCPY(IObuff, ":return ");
18540 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18541 if (STRLEN(s) + 8 >= IOSIZE)
18542 STRCPY(IObuff + IOSIZE - 4, "...");
18543 vim_free(tofree);
18544 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545}
18546
18547/*
18548 * Get next function line.
18549 * Called by do_cmdline() to get the next line.
18550 * Returns allocated string, or NULL for end of function.
18551 */
18552/* ARGSUSED */
18553 char_u *
18554get_func_line(c, cookie, indent)
18555 int c; /* not used */
18556 void *cookie;
18557 int indent; /* not used */
18558{
Bram Moolenaar33570922005-01-25 22:26:29 +000018559 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018560 ufunc_T *fp = fcp->func;
18561 char_u *retval;
18562 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563
18564 /* If breakpoints have been added/deleted need to check for it. */
18565 if (fcp->dbg_tick != debug_tick)
18566 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018567 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 sourcing_lnum);
18569 fcp->dbg_tick = debug_tick;
18570 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018571#ifdef FEAT_PROFILE
18572 if (do_profiling)
18573 func_line_end(cookie);
18574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575
Bram Moolenaar05159a02005-02-26 23:04:13 +000018576 gap = &fp->uf_lines;
18577 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578 retval = NULL;
18579 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18580 retval = NULL;
18581 else
18582 {
18583 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18584 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018585#ifdef FEAT_PROFILE
18586 if (do_profiling)
18587 func_line_start(cookie);
18588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589 }
18590
18591 /* Did we encounter a breakpoint? */
18592 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18593 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018594 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018596 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597 sourcing_lnum);
18598 fcp->dbg_tick = debug_tick;
18599 }
18600
18601 return retval;
18602}
18603
Bram Moolenaar05159a02005-02-26 23:04:13 +000018604#if defined(FEAT_PROFILE) || defined(PROTO)
18605/*
18606 * Called when starting to read a function line.
18607 * "sourcing_lnum" must be correct!
18608 * When skipping lines it may not actually be executed, but we won't find out
18609 * until later and we need to store the time now.
18610 */
18611 void
18612func_line_start(cookie)
18613 void *cookie;
18614{
18615 funccall_T *fcp = (funccall_T *)cookie;
18616 ufunc_T *fp = fcp->func;
18617
18618 if (fp->uf_profiling && sourcing_lnum >= 1
18619 && sourcing_lnum <= fp->uf_lines.ga_len)
18620 {
18621 fp->uf_tml_idx = sourcing_lnum - 1;
18622 fp->uf_tml_execed = FALSE;
18623 profile_start(&fp->uf_tml_start);
18624 profile_zero(&fp->uf_tml_children);
18625 profile_get_wait(&fp->uf_tml_wait);
18626 }
18627}
18628
18629/*
18630 * Called when actually executing a function line.
18631 */
18632 void
18633func_line_exec(cookie)
18634 void *cookie;
18635{
18636 funccall_T *fcp = (funccall_T *)cookie;
18637 ufunc_T *fp = fcp->func;
18638
18639 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18640 fp->uf_tml_execed = TRUE;
18641}
18642
18643/*
18644 * Called when done with a function line.
18645 */
18646 void
18647func_line_end(cookie)
18648 void *cookie;
18649{
18650 funccall_T *fcp = (funccall_T *)cookie;
18651 ufunc_T *fp = fcp->func;
18652
18653 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18654 {
18655 if (fp->uf_tml_execed)
18656 {
18657 ++fp->uf_tml_count[fp->uf_tml_idx];
18658 profile_end(&fp->uf_tml_start);
18659 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18660 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18661 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18662 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18663 }
18664 fp->uf_tml_idx = -1;
18665 }
18666}
18667#endif
18668
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669/*
18670 * Return TRUE if the currently active function should be ended, because a
18671 * return was encountered or an error occured. Used inside a ":while".
18672 */
18673 int
18674func_has_ended(cookie)
18675 void *cookie;
18676{
Bram Moolenaar33570922005-01-25 22:26:29 +000018677 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678
18679 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18680 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018681 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682 || fcp->returned);
18683}
18684
18685/*
18686 * return TRUE if cookie indicates a function which "abort"s on errors.
18687 */
18688 int
18689func_has_abort(cookie)
18690 void *cookie;
18691{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018692 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693}
18694
18695#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18696typedef enum
18697{
18698 VAR_FLAVOUR_DEFAULT,
18699 VAR_FLAVOUR_SESSION,
18700 VAR_FLAVOUR_VIMINFO
18701} var_flavour_T;
18702
18703static var_flavour_T var_flavour __ARGS((char_u *varname));
18704
18705 static var_flavour_T
18706var_flavour(varname)
18707 char_u *varname;
18708{
18709 char_u *p = varname;
18710
18711 if (ASCII_ISUPPER(*p))
18712 {
18713 while (*(++p))
18714 if (ASCII_ISLOWER(*p))
18715 return VAR_FLAVOUR_SESSION;
18716 return VAR_FLAVOUR_VIMINFO;
18717 }
18718 else
18719 return VAR_FLAVOUR_DEFAULT;
18720}
18721#endif
18722
18723#if defined(FEAT_VIMINFO) || defined(PROTO)
18724/*
18725 * Restore global vars that start with a capital from the viminfo file
18726 */
18727 int
18728read_viminfo_varlist(virp, writing)
18729 vir_T *virp;
18730 int writing;
18731{
18732 char_u *tab;
18733 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018734 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735
18736 if (!writing && (find_viminfo_parameter('!') != NULL))
18737 {
18738 tab = vim_strchr(virp->vir_line + 1, '\t');
18739 if (tab != NULL)
18740 {
18741 *tab++ = '\0'; /* isolate the variable name */
18742 if (*tab == 'S') /* string var */
18743 is_string = TRUE;
18744
18745 tab = vim_strchr(tab, '\t');
18746 if (tab != NULL)
18747 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 if (is_string)
18749 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018750 tv.v_type = VAR_STRING;
18751 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753 }
18754 else
18755 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018756 tv.v_type = VAR_NUMBER;
18757 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018759 set_var(virp->vir_line + 1, &tv, FALSE);
18760 if (is_string)
18761 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 }
18763 }
18764 }
18765
18766 return viminfo_readline(virp);
18767}
18768
18769/*
18770 * Write global vars that start with a capital to the viminfo file
18771 */
18772 void
18773write_viminfo_varlist(fp)
18774 FILE *fp;
18775{
Bram Moolenaar33570922005-01-25 22:26:29 +000018776 hashitem_T *hi;
18777 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018778 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018779 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018780 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018781 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018782 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783
18784 if (find_viminfo_parameter('!') == NULL)
18785 return;
18786
18787 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018788
Bram Moolenaar33570922005-01-25 22:26:29 +000018789 todo = globvarht.ht_used;
18790 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018792 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018794 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018795 this_var = HI2DI(hi);
18796 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018797 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018798 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018799 {
18800 case VAR_STRING: s = "STR"; break;
18801 case VAR_NUMBER: s = "NUM"; break;
18802 default: continue;
18803 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018804 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018805 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18806 if (p != NULL)
18807 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018808 vim_free(tofree);
18809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810 }
18811 }
18812}
18813#endif
18814
18815#if defined(FEAT_SESSION) || defined(PROTO)
18816 int
18817store_session_globals(fd)
18818 FILE *fd;
18819{
Bram Moolenaar33570922005-01-25 22:26:29 +000018820 hashitem_T *hi;
18821 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018822 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 char_u *p, *t;
18824
Bram Moolenaar33570922005-01-25 22:26:29 +000018825 todo = globvarht.ht_used;
18826 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018828 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018830 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018831 this_var = HI2DI(hi);
18832 if ((this_var->di_tv.v_type == VAR_NUMBER
18833 || this_var->di_tv.v_type == VAR_STRING)
18834 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018835 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018836 /* Escape special characters with a backslash. Turn a LF and
18837 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018838 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018839 (char_u *)"\\\"\n\r");
18840 if (p == NULL) /* out of memory */
18841 break;
18842 for (t = p; *t != NUL; ++t)
18843 if (*t == '\n')
18844 *t = 'n';
18845 else if (*t == '\r')
18846 *t = 'r';
18847 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000018848 this_var->di_key,
18849 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18850 : ' ',
18851 p,
18852 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18853 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000018854 || put_eol(fd) == FAIL)
18855 {
18856 vim_free(p);
18857 return FAIL;
18858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859 vim_free(p);
18860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861 }
18862 }
18863 return OK;
18864}
18865#endif
18866
Bram Moolenaar661b1822005-07-28 22:36:45 +000018867/*
18868 * Display script name where an item was last set.
18869 * Should only be invoked when 'verbose' is non-zero.
18870 */
18871 void
18872last_set_msg(scriptID)
18873 scid_T scriptID;
18874{
18875 if (scriptID != 0)
18876 {
18877 verbose_enter();
18878 MSG_PUTS(_("\n\tLast set from "));
18879 MSG_PUTS(get_scriptname(scriptID));
18880 verbose_leave();
18881 }
18882}
18883
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884#endif /* FEAT_EVAL */
18885
18886#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
18887
18888
18889#ifdef WIN3264
18890/*
18891 * Functions for ":8" filename modifier: get 8.3 version of a filename.
18892 */
18893static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18894static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
18895static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18896
18897/*
18898 * Get the short pathname of a file.
18899 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
18900 */
18901 static int
18902get_short_pathname(fnamep, bufp, fnamelen)
18903 char_u **fnamep;
18904 char_u **bufp;
18905 int *fnamelen;
18906{
18907 int l,len;
18908 char_u *newbuf;
18909
18910 len = *fnamelen;
18911
18912 l = GetShortPathName(*fnamep, *fnamep, len);
18913 if (l > len - 1)
18914 {
18915 /* If that doesn't work (not enough space), then save the string
18916 * and try again with a new buffer big enough
18917 */
18918 newbuf = vim_strnsave(*fnamep, l);
18919 if (newbuf == NULL)
18920 return 0;
18921
18922 vim_free(*bufp);
18923 *fnamep = *bufp = newbuf;
18924
18925 l = GetShortPathName(*fnamep,*fnamep,l+1);
18926
18927 /* Really should always succeed, as the buffer is big enough */
18928 }
18929
18930 *fnamelen = l;
18931 return 1;
18932}
18933
18934/*
18935 * Create a short path name. Returns the length of the buffer it needs.
18936 * Doesn't copy over the end of the buffer passed in.
18937 */
18938 static int
18939shortpath_for_invalid_fname(fname, bufp, fnamelen)
18940 char_u **fname;
18941 char_u **bufp;
18942 int *fnamelen;
18943{
18944 char_u *s, *p, *pbuf2, *pbuf3;
18945 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018946 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018947
18948 /* Make a copy */
18949 len2 = *fnamelen;
18950 pbuf2 = vim_strnsave(*fname, len2);
18951 pbuf3 = NULL;
18952
18953 s = pbuf2 + len2 - 1; /* Find the end */
18954 slen = 1;
18955 plen = len2;
18956
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018957 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958 {
18959 --s;
18960 ++slen;
18961 --plen;
18962 }
18963
18964 do
18965 {
18966 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018967 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968 {
18969 --s;
18970 ++slen;
18971 --plen;
18972 }
18973 if (s <= pbuf2)
18974 break;
18975
18976 /* Remeber the character that is about to be blatted */
18977 ch = *s;
18978 *s = 0; /* get_short_pathname requires a null-terminated string */
18979
18980 /* Try it in situ */
18981 p = pbuf2;
18982 if (!get_short_pathname(&p, &pbuf3, &plen))
18983 {
18984 vim_free(pbuf2);
18985 return -1;
18986 }
18987 *s = ch; /* Preserve the string */
18988 } while (plen == 0);
18989
18990 if (plen > 0)
18991 {
18992 /* Remeber the length of the new string. */
18993 *fnamelen = len = plen + slen;
18994 vim_free(*bufp);
18995 if (len > len2)
18996 {
18997 /* If there's not enough space in the currently allocated string,
18998 * then copy it to a buffer big enough.
18999 */
19000 *fname= *bufp = vim_strnsave(p, len);
19001 if (*fname == NULL)
19002 return -1;
19003 }
19004 else
19005 {
19006 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19007 *fname = *bufp = pbuf2;
19008 if (p != pbuf2)
19009 strncpy(*fname, p, plen);
19010 pbuf2 = NULL;
19011 }
19012 /* Concat the next bit */
19013 strncpy(*fname + plen, s, slen);
19014 (*fname)[len] = '\0';
19015 }
19016 vim_free(pbuf3);
19017 vim_free(pbuf2);
19018 return 0;
19019}
19020
19021/*
19022 * Get a pathname for a partial path.
19023 */
19024 static int
19025shortpath_for_partial(fnamep, bufp, fnamelen)
19026 char_u **fnamep;
19027 char_u **bufp;
19028 int *fnamelen;
19029{
19030 int sepcount, len, tflen;
19031 char_u *p;
19032 char_u *pbuf, *tfname;
19033 int hasTilde;
19034
19035 /* Count up the path seperators from the RHS.. so we know which part
19036 * of the path to return.
19037 */
19038 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019039 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040 if (vim_ispathsep(*p))
19041 ++sepcount;
19042
19043 /* Need full path first (use expand_env() to remove a "~/") */
19044 hasTilde = (**fnamep == '~');
19045 if (hasTilde)
19046 pbuf = tfname = expand_env_save(*fnamep);
19047 else
19048 pbuf = tfname = FullName_save(*fnamep, FALSE);
19049
19050 len = tflen = STRLEN(tfname);
19051
19052 if (!get_short_pathname(&tfname, &pbuf, &len))
19053 return -1;
19054
19055 if (len == 0)
19056 {
19057 /* Don't have a valid filename, so shorten the rest of the
19058 * path if we can. This CAN give us invalid 8.3 filenames, but
19059 * there's not a lot of point in guessing what it might be.
19060 */
19061 len = tflen;
19062 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19063 return -1;
19064 }
19065
19066 /* Count the paths backward to find the beginning of the desired string. */
19067 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019068 {
19069#ifdef FEAT_MBYTE
19070 if (has_mbyte)
19071 p -= mb_head_off(tfname, p);
19072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073 if (vim_ispathsep(*p))
19074 {
19075 if (sepcount == 0 || (hasTilde && sepcount == 1))
19076 break;
19077 else
19078 sepcount --;
19079 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 if (hasTilde)
19082 {
19083 --p;
19084 if (p >= tfname)
19085 *p = '~';
19086 else
19087 return -1;
19088 }
19089 else
19090 ++p;
19091
19092 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19093 vim_free(*bufp);
19094 *fnamelen = (int)STRLEN(p);
19095 *bufp = pbuf;
19096 *fnamep = p;
19097
19098 return 0;
19099}
19100#endif /* WIN3264 */
19101
19102/*
19103 * Adjust a filename, according to a string of modifiers.
19104 * *fnamep must be NUL terminated when called. When returning, the length is
19105 * determined by *fnamelen.
19106 * Returns valid flags.
19107 * When there is an error, *fnamep is set to NULL.
19108 */
19109 int
19110modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19111 char_u *src; /* string with modifiers */
19112 int *usedlen; /* characters after src that are used */
19113 char_u **fnamep; /* file name so far */
19114 char_u **bufp; /* buffer for allocated file name or NULL */
19115 int *fnamelen; /* length of fnamep */
19116{
19117 int valid = 0;
19118 char_u *tail;
19119 char_u *s, *p, *pbuf;
19120 char_u dirname[MAXPATHL];
19121 int c;
19122 int has_fullname = 0;
19123#ifdef WIN3264
19124 int has_shortname = 0;
19125#endif
19126
19127repeat:
19128 /* ":p" - full path/file_name */
19129 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19130 {
19131 has_fullname = 1;
19132
19133 valid |= VALID_PATH;
19134 *usedlen += 2;
19135
19136 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19137 if ((*fnamep)[0] == '~'
19138#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19139 && ((*fnamep)[1] == '/'
19140# ifdef BACKSLASH_IN_FILENAME
19141 || (*fnamep)[1] == '\\'
19142# endif
19143 || (*fnamep)[1] == NUL)
19144
19145#endif
19146 )
19147 {
19148 *fnamep = expand_env_save(*fnamep);
19149 vim_free(*bufp); /* free any allocated file name */
19150 *bufp = *fnamep;
19151 if (*fnamep == NULL)
19152 return -1;
19153 }
19154
19155 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019156 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 {
19158 if (vim_ispathsep(*p)
19159 && p[1] == '.'
19160 && (p[2] == NUL
19161 || vim_ispathsep(p[2])
19162 || (p[2] == '.'
19163 && (p[3] == NUL || vim_ispathsep(p[3])))))
19164 break;
19165 }
19166
19167 /* FullName_save() is slow, don't use it when not needed. */
19168 if (*p != NUL || !vim_isAbsName(*fnamep))
19169 {
19170 *fnamep = FullName_save(*fnamep, *p != NUL);
19171 vim_free(*bufp); /* free any allocated file name */
19172 *bufp = *fnamep;
19173 if (*fnamep == NULL)
19174 return -1;
19175 }
19176
19177 /* Append a path separator to a directory. */
19178 if (mch_isdir(*fnamep))
19179 {
19180 /* Make room for one or two extra characters. */
19181 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19182 vim_free(*bufp); /* free any allocated file name */
19183 *bufp = *fnamep;
19184 if (*fnamep == NULL)
19185 return -1;
19186 add_pathsep(*fnamep);
19187 }
19188 }
19189
19190 /* ":." - path relative to the current directory */
19191 /* ":~" - path relative to the home directory */
19192 /* ":8" - shortname path - postponed till after */
19193 while (src[*usedlen] == ':'
19194 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19195 {
19196 *usedlen += 2;
19197 if (c == '8')
19198 {
19199#ifdef WIN3264
19200 has_shortname = 1; /* Postpone this. */
19201#endif
19202 continue;
19203 }
19204 pbuf = NULL;
19205 /* Need full path first (use expand_env() to remove a "~/") */
19206 if (!has_fullname)
19207 {
19208 if (c == '.' && **fnamep == '~')
19209 p = pbuf = expand_env_save(*fnamep);
19210 else
19211 p = pbuf = FullName_save(*fnamep, FALSE);
19212 }
19213 else
19214 p = *fnamep;
19215
19216 has_fullname = 0;
19217
19218 if (p != NULL)
19219 {
19220 if (c == '.')
19221 {
19222 mch_dirname(dirname, MAXPATHL);
19223 s = shorten_fname(p, dirname);
19224 if (s != NULL)
19225 {
19226 *fnamep = s;
19227 if (pbuf != NULL)
19228 {
19229 vim_free(*bufp); /* free any allocated file name */
19230 *bufp = pbuf;
19231 pbuf = NULL;
19232 }
19233 }
19234 }
19235 else
19236 {
19237 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19238 /* Only replace it when it starts with '~' */
19239 if (*dirname == '~')
19240 {
19241 s = vim_strsave(dirname);
19242 if (s != NULL)
19243 {
19244 *fnamep = s;
19245 vim_free(*bufp);
19246 *bufp = s;
19247 }
19248 }
19249 }
19250 vim_free(pbuf);
19251 }
19252 }
19253
19254 tail = gettail(*fnamep);
19255 *fnamelen = (int)STRLEN(*fnamep);
19256
19257 /* ":h" - head, remove "/file_name", can be repeated */
19258 /* Don't remove the first "/" or "c:\" */
19259 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19260 {
19261 valid |= VALID_HEAD;
19262 *usedlen += 2;
19263 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019264 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265 --tail;
19266 *fnamelen = (int)(tail - *fnamep);
19267#ifdef VMS
19268 if (*fnamelen > 0)
19269 *fnamelen += 1; /* the path separator is part of the path */
19270#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019271 while (tail > s && !after_pathsep(s, tail))
19272 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273 }
19274
19275 /* ":8" - shortname */
19276 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19277 {
19278 *usedlen += 2;
19279#ifdef WIN3264
19280 has_shortname = 1;
19281#endif
19282 }
19283
19284#ifdef WIN3264
19285 /* Check shortname after we have done 'heads' and before we do 'tails'
19286 */
19287 if (has_shortname)
19288 {
19289 pbuf = NULL;
19290 /* Copy the string if it is shortened by :h */
19291 if (*fnamelen < (int)STRLEN(*fnamep))
19292 {
19293 p = vim_strnsave(*fnamep, *fnamelen);
19294 if (p == 0)
19295 return -1;
19296 vim_free(*bufp);
19297 *bufp = *fnamep = p;
19298 }
19299
19300 /* Split into two implementations - makes it easier. First is where
19301 * there isn't a full name already, second is where there is.
19302 */
19303 if (!has_fullname && !vim_isAbsName(*fnamep))
19304 {
19305 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19306 return -1;
19307 }
19308 else
19309 {
19310 int l;
19311
19312 /* Simple case, already have the full-name
19313 * Nearly always shorter, so try first time. */
19314 l = *fnamelen;
19315 if (!get_short_pathname(fnamep, bufp, &l))
19316 return -1;
19317
19318 if (l == 0)
19319 {
19320 /* Couldn't find the filename.. search the paths.
19321 */
19322 l = *fnamelen;
19323 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19324 return -1;
19325 }
19326 *fnamelen = l;
19327 }
19328 }
19329#endif /* WIN3264 */
19330
19331 /* ":t" - tail, just the basename */
19332 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19333 {
19334 *usedlen += 2;
19335 *fnamelen -= (int)(tail - *fnamep);
19336 *fnamep = tail;
19337 }
19338
19339 /* ":e" - extension, can be repeated */
19340 /* ":r" - root, without extension, can be repeated */
19341 while (src[*usedlen] == ':'
19342 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19343 {
19344 /* find a '.' in the tail:
19345 * - for second :e: before the current fname
19346 * - otherwise: The last '.'
19347 */
19348 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19349 s = *fnamep - 2;
19350 else
19351 s = *fnamep + *fnamelen - 1;
19352 for ( ; s > tail; --s)
19353 if (s[0] == '.')
19354 break;
19355 if (src[*usedlen + 1] == 'e') /* :e */
19356 {
19357 if (s > tail)
19358 {
19359 *fnamelen += (int)(*fnamep - (s + 1));
19360 *fnamep = s + 1;
19361#ifdef VMS
19362 /* cut version from the extension */
19363 s = *fnamep + *fnamelen - 1;
19364 for ( ; s > *fnamep; --s)
19365 if (s[0] == ';')
19366 break;
19367 if (s > *fnamep)
19368 *fnamelen = s - *fnamep;
19369#endif
19370 }
19371 else if (*fnamep <= tail)
19372 *fnamelen = 0;
19373 }
19374 else /* :r */
19375 {
19376 if (s > tail) /* remove one extension */
19377 *fnamelen = (int)(s - *fnamep);
19378 }
19379 *usedlen += 2;
19380 }
19381
19382 /* ":s?pat?foo?" - substitute */
19383 /* ":gs?pat?foo?" - global substitute */
19384 if (src[*usedlen] == ':'
19385 && (src[*usedlen + 1] == 's'
19386 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19387 {
19388 char_u *str;
19389 char_u *pat;
19390 char_u *sub;
19391 int sep;
19392 char_u *flags;
19393 int didit = FALSE;
19394
19395 flags = (char_u *)"";
19396 s = src + *usedlen + 2;
19397 if (src[*usedlen + 1] == 'g')
19398 {
19399 flags = (char_u *)"g";
19400 ++s;
19401 }
19402
19403 sep = *s++;
19404 if (sep)
19405 {
19406 /* find end of pattern */
19407 p = vim_strchr(s, sep);
19408 if (p != NULL)
19409 {
19410 pat = vim_strnsave(s, (int)(p - s));
19411 if (pat != NULL)
19412 {
19413 s = p + 1;
19414 /* find end of substitution */
19415 p = vim_strchr(s, sep);
19416 if (p != NULL)
19417 {
19418 sub = vim_strnsave(s, (int)(p - s));
19419 str = vim_strnsave(*fnamep, *fnamelen);
19420 if (sub != NULL && str != NULL)
19421 {
19422 *usedlen = (int)(p + 1 - src);
19423 s = do_string_sub(str, pat, sub, flags);
19424 if (s != NULL)
19425 {
19426 *fnamep = s;
19427 *fnamelen = (int)STRLEN(s);
19428 vim_free(*bufp);
19429 *bufp = s;
19430 didit = TRUE;
19431 }
19432 }
19433 vim_free(sub);
19434 vim_free(str);
19435 }
19436 vim_free(pat);
19437 }
19438 }
19439 /* after using ":s", repeat all the modifiers */
19440 if (didit)
19441 goto repeat;
19442 }
19443 }
19444
19445 return valid;
19446}
19447
19448/*
19449 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19450 * "flags" can be "g" to do a global substitute.
19451 * Returns an allocated string, NULL for error.
19452 */
19453 char_u *
19454do_string_sub(str, pat, sub, flags)
19455 char_u *str;
19456 char_u *pat;
19457 char_u *sub;
19458 char_u *flags;
19459{
19460 int sublen;
19461 regmatch_T regmatch;
19462 int i;
19463 int do_all;
19464 char_u *tail;
19465 garray_T ga;
19466 char_u *ret;
19467 char_u *save_cpo;
19468
19469 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19470 save_cpo = p_cpo;
19471 p_cpo = (char_u *)"";
19472
19473 ga_init2(&ga, 1, 200);
19474
19475 do_all = (flags[0] == 'g');
19476
19477 regmatch.rm_ic = p_ic;
19478 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19479 if (regmatch.regprog != NULL)
19480 {
19481 tail = str;
19482 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19483 {
19484 /*
19485 * Get some space for a temporary buffer to do the substitution
19486 * into. It will contain:
19487 * - The text up to where the match is.
19488 * - The substituted text.
19489 * - The text after the match.
19490 */
19491 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19492 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19493 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19494 {
19495 ga_clear(&ga);
19496 break;
19497 }
19498
19499 /* copy the text up to where the match is */
19500 i = (int)(regmatch.startp[0] - tail);
19501 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19502 /* add the substituted text */
19503 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19504 + ga.ga_len + i, TRUE, TRUE, FALSE);
19505 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506 /* avoid getting stuck on a match with an empty string */
19507 if (tail == regmatch.endp[0])
19508 {
19509 if (*tail == NUL)
19510 break;
19511 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19512 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513 }
19514 else
19515 {
19516 tail = regmatch.endp[0];
19517 if (*tail == NUL)
19518 break;
19519 }
19520 if (!do_all)
19521 break;
19522 }
19523
19524 if (ga.ga_data != NULL)
19525 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19526
19527 vim_free(regmatch.regprog);
19528 }
19529
19530 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19531 ga_clear(&ga);
19532 p_cpo = save_cpo;
19533
19534 return ret;
19535}
19536
19537#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */