blob: a1404c22434f8e0f038bc27c8f2a859cd23c2488 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000340};
341
342/* shorthand */
343#define vv_type vv_di.di_tv.v_type
344#define vv_nr vv_di.di_tv.vval.v_number
345#define vv_str vv_di.di_tv.vval.v_string
346#define vv_tv vv_di.di_tv
347
348/*
349 * The v: variables are stored in dictionary "vimvardict".
350 * "vimvars_var" is the variable that is used for the "l:" scope.
351 */
352static dict_T vimvardict;
353static dictitem_T vimvars_var;
354#define vimvarht vimvardict.dv_hashtab
355
Bram Moolenaara40058a2005-07-11 22:42:07 +0000356static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
357static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
358#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
359static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
360#endif
361static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
362static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
363static char_u *skip_var_one __ARGS((char_u *arg));
364static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
365static void list_glob_vars __ARGS((void));
366static void list_buf_vars __ARGS((void));
367static void list_win_vars __ARGS((void));
368static void list_vim_vars __ARGS((void));
369static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
370static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
371static int check_changedtick __ARGS((char_u *arg));
372static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
373static void clear_lval __ARGS((lval_T *lp));
374static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
375static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
376static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
377static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
378static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
379static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
380static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
381static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
382static void item_lock __ARGS((typval_T *tv, int deep, int lock));
383static int tv_islocked __ARGS((typval_T *tv));
384
Bram Moolenaar33570922005-01-25 22:26:29 +0000385static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
386static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
387static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
388static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
389static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
390static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000394static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000395static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
397static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
399static list_T *list_alloc __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000400static void list_free __ARGS((list_T *l));
401static listitem_T *listitem_alloc __ARGS((void));
402static void listitem_free __ARGS((listitem_T *item));
403static void listitem_remove __ARGS((list_T *l, listitem_T *item));
404static long list_len __ARGS((list_T *l));
405static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
406static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
407static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000408static listitem_T *list_find __ARGS((list_T *l, long n));
409static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static void list_append __ARGS((list_T *l, listitem_T *item));
411static int list_append_tv __ARGS((list_T *l, typval_T *tv));
412static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
413static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
414static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000415static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
417static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000418static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000419static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
420static void set_ref_in_list __ARGS((list_T *l, int copyID));
421static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static void dict_unref __ARGS((dict_T *d));
423static void dict_free __ARGS((dict_T *d));
424static dictitem_T *dictitem_alloc __ARGS((char_u *key));
425static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
426static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
427static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000428static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static int dict_add __ARGS((dict_T *d, dictitem_T *item));
430static long dict_len __ARGS((dict_T *d));
431static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
432static char_u *dict2string __ARGS((typval_T *tv));
433static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
435static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
436static char_u *string_quote __ARGS((char_u *str, int function));
437static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
438static int find_internal_func __ARGS((char_u *name));
439static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
440static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
441static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000442static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443
444static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
468static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000494static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000508static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000536static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000553static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000554static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000557#ifdef vim_mkdir
558static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
559#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000565static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000583static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000584static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000588static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000589static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
592#ifdef HAVE_STRFTIME
593static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
594#endif
595static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000607static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000623static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624
Bram Moolenaar33570922005-01-25 22:26:29 +0000625static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
626static int get_env_len __ARGS((char_u **arg));
627static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000628static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000629static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
630#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
631#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
632 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000633static 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 +0000634static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000635static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000636static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
637static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static typval_T *alloc_tv __ARGS((void));
639static typval_T *alloc_string_tv __ARGS((char_u *string));
640static void free_tv __ARGS((typval_T *varp));
641static void clear_tv __ARGS((typval_T *varp));
642static void init_tv __ARGS((typval_T *varp));
643static long get_tv_number __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000644static long get_tv_number_chk __ARGS((typval_T *varp, int *denote));
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
646static char_u *get_tv_string __ARGS((typval_T *varp));
647static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000648static char_u *get_tv_string_chk __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000649static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000651static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
653static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
654static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
655static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
656static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
657static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
658static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000659static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000661static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
663static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
664static int eval_fname_script __ARGS((char_u *p));
665static int eval_fname_sid __ARGS((char_u *p));
666static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static ufunc_T *find_func __ARGS((char_u *name));
668static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000669static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000670#ifdef FEAT_PROFILE
671static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000672static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
673static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
674static int
675# ifdef __BORLANDC__
676 _RTLENTRYF
677# endif
678 prof_total_cmp __ARGS((const void *s1, const void *s2));
679static int
680# ifdef __BORLANDC__
681 _RTLENTRYF
682# endif
683 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000684#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000685static int script_autoload __ARGS((char_u *name));
686static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000687static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void func_free __ARGS((ufunc_T *fp));
689static void func_unref __ARGS((char_u *name));
690static void func_ref __ARGS((char_u *name));
691static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
692static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
693
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000694/* Character used as separated in autoload function/variable names. */
695#define AUTOLOAD_CHAR '#'
696
Bram Moolenaar33570922005-01-25 22:26:29 +0000697/*
698 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000699 */
700 void
701eval_init()
702{
Bram Moolenaar33570922005-01-25 22:26:29 +0000703 int i;
704 struct vimvar *p;
705
706 init_var_dict(&globvardict, &globvars_var);
707 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000708 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000709 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000710
711 for (i = 0; i < VV_LEN; ++i)
712 {
713 p = &vimvars[i];
714 STRCPY(p->vv_di.di_key, p->vv_name);
715 if (p->vv_flags & VV_RO)
716 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
717 else if (p->vv_flags & VV_RO_SBX)
718 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
719 else
720 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000721
722 /* add to v: scope dict, unless the value is not always available */
723 if (p->vv_type != VAR_UNKNOWN)
724 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000725 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000726 /* add to compat scope dict */
727 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000728 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000729}
730
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000731#if defined(EXITFREE) || defined(PROTO)
732 void
733eval_clear()
734{
735 int i;
736 struct vimvar *p;
737
738 for (i = 0; i < VV_LEN; ++i)
739 {
740 p = &vimvars[i];
741 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000742 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000743 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000744 p->vv_di.di_tv.vval.v_string = NULL;
745 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000746 }
747 hash_clear(&vimvarht);
748 hash_clear(&compat_hashtab);
749
750 /* script-local variables */
751 for (i = 1; i <= ga_scripts.ga_len; ++i)
752 vars_clear(&SCRIPT_VARS(i));
753 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000754 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000755
756 /* global variables */
757 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000758
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000759 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000760 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000761 hash_clear(&func_hashtab);
762
763 /* unreferenced lists and dicts */
764 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000765}
766#endif
767
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 * Return the name of the executed function.
770 */
771 char_u *
772func_name(cookie)
773 void *cookie;
774{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000775 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776}
777
778/*
779 * Return the address holding the next breakpoint line for a funccall cookie.
780 */
781 linenr_T *
782func_breakpoint(cookie)
783 void *cookie;
784{
Bram Moolenaar33570922005-01-25 22:26:29 +0000785 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786}
787
788/*
789 * Return the address holding the debug tick for a funccall cookie.
790 */
791 int *
792func_dbg_tick(cookie)
793 void *cookie;
794{
Bram Moolenaar33570922005-01-25 22:26:29 +0000795 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797
798/*
799 * Return the nesting level for a funccall cookie.
800 */
801 int
802func_level(cookie)
803 void *cookie;
804{
Bram Moolenaar33570922005-01-25 22:26:29 +0000805 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806}
807
808/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000809funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
811/*
812 * Return TRUE when a function was ended by a ":return" command.
813 */
814 int
815current_func_returned()
816{
817 return current_funccal->returned;
818}
819
820
821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 * Set an internal variable to a string value. Creates the variable if it does
823 * not already exist.
824 */
825 void
826set_internal_string_var(name, value)
827 char_u *name;
828 char_u *value;
829{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000830 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000831 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
833 val = vim_strsave(value);
834 if (val != NULL)
835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000836 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000837 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000839 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000840 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 }
842 }
843}
844
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000845static lval_T *redir_lval = NULL;
846static char_u *redir_endp = NULL;
847static char_u *redir_varname = NULL;
848
849/*
850 * Start recording command output to a variable
851 * Returns OK if successfully completed the setup. FAIL otherwise.
852 */
853 int
854var_redir_start(name, append)
855 char_u *name;
856 int append; /* append to an existing variable */
857{
858 int save_emsg;
859 int err;
860 typval_T tv;
861
862 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000863 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000864 {
865 EMSG(_(e_invarg));
866 return FAIL;
867 }
868
869 redir_varname = vim_strsave(name);
870 if (redir_varname == NULL)
871 return FAIL;
872
873 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
874 if (redir_lval == NULL)
875 {
876 var_redir_stop();
877 return FAIL;
878 }
879
880 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000881 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
882 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000883 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
884 {
885 if (redir_endp != NULL && *redir_endp != NUL)
886 /* Trailing characters are present after the variable name */
887 EMSG(_(e_trailing));
888 else
889 EMSG(_(e_invarg));
890 var_redir_stop();
891 return FAIL;
892 }
893
894 /* check if we can write to the variable: set it to or append an empty
895 * string */
896 save_emsg = did_emsg;
897 did_emsg = FALSE;
898 tv.v_type = VAR_STRING;
899 tv.vval.v_string = (char_u *)"";
900 if (append)
901 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
902 else
903 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
904 err = did_emsg;
905 did_emsg += save_emsg;
906 if (err)
907 {
908 var_redir_stop();
909 return FAIL;
910 }
911 if (redir_lval->ll_newkey != NULL)
912 {
913 /* Dictionary item was created, don't do it again. */
914 vim_free(redir_lval->ll_newkey);
915 redir_lval->ll_newkey = NULL;
916 }
917
918 return OK;
919}
920
921/*
922 * Append "value[len]" to the variable set by var_redir_start().
923 */
924 void
925var_redir_str(value, len)
926 char_u *value;
927 int len;
928{
929 char_u *val;
930 typval_T tv;
931 int save_emsg;
932 int err;
933
934 if (redir_lval == NULL)
935 return;
936
937 if (len == -1)
938 /* Append the entire string */
939 val = vim_strsave(value);
940 else
941 /* Append only the specified number of characters */
942 val = vim_strnsave(value, len);
943 if (val == NULL)
944 return;
945
946 tv.v_type = VAR_STRING;
947 tv.vval.v_string = val;
948
949 save_emsg = did_emsg;
950 did_emsg = FALSE;
951 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
952 err = did_emsg;
953 did_emsg += save_emsg;
954 if (err)
955 var_redir_stop();
956
957 vim_free(tv.vval.v_string);
958}
959
960/*
961 * Stop redirecting command output to a variable.
962 */
963 void
964var_redir_stop()
965{
966 if (redir_lval != NULL)
967 {
968 clear_lval(redir_lval);
969 vim_free(redir_lval);
970 redir_lval = NULL;
971 }
972 vim_free(redir_varname);
973 redir_varname = NULL;
974}
975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976# if defined(FEAT_MBYTE) || defined(PROTO)
977 int
978eval_charconvert(enc_from, enc_to, fname_from, fname_to)
979 char_u *enc_from;
980 char_u *enc_to;
981 char_u *fname_from;
982 char_u *fname_to;
983{
984 int err = FALSE;
985
986 set_vim_var_string(VV_CC_FROM, enc_from, -1);
987 set_vim_var_string(VV_CC_TO, enc_to, -1);
988 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
989 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
990 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
991 err = TRUE;
992 set_vim_var_string(VV_CC_FROM, NULL, -1);
993 set_vim_var_string(VV_CC_TO, NULL, -1);
994 set_vim_var_string(VV_FNAME_IN, NULL, -1);
995 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
996
997 if (err)
998 return FAIL;
999 return OK;
1000}
1001# endif
1002
1003# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1004 int
1005eval_printexpr(fname, args)
1006 char_u *fname;
1007 char_u *args;
1008{
1009 int err = FALSE;
1010
1011 set_vim_var_string(VV_FNAME_IN, fname, -1);
1012 set_vim_var_string(VV_CMDARG, args, -1);
1013 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1014 err = TRUE;
1015 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1016 set_vim_var_string(VV_CMDARG, NULL, -1);
1017
1018 if (err)
1019 {
1020 mch_remove(fname);
1021 return FAIL;
1022 }
1023 return OK;
1024}
1025# endif
1026
1027# if defined(FEAT_DIFF) || defined(PROTO)
1028 void
1029eval_diff(origfile, newfile, outfile)
1030 char_u *origfile;
1031 char_u *newfile;
1032 char_u *outfile;
1033{
1034 int err = FALSE;
1035
1036 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1037 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1038 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1039 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1040 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1041 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1042 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1043}
1044
1045 void
1046eval_patch(origfile, difffile, outfile)
1047 char_u *origfile;
1048 char_u *difffile;
1049 char_u *outfile;
1050{
1051 int err;
1052
1053 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1054 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1055 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1056 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1057 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1058 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1059 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1060}
1061# endif
1062
1063/*
1064 * Top level evaluation function, returning a boolean.
1065 * Sets "error" to TRUE if there was an error.
1066 * Return TRUE or FALSE.
1067 */
1068 int
1069eval_to_bool(arg, error, nextcmd, skip)
1070 char_u *arg;
1071 int *error;
1072 char_u **nextcmd;
1073 int skip; /* only parse, don't execute */
1074{
Bram Moolenaar33570922005-01-25 22:26:29 +00001075 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 int retval = FALSE;
1077
1078 if (skip)
1079 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001080 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 else
1083 {
1084 *error = FALSE;
1085 if (!skip)
1086 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001087 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001088 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090 }
1091 if (skip)
1092 --emsg_skip;
1093
1094 return retval;
1095}
1096
1097/*
1098 * Top level evaluation function, returning a string. If "skip" is TRUE,
1099 * only parsing to "nextcmd" is done, without reporting errors. Return
1100 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1101 */
1102 char_u *
1103eval_to_string_skip(arg, nextcmd, skip)
1104 char_u *arg;
1105 char_u **nextcmd;
1106 int skip; /* only parse, don't execute */
1107{
Bram Moolenaar33570922005-01-25 22:26:29 +00001108 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 char_u *retval;
1110
1111 if (skip)
1112 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001113 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 retval = NULL;
1115 else
1116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001117 retval = vim_strsave(get_tv_string(&tv));
1118 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
1120 if (skip)
1121 --emsg_skip;
1122
1123 return retval;
1124}
1125
1126/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001127 * Skip over an expression at "*pp".
1128 * Return FAIL for an error, OK otherwise.
1129 */
1130 int
1131skip_expr(pp)
1132 char_u **pp;
1133{
Bram Moolenaar33570922005-01-25 22:26:29 +00001134 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001135
1136 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001137 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001138}
1139
1140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 * Top level evaluation function, returning a string.
1142 * Return pointer to allocated memory, or NULL for failure.
1143 */
1144 char_u *
1145eval_to_string(arg, nextcmd)
1146 char_u *arg;
1147 char_u **nextcmd;
1148{
Bram Moolenaar33570922005-01-25 22:26:29 +00001149 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 char_u *retval;
1151
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001152 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 retval = NULL;
1154 else
1155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001156 retval = vim_strsave(get_tv_string(&tv));
1157 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 }
1159
1160 return retval;
1161}
1162
1163/*
1164 * Call eval_to_string() with "sandbox" set and not using local variables.
1165 */
1166 char_u *
1167eval_to_string_safe(arg, nextcmd)
1168 char_u *arg;
1169 char_u **nextcmd;
1170{
1171 char_u *retval;
1172 void *save_funccalp;
1173
1174 save_funccalp = save_funccal();
1175 ++sandbox;
1176 retval = eval_to_string(arg, nextcmd);
1177 --sandbox;
1178 restore_funccal(save_funccalp);
1179 return retval;
1180}
1181
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182/*
1183 * Top level evaluation function, returning a number.
1184 * Evaluates "expr" silently.
1185 * Returns -1 for an error.
1186 */
1187 int
1188eval_to_number(expr)
1189 char_u *expr;
1190{
Bram Moolenaar33570922005-01-25 22:26:29 +00001191 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001193 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
1195 ++emsg_off;
1196
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001197 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 retval = -1;
1199 else
1200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001201 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 }
1204 --emsg_off;
1205
1206 return retval;
1207}
1208
Bram Moolenaara40058a2005-07-11 22:42:07 +00001209/*
1210 * Prepare v: variable "idx" to be used.
1211 * Save the current typeval in "save_tv".
1212 * When not used yet add the variable to the v: hashtable.
1213 */
1214 static void
1215prepare_vimvar(idx, save_tv)
1216 int idx;
1217 typval_T *save_tv;
1218{
1219 *save_tv = vimvars[idx].vv_tv;
1220 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1221 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1222}
1223
1224/*
1225 * Restore v: variable "idx" to typeval "save_tv".
1226 * When no longer defined, remove the variable from the v: hashtable.
1227 */
1228 static void
1229restore_vimvar(idx, save_tv)
1230 int idx;
1231 typval_T *save_tv;
1232{
1233 hashitem_T *hi;
1234
1235 clear_tv(&vimvars[idx].vv_tv);
1236 vimvars[idx].vv_tv = *save_tv;
1237 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1238 {
1239 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1240 if (HASHITEM_EMPTY(hi))
1241 EMSG2(_(e_intern2), "restore_vimvar()");
1242 else
1243 hash_remove(&vimvarht, hi);
1244 }
1245}
1246
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001247#if defined(FEAT_SYN_HL) || defined(PROTO)
1248/*
1249 * Evaluate an expression to a list with suggestions.
1250 * For the "expr:" part of 'spellsuggest'.
1251 */
1252 list_T *
1253eval_spell_expr(badword, expr)
1254 char_u *badword;
1255 char_u *expr;
1256{
1257 typval_T save_val;
1258 typval_T rettv;
1259 list_T *list = NULL;
1260 char_u *p = skipwhite(expr);
1261
1262 /* Set "v:val" to the bad word. */
1263 prepare_vimvar(VV_VAL, &save_val);
1264 vimvars[VV_VAL].vv_type = VAR_STRING;
1265 vimvars[VV_VAL].vv_str = badword;
1266 if (p_verbose == 0)
1267 ++emsg_off;
1268
1269 if (eval1(&p, &rettv, TRUE) == OK)
1270 {
1271 if (rettv.v_type != VAR_LIST)
1272 clear_tv(&rettv);
1273 else
1274 list = rettv.vval.v_list;
1275 }
1276
1277 if (p_verbose == 0)
1278 --emsg_off;
1279 vimvars[VV_VAL].vv_str = NULL;
1280 restore_vimvar(VV_VAL, &save_val);
1281
1282 return list;
1283}
1284
1285/*
1286 * "list" is supposed to contain two items: a word and a number. Return the
1287 * word in "pp" and the number as the return value.
1288 * Return -1 if anything isn't right.
1289 * Used to get the good word and score from the eval_spell_expr() result.
1290 */
1291 int
1292get_spellword(list, pp)
1293 list_T *list;
1294 char_u **pp;
1295{
1296 listitem_T *li;
1297
1298 li = list->lv_first;
1299 if (li == NULL)
1300 return -1;
1301 *pp = get_tv_string(&li->li_tv);
1302
1303 li = li->li_next;
1304 if (li == NULL)
1305 return -1;
1306 return get_tv_number(&li->li_tv);
1307}
1308#endif
1309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1311/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001312 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001314 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001316 static int
1317call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 char_u *func;
1319 int argc;
1320 char_u **argv;
1321 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
Bram Moolenaar33570922005-01-25 22:26:29 +00001324 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 long n;
1326 int len;
1327 int i;
1328 int doesrange;
1329 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001330 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331
Bram Moolenaar33570922005-01-25 22:26:29 +00001332 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001334 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
1336 for (i = 0; i < argc; i++)
1337 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001338 /* Pass a NULL or empty argument as an empty string */
1339 if (argv[i] == NULL || *argv[i] == NUL)
1340 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001341 argvars[i].v_type = VAR_STRING;
1342 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001343 continue;
1344 }
1345
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 /* Recognize a number argument, the others must be strings. */
1347 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1348 if (len != 0 && len == (int)STRLEN(argv[i]))
1349 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001350 argvars[i].v_type = VAR_NUMBER;
1351 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353 else
1354 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001355 argvars[i].v_type = VAR_STRING;
1356 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 }
1358 }
1359
1360 if (safe)
1361 {
1362 save_funccalp = save_funccal();
1363 ++sandbox;
1364 }
1365
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001366 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1367 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001369 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 if (safe)
1371 {
1372 --sandbox;
1373 restore_funccal(save_funccalp);
1374 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001375 vim_free(argvars);
1376
1377 if (ret == FAIL)
1378 clear_tv(rettv);
1379
1380 return ret;
1381}
1382
1383/*
1384 * Call some vimL function and return the result as a string
1385 * Uses argv[argc] for the function arguments.
1386 */
1387 void *
1388call_func_retstr(func, argc, argv, safe)
1389 char_u *func;
1390 int argc;
1391 char_u **argv;
1392 int safe; /* use the sandbox */
1393{
1394 typval_T rettv;
1395 char_u *retval = NULL;
1396
1397 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1398 return NULL;
1399
1400 retval = vim_strsave(get_tv_string(&rettv));
1401
1402 clear_tv(&rettv);
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 return retval;
1405}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001406
1407/*
1408 * Call some vimL function and return the result as a list
1409 * Uses argv[argc] for the function arguments.
1410 */
1411 void *
1412call_func_retlist(func, argc, argv, safe)
1413 char_u *func;
1414 int argc;
1415 char_u **argv;
1416 int safe; /* use the sandbox */
1417{
1418 typval_T rettv;
1419
1420 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1421 return NULL;
1422
1423 if (rettv.v_type != VAR_LIST)
1424 {
1425 clear_tv(&rettv);
1426 return NULL;
1427 }
1428
1429 return rettv.vval.v_list;
1430}
1431
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432#endif
1433
1434/*
1435 * Save the current function call pointer, and set it to NULL.
1436 * Used when executing autocommands and for ":source".
1437 */
1438 void *
1439save_funccal()
1440{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001441 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 current_funccal = NULL;
1444 return (void *)fc;
1445}
1446
1447 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001448restore_funccal(vfc)
1449 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001451 funccall_T *fc = (funccall_T *)vfc;
1452
1453 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454}
1455
Bram Moolenaar05159a02005-02-26 23:04:13 +00001456#if defined(FEAT_PROFILE) || defined(PROTO)
1457/*
1458 * Prepare profiling for entering a child or something else that is not
1459 * counted for the script/function itself.
1460 * Should always be called in pair with prof_child_exit().
1461 */
1462 void
1463prof_child_enter(tm)
1464 proftime_T *tm; /* place to store waittime */
1465{
1466 funccall_T *fc = current_funccal;
1467
1468 if (fc != NULL && fc->func->uf_profiling)
1469 profile_start(&fc->prof_child);
1470 script_prof_save(tm);
1471}
1472
1473/*
1474 * Take care of time spent in a child.
1475 * Should always be called after prof_child_enter().
1476 */
1477 void
1478prof_child_exit(tm)
1479 proftime_T *tm; /* where waittime was stored */
1480{
1481 funccall_T *fc = current_funccal;
1482
1483 if (fc != NULL && fc->func->uf_profiling)
1484 {
1485 profile_end(&fc->prof_child);
1486 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1487 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1488 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1489 }
1490 script_prof_restore(tm);
1491}
1492#endif
1493
1494
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495#ifdef FEAT_FOLDING
1496/*
1497 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1498 * it in "*cp". Doesn't give error messages.
1499 */
1500 int
1501eval_foldexpr(arg, cp)
1502 char_u *arg;
1503 int *cp;
1504{
Bram Moolenaar33570922005-01-25 22:26:29 +00001505 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 int retval;
1507 char_u *s;
1508
1509 ++emsg_off;
1510 ++sandbox;
1511 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001512 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 retval = 0;
1514 else
1515 {
1516 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001517 if (tv.v_type == VAR_NUMBER)
1518 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001519 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 retval = 0;
1521 else
1522 {
1523 /* If the result is a string, check if there is a non-digit before
1524 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001525 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (!VIM_ISDIGIT(*s) && *s != '-')
1527 *cp = *s++;
1528 retval = atol((char *)s);
1529 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001530 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 }
1532 --emsg_off;
1533 --sandbox;
1534
1535 return retval;
1536}
1537#endif
1538
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001540 * ":let" list all variable values
1541 * ":let var1 var2" list variable values
1542 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001543 * ":let var += expr" assignment command.
1544 * ":let var -= expr" assignment command.
1545 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001546 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 */
1548 void
1549ex_let(eap)
1550 exarg_T *eap;
1551{
1552 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001553 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001554 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001556 int var_count = 0;
1557 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001558 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001560 expr = skip_var_list(arg, &var_count, &semicolon);
1561 if (expr == NULL)
1562 return;
1563 expr = vim_strchr(expr, '=');
1564 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001566 /*
1567 * ":let" without "=": list variables
1568 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001569 if (*arg == '[')
1570 EMSG(_(e_invarg));
1571 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001572 /* ":let var1 var2" */
1573 arg = list_arg_vars(eap, arg);
1574 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001576 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001577 list_glob_vars();
1578 list_buf_vars();
1579 list_win_vars();
1580 list_vim_vars();
1581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 eap->nextcmd = check_nextcmd(arg);
1583 }
1584 else
1585 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001586 op[0] = '=';
1587 op[1] = NUL;
1588 if (expr > arg)
1589 {
1590 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1591 op[0] = expr[-1]; /* +=, -= or .= */
1592 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001593 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 if (eap->skip)
1596 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001597 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (eap->skip)
1599 {
1600 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001601 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 --emsg_skip;
1603 }
1604 else if (i != FAIL)
1605 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001606 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001607 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001608 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610 }
1611}
1612
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001613/*
1614 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1615 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001616 * When "nextchars" is not NULL it points to a string with characters that
1617 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1618 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001619 * Returns OK or FAIL;
1620 */
1621 static int
1622ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1623 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001624 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001625 int copy; /* copy values from "tv", don't move */
1626 int semicolon; /* from skip_var_list() */
1627 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001628 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001629{
1630 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001631 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001633 listitem_T *item;
1634 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001635
1636 if (*arg != '[')
1637 {
1638 /*
1639 * ":let var = expr" or ":for var in list"
1640 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001641 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001642 return FAIL;
1643 return OK;
1644 }
1645
1646 /*
1647 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1648 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001649 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001650 {
1651 EMSG(_(e_listreq));
1652 return FAIL;
1653 }
1654
1655 i = list_len(l);
1656 if (semicolon == 0 && var_count < i)
1657 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001658 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659 return FAIL;
1660 }
1661 if (var_count - semicolon > i)
1662 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001663 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001664 return FAIL;
1665 }
1666
1667 item = l->lv_first;
1668 while (*arg != ']')
1669 {
1670 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001671 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001672 item = item->li_next;
1673 if (arg == NULL)
1674 return FAIL;
1675
1676 arg = skipwhite(arg);
1677 if (*arg == ';')
1678 {
1679 /* Put the rest of the list (may be empty) in the var after ';'.
1680 * Create a new list for this. */
1681 l = list_alloc();
1682 if (l == NULL)
1683 return FAIL;
1684 while (item != NULL)
1685 {
1686 list_append_tv(l, &item->li_tv);
1687 item = item->li_next;
1688 }
1689
1690 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001691 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692 ltv.vval.v_list = l;
1693 l->lv_refcount = 1;
1694
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001695 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1696 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 clear_tv(&ltv);
1698 if (arg == NULL)
1699 return FAIL;
1700 break;
1701 }
1702 else if (*arg != ',' && *arg != ']')
1703 {
1704 EMSG2(_(e_intern2), "ex_let_vars()");
1705 return FAIL;
1706 }
1707 }
1708
1709 return OK;
1710}
1711
1712/*
1713 * Skip over assignable variable "var" or list of variables "[var, var]".
1714 * Used for ":let varvar = expr" and ":for varvar in expr".
1715 * For "[var, var]" increment "*var_count" for each variable.
1716 * for "[var, var; var]" set "semicolon".
1717 * Return NULL for an error.
1718 */
1719 static char_u *
1720skip_var_list(arg, var_count, semicolon)
1721 char_u *arg;
1722 int *var_count;
1723 int *semicolon;
1724{
1725 char_u *p, *s;
1726
1727 if (*arg == '[')
1728 {
1729 /* "[var, var]": find the matching ']'. */
1730 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001731 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001732 {
1733 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1734 s = skip_var_one(p);
1735 if (s == p)
1736 {
1737 EMSG2(_(e_invarg2), p);
1738 return NULL;
1739 }
1740 ++*var_count;
1741
1742 p = skipwhite(s);
1743 if (*p == ']')
1744 break;
1745 else if (*p == ';')
1746 {
1747 if (*semicolon == 1)
1748 {
1749 EMSG(_("Double ; in list of variables"));
1750 return NULL;
1751 }
1752 *semicolon = 1;
1753 }
1754 else if (*p != ',')
1755 {
1756 EMSG2(_(e_invarg2), p);
1757 return NULL;
1758 }
1759 }
1760 return p + 1;
1761 }
1762 else
1763 return skip_var_one(arg);
1764}
1765
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001766/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001767 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1768 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001769 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770 static char_u *
1771skip_var_one(arg)
1772 char_u *arg;
1773{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001774 if (*arg == '@' && arg[1] != NUL)
1775 return arg + 2;
1776 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1777 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778}
1779
Bram Moolenaara7043832005-01-21 11:56:39 +00001780/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001781 * List variables for hashtab "ht" with prefix "prefix".
1782 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001783 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001784 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001785list_hashtable_vars(ht, prefix, empty)
1786 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001787 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001788 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001789{
Bram Moolenaar33570922005-01-25 22:26:29 +00001790 hashitem_T *hi;
1791 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001792 int todo;
1793
1794 todo = ht->ht_used;
1795 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1796 {
1797 if (!HASHITEM_EMPTY(hi))
1798 {
1799 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001800 di = HI2DI(hi);
1801 if (empty || di->di_tv.v_type != VAR_STRING
1802 || di->di_tv.vval.v_string != NULL)
1803 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001804 }
1805 }
1806}
1807
1808/*
1809 * List global variables.
1810 */
1811 static void
1812list_glob_vars()
1813{
Bram Moolenaar33570922005-01-25 22:26:29 +00001814 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001815}
1816
1817/*
1818 * List buffer variables.
1819 */
1820 static void
1821list_buf_vars()
1822{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001823 char_u numbuf[NUMBUFLEN];
1824
Bram Moolenaar33570922005-01-25 22:26:29 +00001825 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001826
1827 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1828 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001829}
1830
1831/*
1832 * List window variables.
1833 */
1834 static void
1835list_win_vars()
1836{
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001838}
1839
1840/*
1841 * List Vim variables.
1842 */
1843 static void
1844list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001845{
Bram Moolenaar33570922005-01-25 22:26:29 +00001846 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847}
1848
1849/*
1850 * List variables in "arg".
1851 */
1852 static char_u *
1853list_arg_vars(eap, arg)
1854 exarg_T *eap;
1855 char_u *arg;
1856{
1857 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001858 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001860 char_u *name_start;
1861 char_u *arg_subsc;
1862 char_u *tofree;
1863 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864
1865 while (!ends_excmd(*arg) && !got_int)
1866 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001867 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001869 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001870 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1871 {
1872 emsg_severe = TRUE;
1873 EMSG(_(e_trailing));
1874 break;
1875 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001877 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001879 /* get_name_len() takes care of expanding curly braces */
1880 name_start = name = arg;
1881 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1882 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001884 /* This is mainly to keep test 49 working: when expanding
1885 * curly braces fails overrule the exception error message. */
1886 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001888 emsg_severe = TRUE;
1889 EMSG2(_(e_invarg2), arg);
1890 break;
1891 }
1892 error = TRUE;
1893 }
1894 else
1895 {
1896 if (tofree != NULL)
1897 name = tofree;
1898 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 else
1901 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001902 /* handle d.key, l[idx], f(expr) */
1903 arg_subsc = arg;
1904 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001905 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001907 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001908 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001909 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001910 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001911 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001912 case 'g': list_glob_vars(); break;
1913 case 'b': list_buf_vars(); break;
1914 case 'w': list_win_vars(); break;
1915 case 'v': list_vim_vars(); break;
1916 default:
1917 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001918 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001919 }
1920 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001921 {
1922 char_u numbuf[NUMBUFLEN];
1923 char_u *tf;
1924 int c;
1925 char_u *s;
1926
1927 s = echo_string(&tv, &tf, numbuf);
1928 c = *arg;
1929 *arg = NUL;
1930 list_one_var_a((char_u *)"",
1931 arg == arg_subsc ? name : name_start,
1932 tv.v_type, s == NULL ? (char_u *)"" : s);
1933 *arg = c;
1934 vim_free(tf);
1935 }
1936 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001937 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 }
1939 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001940
1941 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001943
1944 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 }
1946
1947 return arg;
1948}
1949
1950/*
1951 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1952 * Returns a pointer to the char just after the var name.
1953 * Returns NULL if there is an error.
1954 */
1955 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001958 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001961 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962{
1963 int c1;
1964 char_u *name;
1965 char_u *p;
1966 char_u *arg_end = NULL;
1967 int len;
1968 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001969 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001970
1971 /*
1972 * ":let $VAR = expr": Set environment variable.
1973 */
1974 if (*arg == '$')
1975 {
1976 /* Find the end of the name. */
1977 ++arg;
1978 name = arg;
1979 len = get_env_len(&arg);
1980 if (len == 0)
1981 EMSG2(_(e_invarg2), name - 1);
1982 else
1983 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001984 if (op != NULL && (*op == '+' || *op == '-'))
1985 EMSG2(_(e_letwrong), op);
1986 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988 EMSG(_(e_letunexp));
1989 else
1990 {
1991 c1 = name[len];
1992 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001993 p = get_tv_string_chk(tv);
1994 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 {
1996 int mustfree = FALSE;
1997 char_u *s = vim_getenv(name, &mustfree);
1998
1999 if (s != NULL)
2000 {
2001 p = tofree = concat_str(s, p);
2002 if (mustfree)
2003 vim_free(s);
2004 }
2005 }
2006 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002007 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002008 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002009 if (STRICMP(name, "HOME") == 0)
2010 init_homedir();
2011 else if (didset_vim && STRICMP(name, "VIM") == 0)
2012 didset_vim = FALSE;
2013 else if (didset_vimruntime
2014 && STRICMP(name, "VIMRUNTIME") == 0)
2015 didset_vimruntime = FALSE;
2016 arg_end = arg;
2017 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002019 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020 }
2021 }
2022 }
2023
2024 /*
2025 * ":let &option = expr": Set option value.
2026 * ":let &l:option = expr": Set local option value.
2027 * ":let &g:option = expr": Set global option value.
2028 */
2029 else if (*arg == '&')
2030 {
2031 /* Find the end of the name. */
2032 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002033 if (p == NULL || (endchars != NULL
2034 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002035 EMSG(_(e_letunexp));
2036 else
2037 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002038 long n;
2039 int opt_type;
2040 long numval;
2041 char_u *stringval = NULL;
2042 char_u *s;
2043
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002044 c1 = *p;
2045 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002046
2047 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002048 s = get_tv_string_chk(tv); /* != NULL if number or string */
2049 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002050 {
2051 opt_type = get_option_value(arg, &numval,
2052 &stringval, opt_flags);
2053 if ((opt_type == 1 && *op == '.')
2054 || (opt_type == 0 && *op != '.'))
2055 EMSG2(_(e_letwrong), op);
2056 else
2057 {
2058 if (opt_type == 1) /* number */
2059 {
2060 if (*op == '+')
2061 n = numval + n;
2062 else
2063 n = numval - n;
2064 }
2065 else if (opt_type == 0 && stringval != NULL) /* string */
2066 {
2067 s = concat_str(stringval, s);
2068 vim_free(stringval);
2069 stringval = s;
2070 }
2071 }
2072 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002073 if (s != NULL)
2074 {
2075 set_option_value(arg, n, s, opt_flags);
2076 arg_end = p;
2077 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002079 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002080 }
2081 }
2082
2083 /*
2084 * ":let @r = expr": Set register contents.
2085 */
2086 else if (*arg == '@')
2087 {
2088 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002089 if (op != NULL && (*op == '+' || *op == '-'))
2090 EMSG2(_(e_letwrong), op);
2091 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002092 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002093 EMSG(_(e_letunexp));
2094 else
2095 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002096 char_u *tofree = NULL;
2097 char_u *s;
2098
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002099 p = get_tv_string_chk(tv);
2100 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002101 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002102 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002103 if (s != NULL)
2104 {
2105 p = tofree = concat_str(s, p);
2106 vim_free(s);
2107 }
2108 }
2109 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002110 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002111 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002112 arg_end = arg + 1;
2113 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002114 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002115 }
2116 }
2117
2118 /*
2119 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002121 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002122 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002123 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002124 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002125
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002126 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002127 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002128 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002129 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2130 EMSG(_(e_letunexp));
2131 else
2132 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002133 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002134 arg_end = p;
2135 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002136 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002137 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138 }
2139
2140 else
2141 EMSG2(_(e_invarg2), arg);
2142
2143 return arg_end;
2144}
2145
2146/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002147 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2148 */
2149 static int
2150check_changedtick(arg)
2151 char_u *arg;
2152{
2153 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2154 {
2155 EMSG2(_(e_readonlyvar), arg);
2156 return TRUE;
2157 }
2158 return FALSE;
2159}
2160
2161/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 * Get an lval: variable, Dict item or List item that can be assigned a value
2163 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2164 * "name.key", "name.key[expr]" etc.
2165 * Indexing only works if "name" is an existing List or Dictionary.
2166 * "name" points to the start of the name.
2167 * If "rettv" is not NULL it points to the value to be assigned.
2168 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2169 * wrong; must end in space or cmd separator.
2170 *
2171 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002172 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002173 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174 */
2175 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002176get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002178 typval_T *rettv;
2179 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002180 int unlet;
2181 int skip;
2182 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002183 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 char_u *expr_start, *expr_end;
2187 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002188 dictitem_T *v;
2189 typval_T var1;
2190 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002192 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002193 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002194 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002195 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002198 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002199
2200 if (skip)
2201 {
2202 /* When skipping just find the end of the name. */
2203 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002204 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002205 }
2206
2207 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002208 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209 if (expr_start != NULL)
2210 {
2211 /* Don't expand the name when we already know there is an error. */
2212 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2213 && *p != '[' && *p != '.')
2214 {
2215 EMSG(_(e_trailing));
2216 return NULL;
2217 }
2218
2219 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2220 if (lp->ll_exp_name == NULL)
2221 {
2222 /* Report an invalid expression in braces, unless the
2223 * expression evaluation has been cancelled due to an
2224 * aborting error, an interrupt, or an exception. */
2225 if (!aborting() && !quiet)
2226 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002227 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002228 EMSG2(_(e_invarg2), name);
2229 return NULL;
2230 }
2231 }
2232 lp->ll_name = lp->ll_exp_name;
2233 }
2234 else
2235 lp->ll_name = name;
2236
2237 /* Without [idx] or .key we are done. */
2238 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2239 return p;
2240
2241 cc = *p;
2242 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002244 if (v == NULL && !quiet)
2245 EMSG2(_(e_undefvar), lp->ll_name);
2246 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 if (v == NULL)
2248 return NULL;
2249
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002250 /*
2251 * Loop until no more [idx] or .key is following.
2252 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002253 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2257 && !(lp->ll_tv->v_type == VAR_DICT
2258 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 if (!quiet)
2261 EMSG(_("E689: Can only index a List or Dictionary"));
2262 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002264 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266 if (!quiet)
2267 EMSG(_("E708: [:] must come last"));
2268 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002270
Bram Moolenaar8c711452005-01-14 21:53:12 +00002271 len = -1;
2272 if (*p == '.')
2273 {
2274 key = p + 1;
2275 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2276 ;
2277 if (len == 0)
2278 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279 if (!quiet)
2280 EMSG(_(e_emptykey));
2281 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002282 }
2283 p = key + len;
2284 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002285 else
2286 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002287 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002288 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002289 if (*p == ':')
2290 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002291 else
2292 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002293 empty1 = FALSE;
2294 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002296 if (get_tv_string_chk(&var1) == NULL)
2297 {
2298 /* not a number or string */
2299 clear_tv(&var1);
2300 return NULL;
2301 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002302 }
2303
2304 /* Optionally get the second index [ :expr]. */
2305 if (*p == ':')
2306 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002308 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002309 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002311 if (!empty1)
2312 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002314 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002315 if (rettv != NULL && (rettv->v_type != VAR_LIST
2316 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002317 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002318 if (!quiet)
2319 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002320 if (!empty1)
2321 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002323 }
2324 p = skipwhite(p + 1);
2325 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002326 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002327 else
2328 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002329 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002330 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2331 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002332 if (!empty1)
2333 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002334 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002335 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002336 if (get_tv_string_chk(&var2) == NULL)
2337 {
2338 /* not a number or string */
2339 if (!empty1)
2340 clear_tv(&var1);
2341 clear_tv(&var2);
2342 return NULL;
2343 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002344 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002345 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002346 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002347 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002348 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002349
Bram Moolenaar8c711452005-01-14 21:53:12 +00002350 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002351 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002352 if (!quiet)
2353 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002354 if (!empty1)
2355 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002356 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002357 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002358 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002359 }
2360
2361 /* Skip to past ']'. */
2362 ++p;
2363 }
2364
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002366 {
2367 if (len == -1)
2368 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002370 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002371 if (*key == NUL)
2372 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 if (!quiet)
2374 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002375 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002377 }
2378 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379 lp->ll_list = NULL;
2380 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002381 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002383 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002384 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002385 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002386 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002389 if (len == -1)
2390 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002391 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002392 }
2393 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002395 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002396 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002397 if (len == -1)
2398 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002400 p = NULL;
2401 break;
2402 }
2403 if (len == -1)
2404 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002406 }
2407 else
2408 {
2409 /*
2410 * Get the number and item for the only or first index of the List.
2411 */
2412 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002414 else
2415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002416 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002417 clear_tv(&var1);
2418 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002420 lp->ll_list = lp->ll_tv->vval.v_list;
2421 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2422 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002423 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 if (!quiet)
2425 EMSGN(_(e_listidx), lp->ll_n1);
2426 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002427 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002428 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002429 }
2430
2431 /*
2432 * May need to find the item or absolute index for the second
2433 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 * When no index given: "lp->ll_empty2" is TRUE.
2435 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002436 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002439 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002442 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002444 if (ni == NULL)
2445 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 if (!quiet)
2447 EMSGN(_(e_listidx), lp->ll_n2);
2448 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002451 }
2452
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2454 if (lp->ll_n1 < 0)
2455 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2456 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002457 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 if (!quiet)
2459 EMSGN(_(e_listidx), lp->ll_n2);
2460 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002461 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002462 }
2463
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 }
2467
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 return p;
2469}
2470
2471/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002472 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 */
2474 static void
2475clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002476 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477{
2478 vim_free(lp->ll_exp_name);
2479 vim_free(lp->ll_newkey);
2480}
2481
2482/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002483 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 */
2487 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002489 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002491 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494{
2495 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002496 listitem_T *ni;
2497 listitem_T *ri;
2498 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499
2500 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002501 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002503 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 cc = *endp;
2505 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002506 if (op != NULL && *op != '=')
2507 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002508 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002509
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002510 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002511 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2512 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002513 {
2514 if (tv_op(&tv, rettv, op) == OK)
2515 set_var(lp->ll_name, &tv, FALSE);
2516 clear_tv(&tv);
2517 }
2518 }
2519 else
2520 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002524 else if (tv_check_lock(lp->ll_newkey == NULL
2525 ? lp->ll_tv->v_lock
2526 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2527 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 else if (lp->ll_range)
2529 {
2530 /*
2531 * Assign the List values to the list items.
2532 */
2533 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002534 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002535 if (op != NULL && *op != '=')
2536 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2537 else
2538 {
2539 clear_tv(&lp->ll_li->li_tv);
2540 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2541 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 ri = ri->li_next;
2543 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2544 break;
2545 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002546 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 /* Need to add an empty item. */
2548 ni = listitem_alloc();
2549 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002550 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 ri = NULL;
2552 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002553 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002555 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 ni->li_tv.vval.v_number = 0;
2557 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002558 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 lp->ll_li = lp->ll_li->li_next;
2560 ++lp->ll_n1;
2561 }
2562 if (ri != NULL)
2563 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002564 else if (lp->ll_empty2
2565 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 : lp->ll_n1 != lp->ll_n2)
2567 EMSG(_("E711: List value has not enough items"));
2568 }
2569 else
2570 {
2571 /*
2572 * Assign to a List or Dictionary item.
2573 */
2574 if (lp->ll_newkey != NULL)
2575 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002576 if (op != NULL && *op != '=')
2577 {
2578 EMSG2(_(e_letwrong), op);
2579 return;
2580 }
2581
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002583 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 if (di == NULL)
2585 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002586 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2587 {
2588 vim_free(di);
2589 return;
2590 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002592 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002593 else if (op != NULL && *op != '=')
2594 {
2595 tv_op(lp->ll_tv, rettv, op);
2596 return;
2597 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002598 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002600
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 /*
2602 * Assign the value to the variable or list item.
2603 */
2604 if (copy)
2605 copy_tv(rettv, lp->ll_tv);
2606 else
2607 {
2608 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002609 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002611 }
2612 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002613}
2614
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002615/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002616 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2617 * Returns OK or FAIL.
2618 */
2619 static int
2620tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002621 typval_T *tv1;
2622 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002623 char_u *op;
2624{
2625 long n;
2626 char_u numbuf[NUMBUFLEN];
2627 char_u *s;
2628
2629 /* Can't do anything with a Funcref or a Dict on the right. */
2630 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2631 {
2632 switch (tv1->v_type)
2633 {
2634 case VAR_DICT:
2635 case VAR_FUNC:
2636 break;
2637
2638 case VAR_LIST:
2639 if (*op != '+' || tv2->v_type != VAR_LIST)
2640 break;
2641 /* List += List */
2642 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2643 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2644 return OK;
2645
2646 case VAR_NUMBER:
2647 case VAR_STRING:
2648 if (tv2->v_type == VAR_LIST)
2649 break;
2650 if (*op == '+' || *op == '-')
2651 {
2652 /* nr += nr or nr -= nr*/
2653 n = get_tv_number(tv1);
2654 if (*op == '+')
2655 n += get_tv_number(tv2);
2656 else
2657 n -= get_tv_number(tv2);
2658 clear_tv(tv1);
2659 tv1->v_type = VAR_NUMBER;
2660 tv1->vval.v_number = n;
2661 }
2662 else
2663 {
2664 /* str .= str */
2665 s = get_tv_string(tv1);
2666 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2667 clear_tv(tv1);
2668 tv1->v_type = VAR_STRING;
2669 tv1->vval.v_string = s;
2670 }
2671 return OK;
2672 }
2673 }
2674
2675 EMSG2(_(e_letwrong), op);
2676 return FAIL;
2677}
2678
2679/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002680 * Add a watcher to a list.
2681 */
2682 static void
2683list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002684 list_T *l;
2685 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002686{
2687 lw->lw_next = l->lv_watch;
2688 l->lv_watch = lw;
2689}
2690
2691/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002692 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002693 * No warning when it isn't found...
2694 */
2695 static void
2696list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002697 list_T *l;
2698 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002699{
Bram Moolenaar33570922005-01-25 22:26:29 +00002700 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002701
2702 lwp = &l->lv_watch;
2703 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2704 {
2705 if (lw == lwrem)
2706 {
2707 *lwp = lw->lw_next;
2708 break;
2709 }
2710 lwp = &lw->lw_next;
2711 }
2712}
2713
2714/*
2715 * Just before removing an item from a list: advance watchers to the next
2716 * item.
2717 */
2718 static void
2719list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002720 list_T *l;
2721 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002722{
Bram Moolenaar33570922005-01-25 22:26:29 +00002723 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002724
2725 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2726 if (lw->lw_item == item)
2727 lw->lw_item = item->li_next;
2728}
2729
2730/*
2731 * Evaluate the expression used in a ":for var in expr" command.
2732 * "arg" points to "var".
2733 * Set "*errp" to TRUE for an error, FALSE otherwise;
2734 * Return a pointer that holds the info. Null when there is an error.
2735 */
2736 void *
2737eval_for_line(arg, errp, nextcmdp, skip)
2738 char_u *arg;
2739 int *errp;
2740 char_u **nextcmdp;
2741 int skip;
2742{
Bram Moolenaar33570922005-01-25 22:26:29 +00002743 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002744 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002745 typval_T tv;
2746 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002747
2748 *errp = TRUE; /* default: there is an error */
2749
Bram Moolenaar33570922005-01-25 22:26:29 +00002750 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002751 if (fi == NULL)
2752 return NULL;
2753
2754 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2755 if (expr == NULL)
2756 return fi;
2757
2758 expr = skipwhite(expr);
2759 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2760 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002761 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002762 return fi;
2763 }
2764
2765 if (skip)
2766 ++emsg_skip;
2767 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2768 {
2769 *errp = FALSE;
2770 if (!skip)
2771 {
2772 l = tv.vval.v_list;
2773 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002774 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002775 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002776 clear_tv(&tv);
2777 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002778 else
2779 {
2780 fi->fi_list = l;
2781 list_add_watch(l, &fi->fi_lw);
2782 fi->fi_lw.lw_item = l->lv_first;
2783 }
2784 }
2785 }
2786 if (skip)
2787 --emsg_skip;
2788
2789 return fi;
2790}
2791
2792/*
2793 * Use the first item in a ":for" list. Advance to the next.
2794 * Assign the values to the variable (list). "arg" points to the first one.
2795 * Return TRUE when a valid item was found, FALSE when at end of list or
2796 * something wrong.
2797 */
2798 int
2799next_for_item(fi_void, arg)
2800 void *fi_void;
2801 char_u *arg;
2802{
Bram Moolenaar33570922005-01-25 22:26:29 +00002803 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002804 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002805 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002806
2807 item = fi->fi_lw.lw_item;
2808 if (item == NULL)
2809 result = FALSE;
2810 else
2811 {
2812 fi->fi_lw.lw_item = item->li_next;
2813 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2814 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2815 }
2816 return result;
2817}
2818
2819/*
2820 * Free the structure used to store info used by ":for".
2821 */
2822 void
2823free_for_info(fi_void)
2824 void *fi_void;
2825{
Bram Moolenaar33570922005-01-25 22:26:29 +00002826 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002827
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002828 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002829 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002830 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002831 list_unref(fi->fi_list);
2832 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002833 vim_free(fi);
2834}
2835
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2837
2838 void
2839set_context_for_expression(xp, arg, cmdidx)
2840 expand_T *xp;
2841 char_u *arg;
2842 cmdidx_T cmdidx;
2843{
2844 int got_eq = FALSE;
2845 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002846 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002848 if (cmdidx == CMD_let)
2849 {
2850 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002851 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002852 {
2853 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002854 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002855 {
2856 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002857 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002858 if (vim_iswhite(*p))
2859 break;
2860 }
2861 return;
2862 }
2863 }
2864 else
2865 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2866 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 while ((xp->xp_pattern = vim_strpbrk(arg,
2868 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2869 {
2870 c = *xp->xp_pattern;
2871 if (c == '&')
2872 {
2873 c = xp->xp_pattern[1];
2874 if (c == '&')
2875 {
2876 ++xp->xp_pattern;
2877 xp->xp_context = cmdidx != CMD_let || got_eq
2878 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2879 }
2880 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002883 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2884 xp->xp_pattern += 2;
2885
2886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
2888 else if (c == '$')
2889 {
2890 /* environment variable */
2891 xp->xp_context = EXPAND_ENV_VARS;
2892 }
2893 else if (c == '=')
2894 {
2895 got_eq = TRUE;
2896 xp->xp_context = EXPAND_EXPRESSION;
2897 }
2898 else if (c == '<'
2899 && xp->xp_context == EXPAND_FUNCTIONS
2900 && vim_strchr(xp->xp_pattern, '(') == NULL)
2901 {
2902 /* Function name can start with "<SNR>" */
2903 break;
2904 }
2905 else if (cmdidx != CMD_let || got_eq)
2906 {
2907 if (c == '"') /* string */
2908 {
2909 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2910 if (c == '\\' && xp->xp_pattern[1] != NUL)
2911 ++xp->xp_pattern;
2912 xp->xp_context = EXPAND_NOTHING;
2913 }
2914 else if (c == '\'') /* literal string */
2915 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002916 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2918 /* skip */ ;
2919 xp->xp_context = EXPAND_NOTHING;
2920 }
2921 else if (c == '|')
2922 {
2923 if (xp->xp_pattern[1] == '|')
2924 {
2925 ++xp->xp_pattern;
2926 xp->xp_context = EXPAND_EXPRESSION;
2927 }
2928 else
2929 xp->xp_context = EXPAND_COMMANDS;
2930 }
2931 else
2932 xp->xp_context = EXPAND_EXPRESSION;
2933 }
2934 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002935 /* Doesn't look like something valid, expand as an expression
2936 * anyway. */
2937 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 arg = xp->xp_pattern;
2939 if (*arg != NUL)
2940 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2941 /* skip */ ;
2942 }
2943 xp->xp_pattern = arg;
2944}
2945
2946#endif /* FEAT_CMDL_COMPL */
2947
2948/*
2949 * ":1,25call func(arg1, arg2)" function call.
2950 */
2951 void
2952ex_call(eap)
2953 exarg_T *eap;
2954{
2955 char_u *arg = eap->arg;
2956 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002960 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 linenr_T lnum;
2962 int doesrange;
2963 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002964 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002966 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2967 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002968 if (tofree == NULL)
2969 return;
2970
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002971 /* Increase refcount on dictionary, it could get deleted when evaluating
2972 * the arguments. */
2973 if (fudi.fd_dict != NULL)
2974 ++fudi.fd_dict->dv_refcount;
2975
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002976 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2977 len = STRLEN(tofree);
2978 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979
Bram Moolenaar532c7802005-01-27 14:44:31 +00002980 /* Skip white space to allow ":call func ()". Not good, but required for
2981 * backward compatibility. */
2982 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
2985 if (*startarg != '(')
2986 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00002987 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 goto end;
2989 }
2990
2991 /*
2992 * When skipping, evaluate the function once, to find the end of the
2993 * arguments.
2994 * When the function takes a range, this is discovered after the first
2995 * call, and the loop is broken.
2996 */
2997 if (eap->skip)
2998 {
2999 ++emsg_skip;
3000 lnum = eap->line2; /* do it once, also with an invalid range */
3001 }
3002 else
3003 lnum = eap->line1;
3004 for ( ; lnum <= eap->line2; ++lnum)
3005 {
3006 if (!eap->skip && eap->addr_count > 0)
3007 {
3008 curwin->w_cursor.lnum = lnum;
3009 curwin->w_cursor.col = 0;
3010 }
3011 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003012 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003013 eap->line1, eap->line2, &doesrange,
3014 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 {
3016 failed = TRUE;
3017 break;
3018 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003019 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 if (doesrange || eap->skip)
3021 break;
3022 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003023 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003024 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003025 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 if (aborting())
3027 break;
3028 }
3029 if (eap->skip)
3030 --emsg_skip;
3031
3032 if (!failed)
3033 {
3034 /* Check for trailing illegal characters and a following command. */
3035 if (!ends_excmd(*arg))
3036 {
3037 emsg_severe = TRUE;
3038 EMSG(_(e_trailing));
3039 }
3040 else
3041 eap->nextcmd = check_nextcmd(arg);
3042 }
3043
3044end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003045 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003046 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047}
3048
3049/*
3050 * ":unlet[!] var1 ... " command.
3051 */
3052 void
3053ex_unlet(eap)
3054 exarg_T *eap;
3055{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003056 ex_unletlock(eap, eap->arg, 0);
3057}
3058
3059/*
3060 * ":lockvar" and ":unlockvar" commands
3061 */
3062 void
3063ex_lockvar(eap)
3064 exarg_T *eap;
3065{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003067 int deep = 2;
3068
3069 if (eap->forceit)
3070 deep = -1;
3071 else if (vim_isdigit(*arg))
3072 {
3073 deep = getdigits(&arg);
3074 arg = skipwhite(arg);
3075 }
3076
3077 ex_unletlock(eap, arg, deep);
3078}
3079
3080/*
3081 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3082 */
3083 static void
3084ex_unletlock(eap, argstart, deep)
3085 exarg_T *eap;
3086 char_u *argstart;
3087 int deep;
3088{
3089 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003092 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093
3094 do
3095 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003096 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003097 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3098 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003099 if (lv.ll_name == NULL)
3100 error = TRUE; /* error but continue parsing */
3101 if (name_end == NULL || (!vim_iswhite(*name_end)
3102 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003104 if (name_end != NULL)
3105 {
3106 emsg_severe = TRUE;
3107 EMSG(_(e_trailing));
3108 }
3109 if (!(eap->skip || error))
3110 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 break;
3112 }
3113
3114 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003115 {
3116 if (eap->cmdidx == CMD_unlet)
3117 {
3118 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3119 error = TRUE;
3120 }
3121 else
3122 {
3123 if (do_lock_var(&lv, name_end, deep,
3124 eap->cmdidx == CMD_lockvar) == FAIL)
3125 error = TRUE;
3126 }
3127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003129 if (!eap->skip)
3130 clear_lval(&lv);
3131
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 arg = skipwhite(name_end);
3133 } while (!ends_excmd(*arg));
3134
3135 eap->nextcmd = check_nextcmd(arg);
3136}
3137
Bram Moolenaar8c711452005-01-14 21:53:12 +00003138 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003139do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003140 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003141 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003142 int forceit;
3143{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003144 int ret = OK;
3145 int cc;
3146
3147 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003148 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003149 cc = *name_end;
3150 *name_end = NUL;
3151
3152 /* Normal name or expanded name. */
3153 if (check_changedtick(lp->ll_name))
3154 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003155 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003156 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003157 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003158 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003159 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3160 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003161 else if (lp->ll_range)
3162 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003163 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003164
3165 /* Delete a range of List items. */
3166 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3167 {
3168 li = lp->ll_li->li_next;
3169 listitem_remove(lp->ll_list, lp->ll_li);
3170 lp->ll_li = li;
3171 ++lp->ll_n1;
3172 }
3173 }
3174 else
3175 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003176 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003177 /* unlet a List item. */
3178 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003179 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003180 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003181 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003182 }
3183
3184 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003185}
3186
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187/*
3188 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003189 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 */
3191 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003192do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003194 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195{
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 hashtab_T *ht;
3197 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003198 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
Bram Moolenaar33570922005-01-25 22:26:29 +00003200 ht = find_var_ht(name, &varname);
3201 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003203 hi = hash_find(ht, varname);
3204 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003205 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003206 if (var_check_ro(HI2DI(hi)->di_flags, name))
3207 return FAIL;
3208 delete_var(ht, hi);
3209 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003212 if (forceit)
3213 return OK;
3214 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 return FAIL;
3216}
3217
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003218/*
3219 * Lock or unlock variable indicated by "lp".
3220 * "deep" is the levels to go (-1 for unlimited);
3221 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3222 */
3223 static int
3224do_lock_var(lp, name_end, deep, lock)
3225 lval_T *lp;
3226 char_u *name_end;
3227 int deep;
3228 int lock;
3229{
3230 int ret = OK;
3231 int cc;
3232 dictitem_T *di;
3233
3234 if (deep == 0) /* nothing to do */
3235 return OK;
3236
3237 if (lp->ll_tv == NULL)
3238 {
3239 cc = *name_end;
3240 *name_end = NUL;
3241
3242 /* Normal name or expanded name. */
3243 if (check_changedtick(lp->ll_name))
3244 ret = FAIL;
3245 else
3246 {
3247 di = find_var(lp->ll_name, NULL);
3248 if (di == NULL)
3249 ret = FAIL;
3250 else
3251 {
3252 if (lock)
3253 di->di_flags |= DI_FLAGS_LOCK;
3254 else
3255 di->di_flags &= ~DI_FLAGS_LOCK;
3256 item_lock(&di->di_tv, deep, lock);
3257 }
3258 }
3259 *name_end = cc;
3260 }
3261 else if (lp->ll_range)
3262 {
3263 listitem_T *li = lp->ll_li;
3264
3265 /* (un)lock a range of List items. */
3266 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3267 {
3268 item_lock(&li->li_tv, deep, lock);
3269 li = li->li_next;
3270 ++lp->ll_n1;
3271 }
3272 }
3273 else if (lp->ll_list != NULL)
3274 /* (un)lock a List item. */
3275 item_lock(&lp->ll_li->li_tv, deep, lock);
3276 else
3277 /* un(lock) a Dictionary item. */
3278 item_lock(&lp->ll_di->di_tv, deep, lock);
3279
3280 return ret;
3281}
3282
3283/*
3284 * Lock or unlock an item. "deep" is nr of levels to go.
3285 */
3286 static void
3287item_lock(tv, deep, lock)
3288 typval_T *tv;
3289 int deep;
3290 int lock;
3291{
3292 static int recurse = 0;
3293 list_T *l;
3294 listitem_T *li;
3295 dict_T *d;
3296 hashitem_T *hi;
3297 int todo;
3298
3299 if (recurse >= DICT_MAXNEST)
3300 {
3301 EMSG(_("E743: variable nested too deep for (un)lock"));
3302 return;
3303 }
3304 if (deep == 0)
3305 return;
3306 ++recurse;
3307
3308 /* lock/unlock the item itself */
3309 if (lock)
3310 tv->v_lock |= VAR_LOCKED;
3311 else
3312 tv->v_lock &= ~VAR_LOCKED;
3313
3314 switch (tv->v_type)
3315 {
3316 case VAR_LIST:
3317 if ((l = tv->vval.v_list) != NULL)
3318 {
3319 if (lock)
3320 l->lv_lock |= VAR_LOCKED;
3321 else
3322 l->lv_lock &= ~VAR_LOCKED;
3323 if (deep < 0 || deep > 1)
3324 /* recursive: lock/unlock the items the List contains */
3325 for (li = l->lv_first; li != NULL; li = li->li_next)
3326 item_lock(&li->li_tv, deep - 1, lock);
3327 }
3328 break;
3329 case VAR_DICT:
3330 if ((d = tv->vval.v_dict) != NULL)
3331 {
3332 if (lock)
3333 d->dv_lock |= VAR_LOCKED;
3334 else
3335 d->dv_lock &= ~VAR_LOCKED;
3336 if (deep < 0 || deep > 1)
3337 {
3338 /* recursive: lock/unlock the items the List contains */
3339 todo = d->dv_hashtab.ht_used;
3340 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3341 {
3342 if (!HASHITEM_EMPTY(hi))
3343 {
3344 --todo;
3345 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3346 }
3347 }
3348 }
3349 }
3350 }
3351 --recurse;
3352}
3353
Bram Moolenaara40058a2005-07-11 22:42:07 +00003354/*
3355 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3356 * it refers to a List or Dictionary that is locked.
3357 */
3358 static int
3359tv_islocked(tv)
3360 typval_T *tv;
3361{
3362 return (tv->v_lock & VAR_LOCKED)
3363 || (tv->v_type == VAR_LIST
3364 && tv->vval.v_list != NULL
3365 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3366 || (tv->v_type == VAR_DICT
3367 && tv->vval.v_dict != NULL
3368 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3369}
3370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3372/*
3373 * Delete all "menutrans_" variables.
3374 */
3375 void
3376del_menutrans_vars()
3377{
Bram Moolenaar33570922005-01-25 22:26:29 +00003378 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003379 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
Bram Moolenaar33570922005-01-25 22:26:29 +00003381 hash_lock(&globvarht);
3382 todo = globvarht.ht_used;
3383 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003384 {
3385 if (!HASHITEM_EMPTY(hi))
3386 {
3387 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003388 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3389 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003390 }
3391 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003392 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393}
3394#endif
3395
3396#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3397
3398/*
3399 * Local string buffer for the next two functions to store a variable name
3400 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3401 * get_user_var_name().
3402 */
3403
3404static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3405
3406static char_u *varnamebuf = NULL;
3407static int varnamebuflen = 0;
3408
3409/*
3410 * Function to concatenate a prefix and a variable name.
3411 */
3412 static char_u *
3413cat_prefix_varname(prefix, name)
3414 int prefix;
3415 char_u *name;
3416{
3417 int len;
3418
3419 len = (int)STRLEN(name) + 3;
3420 if (len > varnamebuflen)
3421 {
3422 vim_free(varnamebuf);
3423 len += 10; /* some additional space */
3424 varnamebuf = alloc(len);
3425 if (varnamebuf == NULL)
3426 {
3427 varnamebuflen = 0;
3428 return NULL;
3429 }
3430 varnamebuflen = len;
3431 }
3432 *varnamebuf = prefix;
3433 varnamebuf[1] = ':';
3434 STRCPY(varnamebuf + 2, name);
3435 return varnamebuf;
3436}
3437
3438/*
3439 * Function given to ExpandGeneric() to obtain the list of user defined
3440 * (global/buffer/window/built-in) variable names.
3441 */
3442/*ARGSUSED*/
3443 char_u *
3444get_user_var_name(xp, idx)
3445 expand_T *xp;
3446 int idx;
3447{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003448 static long_u gdone;
3449 static long_u bdone;
3450 static long_u wdone;
3451 static int vidx;
3452 static hashitem_T *hi;
3453 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
3455 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003456 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003457
3458 /* Global variables */
3459 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003461 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003462 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003463 else
3464 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003465 while (HASHITEM_EMPTY(hi))
3466 ++hi;
3467 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3468 return cat_prefix_varname('g', hi->hi_key);
3469 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003471
3472 /* b: variables */
3473 ht = &curbuf->b_vars.dv_hashtab;
3474 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003476 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003477 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003478 else
3479 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003480 while (HASHITEM_EMPTY(hi))
3481 ++hi;
3482 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003484 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003486 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 return (char_u *)"b:changedtick";
3488 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003489
3490 /* w: variables */
3491 ht = &curwin->w_vars.dv_hashtab;
3492 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003494 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003495 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003496 else
3497 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003498 while (HASHITEM_EMPTY(hi))
3499 ++hi;
3500 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003502
3503 /* v: variables */
3504 if (vidx < VV_LEN)
3505 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
3507 vim_free(varnamebuf);
3508 varnamebuf = NULL;
3509 varnamebuflen = 0;
3510 return NULL;
3511}
3512
3513#endif /* FEAT_CMDL_COMPL */
3514
3515/*
3516 * types for expressions.
3517 */
3518typedef enum
3519{
3520 TYPE_UNKNOWN = 0
3521 , TYPE_EQUAL /* == */
3522 , TYPE_NEQUAL /* != */
3523 , TYPE_GREATER /* > */
3524 , TYPE_GEQUAL /* >= */
3525 , TYPE_SMALLER /* < */
3526 , TYPE_SEQUAL /* <= */
3527 , TYPE_MATCH /* =~ */
3528 , TYPE_NOMATCH /* !~ */
3529} exptype_T;
3530
3531/*
3532 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003533 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3535 */
3536
3537/*
3538 * Handle zero level expression.
3539 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003540 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 * Return OK or FAIL.
3542 */
3543 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003544eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 char_u **nextcmd;
3548 int evaluate;
3549{
3550 int ret;
3551 char_u *p;
3552
3553 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003554 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 if (ret == FAIL || !ends_excmd(*p))
3556 {
3557 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003558 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 /*
3560 * Report the invalid expression unless the expression evaluation has
3561 * been cancelled due to an aborting error, an interrupt, or an
3562 * exception.
3563 */
3564 if (!aborting())
3565 EMSG2(_(e_invexpr2), arg);
3566 ret = FAIL;
3567 }
3568 if (nextcmd != NULL)
3569 *nextcmd = check_nextcmd(p);
3570
3571 return ret;
3572}
3573
3574/*
3575 * Handle top level expression:
3576 * expr1 ? expr0 : expr0
3577 *
3578 * "arg" must point to the first non-white of the expression.
3579 * "arg" is advanced to the next non-white after the recognized expression.
3580 *
3581 * Return OK or FAIL.
3582 */
3583 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003584eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 int evaluate;
3588{
3589 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003590 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
3592 /*
3593 * Get the first variable.
3594 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003595 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 return FAIL;
3597
3598 if ((*arg)[0] == '?')
3599 {
3600 result = FALSE;
3601 if (evaluate)
3602 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003603 int error = FALSE;
3604
3605 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003607 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003608 if (error)
3609 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 }
3611
3612 /*
3613 * Get the second variable.
3614 */
3615 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003616 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 return FAIL;
3618
3619 /*
3620 * Check for the ":".
3621 */
3622 if ((*arg)[0] != ':')
3623 {
3624 EMSG(_("E109: Missing ':' after '?'"));
3625 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003626 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 return FAIL;
3628 }
3629
3630 /*
3631 * Get the third variable.
3632 */
3633 *arg = skipwhite(*arg + 1);
3634 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3635 {
3636 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003637 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 return FAIL;
3639 }
3640 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003641 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
3643
3644 return OK;
3645}
3646
3647/*
3648 * Handle first level expression:
3649 * expr2 || expr2 || expr2 logical OR
3650 *
3651 * "arg" must point to the first non-white of the expression.
3652 * "arg" is advanced to the next non-white after the recognized expression.
3653 *
3654 * Return OK or FAIL.
3655 */
3656 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003657eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 int evaluate;
3661{
Bram Moolenaar33570922005-01-25 22:26:29 +00003662 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 long result;
3664 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003665 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666
3667 /*
3668 * Get the first variable.
3669 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003670 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 return FAIL;
3672
3673 /*
3674 * Repeat until there is no following "||".
3675 */
3676 first = TRUE;
3677 result = FALSE;
3678 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3679 {
3680 if (evaluate && first)
3681 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003682 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003684 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003685 if (error)
3686 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 first = FALSE;
3688 }
3689
3690 /*
3691 * Get the second variable.
3692 */
3693 *arg = skipwhite(*arg + 2);
3694 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3695 return FAIL;
3696
3697 /*
3698 * Compute the result.
3699 */
3700 if (evaluate && !result)
3701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003702 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003704 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003705 if (error)
3706 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
3708 if (evaluate)
3709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003710 rettv->v_type = VAR_NUMBER;
3711 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 }
3713 }
3714
3715 return OK;
3716}
3717
3718/*
3719 * Handle second level expression:
3720 * expr3 && expr3 && expr3 logical AND
3721 *
3722 * "arg" must point to the first non-white of the expression.
3723 * "arg" is advanced to the next non-white after the recognized expression.
3724 *
3725 * Return OK or FAIL.
3726 */
3727 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003728eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 int evaluate;
3732{
Bram Moolenaar33570922005-01-25 22:26:29 +00003733 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 long result;
3735 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003736 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
3738 /*
3739 * Get the first variable.
3740 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003741 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 return FAIL;
3743
3744 /*
3745 * Repeat until there is no following "&&".
3746 */
3747 first = TRUE;
3748 result = TRUE;
3749 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3750 {
3751 if (evaluate && first)
3752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003753 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003755 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003756 if (error)
3757 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 first = FALSE;
3759 }
3760
3761 /*
3762 * Get the second variable.
3763 */
3764 *arg = skipwhite(*arg + 2);
3765 if (eval4(arg, &var2, evaluate && result) == FAIL)
3766 return FAIL;
3767
3768 /*
3769 * Compute the result.
3770 */
3771 if (evaluate && result)
3772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003773 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003775 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003776 if (error)
3777 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
3779 if (evaluate)
3780 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003781 rettv->v_type = VAR_NUMBER;
3782 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784 }
3785
3786 return OK;
3787}
3788
3789/*
3790 * Handle third level expression:
3791 * var1 == var2
3792 * var1 =~ var2
3793 * var1 != var2
3794 * var1 !~ var2
3795 * var1 > var2
3796 * var1 >= var2
3797 * var1 < var2
3798 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003799 * var1 is var2
3800 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 *
3802 * "arg" must point to the first non-white of the expression.
3803 * "arg" is advanced to the next non-white after the recognized expression.
3804 *
3805 * Return OK or FAIL.
3806 */
3807 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003808eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 int evaluate;
3812{
Bram Moolenaar33570922005-01-25 22:26:29 +00003813 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 char_u *p;
3815 int i;
3816 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003817 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 int len = 2;
3819 long n1, n2;
3820 char_u *s1, *s2;
3821 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3822 regmatch_T regmatch;
3823 int ic;
3824 char_u *save_cpo;
3825
3826 /*
3827 * Get the first variable.
3828 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003829 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 return FAIL;
3831
3832 p = *arg;
3833 switch (p[0])
3834 {
3835 case '=': if (p[1] == '=')
3836 type = TYPE_EQUAL;
3837 else if (p[1] == '~')
3838 type = TYPE_MATCH;
3839 break;
3840 case '!': if (p[1] == '=')
3841 type = TYPE_NEQUAL;
3842 else if (p[1] == '~')
3843 type = TYPE_NOMATCH;
3844 break;
3845 case '>': if (p[1] != '=')
3846 {
3847 type = TYPE_GREATER;
3848 len = 1;
3849 }
3850 else
3851 type = TYPE_GEQUAL;
3852 break;
3853 case '<': if (p[1] != '=')
3854 {
3855 type = TYPE_SMALLER;
3856 len = 1;
3857 }
3858 else
3859 type = TYPE_SEQUAL;
3860 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003861 case 'i': if (p[1] == 's')
3862 {
3863 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3864 len = 5;
3865 if (!vim_isIDc(p[len]))
3866 {
3867 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3868 type_is = TRUE;
3869 }
3870 }
3871 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
3873
3874 /*
3875 * If there is a comparitive operator, use it.
3876 */
3877 if (type != TYPE_UNKNOWN)
3878 {
3879 /* extra question mark appended: ignore case */
3880 if (p[len] == '?')
3881 {
3882 ic = TRUE;
3883 ++len;
3884 }
3885 /* extra '#' appended: match case */
3886 else if (p[len] == '#')
3887 {
3888 ic = FALSE;
3889 ++len;
3890 }
3891 /* nothing appened: use 'ignorecase' */
3892 else
3893 ic = p_ic;
3894
3895 /*
3896 * Get the second variable.
3897 */
3898 *arg = skipwhite(p + len);
3899 if (eval5(arg, &var2, evaluate) == FAIL)
3900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003901 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 return FAIL;
3903 }
3904
3905 if (evaluate)
3906 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003907 if (type_is && rettv->v_type != var2.v_type)
3908 {
3909 /* For "is" a different type always means FALSE, for "notis"
3910 * it means TRUE. */
3911 n1 = (type == TYPE_NEQUAL);
3912 }
3913 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3914 {
3915 if (type_is)
3916 {
3917 n1 = (rettv->v_type == var2.v_type
3918 && rettv->vval.v_list == var2.vval.v_list);
3919 if (type == TYPE_NEQUAL)
3920 n1 = !n1;
3921 }
3922 else if (rettv->v_type != var2.v_type
3923 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3924 {
3925 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003926 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003927 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003928 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003929 clear_tv(rettv);
3930 clear_tv(&var2);
3931 return FAIL;
3932 }
3933 else
3934 {
3935 /* Compare two Lists for being equal or unequal. */
3936 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3937 if (type == TYPE_NEQUAL)
3938 n1 = !n1;
3939 }
3940 }
3941
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003942 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3943 {
3944 if (type_is)
3945 {
3946 n1 = (rettv->v_type == var2.v_type
3947 && rettv->vval.v_dict == var2.vval.v_dict);
3948 if (type == TYPE_NEQUAL)
3949 n1 = !n1;
3950 }
3951 else if (rettv->v_type != var2.v_type
3952 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3953 {
3954 if (rettv->v_type != var2.v_type)
3955 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3956 else
3957 EMSG(_("E736: Invalid operation for Dictionary"));
3958 clear_tv(rettv);
3959 clear_tv(&var2);
3960 return FAIL;
3961 }
3962 else
3963 {
3964 /* Compare two Dictionaries for being equal or unequal. */
3965 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3966 if (type == TYPE_NEQUAL)
3967 n1 = !n1;
3968 }
3969 }
3970
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003971 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3972 {
3973 if (rettv->v_type != var2.v_type
3974 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3975 {
3976 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003977 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003978 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003979 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003980 clear_tv(rettv);
3981 clear_tv(&var2);
3982 return FAIL;
3983 }
3984 else
3985 {
3986 /* Compare two Funcrefs for being equal or unequal. */
3987 if (rettv->vval.v_string == NULL
3988 || var2.vval.v_string == NULL)
3989 n1 = FALSE;
3990 else
3991 n1 = STRCMP(rettv->vval.v_string,
3992 var2.vval.v_string) == 0;
3993 if (type == TYPE_NEQUAL)
3994 n1 = !n1;
3995 }
3996 }
3997
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 /*
3999 * If one of the two variables is a number, compare as a number.
4000 * When using "=~" or "!~", always compare as string.
4001 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004002 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004005 n1 = get_tv_number(rettv);
4006 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 switch (type)
4008 {
4009 case TYPE_EQUAL: n1 = (n1 == n2); break;
4010 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4011 case TYPE_GREATER: n1 = (n1 > n2); break;
4012 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4013 case TYPE_SMALLER: n1 = (n1 < n2); break;
4014 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4015 case TYPE_UNKNOWN:
4016 case TYPE_MATCH:
4017 case TYPE_NOMATCH: break; /* avoid gcc warning */
4018 }
4019 }
4020 else
4021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022 s1 = get_tv_string_buf(rettv, buf1);
4023 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4025 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4026 else
4027 i = 0;
4028 n1 = FALSE;
4029 switch (type)
4030 {
4031 case TYPE_EQUAL: n1 = (i == 0); break;
4032 case TYPE_NEQUAL: n1 = (i != 0); break;
4033 case TYPE_GREATER: n1 = (i > 0); break;
4034 case TYPE_GEQUAL: n1 = (i >= 0); break;
4035 case TYPE_SMALLER: n1 = (i < 0); break;
4036 case TYPE_SEQUAL: n1 = (i <= 0); break;
4037
4038 case TYPE_MATCH:
4039 case TYPE_NOMATCH:
4040 /* avoid 'l' flag in 'cpoptions' */
4041 save_cpo = p_cpo;
4042 p_cpo = (char_u *)"";
4043 regmatch.regprog = vim_regcomp(s2,
4044 RE_MAGIC + RE_STRING);
4045 regmatch.rm_ic = ic;
4046 if (regmatch.regprog != NULL)
4047 {
4048 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4049 vim_free(regmatch.regprog);
4050 if (type == TYPE_NOMATCH)
4051 n1 = !n1;
4052 }
4053 p_cpo = save_cpo;
4054 break;
4055
4056 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4057 }
4058 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004059 clear_tv(rettv);
4060 clear_tv(&var2);
4061 rettv->v_type = VAR_NUMBER;
4062 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
4064 }
4065
4066 return OK;
4067}
4068
4069/*
4070 * Handle fourth level expression:
4071 * + number addition
4072 * - number subtraction
4073 * . string concatenation
4074 *
4075 * "arg" must point to the first non-white of the expression.
4076 * "arg" is advanced to the next non-white after the recognized expression.
4077 *
4078 * Return OK or FAIL.
4079 */
4080 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 int evaluate;
4085{
Bram Moolenaar33570922005-01-25 22:26:29 +00004086 typval_T var2;
4087 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 int op;
4089 long n1, n2;
4090 char_u *s1, *s2;
4091 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4092 char_u *p;
4093
4094 /*
4095 * Get the first variable.
4096 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 return FAIL;
4099
4100 /*
4101 * Repeat computing, until no '+', '-' or '.' is following.
4102 */
4103 for (;;)
4104 {
4105 op = **arg;
4106 if (op != '+' && op != '-' && op != '.')
4107 break;
4108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004109 if (op != '+' || rettv->v_type != VAR_LIST)
4110 {
4111 /* For "list + ...", an illegal use of the first operand as
4112 * a number cannot be determined before evaluating the 2nd
4113 * operand: if this is also a list, all is ok.
4114 * For "something . ...", "something - ..." or "non-list + ...",
4115 * we know that the first operand needs to be a string or number
4116 * without evaluating the 2nd operand. So check before to avoid
4117 * side effects after an error. */
4118 if (evaluate && get_tv_string_chk(rettv) == NULL)
4119 {
4120 clear_tv(rettv);
4121 return FAIL;
4122 }
4123 }
4124
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 /*
4126 * Get the second variable.
4127 */
4128 *arg = skipwhite(*arg + 1);
4129 if (eval6(arg, &var2, evaluate) == FAIL)
4130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004131 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 return FAIL;
4133 }
4134
4135 if (evaluate)
4136 {
4137 /*
4138 * Compute the result.
4139 */
4140 if (op == '.')
4141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004142 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4143 s2 = get_tv_string_buf_chk(&var2, buf2);
4144 if (s2 == NULL) /* type error ? */
4145 {
4146 clear_tv(rettv);
4147 clear_tv(&var2);
4148 return FAIL;
4149 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004150 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 clear_tv(rettv);
4152 rettv->v_type = VAR_STRING;
4153 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004155 else if (op == '+' && rettv->v_type == VAR_LIST
4156 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004157 {
4158 /* concatenate Lists */
4159 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4160 &var3) == FAIL)
4161 {
4162 clear_tv(rettv);
4163 clear_tv(&var2);
4164 return FAIL;
4165 }
4166 clear_tv(rettv);
4167 *rettv = var3;
4168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 else
4170 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 int error = FALSE;
4172
4173 n1 = get_tv_number_chk(rettv, &error);
4174 if (error)
4175 {
4176 /* This can only happen for "list + non-list".
4177 * For "non-list + ..." or "something - ...", we returned
4178 * before evaluating the 2nd operand. */
4179 clear_tv(rettv);
4180 return FAIL;
4181 }
4182 n2 = get_tv_number_chk(&var2, &error);
4183 if (error)
4184 {
4185 clear_tv(rettv);
4186 clear_tv(&var2);
4187 return FAIL;
4188 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 if (op == '+')
4191 n1 = n1 + n2;
4192 else
4193 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 rettv->v_type = VAR_NUMBER;
4195 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
4199 }
4200 return OK;
4201}
4202
4203/*
4204 * Handle fifth level expression:
4205 * * number multiplication
4206 * / number division
4207 * % number modulo
4208 *
4209 * "arg" must point to the first non-white of the expression.
4210 * "arg" is advanced to the next non-white after the recognized expression.
4211 *
4212 * Return OK or FAIL.
4213 */
4214 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004217 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 int evaluate;
4219{
Bram Moolenaar33570922005-01-25 22:26:29 +00004220 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 int op;
4222 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004223 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
4225 /*
4226 * Get the first variable.
4227 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004228 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 return FAIL;
4230
4231 /*
4232 * Repeat computing, until no '*', '/' or '%' is following.
4233 */
4234 for (;;)
4235 {
4236 op = **arg;
4237 if (op != '*' && op != '/' && op != '%')
4238 break;
4239
4240 if (evaluate)
4241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004243 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 if (error)
4245 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247 else
4248 n1 = 0;
4249
4250 /*
4251 * Get the second variable.
4252 */
4253 *arg = skipwhite(*arg + 1);
4254 if (eval7(arg, &var2, evaluate) == FAIL)
4255 return FAIL;
4256
4257 if (evaluate)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 if (error)
4262 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
4264 /*
4265 * Compute the result.
4266 */
4267 if (op == '*')
4268 n1 = n1 * n2;
4269 else if (op == '/')
4270 {
4271 if (n2 == 0) /* give an error message? */
4272 n1 = 0x7fffffffL;
4273 else
4274 n1 = n1 / n2;
4275 }
4276 else
4277 {
4278 if (n2 == 0) /* give an error message? */
4279 n1 = 0;
4280 else
4281 n1 = n1 % n2;
4282 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 rettv->v_type = VAR_NUMBER;
4284 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 }
4287
4288 return OK;
4289}
4290
4291/*
4292 * Handle sixth level expression:
4293 * number number constant
4294 * "string" string contstant
4295 * 'string' literal string contstant
4296 * &option-name option value
4297 * @r register contents
4298 * identifier variable value
4299 * function() function call
4300 * $VAR environment variable
4301 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004302 * [expr, expr] List
4303 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 *
4305 * Also handle:
4306 * ! in front logical NOT
4307 * - in front unary minus
4308 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004309 * trailing [] subscript in String or List
4310 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 *
4312 * "arg" must point to the first non-white of the expression.
4313 * "arg" is advanced to the next non-white after the recognized expression.
4314 *
4315 * Return OK or FAIL.
4316 */
4317 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 int evaluate;
4322{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 long n;
4324 int len;
4325 char_u *s;
4326 int val;
4327 char_u *start_leader, *end_leader;
4328 int ret = OK;
4329 char_u *alias;
4330
4331 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004332 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004333 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004335 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336
4337 /*
4338 * Skip '!' and '-' characters. They are handled later.
4339 */
4340 start_leader = *arg;
4341 while (**arg == '!' || **arg == '-' || **arg == '+')
4342 *arg = skipwhite(*arg + 1);
4343 end_leader = *arg;
4344
4345 switch (**arg)
4346 {
4347 /*
4348 * Number constant.
4349 */
4350 case '0':
4351 case '1':
4352 case '2':
4353 case '3':
4354 case '4':
4355 case '5':
4356 case '6':
4357 case '7':
4358 case '8':
4359 case '9':
4360 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4361 *arg += len;
4362 if (evaluate)
4363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 rettv->v_type = VAR_NUMBER;
4365 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 break;
4368
4369 /*
4370 * String constant: "string".
4371 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004372 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 break;
4374
4375 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004376 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004378 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004379 break;
4380
4381 /*
4382 * List: [expr, expr]
4383 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004384 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 break;
4386
4387 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004388 * Dictionary: {key: val, key: val}
4389 */
4390 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4391 break;
4392
4393 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004394 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004396 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 break;
4398
4399 /*
4400 * Environment variable: $VAR.
4401 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004402 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 break;
4404
4405 /*
4406 * Register contents: @r.
4407 */
4408 case '@': ++*arg;
4409 if (evaluate)
4410 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004411 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004412 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414 if (**arg != NUL)
4415 ++*arg;
4416 break;
4417
4418 /*
4419 * nested expression: (expression).
4420 */
4421 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004422 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 if (**arg == ')')
4424 ++*arg;
4425 else if (ret == OK)
4426 {
4427 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004428 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 ret = FAIL;
4430 }
4431 break;
4432
Bram Moolenaar8c711452005-01-14 21:53:12 +00004433 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 break;
4435 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004436
4437 if (ret == NOTDONE)
4438 {
4439 /*
4440 * Must be a variable or function name.
4441 * Can also be a curly-braces kind of name: {expr}.
4442 */
4443 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004444 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004445 if (alias != NULL)
4446 s = alias;
4447
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004448 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004449 ret = FAIL;
4450 else
4451 {
4452 if (**arg == '(') /* recursive! */
4453 {
4454 /* If "s" is the name of a variable of type VAR_FUNC
4455 * use its contents. */
4456 s = deref_func_name(s, &len);
4457
4458 /* Invoke the function. */
4459 ret = get_func_tv(s, len, rettv, arg,
4460 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004461 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004462 /* Stop the expression evaluation when immediately
4463 * aborting on error, or when an interrupt occurred or
4464 * an exception was thrown but not caught. */
4465 if (aborting())
4466 {
4467 if (ret == OK)
4468 clear_tv(rettv);
4469 ret = FAIL;
4470 }
4471 }
4472 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004473 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004474 else
4475 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004476 }
4477
4478 if (alias != NULL)
4479 vim_free(alias);
4480 }
4481
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 *arg = skipwhite(*arg);
4483
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004484 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4485 * expr(expr). */
4486 if (ret == OK)
4487 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488
4489 /*
4490 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4491 */
4492 if (ret == OK && evaluate && end_leader > start_leader)
4493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004494 int error = FALSE;
4495
4496 val = get_tv_number_chk(rettv, &error);
4497 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004499 clear_tv(rettv);
4500 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004502 else
4503 {
4504 while (end_leader > start_leader)
4505 {
4506 --end_leader;
4507 if (*end_leader == '!')
4508 val = !val;
4509 else if (*end_leader == '-')
4510 val = -val;
4511 }
4512 clear_tv(rettv);
4513 rettv->v_type = VAR_NUMBER;
4514 rettv->vval.v_number = val;
4515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 }
4517
4518 return ret;
4519}
4520
4521/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004522 * Evaluate an "[expr]" or "[expr:expr]" index.
4523 * "*arg" points to the '['.
4524 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4525 */
4526 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004527eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004528 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004529 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004530 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004531 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004532{
4533 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004534 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004535 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004536 long len = -1;
4537 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004538 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004539 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004540
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004541 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004542 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004543 if (verbose)
4544 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004545 return FAIL;
4546 }
4547
Bram Moolenaar8c711452005-01-14 21:53:12 +00004548 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004549 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004550 /*
4551 * dict.name
4552 */
4553 key = *arg + 1;
4554 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4555 ;
4556 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004557 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004558 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004559 }
4560 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004561 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004562 /*
4563 * something[idx]
4564 *
4565 * Get the (first) variable from inside the [].
4566 */
4567 *arg = skipwhite(*arg + 1);
4568 if (**arg == ':')
4569 empty1 = TRUE;
4570 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4571 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004572 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4573 {
4574 /* not a number or string */
4575 clear_tv(&var1);
4576 return FAIL;
4577 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004578
4579 /*
4580 * Get the second variable from inside the [:].
4581 */
4582 if (**arg == ':')
4583 {
4584 range = TRUE;
4585 *arg = skipwhite(*arg + 1);
4586 if (**arg == ']')
4587 empty2 = TRUE;
4588 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004590 if (!empty1)
4591 clear_tv(&var1);
4592 return FAIL;
4593 }
4594 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4595 {
4596 /* not a number or string */
4597 if (!empty1)
4598 clear_tv(&var1);
4599 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004600 return FAIL;
4601 }
4602 }
4603
4604 /* Check for the ']'. */
4605 if (**arg != ']')
4606 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004607 if (verbose)
4608 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004609 clear_tv(&var1);
4610 if (range)
4611 clear_tv(&var2);
4612 return FAIL;
4613 }
4614 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615 }
4616
4617 if (evaluate)
4618 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004619 n1 = 0;
4620 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004622 n1 = get_tv_number(&var1);
4623 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004624 }
4625 if (range)
4626 {
4627 if (empty2)
4628 n2 = -1;
4629 else
4630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 n2 = get_tv_number(&var2);
4632 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004633 }
4634 }
4635
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004636 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004637 {
4638 case VAR_NUMBER:
4639 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004640 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004641 len = (long)STRLEN(s);
4642 if (range)
4643 {
4644 /* The resulting variable is a substring. If the indexes
4645 * are out of range the result is empty. */
4646 if (n1 < 0)
4647 {
4648 n1 = len + n1;
4649 if (n1 < 0)
4650 n1 = 0;
4651 }
4652 if (n2 < 0)
4653 n2 = len + n2;
4654 else if (n2 >= len)
4655 n2 = len;
4656 if (n1 >= len || n2 < 0 || n1 > n2)
4657 s = NULL;
4658 else
4659 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4660 }
4661 else
4662 {
4663 /* The resulting variable is a string of a single
4664 * character. If the index is too big or negative the
4665 * result is empty. */
4666 if (n1 >= len || n1 < 0)
4667 s = NULL;
4668 else
4669 s = vim_strnsave(s + n1, 1);
4670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004671 clear_tv(rettv);
4672 rettv->v_type = VAR_STRING;
4673 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004674 break;
4675
4676 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004677 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004678 if (n1 < 0)
4679 n1 = len + n1;
4680 if (!empty1 && (n1 < 0 || n1 >= len))
4681 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004682 if (verbose)
4683 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004684 return FAIL;
4685 }
4686 if (range)
4687 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004688 list_T *l;
4689 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004690
4691 if (n2 < 0)
4692 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004693 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004694 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004695 if (verbose)
4696 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004697 return FAIL;
4698 }
4699 l = list_alloc();
4700 if (l == NULL)
4701 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004702 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004703 n1 <= n2; ++n1)
4704 {
4705 if (list_append_tv(l, &item->li_tv) == FAIL)
4706 {
4707 list_free(l);
4708 return FAIL;
4709 }
4710 item = item->li_next;
4711 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004712 clear_tv(rettv);
4713 rettv->v_type = VAR_LIST;
4714 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004715 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004716 }
4717 else
4718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004719 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004720 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004721 clear_tv(rettv);
4722 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004723 }
4724 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004725
4726 case VAR_DICT:
4727 if (range)
4728 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004729 if (verbose)
4730 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004731 if (len == -1)
4732 clear_tv(&var1);
4733 return FAIL;
4734 }
4735 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004736 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004737
4738 if (len == -1)
4739 {
4740 key = get_tv_string(&var1);
4741 if (*key == NUL)
4742 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004743 if (verbose)
4744 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004745 clear_tv(&var1);
4746 return FAIL;
4747 }
4748 }
4749
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004750 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004751
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004752 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004753 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004754 if (len == -1)
4755 clear_tv(&var1);
4756 if (item == NULL)
4757 return FAIL;
4758
4759 copy_tv(&item->di_tv, &var1);
4760 clear_tv(rettv);
4761 *rettv = var1;
4762 }
4763 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764 }
4765 }
4766
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004767 return OK;
4768}
4769
4770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 * Get an option value.
4772 * "arg" points to the '&' or '+' before the option name.
4773 * "arg" is advanced to character after the option name.
4774 * Return OK or FAIL.
4775 */
4776 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004777get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004779 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 int evaluate;
4781{
4782 char_u *option_end;
4783 long numval;
4784 char_u *stringval;
4785 int opt_type;
4786 int c;
4787 int working = (**arg == '+'); /* has("+option") */
4788 int ret = OK;
4789 int opt_flags;
4790
4791 /*
4792 * Isolate the option name and find its value.
4793 */
4794 option_end = find_option_end(arg, &opt_flags);
4795 if (option_end == NULL)
4796 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004797 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 EMSG2(_("E112: Option name missing: %s"), *arg);
4799 return FAIL;
4800 }
4801
4802 if (!evaluate)
4803 {
4804 *arg = option_end;
4805 return OK;
4806 }
4807
4808 c = *option_end;
4809 *option_end = NUL;
4810 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004811 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 if (opt_type == -3) /* invalid name */
4814 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004815 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 EMSG2(_("E113: Unknown option: %s"), *arg);
4817 ret = FAIL;
4818 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004819 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
4821 if (opt_type == -2) /* hidden string option */
4822 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004823 rettv->v_type = VAR_STRING;
4824 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
4826 else if (opt_type == -1) /* hidden number option */
4827 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004828 rettv->v_type = VAR_NUMBER;
4829 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 }
4831 else if (opt_type == 1) /* number option */
4832 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004833 rettv->v_type = VAR_NUMBER;
4834 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 }
4836 else /* string option */
4837 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 rettv->v_type = VAR_STRING;
4839 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
4841 }
4842 else if (working && (opt_type == -2 || opt_type == -1))
4843 ret = FAIL;
4844
4845 *option_end = c; /* put back for error messages */
4846 *arg = option_end;
4847
4848 return ret;
4849}
4850
4851/*
4852 * Allocate a variable for a string constant.
4853 * Return OK or FAIL.
4854 */
4855 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004856get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 int evaluate;
4860{
4861 char_u *p;
4862 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 int extra = 0;
4864
4865 /*
4866 * Find the end of the string, skipping backslashed characters.
4867 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004868 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 {
4870 if (*p == '\\' && p[1] != NUL)
4871 {
4872 ++p;
4873 /* A "\<x>" form occupies at least 4 characters, and produces up
4874 * to 6 characters: reserve space for 2 extra */
4875 if (*p == '<')
4876 extra += 2;
4877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879
4880 if (*p != '"')
4881 {
4882 EMSG2(_("E114: Missing quote: %s"), *arg);
4883 return FAIL;
4884 }
4885
4886 /* If only parsing, set *arg and return here */
4887 if (!evaluate)
4888 {
4889 *arg = p + 1;
4890 return OK;
4891 }
4892
4893 /*
4894 * Copy the string into allocated memory, handling backslashed
4895 * characters.
4896 */
4897 name = alloc((unsigned)(p - *arg + extra));
4898 if (name == NULL)
4899 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004900 rettv->v_type = VAR_STRING;
4901 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902
Bram Moolenaar8c711452005-01-14 21:53:12 +00004903 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
4905 if (*p == '\\')
4906 {
4907 switch (*++p)
4908 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004909 case 'b': *name++ = BS; ++p; break;
4910 case 'e': *name++ = ESC; ++p; break;
4911 case 'f': *name++ = FF; ++p; break;
4912 case 'n': *name++ = NL; ++p; break;
4913 case 'r': *name++ = CAR; ++p; break;
4914 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915
4916 case 'X': /* hex: "\x1", "\x12" */
4917 case 'x':
4918 case 'u': /* Unicode: "\u0023" */
4919 case 'U':
4920 if (vim_isxdigit(p[1]))
4921 {
4922 int n, nr;
4923 int c = toupper(*p);
4924
4925 if (c == 'X')
4926 n = 2;
4927 else
4928 n = 4;
4929 nr = 0;
4930 while (--n >= 0 && vim_isxdigit(p[1]))
4931 {
4932 ++p;
4933 nr = (nr << 4) + hex2nr(*p);
4934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004935 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936#ifdef FEAT_MBYTE
4937 /* For "\u" store the number according to
4938 * 'encoding'. */
4939 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004940 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 else
4942#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004943 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 break;
4946
4947 /* octal: "\1", "\12", "\123" */
4948 case '0':
4949 case '1':
4950 case '2':
4951 case '3':
4952 case '4':
4953 case '5':
4954 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955 case '7': *name = *p++ - '0';
4956 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004958 *name = (*name << 3) + *p++ - '0';
4959 if (*p >= '0' && *p <= '7')
4960 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 break;
4964
4965 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004966 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 if (extra != 0)
4968 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 break;
4971 }
4972 /* FALLTHROUGH */
4973
Bram Moolenaar8c711452005-01-14 21:53:12 +00004974 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 break;
4976 }
4977 }
4978 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004979 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 *arg = p + 1;
4984
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 return OK;
4986}
4987
4988/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004989 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 * Return OK or FAIL.
4991 */
4992 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004993get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 int evaluate;
4997{
4998 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004999 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005000 int reduce = 0;
5001
5002 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005003 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005004 */
5005 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5006 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005007 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005008 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005009 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005010 break;
5011 ++reduce;
5012 ++p;
5013 }
5014 }
5015
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005017 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005018 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005019 return FAIL;
5020 }
5021
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005023 if (!evaluate)
5024 {
5025 *arg = p + 1;
5026 return OK;
5027 }
5028
5029 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005030 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005031 */
5032 str = alloc((unsigned)((p - *arg) - reduce));
5033 if (str == NULL)
5034 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035 rettv->v_type = VAR_STRING;
5036 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005037
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005039 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005041 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005043 break;
5044 ++p;
5045 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005047 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005049 *arg = p + 1;
5050
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005051 return OK;
5052}
5053
5054/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005055 * Allocate a variable for a List and fill it from "*arg".
5056 * Return OK or FAIL.
5057 */
5058 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005060 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005061 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005062 int evaluate;
5063{
Bram Moolenaar33570922005-01-25 22:26:29 +00005064 list_T *l = NULL;
5065 typval_T tv;
5066 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005067
5068 if (evaluate)
5069 {
5070 l = list_alloc();
5071 if (l == NULL)
5072 return FAIL;
5073 }
5074
5075 *arg = skipwhite(*arg + 1);
5076 while (**arg != ']' && **arg != NUL)
5077 {
5078 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5079 goto failret;
5080 if (evaluate)
5081 {
5082 item = listitem_alloc();
5083 if (item != NULL)
5084 {
5085 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005086 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005087 list_append(l, item);
5088 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005089 else
5090 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005091 }
5092
5093 if (**arg == ']')
5094 break;
5095 if (**arg != ',')
5096 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005097 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005098 goto failret;
5099 }
5100 *arg = skipwhite(*arg + 1);
5101 }
5102
5103 if (**arg != ']')
5104 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005106failret:
5107 if (evaluate)
5108 list_free(l);
5109 return FAIL;
5110 }
5111
5112 *arg = skipwhite(*arg + 1);
5113 if (evaluate)
5114 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005115 rettv->v_type = VAR_LIST;
5116 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005117 ++l->lv_refcount;
5118 }
5119
5120 return OK;
5121}
5122
5123/*
5124 * Allocate an empty header for a list.
5125 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005126 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005127list_alloc()
5128{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005129 list_T *l;
5130
5131 l = (list_T *)alloc_clear(sizeof(list_T));
5132 if (l != NULL)
5133 {
5134 /* Prepend the list to the list of lists for garbage collection. */
5135 if (first_list != NULL)
5136 first_list->lv_used_prev = l;
5137 l->lv_used_prev = NULL;
5138 l->lv_used_next = first_list;
5139 first_list = l;
5140 }
5141 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005142}
5143
5144/*
5145 * Unreference a list: decrement the reference count and free it when it
5146 * becomes zero.
5147 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005148 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005150 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005151{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005152 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5153 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005154}
5155
5156/*
5157 * Free a list, including all items it points to.
5158 * Ignores the reference count.
5159 */
5160 static void
5161list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005162 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005163{
Bram Moolenaar33570922005-01-25 22:26:29 +00005164 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165
Bram Moolenaard9fba312005-06-26 22:34:35 +00005166 /* Avoid that recursive reference to the list frees us again. */
5167 l->lv_refcount = DEL_REFCOUNT;
5168
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005169 /* Remove the list from the list of lists for garbage collection. */
5170 if (l->lv_used_prev == NULL)
5171 first_list = l->lv_used_next;
5172 else
5173 l->lv_used_prev->lv_used_next = l->lv_used_next;
5174 if (l->lv_used_next != NULL)
5175 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5176
Bram Moolenaard9fba312005-06-26 22:34:35 +00005177 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005178 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005179 /* Remove the item before deleting it. */
5180 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005181 listitem_free(item);
5182 }
5183 vim_free(l);
5184}
5185
5186/*
5187 * Allocate a list item.
5188 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005189 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005190listitem_alloc()
5191{
Bram Moolenaar33570922005-01-25 22:26:29 +00005192 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005193}
5194
5195/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005196 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005197 */
5198 static void
5199listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005200 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005201{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005203 vim_free(item);
5204}
5205
5206/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005207 * Remove a list item from a List and free it. Also clears the value.
5208 */
5209 static void
5210listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005211 list_T *l;
5212 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005213{
5214 list_remove(l, item, item);
5215 listitem_free(item);
5216}
5217
5218/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005219 * Get the number of items in a list.
5220 */
5221 static long
5222list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005223 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225 if (l == NULL)
5226 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005227 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228}
5229
5230/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005231 * Return TRUE when two lists have exactly the same values.
5232 */
5233 static int
5234list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005235 list_T *l1;
5236 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005237 int ic; /* ignore case for strings */
5238{
Bram Moolenaar33570922005-01-25 22:26:29 +00005239 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005240
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005241 if (list_len(l1) != list_len(l2))
5242 return FALSE;
5243
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005244 for (item1 = l1->lv_first, item2 = l2->lv_first;
5245 item1 != NULL && item2 != NULL;
5246 item1 = item1->li_next, item2 = item2->li_next)
5247 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5248 return FALSE;
5249 return item1 == NULL && item2 == NULL;
5250}
5251
5252/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005253 * Return TRUE when two dictionaries have exactly the same key/values.
5254 */
5255 static int
5256dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005257 dict_T *d1;
5258 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005259 int ic; /* ignore case for strings */
5260{
Bram Moolenaar33570922005-01-25 22:26:29 +00005261 hashitem_T *hi;
5262 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005263 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005264
5265 if (dict_len(d1) != dict_len(d2))
5266 return FALSE;
5267
Bram Moolenaar33570922005-01-25 22:26:29 +00005268 todo = d1->dv_hashtab.ht_used;
5269 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005270 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005271 if (!HASHITEM_EMPTY(hi))
5272 {
5273 item2 = dict_find(d2, hi->hi_key, -1);
5274 if (item2 == NULL)
5275 return FALSE;
5276 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5277 return FALSE;
5278 --todo;
5279 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005280 }
5281 return TRUE;
5282}
5283
5284/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005285 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005286 * Compares the items just like "==" would compare them, but strings and
5287 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005288 */
5289 static int
5290tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005291 typval_T *tv1;
5292 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005293 int ic; /* ignore case */
5294{
5295 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005296 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005297
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005298 if (tv1->v_type != tv2->v_type)
5299 return FALSE;
5300
5301 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005302 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005303 case VAR_LIST:
5304 /* recursive! */
5305 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5306
5307 case VAR_DICT:
5308 /* recursive! */
5309 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5310
5311 case VAR_FUNC:
5312 return (tv1->vval.v_string != NULL
5313 && tv2->vval.v_string != NULL
5314 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5315
5316 case VAR_NUMBER:
5317 return tv1->vval.v_number == tv2->vval.v_number;
5318
5319 case VAR_STRING:
5320 s1 = get_tv_string_buf(tv1, buf1);
5321 s2 = get_tv_string_buf(tv2, buf2);
5322 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005323 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005324
5325 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005326 return TRUE;
5327}
5328
5329/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330 * Locate item with index "n" in list "l" and return it.
5331 * A negative index is counted from the end; -1 is the last item.
5332 * Returns NULL when "n" is out of range.
5333 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005334 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005336 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337 long n;
5338{
Bram Moolenaar33570922005-01-25 22:26:29 +00005339 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 long idx;
5341
5342 if (l == NULL)
5343 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005344
5345 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005347 n = l->lv_len + n;
5348
5349 /* Check for index out of range. */
5350 if (n < 0 || n >= l->lv_len)
5351 return NULL;
5352
5353 /* When there is a cached index may start search from there. */
5354 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005356 if (n < l->lv_idx / 2)
5357 {
5358 /* closest to the start of the list */
5359 item = l->lv_first;
5360 idx = 0;
5361 }
5362 else if (n > (l->lv_idx + l->lv_len) / 2)
5363 {
5364 /* closest to the end of the list */
5365 item = l->lv_last;
5366 idx = l->lv_len - 1;
5367 }
5368 else
5369 {
5370 /* closest to the cached index */
5371 item = l->lv_idx_item;
5372 idx = l->lv_idx;
5373 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 }
5375 else
5376 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005377 if (n < l->lv_len / 2)
5378 {
5379 /* closest to the start of the list */
5380 item = l->lv_first;
5381 idx = 0;
5382 }
5383 else
5384 {
5385 /* closest to the end of the list */
5386 item = l->lv_last;
5387 idx = l->lv_len - 1;
5388 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005390
5391 while (n > idx)
5392 {
5393 /* search forward */
5394 item = item->li_next;
5395 ++idx;
5396 }
5397 while (n < idx)
5398 {
5399 /* search backward */
5400 item = item->li_prev;
5401 --idx;
5402 }
5403
5404 /* cache the used index */
5405 l->lv_idx = idx;
5406 l->lv_idx_item = item;
5407
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 return item;
5409}
5410
5411/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005412 * Locate "item" list "l" and return its index.
5413 * Returns -1 when "item" is not in the list.
5414 */
5415 static long
5416list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005417 list_T *l;
5418 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005419{
5420 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005421 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005422
5423 if (l == NULL)
5424 return -1;
5425 idx = 0;
5426 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5427 ++idx;
5428 if (li == NULL)
5429 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005430 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005431}
5432
5433/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 * Append item "item" to the end of list "l".
5435 */
5436 static void
5437list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005438 list_T *l;
5439 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440{
5441 if (l->lv_last == NULL)
5442 {
5443 /* empty list */
5444 l->lv_first = item;
5445 l->lv_last = item;
5446 item->li_prev = NULL;
5447 }
5448 else
5449 {
5450 l->lv_last->li_next = item;
5451 item->li_prev = l->lv_last;
5452 l->lv_last = item;
5453 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005454 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 item->li_next = NULL;
5456}
5457
5458/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005459 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005460 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 */
5462 static int
5463list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005464 list_T *l;
5465 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005467 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468
Bram Moolenaar05159a02005-02-26 23:04:13 +00005469 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005471 copy_tv(tv, &li->li_tv);
5472 list_append(l, li);
5473 return OK;
5474}
5475
5476/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005477 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005478 * Return FAIL when out of memory.
5479 */
5480 int
5481list_append_dict(list, dict)
5482 list_T *list;
5483 dict_T *dict;
5484{
5485 listitem_T *li = listitem_alloc();
5486
5487 if (li == NULL)
5488 return FAIL;
5489 li->li_tv.v_type = VAR_DICT;
5490 li->li_tv.v_lock = 0;
5491 li->li_tv.vval.v_dict = dict;
5492 list_append(list, li);
5493 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 return OK;
5495}
5496
5497/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005499 * If "item" is NULL append at the end.
5500 * Return FAIL when out of memory.
5501 */
5502 static int
5503list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005504 list_T *l;
5505 typval_T *tv;
5506 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005507{
Bram Moolenaar33570922005-01-25 22:26:29 +00005508 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005509
5510 if (ni == NULL)
5511 return FAIL;
5512 copy_tv(tv, &ni->li_tv);
5513 if (item == NULL)
5514 /* Append new item at end of list. */
5515 list_append(l, ni);
5516 else
5517 {
5518 /* Insert new item before existing item. */
5519 ni->li_prev = item->li_prev;
5520 ni->li_next = item;
5521 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005522 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005523 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005524 ++l->lv_idx;
5525 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005526 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005527 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005528 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005529 l->lv_idx_item = NULL;
5530 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005531 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005532 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005533 }
5534 return OK;
5535}
5536
5537/*
5538 * Extend "l1" with "l2".
5539 * If "bef" is NULL append at the end, otherwise insert before this item.
5540 * Returns FAIL when out of memory.
5541 */
5542 static int
5543list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005544 list_T *l1;
5545 list_T *l2;
5546 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005547{
Bram Moolenaar33570922005-01-25 22:26:29 +00005548 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005549
5550 for (item = l2->lv_first; item != NULL; item = item->li_next)
5551 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5552 return FAIL;
5553 return OK;
5554}
5555
5556/*
5557 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5558 * Return FAIL when out of memory.
5559 */
5560 static int
5561list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005562 list_T *l1;
5563 list_T *l2;
5564 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005565{
Bram Moolenaar33570922005-01-25 22:26:29 +00005566 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005567
5568 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005569 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005570 if (l == NULL)
5571 return FAIL;
5572 tv->v_type = VAR_LIST;
5573 tv->vval.v_list = l;
5574
5575 /* append all items from the second list */
5576 return list_extend(l, l2, NULL);
5577}
5578
5579/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005580 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005582 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005583 * Returns NULL when out of memory.
5584 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005585 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005586list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005587 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005588 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005589 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590{
Bram Moolenaar33570922005-01-25 22:26:29 +00005591 list_T *copy;
5592 listitem_T *item;
5593 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005594
5595 if (orig == NULL)
5596 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005597
5598 copy = list_alloc();
5599 if (copy != NULL)
5600 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005601 if (copyID != 0)
5602 {
5603 /* Do this before adding the items, because one of the items may
5604 * refer back to this list. */
5605 orig->lv_copyID = copyID;
5606 orig->lv_copylist = copy;
5607 }
5608 for (item = orig->lv_first; item != NULL && !got_int;
5609 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005610 {
5611 ni = listitem_alloc();
5612 if (ni == NULL)
5613 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005614 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005615 {
5616 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5617 {
5618 vim_free(ni);
5619 break;
5620 }
5621 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005622 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005623 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005624 list_append(copy, ni);
5625 }
5626 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005627 if (item != NULL)
5628 {
5629 list_unref(copy);
5630 copy = NULL;
5631 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 }
5633
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 return copy;
5635}
5636
5637/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005638 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005639 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005641 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005642list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005643 list_T *l;
5644 listitem_T *item;
5645 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005646{
Bram Moolenaar33570922005-01-25 22:26:29 +00005647 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005649 /* notify watchers */
5650 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005651 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005652 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005653 list_fix_watch(l, ip);
5654 if (ip == item2)
5655 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005656 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005657
5658 if (item2->li_next == NULL)
5659 l->lv_last = item->li_prev;
5660 else
5661 item2->li_next->li_prev = item->li_prev;
5662 if (item->li_prev == NULL)
5663 l->lv_first = item2->li_next;
5664 else
5665 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005666 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005667}
5668
5669/*
5670 * Return an allocated string with the string representation of a list.
5671 * May return NULL.
5672 */
5673 static char_u *
5674list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005675 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005676{
5677 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005678
5679 if (tv->vval.v_list == NULL)
5680 return NULL;
5681 ga_init2(&ga, (int)sizeof(char), 80);
5682 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005683 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5684 {
5685 vim_free(ga.ga_data);
5686 return NULL;
5687 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005688 ga_append(&ga, ']');
5689 ga_append(&ga, NUL);
5690 return (char_u *)ga.ga_data;
5691}
5692
5693/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005694 * Join list "l" into a string in "*gap", using separator "sep".
5695 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005696 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005697 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005698 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005699list_join(gap, l, sep, echo)
5700 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005701 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005702 char_u *sep;
5703 int echo;
5704{
5705 int first = TRUE;
5706 char_u *tofree;
5707 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005708 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005709 char_u *s;
5710
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005711 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005712 {
5713 if (first)
5714 first = FALSE;
5715 else
5716 ga_concat(gap, sep);
5717
5718 if (echo)
5719 s = echo_string(&item->li_tv, &tofree, numbuf);
5720 else
5721 s = tv2string(&item->li_tv, &tofree, numbuf);
5722 if (s != NULL)
5723 ga_concat(gap, s);
5724 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005725 if (s == NULL)
5726 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005727 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005728 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005729}
5730
5731/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005732 * Garbage collection for lists and dictionaries.
5733 *
5734 * We use reference counts to be able to free most items right away when they
5735 * are no longer used. But for composite items it's possible that it becomes
5736 * unused while the reference count is > 0: When there is a recursive
5737 * reference. Example:
5738 * :let l = [1, 2, 3]
5739 * :let d = {9: l}
5740 * :let l[1] = d
5741 *
5742 * Since this is quite unusual we handle this with garbage collection: every
5743 * once in a while find out which lists and dicts are not referenced from any
5744 * variable.
5745 *
5746 * Here is a good reference text about garbage collection (refers to Python
5747 * but it applies to all reference-counting mechanisms):
5748 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005749 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005750
5751/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005752 * Do garbage collection for lists and dicts.
5753 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005754 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005755 int
5756garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005757{
5758 dict_T *dd;
5759 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005760 int copyID = ++current_copyID;
5761 buf_T *buf;
5762 win_T *wp;
5763 int i;
5764 funccall_T *fc;
5765 int did_free = FALSE;
5766
5767 /*
5768 * 1. Go through all accessible variables and mark all lists and dicts
5769 * with copyID.
5770 */
5771 /* script-local variables */
5772 for (i = 1; i <= ga_scripts.ga_len; ++i)
5773 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5774
5775 /* buffer-local variables */
5776 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5777 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5778
5779 /* window-local variables */
5780 FOR_ALL_WINDOWS(wp)
5781 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5782
5783 /* global variables */
5784 set_ref_in_ht(&globvarht, copyID);
5785
5786 /* function-local variables */
5787 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5788 {
5789 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5790 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5791 }
5792
5793 /*
5794 * 2. Go through the list of dicts and free items without the copyID.
5795 */
5796 for (dd = first_dict; dd != NULL; )
5797 if (dd->dv_copyID != copyID)
5798 {
5799 dict_free(dd);
5800 did_free = TRUE;
5801
5802 /* restart, next dict may also have been freed */
5803 dd = first_dict;
5804 }
5805 else
5806 dd = dd->dv_used_next;
5807
5808 /*
5809 * 3. Go through the list of lists and free items without the copyID.
5810 */
5811 for (ll = first_list; ll != NULL; )
5812 if (ll->lv_copyID != copyID)
5813 {
5814 list_free(ll);
5815 did_free = TRUE;
5816
5817 /* restart, next dict may also have been freed */
5818 ll = first_list;
5819 }
5820 else
5821 ll = ll->lv_used_next;
5822
5823 return did_free;
5824}
5825
5826/*
5827 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5828 */
5829 static void
5830set_ref_in_ht(ht, copyID)
5831 hashtab_T *ht;
5832 int copyID;
5833{
5834 int todo;
5835 hashitem_T *hi;
5836
5837 todo = ht->ht_used;
5838 for (hi = ht->ht_array; todo > 0; ++hi)
5839 if (!HASHITEM_EMPTY(hi))
5840 {
5841 --todo;
5842 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5843 }
5844}
5845
5846/*
5847 * Mark all lists and dicts referenced through list "l" with "copyID".
5848 */
5849 static void
5850set_ref_in_list(l, copyID)
5851 list_T *l;
5852 int copyID;
5853{
5854 listitem_T *li;
5855
5856 for (li = l->lv_first; li != NULL; li = li->li_next)
5857 set_ref_in_item(&li->li_tv, copyID);
5858}
5859
5860/*
5861 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5862 */
5863 static void
5864set_ref_in_item(tv, copyID)
5865 typval_T *tv;
5866 int copyID;
5867{
5868 dict_T *dd;
5869 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005870
5871 switch (tv->v_type)
5872 {
5873 case VAR_DICT:
5874 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005875 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005876 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005877 /* Didn't see this dict yet. */
5878 dd->dv_copyID = copyID;
5879 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005880 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005881 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005882
5883 case VAR_LIST:
5884 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005885 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005886 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005887 /* Didn't see this list yet. */
5888 ll->lv_copyID = copyID;
5889 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005890 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005891 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005892 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005893 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005894}
5895
5896/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 * Allocate an empty header for a dictionary.
5898 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005899 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005900dict_alloc()
5901{
Bram Moolenaar33570922005-01-25 22:26:29 +00005902 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005903
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005905 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005906 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005907 /* Add the list to the hashtable for garbage collection. */
5908 if (first_dict != NULL)
5909 first_dict->dv_used_prev = d;
5910 d->dv_used_next = first_dict;
5911 d->dv_used_prev = NULL;
5912
Bram Moolenaar33570922005-01-25 22:26:29 +00005913 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005914 d->dv_lock = 0;
5915 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005916 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005917 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005918 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919}
5920
5921/*
5922 * Unreference a Dictionary: decrement the reference count and free it when it
5923 * becomes zero.
5924 */
5925 static void
5926dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005927 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005928{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005929 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
5930 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931}
5932
5933/*
5934 * Free a Dictionary, including all items it contains.
5935 * Ignores the reference count.
5936 */
5937 static void
5938dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005939 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005940{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005941 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005942 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005943 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005944
Bram Moolenaard9fba312005-06-26 22:34:35 +00005945 /* Avoid that recursive reference to the dict frees us again. */
5946 d->dv_refcount = DEL_REFCOUNT;
5947
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005948 /* Remove the dict from the list of dicts for garbage collection. */
5949 if (d->dv_used_prev == NULL)
5950 first_dict = d->dv_used_next;
5951 else
5952 d->dv_used_prev->dv_used_next = d->dv_used_next;
5953 if (d->dv_used_next != NULL)
5954 d->dv_used_next->dv_used_prev = d->dv_used_prev;
5955
5956 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005957 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00005958 todo = d->dv_hashtab.ht_used;
5959 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005960 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005961 if (!HASHITEM_EMPTY(hi))
5962 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005963 /* Remove the item before deleting it, just in case there is
5964 * something recursive causing trouble. */
5965 di = HI2DI(hi);
5966 hash_remove(&d->dv_hashtab, hi);
5967 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005968 --todo;
5969 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005970 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005971 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005972 vim_free(d);
5973}
5974
5975/*
5976 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005977 * The "key" is copied to the new item.
5978 * Note that the value of the item "di_tv" still needs to be initialized!
5979 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005980 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005981 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005982dictitem_alloc(key)
5983 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005984{
Bram Moolenaar33570922005-01-25 22:26:29 +00005985 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005986
Bram Moolenaar33570922005-01-25 22:26:29 +00005987 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005988 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005989 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005990 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005991 di->di_flags = 0;
5992 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005993 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005994}
5995
5996/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005997 * Make a copy of a Dictionary item.
5998 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005999 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006000dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006002{
Bram Moolenaar33570922005-01-25 22:26:29 +00006003 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006004
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006006 if (di != NULL)
6007 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006008 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006009 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006010 copy_tv(&org->di_tv, &di->di_tv);
6011 }
6012 return di;
6013}
6014
6015/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006016 * Remove item "item" from Dictionary "dict" and free it.
6017 */
6018 static void
6019dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006020 dict_T *dict;
6021 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006022{
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006024
Bram Moolenaar33570922005-01-25 22:26:29 +00006025 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006026 if (HASHITEM_EMPTY(hi))
6027 EMSG2(_(e_intern2), "dictitem_remove()");
6028 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006030 dictitem_free(item);
6031}
6032
6033/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006034 * Free a dict item. Also clears the value.
6035 */
6036 static void
6037dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006039{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006040 clear_tv(&item->di_tv);
6041 vim_free(item);
6042}
6043
6044/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006045 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6046 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006047 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006048 * Returns NULL when out of memory.
6049 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006050 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006051dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006053 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006054 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006055{
Bram Moolenaar33570922005-01-25 22:26:29 +00006056 dict_T *copy;
6057 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006058 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006059 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006060
6061 if (orig == NULL)
6062 return NULL;
6063
6064 copy = dict_alloc();
6065 if (copy != NULL)
6066 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006067 if (copyID != 0)
6068 {
6069 orig->dv_copyID = copyID;
6070 orig->dv_copydict = copy;
6071 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006072 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006073 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006074 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006075 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006076 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006077 --todo;
6078
6079 di = dictitem_alloc(hi->hi_key);
6080 if (di == NULL)
6081 break;
6082 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006083 {
6084 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6085 copyID) == FAIL)
6086 {
6087 vim_free(di);
6088 break;
6089 }
6090 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006091 else
6092 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6093 if (dict_add(copy, di) == FAIL)
6094 {
6095 dictitem_free(di);
6096 break;
6097 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006098 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006099 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006100
Bram Moolenaare9a41262005-01-15 22:18:47 +00006101 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006102 if (todo > 0)
6103 {
6104 dict_unref(copy);
6105 copy = NULL;
6106 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006107 }
6108
6109 return copy;
6110}
6111
6112/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006113 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006114 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006115 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006116 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006117dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006118 dict_T *d;
6119 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006120{
Bram Moolenaar33570922005-01-25 22:26:29 +00006121 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006122}
6123
Bram Moolenaar8c711452005-01-14 21:53:12 +00006124/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006125 * Add a number or string entry to dictionary "d".
6126 * When "str" is NULL use number "nr", otherwise use "str".
6127 * Returns FAIL when out of memory and when key already exists.
6128 */
6129 int
6130dict_add_nr_str(d, key, nr, str)
6131 dict_T *d;
6132 char *key;
6133 long nr;
6134 char_u *str;
6135{
6136 dictitem_T *item;
6137
6138 item = dictitem_alloc((char_u *)key);
6139 if (item == NULL)
6140 return FAIL;
6141 item->di_tv.v_lock = 0;
6142 if (str == NULL)
6143 {
6144 item->di_tv.v_type = VAR_NUMBER;
6145 item->di_tv.vval.v_number = nr;
6146 }
6147 else
6148 {
6149 item->di_tv.v_type = VAR_STRING;
6150 item->di_tv.vval.v_string = vim_strsave(str);
6151 }
6152 if (dict_add(d, item) == FAIL)
6153 {
6154 dictitem_free(item);
6155 return FAIL;
6156 }
6157 return OK;
6158}
6159
6160/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006161 * Get the number of items in a Dictionary.
6162 */
6163 static long
6164dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006165 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006166{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006167 if (d == NULL)
6168 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006169 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006170}
6171
6172/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006173 * Find item "key[len]" in Dictionary "d".
6174 * If "len" is negative use strlen(key).
6175 * Returns NULL when not found.
6176 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006177 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006178dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006179 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006180 char_u *key;
6181 int len;
6182{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006183#define AKEYLEN 200
6184 char_u buf[AKEYLEN];
6185 char_u *akey;
6186 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006187 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006188
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006189 if (len < 0)
6190 akey = key;
6191 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006192 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006193 tofree = akey = vim_strnsave(key, len);
6194 if (akey == NULL)
6195 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006196 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006197 else
6198 {
6199 /* Avoid a malloc/free by using buf[]. */
6200 STRNCPY(buf, key, len);
6201 buf[len] = NUL;
6202 akey = buf;
6203 }
6204
Bram Moolenaar33570922005-01-25 22:26:29 +00006205 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006206 vim_free(tofree);
6207 if (HASHITEM_EMPTY(hi))
6208 return NULL;
6209 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006210}
6211
6212/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006213 * Get a string item from a dictionary in allocated memory.
6214 * Returns NULL if the entry doesn't exist or out of memory.
6215 */
6216 char_u *
6217get_dict_string(d, key)
6218 dict_T *d;
6219 char_u *key;
6220{
6221 dictitem_T *di;
6222
6223 di = dict_find(d, key, -1);
6224 if (di == NULL)
6225 return NULL;
6226 return vim_strsave(get_tv_string(&di->di_tv));
6227}
6228
6229/*
6230 * Get a number item from a dictionary.
6231 * Returns 0 if the entry doesn't exist or out of memory.
6232 */
6233 long
6234get_dict_number(d, key)
6235 dict_T *d;
6236 char_u *key;
6237{
6238 dictitem_T *di;
6239
6240 di = dict_find(d, key, -1);
6241 if (di == NULL)
6242 return 0;
6243 return get_tv_number(&di->di_tv);
6244}
6245
6246/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006247 * Return an allocated string with the string representation of a Dictionary.
6248 * May return NULL.
6249 */
6250 static char_u *
6251dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006252 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006253{
6254 garray_T ga;
6255 int first = TRUE;
6256 char_u *tofree;
6257 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006258 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006259 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006260 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006261 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006262
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006263 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006264 return NULL;
6265 ga_init2(&ga, (int)sizeof(char), 80);
6266 ga_append(&ga, '{');
6267
Bram Moolenaar33570922005-01-25 22:26:29 +00006268 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006269 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006270 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006271 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006272 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006273 --todo;
6274
6275 if (first)
6276 first = FALSE;
6277 else
6278 ga_concat(&ga, (char_u *)", ");
6279
6280 tofree = string_quote(hi->hi_key, FALSE);
6281 if (tofree != NULL)
6282 {
6283 ga_concat(&ga, tofree);
6284 vim_free(tofree);
6285 }
6286 ga_concat(&ga, (char_u *)": ");
6287 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6288 if (s != NULL)
6289 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006290 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006291 if (s == NULL)
6292 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006293 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006294 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006295 if (todo > 0)
6296 {
6297 vim_free(ga.ga_data);
6298 return NULL;
6299 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006300
6301 ga_append(&ga, '}');
6302 ga_append(&ga, NUL);
6303 return (char_u *)ga.ga_data;
6304}
6305
6306/*
6307 * Allocate a variable for a Dictionary and fill it from "*arg".
6308 * Return OK or FAIL. Returns NOTDONE for {expr}.
6309 */
6310 static int
6311get_dict_tv(arg, rettv, evaluate)
6312 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006314 int evaluate;
6315{
Bram Moolenaar33570922005-01-25 22:26:29 +00006316 dict_T *d = NULL;
6317 typval_T tvkey;
6318 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006319 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006321 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006322 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006323
6324 /*
6325 * First check if it's not a curly-braces thing: {expr}.
6326 * Must do this without evaluating, otherwise a function may be called
6327 * twice. Unfortunately this means we need to call eval1() twice for the
6328 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006329 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006330 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006331 if (*start != '}')
6332 {
6333 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6334 return FAIL;
6335 if (*start == '}')
6336 return NOTDONE;
6337 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006338
6339 if (evaluate)
6340 {
6341 d = dict_alloc();
6342 if (d == NULL)
6343 return FAIL;
6344 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006345 tvkey.v_type = VAR_UNKNOWN;
6346 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006347
6348 *arg = skipwhite(*arg + 1);
6349 while (**arg != '}' && **arg != NUL)
6350 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006351 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006352 goto failret;
6353 if (**arg != ':')
6354 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006355 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006356 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006357 goto failret;
6358 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006359 key = get_tv_string_buf_chk(&tvkey, buf);
6360 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006362 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6363 if (key != NULL)
6364 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006365 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006366 goto failret;
6367 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006368
6369 *arg = skipwhite(*arg + 1);
6370 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6371 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006372 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006373 goto failret;
6374 }
6375 if (evaluate)
6376 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006377 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006378 if (item != NULL)
6379 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006380 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006381 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006382 clear_tv(&tv);
6383 goto failret;
6384 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006385 item = dictitem_alloc(key);
6386 clear_tv(&tvkey);
6387 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006388 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006389 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006390 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006391 if (dict_add(d, item) == FAIL)
6392 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006393 }
6394 }
6395
6396 if (**arg == '}')
6397 break;
6398 if (**arg != ',')
6399 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006400 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006401 goto failret;
6402 }
6403 *arg = skipwhite(*arg + 1);
6404 }
6405
6406 if (**arg != '}')
6407 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006408 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006409failret:
6410 if (evaluate)
6411 dict_free(d);
6412 return FAIL;
6413 }
6414
6415 *arg = skipwhite(*arg + 1);
6416 if (evaluate)
6417 {
6418 rettv->v_type = VAR_DICT;
6419 rettv->vval.v_dict = d;
6420 ++d->dv_refcount;
6421 }
6422
6423 return OK;
6424}
6425
Bram Moolenaar8c711452005-01-14 21:53:12 +00006426/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427 * Return a string with the string representation of a variable.
6428 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006430 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431 * May return NULL;
6432 */
6433 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006434echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006435 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006436 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006437 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006438{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006439 static int recurse = 0;
6440 char_u *r = NULL;
6441
Bram Moolenaar33570922005-01-25 22:26:29 +00006442 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006443 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006444 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006445 *tofree = NULL;
6446 return NULL;
6447 }
6448 ++recurse;
6449
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006450 switch (tv->v_type)
6451 {
6452 case VAR_FUNC:
6453 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006454 r = tv->vval.v_string;
6455 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006456 case VAR_LIST:
6457 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006458 r = *tofree;
6459 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006460 case VAR_DICT:
6461 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006462 r = *tofree;
6463 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006464 case VAR_STRING:
6465 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006466 *tofree = NULL;
6467 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006468 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006469 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006470 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006471 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006472 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006473
6474 --recurse;
6475 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006476}
6477
6478/*
6479 * Return a string with the string representation of a variable.
6480 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6481 * "numbuf" is used for a number.
6482 * Puts quotes around strings, so that they can be parsed back by eval().
6483 * May return NULL;
6484 */
6485 static char_u *
6486tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006488 char_u **tofree;
6489 char_u *numbuf;
6490{
6491 switch (tv->v_type)
6492 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006493 case VAR_FUNC:
6494 *tofree = string_quote(tv->vval.v_string, TRUE);
6495 return *tofree;
6496 case VAR_STRING:
6497 *tofree = string_quote(tv->vval.v_string, FALSE);
6498 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006499 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006500 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006501 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006502 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006503 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006504 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006505 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006506 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006507}
6508
6509/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006510 * Return string "str" in ' quotes, doubling ' characters.
6511 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006512 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006513 */
6514 static char_u *
6515string_quote(str, function)
6516 char_u *str;
6517 int function;
6518{
Bram Moolenaar33570922005-01-25 22:26:29 +00006519 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006520 char_u *p, *r, *s;
6521
Bram Moolenaar33570922005-01-25 22:26:29 +00006522 len = (function ? 13 : 3);
6523 if (str != NULL)
6524 {
6525 len += STRLEN(str);
6526 for (p = str; *p != NUL; mb_ptr_adv(p))
6527 if (*p == '\'')
6528 ++len;
6529 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006530 s = r = alloc(len);
6531 if (r != NULL)
6532 {
6533 if (function)
6534 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006535 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006536 r += 10;
6537 }
6538 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006539 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006540 if (str != NULL)
6541 for (p = str; *p != NUL; )
6542 {
6543 if (*p == '\'')
6544 *r++ = '\'';
6545 MB_COPY_CHAR(p, r);
6546 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006547 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006548 if (function)
6549 *r++ = ')';
6550 *r++ = NUL;
6551 }
6552 return s;
6553}
6554
6555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 * Get the value of an environment variable.
6557 * "arg" is pointing to the '$'. It is advanced to after the name.
6558 * If the environment variable was not set, silently assume it is empty.
6559 * Always return OK.
6560 */
6561 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006562get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 int evaluate;
6566{
6567 char_u *string = NULL;
6568 int len;
6569 int cc;
6570 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006571 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
6573 ++*arg;
6574 name = *arg;
6575 len = get_env_len(arg);
6576 if (evaluate)
6577 {
6578 if (len != 0)
6579 {
6580 cc = name[len];
6581 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006582 /* first try vim_getenv(), fast for normal environment vars */
6583 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006585 {
6586 if (!mustfree)
6587 string = vim_strsave(string);
6588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 else
6590 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006591 if (mustfree)
6592 vim_free(string);
6593
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 /* next try expanding things like $VIM and ${HOME} */
6595 string = expand_env_save(name - 1);
6596 if (string != NULL && *string == '$')
6597 {
6598 vim_free(string);
6599 string = NULL;
6600 }
6601 }
6602 name[len] = cc;
6603 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006604 rettv->v_type = VAR_STRING;
6605 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 }
6607
6608 return OK;
6609}
6610
6611/*
6612 * Array with names and number of arguments of all internal functions
6613 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6614 */
6615static struct fst
6616{
6617 char *f_name; /* function name */
6618 char f_min_argc; /* minimal number of arguments */
6619 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006620 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622} functions[] =
6623{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006624 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 {"append", 2, 2, f_append},
6626 {"argc", 0, 0, f_argc},
6627 {"argidx", 0, 0, f_argidx},
6628 {"argv", 1, 1, f_argv},
6629 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006630 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 {"bufexists", 1, 1, f_bufexists},
6632 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6633 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6634 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6635 {"buflisted", 1, 1, f_buflisted},
6636 {"bufloaded", 1, 1, f_bufloaded},
6637 {"bufname", 1, 1, f_bufname},
6638 {"bufnr", 1, 1, f_bufnr},
6639 {"bufwinnr", 1, 1, f_bufwinnr},
6640 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006641 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006642 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 {"char2nr", 1, 1, f_char2nr},
6644 {"cindent", 1, 1, f_cindent},
6645 {"col", 1, 1, f_col},
6646 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006648 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 {"cscope_connection",0,3, f_cscope_connection},
6650 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006651 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 {"delete", 1, 1, f_delete},
6653 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006654 {"diff_filler", 1, 1, f_diff_filler},
6655 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006656 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006658 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 {"eventhandler", 0, 0, f_eventhandler},
6660 {"executable", 1, 1, f_executable},
6661 {"exists", 1, 1, f_exists},
6662 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006663 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6665 {"filereadable", 1, 1, f_filereadable},
6666 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006667 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006668 {"finddir", 1, 3, f_finddir},
6669 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 {"fnamemodify", 2, 2, f_fnamemodify},
6671 {"foldclosed", 1, 1, f_foldclosed},
6672 {"foldclosedend", 1, 1, f_foldclosedend},
6673 {"foldlevel", 1, 1, f_foldlevel},
6674 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006675 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006677 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006678 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006679 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 {"getbufvar", 2, 2, f_getbufvar},
6681 {"getchar", 0, 1, f_getchar},
6682 {"getcharmod", 0, 0, f_getcharmod},
6683 {"getcmdline", 0, 0, f_getcmdline},
6684 {"getcmdpos", 0, 0, f_getcmdpos},
6685 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006686 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006687 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 {"getfsize", 1, 1, f_getfsize},
6689 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006690 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006691 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006692 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006693 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 {"getregtype", 0, 1, f_getregtype},
6695 {"getwinposx", 0, 0, f_getwinposx},
6696 {"getwinposy", 0, 0, f_getwinposy},
6697 {"getwinvar", 2, 2, f_getwinvar},
6698 {"glob", 1, 1, f_glob},
6699 {"globpath", 2, 2, f_globpath},
6700 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006701 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 {"hasmapto", 1, 2, f_hasmapto},
6703 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6704 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6705 {"histadd", 2, 2, f_histadd},
6706 {"histdel", 1, 2, f_histdel},
6707 {"histget", 1, 2, f_histget},
6708 {"histnr", 1, 1, f_histnr},
6709 {"hlID", 1, 1, f_hlID},
6710 {"hlexists", 1, 1, f_hlexists},
6711 {"hostname", 0, 0, f_hostname},
6712 {"iconv", 3, 3, f_iconv},
6713 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006714 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 {"input", 1, 2, f_input},
6716 {"inputdialog", 1, 3, f_inputdialog},
6717 {"inputrestore", 0, 0, f_inputrestore},
6718 {"inputsave", 0, 0, f_inputsave},
6719 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006720 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006722 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006723 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006724 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006725 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006727 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 {"libcall", 3, 3, f_libcall},
6729 {"libcallnr", 3, 3, f_libcallnr},
6730 {"line", 1, 1, f_line},
6731 {"line2byte", 1, 1, f_line2byte},
6732 {"lispindent", 1, 1, f_lispindent},
6733 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006734 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 {"maparg", 1, 2, f_maparg},
6736 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006737 {"match", 2, 4, f_match},
6738 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006739 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006740 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006741 {"max", 1, 1, f_max},
6742 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006743#ifdef vim_mkdir
6744 {"mkdir", 1, 3, f_mkdir},
6745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 {"mode", 0, 0, f_mode},
6747 {"nextnonblank", 1, 1, f_nextnonblank},
6748 {"nr2char", 1, 1, f_nr2char},
6749 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006750 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006751 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 {"remote_expr", 2, 3, f_remote_expr},
6753 {"remote_foreground", 1, 1, f_remote_foreground},
6754 {"remote_peek", 1, 2, f_remote_peek},
6755 {"remote_read", 1, 1, f_remote_read},
6756 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006757 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006759 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006761 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {"search", 1, 2, f_search},
6763 {"searchpair", 3, 5, f_searchpair},
6764 {"server2client", 2, 2, f_server2client},
6765 {"serverlist", 0, 0, f_serverlist},
6766 {"setbufvar", 3, 3, f_setbufvar},
6767 {"setcmdpos", 1, 1, f_setcmdpos},
6768 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006769 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 {"setreg", 2, 3, f_setreg},
6771 {"setwinvar", 3, 3, f_setwinvar},
6772 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006773 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006774 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006775 {"spellbadword", 0, 0, f_spellbadword},
6776 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006777 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778#ifdef HAVE_STRFTIME
6779 {"strftime", 1, 2, f_strftime},
6780#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006781 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006782 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 {"strlen", 1, 1, f_strlen},
6784 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006785 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 {"strtrans", 1, 1, f_strtrans},
6787 {"submatch", 1, 1, f_submatch},
6788 {"substitute", 4, 4, f_substitute},
6789 {"synID", 3, 3, f_synID},
6790 {"synIDattr", 2, 3, f_synIDattr},
6791 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006792 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006793 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 {"tempname", 0, 0, f_tempname},
6795 {"tolower", 1, 1, f_tolower},
6796 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006797 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006799 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 {"virtcol", 1, 1, f_virtcol},
6801 {"visualmode", 0, 1, f_visualmode},
6802 {"winbufnr", 1, 1, f_winbufnr},
6803 {"wincol", 0, 0, f_wincol},
6804 {"winheight", 1, 1, f_winheight},
6805 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006806 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {"winrestcmd", 0, 0, f_winrestcmd},
6808 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006809 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810};
6811
6812#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6813
6814/*
6815 * Function given to ExpandGeneric() to obtain the list of internal
6816 * or user defined function names.
6817 */
6818 char_u *
6819get_function_name(xp, idx)
6820 expand_T *xp;
6821 int idx;
6822{
6823 static int intidx = -1;
6824 char_u *name;
6825
6826 if (idx == 0)
6827 intidx = -1;
6828 if (intidx < 0)
6829 {
6830 name = get_user_func_name(xp, idx);
6831 if (name != NULL)
6832 return name;
6833 }
6834 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6835 {
6836 STRCPY(IObuff, functions[intidx].f_name);
6837 STRCAT(IObuff, "(");
6838 if (functions[intidx].f_max_argc == 0)
6839 STRCAT(IObuff, ")");
6840 return IObuff;
6841 }
6842
6843 return NULL;
6844}
6845
6846/*
6847 * Function given to ExpandGeneric() to obtain the list of internal or
6848 * user defined variable or function names.
6849 */
6850/*ARGSUSED*/
6851 char_u *
6852get_expr_name(xp, idx)
6853 expand_T *xp;
6854 int idx;
6855{
6856 static int intidx = -1;
6857 char_u *name;
6858
6859 if (idx == 0)
6860 intidx = -1;
6861 if (intidx < 0)
6862 {
6863 name = get_function_name(xp, idx);
6864 if (name != NULL)
6865 return name;
6866 }
6867 return get_user_var_name(xp, ++intidx);
6868}
6869
6870#endif /* FEAT_CMDL_COMPL */
6871
6872/*
6873 * Find internal function in table above.
6874 * Return index, or -1 if not found
6875 */
6876 static int
6877find_internal_func(name)
6878 char_u *name; /* name of the function */
6879{
6880 int first = 0;
6881 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6882 int cmp;
6883 int x;
6884
6885 /*
6886 * Find the function name in the table. Binary search.
6887 */
6888 while (first <= last)
6889 {
6890 x = first + ((unsigned)(last - first) >> 1);
6891 cmp = STRCMP(name, functions[x].f_name);
6892 if (cmp < 0)
6893 last = x - 1;
6894 else if (cmp > 0)
6895 first = x + 1;
6896 else
6897 return x;
6898 }
6899 return -1;
6900}
6901
6902/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006903 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6904 * name it contains, otherwise return "name".
6905 */
6906 static char_u *
6907deref_func_name(name, lenp)
6908 char_u *name;
6909 int *lenp;
6910{
Bram Moolenaar33570922005-01-25 22:26:29 +00006911 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006912 int cc;
6913
6914 cc = name[*lenp];
6915 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006916 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006917 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006918 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006919 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006920 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006921 {
6922 *lenp = 0;
6923 return (char_u *)""; /* just in case */
6924 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006925 *lenp = STRLEN(v->di_tv.vval.v_string);
6926 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006927 }
6928
6929 return name;
6930}
6931
6932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 * Allocate a variable for the result of a function.
6934 * Return OK or FAIL.
6935 */
6936 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006937get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6938 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 char_u *name; /* name of the function */
6940 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 char_u **arg; /* argument, pointing to the '(' */
6943 linenr_T firstline; /* first line of range */
6944 linenr_T lastline; /* last line of range */
6945 int *doesrange; /* return: function handled range */
6946 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006947 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948{
6949 char_u *argp;
6950 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006951 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 int argcount = 0; /* number of arguments found */
6953
6954 /*
6955 * Get the arguments.
6956 */
6957 argp = *arg;
6958 while (argcount < MAX_FUNC_ARGS)
6959 {
6960 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6961 if (*argp == ')' || *argp == ',' || *argp == NUL)
6962 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6964 {
6965 ret = FAIL;
6966 break;
6967 }
6968 ++argcount;
6969 if (*argp != ',')
6970 break;
6971 }
6972 if (*argp == ')')
6973 ++argp;
6974 else
6975 ret = FAIL;
6976
6977 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006978 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006979 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006981 {
6982 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006983 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006984 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006985 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987
6988 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006989 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990
6991 *arg = skipwhite(argp);
6992 return ret;
6993}
6994
6995
6996/*
6997 * Call a function with its resolved parameters
6998 * Return OK or FAIL.
6999 */
7000 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007001call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007002 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 char_u *name; /* name of the function */
7004 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007005 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007007 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 linenr_T firstline; /* first line of range */
7009 linenr_T lastline; /* last line of range */
7010 int *doesrange; /* return: function handled range */
7011 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007012 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013{
7014 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015#define ERROR_UNKNOWN 0
7016#define ERROR_TOOMANY 1
7017#define ERROR_TOOFEW 2
7018#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007019#define ERROR_DICT 4
7020#define ERROR_NONE 5
7021#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 int error = ERROR_NONE;
7023 int i;
7024 int llen;
7025 ufunc_T *fp;
7026 int cc;
7027#define FLEN_FIXED 40
7028 char_u fname_buf[FLEN_FIXED + 1];
7029 char_u *fname;
7030
7031 /*
7032 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7033 * Change <SNR>123_name() to K_SNR 123_name().
7034 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7035 */
7036 cc = name[len];
7037 name[len] = NUL;
7038 llen = eval_fname_script(name);
7039 if (llen > 0)
7040 {
7041 fname_buf[0] = K_SPECIAL;
7042 fname_buf[1] = KS_EXTRA;
7043 fname_buf[2] = (int)KE_SNR;
7044 i = 3;
7045 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7046 {
7047 if (current_SID <= 0)
7048 error = ERROR_SCRIPT;
7049 else
7050 {
7051 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7052 i = (int)STRLEN(fname_buf);
7053 }
7054 }
7055 if (i + STRLEN(name + llen) < FLEN_FIXED)
7056 {
7057 STRCPY(fname_buf + i, name + llen);
7058 fname = fname_buf;
7059 }
7060 else
7061 {
7062 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7063 if (fname == NULL)
7064 error = ERROR_OTHER;
7065 else
7066 {
7067 mch_memmove(fname, fname_buf, (size_t)i);
7068 STRCPY(fname + i, name + llen);
7069 }
7070 }
7071 }
7072 else
7073 fname = name;
7074
7075 *doesrange = FALSE;
7076
7077
7078 /* execute the function if no errors detected and executing */
7079 if (evaluate && error == ERROR_NONE)
7080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007081 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 error = ERROR_UNKNOWN;
7083
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007084 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 {
7086 /*
7087 * User defined function.
7088 */
7089 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007090
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007092 /* Trigger FuncUndefined event, may load the function. */
7093 if (fp == NULL
7094 && apply_autocmds(EVENT_FUNCUNDEFINED,
7095 fname, fname, TRUE, NULL)
7096 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007098 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 fp = find_func(fname);
7100 }
7101#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007102 /* Try loading a package. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007103 if (fp == NULL && script_autoload(fname) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007104 {
7105 /* loaded a package, search for the function again */
7106 fp = find_func(fname);
7107 }
7108
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 if (fp != NULL)
7110 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007111 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007113 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007115 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007117 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007118 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 else
7120 {
7121 /*
7122 * Call the user function.
7123 * Save and restore search patterns, script variables and
7124 * redo buffer.
7125 */
7126 save_search_patterns();
7127 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007128 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007129 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007130 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007131 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7132 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7133 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007134 /* Function was unreferenced while being used, free it
7135 * now. */
7136 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 restoreRedobuff();
7138 restore_search_patterns();
7139 error = ERROR_NONE;
7140 }
7141 }
7142 }
7143 else
7144 {
7145 /*
7146 * Find the function name in the table, call its implementation.
7147 */
7148 i = find_internal_func(fname);
7149 if (i >= 0)
7150 {
7151 if (argcount < functions[i].f_min_argc)
7152 error = ERROR_TOOFEW;
7153 else if (argcount > functions[i].f_max_argc)
7154 error = ERROR_TOOMANY;
7155 else
7156 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007157 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007158 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 error = ERROR_NONE;
7160 }
7161 }
7162 }
7163 /*
7164 * The function call (or "FuncUndefined" autocommand sequence) might
7165 * have been aborted by an error, an interrupt, or an explicitly thrown
7166 * exception that has not been caught so far. This situation can be
7167 * tested for by calling aborting(). For an error in an internal
7168 * function or for the "E132" error in call_user_func(), however, the
7169 * throw point at which the "force_abort" flag (temporarily reset by
7170 * emsg()) is normally updated has not been reached yet. We need to
7171 * update that flag first to make aborting() reliable.
7172 */
7173 update_force_abort();
7174 }
7175 if (error == ERROR_NONE)
7176 ret = OK;
7177
7178 /*
7179 * Report an error unless the argument evaluation or function call has been
7180 * cancelled due to an aborting error, an interrupt, or an exception.
7181 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007182 if (!aborting())
7183 {
7184 switch (error)
7185 {
7186 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007187 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007188 break;
7189 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007190 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007191 break;
7192 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007193 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007194 name);
7195 break;
7196 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007197 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198 name);
7199 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007200 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007201 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007202 name);
7203 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007204 }
7205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206
7207 name[len] = cc;
7208 if (fname != name && fname != fname_buf)
7209 vim_free(fname);
7210
7211 return ret;
7212}
7213
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007214/*
7215 * Give an error message with a function name. Handle <SNR> things.
7216 */
7217 static void
7218emsg_funcname(msg, name)
7219 char *msg;
7220 char_u *name;
7221{
7222 char_u *p;
7223
7224 if (*name == K_SPECIAL)
7225 p = concat_str((char_u *)"<SNR>", name + 3);
7226 else
7227 p = name;
7228 EMSG2(_(msg), p);
7229 if (p != name)
7230 vim_free(p);
7231}
7232
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233/*********************************************
7234 * Implementation of the built-in functions
7235 */
7236
7237/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007238 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 */
7240 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007241f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007242 typval_T *argvars;
7243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244{
Bram Moolenaar33570922005-01-25 22:26:29 +00007245 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007247 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007248 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007250 if ((l = argvars[0].vval.v_list) != NULL
7251 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7252 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007253 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007254 }
7255 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007256 EMSG(_(e_listreq));
7257}
7258
7259/*
7260 * "append(lnum, string/list)" function
7261 */
7262 static void
7263f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007264 typval_T *argvars;
7265 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007266{
7267 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007268 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007269 list_T *l = NULL;
7270 listitem_T *li = NULL;
7271 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007272 long added = 0;
7273
Bram Moolenaar0d660222005-01-07 21:51:51 +00007274 lnum = get_tv_lnum(argvars);
7275 if (lnum >= 0
7276 && lnum <= curbuf->b_ml.ml_line_count
7277 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007278 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007279 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007280 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007281 l = argvars[1].vval.v_list;
7282 if (l == NULL)
7283 return;
7284 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007285 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007286 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007287 for (;;)
7288 {
7289 if (l == NULL)
7290 tv = &argvars[1]; /* append a string */
7291 else if (li == NULL)
7292 break; /* end of list */
7293 else
7294 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007295 line = get_tv_string_chk(tv);
7296 if (line == NULL) /* type error */
7297 {
7298 rettv->vval.v_number = 1; /* Failed */
7299 break;
7300 }
7301 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007302 ++added;
7303 if (l == NULL)
7304 break;
7305 li = li->li_next;
7306 }
7307
7308 appended_lines_mark(lnum, added);
7309 if (curwin->w_cursor.lnum > lnum)
7310 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007312 else
7313 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314}
7315
7316/*
7317 * "argc()" function
7318 */
7319/* ARGSUSED */
7320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007321f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 typval_T *argvars;
7323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007325 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326}
7327
7328/*
7329 * "argidx()" function
7330 */
7331/* ARGSUSED */
7332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007333f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 typval_T *argvars;
7335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007337 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338}
7339
7340/*
7341 * "argv(nr)" function
7342 */
7343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007344f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007345 typval_T *argvars;
7346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347{
7348 int idx;
7349
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007350 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007352 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007354 rettv->vval.v_string = NULL;
7355 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356}
7357
7358/*
7359 * "browse(save, title, initdir, default)" function
7360 */
7361/* ARGSUSED */
7362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007363f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007364 typval_T *argvars;
7365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366{
7367#ifdef FEAT_BROWSE
7368 int save;
7369 char_u *title;
7370 char_u *initdir;
7371 char_u *defname;
7372 char_u buf[NUMBUFLEN];
7373 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007374 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007376 save = get_tv_number_chk(&argvars[0], &error);
7377 title = get_tv_string_chk(&argvars[1]);
7378 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7379 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007381 if (error || title == NULL || initdir == NULL || defname == NULL)
7382 rettv->vval.v_string = NULL;
7383 else
7384 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007385 do_browse(save ? BROWSE_SAVE : 0,
7386 title, defname, NULL, initdir, NULL, curbuf);
7387#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007388 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007389#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007390 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007391}
7392
7393/*
7394 * "browsedir(title, initdir)" function
7395 */
7396/* ARGSUSED */
7397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007398f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007399 typval_T *argvars;
7400 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007401{
7402#ifdef FEAT_BROWSE
7403 char_u *title;
7404 char_u *initdir;
7405 char_u buf[NUMBUFLEN];
7406
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007407 title = get_tv_string_chk(&argvars[0]);
7408 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007409
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007410 if (title == NULL || initdir == NULL)
7411 rettv->vval.v_string = NULL;
7412 else
7413 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007414 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007416 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007418 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419}
7420
Bram Moolenaar33570922005-01-25 22:26:29 +00007421static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007422
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423/*
7424 * Find a buffer by number or exact name.
7425 */
7426 static buf_T *
7427find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429{
7430 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007432 if (avar->v_type == VAR_NUMBER)
7433 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007434 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007436 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007437 if (buf == NULL)
7438 {
7439 /* No full path name match, try a match with a URL or a "nofile"
7440 * buffer, these don't use the full path. */
7441 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7442 if (buf->b_fname != NULL
7443 && (path_with_url(buf->b_fname)
7444#ifdef FEAT_QUICKFIX
7445 || bt_nofile(buf)
7446#endif
7447 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007448 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007449 break;
7450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 }
7452 return buf;
7453}
7454
7455/*
7456 * "bufexists(expr)" function
7457 */
7458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007459f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007460 typval_T *argvars;
7461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007463 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464}
7465
7466/*
7467 * "buflisted(expr)" function
7468 */
7469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007470f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007471 typval_T *argvars;
7472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473{
7474 buf_T *buf;
7475
7476 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007477 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478}
7479
7480/*
7481 * "bufloaded(expr)" function
7482 */
7483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007484f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007485 typval_T *argvars;
7486 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487{
7488 buf_T *buf;
7489
7490 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007491 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492}
7493
Bram Moolenaar33570922005-01-25 22:26:29 +00007494static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007495
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496/*
7497 * Get buffer by number or pattern.
7498 */
7499 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007500get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007503 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 int save_magic;
7505 char_u *save_cpo;
7506 buf_T *buf;
7507
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007508 if (tv->v_type == VAR_NUMBER)
7509 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007510 if (tv->v_type != VAR_STRING)
7511 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 if (name == NULL || *name == NUL)
7513 return curbuf;
7514 if (name[0] == '$' && name[1] == NUL)
7515 return lastbuf;
7516
7517 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7518 save_magic = p_magic;
7519 p_magic = TRUE;
7520 save_cpo = p_cpo;
7521 p_cpo = (char_u *)"";
7522
7523 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7524 TRUE, FALSE));
7525
7526 p_magic = save_magic;
7527 p_cpo = save_cpo;
7528
7529 /* If not found, try expanding the name, like done for bufexists(). */
7530 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007531 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532
7533 return buf;
7534}
7535
7536/*
7537 * "bufname(expr)" function
7538 */
7539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007540f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007541 typval_T *argvars;
7542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543{
7544 buf_T *buf;
7545
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007546 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007548 buf = get_buf_tv(&argvars[0]);
7549 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007551 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007553 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 --emsg_off;
7555}
7556
7557/*
7558 * "bufnr(expr)" function
7559 */
7560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007561f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007562 typval_T *argvars;
7563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564{
7565 buf_T *buf;
7566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007567 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007569 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007571 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007573 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 --emsg_off;
7575}
7576
7577/*
7578 * "bufwinnr(nr)" function
7579 */
7580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007581f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007582 typval_T *argvars;
7583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584{
7585#ifdef FEAT_WINDOWS
7586 win_T *wp;
7587 int winnr = 0;
7588#endif
7589 buf_T *buf;
7590
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007591 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007593 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594#ifdef FEAT_WINDOWS
7595 for (wp = firstwin; wp; wp = wp->w_next)
7596 {
7597 ++winnr;
7598 if (wp->w_buffer == buf)
7599 break;
7600 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007601 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007603 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604#endif
7605 --emsg_off;
7606}
7607
7608/*
7609 * "byte2line(byte)" function
7610 */
7611/*ARGSUSED*/
7612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007613f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007614 typval_T *argvars;
7615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616{
7617#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007618 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619#else
7620 long boff = 0;
7621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007622 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007624 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007626 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 (linenr_T)0, &boff);
7628#endif
7629}
7630
7631/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007632 * "byteidx()" function
7633 */
7634/*ARGSUSED*/
7635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007636f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007637 typval_T *argvars;
7638 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007639{
7640#ifdef FEAT_MBYTE
7641 char_u *t;
7642#endif
7643 char_u *str;
7644 long idx;
7645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007646 str = get_tv_string_chk(&argvars[0]);
7647 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007648 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007649 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007650 return;
7651
7652#ifdef FEAT_MBYTE
7653 t = str;
7654 for ( ; idx > 0; idx--)
7655 {
7656 if (*t == NUL) /* EOL reached */
7657 return;
7658 t += mb_ptr2len_check(t);
7659 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007660 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007661#else
7662 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007663 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007664#endif
7665}
7666
7667/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007668 * "call(func, arglist)" function
7669 */
7670 static void
7671f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007672 typval_T *argvars;
7673 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007674{
7675 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007676 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007677 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007678 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007679 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007680 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007681
7682 rettv->vval.v_number = 0;
7683 if (argvars[1].v_type != VAR_LIST)
7684 {
7685 EMSG(_(e_listreq));
7686 return;
7687 }
7688 if (argvars[1].vval.v_list == NULL)
7689 return;
7690
7691 if (argvars[0].v_type == VAR_FUNC)
7692 func = argvars[0].vval.v_string;
7693 else
7694 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007695 if (*func == NUL)
7696 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007697
Bram Moolenaare9a41262005-01-15 22:18:47 +00007698 if (argvars[2].v_type != VAR_UNKNOWN)
7699 {
7700 if (argvars[2].v_type != VAR_DICT)
7701 {
7702 EMSG(_(e_dictreq));
7703 return;
7704 }
7705 selfdict = argvars[2].vval.v_dict;
7706 }
7707
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007708 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7709 item = item->li_next)
7710 {
7711 if (argc == MAX_FUNC_ARGS)
7712 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007713 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007714 break;
7715 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007716 /* Make a copy of each argument. This is needed to be able to set
7717 * v_lock to VAR_FIXED in the copy without changing the original list.
7718 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007719 copy_tv(&item->li_tv, &argv[argc++]);
7720 }
7721
7722 if (item == NULL)
7723 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007724 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7725 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007726
7727 /* Free the arguments. */
7728 while (argc > 0)
7729 clear_tv(&argv[--argc]);
7730}
7731
7732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 * "char2nr(string)" function
7734 */
7735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007736f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 typval_T *argvars;
7738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739{
7740#ifdef FEAT_MBYTE
7741 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007742 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 else
7744#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007745 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746}
7747
7748/*
7749 * "cindent(lnum)" function
7750 */
7751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007752f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007753 typval_T *argvars;
7754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755{
7756#ifdef FEAT_CINDENT
7757 pos_T pos;
7758 linenr_T lnum;
7759
7760 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007761 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7763 {
7764 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 curwin->w_cursor = pos;
7767 }
7768 else
7769#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007770 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771}
7772
7773/*
7774 * "col(string)" function
7775 */
7776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007777f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007778 typval_T *argvars;
7779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780{
7781 colnr_T col = 0;
7782 pos_T *fp;
7783
7784 fp = var2fpos(&argvars[0], FALSE);
7785 if (fp != NULL)
7786 {
7787 if (fp->col == MAXCOL)
7788 {
7789 /* '> can be MAXCOL, get the length of the line then */
7790 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7791 col = STRLEN(ml_get(fp->lnum)) + 1;
7792 else
7793 col = MAXCOL;
7794 }
7795 else
7796 {
7797 col = fp->col + 1;
7798#ifdef FEAT_VIRTUALEDIT
7799 /* col(".") when the cursor is on the NUL at the end of the line
7800 * because of "coladd" can be seen as an extra column. */
7801 if (virtual_active() && fp == &curwin->w_cursor)
7802 {
7803 char_u *p = ml_get_cursor();
7804
7805 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7806 curwin->w_virtcol - curwin->w_cursor.coladd))
7807 {
7808# ifdef FEAT_MBYTE
7809 int l;
7810
7811 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7812 col += l;
7813# else
7814 if (*p != NUL && p[1] == NUL)
7815 ++col;
7816# endif
7817 }
7818 }
7819#endif
7820 }
7821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007822 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823}
7824
7825/*
7826 * "confirm(message, buttons[, default [, type]])" function
7827 */
7828/*ARGSUSED*/
7829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007830f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007831 typval_T *argvars;
7832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833{
7834#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7835 char_u *message;
7836 char_u *buttons = NULL;
7837 char_u buf[NUMBUFLEN];
7838 char_u buf2[NUMBUFLEN];
7839 int def = 1;
7840 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007841 char_u *typestr;
7842 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007844 message = get_tv_string_chk(&argvars[0]);
7845 if (message == NULL)
7846 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007847 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007849 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7850 if (buttons == NULL)
7851 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007852 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007854 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007855 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007857 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7858 if (typestr == NULL)
7859 error = TRUE;
7860 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007862 switch (TOUPPER_ASC(*typestr))
7863 {
7864 case 'E': type = VIM_ERROR; break;
7865 case 'Q': type = VIM_QUESTION; break;
7866 case 'I': type = VIM_INFO; break;
7867 case 'W': type = VIM_WARNING; break;
7868 case 'G': type = VIM_GENERIC; break;
7869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 }
7871 }
7872 }
7873 }
7874
7875 if (buttons == NULL || *buttons == NUL)
7876 buttons = (char_u *)_("&Ok");
7877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007878 if (error)
7879 rettv->vval.v_number = 0;
7880 else
7881 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 def, NULL);
7883#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007884 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885#endif
7886}
7887
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007888/*
7889 * "copy()" function
7890 */
7891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007892f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007893 typval_T *argvars;
7894 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007895{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007896 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007897}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898
7899/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007900 * "count()" function
7901 */
7902 static void
7903f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 typval_T *argvars;
7905 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007906{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007907 long n = 0;
7908 int ic = FALSE;
7909
Bram Moolenaare9a41262005-01-15 22:18:47 +00007910 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007911 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007912 listitem_T *li;
7913 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007914 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007915
Bram Moolenaare9a41262005-01-15 22:18:47 +00007916 if ((l = argvars[0].vval.v_list) != NULL)
7917 {
7918 li = l->lv_first;
7919 if (argvars[2].v_type != VAR_UNKNOWN)
7920 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007921 int error = FALSE;
7922
7923 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007924 if (argvars[3].v_type != VAR_UNKNOWN)
7925 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007926 idx = get_tv_number_chk(&argvars[3], &error);
7927 if (!error)
7928 {
7929 li = list_find(l, idx);
7930 if (li == NULL)
7931 EMSGN(_(e_listidx), idx);
7932 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007933 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007934 if (error)
7935 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007936 }
7937
7938 for ( ; li != NULL; li = li->li_next)
7939 if (tv_equal(&li->li_tv, &argvars[1], ic))
7940 ++n;
7941 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007942 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007943 else if (argvars[0].v_type == VAR_DICT)
7944 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 int todo;
7946 dict_T *d;
7947 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007948
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007949 if ((d = argvars[0].vval.v_dict) != NULL)
7950 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007951 int error = FALSE;
7952
Bram Moolenaare9a41262005-01-15 22:18:47 +00007953 if (argvars[2].v_type != VAR_UNKNOWN)
7954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007955 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007956 if (argvars[3].v_type != VAR_UNKNOWN)
7957 EMSG(_(e_invarg));
7958 }
7959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007960 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007961 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007962 {
7963 if (!HASHITEM_EMPTY(hi))
7964 {
7965 --todo;
7966 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7967 ++n;
7968 }
7969 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007970 }
7971 }
7972 else
7973 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007974 rettv->vval.v_number = n;
7975}
7976
7977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7979 *
7980 * Checks the existence of a cscope connection.
7981 */
7982/*ARGSUSED*/
7983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007985 typval_T *argvars;
7986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
7988#ifdef FEAT_CSCOPE
7989 int num = 0;
7990 char_u *dbpath = NULL;
7991 char_u *prepend = NULL;
7992 char_u buf[NUMBUFLEN];
7993
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007994 if (argvars[0].v_type != VAR_UNKNOWN
7995 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007997 num = (int)get_tv_number(&argvars[0]);
7998 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007999 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008000 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 }
8002
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008003 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008005 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006#endif
8007}
8008
8009/*
8010 * "cursor(lnum, col)" function
8011 *
8012 * Moves the cursor to the specified line and column
8013 */
8014/*ARGSUSED*/
8015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008016f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008017 typval_T *argvars;
8018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019{
8020 long line, col;
8021
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008022 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008023 col = get_tv_number_chk(&argvars[1], NULL);
8024 if (line < 0 || col < 0)
8025 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 if (line > 0)
8027 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 if (col > 0)
8029 curwin->w_cursor.col = col - 1;
8030#ifdef FEAT_VIRTUALEDIT
8031 curwin->w_cursor.coladd = 0;
8032#endif
8033
8034 /* Make sure the cursor is in a valid position. */
8035 check_cursor();
8036#ifdef FEAT_MBYTE
8037 /* Correct cursor for multi-byte character. */
8038 if (has_mbyte)
8039 mb_adjust_cursor();
8040#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008041
8042 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043}
8044
8045/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008046 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 */
8048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008049f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008050 typval_T *argvars;
8051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008053 int noref = 0;
8054
8055 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008056 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008057 if (noref < 0 || noref > 1)
8058 EMSG(_(e_invarg));
8059 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008060 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061}
8062
8063/*
8064 * "delete()" function
8065 */
8066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008067f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008068 typval_T *argvars;
8069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070{
8071 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008072 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008074 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075}
8076
8077/*
8078 * "did_filetype()" function
8079 */
8080/*ARGSUSED*/
8081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008082f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008083 typval_T *argvars;
8084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085{
8086#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008087 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008089 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090#endif
8091}
8092
8093/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008094 * "diff_filler()" function
8095 */
8096/*ARGSUSED*/
8097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008098f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008099 typval_T *argvars;
8100 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008101{
8102#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008103 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008104#endif
8105}
8106
8107/*
8108 * "diff_hlID()" function
8109 */
8110/*ARGSUSED*/
8111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008112f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008113 typval_T *argvars;
8114 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008115{
8116#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008117 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008118 static linenr_T prev_lnum = 0;
8119 static int changedtick = 0;
8120 static int fnum = 0;
8121 static int change_start = 0;
8122 static int change_end = 0;
8123 static enum hlf_value hlID = 0;
8124 int filler_lines;
8125 int col;
8126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008127 if (lnum < 0) /* ignore type error in {lnum} arg */
8128 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008129 if (lnum != prev_lnum
8130 || changedtick != curbuf->b_changedtick
8131 || fnum != curbuf->b_fnum)
8132 {
8133 /* New line, buffer, change: need to get the values. */
8134 filler_lines = diff_check(curwin, lnum);
8135 if (filler_lines < 0)
8136 {
8137 if (filler_lines == -1)
8138 {
8139 change_start = MAXCOL;
8140 change_end = -1;
8141 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8142 hlID = HLF_ADD; /* added line */
8143 else
8144 hlID = HLF_CHD; /* changed line */
8145 }
8146 else
8147 hlID = HLF_ADD; /* added line */
8148 }
8149 else
8150 hlID = (enum hlf_value)0;
8151 prev_lnum = lnum;
8152 changedtick = curbuf->b_changedtick;
8153 fnum = curbuf->b_fnum;
8154 }
8155
8156 if (hlID == HLF_CHD || hlID == HLF_TXD)
8157 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008158 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008159 if (col >= change_start && col <= change_end)
8160 hlID = HLF_TXD; /* changed text */
8161 else
8162 hlID = HLF_CHD; /* changed line */
8163 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008164 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008165#endif
8166}
8167
8168/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008169 * "empty({expr})" function
8170 */
8171 static void
8172f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008173 typval_T *argvars;
8174 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008175{
8176 int n;
8177
8178 switch (argvars[0].v_type)
8179 {
8180 case VAR_STRING:
8181 case VAR_FUNC:
8182 n = argvars[0].vval.v_string == NULL
8183 || *argvars[0].vval.v_string == NUL;
8184 break;
8185 case VAR_NUMBER:
8186 n = argvars[0].vval.v_number == 0;
8187 break;
8188 case VAR_LIST:
8189 n = argvars[0].vval.v_list == NULL
8190 || argvars[0].vval.v_list->lv_first == NULL;
8191 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008192 case VAR_DICT:
8193 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008194 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008195 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008196 default:
8197 EMSG2(_(e_intern2), "f_empty()");
8198 n = 0;
8199 }
8200
8201 rettv->vval.v_number = n;
8202}
8203
8204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 * "escape({string}, {chars})" function
8206 */
8207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008208f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008209 typval_T *argvars;
8210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211{
8212 char_u buf[NUMBUFLEN];
8213
Bram Moolenaar758711c2005-02-02 23:11:38 +00008214 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8215 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008216 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217}
8218
8219/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008220 * "eval()" function
8221 */
8222/*ARGSUSED*/
8223 static void
8224f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008225 typval_T *argvars;
8226 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008227{
8228 char_u *s;
8229
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008230 s = get_tv_string_chk(&argvars[0]);
8231 if (s != NULL)
8232 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008233
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008234 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8235 {
8236 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008237 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008238 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008239 else if (*s != NUL)
8240 EMSG(_(e_trailing));
8241}
8242
8243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 * "eventhandler()" function
8245 */
8246/*ARGSUSED*/
8247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008248f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008249 typval_T *argvars;
8250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008252 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253}
8254
8255/*
8256 * "executable()" function
8257 */
8258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008259f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008260 typval_T *argvars;
8261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008263 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264}
8265
8266/*
8267 * "exists()" function
8268 */
8269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008270f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008271 typval_T *argvars;
8272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273{
8274 char_u *p;
8275 char_u *name;
8276 int n = FALSE;
8277 int len = 0;
8278
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008279 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 if (*p == '$') /* environment variable */
8281 {
8282 /* first try "normal" environment variables (fast) */
8283 if (mch_getenv(p + 1) != NULL)
8284 n = TRUE;
8285 else
8286 {
8287 /* try expanding things like $VIM and ${HOME} */
8288 p = expand_env_save(p);
8289 if (p != NULL && *p != '$')
8290 n = TRUE;
8291 vim_free(p);
8292 }
8293 }
8294 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008295 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 else if (*p == '*') /* internal or user defined function */
8297 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008298 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 }
8300 else if (*p == ':')
8301 {
8302 n = cmd_exists(p + 1);
8303 }
8304 else if (*p == '#')
8305 {
8306#ifdef FEAT_AUTOCMD
8307 name = p + 1;
8308 p = vim_strchr(name, '#');
8309 if (p != NULL)
8310 n = au_exists(name, p, p + 1);
8311 else
8312 n = au_exists(name, name + STRLEN(name), NULL);
8313#endif
8314 }
8315 else /* internal variable */
8316 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008317 char_u *tofree;
8318 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008320 /* get_name_len() takes care of expanding curly braces */
8321 name = p;
8322 len = get_name_len(&p, &tofree, TRUE, FALSE);
8323 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008325 if (tofree != NULL)
8326 name = tofree;
8327 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8328 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008330 /* handle d.key, l[idx], f(expr) */
8331 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8332 if (n)
8333 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 }
8335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008337 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 }
8339
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008340 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341}
8342
8343/*
8344 * "expand()" function
8345 */
8346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008347f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008348 typval_T *argvars;
8349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350{
8351 char_u *s;
8352 int len;
8353 char_u *errormsg;
8354 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8355 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008356 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008358 rettv->v_type = VAR_STRING;
8359 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 if (*s == '%' || *s == '#' || *s == '<')
8361 {
8362 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008363 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 --emsg_off;
8365 }
8366 else
8367 {
8368 /* When the optional second argument is non-zero, don't remove matches
8369 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008370 if (argvars[1].v_type != VAR_UNKNOWN
8371 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008373 if (!error)
8374 {
8375 ExpandInit(&xpc);
8376 xpc.xp_context = EXPAND_FILES;
8377 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8378 ExpandCleanup(&xpc);
8379 }
8380 else
8381 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 }
8383}
8384
8385/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008386 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008387 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008388 */
8389 static void
8390f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008391 typval_T *argvars;
8392 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008393{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008394 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008395 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008396 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008397 list_T *l1, *l2;
8398 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008399 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008400 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008401
Bram Moolenaare9a41262005-01-15 22:18:47 +00008402 l1 = argvars[0].vval.v_list;
8403 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008404 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8405 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008406 {
8407 if (argvars[2].v_type != VAR_UNKNOWN)
8408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008409 before = get_tv_number_chk(&argvars[2], &error);
8410 if (error)
8411 return; /* type error; errmsg already given */
8412
Bram Moolenaar758711c2005-02-02 23:11:38 +00008413 if (before == l1->lv_len)
8414 item = NULL;
8415 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008416 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008417 item = list_find(l1, before);
8418 if (item == NULL)
8419 {
8420 EMSGN(_(e_listidx), before);
8421 return;
8422 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008423 }
8424 }
8425 else
8426 item = NULL;
8427 list_extend(l1, l2, item);
8428
Bram Moolenaare9a41262005-01-15 22:18:47 +00008429 copy_tv(&argvars[0], rettv);
8430 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008431 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008432 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8433 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008434 dict_T *d1, *d2;
8435 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008436 char_u *action;
8437 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008438 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008439 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008440
8441 d1 = argvars[0].vval.v_dict;
8442 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008443 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8444 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008445 {
8446 /* Check the third argument. */
8447 if (argvars[2].v_type != VAR_UNKNOWN)
8448 {
8449 static char *(av[]) = {"keep", "force", "error"};
8450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008451 action = get_tv_string_chk(&argvars[2]);
8452 if (action == NULL)
8453 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008454 for (i = 0; i < 3; ++i)
8455 if (STRCMP(action, av[i]) == 0)
8456 break;
8457 if (i == 3)
8458 {
8459 EMSGN(_(e_invarg2), action);
8460 return;
8461 }
8462 }
8463 else
8464 action = (char_u *)"force";
8465
8466 /* Go over all entries in the second dict and add them to the
8467 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008468 todo = d2->dv_hashtab.ht_used;
8469 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008470 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008471 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008472 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008473 --todo;
8474 di1 = dict_find(d1, hi2->hi_key, -1);
8475 if (di1 == NULL)
8476 {
8477 di1 = dictitem_copy(HI2DI(hi2));
8478 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8479 dictitem_free(di1);
8480 }
8481 else if (*action == 'e')
8482 {
8483 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8484 break;
8485 }
8486 else if (*action == 'f')
8487 {
8488 clear_tv(&di1->di_tv);
8489 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8490 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008491 }
8492 }
8493
Bram Moolenaare9a41262005-01-15 22:18:47 +00008494 copy_tv(&argvars[0], rettv);
8495 }
8496 }
8497 else
8498 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008499}
8500
8501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 * "filereadable()" function
8503 */
8504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008505f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008506 typval_T *argvars;
8507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508{
8509 FILE *fd;
8510 char_u *p;
8511 int n;
8512
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008513 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8515 {
8516 n = TRUE;
8517 fclose(fd);
8518 }
8519 else
8520 n = FALSE;
8521
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008522 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523}
8524
8525/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008526 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 * rights to write into.
8528 */
8529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008530f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008531 typval_T *argvars;
8532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008534 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535}
8536
Bram Moolenaar33570922005-01-25 22:26:29 +00008537static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008538
8539 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008540findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008541 typval_T *argvars;
8542 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008543 int dir;
8544{
8545#ifdef FEAT_SEARCHPATH
8546 char_u *fname;
8547 char_u *fresult = NULL;
8548 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8549 char_u *p;
8550 char_u pathbuf[NUMBUFLEN];
8551 int count = 1;
8552 int first = TRUE;
8553
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008554 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008555
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008556 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008558 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8559 if (p == NULL)
8560 count = -1; /* error */
8561 else
8562 {
8563 if (*p != NUL)
8564 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008565
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008566 if (argvars[2].v_type != VAR_UNKNOWN)
8567 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8568 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008569 }
8570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008571 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008572 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008573 do
8574 {
8575 vim_free(fresult);
8576 fresult = find_file_in_path_option(first ? fname : NULL,
8577 first ? (int)STRLEN(fname) : 0,
8578 0, first, path, dir, NULL);
8579 first = FALSE;
8580 } while (--count > 0 && fresult != NULL);
8581 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008582
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008583 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008584#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008585 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008586#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008587 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008588}
8589
Bram Moolenaar33570922005-01-25 22:26:29 +00008590static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8591static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008592
8593/*
8594 * Implementation of map() and filter().
8595 */
8596 static void
8597filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008598 typval_T *argvars;
8599 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008600 int map;
8601{
8602 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008603 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008604 listitem_T *li, *nli;
8605 list_T *l = NULL;
8606 dictitem_T *di;
8607 hashtab_T *ht;
8608 hashitem_T *hi;
8609 dict_T *d = NULL;
8610 typval_T save_val;
8611 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008612 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008613 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008614 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8615
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008616
8617 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008618 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008619 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008620 if ((l = argvars[0].vval.v_list) == NULL
8621 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008622 return;
8623 }
8624 else if (argvars[0].v_type == VAR_DICT)
8625 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008626 if ((d = argvars[0].vval.v_dict) == NULL
8627 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008628 return;
8629 }
8630 else
8631 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008632 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008633 return;
8634 }
8635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008636 expr = get_tv_string_buf_chk(&argvars[1], buf);
8637 /* On type errors, the preceding call has already displayed an error
8638 * message. Avoid a misleading error message for an empty string that
8639 * was not passed as argument. */
8640 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008641 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008642 prepare_vimvar(VV_VAL, &save_val);
8643 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008644
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008645 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008646 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008647 prepare_vimvar(VV_KEY, &save_key);
8648 vimvars[VV_KEY].vv_type = VAR_STRING;
8649
8650 ht = &d->dv_hashtab;
8651 hash_lock(ht);
8652 todo = ht->ht_used;
8653 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008655 if (!HASHITEM_EMPTY(hi))
8656 {
8657 --todo;
8658 di = HI2DI(hi);
8659 if (tv_check_lock(di->di_tv.v_lock, msg))
8660 break;
8661 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8662 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8663 break;
8664 if (!map && rem)
8665 dictitem_remove(d, di);
8666 clear_tv(&vimvars[VV_KEY].vv_tv);
8667 }
8668 }
8669 hash_unlock(ht);
8670
8671 restore_vimvar(VV_KEY, &save_key);
8672 }
8673 else
8674 {
8675 for (li = l->lv_first; li != NULL; li = nli)
8676 {
8677 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008678 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008679 nli = li->li_next;
8680 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008681 break;
8682 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008683 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008684 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008685 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008687 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008688 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008689
8690 copy_tv(&argvars[0], rettv);
8691}
8692
8693 static int
8694filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008695 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008696 char_u *expr;
8697 int map;
8698 int *remp;
8699{
Bram Moolenaar33570922005-01-25 22:26:29 +00008700 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008701 char_u *s;
8702
Bram Moolenaar33570922005-01-25 22:26:29 +00008703 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008704 s = expr;
8705 if (eval1(&s, &rettv, TRUE) == FAIL)
8706 return FAIL;
8707 if (*s != NUL) /* check for trailing chars after expr */
8708 {
8709 EMSG2(_(e_invexpr2), s);
8710 return FAIL;
8711 }
8712 if (map)
8713 {
8714 /* map(): replace the list item value */
8715 clear_tv(tv);
8716 *tv = rettv;
8717 }
8718 else
8719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008720 int error = FALSE;
8721
Bram Moolenaare9a41262005-01-15 22:18:47 +00008722 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008723 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008724 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008725 /* On type error, nothing has been removed; return FAIL to stop the
8726 * loop. The error message was given by get_tv_number_chk(). */
8727 if (error)
8728 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008729 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008730 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008731 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008732}
8733
8734/*
8735 * "filter()" function
8736 */
8737 static void
8738f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008739 typval_T *argvars;
8740 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008741{
8742 filter_map(argvars, rettv, FALSE);
8743}
8744
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008745/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008746 * "finddir({fname}[, {path}[, {count}]])" function
8747 */
8748 static void
8749f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008750 typval_T *argvars;
8751 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008752{
8753 findfilendir(argvars, rettv, TRUE);
8754}
8755
8756/*
8757 * "findfile({fname}[, {path}[, {count}]])" function
8758 */
8759 static void
8760f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008761 typval_T *argvars;
8762 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008763{
8764 findfilendir(argvars, rettv, FALSE);
8765}
8766
8767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 * "fnamemodify({fname}, {mods})" function
8769 */
8770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008771f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008772 typval_T *argvars;
8773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774{
8775 char_u *fname;
8776 char_u *mods;
8777 int usedlen = 0;
8778 int len;
8779 char_u *fbuf = NULL;
8780 char_u buf[NUMBUFLEN];
8781
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008782 fname = get_tv_string_chk(&argvars[0]);
8783 mods = get_tv_string_buf_chk(&argvars[1], buf);
8784 if (fname == NULL || mods == NULL)
8785 fname = NULL;
8786 else
8787 {
8788 len = (int)STRLEN(fname);
8789 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008792 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008794 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008796 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 vim_free(fbuf);
8798}
8799
Bram Moolenaar33570922005-01-25 22:26:29 +00008800static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801
8802/*
8803 * "foldclosed()" function
8804 */
8805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008806foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008807 typval_T *argvars;
8808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 int end;
8810{
8811#ifdef FEAT_FOLDING
8812 linenr_T lnum;
8813 linenr_T first, last;
8814
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008815 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8817 {
8818 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8819 {
8820 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008823 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 return;
8825 }
8826 }
8827#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008828 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829}
8830
8831/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008832 * "foldclosed()" function
8833 */
8834 static void
8835f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008836 typval_T *argvars;
8837 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008838{
8839 foldclosed_both(argvars, rettv, FALSE);
8840}
8841
8842/*
8843 * "foldclosedend()" function
8844 */
8845 static void
8846f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008847 typval_T *argvars;
8848 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008849{
8850 foldclosed_both(argvars, rettv, TRUE);
8851}
8852
8853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 * "foldlevel()" function
8855 */
8856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008857f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008858 typval_T *argvars;
8859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860{
8861#ifdef FEAT_FOLDING
8862 linenr_T lnum;
8863
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008864 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008866 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 else
8868#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008869 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870}
8871
8872/*
8873 * "foldtext()" function
8874 */
8875/*ARGSUSED*/
8876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008877f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008878 typval_T *argvars;
8879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880{
8881#ifdef FEAT_FOLDING
8882 linenr_T lnum;
8883 char_u *s;
8884 char_u *r;
8885 int len;
8886 char *txt;
8887#endif
8888
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008889 rettv->v_type = VAR_STRING;
8890 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008892 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8893 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8894 <= curbuf->b_ml.ml_line_count
8895 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 {
8897 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008898 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8899 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 {
8901 if (!linewhite(lnum))
8902 break;
8903 ++lnum;
8904 }
8905
8906 /* Find interesting text in this line. */
8907 s = skipwhite(ml_get(lnum));
8908 /* skip C comment-start */
8909 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008910 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008912 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008913 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008914 {
8915 s = skipwhite(ml_get(lnum + 1));
8916 if (*s == '*')
8917 s = skipwhite(s + 1);
8918 }
8919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 txt = _("+-%s%3ld lines: ");
8921 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008922 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 + 20 /* for %3ld */
8924 + STRLEN(s))); /* concatenated */
8925 if (r != NULL)
8926 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008927 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8928 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8929 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 len = (int)STRLEN(r);
8931 STRCAT(r, s);
8932 /* remove 'foldmarker' and 'commentstring' */
8933 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 }
8936 }
8937#endif
8938}
8939
8940/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008941 * "foldtextresult(lnum)" function
8942 */
8943/*ARGSUSED*/
8944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008945f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008946 typval_T *argvars;
8947 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008948{
8949#ifdef FEAT_FOLDING
8950 linenr_T lnum;
8951 char_u *text;
8952 char_u buf[51];
8953 foldinfo_T foldinfo;
8954 int fold_count;
8955#endif
8956
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957 rettv->v_type = VAR_STRING;
8958 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008959#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008961 /* treat illegal types and illegal string values for {lnum} the same */
8962 if (lnum < 0)
8963 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008964 fold_count = foldedCount(curwin, lnum, &foldinfo);
8965 if (fold_count > 0)
8966 {
8967 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8968 &foldinfo, buf);
8969 if (text == buf)
8970 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008971 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008972 }
8973#endif
8974}
8975
8976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 * "foreground()" function
8978 */
8979/*ARGSUSED*/
8980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008982 typval_T *argvars;
8983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008985 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986#ifdef FEAT_GUI
8987 if (gui.in_use)
8988 gui_mch_set_foreground();
8989#else
8990# ifdef WIN32
8991 win32_set_foreground();
8992# endif
8993#endif
8994}
8995
8996/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008997 * "function()" function
8998 */
8999/*ARGSUSED*/
9000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009001f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009002 typval_T *argvars;
9003 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009004{
9005 char_u *s;
9006
Bram Moolenaara7043832005-01-21 11:56:39 +00009007 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009008 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009009 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009010 EMSG2(_(e_invarg2), s);
9011 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009012 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009013 else
9014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009015 rettv->vval.v_string = vim_strsave(s);
9016 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009017 }
9018}
9019
9020/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009021 * "garbagecollect()" function
9022 */
9023/*ARGSUSED*/
9024 static void
9025f_garbagecollect(argvars, rettv)
9026 typval_T *argvars;
9027 typval_T *rettv;
9028{
9029 garbage_collect();
9030}
9031
9032/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009033 * "get()" function
9034 */
9035 static void
9036f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009037 typval_T *argvars;
9038 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009039{
Bram Moolenaar33570922005-01-25 22:26:29 +00009040 listitem_T *li;
9041 list_T *l;
9042 dictitem_T *di;
9043 dict_T *d;
9044 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009045
Bram Moolenaare9a41262005-01-15 22:18:47 +00009046 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009047 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009048 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009049 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009050 int error = FALSE;
9051
9052 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9053 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009054 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009055 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009056 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009057 else if (argvars[0].v_type == VAR_DICT)
9058 {
9059 if ((d = argvars[0].vval.v_dict) != NULL)
9060 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009061 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009062 if (di != NULL)
9063 tv = &di->di_tv;
9064 }
9065 }
9066 else
9067 EMSG2(_(e_listdictarg), "get()");
9068
9069 if (tv == NULL)
9070 {
9071 if (argvars[2].v_type == VAR_UNKNOWN)
9072 rettv->vval.v_number = 0;
9073 else
9074 copy_tv(&argvars[2], rettv);
9075 }
9076 else
9077 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009078}
9079
9080/*
9081 * "getbufvar()" function
9082 */
9083 static void
9084f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009085 typval_T *argvars;
9086 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009087{
9088 buf_T *buf;
9089 buf_T *save_curbuf;
9090 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009091 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009092
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009093 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9094 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009095 ++emsg_off;
9096 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009097
9098 rettv->v_type = VAR_STRING;
9099 rettv->vval.v_string = NULL;
9100
9101 if (buf != NULL && varname != NULL)
9102 {
9103 if (*varname == '&') /* buffer-local-option */
9104 {
9105 /* set curbuf to be our buf, temporarily */
9106 save_curbuf = curbuf;
9107 curbuf = buf;
9108
9109 get_option_tv(&varname, rettv, TRUE);
9110
9111 /* restore previous notion of curbuf */
9112 curbuf = save_curbuf;
9113 }
9114 else
9115 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009116 if (*varname == NUL)
9117 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9118 * scope prefix before the NUL byte is required by
9119 * find_var_in_ht(). */
9120 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009121 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009122 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009123 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009124 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009125 }
9126 }
9127
9128 --emsg_off;
9129}
9130
9131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 * "getchar()" function
9133 */
9134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009135f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009136 typval_T *argvars;
9137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138{
9139 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009140 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141
9142 ++no_mapping;
9143 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009144 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 /* getchar(): blocking wait. */
9146 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009147 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 /* getchar(1): only check if char avail */
9149 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009150 else if (error || vpeekc() == NUL)
9151 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 n = 0;
9153 else
9154 /* getchar(0) and char avail: return char */
9155 n = safe_vgetc();
9156 --no_mapping;
9157 --allow_keys;
9158
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009159 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 if (IS_SPECIAL(n) || mod_mask != 0)
9161 {
9162 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9163 int i = 0;
9164
9165 /* Turn a special key into three bytes, plus modifier. */
9166 if (mod_mask != 0)
9167 {
9168 temp[i++] = K_SPECIAL;
9169 temp[i++] = KS_MODIFIER;
9170 temp[i++] = mod_mask;
9171 }
9172 if (IS_SPECIAL(n))
9173 {
9174 temp[i++] = K_SPECIAL;
9175 temp[i++] = K_SECOND(n);
9176 temp[i++] = K_THIRD(n);
9177 }
9178#ifdef FEAT_MBYTE
9179 else if (has_mbyte)
9180 i += (*mb_char2bytes)(n, temp + i);
9181#endif
9182 else
9183 temp[i++] = n;
9184 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009185 rettv->v_type = VAR_STRING;
9186 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 }
9188}
9189
9190/*
9191 * "getcharmod()" function
9192 */
9193/*ARGSUSED*/
9194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009196 typval_T *argvars;
9197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009199 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200}
9201
9202/*
9203 * "getcmdline()" function
9204 */
9205/*ARGSUSED*/
9206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009207f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009208 typval_T *argvars;
9209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009211 rettv->v_type = VAR_STRING;
9212 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213}
9214
9215/*
9216 * "getcmdpos()" function
9217 */
9218/*ARGSUSED*/
9219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009220f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009221 typval_T *argvars;
9222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009224 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225}
9226
9227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 * "getcwd()" function
9229 */
9230/*ARGSUSED*/
9231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009232f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009233 typval_T *argvars;
9234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235{
9236 char_u cwd[MAXPATHL];
9237
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009238 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009240 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 else
9242 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009243 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009245 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246#endif
9247 }
9248}
9249
9250/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009251 * "getfontname()" function
9252 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009253/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009255f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009256 typval_T *argvars;
9257 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009258{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009259 rettv->v_type = VAR_STRING;
9260 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009261#ifdef FEAT_GUI
9262 if (gui.in_use)
9263 {
9264 GuiFont font;
9265 char_u *name = NULL;
9266
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009267 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009268 {
9269 /* Get the "Normal" font. Either the name saved by
9270 * hl_set_font_name() or from the font ID. */
9271 font = gui.norm_font;
9272 name = hl_get_font_name();
9273 }
9274 else
9275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009276 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009277 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9278 return;
9279 font = gui_mch_get_font(name, FALSE);
9280 if (font == NOFONT)
9281 return; /* Invalid font name, return empty string. */
9282 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009283 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009284 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009285 gui_mch_free_font(font);
9286 }
9287#endif
9288}
9289
9290/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009291 * "getfperm({fname})" function
9292 */
9293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009294f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009295 typval_T *argvars;
9296 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009297{
9298 char_u *fname;
9299 struct stat st;
9300 char_u *perm = NULL;
9301 char_u flags[] = "rwx";
9302 int i;
9303
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009304 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009305
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009306 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009307 if (mch_stat((char *)fname, &st) >= 0)
9308 {
9309 perm = vim_strsave((char_u *)"---------");
9310 if (perm != NULL)
9311 {
9312 for (i = 0; i < 9; i++)
9313 {
9314 if (st.st_mode & (1 << (8 - i)))
9315 perm[i] = flags[i % 3];
9316 }
9317 }
9318 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009319 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009320}
9321
9322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 * "getfsize({fname})" function
9324 */
9325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009327 typval_T *argvars;
9328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329{
9330 char_u *fname;
9331 struct stat st;
9332
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009333 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009335 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336
9337 if (mch_stat((char *)fname, &st) >= 0)
9338 {
9339 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009340 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009342 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 }
9344 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009345 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346}
9347
9348/*
9349 * "getftime({fname})" function
9350 */
9351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009353 typval_T *argvars;
9354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355{
9356 char_u *fname;
9357 struct stat st;
9358
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009359 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360
9361 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009364 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365}
9366
9367/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009368 * "getftype({fname})" function
9369 */
9370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009372 typval_T *argvars;
9373 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009374{
9375 char_u *fname;
9376 struct stat st;
9377 char_u *type = NULL;
9378 char *t;
9379
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009380 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009381
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009382 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009383 if (mch_lstat((char *)fname, &st) >= 0)
9384 {
9385#ifdef S_ISREG
9386 if (S_ISREG(st.st_mode))
9387 t = "file";
9388 else if (S_ISDIR(st.st_mode))
9389 t = "dir";
9390# ifdef S_ISLNK
9391 else if (S_ISLNK(st.st_mode))
9392 t = "link";
9393# endif
9394# ifdef S_ISBLK
9395 else if (S_ISBLK(st.st_mode))
9396 t = "bdev";
9397# endif
9398# ifdef S_ISCHR
9399 else if (S_ISCHR(st.st_mode))
9400 t = "cdev";
9401# endif
9402# ifdef S_ISFIFO
9403 else if (S_ISFIFO(st.st_mode))
9404 t = "fifo";
9405# endif
9406# ifdef S_ISSOCK
9407 else if (S_ISSOCK(st.st_mode))
9408 t = "fifo";
9409# endif
9410 else
9411 t = "other";
9412#else
9413# ifdef S_IFMT
9414 switch (st.st_mode & S_IFMT)
9415 {
9416 case S_IFREG: t = "file"; break;
9417 case S_IFDIR: t = "dir"; break;
9418# ifdef S_IFLNK
9419 case S_IFLNK: t = "link"; break;
9420# endif
9421# ifdef S_IFBLK
9422 case S_IFBLK: t = "bdev"; break;
9423# endif
9424# ifdef S_IFCHR
9425 case S_IFCHR: t = "cdev"; break;
9426# endif
9427# ifdef S_IFIFO
9428 case S_IFIFO: t = "fifo"; break;
9429# endif
9430# ifdef S_IFSOCK
9431 case S_IFSOCK: t = "socket"; break;
9432# endif
9433 default: t = "other";
9434 }
9435# else
9436 if (mch_isdir(fname))
9437 t = "dir";
9438 else
9439 t = "file";
9440# endif
9441#endif
9442 type = vim_strsave((char_u *)t);
9443 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009445}
9446
9447/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009448 * "getline(lnum)" function
9449 */
9450 static void
9451f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009452 typval_T *argvars;
9453 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009454{
9455 linenr_T lnum;
9456 linenr_T end;
9457 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00009458 list_T *l;
9459 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009460
9461 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009462 if (lnum < 0)
9463 rettv->vval.v_number = 0; /* failure; error message already given */
9464 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009465 {
9466 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9467 p = ml_get(lnum);
9468 else
9469 p = (char_u *)"";
9470
9471 rettv->v_type = VAR_STRING;
9472 rettv->vval.v_string = vim_strsave(p);
9473 }
9474 else
9475 {
9476 end = get_tv_lnum(&argvars[1]);
9477 if (end < lnum)
9478 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009479 if (end >= 0) /* else: error message already given */
9480 EMSG(_(e_invrange));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009481 rettv->vval.v_number = 0;
9482 }
9483 else
9484 {
9485 l = list_alloc();
9486 if (l != NULL)
9487 {
9488 if (lnum < 1)
9489 lnum = 1;
9490 if (end > curbuf->b_ml.ml_line_count)
9491 end = curbuf->b_ml.ml_line_count;
9492 while (lnum <= end)
9493 {
9494 li = listitem_alloc();
9495 if (li == NULL)
9496 break;
9497 list_append(l, li);
9498 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009499 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009500 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
9501 }
9502 rettv->vval.v_list = l;
9503 rettv->v_type = VAR_LIST;
9504 ++l->lv_refcount;
9505 }
9506 }
9507 }
9508}
9509
9510/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009511 * "getqflist()" function
9512 */
9513/*ARGSUSED*/
9514 static void
9515f_getqflist(argvars, rettv)
9516 typval_T *argvars;
9517 typval_T *rettv;
9518{
9519#ifdef FEAT_QUICKFIX
9520 list_T *l;
9521#endif
9522
9523 rettv->vval.v_number = FALSE;
9524#ifdef FEAT_QUICKFIX
9525 l = list_alloc();
9526 if (l != NULL)
9527 {
9528 if (get_errorlist(l) != FAIL)
9529 {
9530 rettv->vval.v_list = l;
9531 rettv->v_type = VAR_LIST;
9532 ++l->lv_refcount;
9533 }
9534 else
9535 list_free(l);
9536 }
9537#endif
9538}
9539
9540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 * "getreg()" function
9542 */
9543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009544f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009545 typval_T *argvars;
9546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547{
9548 char_u *strregname;
9549 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009550 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009551 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009553 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009554 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009555 strregname = get_tv_string_chk(&argvars[0]);
9556 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009557 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009558 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009561 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 regname = (strregname == NULL ? '"' : *strregname);
9563 if (regname == 0)
9564 regname = '"';
9565
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009567 rettv->vval.v_string = error ? NULL :
9568 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569}
9570
9571/*
9572 * "getregtype()" function
9573 */
9574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009575f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009576 typval_T *argvars;
9577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578{
9579 char_u *strregname;
9580 int regname;
9581 char_u buf[NUMBUFLEN + 2];
9582 long reglen = 0;
9583
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009584 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 {
9586 strregname = get_tv_string_chk(&argvars[0]);
9587 if (strregname == NULL) /* type error; errmsg already given */
9588 {
9589 rettv->v_type = VAR_STRING;
9590 rettv->vval.v_string = NULL;
9591 return;
9592 }
9593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 else
9595 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009596 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597
9598 regname = (strregname == NULL ? '"' : *strregname);
9599 if (regname == 0)
9600 regname = '"';
9601
9602 buf[0] = NUL;
9603 buf[1] = NUL;
9604 switch (get_reg_type(regname, &reglen))
9605 {
9606 case MLINE: buf[0] = 'V'; break;
9607 case MCHAR: buf[0] = 'v'; break;
9608#ifdef FEAT_VISUAL
9609 case MBLOCK:
9610 buf[0] = Ctrl_V;
9611 sprintf((char *)buf + 1, "%ld", reglen + 1);
9612 break;
9613#endif
9614 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009615 rettv->v_type = VAR_STRING;
9616 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617}
9618
9619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 * "getwinposx()" function
9621 */
9622/*ARGSUSED*/
9623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009624f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009625 typval_T *argvars;
9626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629#ifdef FEAT_GUI
9630 if (gui.in_use)
9631 {
9632 int x, y;
9633
9634 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009635 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 }
9637#endif
9638}
9639
9640/*
9641 * "getwinposy()" function
9642 */
9643/*ARGSUSED*/
9644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009645f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009646 typval_T *argvars;
9647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650#ifdef FEAT_GUI
9651 if (gui.in_use)
9652 {
9653 int x, y;
9654
9655 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009656 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 }
9658#endif
9659}
9660
Bram Moolenaara40058a2005-07-11 22:42:07 +00009661static win_T *find_win_by_nr __ARGS((typval_T *vp));
9662
9663 static win_T *
9664find_win_by_nr(vp)
9665 typval_T *vp;
9666{
9667#ifdef FEAT_WINDOWS
9668 win_T *wp;
9669#endif
9670 int nr;
9671
9672 nr = get_tv_number_chk(vp, NULL);
9673
9674#ifdef FEAT_WINDOWS
9675 if (nr < 0)
9676 return NULL;
9677 if (nr == 0)
9678 return curwin;
9679
9680 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9681 if (--nr <= 0)
9682 break;
9683 return wp;
9684#else
9685 if (nr == 0 || nr == 1)
9686 return curwin;
9687 return NULL;
9688#endif
9689}
9690
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691/*
9692 * "getwinvar()" function
9693 */
9694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009696 typval_T *argvars;
9697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698{
9699 win_T *win, *oldcurwin;
9700 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009701 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009704 varname = get_tv_string_chk(&argvars[1]);
9705 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707 rettv->v_type = VAR_STRING;
9708 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709
9710 if (win != NULL && varname != NULL)
9711 {
9712 if (*varname == '&') /* window-local-option */
9713 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009714 /* Set curwin to be our win, temporarily. Also set curbuf, so
9715 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 oldcurwin = curwin;
9717 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009718 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721
9722 /* restore previous notion of curwin */
9723 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009724 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 }
9726 else
9727 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009728 if (*varname == NUL)
9729 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9730 * scope prefix before the NUL byte is required by
9731 * find_var_in_ht(). */
9732 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009734 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009736 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 }
9738 }
9739
9740 --emsg_off;
9741}
9742
9743/*
9744 * "glob()" function
9745 */
9746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009747f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009748 typval_T *argvars;
9749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750{
9751 expand_T xpc;
9752
9753 ExpandInit(&xpc);
9754 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009755 rettv->v_type = VAR_STRING;
9756 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9758 ExpandCleanup(&xpc);
9759}
9760
9761/*
9762 * "globpath()" function
9763 */
9764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009765f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009766 typval_T *argvars;
9767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768{
9769 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009770 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009773 if (file == NULL)
9774 rettv->vval.v_string = NULL;
9775 else
9776 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777}
9778
9779/*
9780 * "has()" function
9781 */
9782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009783f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009784 typval_T *argvars;
9785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786{
9787 int i;
9788 char_u *name;
9789 int n = FALSE;
9790 static char *(has_list[]) =
9791 {
9792#ifdef AMIGA
9793 "amiga",
9794# ifdef FEAT_ARP
9795 "arp",
9796# endif
9797#endif
9798#ifdef __BEOS__
9799 "beos",
9800#endif
9801#ifdef MSDOS
9802# ifdef DJGPP
9803 "dos32",
9804# else
9805 "dos16",
9806# endif
9807#endif
9808#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9809 "mac",
9810#endif
9811#if defined(MACOS_X_UNIX)
9812 "macunix",
9813#endif
9814#ifdef OS2
9815 "os2",
9816#endif
9817#ifdef __QNX__
9818 "qnx",
9819#endif
9820#ifdef RISCOS
9821 "riscos",
9822#endif
9823#ifdef UNIX
9824 "unix",
9825#endif
9826#ifdef VMS
9827 "vms",
9828#endif
9829#ifdef WIN16
9830 "win16",
9831#endif
9832#ifdef WIN32
9833 "win32",
9834#endif
9835#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9836 "win32unix",
9837#endif
9838#ifdef WIN64
9839 "win64",
9840#endif
9841#ifdef EBCDIC
9842 "ebcdic",
9843#endif
9844#ifndef CASE_INSENSITIVE_FILENAME
9845 "fname_case",
9846#endif
9847#ifdef FEAT_ARABIC
9848 "arabic",
9849#endif
9850#ifdef FEAT_AUTOCMD
9851 "autocmd",
9852#endif
9853#ifdef FEAT_BEVAL
9854 "balloon_eval",
9855#endif
9856#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9857 "builtin_terms",
9858# ifdef ALL_BUILTIN_TCAPS
9859 "all_builtin_terms",
9860# endif
9861#endif
9862#ifdef FEAT_BYTEOFF
9863 "byte_offset",
9864#endif
9865#ifdef FEAT_CINDENT
9866 "cindent",
9867#endif
9868#ifdef FEAT_CLIENTSERVER
9869 "clientserver",
9870#endif
9871#ifdef FEAT_CLIPBOARD
9872 "clipboard",
9873#endif
9874#ifdef FEAT_CMDL_COMPL
9875 "cmdline_compl",
9876#endif
9877#ifdef FEAT_CMDHIST
9878 "cmdline_hist",
9879#endif
9880#ifdef FEAT_COMMENTS
9881 "comments",
9882#endif
9883#ifdef FEAT_CRYPT
9884 "cryptv",
9885#endif
9886#ifdef FEAT_CSCOPE
9887 "cscope",
9888#endif
9889#ifdef DEBUG
9890 "debug",
9891#endif
9892#ifdef FEAT_CON_DIALOG
9893 "dialog_con",
9894#endif
9895#ifdef FEAT_GUI_DIALOG
9896 "dialog_gui",
9897#endif
9898#ifdef FEAT_DIFF
9899 "diff",
9900#endif
9901#ifdef FEAT_DIGRAPHS
9902 "digraphs",
9903#endif
9904#ifdef FEAT_DND
9905 "dnd",
9906#endif
9907#ifdef FEAT_EMACS_TAGS
9908 "emacs_tags",
9909#endif
9910 "eval", /* always present, of course! */
9911#ifdef FEAT_EX_EXTRA
9912 "ex_extra",
9913#endif
9914#ifdef FEAT_SEARCH_EXTRA
9915 "extra_search",
9916#endif
9917#ifdef FEAT_FKMAP
9918 "farsi",
9919#endif
9920#ifdef FEAT_SEARCHPATH
9921 "file_in_path",
9922#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009923#if defined(UNIX) && !defined(USE_SYSTEM)
9924 "filterpipe",
9925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926#ifdef FEAT_FIND_ID
9927 "find_in_path",
9928#endif
9929#ifdef FEAT_FOLDING
9930 "folding",
9931#endif
9932#ifdef FEAT_FOOTER
9933 "footer",
9934#endif
9935#if !defined(USE_SYSTEM) && defined(UNIX)
9936 "fork",
9937#endif
9938#ifdef FEAT_GETTEXT
9939 "gettext",
9940#endif
9941#ifdef FEAT_GUI
9942 "gui",
9943#endif
9944#ifdef FEAT_GUI_ATHENA
9945# ifdef FEAT_GUI_NEXTAW
9946 "gui_neXtaw",
9947# else
9948 "gui_athena",
9949# endif
9950#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009951#ifdef FEAT_GUI_KDE
9952 "gui_kde",
9953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954#ifdef FEAT_GUI_GTK
9955 "gui_gtk",
9956# ifdef HAVE_GTK2
9957 "gui_gtk2",
9958# endif
9959#endif
9960#ifdef FEAT_GUI_MAC
9961 "gui_mac",
9962#endif
9963#ifdef FEAT_GUI_MOTIF
9964 "gui_motif",
9965#endif
9966#ifdef FEAT_GUI_PHOTON
9967 "gui_photon",
9968#endif
9969#ifdef FEAT_GUI_W16
9970 "gui_win16",
9971#endif
9972#ifdef FEAT_GUI_W32
9973 "gui_win32",
9974#endif
9975#ifdef FEAT_HANGULIN
9976 "hangul_input",
9977#endif
9978#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
9979 "iconv",
9980#endif
9981#ifdef FEAT_INS_EXPAND
9982 "insert_expand",
9983#endif
9984#ifdef FEAT_JUMPLIST
9985 "jumplist",
9986#endif
9987#ifdef FEAT_KEYMAP
9988 "keymap",
9989#endif
9990#ifdef FEAT_LANGMAP
9991 "langmap",
9992#endif
9993#ifdef FEAT_LIBCALL
9994 "libcall",
9995#endif
9996#ifdef FEAT_LINEBREAK
9997 "linebreak",
9998#endif
9999#ifdef FEAT_LISP
10000 "lispindent",
10001#endif
10002#ifdef FEAT_LISTCMDS
10003 "listcmds",
10004#endif
10005#ifdef FEAT_LOCALMAP
10006 "localmap",
10007#endif
10008#ifdef FEAT_MENU
10009 "menu",
10010#endif
10011#ifdef FEAT_SESSION
10012 "mksession",
10013#endif
10014#ifdef FEAT_MODIFY_FNAME
10015 "modify_fname",
10016#endif
10017#ifdef FEAT_MOUSE
10018 "mouse",
10019#endif
10020#ifdef FEAT_MOUSESHAPE
10021 "mouseshape",
10022#endif
10023#if defined(UNIX) || defined(VMS)
10024# ifdef FEAT_MOUSE_DEC
10025 "mouse_dec",
10026# endif
10027# ifdef FEAT_MOUSE_GPM
10028 "mouse_gpm",
10029# endif
10030# ifdef FEAT_MOUSE_JSB
10031 "mouse_jsbterm",
10032# endif
10033# ifdef FEAT_MOUSE_NET
10034 "mouse_netterm",
10035# endif
10036# ifdef FEAT_MOUSE_PTERM
10037 "mouse_pterm",
10038# endif
10039# ifdef FEAT_MOUSE_XTERM
10040 "mouse_xterm",
10041# endif
10042#endif
10043#ifdef FEAT_MBYTE
10044 "multi_byte",
10045#endif
10046#ifdef FEAT_MBYTE_IME
10047 "multi_byte_ime",
10048#endif
10049#ifdef FEAT_MULTI_LANG
10050 "multi_lang",
10051#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010052#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010053#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010054 "mzscheme",
10055#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057#ifdef FEAT_OLE
10058 "ole",
10059#endif
10060#ifdef FEAT_OSFILETYPE
10061 "osfiletype",
10062#endif
10063#ifdef FEAT_PATH_EXTRA
10064 "path_extra",
10065#endif
10066#ifdef FEAT_PERL
10067#ifndef DYNAMIC_PERL
10068 "perl",
10069#endif
10070#endif
10071#ifdef FEAT_PYTHON
10072#ifndef DYNAMIC_PYTHON
10073 "python",
10074#endif
10075#endif
10076#ifdef FEAT_POSTSCRIPT
10077 "postscript",
10078#endif
10079#ifdef FEAT_PRINTER
10080 "printer",
10081#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010082#ifdef FEAT_PROFILE
10083 "profile",
10084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085#ifdef FEAT_QUICKFIX
10086 "quickfix",
10087#endif
10088#ifdef FEAT_RIGHTLEFT
10089 "rightleft",
10090#endif
10091#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10092 "ruby",
10093#endif
10094#ifdef FEAT_SCROLLBIND
10095 "scrollbind",
10096#endif
10097#ifdef FEAT_CMDL_INFO
10098 "showcmd",
10099 "cmdline_info",
10100#endif
10101#ifdef FEAT_SIGNS
10102 "signs",
10103#endif
10104#ifdef FEAT_SMARTINDENT
10105 "smartindent",
10106#endif
10107#ifdef FEAT_SNIFF
10108 "sniff",
10109#endif
10110#ifdef FEAT_STL_OPT
10111 "statusline",
10112#endif
10113#ifdef FEAT_SUN_WORKSHOP
10114 "sun_workshop",
10115#endif
10116#ifdef FEAT_NETBEANS_INTG
10117 "netbeans_intg",
10118#endif
10119#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010120 "spell",
10121#endif
10122#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 "syntax",
10124#endif
10125#if defined(USE_SYSTEM) || !defined(UNIX)
10126 "system",
10127#endif
10128#ifdef FEAT_TAG_BINS
10129 "tag_binary",
10130#endif
10131#ifdef FEAT_TAG_OLDSTATIC
10132 "tag_old_static",
10133#endif
10134#ifdef FEAT_TAG_ANYWHITE
10135 "tag_any_white",
10136#endif
10137#ifdef FEAT_TCL
10138# ifndef DYNAMIC_TCL
10139 "tcl",
10140# endif
10141#endif
10142#ifdef TERMINFO
10143 "terminfo",
10144#endif
10145#ifdef FEAT_TERMRESPONSE
10146 "termresponse",
10147#endif
10148#ifdef FEAT_TEXTOBJ
10149 "textobjects",
10150#endif
10151#ifdef HAVE_TGETENT
10152 "tgetent",
10153#endif
10154#ifdef FEAT_TITLE
10155 "title",
10156#endif
10157#ifdef FEAT_TOOLBAR
10158 "toolbar",
10159#endif
10160#ifdef FEAT_USR_CMDS
10161 "user-commands", /* was accidentally included in 5.4 */
10162 "user_commands",
10163#endif
10164#ifdef FEAT_VIMINFO
10165 "viminfo",
10166#endif
10167#ifdef FEAT_VERTSPLIT
10168 "vertsplit",
10169#endif
10170#ifdef FEAT_VIRTUALEDIT
10171 "virtualedit",
10172#endif
10173#ifdef FEAT_VISUAL
10174 "visual",
10175#endif
10176#ifdef FEAT_VISUALEXTRA
10177 "visualextra",
10178#endif
10179#ifdef FEAT_VREPLACE
10180 "vreplace",
10181#endif
10182#ifdef FEAT_WILDIGN
10183 "wildignore",
10184#endif
10185#ifdef FEAT_WILDMENU
10186 "wildmenu",
10187#endif
10188#ifdef FEAT_WINDOWS
10189 "windows",
10190#endif
10191#ifdef FEAT_WAK
10192 "winaltkeys",
10193#endif
10194#ifdef FEAT_WRITEBACKUP
10195 "writebackup",
10196#endif
10197#ifdef FEAT_XIM
10198 "xim",
10199#endif
10200#ifdef FEAT_XFONTSET
10201 "xfontset",
10202#endif
10203#ifdef USE_XSMP
10204 "xsmp",
10205#endif
10206#ifdef USE_XSMP_INTERACT
10207 "xsmp_interact",
10208#endif
10209#ifdef FEAT_XCLIPBOARD
10210 "xterm_clipboard",
10211#endif
10212#ifdef FEAT_XTERM_SAVE
10213 "xterm_save",
10214#endif
10215#if defined(UNIX) && defined(FEAT_X11)
10216 "X11",
10217#endif
10218 NULL
10219 };
10220
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010221 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 for (i = 0; has_list[i] != NULL; ++i)
10223 if (STRICMP(name, has_list[i]) == 0)
10224 {
10225 n = TRUE;
10226 break;
10227 }
10228
10229 if (n == FALSE)
10230 {
10231 if (STRNICMP(name, "patch", 5) == 0)
10232 n = has_patch(atoi((char *)name + 5));
10233 else if (STRICMP(name, "vim_starting") == 0)
10234 n = (starting != 0);
10235#ifdef DYNAMIC_TCL
10236 else if (STRICMP(name, "tcl") == 0)
10237 n = tcl_enabled(FALSE);
10238#endif
10239#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10240 else if (STRICMP(name, "iconv") == 0)
10241 n = iconv_enabled(FALSE);
10242#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010243#ifdef DYNAMIC_MZSCHEME
10244 else if (STRICMP(name, "mzscheme") == 0)
10245 n = mzscheme_enabled(FALSE);
10246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247#ifdef DYNAMIC_RUBY
10248 else if (STRICMP(name, "ruby") == 0)
10249 n = ruby_enabled(FALSE);
10250#endif
10251#ifdef DYNAMIC_PYTHON
10252 else if (STRICMP(name, "python") == 0)
10253 n = python_enabled(FALSE);
10254#endif
10255#ifdef DYNAMIC_PERL
10256 else if (STRICMP(name, "perl") == 0)
10257 n = perl_enabled(FALSE);
10258#endif
10259#ifdef FEAT_GUI
10260 else if (STRICMP(name, "gui_running") == 0)
10261 n = (gui.in_use || gui.starting);
10262# ifdef FEAT_GUI_W32
10263 else if (STRICMP(name, "gui_win32s") == 0)
10264 n = gui_is_win32s();
10265# endif
10266# ifdef FEAT_BROWSE
10267 else if (STRICMP(name, "browse") == 0)
10268 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10269# endif
10270#endif
10271#ifdef FEAT_SYN_HL
10272 else if (STRICMP(name, "syntax_items") == 0)
10273 n = syntax_present(curbuf);
10274#endif
10275#if defined(WIN3264)
10276 else if (STRICMP(name, "win95") == 0)
10277 n = mch_windows95();
10278#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010279#ifdef FEAT_NETBEANS_INTG
10280 else if (STRICMP(name, "netbeans_enabled") == 0)
10281 n = usingNetbeans;
10282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 }
10284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010285 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286}
10287
10288/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010289 * "has_key()" function
10290 */
10291 static void
10292f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010293 typval_T *argvars;
10294 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010295{
10296 rettv->vval.v_number = 0;
10297 if (argvars[0].v_type != VAR_DICT)
10298 {
10299 EMSG(_(e_dictreq));
10300 return;
10301 }
10302 if (argvars[0].vval.v_dict == NULL)
10303 return;
10304
10305 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010306 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010307}
10308
10309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 * "hasmapto()" function
10311 */
10312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010313f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010314 typval_T *argvars;
10315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316{
10317 char_u *name;
10318 char_u *mode;
10319 char_u buf[NUMBUFLEN];
10320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010321 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010322 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 mode = (char_u *)"nvo";
10324 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010325 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326
10327 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010328 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010330 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331}
10332
10333/*
10334 * "histadd()" function
10335 */
10336/*ARGSUSED*/
10337 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010338f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010339 typval_T *argvars;
10340 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341{
10342#ifdef FEAT_CMDHIST
10343 int histype;
10344 char_u *str;
10345 char_u buf[NUMBUFLEN];
10346#endif
10347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010348 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 if (check_restricted() || check_secure())
10350 return;
10351#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010352 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10353 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 if (histype >= 0)
10355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010356 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 if (*str != NUL)
10358 {
10359 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010360 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 return;
10362 }
10363 }
10364#endif
10365}
10366
10367/*
10368 * "histdel()" function
10369 */
10370/*ARGSUSED*/
10371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010372f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010373 typval_T *argvars;
10374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375{
10376#ifdef FEAT_CMDHIST
10377 int n;
10378 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010379 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010381 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10382 if (str == NULL)
10383 n = 0;
10384 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010386 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010387 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010389 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010390 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 else
10392 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010393 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010394 get_tv_string_buf(&argvars[1], buf));
10395 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010397 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398#endif
10399}
10400
10401/*
10402 * "histget()" function
10403 */
10404/*ARGSUSED*/
10405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010406f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010407 typval_T *argvars;
10408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409{
10410#ifdef FEAT_CMDHIST
10411 int type;
10412 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010413 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010415 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10416 if (str == NULL)
10417 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010419 {
10420 type = get_histtype(str);
10421 if (argvars[1].v_type == VAR_UNKNOWN)
10422 idx = get_history_idx(type);
10423 else
10424 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10425 /* -1 on type error */
10426 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010429 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010431 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432}
10433
10434/*
10435 * "histnr()" function
10436 */
10437/*ARGSUSED*/
10438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010439f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010440 typval_T *argvars;
10441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442{
10443 int i;
10444
10445#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010446 char_u *history = get_tv_string_chk(&argvars[0]);
10447
10448 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 if (i >= HIST_CMD && i < HIST_COUNT)
10450 i = get_history_idx(i);
10451 else
10452#endif
10453 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010454 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455}
10456
10457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 * "highlightID(name)" function
10459 */
10460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010461f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010462 typval_T *argvars;
10463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010465 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466}
10467
10468/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010469 * "highlight_exists()" function
10470 */
10471 static void
10472f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010473 typval_T *argvars;
10474 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010475{
10476 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10477}
10478
10479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 * "hostname()" function
10481 */
10482/*ARGSUSED*/
10483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010484f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010485 typval_T *argvars;
10486 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487{
10488 char_u hostname[256];
10489
10490 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010491 rettv->v_type = VAR_STRING;
10492 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493}
10494
10495/*
10496 * iconv() function
10497 */
10498/*ARGSUSED*/
10499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010500f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010501 typval_T *argvars;
10502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503{
10504#ifdef FEAT_MBYTE
10505 char_u buf1[NUMBUFLEN];
10506 char_u buf2[NUMBUFLEN];
10507 char_u *from, *to, *str;
10508 vimconv_T vimconv;
10509#endif
10510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010511 rettv->v_type = VAR_STRING;
10512 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513
10514#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010515 str = get_tv_string(&argvars[0]);
10516 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10517 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 vimconv.vc_type = CONV_NONE;
10519 convert_setup(&vimconv, from, to);
10520
10521 /* If the encodings are equal, no conversion needed. */
10522 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010523 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010525 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526
10527 convert_setup(&vimconv, NULL, NULL);
10528 vim_free(from);
10529 vim_free(to);
10530#endif
10531}
10532
10533/*
10534 * "indent()" function
10535 */
10536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010537f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010538 typval_T *argvars;
10539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540{
10541 linenr_T lnum;
10542
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010543 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010545 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010547 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548}
10549
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010550/*
10551 * "index()" function
10552 */
10553 static void
10554f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010555 typval_T *argvars;
10556 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010557{
Bram Moolenaar33570922005-01-25 22:26:29 +000010558 list_T *l;
10559 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010560 long idx = 0;
10561 int ic = FALSE;
10562
10563 rettv->vval.v_number = -1;
10564 if (argvars[0].v_type != VAR_LIST)
10565 {
10566 EMSG(_(e_listreq));
10567 return;
10568 }
10569 l = argvars[0].vval.v_list;
10570 if (l != NULL)
10571 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010572 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010573 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010575 int error = FALSE;
10576
Bram Moolenaar758711c2005-02-02 23:11:38 +000010577 /* Start at specified item. Use the cached index that list_find()
10578 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010579 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010580 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010581 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010582 ic = get_tv_number_chk(&argvars[3], &error);
10583 if (error)
10584 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010585 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010586
Bram Moolenaar758711c2005-02-02 23:11:38 +000010587 for ( ; item != NULL; item = item->li_next, ++idx)
10588 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010589 {
10590 rettv->vval.v_number = idx;
10591 break;
10592 }
10593 }
10594}
10595
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596static int inputsecret_flag = 0;
10597
10598/*
10599 * "input()" function
10600 * Also handles inputsecret() when inputsecret is set.
10601 */
10602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010603f_input(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{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010607 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 char_u *p = NULL;
10609 int c;
10610 char_u buf[NUMBUFLEN];
10611 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010614 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615
10616#ifdef NO_CONSOLE_INPUT
10617 /* While starting up, there is no place to enter text. */
10618 if (no_console_input())
10619 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010620 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 return;
10622 }
10623#endif
10624
10625 cmd_silent = FALSE; /* Want to see the prompt. */
10626 if (prompt != NULL)
10627 {
10628 /* Only the part of the message after the last NL is considered as
10629 * prompt for the command line */
10630 p = vim_strrchr(prompt, '\n');
10631 if (p == NULL)
10632 p = prompt;
10633 else
10634 {
10635 ++p;
10636 c = *p;
10637 *p = NUL;
10638 msg_start();
10639 msg_clr_eos();
10640 msg_puts_attr(prompt, echo_attr);
10641 msg_didout = FALSE;
10642 msg_starthere();
10643 *p = c;
10644 }
10645 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010647 if (argvars[1].v_type != VAR_UNKNOWN)
10648 {
10649 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10650 if (defstr != NULL)
10651 stuffReadbuffSpec(defstr);
10652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010654 if (defstr != NULL)
10655 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010658 /* since the user typed this, no need to wait for return */
10659 need_wait_return = FALSE;
10660 msg_didout = FALSE;
10661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 cmd_silent = cmd_silent_save;
10663}
10664
10665/*
10666 * "inputdialog()" function
10667 */
10668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010669f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010670 typval_T *argvars;
10671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672{
10673#if defined(FEAT_GUI_TEXTDIALOG)
10674 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10675 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10676 {
10677 char_u *message;
10678 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010679 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010681 message = get_tv_string_chk(&argvars[0]);
10682 if (argvars[1].v_type != VAR_UNKNOWN
10683 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010685 STRNCPY(IObuff, defstr, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 IObuff[IOSIZE - 1] = NUL;
10687 }
10688 else
10689 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010690 if (message != NULL && defstr != NULL
10691 && do_dialog(VIM_QUESTION, NULL, message,
10692 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010693 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 else
10695 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010696 if (message != NULL && defstr != NULL
10697 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010698 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010699 rettv->vval.v_string = vim_strsave(
10700 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010702 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 }
10706 else
10707#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010708 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709}
10710
10711static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10712
10713/*
10714 * "inputrestore()" function
10715 */
10716/*ARGSUSED*/
10717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010718f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010719 typval_T *argvars;
10720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721{
10722 if (ga_userinput.ga_len > 0)
10723 {
10724 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10726 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010727 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 }
10729 else if (p_verbose > 1)
10730 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010731 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010732 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 }
10734}
10735
10736/*
10737 * "inputsave()" function
10738 */
10739/*ARGSUSED*/
10740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010741f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010742 typval_T *argvars;
10743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744{
10745 /* Add an entry to the stack of typehead storage. */
10746 if (ga_grow(&ga_userinput, 1) == OK)
10747 {
10748 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10749 + ga_userinput.ga_len);
10750 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010751 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 }
10753 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755}
10756
10757/*
10758 * "inputsecret()" function
10759 */
10760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010761f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010762 typval_T *argvars;
10763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764{
10765 ++cmdline_star;
10766 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010767 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 --cmdline_star;
10769 --inputsecret_flag;
10770}
10771
10772/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010773 * "insert()" function
10774 */
10775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010776f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010777 typval_T *argvars;
10778 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010779{
10780 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010781 listitem_T *item;
10782 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010783 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010784
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010785 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010786 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010787 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010788 else if ((l = argvars[0].vval.v_list) != NULL
10789 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010790 {
10791 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010792 before = get_tv_number_chk(&argvars[2], &error);
10793 if (error)
10794 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010795
Bram Moolenaar758711c2005-02-02 23:11:38 +000010796 if (before == l->lv_len)
10797 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010798 else
10799 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010800 item = list_find(l, before);
10801 if (item == NULL)
10802 {
10803 EMSGN(_(e_listidx), before);
10804 l = NULL;
10805 }
10806 }
10807 if (l != NULL)
10808 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010809 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010810 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010811 }
10812 }
10813}
10814
10815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 * "isdirectory()" function
10817 */
10818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010820 typval_T *argvars;
10821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010823 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824}
10825
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010826/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010827 * "islocked()" function
10828 */
10829 static void
10830f_islocked(argvars, rettv)
10831 typval_T *argvars;
10832 typval_T *rettv;
10833{
10834 lval_T lv;
10835 char_u *end;
10836 dictitem_T *di;
10837
10838 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010839 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10840 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010841 if (end != NULL && lv.ll_name != NULL)
10842 {
10843 if (*end != NUL)
10844 EMSG(_(e_trailing));
10845 else
10846 {
10847 if (lv.ll_tv == NULL)
10848 {
10849 if (check_changedtick(lv.ll_name))
10850 rettv->vval.v_number = 1; /* always locked */
10851 else
10852 {
10853 di = find_var(lv.ll_name, NULL);
10854 if (di != NULL)
10855 {
10856 /* Consider a variable locked when:
10857 * 1. the variable itself is locked
10858 * 2. the value of the variable is locked.
10859 * 3. the List or Dict value is locked.
10860 */
10861 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10862 || tv_islocked(&di->di_tv));
10863 }
10864 }
10865 }
10866 else if (lv.ll_range)
10867 EMSG(_("E745: Range not allowed"));
10868 else if (lv.ll_newkey != NULL)
10869 EMSG2(_(e_dictkey), lv.ll_newkey);
10870 else if (lv.ll_list != NULL)
10871 /* List item. */
10872 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10873 else
10874 /* Dictionary item. */
10875 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10876 }
10877 }
10878
10879 clear_lval(&lv);
10880}
10881
Bram Moolenaar33570922005-01-25 22:26:29 +000010882static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010883
10884/*
10885 * Turn a dict into a list:
10886 * "what" == 0: list of keys
10887 * "what" == 1: list of values
10888 * "what" == 2: list of items
10889 */
10890 static void
10891dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010892 typval_T *argvars;
10893 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010894 int what;
10895{
Bram Moolenaar33570922005-01-25 22:26:29 +000010896 list_T *l;
10897 list_T *l2;
10898 dictitem_T *di;
10899 hashitem_T *hi;
10900 listitem_T *li;
10901 listitem_T *li2;
10902 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010903 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010904
10905 rettv->vval.v_number = 0;
10906 if (argvars[0].v_type != VAR_DICT)
10907 {
10908 EMSG(_(e_dictreq));
10909 return;
10910 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010911 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010912 return;
10913
10914 l = list_alloc();
10915 if (l == NULL)
10916 return;
10917 rettv->v_type = VAR_LIST;
10918 rettv->vval.v_list = l;
10919 ++l->lv_refcount;
10920
Bram Moolenaar33570922005-01-25 22:26:29 +000010921 todo = d->dv_hashtab.ht_used;
10922 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010923 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010924 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010925 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010926 --todo;
10927 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010928
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010929 li = listitem_alloc();
10930 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010931 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010932 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010933
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010934 if (what == 0)
10935 {
10936 /* keys() */
10937 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010938 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010939 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10940 }
10941 else if (what == 1)
10942 {
10943 /* values() */
10944 copy_tv(&di->di_tv, &li->li_tv);
10945 }
10946 else
10947 {
10948 /* items() */
10949 l2 = list_alloc();
10950 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010951 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010952 li->li_tv.vval.v_list = l2;
10953 if (l2 == NULL)
10954 break;
10955 ++l2->lv_refcount;
10956
10957 li2 = listitem_alloc();
10958 if (li2 == NULL)
10959 break;
10960 list_append(l2, li2);
10961 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010962 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010963 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
10964
10965 li2 = listitem_alloc();
10966 if (li2 == NULL)
10967 break;
10968 list_append(l2, li2);
10969 copy_tv(&di->di_tv, &li2->li_tv);
10970 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010971 }
10972 }
10973}
10974
10975/*
10976 * "items(dict)" function
10977 */
10978 static void
10979f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010980 typval_T *argvars;
10981 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010982{
10983 dict_list(argvars, rettv, 2);
10984}
10985
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010987 * "join()" function
10988 */
10989 static void
10990f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010991 typval_T *argvars;
10992 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010993{
10994 garray_T ga;
10995 char_u *sep;
10996
10997 rettv->vval.v_number = 0;
10998 if (argvars[0].v_type != VAR_LIST)
10999 {
11000 EMSG(_(e_listreq));
11001 return;
11002 }
11003 if (argvars[0].vval.v_list == NULL)
11004 return;
11005 if (argvars[1].v_type == VAR_UNKNOWN)
11006 sep = (char_u *)" ";
11007 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011008 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011009
11010 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011011
11012 if (sep != NULL)
11013 {
11014 ga_init2(&ga, (int)sizeof(char), 80);
11015 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11016 ga_append(&ga, NUL);
11017 rettv->vval.v_string = (char_u *)ga.ga_data;
11018 }
11019 else
11020 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011021}
11022
11023/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011024 * "keys()" function
11025 */
11026 static void
11027f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011028 typval_T *argvars;
11029 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011030{
11031 dict_list(argvars, rettv, 0);
11032}
11033
11034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 * "last_buffer_nr()" function.
11036 */
11037/*ARGSUSED*/
11038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011040 typval_T *argvars;
11041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042{
11043 int n = 0;
11044 buf_T *buf;
11045
11046 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11047 if (n < buf->b_fnum)
11048 n = buf->b_fnum;
11049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011050 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011051}
11052
11053/*
11054 * "len()" function
11055 */
11056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011058 typval_T *argvars;
11059 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011060{
11061 switch (argvars[0].v_type)
11062 {
11063 case VAR_STRING:
11064 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065 rettv->vval.v_number = (varnumber_T)STRLEN(
11066 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011067 break;
11068 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011069 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011070 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011071 case VAR_DICT:
11072 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11073 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011074 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011075 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011076 break;
11077 }
11078}
11079
Bram Moolenaar33570922005-01-25 22:26:29 +000011080static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011081
11082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011083libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011084 typval_T *argvars;
11085 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011086 int type;
11087{
11088#ifdef FEAT_LIBCALL
11089 char_u *string_in;
11090 char_u **string_result;
11091 int nr_result;
11092#endif
11093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011094 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011095 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011096 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011097 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011098 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011099
11100 if (check_restricted() || check_secure())
11101 return;
11102
11103#ifdef FEAT_LIBCALL
11104 /* The first two args must be strings, otherwise its meaningless */
11105 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11106 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011107 string_in = NULL;
11108 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011109 string_in = argvars[2].vval.v_string;
11110 if (type == VAR_NUMBER)
11111 string_result = NULL;
11112 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011113 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011114 if (mch_libcall(argvars[0].vval.v_string,
11115 argvars[1].vval.v_string,
11116 string_in,
11117 argvars[2].vval.v_number,
11118 string_result,
11119 &nr_result) == OK
11120 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011122 }
11123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124}
11125
11126/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011127 * "libcall()" function
11128 */
11129 static void
11130f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011131 typval_T *argvars;
11132 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011133{
11134 libcall_common(argvars, rettv, VAR_STRING);
11135}
11136
11137/*
11138 * "libcallnr()" function
11139 */
11140 static void
11141f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011142 typval_T *argvars;
11143 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011144{
11145 libcall_common(argvars, rettv, VAR_NUMBER);
11146}
11147
11148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 * "line(string)" function
11150 */
11151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011153 typval_T *argvars;
11154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155{
11156 linenr_T lnum = 0;
11157 pos_T *fp;
11158
11159 fp = var2fpos(&argvars[0], TRUE);
11160 if (fp != NULL)
11161 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011162 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163}
11164
11165/*
11166 * "line2byte(lnum)" function
11167 */
11168/*ARGSUSED*/
11169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011170f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011171 typval_T *argvars;
11172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173{
11174#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011175 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176#else
11177 linenr_T lnum;
11178
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011179 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011181 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011183 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11184 if (rettv->vval.v_number >= 0)
11185 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186#endif
11187}
11188
11189/*
11190 * "lispindent(lnum)" function
11191 */
11192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011193f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011194 typval_T *argvars;
11195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196{
11197#ifdef FEAT_LISP
11198 pos_T pos;
11199 linenr_T lnum;
11200
11201 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011202 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11204 {
11205 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011206 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 curwin->w_cursor = pos;
11208 }
11209 else
11210#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011211 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212}
11213
11214/*
11215 * "localtime()" function
11216 */
11217/*ARGSUSED*/
11218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011219f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011220 typval_T *argvars;
11221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011223 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224}
11225
Bram Moolenaar33570922005-01-25 22:26:29 +000011226static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227
11228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011230 typval_T *argvars;
11231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232 int exact;
11233{
11234 char_u *keys;
11235 char_u *which;
11236 char_u buf[NUMBUFLEN];
11237 char_u *keys_buf = NULL;
11238 char_u *rhs;
11239 int mode;
11240 garray_T ga;
11241
11242 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011243 rettv->v_type = VAR_STRING;
11244 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011246 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 if (*keys == NUL)
11248 return;
11249
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011250 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011251 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 else
11253 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011254 if (which == NULL)
11255 return;
11256
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 mode = get_map_mode(&which, 0);
11258
11259 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011260 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 vim_free(keys_buf);
11262 if (rhs != NULL)
11263 {
11264 ga_init(&ga);
11265 ga.ga_itemsize = 1;
11266 ga.ga_growsize = 40;
11267
11268 while (*rhs != NUL)
11269 ga_concat(&ga, str2special(&rhs, FALSE));
11270
11271 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 }
11274}
11275
11276/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011277 * "map()" function
11278 */
11279 static void
11280f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011281 typval_T *argvars;
11282 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011283{
11284 filter_map(argvars, rettv, TRUE);
11285}
11286
11287/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011288 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 */
11290 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011291f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 typval_T *argvars;
11293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011295 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296}
11297
11298/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011299 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 */
11301 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011302f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011303 typval_T *argvars;
11304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011306 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307}
11308
Bram Moolenaar33570922005-01-25 22:26:29 +000011309static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310
11311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011312find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011313 typval_T *argvars;
11314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 int type;
11316{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011317 char_u *str = NULL;
11318 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 char_u *pat;
11320 regmatch_T regmatch;
11321 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011322 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323 char_u *save_cpo;
11324 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011325 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011326 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011327 list_T *l = NULL;
11328 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011329 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011330 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331
11332 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11333 save_cpo = p_cpo;
11334 p_cpo = (char_u *)"";
11335
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011336 rettv->vval.v_number = -1;
11337 if (type == 3)
11338 {
11339 /* return empty list when there are no matches */
11340 if ((rettv->vval.v_list = list_alloc()) == NULL)
11341 goto theend;
11342 rettv->v_type = VAR_LIST;
11343 ++rettv->vval.v_list->lv_refcount;
11344 }
11345 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347 rettv->v_type = VAR_STRING;
11348 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011351 if (argvars[0].v_type == VAR_LIST)
11352 {
11353 if ((l = argvars[0].vval.v_list) == NULL)
11354 goto theend;
11355 li = l->lv_first;
11356 }
11357 else
11358 expr = str = get_tv_string(&argvars[0]);
11359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011360 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11361 if (pat == NULL)
11362 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011363
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011364 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011366 int error = FALSE;
11367
11368 start = get_tv_number_chk(&argvars[2], &error);
11369 if (error)
11370 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011371 if (l != NULL)
11372 {
11373 li = list_find(l, start);
11374 if (li == NULL)
11375 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011376 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011377 }
11378 else
11379 {
11380 if (start < 0)
11381 start = 0;
11382 if (start > (long)STRLEN(str))
11383 goto theend;
11384 str += start;
11385 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011386
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011387 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011388 nth = get_tv_number_chk(&argvars[3], &error);
11389 if (error)
11390 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391 }
11392
11393 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11394 if (regmatch.regprog != NULL)
11395 {
11396 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011397
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011398 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011399 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011400 if (l != NULL)
11401 {
11402 if (li == NULL)
11403 {
11404 match = FALSE;
11405 break;
11406 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011407 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011408 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011409 if (str == NULL)
11410 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011411 }
11412
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011413 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011414
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011415 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011416 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011417 if (l == NULL && !match)
11418 break;
11419
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011420 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011421 if (l != NULL)
11422 {
11423 li = li->li_next;
11424 ++idx;
11425 }
11426 else
11427 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011428#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011429 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011430#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011431 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011432#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011433 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011434 }
11435
11436 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011438 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011439 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011440 int i;
11441
11442 /* return list with matched string and submatches */
11443 for (i = 0; i < NSUBEXP; ++i)
11444 {
11445 if (regmatch.endp[i] == NULL)
11446 break;
11447 li = listitem_alloc();
11448 if (li == NULL)
11449 break;
11450 li->li_tv.v_type = VAR_STRING;
11451 li->li_tv.v_lock = 0;
11452 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11453 (int)(regmatch.endp[i] - regmatch.startp[i]));
11454 list_append(rettv->vval.v_list, li);
11455 }
11456 }
11457 else if (type == 2)
11458 {
11459 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011460 if (l != NULL)
11461 copy_tv(&li->li_tv, rettv);
11462 else
11463 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011465 }
11466 else if (l != NULL)
11467 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468 else
11469 {
11470 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 (varnumber_T)(regmatch.startp[0] - str);
11473 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 }
11478 }
11479 vim_free(regmatch.regprog);
11480 }
11481
11482theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011483 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484 p_cpo = save_cpo;
11485}
11486
11487/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011488 * "match()" function
11489 */
11490 static void
11491f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011492 typval_T *argvars;
11493 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011494{
11495 find_some_match(argvars, rettv, 1);
11496}
11497
11498/*
11499 * "matchend()" function
11500 */
11501 static void
11502f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011503 typval_T *argvars;
11504 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011505{
11506 find_some_match(argvars, rettv, 0);
11507}
11508
11509/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011510 * "matchlist()" function
11511 */
11512 static void
11513f_matchlist(argvars, rettv)
11514 typval_T *argvars;
11515 typval_T *rettv;
11516{
11517 find_some_match(argvars, rettv, 3);
11518}
11519
11520/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011521 * "matchstr()" function
11522 */
11523 static void
11524f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011525 typval_T *argvars;
11526 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011527{
11528 find_some_match(argvars, rettv, 2);
11529}
11530
Bram Moolenaar33570922005-01-25 22:26:29 +000011531static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011532
11533 static void
11534max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011535 typval_T *argvars;
11536 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011537 int domax;
11538{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011539 long n = 0;
11540 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011541 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011542
11543 if (argvars[0].v_type == VAR_LIST)
11544 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011545 list_T *l;
11546 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011547
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011548 l = argvars[0].vval.v_list;
11549 if (l != NULL)
11550 {
11551 li = l->lv_first;
11552 if (li != NULL)
11553 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011554 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011555 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011556 {
11557 li = li->li_next;
11558 if (li == NULL)
11559 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011560 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011561 if (domax ? i > n : i < n)
11562 n = i;
11563 }
11564 }
11565 }
11566 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011567 else if (argvars[0].v_type == VAR_DICT)
11568 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011569 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011570 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011571 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011572 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011573
11574 d = argvars[0].vval.v_dict;
11575 if (d != NULL)
11576 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011577 todo = d->dv_hashtab.ht_used;
11578 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011579 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011580 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011581 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011582 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011583 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011584 if (first)
11585 {
11586 n = i;
11587 first = FALSE;
11588 }
11589 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011590 n = i;
11591 }
11592 }
11593 }
11594 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011595 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011596 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011597 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011598}
11599
11600/*
11601 * "max()" function
11602 */
11603 static void
11604f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011605 typval_T *argvars;
11606 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011607{
11608 max_min(argvars, rettv, TRUE);
11609}
11610
11611/*
11612 * "min()" function
11613 */
11614 static void
11615f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011616 typval_T *argvars;
11617 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011618{
11619 max_min(argvars, rettv, FALSE);
11620}
11621
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011622static int mkdir_recurse __ARGS((char_u *dir, int prot));
11623
11624/*
11625 * Create the directory in which "dir" is located, and higher levels when
11626 * needed.
11627 */
11628 static int
11629mkdir_recurse(dir, prot)
11630 char_u *dir;
11631 int prot;
11632{
11633 char_u *p;
11634 char_u *updir;
11635 int r = FAIL;
11636
11637 /* Get end of directory name in "dir".
11638 * We're done when it's "/" or "c:/". */
11639 p = gettail_sep(dir);
11640 if (p <= get_past_head(dir))
11641 return OK;
11642
11643 /* If the directory exists we're done. Otherwise: create it.*/
11644 updir = vim_strnsave(dir, (int)(p - dir));
11645 if (updir == NULL)
11646 return FAIL;
11647 if (mch_isdir(updir))
11648 r = OK;
11649 else if (mkdir_recurse(updir, prot) == OK)
11650 r = vim_mkdir_emsg(updir, prot);
11651 vim_free(updir);
11652 return r;
11653}
11654
11655#ifdef vim_mkdir
11656/*
11657 * "mkdir()" function
11658 */
11659 static void
11660f_mkdir(argvars, rettv)
11661 typval_T *argvars;
11662 typval_T *rettv;
11663{
11664 char_u *dir;
11665 char_u buf[NUMBUFLEN];
11666 int prot = 0755;
11667
11668 rettv->vval.v_number = FAIL;
11669 if (check_restricted() || check_secure())
11670 return;
11671
11672 dir = get_tv_string_buf(&argvars[0], buf);
11673 if (argvars[1].v_type != VAR_UNKNOWN)
11674 {
11675 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011676 prot = get_tv_number_chk(&argvars[2], NULL);
11677 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011678 mkdir_recurse(dir, prot);
11679 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011680 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011681}
11682#endif
11683
Bram Moolenaar0d660222005-01-07 21:51:51 +000011684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685 * "mode()" function
11686 */
11687/*ARGSUSED*/
11688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011689f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011690 typval_T *argvars;
11691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692{
11693 char_u buf[2];
11694
11695#ifdef FEAT_VISUAL
11696 if (VIsual_active)
11697 {
11698 if (VIsual_select)
11699 buf[0] = VIsual_mode + 's' - 'v';
11700 else
11701 buf[0] = VIsual_mode;
11702 }
11703 else
11704#endif
11705 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11706 buf[0] = 'r';
11707 else if (State & INSERT)
11708 {
11709 if (State & REPLACE_FLAG)
11710 buf[0] = 'R';
11711 else
11712 buf[0] = 'i';
11713 }
11714 else if (State & CMDLINE)
11715 buf[0] = 'c';
11716 else
11717 buf[0] = 'n';
11718
11719 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011720 rettv->vval.v_string = vim_strsave(buf);
11721 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722}
11723
11724/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011725 * "nextnonblank()" function
11726 */
11727 static void
11728f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011729 typval_T *argvars;
11730 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011731{
11732 linenr_T lnum;
11733
11734 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011736 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011737 {
11738 lnum = 0;
11739 break;
11740 }
11741 if (*skipwhite(ml_get(lnum)) != NUL)
11742 break;
11743 }
11744 rettv->vval.v_number = lnum;
11745}
11746
11747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011748 * "nr2char()" function
11749 */
11750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011751f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011752 typval_T *argvars;
11753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754{
11755 char_u buf[NUMBUFLEN];
11756
11757#ifdef FEAT_MBYTE
11758 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011759 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760 else
11761#endif
11762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011763 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 buf[1] = NUL;
11765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011766 rettv->v_type = VAR_STRING;
11767 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011768}
11769
11770/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011771 * "prevnonblank()" function
11772 */
11773 static void
11774f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011775 typval_T *argvars;
11776 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011777{
11778 linenr_T lnum;
11779
11780 lnum = get_tv_lnum(argvars);
11781 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11782 lnum = 0;
11783 else
11784 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11785 --lnum;
11786 rettv->vval.v_number = lnum;
11787}
11788
Bram Moolenaar8c711452005-01-14 21:53:12 +000011789/*
11790 * "range()" function
11791 */
11792 static void
11793f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011794 typval_T *argvars;
11795 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011796{
11797 long start;
11798 long end;
11799 long stride = 1;
11800 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011801 list_T *l;
11802 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011803 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011804
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011805 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011806 if (argvars[1].v_type == VAR_UNKNOWN)
11807 {
11808 end = start - 1;
11809 start = 0;
11810 }
11811 else
11812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011813 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011814 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011815 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011816 }
11817
11818 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011819 if (error)
11820 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000011821 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011822 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000011823 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011824 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011825 else
11826 {
11827 l = list_alloc();
11828 if (l != NULL)
11829 {
11830 rettv->v_type = VAR_LIST;
11831 rettv->vval.v_list = l;
11832 ++l->lv_refcount;
11833
11834 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11835 {
11836 li = listitem_alloc();
11837 if (li == NULL)
11838 break;
11839 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011840 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011841 li->li_tv.vval.v_number = i;
11842 list_append(l, li);
11843 }
11844 }
11845 }
11846}
11847
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011848/*
11849 * "readfile()" function
11850 */
11851 static void
11852f_readfile(argvars, rettv)
11853 typval_T *argvars;
11854 typval_T *rettv;
11855{
11856 int binary = FALSE;
11857 char_u *fname;
11858 FILE *fd;
11859 list_T *l;
11860 listitem_T *li;
11861#define FREAD_SIZE 200 /* optimized for text lines */
11862 char_u buf[FREAD_SIZE];
11863 int readlen; /* size of last fread() */
11864 int buflen; /* nr of valid chars in buf[] */
11865 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11866 int tolist; /* first byte in buf[] still to be put in list */
11867 int chop; /* how many CR to chop off */
11868 char_u *prev = NULL; /* previously read bytes, if any */
11869 int prevlen = 0; /* length of "prev" if not NULL */
11870 char_u *s;
11871 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011872 long maxline = MAXLNUM;
11873 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011874
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011875 if (argvars[1].v_type != VAR_UNKNOWN)
11876 {
11877 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11878 binary = TRUE;
11879 if (argvars[2].v_type != VAR_UNKNOWN)
11880 maxline = get_tv_number(&argvars[2]);
11881 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011882
11883 l = list_alloc();
11884 if (l == NULL)
11885 return;
11886 rettv->v_type = VAR_LIST;
11887 rettv->vval.v_list = l;
11888 l->lv_refcount = 1;
11889
11890 /* Always open the file in binary mode, library functions have a mind of
11891 * their own about CR-LF conversion. */
11892 fname = get_tv_string(&argvars[0]);
11893 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11894 {
11895 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11896 return;
11897 }
11898
11899 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011900 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011901 {
11902 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11903 buflen = filtd + readlen;
11904 tolist = 0;
11905 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11906 {
11907 if (buf[filtd] == '\n' || readlen <= 0)
11908 {
11909 /* Only when in binary mode add an empty list item when the
11910 * last line ends in a '\n'. */
11911 if (!binary && readlen == 0 && filtd == 0)
11912 break;
11913
11914 /* Found end-of-line or end-of-file: add a text line to the
11915 * list. */
11916 chop = 0;
11917 if (!binary)
11918 while (filtd - chop - 1 >= tolist
11919 && buf[filtd - chop - 1] == '\r')
11920 ++chop;
11921 len = filtd - tolist - chop;
11922 if (prev == NULL)
11923 s = vim_strnsave(buf + tolist, len);
11924 else
11925 {
11926 s = alloc((unsigned)(prevlen + len + 1));
11927 if (s != NULL)
11928 {
11929 mch_memmove(s, prev, prevlen);
11930 vim_free(prev);
11931 prev = NULL;
11932 mch_memmove(s + prevlen, buf + tolist, len);
11933 s[prevlen + len] = NUL;
11934 }
11935 }
11936 tolist = filtd + 1;
11937
11938 li = listitem_alloc();
11939 if (li == NULL)
11940 {
11941 vim_free(s);
11942 break;
11943 }
11944 li->li_tv.v_type = VAR_STRING;
11945 li->li_tv.v_lock = 0;
11946 li->li_tv.vval.v_string = s;
11947 list_append(l, li);
11948
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011949 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011950 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011951 if (readlen <= 0)
11952 break;
11953 }
11954 else if (buf[filtd] == NUL)
11955 buf[filtd] = '\n';
11956 }
11957 if (readlen <= 0)
11958 break;
11959
11960 if (tolist == 0)
11961 {
11962 /* "buf" is full, need to move text to an allocated buffer */
11963 if (prev == NULL)
11964 {
11965 prev = vim_strnsave(buf, buflen);
11966 prevlen = buflen;
11967 }
11968 else
11969 {
11970 s = alloc((unsigned)(prevlen + buflen));
11971 if (s != NULL)
11972 {
11973 mch_memmove(s, prev, prevlen);
11974 mch_memmove(s + prevlen, buf, buflen);
11975 vim_free(prev);
11976 prev = s;
11977 prevlen += buflen;
11978 }
11979 }
11980 filtd = 0;
11981 }
11982 else
11983 {
11984 mch_memmove(buf, buf + tolist, buflen - tolist);
11985 filtd -= tolist;
11986 }
11987 }
11988
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011989 /*
11990 * For a negative line count use only the lines at the end of the file,
11991 * free the rest.
11992 */
11993 if (maxline < 0)
11994 while (cnt > -maxline)
11995 {
11996 listitem_remove(l, l->lv_first);
11997 --cnt;
11998 }
11999
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012000 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012001 fclose(fd);
12002}
12003
12004
Bram Moolenaar0d660222005-01-07 21:51:51 +000012005#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12006static void make_connection __ARGS((void));
12007static int check_connection __ARGS((void));
12008
12009 static void
12010make_connection()
12011{
12012 if (X_DISPLAY == NULL
12013# ifdef FEAT_GUI
12014 && !gui.in_use
12015# endif
12016 )
12017 {
12018 x_force_connect = TRUE;
12019 setup_term_clip();
12020 x_force_connect = FALSE;
12021 }
12022}
12023
12024 static int
12025check_connection()
12026{
12027 make_connection();
12028 if (X_DISPLAY == NULL)
12029 {
12030 EMSG(_("E240: No connection to Vim server"));
12031 return FAIL;
12032 }
12033 return OK;
12034}
12035#endif
12036
12037#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012038static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012039
12040 static void
12041remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012042 typval_T *argvars;
12043 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012044 int expr;
12045{
12046 char_u *server_name;
12047 char_u *keys;
12048 char_u *r = NULL;
12049 char_u buf[NUMBUFLEN];
12050# ifdef WIN32
12051 HWND w;
12052# else
12053 Window w;
12054# endif
12055
12056 if (check_restricted() || check_secure())
12057 return;
12058
12059# ifdef FEAT_X11
12060 if (check_connection() == FAIL)
12061 return;
12062# endif
12063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012064 server_name = get_tv_string_chk(&argvars[0]);
12065 if (server_name == NULL)
12066 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012067 keys = get_tv_string_buf(&argvars[1], buf);
12068# ifdef WIN32
12069 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12070# else
12071 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12072 < 0)
12073# endif
12074 {
12075 if (r != NULL)
12076 EMSG(r); /* sending worked but evaluation failed */
12077 else
12078 EMSG2(_("E241: Unable to send to %s"), server_name);
12079 return;
12080 }
12081
12082 rettv->vval.v_string = r;
12083
12084 if (argvars[2].v_type != VAR_UNKNOWN)
12085 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012086 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012087 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012088 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012089
12090 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012091 v.di_tv.v_type = VAR_STRING;
12092 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012093 idvar = get_tv_string_chk(&argvars[2]);
12094 if (idvar != NULL)
12095 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012096 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012097 }
12098}
12099#endif
12100
12101/*
12102 * "remote_expr()" function
12103 */
12104/*ARGSUSED*/
12105 static void
12106f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012107 typval_T *argvars;
12108 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012109{
12110 rettv->v_type = VAR_STRING;
12111 rettv->vval.v_string = NULL;
12112#ifdef FEAT_CLIENTSERVER
12113 remote_common(argvars, rettv, TRUE);
12114#endif
12115}
12116
12117/*
12118 * "remote_foreground()" function
12119 */
12120/*ARGSUSED*/
12121 static void
12122f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012123 typval_T *argvars;
12124 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012125{
12126 rettv->vval.v_number = 0;
12127#ifdef FEAT_CLIENTSERVER
12128# ifdef WIN32
12129 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012130 {
12131 char_u *server_name = get_tv_string_chk(&argvars[0]);
12132
12133 if (server_name != NULL)
12134 serverForeground(server_name);
12135 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012136# else
12137 /* Send a foreground() expression to the server. */
12138 argvars[1].v_type = VAR_STRING;
12139 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12140 argvars[2].v_type = VAR_UNKNOWN;
12141 remote_common(argvars, rettv, TRUE);
12142 vim_free(argvars[1].vval.v_string);
12143# endif
12144#endif
12145}
12146
12147/*ARGSUSED*/
12148 static void
12149f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012150 typval_T *argvars;
12151 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012152{
12153#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012154 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012155 char_u *s = NULL;
12156# ifdef WIN32
12157 int n = 0;
12158# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012159 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012160
12161 if (check_restricted() || check_secure())
12162 {
12163 rettv->vval.v_number = -1;
12164 return;
12165 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012166 serverid = get_tv_string_chk(&argvars[0]);
12167 if (serverid == NULL)
12168 {
12169 rettv->vval.v_number = -1;
12170 return; /* type error; errmsg already given */
12171 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012172# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012173 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012174 if (n == 0)
12175 rettv->vval.v_number = -1;
12176 else
12177 {
12178 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12179 rettv->vval.v_number = (s != NULL);
12180 }
12181# else
12182 rettv->vval.v_number = 0;
12183 if (check_connection() == FAIL)
12184 return;
12185
12186 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012187 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012188# endif
12189
12190 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012192 char_u *retvar;
12193
Bram Moolenaar33570922005-01-25 22:26:29 +000012194 v.di_tv.v_type = VAR_STRING;
12195 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012196 retvar = get_tv_string_chk(&argvars[1]);
12197 if (retvar != NULL)
12198 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012199 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012200 }
12201#else
12202 rettv->vval.v_number = -1;
12203#endif
12204}
12205
12206/*ARGSUSED*/
12207 static void
12208f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012209 typval_T *argvars;
12210 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012211{
12212 char_u *r = NULL;
12213
12214#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012215 char_u *serverid = get_tv_string_chk(&argvars[0]);
12216
12217 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012218 {
12219# ifdef WIN32
12220 /* The server's HWND is encoded in the 'id' parameter */
12221 int n = 0;
12222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012223 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012224 if (n != 0)
12225 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12226 if (r == NULL)
12227# else
12228 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012229 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012230# endif
12231 EMSG(_("E277: Unable to read a server reply"));
12232 }
12233#endif
12234 rettv->v_type = VAR_STRING;
12235 rettv->vval.v_string = r;
12236}
12237
12238/*
12239 * "remote_send()" function
12240 */
12241/*ARGSUSED*/
12242 static void
12243f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012244 typval_T *argvars;
12245 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012246{
12247 rettv->v_type = VAR_STRING;
12248 rettv->vval.v_string = NULL;
12249#ifdef FEAT_CLIENTSERVER
12250 remote_common(argvars, rettv, FALSE);
12251#endif
12252}
12253
12254/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012255 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012256 */
12257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012259 typval_T *argvars;
12260 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012261{
Bram Moolenaar33570922005-01-25 22:26:29 +000012262 list_T *l;
12263 listitem_T *item, *item2;
12264 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012265 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012266 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012267 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012268 dict_T *d;
12269 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012270
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012271 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012272 if (argvars[0].v_type == VAR_DICT)
12273 {
12274 if (argvars[2].v_type != VAR_UNKNOWN)
12275 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012276 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012277 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012279 key = get_tv_string_chk(&argvars[1]);
12280 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012282 di = dict_find(d, key, -1);
12283 if (di == NULL)
12284 EMSG2(_(e_dictkey), key);
12285 else
12286 {
12287 *rettv = di->di_tv;
12288 init_tv(&di->di_tv);
12289 dictitem_remove(d, di);
12290 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012291 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012292 }
12293 }
12294 else if (argvars[0].v_type != VAR_LIST)
12295 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012296 else if ((l = argvars[0].vval.v_list) != NULL
12297 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012299 int error = FALSE;
12300
12301 idx = get_tv_number_chk(&argvars[1], &error);
12302 if (error)
12303 ; /* type error: do nothing, errmsg already given */
12304 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012305 EMSGN(_(e_listidx), idx);
12306 else
12307 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012308 if (argvars[2].v_type == VAR_UNKNOWN)
12309 {
12310 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012311 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012312 *rettv = item->li_tv;
12313 vim_free(item);
12314 }
12315 else
12316 {
12317 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012318 end = get_tv_number_chk(&argvars[2], &error);
12319 if (error)
12320 ; /* type error: do nothing */
12321 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012322 EMSGN(_(e_listidx), end);
12323 else
12324 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012325 int cnt = 0;
12326
12327 for (li = item; li != NULL; li = li->li_next)
12328 {
12329 ++cnt;
12330 if (li == item2)
12331 break;
12332 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012333 if (li == NULL) /* didn't find "item2" after "item" */
12334 EMSG(_(e_invrange));
12335 else
12336 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012337 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012338 l = list_alloc();
12339 if (l != NULL)
12340 {
12341 rettv->v_type = VAR_LIST;
12342 rettv->vval.v_list = l;
12343 l->lv_first = item;
12344 l->lv_last = item2;
12345 l->lv_refcount = 1;
12346 item->li_prev = NULL;
12347 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012348 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012349 }
12350 }
12351 }
12352 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012353 }
12354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355}
12356
12357/*
12358 * "rename({from}, {to})" function
12359 */
12360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012361f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012362 typval_T *argvars;
12363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364{
12365 char_u buf[NUMBUFLEN];
12366
12367 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012370 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12371 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372}
12373
12374/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012375 * "repeat()" function
12376 */
12377/*ARGSUSED*/
12378 static void
12379f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012380 typval_T *argvars;
12381 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012382{
12383 char_u *p;
12384 int n;
12385 int slen;
12386 int len;
12387 char_u *r;
12388 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012389 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012390
12391 n = get_tv_number(&argvars[1]);
12392 if (argvars[0].v_type == VAR_LIST)
12393 {
12394 l = list_alloc();
12395 if (l != NULL && argvars[0].vval.v_list != NULL)
12396 {
12397 l->lv_refcount = 1;
12398 while (n-- > 0)
12399 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12400 break;
12401 }
12402 rettv->v_type = VAR_LIST;
12403 rettv->vval.v_list = l;
12404 }
12405 else
12406 {
12407 p = get_tv_string(&argvars[0]);
12408 rettv->v_type = VAR_STRING;
12409 rettv->vval.v_string = NULL;
12410
12411 slen = (int)STRLEN(p);
12412 len = slen * n;
12413 if (len <= 0)
12414 return;
12415
12416 r = alloc(len + 1);
12417 if (r != NULL)
12418 {
12419 for (i = 0; i < n; i++)
12420 mch_memmove(r + i * slen, p, (size_t)slen);
12421 r[len] = NUL;
12422 }
12423
12424 rettv->vval.v_string = r;
12425 }
12426}
12427
12428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429 * "resolve()" function
12430 */
12431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012432f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012433 typval_T *argvars;
12434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435{
12436 char_u *p;
12437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012438 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439#ifdef FEAT_SHORTCUT
12440 {
12441 char_u *v = NULL;
12442
12443 v = mch_resolve_shortcut(p);
12444 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012445 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012447 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012448 }
12449#else
12450# ifdef HAVE_READLINK
12451 {
12452 char_u buf[MAXPATHL + 1];
12453 char_u *cpy;
12454 int len;
12455 char_u *remain = NULL;
12456 char_u *q;
12457 int is_relative_to_current = FALSE;
12458 int has_trailing_pathsep = FALSE;
12459 int limit = 100;
12460
12461 p = vim_strsave(p);
12462
12463 if (p[0] == '.' && (vim_ispathsep(p[1])
12464 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12465 is_relative_to_current = TRUE;
12466
12467 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012468 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469 has_trailing_pathsep = TRUE;
12470
12471 q = getnextcomp(p);
12472 if (*q != NUL)
12473 {
12474 /* Separate the first path component in "p", and keep the
12475 * remainder (beginning with the path separator). */
12476 remain = vim_strsave(q - 1);
12477 q[-1] = NUL;
12478 }
12479
12480 for (;;)
12481 {
12482 for (;;)
12483 {
12484 len = readlink((char *)p, (char *)buf, MAXPATHL);
12485 if (len <= 0)
12486 break;
12487 buf[len] = NUL;
12488
12489 if (limit-- == 0)
12490 {
12491 vim_free(p);
12492 vim_free(remain);
12493 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012494 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495 goto fail;
12496 }
12497
12498 /* Ensure that the result will have a trailing path separator
12499 * if the argument has one. */
12500 if (remain == NULL && has_trailing_pathsep)
12501 add_pathsep(buf);
12502
12503 /* Separate the first path component in the link value and
12504 * concatenate the remainders. */
12505 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12506 if (*q != NUL)
12507 {
12508 if (remain == NULL)
12509 remain = vim_strsave(q - 1);
12510 else
12511 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012512 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 if (cpy != NULL)
12514 {
12515 STRCAT(cpy, remain);
12516 vim_free(remain);
12517 remain = cpy;
12518 }
12519 }
12520 q[-1] = NUL;
12521 }
12522
12523 q = gettail(p);
12524 if (q > p && *q == NUL)
12525 {
12526 /* Ignore trailing path separator. */
12527 q[-1] = NUL;
12528 q = gettail(p);
12529 }
12530 if (q > p && !mch_isFullName(buf))
12531 {
12532 /* symlink is relative to directory of argument */
12533 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12534 if (cpy != NULL)
12535 {
12536 STRCPY(cpy, p);
12537 STRCPY(gettail(cpy), buf);
12538 vim_free(p);
12539 p = cpy;
12540 }
12541 }
12542 else
12543 {
12544 vim_free(p);
12545 p = vim_strsave(buf);
12546 }
12547 }
12548
12549 if (remain == NULL)
12550 break;
12551
12552 /* Append the first path component of "remain" to "p". */
12553 q = getnextcomp(remain + 1);
12554 len = q - remain - (*q != NUL);
12555 cpy = vim_strnsave(p, STRLEN(p) + len);
12556 if (cpy != NULL)
12557 {
12558 STRNCAT(cpy, remain, len);
12559 vim_free(p);
12560 p = cpy;
12561 }
12562 /* Shorten "remain". */
12563 if (*q != NUL)
12564 STRCPY(remain, q - 1);
12565 else
12566 {
12567 vim_free(remain);
12568 remain = NULL;
12569 }
12570 }
12571
12572 /* If the result is a relative path name, make it explicitly relative to
12573 * the current directory if and only if the argument had this form. */
12574 if (!vim_ispathsep(*p))
12575 {
12576 if (is_relative_to_current
12577 && *p != NUL
12578 && !(p[0] == '.'
12579 && (p[1] == NUL
12580 || vim_ispathsep(p[1])
12581 || (p[1] == '.'
12582 && (p[2] == NUL
12583 || vim_ispathsep(p[2]))))))
12584 {
12585 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012586 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587 if (cpy != NULL)
12588 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012589 vim_free(p);
12590 p = cpy;
12591 }
12592 }
12593 else if (!is_relative_to_current)
12594 {
12595 /* Strip leading "./". */
12596 q = p;
12597 while (q[0] == '.' && vim_ispathsep(q[1]))
12598 q += 2;
12599 if (q > p)
12600 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12601 }
12602 }
12603
12604 /* Ensure that the result will have no trailing path separator
12605 * if the argument had none. But keep "/" or "//". */
12606 if (!has_trailing_pathsep)
12607 {
12608 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012609 if (after_pathsep(p, q))
12610 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611 }
12612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012613 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 }
12615# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012616 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617# endif
12618#endif
12619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012620 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621
12622#ifdef HAVE_READLINK
12623fail:
12624#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012625 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626}
12627
12628/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012629 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630 */
12631 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012632f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012633 typval_T *argvars;
12634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635{
Bram Moolenaar33570922005-01-25 22:26:29 +000012636 list_T *l;
12637 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638
Bram Moolenaar0d660222005-01-07 21:51:51 +000012639 rettv->vval.v_number = 0;
12640 if (argvars[0].v_type != VAR_LIST)
12641 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012642 else if ((l = argvars[0].vval.v_list) != NULL
12643 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012644 {
12645 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012646 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012647 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012648 while (li != NULL)
12649 {
12650 ni = li->li_prev;
12651 list_append(l, li);
12652 li = ni;
12653 }
12654 rettv->vval.v_list = l;
12655 rettv->v_type = VAR_LIST;
12656 ++l->lv_refcount;
12657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658}
12659
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012660#define SP_NOMOVE 1 /* don't move cursor */
12661#define SP_REPEAT 2 /* repeat to find outer pair */
12662#define SP_RETCOUNT 4 /* return matchcount */
12663
Bram Moolenaar33570922005-01-25 22:26:29 +000012664static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012665
12666/*
12667 * Get flags for a search function.
12668 * Possibly sets "p_ws".
12669 * Returns BACKWARD, FORWARD or zero (for an error).
12670 */
12671 static int
12672get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012673 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012674 int *flagsp;
12675{
12676 int dir = FORWARD;
12677 char_u *flags;
12678 char_u nbuf[NUMBUFLEN];
12679 int mask;
12680
12681 if (varp->v_type != VAR_UNKNOWN)
12682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012683 flags = get_tv_string_buf_chk(varp, nbuf);
12684 if (flags == NULL)
12685 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012686 while (*flags != NUL)
12687 {
12688 switch (*flags)
12689 {
12690 case 'b': dir = BACKWARD; break;
12691 case 'w': p_ws = TRUE; break;
12692 case 'W': p_ws = FALSE; break;
12693 default: mask = 0;
12694 if (flagsp != NULL)
12695 switch (*flags)
12696 {
12697 case 'n': mask = SP_NOMOVE; break;
12698 case 'r': mask = SP_REPEAT; break;
12699 case 'm': mask = SP_RETCOUNT; break;
12700 }
12701 if (mask == 0)
12702 {
12703 EMSG2(_(e_invarg2), flags);
12704 dir = 0;
12705 }
12706 else
12707 *flagsp |= mask;
12708 }
12709 if (dir == 0)
12710 break;
12711 ++flags;
12712 }
12713 }
12714 return dir;
12715}
12716
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717/*
12718 * "search()" function
12719 */
12720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012721f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012722 typval_T *argvars;
12723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724{
12725 char_u *pat;
12726 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012727 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728 int save_p_ws = p_ws;
12729 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012730 int flags = 0;
12731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012734 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012735 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12736 if (dir == 0)
12737 goto theend;
12738 if ((flags & ~SP_NOMOVE) != 0)
12739 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012740 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012741 goto theend;
12742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012744 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12746 SEARCH_KEEP, RE_SEARCH) != FAIL)
12747 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012748 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749 curwin->w_cursor = pos;
12750 /* "/$" will put the cursor after the end of the line, may need to
12751 * correct that here */
12752 check_cursor();
12753 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012754
12755 /* If 'n' flag is used: restore cursor position. */
12756 if (flags & SP_NOMOVE)
12757 curwin->w_cursor = save_cursor;
12758theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759 p_ws = save_p_ws;
12760}
12761
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762/*
12763 * "searchpair()" function
12764 */
12765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012766f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012767 typval_T *argvars;
12768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769{
12770 char_u *spat, *mpat, *epat;
12771 char_u *skip;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012772 char_u *pat, *pat2 = NULL, *pat3 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773 pos_T pos;
12774 pos_T firstpos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012775 pos_T foundpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776 pos_T save_cursor;
12777 pos_T save_pos;
12778 int save_p_ws = p_ws;
12779 char_u *save_cpo;
12780 int dir;
12781 int flags = 0;
12782 char_u nbuf1[NUMBUFLEN];
12783 char_u nbuf2[NUMBUFLEN];
12784 char_u nbuf3[NUMBUFLEN];
12785 int n;
12786 int r;
12787 int nest = 1;
12788 int err;
12789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012790 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791
12792 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12793 save_cpo = p_cpo;
12794 p_cpo = (char_u *)"";
12795
12796 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012797 spat = get_tv_string_chk(&argvars[0]);
12798 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
12799 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
12800 if (spat == NULL || mpat == NULL || epat == NULL)
12801 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802
12803 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12804 * start/middle/end (pat3, for the top pair). */
12805 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12806 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12807 if (pat2 == NULL || pat3 == NULL)
12808 goto theend;
12809 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12810 if (*mpat == NUL)
12811 STRCPY(pat3, pat2);
12812 else
12813 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12814 spat, epat, mpat);
12815
12816 /* Handle the optional fourth argument: flags */
12817 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012818 if (dir == 0)
12819 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820
12821 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012822 if (argvars[3].v_type == VAR_UNKNOWN
12823 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824 skip = (char_u *)"";
12825 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012826 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
12827 if (skip == NULL)
12828 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829
12830 save_cursor = curwin->w_cursor;
12831 pos = curwin->w_cursor;
12832 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012833 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834 pat = pat3;
12835 for (;;)
12836 {
12837 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12838 SEARCH_KEEP, RE_SEARCH);
12839 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12840 /* didn't find it or found the first match again: FAIL */
12841 break;
12842
12843 if (firstpos.lnum == 0)
12844 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012845 if (equalpos(pos, foundpos))
12846 {
12847 /* Found the same position again. Can happen with a pattern that
12848 * has "\zs" at the end and searching backwards. Advance one
12849 * character and try again. */
12850 if (dir == BACKWARD)
12851 decl(&pos);
12852 else
12853 incl(&pos);
12854 }
12855 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856
12857 /* If the skip pattern matches, ignore this match. */
12858 if (*skip != NUL)
12859 {
12860 save_pos = curwin->w_cursor;
12861 curwin->w_cursor = pos;
12862 r = eval_to_bool(skip, &err, NULL, FALSE);
12863 curwin->w_cursor = save_pos;
12864 if (err)
12865 {
12866 /* Evaluating {skip} caused an error, break here. */
12867 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 break;
12870 }
12871 if (r)
12872 continue;
12873 }
12874
12875 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12876 {
12877 /* Found end when searching backwards or start when searching
12878 * forward: nested pair. */
12879 ++nest;
12880 pat = pat2; /* nested, don't search for middle */
12881 }
12882 else
12883 {
12884 /* Found end when searching forward or start when searching
12885 * backward: end of (nested) pair; or found middle in outer pair. */
12886 if (--nest == 1)
12887 pat = pat3; /* outer level, search for middle */
12888 }
12889
12890 if (nest == 0)
12891 {
12892 /* Found the match: return matchcount or line number. */
12893 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012894 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012896 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897 curwin->w_cursor = pos;
12898 if (!(flags & SP_REPEAT))
12899 break;
12900 nest = 1; /* search for next unmatched */
12901 }
12902 }
12903
12904 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012905 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906 curwin->w_cursor = save_cursor;
12907
12908theend:
12909 vim_free(pat2);
12910 vim_free(pat3);
12911 p_ws = save_p_ws;
12912 p_cpo = save_cpo;
12913}
12914
Bram Moolenaar0d660222005-01-07 21:51:51 +000012915/*ARGSUSED*/
12916 static void
12917f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012918 typval_T *argvars;
12919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012921#ifdef FEAT_CLIENTSERVER
12922 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012923 char_u *server = get_tv_string_chk(&argvars[0]);
12924 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925
Bram Moolenaar0d660222005-01-07 21:51:51 +000012926 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012927 if (server == NULL || reply == NULL)
12928 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012929 if (check_restricted() || check_secure())
12930 return;
12931# ifdef FEAT_X11
12932 if (check_connection() == FAIL)
12933 return;
12934# endif
12935
12936 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012938 EMSG(_("E258: Unable to send to client"));
12939 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012941 rettv->vval.v_number = 0;
12942#else
12943 rettv->vval.v_number = -1;
12944#endif
12945}
12946
12947/*ARGSUSED*/
12948 static void
12949f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012950 typval_T *argvars;
12951 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012952{
12953 char_u *r = NULL;
12954
12955#ifdef FEAT_CLIENTSERVER
12956# ifdef WIN32
12957 r = serverGetVimNames();
12958# else
12959 make_connection();
12960 if (X_DISPLAY != NULL)
12961 r = serverGetVimNames(X_DISPLAY);
12962# endif
12963#endif
12964 rettv->v_type = VAR_STRING;
12965 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966}
12967
12968/*
12969 * "setbufvar()" function
12970 */
12971/*ARGSUSED*/
12972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012973f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012974 typval_T *argvars;
12975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976{
12977 buf_T *buf;
12978#ifdef FEAT_AUTOCMD
12979 aco_save_T aco;
12980#else
12981 buf_T *save_curbuf;
12982#endif
12983 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012984 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985 char_u nbuf[NUMBUFLEN];
12986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012987 rettv->vval.v_number = 0;
12988
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989 if (check_restricted() || check_secure())
12990 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012991 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12992 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012993 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994 varp = &argvars[2];
12995
12996 if (buf != NULL && varname != NULL && varp != NULL)
12997 {
12998 /* set curbuf to be our buf, temporarily */
12999#ifdef FEAT_AUTOCMD
13000 aucmd_prepbuf(&aco, buf);
13001#else
13002 save_curbuf = curbuf;
13003 curbuf = buf;
13004#endif
13005
13006 if (*varname == '&')
13007 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013008 long numval;
13009 char_u *strval;
13010 int error = FALSE;
13011
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013013 numval = get_tv_number_chk(varp, &error);
13014 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013015 if (!error && strval != NULL)
13016 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017 }
13018 else
13019 {
13020 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13021 if (bufvarname != NULL)
13022 {
13023 STRCPY(bufvarname, "b:");
13024 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013025 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026 vim_free(bufvarname);
13027 }
13028 }
13029
13030 /* reset notion of buffer */
13031#ifdef FEAT_AUTOCMD
13032 aucmd_restbuf(&aco);
13033#else
13034 curbuf = save_curbuf;
13035#endif
13036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037}
13038
13039/*
13040 * "setcmdpos()" function
13041 */
13042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013044 typval_T *argvars;
13045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013047 int pos = (int)get_tv_number(&argvars[0]) - 1;
13048
13049 if (pos >= 0)
13050 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051}
13052
13053/*
13054 * "setline()" function
13055 */
13056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013057f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013058 typval_T *argvars;
13059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060{
13061 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013062 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013063 list_T *l = NULL;
13064 listitem_T *li = NULL;
13065 long added = 0;
13066 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013068 lnum = get_tv_lnum(&argvars[0]);
13069 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013071 l = argvars[1].vval.v_list;
13072 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013074 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013075 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013076
13077 rettv->vval.v_number = 0; /* OK */
13078 for (;;)
13079 {
13080 if (l != NULL)
13081 {
13082 /* list argument, get next string */
13083 if (li == NULL)
13084 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013085 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013086 li = li->li_next;
13087 }
13088
13089 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013090 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013091 break;
13092 if (lnum <= curbuf->b_ml.ml_line_count)
13093 {
13094 /* existing line, replace it */
13095 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13096 {
13097 changed_bytes(lnum, 0);
13098 check_cursor_col();
13099 rettv->vval.v_number = 0; /* OK */
13100 }
13101 }
13102 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13103 {
13104 /* lnum is one past the last line, append the line */
13105 ++added;
13106 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13107 rettv->vval.v_number = 0; /* OK */
13108 }
13109
13110 if (l == NULL) /* only one string argument */
13111 break;
13112 ++lnum;
13113 }
13114
13115 if (added > 0)
13116 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117}
13118
13119/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013120 * "setqflist()" function
13121 */
13122/*ARGSUSED*/
13123 static void
13124f_setqflist(argvars, rettv)
13125 typval_T *argvars;
13126 typval_T *rettv;
13127{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013128 char_u *act;
13129 int action = ' ';
13130
Bram Moolenaar2641f772005-03-25 21:58:17 +000013131 rettv->vval.v_number = -1;
13132
13133#ifdef FEAT_QUICKFIX
13134 if (argvars[0].v_type != VAR_LIST)
13135 EMSG(_(e_listreq));
13136 else
13137 {
13138 list_T *l = argvars[0].vval.v_list;
13139
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013140 if (argvars[1].v_type == VAR_STRING)
13141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013142 act = get_tv_string_chk(&argvars[1]);
13143 if (act == NULL)
13144 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013145 if (*act == 'a' || *act == 'r')
13146 action = *act;
13147 }
13148
13149 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013150 rettv->vval.v_number = 0;
13151 }
13152#endif
13153}
13154
13155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156 * "setreg()" function
13157 */
13158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013159f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013160 typval_T *argvars;
13161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162{
13163 int regname;
13164 char_u *strregname;
13165 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013166 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167 int append;
13168 char_u yank_type;
13169 long block_len;
13170
13171 block_len = -1;
13172 yank_type = MAUTO;
13173 append = FALSE;
13174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013175 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013176 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013178 if (strregname == NULL)
13179 return; /* type error; errmsg already given */
13180 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181 if (regname == 0 || regname == '@')
13182 regname = '"';
13183 else if (regname == '=')
13184 return;
13185
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013186 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013188 stropt = get_tv_string_chk(&argvars[2]);
13189 if (stropt == NULL)
13190 return; /* type error */
13191 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192 switch (*stropt)
13193 {
13194 case 'a': case 'A': /* append */
13195 append = TRUE;
13196 break;
13197 case 'v': case 'c': /* character-wise selection */
13198 yank_type = MCHAR;
13199 break;
13200 case 'V': case 'l': /* line-wise selection */
13201 yank_type = MLINE;
13202 break;
13203#ifdef FEAT_VISUAL
13204 case 'b': case Ctrl_V: /* block-wise selection */
13205 yank_type = MBLOCK;
13206 if (VIM_ISDIGIT(stropt[1]))
13207 {
13208 ++stropt;
13209 block_len = getdigits(&stropt) - 1;
13210 --stropt;
13211 }
13212 break;
13213#endif
13214 }
13215 }
13216
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013217 strval = get_tv_string_chk(&argvars[1]);
13218 if (strval != NULL)
13219 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013221 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222}
13223
13224
13225/*
13226 * "setwinvar(expr)" function
13227 */
13228/*ARGSUSED*/
13229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013230f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013231 typval_T *argvars;
13232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233{
13234 win_T *win;
13235#ifdef FEAT_WINDOWS
13236 win_T *save_curwin;
13237#endif
13238 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013239 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240 char_u nbuf[NUMBUFLEN];
13241
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013242 rettv->vval.v_number = 0;
13243
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244 if (check_restricted() || check_secure())
13245 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013247 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248 varp = &argvars[2];
13249
13250 if (win != NULL && varname != NULL && varp != NULL)
13251 {
13252#ifdef FEAT_WINDOWS
13253 /* set curwin to be our win, temporarily */
13254 save_curwin = curwin;
13255 curwin = win;
13256 curbuf = curwin->w_buffer;
13257#endif
13258
13259 if (*varname == '&')
13260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013261 long numval;
13262 char_u *strval;
13263 int error = FALSE;
13264
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013266 numval = get_tv_number_chk(varp, &error);
13267 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013268 if (!error && strval != NULL)
13269 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270 }
13271 else
13272 {
13273 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13274 if (winvarname != NULL)
13275 {
13276 STRCPY(winvarname, "w:");
13277 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013278 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279 vim_free(winvarname);
13280 }
13281 }
13282
13283#ifdef FEAT_WINDOWS
13284 /* Restore current window, if it's still valid (autocomands can make
13285 * it invalid). */
13286 if (win_valid(save_curwin))
13287 {
13288 curwin = save_curwin;
13289 curbuf = curwin->w_buffer;
13290 }
13291#endif
13292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293}
13294
13295/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013296 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297 */
13298 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013299f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013300 typval_T *argvars;
13301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013303 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304
Bram Moolenaar0d660222005-01-07 21:51:51 +000013305 p = get_tv_string(&argvars[0]);
13306 rettv->vval.v_string = vim_strsave(p);
13307 simplify_filename(rettv->vval.v_string); /* simplify in place */
13308 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309}
13310
Bram Moolenaar0d660222005-01-07 21:51:51 +000013311static int
13312#ifdef __BORLANDC__
13313 _RTLENTRYF
13314#endif
13315 item_compare __ARGS((const void *s1, const void *s2));
13316static int
13317#ifdef __BORLANDC__
13318 _RTLENTRYF
13319#endif
13320 item_compare2 __ARGS((const void *s1, const void *s2));
13321
13322static int item_compare_ic;
13323static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013324static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013325#define ITEM_COMPARE_FAIL 999
13326
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013328 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013330 static int
13331#ifdef __BORLANDC__
13332_RTLENTRYF
13333#endif
13334item_compare(s1, s2)
13335 const void *s1;
13336 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013338 char_u *p1, *p2;
13339 char_u *tofree1, *tofree2;
13340 int res;
13341 char_u numbuf1[NUMBUFLEN];
13342 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343
Bram Moolenaar33570922005-01-25 22:26:29 +000013344 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13345 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013346 if (item_compare_ic)
13347 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013349 res = STRCMP(p1, p2);
13350 vim_free(tofree1);
13351 vim_free(tofree2);
13352 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353}
13354
13355 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013356#ifdef __BORLANDC__
13357_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013359item_compare2(s1, s2)
13360 const void *s1;
13361 const void *s2;
13362{
13363 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013364 typval_T rettv;
13365 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013366 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013368 /* shortcut after failure in previous call; compare all items equal */
13369 if (item_compare_func_err)
13370 return 0;
13371
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013372 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13373 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013374 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13375 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013376
13377 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13378 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013379 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013380 clear_tv(&argv[0]);
13381 clear_tv(&argv[1]);
13382
13383 if (res == FAIL)
13384 res = ITEM_COMPARE_FAIL;
13385 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013386 /* return value has wrong type */
13387 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13388 if (item_compare_func_err)
13389 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013390 clear_tv(&rettv);
13391 return res;
13392}
13393
13394/*
13395 * "sort({list})" function
13396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013398f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013399 typval_T *argvars;
13400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401{
Bram Moolenaar33570922005-01-25 22:26:29 +000013402 list_T *l;
13403 listitem_T *li;
13404 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013405 long len;
13406 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407
Bram Moolenaar0d660222005-01-07 21:51:51 +000013408 rettv->vval.v_number = 0;
13409 if (argvars[0].v_type != VAR_LIST)
13410 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411 else
13412 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013413 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013414 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013415 return;
13416 rettv->vval.v_list = l;
13417 rettv->v_type = VAR_LIST;
13418 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419
Bram Moolenaar0d660222005-01-07 21:51:51 +000013420 len = list_len(l);
13421 if (len <= 1)
13422 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423
Bram Moolenaar0d660222005-01-07 21:51:51 +000013424 item_compare_ic = FALSE;
13425 item_compare_func = NULL;
13426 if (argvars[1].v_type != VAR_UNKNOWN)
13427 {
13428 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013429 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013430 else
13431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013432 int error = FALSE;
13433
13434 i = get_tv_number_chk(&argvars[1], &error);
13435 if (error)
13436 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013437 if (i == 1)
13438 item_compare_ic = TRUE;
13439 else
13440 item_compare_func = get_tv_string(&argvars[1]);
13441 }
13442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443
Bram Moolenaar0d660222005-01-07 21:51:51 +000013444 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013445 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013446 if (ptrs == NULL)
13447 return;
13448 i = 0;
13449 for (li = l->lv_first; li != NULL; li = li->li_next)
13450 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013452 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013453 /* test the compare function */
13454 if (item_compare_func != NULL
13455 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13456 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013457 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013459 {
13460 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013461 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013462 item_compare_func == NULL ? item_compare : item_compare2);
13463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013464 if (!item_compare_func_err)
13465 {
13466 /* Clear the List and append the items in the sorted order. */
13467 l->lv_first = l->lv_last = NULL;
13468 l->lv_len = 0;
13469 for (i = 0; i < len; ++i)
13470 list_append(l, ptrs[i]);
13471 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013472 }
13473
13474 vim_free(ptrs);
13475 }
13476}
13477
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013478/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013479 * "soundfold({word})" function
13480 */
13481 static void
13482f_soundfold(argvars, rettv)
13483 typval_T *argvars;
13484 typval_T *rettv;
13485{
13486 char_u *s;
13487
13488 rettv->v_type = VAR_STRING;
13489 s = get_tv_string(&argvars[0]);
13490#ifdef FEAT_SYN_HL
13491 rettv->vval.v_string = eval_soundfold(s);
13492#else
13493 rettv->vval.v_string = vim_strsave(s);
13494#endif
13495}
13496
13497/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013498 * "spellbadword()" function
13499 */
13500/* ARGSUSED */
13501 static void
13502f_spellbadword(argvars, rettv)
13503 typval_T *argvars;
13504 typval_T *rettv;
13505{
13506 int attr;
13507 char_u *ptr;
13508 int len;
13509
13510 rettv->vval.v_string = NULL;
13511 rettv->v_type = VAR_STRING;
13512
13513#ifdef FEAT_SYN_HL
13514 /* Find the start of the badly spelled word. */
13515 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
13516 return;
13517
13518 /* Get the length of the word and copy it. */
13519 ptr = ml_get_cursor();
Bram Moolenaar51ac12f2005-07-02 23:21:11 +000013520 len = spell_check(curwin, ptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013521 rettv->vval.v_string = vim_strnsave(ptr, len);
13522#endif
13523}
13524
13525/*
13526 * "spellsuggest()" function
13527 */
13528 static void
13529f_spellsuggest(argvars, rettv)
13530 typval_T *argvars;
13531 typval_T *rettv;
13532{
13533 char_u *str;
13534 int maxcount;
13535 garray_T ga;
13536 list_T *l;
13537 listitem_T *li;
13538 int i;
13539
13540 l = list_alloc();
13541 if (l == NULL)
13542 return;
13543 rettv->v_type = VAR_LIST;
13544 rettv->vval.v_list = l;
13545 ++l->lv_refcount;
13546
13547#ifdef FEAT_SYN_HL
13548 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13549 {
13550 str = get_tv_string(&argvars[0]);
13551 if (argvars[1].v_type != VAR_UNKNOWN)
13552 {
13553 maxcount = get_tv_number(&argvars[1]);
13554 if (maxcount <= 0)
13555 return;
13556 }
13557 else
13558 maxcount = 25;
13559
13560 spell_suggest_list(&ga, str, maxcount);
13561
13562 for (i = 0; i < ga.ga_len; ++i)
13563 {
13564 str = ((char_u **)ga.ga_data)[i];
13565
13566 li = listitem_alloc();
13567 if (li == NULL)
13568 vim_free(str);
13569 else
13570 {
13571 li->li_tv.v_type = VAR_STRING;
13572 li->li_tv.v_lock = 0;
13573 li->li_tv.vval.v_string = str;
13574 list_append(l, li);
13575 }
13576 }
13577 ga_clear(&ga);
13578 }
13579#endif
13580}
13581
Bram Moolenaar0d660222005-01-07 21:51:51 +000013582 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013583f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013584 typval_T *argvars;
13585 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013586{
13587 char_u *str;
13588 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013589 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013590 regmatch_T regmatch;
13591 char_u patbuf[NUMBUFLEN];
13592 char_u *save_cpo;
13593 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013594 listitem_T *ni;
13595 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013596 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013597 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013598 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013599
13600 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13601 save_cpo = p_cpo;
13602 p_cpo = (char_u *)"";
13603
13604 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013605 if (argvars[1].v_type != VAR_UNKNOWN)
13606 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013607 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13608 if (pat == NULL)
13609 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013610 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013611 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013612 }
13613 if (pat == NULL || *pat == NUL)
13614 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013615
13616 l = list_alloc();
13617 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013619 rettv->v_type = VAR_LIST;
13620 rettv->vval.v_list = l;
13621 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013622 if (typeerr)
13623 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624
Bram Moolenaar0d660222005-01-07 21:51:51 +000013625 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13626 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013628 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013629 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013630 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013631 if (*str == NUL)
13632 match = FALSE; /* empty item at the end */
13633 else
13634 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013635 if (match)
13636 end = regmatch.startp[0];
13637 else
13638 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013639 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13640 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013641 {
13642 ni = listitem_alloc();
13643 if (ni == NULL)
13644 break;
13645 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013646 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013647 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13648 list_append(l, ni);
13649 }
13650 if (!match)
13651 break;
13652 /* Advance to just after the match. */
13653 if (regmatch.endp[0] > str)
13654 col = 0;
13655 else
13656 {
13657 /* Don't get stuck at the same match. */
13658#ifdef FEAT_MBYTE
13659 col = mb_ptr2len_check(regmatch.endp[0]);
13660#else
13661 col = 1;
13662#endif
13663 }
13664 str = regmatch.endp[0];
13665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666
Bram Moolenaar0d660222005-01-07 21:51:51 +000013667 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669
Bram Moolenaar0d660222005-01-07 21:51:51 +000013670 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671}
13672
13673#ifdef HAVE_STRFTIME
13674/*
13675 * "strftime({format}[, {time}])" function
13676 */
13677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013678f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013679 typval_T *argvars;
13680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681{
13682 char_u result_buf[256];
13683 struct tm *curtime;
13684 time_t seconds;
13685 char_u *p;
13686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013687 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013689 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013690 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691 seconds = time(NULL);
13692 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013693 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694 curtime = localtime(&seconds);
13695 /* MSVC returns NULL for an invalid value of seconds. */
13696 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013697 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698 else
13699 {
13700# ifdef FEAT_MBYTE
13701 vimconv_T conv;
13702 char_u *enc;
13703
13704 conv.vc_type = CONV_NONE;
13705 enc = enc_locale();
13706 convert_setup(&conv, p_enc, enc);
13707 if (conv.vc_type != CONV_NONE)
13708 p = string_convert(&conv, p, NULL);
13709# endif
13710 if (p != NULL)
13711 (void)strftime((char *)result_buf, sizeof(result_buf),
13712 (char *)p, curtime);
13713 else
13714 result_buf[0] = NUL;
13715
13716# ifdef FEAT_MBYTE
13717 if (conv.vc_type != CONV_NONE)
13718 vim_free(p);
13719 convert_setup(&conv, enc, p_enc);
13720 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013721 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722 else
13723# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013724 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725
13726# ifdef FEAT_MBYTE
13727 /* Release conversion descriptors */
13728 convert_setup(&conv, NULL, NULL);
13729 vim_free(enc);
13730# endif
13731 }
13732}
13733#endif
13734
13735/*
13736 * "stridx()" function
13737 */
13738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013739f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013740 typval_T *argvars;
13741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742{
13743 char_u buf[NUMBUFLEN];
13744 char_u *needle;
13745 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000013746 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000013748 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013750 needle = get_tv_string_chk(&argvars[1]);
13751 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000013752 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013753 if (needle == NULL || haystack == NULL)
13754 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755
Bram Moolenaar33570922005-01-25 22:26:29 +000013756 if (argvars[2].v_type != VAR_UNKNOWN)
13757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013758 int error = FALSE;
13759
13760 start_idx = get_tv_number_chk(&argvars[2], &error);
13761 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000013762 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013763 if (start_idx >= 0)
13764 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000013765 }
13766
13767 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13768 if (pos != NULL)
13769 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770}
13771
13772/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013773 * "string()" function
13774 */
13775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013776f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013777 typval_T *argvars;
13778 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013779{
13780 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013781 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013782
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013783 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013784 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013785 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013786 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787}
13788
13789/*
13790 * "strlen()" function
13791 */
13792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013793f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013794 typval_T *argvars;
13795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013797 rettv->vval.v_number = (varnumber_T)(STRLEN(
13798 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799}
13800
13801/*
13802 * "strpart()" function
13803 */
13804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013805f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013806 typval_T *argvars;
13807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808{
13809 char_u *p;
13810 int n;
13811 int len;
13812 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013813 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013815 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816 slen = (int)STRLEN(p);
13817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013818 n = get_tv_number_chk(&argvars[1], &error);
13819 if (error)
13820 len = 0;
13821 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013822 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 else
13824 len = slen - n; /* default len: all bytes that are available. */
13825
13826 /*
13827 * Only return the overlap between the specified part and the actual
13828 * string.
13829 */
13830 if (n < 0)
13831 {
13832 len += n;
13833 n = 0;
13834 }
13835 else if (n > slen)
13836 n = slen;
13837 if (len < 0)
13838 len = 0;
13839 else if (n + len > slen)
13840 len = slen - n;
13841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013842 rettv->v_type = VAR_STRING;
13843 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844}
13845
13846/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013847 * "strridx()" function
13848 */
13849 static void
13850f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013851 typval_T *argvars;
13852 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013853{
13854 char_u buf[NUMBUFLEN];
13855 char_u *needle;
13856 char_u *haystack;
13857 char_u *rest;
13858 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013859 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013861 needle = get_tv_string_chk(&argvars[1]);
13862 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013863 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013864
13865 rettv->vval.v_number = -1;
13866 if (needle == NULL || haystack == NULL)
13867 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013868 if (argvars[2].v_type != VAR_UNKNOWN)
13869 {
13870 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013871 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000013872 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013873 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013874 }
13875 else
13876 end_idx = haystack_len;
13877
Bram Moolenaar0d660222005-01-07 21:51:51 +000013878 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000013879 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013880 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013881 lastmatch = haystack + end_idx;
13882 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013883 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000013884 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013885 for (rest = haystack; *rest != '\0'; ++rest)
13886 {
13887 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013888 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013889 break;
13890 lastmatch = rest;
13891 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000013892 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013893
13894 if (lastmatch == NULL)
13895 rettv->vval.v_number = -1;
13896 else
13897 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13898}
13899
13900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 * "strtrans()" function
13902 */
13903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013904f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013905 typval_T *argvars;
13906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013908 rettv->v_type = VAR_STRING;
13909 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910}
13911
13912/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013913 * "submatch()" function
13914 */
13915 static void
13916f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013917 typval_T *argvars;
13918 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013919{
13920 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013921 rettv->vval.v_string =
13922 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013923}
13924
13925/*
13926 * "substitute()" function
13927 */
13928 static void
13929f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013930 typval_T *argvars;
13931 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013932{
13933 char_u patbuf[NUMBUFLEN];
13934 char_u subbuf[NUMBUFLEN];
13935 char_u flagsbuf[NUMBUFLEN];
13936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013937 char_u *str = get_tv_string_chk(&argvars[0]);
13938 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13939 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
13940 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
13941
Bram Moolenaar0d660222005-01-07 21:51:51 +000013942 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013943 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
13944 rettv->vval.v_string = NULL;
13945 else
13946 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013947}
13948
13949/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013950 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 */
13952/*ARGSUSED*/
13953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013954f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013955 typval_T *argvars;
13956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957{
13958 int id = 0;
13959#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013960 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961 long col;
13962 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000013963 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013965 lnum = get_tv_lnum(argvars); /* -1 on type error */
13966 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
13967 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013969 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013970 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13971 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972#endif
13973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013974 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975}
13976
13977/*
13978 * "synIDattr(id, what [, mode])" function
13979 */
13980/*ARGSUSED*/
13981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013982f_synIDattr(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 *p = NULL;
13987#ifdef FEAT_SYN_HL
13988 int id;
13989 char_u *what;
13990 char_u *mode;
13991 char_u modebuf[NUMBUFLEN];
13992 int modec;
13993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013994 id = get_tv_number(&argvars[0]);
13995 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013996 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013998 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999 modec = TOLOWER_ASC(mode[0]);
14000 if (modec != 't' && modec != 'c'
14001#ifdef FEAT_GUI
14002 && modec != 'g'
14003#endif
14004 )
14005 modec = 0; /* replace invalid with current */
14006 }
14007 else
14008 {
14009#ifdef FEAT_GUI
14010 if (gui.in_use)
14011 modec = 'g';
14012 else
14013#endif
14014 if (t_colors > 1)
14015 modec = 'c';
14016 else
14017 modec = 't';
14018 }
14019
14020
14021 switch (TOLOWER_ASC(what[0]))
14022 {
14023 case 'b':
14024 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14025 p = highlight_color(id, what, modec);
14026 else /* bold */
14027 p = highlight_has_attr(id, HL_BOLD, modec);
14028 break;
14029
14030 case 'f': /* fg[#] */
14031 p = highlight_color(id, what, modec);
14032 break;
14033
14034 case 'i':
14035 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14036 p = highlight_has_attr(id, HL_INVERSE, modec);
14037 else /* italic */
14038 p = highlight_has_attr(id, HL_ITALIC, modec);
14039 break;
14040
14041 case 'n': /* name */
14042 p = get_highlight_name(NULL, id - 1);
14043 break;
14044
14045 case 'r': /* reverse */
14046 p = highlight_has_attr(id, HL_INVERSE, modec);
14047 break;
14048
14049 case 's': /* standout */
14050 p = highlight_has_attr(id, HL_STANDOUT, modec);
14051 break;
14052
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014053 case 'u':
14054 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14055 /* underline */
14056 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14057 else
14058 /* undercurl */
14059 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060 break;
14061 }
14062
14063 if (p != NULL)
14064 p = vim_strsave(p);
14065#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014066 rettv->v_type = VAR_STRING;
14067 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068}
14069
14070/*
14071 * "synIDtrans(id)" function
14072 */
14073/*ARGSUSED*/
14074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014075f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014076 typval_T *argvars;
14077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078{
14079 int id;
14080
14081#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014082 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083
14084 if (id > 0)
14085 id = syn_get_final_id(id);
14086 else
14087#endif
14088 id = 0;
14089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014090 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091}
14092
14093/*
14094 * "system()" function
14095 */
14096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014097f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014098 typval_T *argvars;
14099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014101 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014103 char_u *infile = NULL;
14104 char_u buf[NUMBUFLEN];
14105 int err = FALSE;
14106 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014108 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014109 {
14110 /*
14111 * Write the string to a temp file, to be used for input of the shell
14112 * command.
14113 */
14114 if ((infile = vim_tempname('i')) == NULL)
14115 {
14116 EMSG(_(e_notmp));
14117 return;
14118 }
14119
14120 fd = mch_fopen((char *)infile, WRITEBIN);
14121 if (fd == NULL)
14122 {
14123 EMSG2(_(e_notopen), infile);
14124 goto done;
14125 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014126 p = get_tv_string_buf_chk(&argvars[1], buf);
14127 if (p == NULL)
14128 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014129 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14130 err = TRUE;
14131 if (fclose(fd) != 0)
14132 err = TRUE;
14133 if (err)
14134 {
14135 EMSG(_("E677: Error writing temp file"));
14136 goto done;
14137 }
14138 }
14139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014140 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014141
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142#ifdef USE_CR
14143 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014144 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145 {
14146 char_u *s;
14147
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014148 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 {
14150 if (*s == CAR)
14151 *s = NL;
14152 }
14153 }
14154#else
14155# ifdef USE_CRNL
14156 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014157 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 {
14159 char_u *s, *d;
14160
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014161 d = res;
14162 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163 {
14164 if (s[0] == CAR && s[1] == NL)
14165 ++s;
14166 *d++ = *s;
14167 }
14168 *d = NUL;
14169 }
14170# endif
14171#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014172
14173done:
14174 if (infile != NULL)
14175 {
14176 mch_remove(infile);
14177 vim_free(infile);
14178 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014179 rettv->v_type = VAR_STRING;
14180 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181}
14182
14183/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014184 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014185 */
14186 static void
14187f_taglist(argvars, rettv)
14188 typval_T *argvars;
14189 typval_T *rettv;
14190{
14191 char_u *tag_pattern;
14192 list_T *l;
14193
14194 tag_pattern = get_tv_string(&argvars[0]);
14195
14196 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014197 if (*tag_pattern == NUL)
14198 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014199
14200 l = list_alloc();
14201 if (l != NULL)
14202 {
14203 if (get_tags(l, tag_pattern) != FAIL)
14204 {
14205 rettv->vval.v_list = l;
14206 rettv->v_type = VAR_LIST;
14207 ++l->lv_refcount;
14208 }
14209 else
14210 list_free(l);
14211 }
14212}
14213
14214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215 * "tempname()" function
14216 */
14217/*ARGSUSED*/
14218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014219f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014220 typval_T *argvars;
14221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222{
14223 static int x = 'A';
14224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014225 rettv->v_type = VAR_STRING;
14226 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227
14228 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14229 * names. Skip 'I' and 'O', they are used for shell redirection. */
14230 do
14231 {
14232 if (x == 'Z')
14233 x = '0';
14234 else if (x == '9')
14235 x = 'A';
14236 else
14237 {
14238#ifdef EBCDIC
14239 if (x == 'I')
14240 x = 'J';
14241 else if (x == 'R')
14242 x = 'S';
14243 else
14244#endif
14245 ++x;
14246 }
14247 } while (x == 'I' || x == 'O');
14248}
14249
14250/*
14251 * "tolower(string)" function
14252 */
14253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014254f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014255 typval_T *argvars;
14256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257{
14258 char_u *p;
14259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014260 p = vim_strsave(get_tv_string(&argvars[0]));
14261 rettv->v_type = VAR_STRING;
14262 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263
14264 if (p != NULL)
14265 while (*p != NUL)
14266 {
14267#ifdef FEAT_MBYTE
14268 int l;
14269
14270 if (enc_utf8)
14271 {
14272 int c, lc;
14273
14274 c = utf_ptr2char(p);
14275 lc = utf_tolower(c);
14276 l = utf_ptr2len_check(p);
14277 /* TODO: reallocate string when byte count changes. */
14278 if (utf_char2len(lc) == l)
14279 utf_char2bytes(lc, p);
14280 p += l;
14281 }
14282 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
14283 p += l; /* skip multi-byte character */
14284 else
14285#endif
14286 {
14287 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14288 ++p;
14289 }
14290 }
14291}
14292
14293/*
14294 * "toupper(string)" function
14295 */
14296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014297f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014298 typval_T *argvars;
14299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014301 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014302 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303}
14304
14305/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014306 * "tr(string, fromstr, tostr)" function
14307 */
14308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014309f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014310 typval_T *argvars;
14311 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014312{
14313 char_u *instr;
14314 char_u *fromstr;
14315 char_u *tostr;
14316 char_u *p;
14317#ifdef FEAT_MBYTE
14318 int inlen;
14319 int fromlen;
14320 int tolen;
14321 int idx;
14322 char_u *cpstr;
14323 int cplen;
14324 int first = TRUE;
14325#endif
14326 char_u buf[NUMBUFLEN];
14327 char_u buf2[NUMBUFLEN];
14328 garray_T ga;
14329
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014330 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014331 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14332 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014333
14334 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014335 rettv->v_type = VAR_STRING;
14336 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014337 if (fromstr == NULL || tostr == NULL)
14338 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014339 ga_init2(&ga, (int)sizeof(char), 80);
14340
14341#ifdef FEAT_MBYTE
14342 if (!has_mbyte)
14343#endif
14344 /* not multi-byte: fromstr and tostr must be the same length */
14345 if (STRLEN(fromstr) != STRLEN(tostr))
14346 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014347#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014348error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014349#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014350 EMSG2(_(e_invarg2), fromstr);
14351 ga_clear(&ga);
14352 return;
14353 }
14354
14355 /* fromstr and tostr have to contain the same number of chars */
14356 while (*instr != NUL)
14357 {
14358#ifdef FEAT_MBYTE
14359 if (has_mbyte)
14360 {
14361 inlen = mb_ptr2len_check(instr);
14362 cpstr = instr;
14363 cplen = inlen;
14364 idx = 0;
14365 for (p = fromstr; *p != NUL; p += fromlen)
14366 {
14367 fromlen = mb_ptr2len_check(p);
14368 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14369 {
14370 for (p = tostr; *p != NUL; p += tolen)
14371 {
14372 tolen = mb_ptr2len_check(p);
14373 if (idx-- == 0)
14374 {
14375 cplen = tolen;
14376 cpstr = p;
14377 break;
14378 }
14379 }
14380 if (*p == NUL) /* tostr is shorter than fromstr */
14381 goto error;
14382 break;
14383 }
14384 ++idx;
14385 }
14386
14387 if (first && cpstr == instr)
14388 {
14389 /* Check that fromstr and tostr have the same number of
14390 * (multi-byte) characters. Done only once when a character
14391 * of instr doesn't appear in fromstr. */
14392 first = FALSE;
14393 for (p = tostr; *p != NUL; p += tolen)
14394 {
14395 tolen = mb_ptr2len_check(p);
14396 --idx;
14397 }
14398 if (idx != 0)
14399 goto error;
14400 }
14401
14402 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014403 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014404 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014405
14406 instr += inlen;
14407 }
14408 else
14409#endif
14410 {
14411 /* When not using multi-byte chars we can do it faster. */
14412 p = vim_strchr(fromstr, *instr);
14413 if (p != NULL)
14414 ga_append(&ga, tostr[p - fromstr]);
14415 else
14416 ga_append(&ga, *instr);
14417 ++instr;
14418 }
14419 }
14420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014421 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014422}
14423
14424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425 * "type(expr)" function
14426 */
14427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014428f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014429 typval_T *argvars;
14430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014432 int n;
14433
14434 switch (argvars[0].v_type)
14435 {
14436 case VAR_NUMBER: n = 0; break;
14437 case VAR_STRING: n = 1; break;
14438 case VAR_FUNC: n = 2; break;
14439 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014440 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014441 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14442 }
14443 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444}
14445
14446/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014447 * "values(dict)" function
14448 */
14449 static void
14450f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014451 typval_T *argvars;
14452 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014453{
14454 dict_list(argvars, rettv, 1);
14455}
14456
14457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458 * "virtcol(string)" function
14459 */
14460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014462 typval_T *argvars;
14463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464{
14465 colnr_T vcol = 0;
14466 pos_T *fp;
14467
14468 fp = var2fpos(&argvars[0], FALSE);
14469 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14470 {
14471 getvvcol(curwin, fp, NULL, NULL, &vcol);
14472 ++vcol;
14473 }
14474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014475 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476}
14477
14478/*
14479 * "visualmode()" function
14480 */
14481/*ARGSUSED*/
14482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014483f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014484 typval_T *argvars;
14485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486{
14487#ifdef FEAT_VISUAL
14488 char_u str[2];
14489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014490 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491 str[0] = curbuf->b_visual_mode_eval;
14492 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014493 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494
14495 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014496 if ((argvars[0].v_type == VAR_NUMBER
14497 && argvars[0].vval.v_number != 0)
14498 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014499 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500 curbuf->b_visual_mode_eval = NUL;
14501#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014502 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503#endif
14504}
14505
14506/*
14507 * "winbufnr(nr)" function
14508 */
14509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014510f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014511 typval_T *argvars;
14512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513{
14514 win_T *wp;
14515
14516 wp = find_win_by_nr(&argvars[0]);
14517 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014518 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014520 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521}
14522
14523/*
14524 * "wincol()" function
14525 */
14526/*ARGSUSED*/
14527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014528f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014529 typval_T *argvars;
14530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531{
14532 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014533 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534}
14535
14536/*
14537 * "winheight(nr)" function
14538 */
14539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014540f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014541 typval_T *argvars;
14542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543{
14544 win_T *wp;
14545
14546 wp = find_win_by_nr(&argvars[0]);
14547 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014548 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014550 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551}
14552
14553/*
14554 * "winline()" function
14555 */
14556/*ARGSUSED*/
14557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014558f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014559 typval_T *argvars;
14560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561{
14562 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014563 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564}
14565
14566/*
14567 * "winnr()" function
14568 */
14569/* ARGSUSED */
14570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014571f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014572 typval_T *argvars;
14573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574{
14575 int nr = 1;
14576#ifdef FEAT_WINDOWS
14577 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014578 win_T *twin = curwin;
14579 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014581 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014583 arg = get_tv_string_chk(&argvars[0]);
14584 if (arg == NULL)
14585 nr = 0; /* type error; errmsg already given */
14586 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014587 twin = lastwin;
14588 else if (STRCMP(arg, "#") == 0)
14589 {
14590 twin = prevwin;
14591 if (prevwin == NULL)
14592 nr = 0;
14593 }
14594 else
14595 {
14596 EMSG2(_(e_invexpr2), arg);
14597 nr = 0;
14598 }
14599 }
14600
14601 if (nr > 0)
14602 for (wp = firstwin; wp != twin; wp = wp->w_next)
14603 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014605 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606}
14607
14608/*
14609 * "winrestcmd()" function
14610 */
14611/* ARGSUSED */
14612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014613f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014614 typval_T *argvars;
14615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616{
14617#ifdef FEAT_WINDOWS
14618 win_T *wp;
14619 int winnr = 1;
14620 garray_T ga;
14621 char_u buf[50];
14622
14623 ga_init2(&ga, (int)sizeof(char), 70);
14624 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14625 {
14626 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14627 ga_concat(&ga, buf);
14628# ifdef FEAT_VERTSPLIT
14629 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14630 ga_concat(&ga, buf);
14631# endif
14632 ++winnr;
14633 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014634 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014636 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014638 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014640 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641}
14642
14643/*
14644 * "winwidth(nr)" function
14645 */
14646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014647f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014648 typval_T *argvars;
14649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650{
14651 win_T *wp;
14652
14653 wp = find_win_by_nr(&argvars[0]);
14654 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014655 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656 else
14657#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014658 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014660 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661#endif
14662}
14663
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014665 * "writefile()" function
14666 */
14667 static void
14668f_writefile(argvars, rettv)
14669 typval_T *argvars;
14670 typval_T *rettv;
14671{
14672 int binary = FALSE;
14673 char_u *fname;
14674 FILE *fd;
14675 listitem_T *li;
14676 char_u *s;
14677 int ret = 0;
14678 int c;
14679
14680 if (argvars[0].v_type != VAR_LIST)
14681 {
14682 EMSG2(_(e_listarg), "writefile()");
14683 return;
14684 }
14685 if (argvars[0].vval.v_list == NULL)
14686 return;
14687
14688 if (argvars[2].v_type != VAR_UNKNOWN
14689 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
14690 binary = TRUE;
14691
14692 /* Always open the file in binary mode, library functions have a mind of
14693 * their own about CR-LF conversion. */
14694 fname = get_tv_string(&argvars[1]);
14695 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
14696 {
14697 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14698 ret = -1;
14699 }
14700 else
14701 {
14702 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
14703 li = li->li_next)
14704 {
14705 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
14706 {
14707 if (*s == '\n')
14708 c = putc(NUL, fd);
14709 else
14710 c = putc(*s, fd);
14711 if (c == EOF)
14712 {
14713 ret = -1;
14714 break;
14715 }
14716 }
14717 if (!binary || li->li_next != NULL)
14718 if (putc('\n', fd) == EOF)
14719 {
14720 ret = -1;
14721 break;
14722 }
14723 if (ret < 0)
14724 {
14725 EMSG(_(e_write));
14726 break;
14727 }
14728 }
14729 fclose(fd);
14730 }
14731
14732 rettv->vval.v_number = ret;
14733}
14734
14735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 * Translate a String variable into a position.
14737 */
14738 static pos_T *
14739var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000014740 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741 int lnum; /* TRUE when $ is last line */
14742{
14743 char_u *name;
14744 static pos_T pos;
14745 pos_T *pp;
14746
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014747 name = get_tv_string_chk(varp);
14748 if (name == NULL)
14749 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 if (name[0] == '.') /* cursor */
14751 return &curwin->w_cursor;
14752 if (name[0] == '\'') /* mark */
14753 {
14754 pp = getmark(name[1], FALSE);
14755 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
14756 return NULL;
14757 return pp;
14758 }
14759 if (name[0] == '$') /* last column or line */
14760 {
14761 if (lnum)
14762 {
14763 pos.lnum = curbuf->b_ml.ml_line_count;
14764 pos.col = 0;
14765 }
14766 else
14767 {
14768 pos.lnum = curwin->w_cursor.lnum;
14769 pos.col = (colnr_T)STRLEN(ml_get_curline());
14770 }
14771 return &pos;
14772 }
14773 return NULL;
14774}
14775
14776/*
14777 * Get the length of an environment variable name.
14778 * Advance "arg" to the first character after the name.
14779 * Return 0 for error.
14780 */
14781 static int
14782get_env_len(arg)
14783 char_u **arg;
14784{
14785 char_u *p;
14786 int len;
14787
14788 for (p = *arg; vim_isIDc(*p); ++p)
14789 ;
14790 if (p == *arg) /* no name found */
14791 return 0;
14792
14793 len = (int)(p - *arg);
14794 *arg = p;
14795 return len;
14796}
14797
14798/*
14799 * Get the length of the name of a function or internal variable.
14800 * "arg" is advanced to the first non-white character after the name.
14801 * Return 0 if something is wrong.
14802 */
14803 static int
14804get_id_len(arg)
14805 char_u **arg;
14806{
14807 char_u *p;
14808 int len;
14809
14810 /* Find the end of the name. */
14811 for (p = *arg; eval_isnamec(*p); ++p)
14812 ;
14813 if (p == *arg) /* no name found */
14814 return 0;
14815
14816 len = (int)(p - *arg);
14817 *arg = skipwhite(p);
14818
14819 return len;
14820}
14821
14822/*
Bram Moolenaara7043832005-01-21 11:56:39 +000014823 * Get the length of the name of a variable or function.
14824 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014826 * Return -1 if curly braces expansion failed.
14827 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828 * If the name contains 'magic' {}'s, expand them and return the
14829 * expanded name in an allocated string via 'alias' - caller must free.
14830 */
14831 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014832get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833 char_u **arg;
14834 char_u **alias;
14835 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014836 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837{
14838 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839 char_u *p;
14840 char_u *expr_start;
14841 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842
14843 *alias = NULL; /* default to no alias */
14844
14845 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
14846 && (*arg)[2] == (int)KE_SNR)
14847 {
14848 /* hard coded <SNR>, already translated */
14849 *arg += 3;
14850 return get_id_len(arg) + 3;
14851 }
14852 len = eval_fname_script(*arg);
14853 if (len > 0)
14854 {
14855 /* literal "<SID>", "s:" or "<SNR>" */
14856 *arg += len;
14857 }
14858
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014860 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014861 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014862 p = find_name_end(*arg, &expr_start, &expr_end,
14863 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864 if (expr_start != NULL)
14865 {
14866 char_u *temp_string;
14867
14868 if (!evaluate)
14869 {
14870 len += (int)(p - *arg);
14871 *arg = skipwhite(p);
14872 return len;
14873 }
14874
14875 /*
14876 * Include any <SID> etc in the expanded string:
14877 * Thus the -len here.
14878 */
14879 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
14880 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014881 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882 *alias = temp_string;
14883 *arg = skipwhite(p);
14884 return (int)STRLEN(temp_string);
14885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014886
14887 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014888 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014889 EMSG2(_(e_invexpr2), *arg);
14890
14891 return len;
14892}
14893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014894/*
14895 * Find the end of a variable or function name, taking care of magic braces.
14896 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
14897 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014898 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014899 * Return a pointer to just after the name. Equal to "arg" if there is no
14900 * valid name.
14901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014902 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014903find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904 char_u *arg;
14905 char_u **expr_start;
14906 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014907 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014909 int mb_nest = 0;
14910 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911 char_u *p;
14912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014913 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014915 *expr_start = NULL;
14916 *expr_end = NULL;
14917 }
14918
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014919 /* Quick check for valid starting character. */
14920 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
14921 return arg;
14922
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014923 for (p = arg; *p != NUL
14924 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014925 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014926 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014927 || mb_nest != 0
14928 || br_nest != 0); ++p)
14929 {
14930 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014932 if (*p == '[')
14933 ++br_nest;
14934 else if (*p == ']')
14935 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014937 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014939 if (*p == '{')
14940 {
14941 mb_nest++;
14942 if (expr_start != NULL && *expr_start == NULL)
14943 *expr_start = p;
14944 }
14945 else if (*p == '}')
14946 {
14947 mb_nest--;
14948 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
14949 *expr_end = p;
14950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014952 }
14953
14954 return p;
14955}
14956
14957/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014958 * Expands out the 'magic' {}'s in a variable/function name.
14959 * Note that this can call itself recursively, to deal with
14960 * constructs like foo{bar}{baz}{bam}
14961 * The four pointer arguments point to "foo{expre}ss{ion}bar"
14962 * "in_start" ^
14963 * "expr_start" ^
14964 * "expr_end" ^
14965 * "in_end" ^
14966 *
14967 * Returns a new allocated string, which the caller must free.
14968 * Returns NULL for failure.
14969 */
14970 static char_u *
14971make_expanded_name(in_start, expr_start, expr_end, in_end)
14972 char_u *in_start;
14973 char_u *expr_start;
14974 char_u *expr_end;
14975 char_u *in_end;
14976{
14977 char_u c1;
14978 char_u *retval = NULL;
14979 char_u *temp_result;
14980 char_u *nextcmd = NULL;
14981
14982 if (expr_end == NULL || in_end == NULL)
14983 return NULL;
14984 *expr_start = NUL;
14985 *expr_end = NUL;
14986 c1 = *in_end;
14987 *in_end = NUL;
14988
14989 temp_result = eval_to_string(expr_start + 1, &nextcmd);
14990 if (temp_result != NULL && nextcmd == NULL)
14991 {
14992 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
14993 + (in_end - expr_end) + 1));
14994 if (retval != NULL)
14995 {
14996 STRCPY(retval, in_start);
14997 STRCAT(retval, temp_result);
14998 STRCAT(retval, expr_end + 1);
14999 }
15000 }
15001 vim_free(temp_result);
15002
15003 *in_end = c1; /* put char back for error messages */
15004 *expr_start = '{';
15005 *expr_end = '}';
15006
15007 if (retval != NULL)
15008 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015009 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015010 if (expr_start != NULL)
15011 {
15012 /* Further expansion! */
15013 temp_result = make_expanded_name(retval, expr_start,
15014 expr_end, temp_result);
15015 vim_free(retval);
15016 retval = temp_result;
15017 }
15018 }
15019
15020 return retval;
15021}
15022
15023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015025 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026 */
15027 static int
15028eval_isnamec(c)
15029 int c;
15030{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015031 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15032}
15033
15034/*
15035 * Return TRUE if character "c" can be used as the first character in a
15036 * variable or function name (excluding '{' and '}').
15037 */
15038 static int
15039eval_isnamec1(c)
15040 int c;
15041{
15042 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043}
15044
15045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015046 * Set number v: variable to "val".
15047 */
15048 void
15049set_vim_var_nr(idx, val)
15050 int idx;
15051 long val;
15052{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015053 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054}
15055
15056/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015057 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058 */
15059 long
15060get_vim_var_nr(idx)
15061 int idx;
15062{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015063 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064}
15065
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015066#if defined(FEAT_AUTOCMD) || defined(PROTO)
15067/*
15068 * Get string v: variable value. Uses a static buffer, can only be used once.
15069 */
15070 char_u *
15071get_vim_var_str(idx)
15072 int idx;
15073{
15074 return get_tv_string(&vimvars[idx].vv_tv);
15075}
15076#endif
15077
Bram Moolenaar071d4272004-06-13 20:20:40 +000015078/*
15079 * Set v:count, v:count1 and v:prevcount.
15080 */
15081 void
15082set_vcount(count, count1)
15083 long count;
15084 long count1;
15085{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015086 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15087 vimvars[VV_COUNT].vv_nr = count;
15088 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089}
15090
15091/*
15092 * Set string v: variable to a copy of "val".
15093 */
15094 void
15095set_vim_var_string(idx, val, len)
15096 int idx;
15097 char_u *val;
15098 int len; /* length of "val" to use or -1 (whole string) */
15099{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015100 /* Need to do this (at least) once, since we can't initialize a union.
15101 * Will always be invoked when "v:progname" is set. */
15102 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15103
Bram Moolenaare9a41262005-01-15 22:18:47 +000015104 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015105 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015106 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015108 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015110 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111}
15112
15113/*
15114 * Set v:register if needed.
15115 */
15116 void
15117set_reg_var(c)
15118 int c;
15119{
15120 char_u regname;
15121
15122 if (c == 0 || c == ' ')
15123 regname = '"';
15124 else
15125 regname = c;
15126 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015127 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128 set_vim_var_string(VV_REG, &regname, 1);
15129}
15130
15131/*
15132 * Get or set v:exception. If "oldval" == NULL, return the current value.
15133 * Otherwise, restore the value to "oldval" and return NULL.
15134 * Must always be called in pairs to save and restore v:exception! Does not
15135 * take care of memory allocations.
15136 */
15137 char_u *
15138v_exception(oldval)
15139 char_u *oldval;
15140{
15141 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015142 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143
Bram Moolenaare9a41262005-01-15 22:18:47 +000015144 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145 return NULL;
15146}
15147
15148/*
15149 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15150 * Otherwise, restore the value to "oldval" and return NULL.
15151 * Must always be called in pairs to save and restore v:throwpoint! Does not
15152 * take care of memory allocations.
15153 */
15154 char_u *
15155v_throwpoint(oldval)
15156 char_u *oldval;
15157{
15158 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015159 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160
Bram Moolenaare9a41262005-01-15 22:18:47 +000015161 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162 return NULL;
15163}
15164
15165#if defined(FEAT_AUTOCMD) || defined(PROTO)
15166/*
15167 * Set v:cmdarg.
15168 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15169 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15170 * Must always be called in pairs!
15171 */
15172 char_u *
15173set_cmdarg(eap, oldarg)
15174 exarg_T *eap;
15175 char_u *oldarg;
15176{
15177 char_u *oldval;
15178 char_u *newval;
15179 unsigned len;
15180
Bram Moolenaare9a41262005-01-15 22:18:47 +000015181 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015182 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015183 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015184 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015185 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015186 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187 }
15188
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015189 if (eap->force_bin == FORCE_BIN)
15190 len = 6;
15191 else if (eap->force_bin == FORCE_NOBIN)
15192 len = 8;
15193 else
15194 len = 0;
15195 if (eap->force_ff != 0)
15196 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15197# ifdef FEAT_MBYTE
15198 if (eap->force_enc != 0)
15199 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15200# endif
15201
15202 newval = alloc(len + 1);
15203 if (newval == NULL)
15204 return NULL;
15205
15206 if (eap->force_bin == FORCE_BIN)
15207 sprintf((char *)newval, " ++bin");
15208 else if (eap->force_bin == FORCE_NOBIN)
15209 sprintf((char *)newval, " ++nobin");
15210 else
15211 *newval = NUL;
15212 if (eap->force_ff != 0)
15213 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15214 eap->cmd + eap->force_ff);
15215# ifdef FEAT_MBYTE
15216 if (eap->force_enc != 0)
15217 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15218 eap->cmd + eap->force_enc);
15219# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015220 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015221 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015222}
15223#endif
15224
15225/*
15226 * Get the value of internal variable "name".
15227 * Return OK or FAIL.
15228 */
15229 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015230get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015231 char_u *name;
15232 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015233 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015234 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235{
15236 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015237 typval_T *tv = NULL;
15238 typval_T atv;
15239 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241
15242 /* truncate the name, so that we can use strcmp() */
15243 cc = name[len];
15244 name[len] = NUL;
15245
15246 /*
15247 * Check for "b:changedtick".
15248 */
15249 if (STRCMP(name, "b:changedtick") == 0)
15250 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015251 atv.v_type = VAR_NUMBER;
15252 atv.vval.v_number = curbuf->b_changedtick;
15253 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015254 }
15255
15256 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257 * Check for user-defined variables.
15258 */
15259 else
15260 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015261 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015263 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 }
15265
Bram Moolenaare9a41262005-01-15 22:18:47 +000015266 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015268 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015269 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270 ret = FAIL;
15271 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015272 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015273 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274
15275 name[len] = cc;
15276
15277 return ret;
15278}
15279
15280/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015281 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15282 * Also handle function call with Funcref variable: func(expr)
15283 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15284 */
15285 static int
15286handle_subscript(arg, rettv, evaluate, verbose)
15287 char_u **arg;
15288 typval_T *rettv;
15289 int evaluate; /* do more than finding the end */
15290 int verbose; /* give error messages */
15291{
15292 int ret = OK;
15293 dict_T *selfdict = NULL;
15294 char_u *s;
15295 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015296 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015297
15298 while (ret == OK
15299 && (**arg == '['
15300 || (**arg == '.' && rettv->v_type == VAR_DICT)
15301 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15302 && !vim_iswhite(*(*arg - 1)))
15303 {
15304 if (**arg == '(')
15305 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015306 /* need to copy the funcref so that we can clear rettv */
15307 functv = *rettv;
15308 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015309
15310 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015311 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015312 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015313 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15314 &len, evaluate, selfdict);
15315
15316 /* Clear the funcref afterwards, so that deleting it while
15317 * evaluating the arguments is possible (see test55). */
15318 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015319
15320 /* Stop the expression evaluation when immediately aborting on
15321 * error, or when an interrupt occurred or an exception was thrown
15322 * but not caught. */
15323 if (aborting())
15324 {
15325 if (ret == OK)
15326 clear_tv(rettv);
15327 ret = FAIL;
15328 }
15329 dict_unref(selfdict);
15330 selfdict = NULL;
15331 }
15332 else /* **arg == '[' || **arg == '.' */
15333 {
15334 dict_unref(selfdict);
15335 if (rettv->v_type == VAR_DICT)
15336 {
15337 selfdict = rettv->vval.v_dict;
15338 if (selfdict != NULL)
15339 ++selfdict->dv_refcount;
15340 }
15341 else
15342 selfdict = NULL;
15343 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15344 {
15345 clear_tv(rettv);
15346 ret = FAIL;
15347 }
15348 }
15349 }
15350 dict_unref(selfdict);
15351 return ret;
15352}
15353
15354/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015355 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15356 * value).
15357 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015358 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015359alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015360{
Bram Moolenaar33570922005-01-25 22:26:29 +000015361 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015362}
15363
15364/*
15365 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366 * The string "s" must have been allocated, it is consumed.
15367 * Return NULL for out of memory, the variable otherwise.
15368 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015369 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015370alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371 char_u *s;
15372{
Bram Moolenaar33570922005-01-25 22:26:29 +000015373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015375 rettv = alloc_tv();
15376 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015378 rettv->v_type = VAR_STRING;
15379 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 }
15381 else
15382 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015383 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384}
15385
15386/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015387 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388 */
15389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015390free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015391 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392{
15393 if (varp != NULL)
15394 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015395 switch (varp->v_type)
15396 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015397 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015398 func_unref(varp->vval.v_string);
15399 /*FALLTHROUGH*/
15400 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015401 vim_free(varp->vval.v_string);
15402 break;
15403 case VAR_LIST:
15404 list_unref(varp->vval.v_list);
15405 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015406 case VAR_DICT:
15407 dict_unref(varp->vval.v_dict);
15408 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015409 case VAR_NUMBER:
15410 case VAR_UNKNOWN:
15411 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015412 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015413 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015414 break;
15415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416 vim_free(varp);
15417 }
15418}
15419
15420/*
15421 * Free the memory for a variable value and set the value to NULL or 0.
15422 */
15423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015424clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015425 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426{
15427 if (varp != NULL)
15428 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015429 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015431 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015432 func_unref(varp->vval.v_string);
15433 /*FALLTHROUGH*/
15434 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015435 vim_free(varp->vval.v_string);
15436 varp->vval.v_string = NULL;
15437 break;
15438 case VAR_LIST:
15439 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015440 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015441 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015442 case VAR_DICT:
15443 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015444 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015445 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015446 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015447 varp->vval.v_number = 0;
15448 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015449 case VAR_UNKNOWN:
15450 break;
15451 default:
15452 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015454 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455 }
15456}
15457
15458/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015459 * Set the value of a variable to NULL without freeing items.
15460 */
15461 static void
15462init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015463 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015464{
15465 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015466 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015467}
15468
15469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015470 * Get the number value of a variable.
15471 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015472 * For incompatible types, return 0.
15473 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15474 * caller of incompatible types: it sets *denote to TRUE if "denote"
15475 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476 */
15477 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015478get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015479 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015481 int error = FALSE;
15482
15483 return get_tv_number_chk(varp, &error); /* return 0L on error */
15484}
15485
15486 static long
15487get_tv_number_chk(varp, denote)
15488 typval_T *varp;
15489 int *denote;
15490{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015491 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015492
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015493 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015495 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015496 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015497 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015498 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015499 break;
15500 case VAR_STRING:
15501 if (varp->vval.v_string != NULL)
15502 vim_str2nr(varp->vval.v_string, NULL, NULL,
15503 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015504 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015505 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015506 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015507 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015508 case VAR_DICT:
15509 EMSG(_("E728: Using a Dictionary as a number"));
15510 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015511 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015512 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015513 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015515 if (denote == NULL) /* useful for values that must be unsigned */
15516 n = -1;
15517 else
15518 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015519 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015520}
15521
15522/*
15523 * Get the lnum from the first argument. Also accepts ".", "$", etc.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015524 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 */
15526 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015527get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015528 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529{
Bram Moolenaar33570922005-01-25 22:26:29 +000015530 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531 linenr_T lnum;
15532
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015533 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 if (lnum == 0) /* no valid number, try using line() */
15535 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015536 rettv.v_type = VAR_NUMBER;
15537 f_line(argvars, &rettv);
15538 lnum = rettv.vval.v_number;
15539 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 }
15541 return lnum;
15542}
15543
15544/*
15545 * Get the string value of a variable.
15546 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015547 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15548 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 * If the String variable has never been set, return an empty string.
15550 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015551 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15552 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 */
15554 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015555get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015556 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557{
15558 static char_u mybuf[NUMBUFLEN];
15559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015560 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561}
15562
15563 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015564get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015565 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015566 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015568 char_u *res = get_tv_string_buf_chk(varp, buf);
15569
15570 return res != NULL ? res : (char_u *)"";
15571}
15572
15573 static char_u *
15574get_tv_string_chk(varp)
15575 typval_T *varp;
15576{
15577 static char_u mybuf[NUMBUFLEN];
15578
15579 return get_tv_string_buf_chk(varp, mybuf);
15580}
15581
15582 static char_u *
15583get_tv_string_buf_chk(varp, buf)
15584 typval_T *varp;
15585 char_u *buf;
15586{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015587 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015589 case VAR_NUMBER:
15590 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15591 return buf;
15592 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015593 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015594 break;
15595 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015596 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015597 break;
15598 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015599 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015600 break;
15601 case VAR_STRING:
15602 if (varp->vval.v_string != NULL)
15603 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015604 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015605 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015606 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015607 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015609 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610}
15611
15612/*
15613 * Find variable "name" in the list of variables.
15614 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015615 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015616 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015617 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015619 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015620find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015622 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015625 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626
Bram Moolenaara7043832005-01-21 11:56:39 +000015627 ht = find_var_ht(name, &varname);
15628 if (htp != NULL)
15629 *htp = ht;
15630 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015632 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633}
15634
15635/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015636 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000015637 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015639 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015640find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000015641 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000015642 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015643 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000015644{
Bram Moolenaar33570922005-01-25 22:26:29 +000015645 hashitem_T *hi;
15646
15647 if (*varname == NUL)
15648 {
15649 /* Must be something like "s:", otherwise "ht" would be NULL. */
15650 switch (varname[-2])
15651 {
15652 case 's': return &SCRIPT_SV(current_SID).sv_var;
15653 case 'g': return &globvars_var;
15654 case 'v': return &vimvars_var;
15655 case 'b': return &curbuf->b_bufvar;
15656 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015657 case 'l': return current_funccal == NULL
15658 ? NULL : &current_funccal->l_vars_var;
15659 case 'a': return current_funccal == NULL
15660 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000015661 }
15662 return NULL;
15663 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015664
15665 hi = hash_find(ht, varname);
15666 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015667 {
15668 /* For global variables we may try auto-loading the script. If it
15669 * worked find the variable again. */
15670 if (ht == &globvarht && !writing
15671 && script_autoload(varname) && !aborting())
15672 hi = hash_find(ht, varname);
15673 if (HASHITEM_EMPTY(hi))
15674 return NULL;
15675 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015676 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015677}
15678
15679/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015680 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015681 * Set "varname" to the start of name without ':'.
15682 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015683 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015684find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685 char_u *name;
15686 char_u **varname;
15687{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015688 hashitem_T *hi;
15689
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 if (name[1] != ':')
15691 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015692 /* The name must not start with a colon or #. */
15693 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694 return NULL;
15695 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015696
15697 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015698 hi = hash_find(&compat_hashtab, name);
15699 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000015700 return &compat_hashtab;
15701
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015703 return &globvarht; /* global variable */
15704 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705 }
15706 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015707 if (*name == 'g') /* global variable */
15708 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015709 /* There must be no ':' or '#' in the rest of the name, unless g: is used
15710 */
15711 if (vim_strchr(name + 2, ':') != NULL
15712 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015713 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015715 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015717 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000015718 if (*name == 'v') /* v: variable */
15719 return &vimvarht;
15720 if (*name == 'a' && current_funccal != NULL) /* function argument */
15721 return &current_funccal->l_avars.dv_hashtab;
15722 if (*name == 'l' && current_funccal != NULL) /* local function variable */
15723 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724 if (*name == 's' /* script variable */
15725 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
15726 return &SCRIPT_VARS(current_SID);
15727 return NULL;
15728}
15729
15730/*
15731 * Get the string value of a (global/local) variable.
15732 * Returns NULL when it doesn't exist.
15733 */
15734 char_u *
15735get_var_value(name)
15736 char_u *name;
15737{
Bram Moolenaar33570922005-01-25 22:26:29 +000015738 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739
Bram Moolenaara7043832005-01-21 11:56:39 +000015740 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741 if (v == NULL)
15742 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015743 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744}
15745
15746/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015747 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 * sourcing this script and when executing functions defined in the script.
15749 */
15750 void
15751new_script_vars(id)
15752 scid_T id;
15753{
Bram Moolenaara7043832005-01-21 11:56:39 +000015754 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000015755 hashtab_T *ht;
15756 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000015757
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
15759 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015760 /* Re-allocating ga_data means that an ht_array pointing to
15761 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000015762 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000015763 for (i = 1; i <= ga_scripts.ga_len; ++i)
15764 {
15765 ht = &SCRIPT_VARS(i);
15766 if (ht->ht_mask == HT_INIT_SIZE - 1)
15767 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000015768 sv = &SCRIPT_SV(i);
15769 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000015770 }
15771
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 while (ga_scripts.ga_len < id)
15773 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015774 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
15775 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 }
15778 }
15779}
15780
15781/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015782 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
15783 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784 */
15785 void
Bram Moolenaar33570922005-01-25 22:26:29 +000015786init_var_dict(dict, dict_var)
15787 dict_T *dict;
15788 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789{
Bram Moolenaar33570922005-01-25 22:26:29 +000015790 hash_init(&dict->dv_hashtab);
15791 dict->dv_refcount = 99999;
15792 dict_var->di_tv.vval.v_dict = dict;
15793 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015794 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015795 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15796 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797}
15798
15799/*
15800 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000015801 * Frees all allocated variables and the value they contain.
15802 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803 */
15804 void
Bram Moolenaara7043832005-01-21 11:56:39 +000015805vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000015806 hashtab_T *ht;
15807{
15808 vars_clear_ext(ht, TRUE);
15809}
15810
15811/*
15812 * Like vars_clear(), but only free the value if "free_val" is TRUE.
15813 */
15814 static void
15815vars_clear_ext(ht, free_val)
15816 hashtab_T *ht;
15817 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818{
Bram Moolenaara7043832005-01-21 11:56:39 +000015819 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000015820 hashitem_T *hi;
15821 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822
Bram Moolenaar33570922005-01-25 22:26:29 +000015823 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000015824 todo = ht->ht_used;
15825 for (hi = ht->ht_array; todo > 0; ++hi)
15826 {
15827 if (!HASHITEM_EMPTY(hi))
15828 {
15829 --todo;
15830
Bram Moolenaar33570922005-01-25 22:26:29 +000015831 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000015832 * ht_array might change then. hash_clear() takes care of it
15833 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015834 v = HI2DI(hi);
15835 if (free_val)
15836 clear_tv(&v->di_tv);
15837 if ((v->di_flags & DI_FLAGS_FIX) == 0)
15838 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000015839 }
15840 }
15841 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015842 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843}
15844
Bram Moolenaara7043832005-01-21 11:56:39 +000015845/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015846 * Delete a variable from hashtab "ht" at item "hi".
15847 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000015848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000015850delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000015851 hashtab_T *ht;
15852 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015853{
Bram Moolenaar33570922005-01-25 22:26:29 +000015854 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015855
15856 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000015857 clear_tv(&di->di_tv);
15858 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859}
15860
15861/*
15862 * List the value of one internal variable.
15863 */
15864 static void
15865list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000015866 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867 char_u *prefix;
15868{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015869 char_u *tofree;
15870 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015871 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015872
Bram Moolenaar33570922005-01-25 22:26:29 +000015873 s = echo_string(&v->di_tv, &tofree, numbuf);
15874 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015875 s == NULL ? (char_u *)"" : s);
15876 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877}
15878
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879 static void
15880list_one_var_a(prefix, name, type, string)
15881 char_u *prefix;
15882 char_u *name;
15883 int type;
15884 char_u *string;
15885{
15886 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
15887 if (name != NULL) /* "a:" vars don't have a name stored */
15888 msg_puts(name);
15889 msg_putchar(' ');
15890 msg_advance(22);
15891 if (type == VAR_NUMBER)
15892 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015893 else if (type == VAR_FUNC)
15894 msg_putchar('*');
15895 else if (type == VAR_LIST)
15896 {
15897 msg_putchar('[');
15898 if (*string == '[')
15899 ++string;
15900 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015901 else if (type == VAR_DICT)
15902 {
15903 msg_putchar('{');
15904 if (*string == '{')
15905 ++string;
15906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907 else
15908 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015909
Bram Moolenaar071d4272004-06-13 20:20:40 +000015910 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015911
15912 if (type == VAR_FUNC)
15913 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914}
15915
15916/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015917 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015918 * If the variable already exists, the value is updated.
15919 * Otherwise the variable is created.
15920 */
15921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015922set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015923 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015924 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015925 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015926{
Bram Moolenaar33570922005-01-25 22:26:29 +000015927 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015929 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015930 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015932 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015933 {
15934 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
15935 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
15936 ? name[2] : name[0]))
15937 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015938 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015939 return;
15940 }
15941 if (function_exists(name))
15942 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015943 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015944 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015945 return;
15946 }
15947 }
15948
Bram Moolenaara7043832005-01-21 11:56:39 +000015949 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015950 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000015951 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000015952 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000015953 return;
15954 }
15955
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015956 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015957 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015958 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015959 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015960 if (var_check_ro(v->di_flags, name)
15961 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000015962 return;
15963 if (v->di_tv.v_type != tv->v_type
15964 && !((v->di_tv.v_type == VAR_STRING
15965 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015966 && (tv->v_type == VAR_STRING
15967 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015968 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015969 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015970 return;
15971 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015972
15973 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000015974 * Handle setting internal v: variables separately: we don't change
15975 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000015976 */
15977 if (ht == &vimvarht)
15978 {
15979 if (v->di_tv.v_type == VAR_STRING)
15980 {
15981 vim_free(v->di_tv.vval.v_string);
15982 if (copy || tv->v_type != VAR_STRING)
15983 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
15984 else
15985 {
15986 /* Take over the string to avoid an extra alloc/free. */
15987 v->di_tv.vval.v_string = tv->vval.v_string;
15988 tv->vval.v_string = NULL;
15989 }
15990 }
15991 else if (v->di_tv.v_type != VAR_NUMBER)
15992 EMSG2(_(e_intern2), "set_var()");
15993 else
15994 v->di_tv.vval.v_number = get_tv_number(tv);
15995 return;
15996 }
15997
15998 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999 }
16000 else /* add a new variable */
16001 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016002 /* Make sure the variable name is valid. */
16003 for (p = varname; *p != NUL; ++p)
16004 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16005 {
16006 EMSG2(_(e_illvar), varname);
16007 return;
16008 }
16009
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016010 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16011 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016012 if (v == NULL)
16013 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016014 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016015 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016017 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 return;
16019 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016020 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016023 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016024 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016025 else
16026 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016027 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016028 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016029 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031}
16032
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016033/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016034 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16035 * Also give an error message.
16036 */
16037 static int
16038var_check_ro(flags, name)
16039 int flags;
16040 char_u *name;
16041{
16042 if (flags & DI_FLAGS_RO)
16043 {
16044 EMSG2(_(e_readonlyvar), name);
16045 return TRUE;
16046 }
16047 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16048 {
16049 EMSG2(_(e_readonlysbx), name);
16050 return TRUE;
16051 }
16052 return FALSE;
16053}
16054
16055/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016056 * Return TRUE if typeval "tv" is set to be locked (immutable).
16057 * Also give an error message, using "name".
16058 */
16059 static int
16060tv_check_lock(lock, name)
16061 int lock;
16062 char_u *name;
16063{
16064 if (lock & VAR_LOCKED)
16065 {
16066 EMSG2(_("E741: Value is locked: %s"),
16067 name == NULL ? (char_u *)_("Unknown") : name);
16068 return TRUE;
16069 }
16070 if (lock & VAR_FIXED)
16071 {
16072 EMSG2(_("E742: Cannot change value of %s"),
16073 name == NULL ? (char_u *)_("Unknown") : name);
16074 return TRUE;
16075 }
16076 return FALSE;
16077}
16078
16079/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016080 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016081 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016082 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016085copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016086 typval_T *from;
16087 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016089 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016090 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016091 switch (from->v_type)
16092 {
16093 case VAR_NUMBER:
16094 to->vval.v_number = from->vval.v_number;
16095 break;
16096 case VAR_STRING:
16097 case VAR_FUNC:
16098 if (from->vval.v_string == NULL)
16099 to->vval.v_string = NULL;
16100 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016101 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016102 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016103 if (from->v_type == VAR_FUNC)
16104 func_ref(to->vval.v_string);
16105 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016106 break;
16107 case VAR_LIST:
16108 if (from->vval.v_list == NULL)
16109 to->vval.v_list = NULL;
16110 else
16111 {
16112 to->vval.v_list = from->vval.v_list;
16113 ++to->vval.v_list->lv_refcount;
16114 }
16115 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016116 case VAR_DICT:
16117 if (from->vval.v_dict == NULL)
16118 to->vval.v_dict = NULL;
16119 else
16120 {
16121 to->vval.v_dict = from->vval.v_dict;
16122 ++to->vval.v_dict->dv_refcount;
16123 }
16124 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016125 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016126 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016127 break;
16128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129}
16130
16131/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016132 * Make a copy of an item.
16133 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016134 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16135 * reference to an already copied list/dict can be used.
16136 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016137 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016138 static int
16139item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016140 typval_T *from;
16141 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016142 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016143 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016144{
16145 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016146 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016147
Bram Moolenaar33570922005-01-25 22:26:29 +000016148 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016149 {
16150 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016151 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016152 }
16153 ++recurse;
16154
16155 switch (from->v_type)
16156 {
16157 case VAR_NUMBER:
16158 case VAR_STRING:
16159 case VAR_FUNC:
16160 copy_tv(from, to);
16161 break;
16162 case VAR_LIST:
16163 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016164 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016165 if (from->vval.v_list == NULL)
16166 to->vval.v_list = NULL;
16167 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16168 {
16169 /* use the copy made earlier */
16170 to->vval.v_list = from->vval.v_list->lv_copylist;
16171 ++to->vval.v_list->lv_refcount;
16172 }
16173 else
16174 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16175 if (to->vval.v_list == NULL)
16176 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016177 break;
16178 case VAR_DICT:
16179 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016180 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016181 if (from->vval.v_dict == NULL)
16182 to->vval.v_dict = NULL;
16183 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16184 {
16185 /* use the copy made earlier */
16186 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16187 ++to->vval.v_dict->dv_refcount;
16188 }
16189 else
16190 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16191 if (to->vval.v_dict == NULL)
16192 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016193 break;
16194 default:
16195 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016196 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016197 }
16198 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016199 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016200}
16201
16202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203 * ":echo expr1 ..." print each argument separated with a space, add a
16204 * newline at the end.
16205 * ":echon expr1 ..." print each argument plain.
16206 */
16207 void
16208ex_echo(eap)
16209 exarg_T *eap;
16210{
16211 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016212 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016213 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214 char_u *p;
16215 int needclr = TRUE;
16216 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016217 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218
16219 if (eap->skip)
16220 ++emsg_skip;
16221 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16222 {
16223 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016224 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225 {
16226 /*
16227 * Report the invalid expression unless the expression evaluation
16228 * has been cancelled due to an aborting error, an interrupt, or an
16229 * exception.
16230 */
16231 if (!aborting())
16232 EMSG2(_(e_invexpr2), p);
16233 break;
16234 }
16235 if (!eap->skip)
16236 {
16237 if (atstart)
16238 {
16239 atstart = FALSE;
16240 /* Call msg_start() after eval1(), evaluating the expression
16241 * may cause a message to appear. */
16242 if (eap->cmdidx == CMD_echo)
16243 msg_start();
16244 }
16245 else if (eap->cmdidx == CMD_echo)
16246 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016247 p = echo_string(&rettv, &tofree, numbuf);
16248 if (p != NULL)
16249 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016251 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016253 if (*p != TAB && needclr)
16254 {
16255 /* remove any text still there from the command */
16256 msg_clr_eos();
16257 needclr = FALSE;
16258 }
16259 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260 }
16261 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016262 {
16263#ifdef FEAT_MBYTE
16264 if (has_mbyte)
16265 {
16266 int i = (*mb_ptr2len_check)(p);
16267
16268 (void)msg_outtrans_len_attr(p, i, echo_attr);
16269 p += i - 1;
16270 }
16271 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016273 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016276 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016277 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016278 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279 arg = skipwhite(arg);
16280 }
16281 eap->nextcmd = check_nextcmd(arg);
16282
16283 if (eap->skip)
16284 --emsg_skip;
16285 else
16286 {
16287 /* remove text that may still be there from the command */
16288 if (needclr)
16289 msg_clr_eos();
16290 if (eap->cmdidx == CMD_echo)
16291 msg_end();
16292 }
16293}
16294
16295/*
16296 * ":echohl {name}".
16297 */
16298 void
16299ex_echohl(eap)
16300 exarg_T *eap;
16301{
16302 int id;
16303
16304 id = syn_name2id(eap->arg);
16305 if (id == 0)
16306 echo_attr = 0;
16307 else
16308 echo_attr = syn_id2attr(id);
16309}
16310
16311/*
16312 * ":execute expr1 ..." execute the result of an expression.
16313 * ":echomsg expr1 ..." Print a message
16314 * ":echoerr expr1 ..." Print an error
16315 * Each gets spaces around each argument and a newline at the end for
16316 * echo commands
16317 */
16318 void
16319ex_execute(eap)
16320 exarg_T *eap;
16321{
16322 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016323 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324 int ret = OK;
16325 char_u *p;
16326 garray_T ga;
16327 int len;
16328 int save_did_emsg;
16329
16330 ga_init2(&ga, 1, 80);
16331
16332 if (eap->skip)
16333 ++emsg_skip;
16334 while (*arg != NUL && *arg != '|' && *arg != '\n')
16335 {
16336 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016337 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338 {
16339 /*
16340 * Report the invalid expression unless the expression evaluation
16341 * has been cancelled due to an aborting error, an interrupt, or an
16342 * exception.
16343 */
16344 if (!aborting())
16345 EMSG2(_(e_invexpr2), p);
16346 ret = FAIL;
16347 break;
16348 }
16349
16350 if (!eap->skip)
16351 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016352 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353 len = (int)STRLEN(p);
16354 if (ga_grow(&ga, len + 2) == FAIL)
16355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016356 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357 ret = FAIL;
16358 break;
16359 }
16360 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363 ga.ga_len += len;
16364 }
16365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016366 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016367 arg = skipwhite(arg);
16368 }
16369
16370 if (ret != FAIL && ga.ga_data != NULL)
16371 {
16372 if (eap->cmdidx == CMD_echomsg)
16373 MSG_ATTR(ga.ga_data, echo_attr);
16374 else if (eap->cmdidx == CMD_echoerr)
16375 {
16376 /* We don't want to abort following commands, restore did_emsg. */
16377 save_did_emsg = did_emsg;
16378 EMSG((char_u *)ga.ga_data);
16379 if (!force_abort)
16380 did_emsg = save_did_emsg;
16381 }
16382 else if (eap->cmdidx == CMD_execute)
16383 do_cmdline((char_u *)ga.ga_data,
16384 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16385 }
16386
16387 ga_clear(&ga);
16388
16389 if (eap->skip)
16390 --emsg_skip;
16391
16392 eap->nextcmd = check_nextcmd(arg);
16393}
16394
16395/*
16396 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16397 * "arg" points to the "&" or '+' when called, to "option" when returning.
16398 * Returns NULL when no option name found. Otherwise pointer to the char
16399 * after the option name.
16400 */
16401 static char_u *
16402find_option_end(arg, opt_flags)
16403 char_u **arg;
16404 int *opt_flags;
16405{
16406 char_u *p = *arg;
16407
16408 ++p;
16409 if (*p == 'g' && p[1] == ':')
16410 {
16411 *opt_flags = OPT_GLOBAL;
16412 p += 2;
16413 }
16414 else if (*p == 'l' && p[1] == ':')
16415 {
16416 *opt_flags = OPT_LOCAL;
16417 p += 2;
16418 }
16419 else
16420 *opt_flags = 0;
16421
16422 if (!ASCII_ISALPHA(*p))
16423 return NULL;
16424 *arg = p;
16425
16426 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16427 p += 4; /* termcap option */
16428 else
16429 while (ASCII_ISALPHA(*p))
16430 ++p;
16431 return p;
16432}
16433
16434/*
16435 * ":function"
16436 */
16437 void
16438ex_function(eap)
16439 exarg_T *eap;
16440{
16441 char_u *theline;
16442 int j;
16443 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 char_u *name = NULL;
16446 char_u *p;
16447 char_u *arg;
16448 garray_T newargs;
16449 garray_T newlines;
16450 int varargs = FALSE;
16451 int mustend = FALSE;
16452 int flags = 0;
16453 ufunc_T *fp;
16454 int indent;
16455 int nesting;
16456 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016457 dictitem_T *v;
16458 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016459 static int func_nr = 0; /* number for nameless function */
16460 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016461 hashtab_T *ht;
16462 int todo;
16463 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464
16465 /*
16466 * ":function" without argument: list functions.
16467 */
16468 if (ends_excmd(*eap->arg))
16469 {
16470 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016471 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016472 todo = func_hashtab.ht_used;
16473 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016474 {
16475 if (!HASHITEM_EMPTY(hi))
16476 {
16477 --todo;
16478 fp = HI2UF(hi);
16479 if (!isdigit(*fp->uf_name))
16480 list_func_head(fp, FALSE);
16481 }
16482 }
16483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016484 eap->nextcmd = check_nextcmd(eap->arg);
16485 return;
16486 }
16487
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016488 /*
16489 * Get the function name. There are these situations:
16490 * func normal function name
16491 * "name" == func, "fudi.fd_dict" == NULL
16492 * dict.func new dictionary entry
16493 * "name" == NULL, "fudi.fd_dict" set,
16494 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16495 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016496 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016497 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16498 * dict.func existing dict entry that's not a Funcref
16499 * "name" == NULL, "fudi.fd_dict" set,
16500 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016503 name = trans_function_name(&p, eap->skip, 0, &fudi);
16504 paren = (vim_strchr(p, '(') != NULL);
16505 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016506 {
16507 /*
16508 * Return on an invalid expression in braces, unless the expression
16509 * evaluation has been cancelled due to an aborting error, an
16510 * interrupt, or an exception.
16511 */
16512 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016513 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016514 if (!eap->skip && fudi.fd_newkey != NULL)
16515 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016516 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519 else
16520 eap->skip = TRUE;
16521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016522 /* An error in a function call during evaluation of an expression in magic
16523 * braces should not cause the function not to be defined. */
16524 saved_did_emsg = did_emsg;
16525 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526
16527 /*
16528 * ":function func" with only function name: list function.
16529 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016530 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531 {
16532 if (!ends_excmd(*skipwhite(p)))
16533 {
16534 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016535 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 }
16537 eap->nextcmd = check_nextcmd(p);
16538 if (eap->nextcmd != NULL)
16539 *p = NUL;
16540 if (!eap->skip && !got_int)
16541 {
16542 fp = find_func(name);
16543 if (fp != NULL)
16544 {
16545 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016546 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547 {
16548 msg_putchar('\n');
16549 msg_outnum((long)(j + 1));
16550 if (j < 9)
16551 msg_putchar(' ');
16552 if (j < 99)
16553 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016554 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 out_flush(); /* show a line at a time */
16556 ui_breakcheck();
16557 }
16558 if (!got_int)
16559 {
16560 msg_putchar('\n');
16561 msg_puts((char_u *)" endfunction");
16562 }
16563 }
16564 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016565 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016567 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568 }
16569
16570 /*
16571 * ":function name(arg1, arg2)" Define function.
16572 */
16573 p = skipwhite(p);
16574 if (*p != '(')
16575 {
16576 if (!eap->skip)
16577 {
16578 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016579 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580 }
16581 /* attempt to continue by skipping some text */
16582 if (vim_strchr(p, '(') != NULL)
16583 p = vim_strchr(p, '(');
16584 }
16585 p = skipwhite(p + 1);
16586
16587 ga_init2(&newargs, (int)sizeof(char_u *), 3);
16588 ga_init2(&newlines, (int)sizeof(char_u *), 3);
16589
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016590 if (!eap->skip)
16591 {
16592 /* Check the name of the function. */
16593 if (name != NULL)
16594 arg = name;
16595 else
16596 arg = fudi.fd_newkey;
16597 if (arg != NULL)
16598 {
16599 if (*arg == K_SPECIAL)
16600 j = 3;
16601 else
16602 j = 0;
16603 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
16604 : eval_isnamec(arg[j])))
16605 ++j;
16606 if (arg[j] != NUL)
16607 emsg_funcname(_(e_invarg2), arg);
16608 }
16609 }
16610
Bram Moolenaar071d4272004-06-13 20:20:40 +000016611 /*
16612 * Isolate the arguments: "arg1, arg2, ...)"
16613 */
16614 while (*p != ')')
16615 {
16616 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
16617 {
16618 varargs = TRUE;
16619 p += 3;
16620 mustend = TRUE;
16621 }
16622 else
16623 {
16624 arg = p;
16625 while (ASCII_ISALNUM(*p) || *p == '_')
16626 ++p;
16627 if (arg == p || isdigit(*arg)
16628 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
16629 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
16630 {
16631 if (!eap->skip)
16632 EMSG2(_("E125: Illegal argument: %s"), arg);
16633 break;
16634 }
16635 if (ga_grow(&newargs, 1) == FAIL)
16636 goto erret;
16637 c = *p;
16638 *p = NUL;
16639 arg = vim_strsave(arg);
16640 if (arg == NULL)
16641 goto erret;
16642 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
16643 *p = c;
16644 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645 if (*p == ',')
16646 ++p;
16647 else
16648 mustend = TRUE;
16649 }
16650 p = skipwhite(p);
16651 if (mustend && *p != ')')
16652 {
16653 if (!eap->skip)
16654 EMSG2(_(e_invarg2), eap->arg);
16655 break;
16656 }
16657 }
16658 ++p; /* skip the ')' */
16659
Bram Moolenaare9a41262005-01-15 22:18:47 +000016660 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661 for (;;)
16662 {
16663 p = skipwhite(p);
16664 if (STRNCMP(p, "range", 5) == 0)
16665 {
16666 flags |= FC_RANGE;
16667 p += 5;
16668 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016669 else if (STRNCMP(p, "dict", 4) == 0)
16670 {
16671 flags |= FC_DICT;
16672 p += 4;
16673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674 else if (STRNCMP(p, "abort", 5) == 0)
16675 {
16676 flags |= FC_ABORT;
16677 p += 5;
16678 }
16679 else
16680 break;
16681 }
16682
16683 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
16684 EMSG(_(e_trailing));
16685
16686 /*
16687 * Read the body of the function, until ":endfunction" is found.
16688 */
16689 if (KeyTyped)
16690 {
16691 /* Check if the function already exists, don't let the user type the
16692 * whole function before telling him it doesn't work! For a script we
16693 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016694 if (!eap->skip && !eap->forceit)
16695 {
16696 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
16697 EMSG(_(e_funcdict));
16698 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016699 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016701
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016702 if (!eap->skip && did_emsg)
16703 goto erret;
16704
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705 msg_putchar('\n'); /* don't overwrite the function name */
16706 cmdline_row = msg_row;
16707 }
16708
16709 indent = 2;
16710 nesting = 0;
16711 for (;;)
16712 {
16713 msg_scroll = TRUE;
16714 need_wait_return = FALSE;
16715 if (eap->getline == NULL)
16716 theline = getcmdline(':', 0L, indent);
16717 else
16718 theline = eap->getline(':', eap->cookie, indent);
16719 if (KeyTyped)
16720 lines_left = Rows - 1;
16721 if (theline == NULL)
16722 {
16723 EMSG(_("E126: Missing :endfunction"));
16724 goto erret;
16725 }
16726
16727 if (skip_until != NULL)
16728 {
16729 /* between ":append" and "." and between ":python <<EOF" and "EOF"
16730 * don't check for ":endfunc". */
16731 if (STRCMP(theline, skip_until) == 0)
16732 {
16733 vim_free(skip_until);
16734 skip_until = NULL;
16735 }
16736 }
16737 else
16738 {
16739 /* skip ':' and blanks*/
16740 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
16741 ;
16742
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016743 /* Check for "endfunction". */
16744 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 {
16746 vim_free(theline);
16747 break;
16748 }
16749
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016750 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751 * at "end". */
16752 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
16753 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016754 else if (STRNCMP(p, "if", 2) == 0
16755 || STRNCMP(p, "wh", 2) == 0
16756 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000016757 || STRNCMP(p, "try", 3) == 0)
16758 indent += 2;
16759
16760 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016761 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016762 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016763 if (*p == '!')
16764 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765 p += eval_fname_script(p);
16766 if (ASCII_ISALPHA(*p))
16767 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016768 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769 if (*skipwhite(p) == '(')
16770 {
16771 ++nesting;
16772 indent += 2;
16773 }
16774 }
16775 }
16776
16777 /* Check for ":append" or ":insert". */
16778 p = skip_range(p, NULL);
16779 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
16780 || (p[0] == 'i'
16781 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
16782 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
16783 skip_until = vim_strsave((char_u *)".");
16784
16785 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
16786 arg = skipwhite(skiptowhite(p));
16787 if (arg[0] == '<' && arg[1] =='<'
16788 && ((p[0] == 'p' && p[1] == 'y'
16789 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
16790 || (p[0] == 'p' && p[1] == 'e'
16791 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
16792 || (p[0] == 't' && p[1] == 'c'
16793 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
16794 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
16795 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000016796 || (p[0] == 'm' && p[1] == 'z'
16797 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 ))
16799 {
16800 /* ":python <<" continues until a dot, like ":append" */
16801 p = skipwhite(arg + 2);
16802 if (*p == NUL)
16803 skip_until = vim_strsave((char_u *)".");
16804 else
16805 skip_until = vim_strsave(p);
16806 }
16807 }
16808
16809 /* Add the line to the function. */
16810 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016811 {
16812 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016814 }
16815
16816 /* Copy the line to newly allocated memory. get_one_sourceline()
16817 * allocates 250 bytes per line, this saves 80% on average. The cost
16818 * is an extra alloc/free. */
16819 p = vim_strsave(theline);
16820 if (p != NULL)
16821 {
16822 vim_free(theline);
16823 theline = p;
16824 }
16825
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
16827 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828 }
16829
16830 /* Don't define the function when skipping commands or when an error was
16831 * detected. */
16832 if (eap->skip || did_emsg)
16833 goto erret;
16834
16835 /*
16836 * If there are no errors, add the function
16837 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016838 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016839 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016840 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000016841 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016842 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016843 emsg_funcname("E707: Function name conflicts with variable: %s",
16844 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016845 goto erret;
16846 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016847
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016848 fp = find_func(name);
16849 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016851 if (!eap->forceit)
16852 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016853 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016854 goto erret;
16855 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016856 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016857 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016858 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016859 name);
16860 goto erret;
16861 }
16862 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016863 ga_clear_strings(&(fp->uf_args));
16864 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016865 vim_free(name);
16866 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868 }
16869 else
16870 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016871 char numbuf[20];
16872
16873 fp = NULL;
16874 if (fudi.fd_newkey == NULL && !eap->forceit)
16875 {
16876 EMSG(_(e_funcdict));
16877 goto erret;
16878 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000016879 if (fudi.fd_di == NULL)
16880 {
16881 /* Can't add a function to a locked dictionary */
16882 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
16883 goto erret;
16884 }
16885 /* Can't change an existing function if it is locked */
16886 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
16887 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016888
16889 /* Give the function a sequential number. Can only be used with a
16890 * Funcref! */
16891 vim_free(name);
16892 sprintf(numbuf, "%d", ++func_nr);
16893 name = vim_strsave((char_u *)numbuf);
16894 if (name == NULL)
16895 goto erret;
16896 }
16897
16898 if (fp == NULL)
16899 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016900 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016901 {
16902 int slen, plen;
16903 char_u *scriptname;
16904
16905 /* Check that the autoload name matches the script name. */
16906 j = FAIL;
16907 if (sourcing_name != NULL)
16908 {
16909 scriptname = autoload_name(name);
16910 if (scriptname != NULL)
16911 {
16912 p = vim_strchr(scriptname, '/');
16913 plen = STRLEN(p);
16914 slen = STRLEN(sourcing_name);
16915 if (slen > plen && fnamecmp(p,
16916 sourcing_name + slen - plen) == 0)
16917 j = OK;
16918 vim_free(scriptname);
16919 }
16920 }
16921 if (j == FAIL)
16922 {
16923 EMSG2(_("E746: Function name does not match script file name: %s"), name);
16924 goto erret;
16925 }
16926 }
16927
16928 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929 if (fp == NULL)
16930 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016931
16932 if (fudi.fd_dict != NULL)
16933 {
16934 if (fudi.fd_di == NULL)
16935 {
16936 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016937 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016938 if (fudi.fd_di == NULL)
16939 {
16940 vim_free(fp);
16941 goto erret;
16942 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016943 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
16944 {
16945 vim_free(fudi.fd_di);
16946 goto erret;
16947 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016948 }
16949 else
16950 /* overwrite existing dict entry */
16951 clear_tv(&fudi.fd_di->di_tv);
16952 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016953 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016954 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016955 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016956 }
16957
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016959 STRCPY(fp->uf_name, name);
16960 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016962 fp->uf_args = newargs;
16963 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016964#ifdef FEAT_PROFILE
16965 fp->uf_tml_count = NULL;
16966 fp->uf_tml_total = NULL;
16967 fp->uf_tml_self = NULL;
16968 fp->uf_profiling = FALSE;
16969 if (prof_def_func())
16970 func_do_profile(fp);
16971#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016972 fp->uf_varargs = varargs;
16973 fp->uf_flags = flags;
16974 fp->uf_calls = 0;
16975 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016976 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977
16978erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 ga_clear_strings(&newargs);
16980 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016981ret_free:
16982 vim_free(skip_until);
16983 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986}
16987
16988/*
16989 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000016990 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016992 * flags:
16993 * TFN_INT: internal function name OK
16994 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995 * Advances "pp" to just after the function name (if no error).
16996 */
16997 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016998trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 char_u **pp;
17000 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017001 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017002 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017004 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005 char_u *start;
17006 char_u *end;
17007 int lead;
17008 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017010 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017011
17012 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017013 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017015
17016 /* Check for hard coded <SNR>: already translated function ID (from a user
17017 * command). */
17018 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17019 && (*pp)[2] == (int)KE_SNR)
17020 {
17021 *pp += 3;
17022 len = get_id_len(pp) + 3;
17023 return vim_strnsave(start, len);
17024 }
17025
17026 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17027 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017029 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017031
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017032 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17033 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017034 if (end == start)
17035 {
17036 if (!skip)
17037 EMSG(_("E129: Function name required"));
17038 goto theend;
17039 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017040 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017041 {
17042 /*
17043 * Report an invalid expression in braces, unless the expression
17044 * evaluation has been cancelled due to an aborting error, an
17045 * interrupt, or an exception.
17046 */
17047 if (!aborting())
17048 {
17049 if (end != NULL)
17050 EMSG2(_(e_invarg2), start);
17051 }
17052 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017053 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017054 goto theend;
17055 }
17056
17057 if (lv.ll_tv != NULL)
17058 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017059 if (fdp != NULL)
17060 {
17061 fdp->fd_dict = lv.ll_dict;
17062 fdp->fd_newkey = lv.ll_newkey;
17063 lv.ll_newkey = NULL;
17064 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017065 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017066 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17067 {
17068 name = vim_strsave(lv.ll_tv->vval.v_string);
17069 *pp = end;
17070 }
17071 else
17072 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017073 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17074 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017075 EMSG(_(e_funcref));
17076 else
17077 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017078 name = NULL;
17079 }
17080 goto theend;
17081 }
17082
17083 if (lv.ll_name == NULL)
17084 {
17085 /* Error found, but continue after the function name. */
17086 *pp = end;
17087 goto theend;
17088 }
17089
17090 if (lv.ll_exp_name != NULL)
17091 len = STRLEN(lv.ll_exp_name);
17092 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017093 {
17094 if (lead == 2) /* skip over "s:" */
17095 lv.ll_name += 2;
17096 len = (int)(end - lv.ll_name);
17097 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017098
17099 /*
17100 * Copy the function name to allocated memory.
17101 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17102 * Accept <SNR>123_name() outside a script.
17103 */
17104 if (skip)
17105 lead = 0; /* do nothing */
17106 else if (lead > 0)
17107 {
17108 lead = 3;
17109 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17110 {
17111 if (current_SID <= 0)
17112 {
17113 EMSG(_(e_usingsid));
17114 goto theend;
17115 }
17116 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17117 lead += (int)STRLEN(sid_buf);
17118 }
17119 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017120 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017121 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017122 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017123 goto theend;
17124 }
17125 name = alloc((unsigned)(len + lead + 1));
17126 if (name != NULL)
17127 {
17128 if (lead > 0)
17129 {
17130 name[0] = K_SPECIAL;
17131 name[1] = KS_EXTRA;
17132 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017133 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017134 STRCPY(name + 3, sid_buf);
17135 }
17136 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17137 name[len + lead] = NUL;
17138 }
17139 *pp = end;
17140
17141theend:
17142 clear_lval(&lv);
17143 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144}
17145
17146/*
17147 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17148 * Return 2 if "p" starts with "s:".
17149 * Return 0 otherwise.
17150 */
17151 static int
17152eval_fname_script(p)
17153 char_u *p;
17154{
17155 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17156 || STRNICMP(p + 1, "SNR>", 4) == 0))
17157 return 5;
17158 if (p[0] == 's' && p[1] == ':')
17159 return 2;
17160 return 0;
17161}
17162
17163/*
17164 * Return TRUE if "p" starts with "<SID>" or "s:".
17165 * Only works if eval_fname_script() returned non-zero for "p"!
17166 */
17167 static int
17168eval_fname_sid(p)
17169 char_u *p;
17170{
17171 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17172}
17173
17174/*
17175 * List the head of the function: "name(arg1, arg2)".
17176 */
17177 static void
17178list_func_head(fp, indent)
17179 ufunc_T *fp;
17180 int indent;
17181{
17182 int j;
17183
17184 msg_start();
17185 if (indent)
17186 MSG_PUTS(" ");
17187 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017188 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 {
17190 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017191 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192 }
17193 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017194 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017196 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197 {
17198 if (j)
17199 MSG_PUTS(", ");
17200 msg_puts(FUNCARG(fp, j));
17201 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017202 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203 {
17204 if (j)
17205 MSG_PUTS(", ");
17206 MSG_PUTS("...");
17207 }
17208 msg_putchar(')');
17209}
17210
17211/*
17212 * Find a function by name, return pointer to it in ufuncs.
17213 * Return NULL for unknown function.
17214 */
17215 static ufunc_T *
17216find_func(name)
17217 char_u *name;
17218{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017219 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017221 hi = hash_find(&func_hashtab, name);
17222 if (!HASHITEM_EMPTY(hi))
17223 return HI2UF(hi);
17224 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225}
17226
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017227#if defined(EXITFREE) || defined(PROTO)
17228 void
17229free_all_functions()
17230{
17231 hashitem_T *hi;
17232
17233 /* Need to start all over every time, because func_free() may change the
17234 * hash table. */
17235 while (func_hashtab.ht_used > 0)
17236 for (hi = func_hashtab.ht_array; ; ++hi)
17237 if (!HASHITEM_EMPTY(hi))
17238 {
17239 func_free(HI2UF(hi));
17240 break;
17241 }
17242}
17243#endif
17244
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017245/*
17246 * Return TRUE if a function "name" exists.
17247 */
17248 static int
17249function_exists(name)
17250 char_u *name;
17251{
17252 char_u *p = name;
17253 int n = FALSE;
17254
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017255 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017256 if (p != NULL)
17257 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017258 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017259 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017260 else
17261 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017262 vim_free(p);
17263 }
17264 return n;
17265}
17266
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017267/*
17268 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017269 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017270 */
17271 static int
17272builtin_function(name)
17273 char_u *name;
17274{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017275 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17276 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017277}
17278
Bram Moolenaar05159a02005-02-26 23:04:13 +000017279#if defined(FEAT_PROFILE) || defined(PROTO)
17280/*
17281 * Start profiling function "fp".
17282 */
17283 static void
17284func_do_profile(fp)
17285 ufunc_T *fp;
17286{
17287 fp->uf_tm_count = 0;
17288 profile_zero(&fp->uf_tm_self);
17289 profile_zero(&fp->uf_tm_total);
17290 if (fp->uf_tml_count == NULL)
17291 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17292 (sizeof(int) * fp->uf_lines.ga_len));
17293 if (fp->uf_tml_total == NULL)
17294 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17295 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17296 if (fp->uf_tml_self == NULL)
17297 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17298 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17299 fp->uf_tml_idx = -1;
17300 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17301 || fp->uf_tml_self == NULL)
17302 return; /* out of memory */
17303
17304 fp->uf_profiling = TRUE;
17305}
17306
17307/*
17308 * Dump the profiling results for all functions in file "fd".
17309 */
17310 void
17311func_dump_profile(fd)
17312 FILE *fd;
17313{
17314 hashitem_T *hi;
17315 int todo;
17316 ufunc_T *fp;
17317 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017318 ufunc_T **sorttab;
17319 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017320
17321 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017322 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17323
Bram Moolenaar05159a02005-02-26 23:04:13 +000017324 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17325 {
17326 if (!HASHITEM_EMPTY(hi))
17327 {
17328 --todo;
17329 fp = HI2UF(hi);
17330 if (fp->uf_profiling)
17331 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017332 if (sorttab != NULL)
17333 sorttab[st_len++] = fp;
17334
Bram Moolenaar05159a02005-02-26 23:04:13 +000017335 if (fp->uf_name[0] == K_SPECIAL)
17336 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17337 else
17338 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17339 if (fp->uf_tm_count == 1)
17340 fprintf(fd, "Called 1 time\n");
17341 else
17342 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17343 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17344 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17345 fprintf(fd, "\n");
17346 fprintf(fd, "count total (s) self (s)\n");
17347
17348 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17349 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017350 prof_func_line(fd, fp->uf_tml_count[i],
17351 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017352 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17353 }
17354 fprintf(fd, "\n");
17355 }
17356 }
17357 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017358
17359 if (sorttab != NULL && st_len > 0)
17360 {
17361 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17362 prof_total_cmp);
17363 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17364 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17365 prof_self_cmp);
17366 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17367 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017368}
Bram Moolenaar73830342005-02-28 22:48:19 +000017369
17370 static void
17371prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17372 FILE *fd;
17373 ufunc_T **sorttab;
17374 int st_len;
17375 char *title;
17376 int prefer_self; /* when equal print only self time */
17377{
17378 int i;
17379 ufunc_T *fp;
17380
17381 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17382 fprintf(fd, "count total (s) self (s) function\n");
17383 for (i = 0; i < 20 && i < st_len; ++i)
17384 {
17385 fp = sorttab[i];
17386 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17387 prefer_self);
17388 if (fp->uf_name[0] == K_SPECIAL)
17389 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17390 else
17391 fprintf(fd, " %s()\n", fp->uf_name);
17392 }
17393 fprintf(fd, "\n");
17394}
17395
17396/*
17397 * Print the count and times for one function or function line.
17398 */
17399 static void
17400prof_func_line(fd, count, total, self, prefer_self)
17401 FILE *fd;
17402 int count;
17403 proftime_T *total;
17404 proftime_T *self;
17405 int prefer_self; /* when equal print only self time */
17406{
17407 if (count > 0)
17408 {
17409 fprintf(fd, "%5d ", count);
17410 if (prefer_self && profile_equal(total, self))
17411 fprintf(fd, " ");
17412 else
17413 fprintf(fd, "%s ", profile_msg(total));
17414 if (!prefer_self && profile_equal(total, self))
17415 fprintf(fd, " ");
17416 else
17417 fprintf(fd, "%s ", profile_msg(self));
17418 }
17419 else
17420 fprintf(fd, " ");
17421}
17422
17423/*
17424 * Compare function for total time sorting.
17425 */
17426 static int
17427#ifdef __BORLANDC__
17428_RTLENTRYF
17429#endif
17430prof_total_cmp(s1, s2)
17431 const void *s1;
17432 const void *s2;
17433{
17434 ufunc_T *p1, *p2;
17435
17436 p1 = *(ufunc_T **)s1;
17437 p2 = *(ufunc_T **)s2;
17438 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17439}
17440
17441/*
17442 * Compare function for self time sorting.
17443 */
17444 static int
17445#ifdef __BORLANDC__
17446_RTLENTRYF
17447#endif
17448prof_self_cmp(s1, s2)
17449 const void *s1;
17450 const void *s2;
17451{
17452 ufunc_T *p1, *p2;
17453
17454 p1 = *(ufunc_T **)s1;
17455 p2 = *(ufunc_T **)s2;
17456 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17457}
17458
Bram Moolenaar05159a02005-02-26 23:04:13 +000017459#endif
17460
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017461/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017462 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017463 * Return TRUE if a package was loaded.
17464 */
17465 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017466script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017467 char_u *name;
17468{
17469 char_u *p;
17470 char_u *scriptname;
17471 int ret = FALSE;
17472
17473 /* If there is no colon after name[1] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017474 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017475 if (p == NULL || p <= name + 2)
17476 return FALSE;
17477
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017478 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
17479 scriptname = autoload_name(name);
17480 if (cmd_runtime(scriptname, FALSE) == OK)
17481 ret = TRUE;
17482
17483 vim_free(scriptname);
17484 return ret;
17485}
17486
17487/*
17488 * Return the autoload script name for a function or variable name.
17489 * Returns NULL when out of memory.
17490 */
17491 static char_u *
17492autoload_name(name)
17493 char_u *name;
17494{
17495 char_u *p;
17496 char_u *scriptname;
17497
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017498 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017499 scriptname = alloc((unsigned)(STRLEN(name) + 14));
17500 if (scriptname == NULL)
17501 return FALSE;
17502 STRCPY(scriptname, "autoload/");
17503 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017504 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017505 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017506 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017507 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017508 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017509}
17510
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
17512
17513/*
17514 * Function given to ExpandGeneric() to obtain the list of user defined
17515 * function names.
17516 */
17517 char_u *
17518get_user_func_name(xp, idx)
17519 expand_T *xp;
17520 int idx;
17521{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017522 static long_u done;
17523 static hashitem_T *hi;
17524 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525
17526 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017528 done = 0;
17529 hi = func_hashtab.ht_array;
17530 }
17531 if (done < func_hashtab.ht_used)
17532 {
17533 if (done++ > 0)
17534 ++hi;
17535 while (HASHITEM_EMPTY(hi))
17536 ++hi;
17537 fp = HI2UF(hi);
17538
17539 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
17540 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541
17542 cat_func_name(IObuff, fp);
17543 if (xp->xp_context != EXPAND_USER_FUNC)
17544 {
17545 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017546 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547 STRCAT(IObuff, ")");
17548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549 return IObuff;
17550 }
17551 return NULL;
17552}
17553
17554#endif /* FEAT_CMDL_COMPL */
17555
17556/*
17557 * Copy the function name of "fp" to buffer "buf".
17558 * "buf" must be able to hold the function name plus three bytes.
17559 * Takes care of script-local function names.
17560 */
17561 static void
17562cat_func_name(buf, fp)
17563 char_u *buf;
17564 ufunc_T *fp;
17565{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017566 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567 {
17568 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017569 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570 }
17571 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017572 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573}
17574
17575/*
17576 * ":delfunction {name}"
17577 */
17578 void
17579ex_delfunction(eap)
17580 exarg_T *eap;
17581{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017582 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583 char_u *p;
17584 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017585 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586
17587 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017588 name = trans_function_name(&p, eap->skip, 0, &fudi);
17589 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017591 {
17592 if (fudi.fd_dict != NULL && !eap->skip)
17593 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596 if (!ends_excmd(*skipwhite(p)))
17597 {
17598 vim_free(name);
17599 EMSG(_(e_trailing));
17600 return;
17601 }
17602 eap->nextcmd = check_nextcmd(p);
17603 if (eap->nextcmd != NULL)
17604 *p = NUL;
17605
17606 if (!eap->skip)
17607 fp = find_func(name);
17608 vim_free(name);
17609
17610 if (!eap->skip)
17611 {
17612 if (fp == NULL)
17613 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017614 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 return;
17616 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017617 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618 {
17619 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
17620 return;
17621 }
17622
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017623 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017625 /* Delete the dict item that refers to the function, it will
17626 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017627 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017629 else
17630 func_free(fp);
17631 }
17632}
17633
17634/*
17635 * Free a function and remove it from the list of functions.
17636 */
17637 static void
17638func_free(fp)
17639 ufunc_T *fp;
17640{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017641 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017642
17643 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017644 ga_clear_strings(&(fp->uf_args));
17645 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000017646#ifdef FEAT_PROFILE
17647 vim_free(fp->uf_tml_count);
17648 vim_free(fp->uf_tml_total);
17649 vim_free(fp->uf_tml_self);
17650#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017651
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017652 /* remove the function from the function hashtable */
17653 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
17654 if (HASHITEM_EMPTY(hi))
17655 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017656 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017657 hash_remove(&func_hashtab, hi);
17658
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017659 vim_free(fp);
17660}
17661
17662/*
17663 * Unreference a Function: decrement the reference count and free it when it
17664 * becomes zero. Only for numbered functions.
17665 */
17666 static void
17667func_unref(name)
17668 char_u *name;
17669{
17670 ufunc_T *fp;
17671
17672 if (name != NULL && isdigit(*name))
17673 {
17674 fp = find_func(name);
17675 if (fp == NULL)
17676 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017677 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017678 {
17679 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017680 * when "uf_calls" becomes zero. */
17681 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017682 func_free(fp);
17683 }
17684 }
17685}
17686
17687/*
17688 * Count a reference to a Function.
17689 */
17690 static void
17691func_ref(name)
17692 char_u *name;
17693{
17694 ufunc_T *fp;
17695
17696 if (name != NULL && isdigit(*name))
17697 {
17698 fp = find_func(name);
17699 if (fp == NULL)
17700 EMSG2(_(e_intern2), "func_ref()");
17701 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017702 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703 }
17704}
17705
17706/*
17707 * Call a user function.
17708 */
17709 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000017710call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 ufunc_T *fp; /* pointer to function */
17712 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000017713 typval_T *argvars; /* arguments */
17714 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715 linenr_T firstline; /* first line of range */
17716 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000017717 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718{
Bram Moolenaar33570922005-01-25 22:26:29 +000017719 char_u *save_sourcing_name;
17720 linenr_T save_sourcing_lnum;
17721 scid_T save_current_SID;
17722 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000017723 int save_did_emsg;
17724 static int depth = 0;
17725 dictitem_T *v;
17726 int fixvar_idx = 0; /* index in fixvar[] */
17727 int i;
17728 int ai;
17729 char_u numbuf[NUMBUFLEN];
17730 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017731#ifdef FEAT_PROFILE
17732 proftime_T wait_start;
17733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734
17735 /* If depth of calling is getting too high, don't execute the function */
17736 if (depth >= p_mfd)
17737 {
17738 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017739 rettv->v_type = VAR_NUMBER;
17740 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741 return;
17742 }
17743 ++depth;
17744
17745 line_breakcheck(); /* check for CTRL-C hit */
17746
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017747 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000017748 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017750 fc.rettv = rettv;
17751 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752 fc.linenr = 0;
17753 fc.returned = FALSE;
17754 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017756 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757 fc.dbg_tick = debug_tick;
17758
Bram Moolenaar33570922005-01-25 22:26:29 +000017759 /*
17760 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
17761 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
17762 * each argument variable and saves a lot of time.
17763 */
17764 /*
17765 * Init l: variables.
17766 */
17767 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000017768 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017769 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017770 /* Set l:self to "selfdict". */
17771 v = &fc.fixvar[fixvar_idx++].var;
17772 STRCPY(v->di_key, "self");
17773 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
17774 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
17775 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017776 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000017777 v->di_tv.vval.v_dict = selfdict;
17778 ++selfdict->dv_refcount;
17779 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017780
Bram Moolenaar33570922005-01-25 22:26:29 +000017781 /*
17782 * Init a: variables.
17783 * Set a:0 to "argcount".
17784 * Set a:000 to a list with room for the "..." arguments.
17785 */
17786 init_var_dict(&fc.l_avars, &fc.l_avars_var);
17787 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017788 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000017789 v = &fc.fixvar[fixvar_idx++].var;
17790 STRCPY(v->di_key, "000");
17791 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17792 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17793 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017794 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017795 v->di_tv.vval.v_list = &fc.l_varlist;
17796 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
17797 fc.l_varlist.lv_refcount = 99999;
17798
17799 /*
17800 * Set a:firstline to "firstline" and a:lastline to "lastline".
17801 * Set a:name to named arguments.
17802 * Set a:N to the "..." arguments.
17803 */
17804 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
17805 (varnumber_T)firstline);
17806 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
17807 (varnumber_T)lastline);
17808 for (i = 0; i < argcount; ++i)
17809 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017810 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017811 if (ai < 0)
17812 /* named argument a:name */
17813 name = FUNCARG(fp, i);
17814 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017815 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017816 /* "..." argument a:1, a:2, etc. */
17817 sprintf((char *)numbuf, "%d", ai + 1);
17818 name = numbuf;
17819 }
17820 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
17821 {
17822 v = &fc.fixvar[fixvar_idx++].var;
17823 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17824 }
17825 else
17826 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017827 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17828 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000017829 if (v == NULL)
17830 break;
17831 v->di_flags = DI_FLAGS_RO;
17832 }
17833 STRCPY(v->di_key, name);
17834 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17835
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017836 /* Note: the values are copied directly to avoid alloc/free.
17837 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017838 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017839 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017840
17841 if (ai >= 0 && ai < MAX_FUNC_ARGS)
17842 {
17843 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
17844 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017845 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017846 }
17847 }
17848
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849 /* Don't redraw while executing the function. */
17850 ++RedrawingDisabled;
17851 save_sourcing_name = sourcing_name;
17852 save_sourcing_lnum = sourcing_lnum;
17853 sourcing_lnum = 1;
17854 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017855 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856 if (sourcing_name != NULL)
17857 {
17858 if (save_sourcing_name != NULL
17859 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
17860 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
17861 else
17862 STRCPY(sourcing_name, "function ");
17863 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
17864
17865 if (p_verbose >= 12)
17866 {
17867 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017868 verbose_enter_scroll();
17869
Bram Moolenaar555b2802005-05-19 21:08:39 +000017870 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871 if (p_verbose >= 14)
17872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000017874 char_u numbuf[NUMBUFLEN];
17875 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876
17877 msg_puts((char_u *)"(");
17878 for (i = 0; i < argcount; ++i)
17879 {
17880 if (i > 0)
17881 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017882 if (argvars[i].v_type == VAR_NUMBER)
17883 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884 else
17885 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017886 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017887 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017888 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017889 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890 }
17891 }
17892 msg_puts((char_u *)")");
17893 }
17894 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017895
17896 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897 --no_wait_return;
17898 }
17899 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017900#ifdef FEAT_PROFILE
17901 if (do_profiling)
17902 {
17903 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
17904 func_do_profile(fp);
17905 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017906 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000017907 {
17908 ++fp->uf_tm_count;
17909 profile_start(&fp->uf_tm_start);
17910 profile_zero(&fp->uf_tm_children);
17911 }
17912 script_prof_save(&wait_start);
17913 }
17914#endif
17915
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017917 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918 save_did_emsg = did_emsg;
17919 did_emsg = FALSE;
17920
17921 /* call do_cmdline() to execute the lines */
17922 do_cmdline(NULL, get_func_line, (void *)&fc,
17923 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
17924
17925 --RedrawingDisabled;
17926
17927 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017928 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017930 clear_tv(rettv);
17931 rettv->v_type = VAR_NUMBER;
17932 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933 }
17934
Bram Moolenaar05159a02005-02-26 23:04:13 +000017935#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017936 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000017937 {
17938 profile_end(&fp->uf_tm_start);
17939 profile_sub_wait(&wait_start, &fp->uf_tm_start);
17940 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
17941 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
17942 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017943 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017944 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017945 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
17946 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017947 }
17948 }
17949#endif
17950
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 /* when being verbose, mention the return value */
17952 if (p_verbose >= 12)
17953 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017955 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000017958 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017959 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000017960 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
17961 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017962 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017964 char_u buf[MSG_BUF_LEN];
17965 char_u numbuf[NUMBUFLEN];
17966 char_u *tofree;
17967
Bram Moolenaar555b2802005-05-19 21:08:39 +000017968 /* The value may be very long. Skip the middle part, so that we
17969 * have some idea how it starts and ends. smsg() would always
17970 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000017971 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017972 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000017973 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017974 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975 }
17976 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017977
17978 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979 --no_wait_return;
17980 }
17981
17982 vim_free(sourcing_name);
17983 sourcing_name = save_sourcing_name;
17984 sourcing_lnum = save_sourcing_lnum;
17985 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017986#ifdef FEAT_PROFILE
17987 if (do_profiling)
17988 script_prof_restore(&wait_start);
17989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990
17991 if (p_verbose >= 12 && sourcing_name != NULL)
17992 {
17993 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017994 verbose_enter_scroll();
17995
Bram Moolenaar555b2802005-05-19 21:08:39 +000017996 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017998
17999 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018000 --no_wait_return;
18001 }
18002
18003 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018004 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005
Bram Moolenaar33570922005-01-25 22:26:29 +000018006 /* The a: variables typevals were not alloced, only free the allocated
18007 * variables. */
18008 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18009
18010 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 --depth;
18012}
18013
18014/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018015 * Add a number variable "name" to dict "dp" with value "nr".
18016 */
18017 static void
18018add_nr_var(dp, v, name, nr)
18019 dict_T *dp;
18020 dictitem_T *v;
18021 char *name;
18022 varnumber_T nr;
18023{
18024 STRCPY(v->di_key, name);
18025 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18026 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18027 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018028 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018029 v->di_tv.vval.v_number = nr;
18030}
18031
18032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 * ":return [expr]"
18034 */
18035 void
18036ex_return(eap)
18037 exarg_T *eap;
18038{
18039 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018040 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 int returning = FALSE;
18042
18043 if (current_funccal == NULL)
18044 {
18045 EMSG(_("E133: :return not inside a function"));
18046 return;
18047 }
18048
18049 if (eap->skip)
18050 ++emsg_skip;
18051
18052 eap->nextcmd = NULL;
18053 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018054 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 {
18056 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018057 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018059 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 }
18061 /* It's safer to return also on error. */
18062 else if (!eap->skip)
18063 {
18064 /*
18065 * Return unless the expression evaluation has been cancelled due to an
18066 * aborting error, an interrupt, or an exception.
18067 */
18068 if (!aborting())
18069 returning = do_return(eap, FALSE, TRUE, NULL);
18070 }
18071
18072 /* When skipping or the return gets pending, advance to the next command
18073 * in this line (!returning). Otherwise, ignore the rest of the line.
18074 * Following lines will be ignored by get_func_line(). */
18075 if (returning)
18076 eap->nextcmd = NULL;
18077 else if (eap->nextcmd == NULL) /* no argument */
18078 eap->nextcmd = check_nextcmd(arg);
18079
18080 if (eap->skip)
18081 --emsg_skip;
18082}
18083
18084/*
18085 * Return from a function. Possibly makes the return pending. Also called
18086 * for a pending return at the ":endtry" or after returning from an extra
18087 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018088 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018089 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090 * FALSE when the return gets pending.
18091 */
18092 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018093do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 exarg_T *eap;
18095 int reanimate;
18096 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018097 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098{
18099 int idx;
18100 struct condstack *cstack = eap->cstack;
18101
18102 if (reanimate)
18103 /* Undo the return. */
18104 current_funccal->returned = FALSE;
18105
18106 /*
18107 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18108 * not in its finally clause (which then is to be executed next) is found.
18109 * In this case, make the ":return" pending for execution at the ":endtry".
18110 * Otherwise, return normally.
18111 */
18112 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18113 if (idx >= 0)
18114 {
18115 cstack->cs_pending[idx] = CSTP_RETURN;
18116
18117 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018118 /* A pending return again gets pending. "rettv" points to an
18119 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018121 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122 else
18123 {
18124 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018125 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018127 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018129 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130 {
18131 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018132 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018133 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134 else
18135 EMSG(_(e_outofmem));
18136 }
18137 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018138 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139
18140 if (reanimate)
18141 {
18142 /* The pending return value could be overwritten by a ":return"
18143 * without argument in a finally clause; reset the default
18144 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018145 current_funccal->rettv->v_type = VAR_NUMBER;
18146 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 }
18148 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018149 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150 }
18151 else
18152 {
18153 current_funccal->returned = TRUE;
18154
18155 /* If the return is carried out now, store the return value. For
18156 * a return immediately after reanimation, the value is already
18157 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018158 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018160 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018161 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018163 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164 }
18165 }
18166
18167 return idx < 0;
18168}
18169
18170/*
18171 * Free the variable with a pending return value.
18172 */
18173 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018174discard_pending_return(rettv)
18175 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176{
Bram Moolenaar33570922005-01-25 22:26:29 +000018177 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178}
18179
18180/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018181 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182 * is an allocated string. Used by report_pending() for verbose messages.
18183 */
18184 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018185get_return_cmd(rettv)
18186 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018188 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018189 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018190 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018192 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018193 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018194 if (s == NULL)
18195 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018196
18197 STRCPY(IObuff, ":return ");
18198 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18199 if (STRLEN(s) + 8 >= IOSIZE)
18200 STRCPY(IObuff + IOSIZE - 4, "...");
18201 vim_free(tofree);
18202 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203}
18204
18205/*
18206 * Get next function line.
18207 * Called by do_cmdline() to get the next line.
18208 * Returns allocated string, or NULL for end of function.
18209 */
18210/* ARGSUSED */
18211 char_u *
18212get_func_line(c, cookie, indent)
18213 int c; /* not used */
18214 void *cookie;
18215 int indent; /* not used */
18216{
Bram Moolenaar33570922005-01-25 22:26:29 +000018217 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018218 ufunc_T *fp = fcp->func;
18219 char_u *retval;
18220 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221
18222 /* If breakpoints have been added/deleted need to check for it. */
18223 if (fcp->dbg_tick != debug_tick)
18224 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018225 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226 sourcing_lnum);
18227 fcp->dbg_tick = debug_tick;
18228 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018229#ifdef FEAT_PROFILE
18230 if (do_profiling)
18231 func_line_end(cookie);
18232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233
Bram Moolenaar05159a02005-02-26 23:04:13 +000018234 gap = &fp->uf_lines;
18235 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018236 retval = NULL;
18237 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18238 retval = NULL;
18239 else
18240 {
18241 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18242 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018243#ifdef FEAT_PROFILE
18244 if (do_profiling)
18245 func_line_start(cookie);
18246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 }
18248
18249 /* Did we encounter a breakpoint? */
18250 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18251 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018252 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018254 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255 sourcing_lnum);
18256 fcp->dbg_tick = debug_tick;
18257 }
18258
18259 return retval;
18260}
18261
Bram Moolenaar05159a02005-02-26 23:04:13 +000018262#if defined(FEAT_PROFILE) || defined(PROTO)
18263/*
18264 * Called when starting to read a function line.
18265 * "sourcing_lnum" must be correct!
18266 * When skipping lines it may not actually be executed, but we won't find out
18267 * until later and we need to store the time now.
18268 */
18269 void
18270func_line_start(cookie)
18271 void *cookie;
18272{
18273 funccall_T *fcp = (funccall_T *)cookie;
18274 ufunc_T *fp = fcp->func;
18275
18276 if (fp->uf_profiling && sourcing_lnum >= 1
18277 && sourcing_lnum <= fp->uf_lines.ga_len)
18278 {
18279 fp->uf_tml_idx = sourcing_lnum - 1;
18280 fp->uf_tml_execed = FALSE;
18281 profile_start(&fp->uf_tml_start);
18282 profile_zero(&fp->uf_tml_children);
18283 profile_get_wait(&fp->uf_tml_wait);
18284 }
18285}
18286
18287/*
18288 * Called when actually executing a function line.
18289 */
18290 void
18291func_line_exec(cookie)
18292 void *cookie;
18293{
18294 funccall_T *fcp = (funccall_T *)cookie;
18295 ufunc_T *fp = fcp->func;
18296
18297 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18298 fp->uf_tml_execed = TRUE;
18299}
18300
18301/*
18302 * Called when done with a function line.
18303 */
18304 void
18305func_line_end(cookie)
18306 void *cookie;
18307{
18308 funccall_T *fcp = (funccall_T *)cookie;
18309 ufunc_T *fp = fcp->func;
18310
18311 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18312 {
18313 if (fp->uf_tml_execed)
18314 {
18315 ++fp->uf_tml_count[fp->uf_tml_idx];
18316 profile_end(&fp->uf_tml_start);
18317 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18318 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18319 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18320 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18321 }
18322 fp->uf_tml_idx = -1;
18323 }
18324}
18325#endif
18326
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327/*
18328 * Return TRUE if the currently active function should be ended, because a
18329 * return was encountered or an error occured. Used inside a ":while".
18330 */
18331 int
18332func_has_ended(cookie)
18333 void *cookie;
18334{
Bram Moolenaar33570922005-01-25 22:26:29 +000018335 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336
18337 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18338 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018339 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340 || fcp->returned);
18341}
18342
18343/*
18344 * return TRUE if cookie indicates a function which "abort"s on errors.
18345 */
18346 int
18347func_has_abort(cookie)
18348 void *cookie;
18349{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018350 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018351}
18352
18353#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18354typedef enum
18355{
18356 VAR_FLAVOUR_DEFAULT,
18357 VAR_FLAVOUR_SESSION,
18358 VAR_FLAVOUR_VIMINFO
18359} var_flavour_T;
18360
18361static var_flavour_T var_flavour __ARGS((char_u *varname));
18362
18363 static var_flavour_T
18364var_flavour(varname)
18365 char_u *varname;
18366{
18367 char_u *p = varname;
18368
18369 if (ASCII_ISUPPER(*p))
18370 {
18371 while (*(++p))
18372 if (ASCII_ISLOWER(*p))
18373 return VAR_FLAVOUR_SESSION;
18374 return VAR_FLAVOUR_VIMINFO;
18375 }
18376 else
18377 return VAR_FLAVOUR_DEFAULT;
18378}
18379#endif
18380
18381#if defined(FEAT_VIMINFO) || defined(PROTO)
18382/*
18383 * Restore global vars that start with a capital from the viminfo file
18384 */
18385 int
18386read_viminfo_varlist(virp, writing)
18387 vir_T *virp;
18388 int writing;
18389{
18390 char_u *tab;
18391 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018392 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393
18394 if (!writing && (find_viminfo_parameter('!') != NULL))
18395 {
18396 tab = vim_strchr(virp->vir_line + 1, '\t');
18397 if (tab != NULL)
18398 {
18399 *tab++ = '\0'; /* isolate the variable name */
18400 if (*tab == 'S') /* string var */
18401 is_string = TRUE;
18402
18403 tab = vim_strchr(tab, '\t');
18404 if (tab != NULL)
18405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406 if (is_string)
18407 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018408 tv.v_type = VAR_STRING;
18409 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018410 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411 }
18412 else
18413 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018414 tv.v_type = VAR_NUMBER;
18415 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018416 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018417 set_var(virp->vir_line + 1, &tv, FALSE);
18418 if (is_string)
18419 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018420 }
18421 }
18422 }
18423
18424 return viminfo_readline(virp);
18425}
18426
18427/*
18428 * Write global vars that start with a capital to the viminfo file
18429 */
18430 void
18431write_viminfo_varlist(fp)
18432 FILE *fp;
18433{
Bram Moolenaar33570922005-01-25 22:26:29 +000018434 hashitem_T *hi;
18435 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018436 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018437 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018438 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018439 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018440 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441
18442 if (find_viminfo_parameter('!') == NULL)
18443 return;
18444
18445 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018446
Bram Moolenaar33570922005-01-25 22:26:29 +000018447 todo = globvarht.ht_used;
18448 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018450 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018452 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018453 this_var = HI2DI(hi);
18454 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018455 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018456 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018457 {
18458 case VAR_STRING: s = "STR"; break;
18459 case VAR_NUMBER: s = "NUM"; break;
18460 default: continue;
18461 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018462 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018463 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18464 if (p != NULL)
18465 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018466 vim_free(tofree);
18467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468 }
18469 }
18470}
18471#endif
18472
18473#if defined(FEAT_SESSION) || defined(PROTO)
18474 int
18475store_session_globals(fd)
18476 FILE *fd;
18477{
Bram Moolenaar33570922005-01-25 22:26:29 +000018478 hashitem_T *hi;
18479 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018480 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481 char_u *p, *t;
18482
Bram Moolenaar33570922005-01-25 22:26:29 +000018483 todo = globvarht.ht_used;
18484 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018486 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018488 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018489 this_var = HI2DI(hi);
18490 if ((this_var->di_tv.v_type == VAR_NUMBER
18491 || this_var->di_tv.v_type == VAR_STRING)
18492 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018493 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018494 /* Escape special characters with a backslash. Turn a LF and
18495 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018496 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018497 (char_u *)"\\\"\n\r");
18498 if (p == NULL) /* out of memory */
18499 break;
18500 for (t = p; *t != NUL; ++t)
18501 if (*t == '\n')
18502 *t = 'n';
18503 else if (*t == '\r')
18504 *t = 'r';
18505 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000018506 this_var->di_key,
18507 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18508 : ' ',
18509 p,
18510 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18511 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000018512 || put_eol(fd) == FAIL)
18513 {
18514 vim_free(p);
18515 return FAIL;
18516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517 vim_free(p);
18518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519 }
18520 }
18521 return OK;
18522}
18523#endif
18524
18525#endif /* FEAT_EVAL */
18526
18527#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
18528
18529
18530#ifdef WIN3264
18531/*
18532 * Functions for ":8" filename modifier: get 8.3 version of a filename.
18533 */
18534static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18535static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
18536static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18537
18538/*
18539 * Get the short pathname of a file.
18540 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
18541 */
18542 static int
18543get_short_pathname(fnamep, bufp, fnamelen)
18544 char_u **fnamep;
18545 char_u **bufp;
18546 int *fnamelen;
18547{
18548 int l,len;
18549 char_u *newbuf;
18550
18551 len = *fnamelen;
18552
18553 l = GetShortPathName(*fnamep, *fnamep, len);
18554 if (l > len - 1)
18555 {
18556 /* If that doesn't work (not enough space), then save the string
18557 * and try again with a new buffer big enough
18558 */
18559 newbuf = vim_strnsave(*fnamep, l);
18560 if (newbuf == NULL)
18561 return 0;
18562
18563 vim_free(*bufp);
18564 *fnamep = *bufp = newbuf;
18565
18566 l = GetShortPathName(*fnamep,*fnamep,l+1);
18567
18568 /* Really should always succeed, as the buffer is big enough */
18569 }
18570
18571 *fnamelen = l;
18572 return 1;
18573}
18574
18575/*
18576 * Create a short path name. Returns the length of the buffer it needs.
18577 * Doesn't copy over the end of the buffer passed in.
18578 */
18579 static int
18580shortpath_for_invalid_fname(fname, bufp, fnamelen)
18581 char_u **fname;
18582 char_u **bufp;
18583 int *fnamelen;
18584{
18585 char_u *s, *p, *pbuf2, *pbuf3;
18586 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018587 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588
18589 /* Make a copy */
18590 len2 = *fnamelen;
18591 pbuf2 = vim_strnsave(*fname, len2);
18592 pbuf3 = NULL;
18593
18594 s = pbuf2 + len2 - 1; /* Find the end */
18595 slen = 1;
18596 plen = len2;
18597
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018598 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018599 {
18600 --s;
18601 ++slen;
18602 --plen;
18603 }
18604
18605 do
18606 {
18607 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018608 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609 {
18610 --s;
18611 ++slen;
18612 --plen;
18613 }
18614 if (s <= pbuf2)
18615 break;
18616
18617 /* Remeber the character that is about to be blatted */
18618 ch = *s;
18619 *s = 0; /* get_short_pathname requires a null-terminated string */
18620
18621 /* Try it in situ */
18622 p = pbuf2;
18623 if (!get_short_pathname(&p, &pbuf3, &plen))
18624 {
18625 vim_free(pbuf2);
18626 return -1;
18627 }
18628 *s = ch; /* Preserve the string */
18629 } while (plen == 0);
18630
18631 if (plen > 0)
18632 {
18633 /* Remeber the length of the new string. */
18634 *fnamelen = len = plen + slen;
18635 vim_free(*bufp);
18636 if (len > len2)
18637 {
18638 /* If there's not enough space in the currently allocated string,
18639 * then copy it to a buffer big enough.
18640 */
18641 *fname= *bufp = vim_strnsave(p, len);
18642 if (*fname == NULL)
18643 return -1;
18644 }
18645 else
18646 {
18647 /* Transfer pbuf2 to being the main buffer (it's big enough) */
18648 *fname = *bufp = pbuf2;
18649 if (p != pbuf2)
18650 strncpy(*fname, p, plen);
18651 pbuf2 = NULL;
18652 }
18653 /* Concat the next bit */
18654 strncpy(*fname + plen, s, slen);
18655 (*fname)[len] = '\0';
18656 }
18657 vim_free(pbuf3);
18658 vim_free(pbuf2);
18659 return 0;
18660}
18661
18662/*
18663 * Get a pathname for a partial path.
18664 */
18665 static int
18666shortpath_for_partial(fnamep, bufp, fnamelen)
18667 char_u **fnamep;
18668 char_u **bufp;
18669 int *fnamelen;
18670{
18671 int sepcount, len, tflen;
18672 char_u *p;
18673 char_u *pbuf, *tfname;
18674 int hasTilde;
18675
18676 /* Count up the path seperators from the RHS.. so we know which part
18677 * of the path to return.
18678 */
18679 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018680 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681 if (vim_ispathsep(*p))
18682 ++sepcount;
18683
18684 /* Need full path first (use expand_env() to remove a "~/") */
18685 hasTilde = (**fnamep == '~');
18686 if (hasTilde)
18687 pbuf = tfname = expand_env_save(*fnamep);
18688 else
18689 pbuf = tfname = FullName_save(*fnamep, FALSE);
18690
18691 len = tflen = STRLEN(tfname);
18692
18693 if (!get_short_pathname(&tfname, &pbuf, &len))
18694 return -1;
18695
18696 if (len == 0)
18697 {
18698 /* Don't have a valid filename, so shorten the rest of the
18699 * path if we can. This CAN give us invalid 8.3 filenames, but
18700 * there's not a lot of point in guessing what it might be.
18701 */
18702 len = tflen;
18703 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
18704 return -1;
18705 }
18706
18707 /* Count the paths backward to find the beginning of the desired string. */
18708 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018709 {
18710#ifdef FEAT_MBYTE
18711 if (has_mbyte)
18712 p -= mb_head_off(tfname, p);
18713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018714 if (vim_ispathsep(*p))
18715 {
18716 if (sepcount == 0 || (hasTilde && sepcount == 1))
18717 break;
18718 else
18719 sepcount --;
18720 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722 if (hasTilde)
18723 {
18724 --p;
18725 if (p >= tfname)
18726 *p = '~';
18727 else
18728 return -1;
18729 }
18730 else
18731 ++p;
18732
18733 /* Copy in the string - p indexes into tfname - allocated at pbuf */
18734 vim_free(*bufp);
18735 *fnamelen = (int)STRLEN(p);
18736 *bufp = pbuf;
18737 *fnamep = p;
18738
18739 return 0;
18740}
18741#endif /* WIN3264 */
18742
18743/*
18744 * Adjust a filename, according to a string of modifiers.
18745 * *fnamep must be NUL terminated when called. When returning, the length is
18746 * determined by *fnamelen.
18747 * Returns valid flags.
18748 * When there is an error, *fnamep is set to NULL.
18749 */
18750 int
18751modify_fname(src, usedlen, fnamep, bufp, fnamelen)
18752 char_u *src; /* string with modifiers */
18753 int *usedlen; /* characters after src that are used */
18754 char_u **fnamep; /* file name so far */
18755 char_u **bufp; /* buffer for allocated file name or NULL */
18756 int *fnamelen; /* length of fnamep */
18757{
18758 int valid = 0;
18759 char_u *tail;
18760 char_u *s, *p, *pbuf;
18761 char_u dirname[MAXPATHL];
18762 int c;
18763 int has_fullname = 0;
18764#ifdef WIN3264
18765 int has_shortname = 0;
18766#endif
18767
18768repeat:
18769 /* ":p" - full path/file_name */
18770 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
18771 {
18772 has_fullname = 1;
18773
18774 valid |= VALID_PATH;
18775 *usedlen += 2;
18776
18777 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
18778 if ((*fnamep)[0] == '~'
18779#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
18780 && ((*fnamep)[1] == '/'
18781# ifdef BACKSLASH_IN_FILENAME
18782 || (*fnamep)[1] == '\\'
18783# endif
18784 || (*fnamep)[1] == NUL)
18785
18786#endif
18787 )
18788 {
18789 *fnamep = expand_env_save(*fnamep);
18790 vim_free(*bufp); /* free any allocated file name */
18791 *bufp = *fnamep;
18792 if (*fnamep == NULL)
18793 return -1;
18794 }
18795
18796 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018797 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 {
18799 if (vim_ispathsep(*p)
18800 && p[1] == '.'
18801 && (p[2] == NUL
18802 || vim_ispathsep(p[2])
18803 || (p[2] == '.'
18804 && (p[3] == NUL || vim_ispathsep(p[3])))))
18805 break;
18806 }
18807
18808 /* FullName_save() is slow, don't use it when not needed. */
18809 if (*p != NUL || !vim_isAbsName(*fnamep))
18810 {
18811 *fnamep = FullName_save(*fnamep, *p != NUL);
18812 vim_free(*bufp); /* free any allocated file name */
18813 *bufp = *fnamep;
18814 if (*fnamep == NULL)
18815 return -1;
18816 }
18817
18818 /* Append a path separator to a directory. */
18819 if (mch_isdir(*fnamep))
18820 {
18821 /* Make room for one or two extra characters. */
18822 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
18823 vim_free(*bufp); /* free any allocated file name */
18824 *bufp = *fnamep;
18825 if (*fnamep == NULL)
18826 return -1;
18827 add_pathsep(*fnamep);
18828 }
18829 }
18830
18831 /* ":." - path relative to the current directory */
18832 /* ":~" - path relative to the home directory */
18833 /* ":8" - shortname path - postponed till after */
18834 while (src[*usedlen] == ':'
18835 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
18836 {
18837 *usedlen += 2;
18838 if (c == '8')
18839 {
18840#ifdef WIN3264
18841 has_shortname = 1; /* Postpone this. */
18842#endif
18843 continue;
18844 }
18845 pbuf = NULL;
18846 /* Need full path first (use expand_env() to remove a "~/") */
18847 if (!has_fullname)
18848 {
18849 if (c == '.' && **fnamep == '~')
18850 p = pbuf = expand_env_save(*fnamep);
18851 else
18852 p = pbuf = FullName_save(*fnamep, FALSE);
18853 }
18854 else
18855 p = *fnamep;
18856
18857 has_fullname = 0;
18858
18859 if (p != NULL)
18860 {
18861 if (c == '.')
18862 {
18863 mch_dirname(dirname, MAXPATHL);
18864 s = shorten_fname(p, dirname);
18865 if (s != NULL)
18866 {
18867 *fnamep = s;
18868 if (pbuf != NULL)
18869 {
18870 vim_free(*bufp); /* free any allocated file name */
18871 *bufp = pbuf;
18872 pbuf = NULL;
18873 }
18874 }
18875 }
18876 else
18877 {
18878 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
18879 /* Only replace it when it starts with '~' */
18880 if (*dirname == '~')
18881 {
18882 s = vim_strsave(dirname);
18883 if (s != NULL)
18884 {
18885 *fnamep = s;
18886 vim_free(*bufp);
18887 *bufp = s;
18888 }
18889 }
18890 }
18891 vim_free(pbuf);
18892 }
18893 }
18894
18895 tail = gettail(*fnamep);
18896 *fnamelen = (int)STRLEN(*fnamep);
18897
18898 /* ":h" - head, remove "/file_name", can be repeated */
18899 /* Don't remove the first "/" or "c:\" */
18900 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
18901 {
18902 valid |= VALID_HEAD;
18903 *usedlen += 2;
18904 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018905 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906 --tail;
18907 *fnamelen = (int)(tail - *fnamep);
18908#ifdef VMS
18909 if (*fnamelen > 0)
18910 *fnamelen += 1; /* the path separator is part of the path */
18911#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018912 while (tail > s && !after_pathsep(s, tail))
18913 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914 }
18915
18916 /* ":8" - shortname */
18917 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
18918 {
18919 *usedlen += 2;
18920#ifdef WIN3264
18921 has_shortname = 1;
18922#endif
18923 }
18924
18925#ifdef WIN3264
18926 /* Check shortname after we have done 'heads' and before we do 'tails'
18927 */
18928 if (has_shortname)
18929 {
18930 pbuf = NULL;
18931 /* Copy the string if it is shortened by :h */
18932 if (*fnamelen < (int)STRLEN(*fnamep))
18933 {
18934 p = vim_strnsave(*fnamep, *fnamelen);
18935 if (p == 0)
18936 return -1;
18937 vim_free(*bufp);
18938 *bufp = *fnamep = p;
18939 }
18940
18941 /* Split into two implementations - makes it easier. First is where
18942 * there isn't a full name already, second is where there is.
18943 */
18944 if (!has_fullname && !vim_isAbsName(*fnamep))
18945 {
18946 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
18947 return -1;
18948 }
18949 else
18950 {
18951 int l;
18952
18953 /* Simple case, already have the full-name
18954 * Nearly always shorter, so try first time. */
18955 l = *fnamelen;
18956 if (!get_short_pathname(fnamep, bufp, &l))
18957 return -1;
18958
18959 if (l == 0)
18960 {
18961 /* Couldn't find the filename.. search the paths.
18962 */
18963 l = *fnamelen;
18964 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
18965 return -1;
18966 }
18967 *fnamelen = l;
18968 }
18969 }
18970#endif /* WIN3264 */
18971
18972 /* ":t" - tail, just the basename */
18973 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
18974 {
18975 *usedlen += 2;
18976 *fnamelen -= (int)(tail - *fnamep);
18977 *fnamep = tail;
18978 }
18979
18980 /* ":e" - extension, can be repeated */
18981 /* ":r" - root, without extension, can be repeated */
18982 while (src[*usedlen] == ':'
18983 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
18984 {
18985 /* find a '.' in the tail:
18986 * - for second :e: before the current fname
18987 * - otherwise: The last '.'
18988 */
18989 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
18990 s = *fnamep - 2;
18991 else
18992 s = *fnamep + *fnamelen - 1;
18993 for ( ; s > tail; --s)
18994 if (s[0] == '.')
18995 break;
18996 if (src[*usedlen + 1] == 'e') /* :e */
18997 {
18998 if (s > tail)
18999 {
19000 *fnamelen += (int)(*fnamep - (s + 1));
19001 *fnamep = s + 1;
19002#ifdef VMS
19003 /* cut version from the extension */
19004 s = *fnamep + *fnamelen - 1;
19005 for ( ; s > *fnamep; --s)
19006 if (s[0] == ';')
19007 break;
19008 if (s > *fnamep)
19009 *fnamelen = s - *fnamep;
19010#endif
19011 }
19012 else if (*fnamep <= tail)
19013 *fnamelen = 0;
19014 }
19015 else /* :r */
19016 {
19017 if (s > tail) /* remove one extension */
19018 *fnamelen = (int)(s - *fnamep);
19019 }
19020 *usedlen += 2;
19021 }
19022
19023 /* ":s?pat?foo?" - substitute */
19024 /* ":gs?pat?foo?" - global substitute */
19025 if (src[*usedlen] == ':'
19026 && (src[*usedlen + 1] == 's'
19027 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19028 {
19029 char_u *str;
19030 char_u *pat;
19031 char_u *sub;
19032 int sep;
19033 char_u *flags;
19034 int didit = FALSE;
19035
19036 flags = (char_u *)"";
19037 s = src + *usedlen + 2;
19038 if (src[*usedlen + 1] == 'g')
19039 {
19040 flags = (char_u *)"g";
19041 ++s;
19042 }
19043
19044 sep = *s++;
19045 if (sep)
19046 {
19047 /* find end of pattern */
19048 p = vim_strchr(s, sep);
19049 if (p != NULL)
19050 {
19051 pat = vim_strnsave(s, (int)(p - s));
19052 if (pat != NULL)
19053 {
19054 s = p + 1;
19055 /* find end of substitution */
19056 p = vim_strchr(s, sep);
19057 if (p != NULL)
19058 {
19059 sub = vim_strnsave(s, (int)(p - s));
19060 str = vim_strnsave(*fnamep, *fnamelen);
19061 if (sub != NULL && str != NULL)
19062 {
19063 *usedlen = (int)(p + 1 - src);
19064 s = do_string_sub(str, pat, sub, flags);
19065 if (s != NULL)
19066 {
19067 *fnamep = s;
19068 *fnamelen = (int)STRLEN(s);
19069 vim_free(*bufp);
19070 *bufp = s;
19071 didit = TRUE;
19072 }
19073 }
19074 vim_free(sub);
19075 vim_free(str);
19076 }
19077 vim_free(pat);
19078 }
19079 }
19080 /* after using ":s", repeat all the modifiers */
19081 if (didit)
19082 goto repeat;
19083 }
19084 }
19085
19086 return valid;
19087}
19088
19089/*
19090 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19091 * "flags" can be "g" to do a global substitute.
19092 * Returns an allocated string, NULL for error.
19093 */
19094 char_u *
19095do_string_sub(str, pat, sub, flags)
19096 char_u *str;
19097 char_u *pat;
19098 char_u *sub;
19099 char_u *flags;
19100{
19101 int sublen;
19102 regmatch_T regmatch;
19103 int i;
19104 int do_all;
19105 char_u *tail;
19106 garray_T ga;
19107 char_u *ret;
19108 char_u *save_cpo;
19109
19110 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19111 save_cpo = p_cpo;
19112 p_cpo = (char_u *)"";
19113
19114 ga_init2(&ga, 1, 200);
19115
19116 do_all = (flags[0] == 'g');
19117
19118 regmatch.rm_ic = p_ic;
19119 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19120 if (regmatch.regprog != NULL)
19121 {
19122 tail = str;
19123 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19124 {
19125 /*
19126 * Get some space for a temporary buffer to do the substitution
19127 * into. It will contain:
19128 * - The text up to where the match is.
19129 * - The substituted text.
19130 * - The text after the match.
19131 */
19132 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19133 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19134 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19135 {
19136 ga_clear(&ga);
19137 break;
19138 }
19139
19140 /* copy the text up to where the match is */
19141 i = (int)(regmatch.startp[0] - tail);
19142 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19143 /* add the substituted text */
19144 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19145 + ga.ga_len + i, TRUE, TRUE, FALSE);
19146 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147 /* avoid getting stuck on a match with an empty string */
19148 if (tail == regmatch.endp[0])
19149 {
19150 if (*tail == NUL)
19151 break;
19152 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19153 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154 }
19155 else
19156 {
19157 tail = regmatch.endp[0];
19158 if (*tail == NUL)
19159 break;
19160 }
19161 if (!do_all)
19162 break;
19163 }
19164
19165 if (ga.ga_data != NULL)
19166 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19167
19168 vim_free(regmatch.regprog);
19169 }
19170
19171 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19172 ga_clear(&ga);
19173 p_cpo = save_cpo;
19174
19175 return ret;
19176}
19177
19178#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */