blob: f2cc2a0b7b81544e20ca1c2792165acfe2fbf7b9 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000340};
341
342/* shorthand */
343#define vv_type vv_di.di_tv.v_type
344#define vv_nr vv_di.di_tv.vval.v_number
345#define vv_str vv_di.di_tv.vval.v_string
346#define vv_tv vv_di.di_tv
347
348/*
349 * The v: variables are stored in dictionary "vimvardict".
350 * "vimvars_var" is the variable that is used for the "l:" scope.
351 */
352static dict_T vimvardict;
353static dictitem_T vimvars_var;
354#define vimvarht vimvardict.dv_hashtab
355
Bram Moolenaara40058a2005-07-11 22:42:07 +0000356static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
357static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
358#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
359static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
360#endif
361static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
362static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
363static char_u *skip_var_one __ARGS((char_u *arg));
364static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
365static void list_glob_vars __ARGS((void));
366static void list_buf_vars __ARGS((void));
367static void list_win_vars __ARGS((void));
368static void list_vim_vars __ARGS((void));
369static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
370static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
371static int check_changedtick __ARGS((char_u *arg));
372static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
373static void clear_lval __ARGS((lval_T *lp));
374static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
375static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
376static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
377static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
378static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
379static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
380static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
381static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
382static void item_lock __ARGS((typval_T *tv, int deep, int lock));
383static int tv_islocked __ARGS((typval_T *tv));
384
Bram Moolenaar33570922005-01-25 22:26:29 +0000385static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
386static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
387static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
388static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
389static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
390static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000394static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000395static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
397static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
399static list_T *list_alloc __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000400static void list_free __ARGS((list_T *l));
401static listitem_T *listitem_alloc __ARGS((void));
402static void listitem_free __ARGS((listitem_T *item));
403static void listitem_remove __ARGS((list_T *l, listitem_T *item));
404static long list_len __ARGS((list_T *l));
405static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
406static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
407static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000408static listitem_T *list_find __ARGS((list_T *l, long n));
409static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static void list_append __ARGS((list_T *l, listitem_T *item));
411static int list_append_tv __ARGS((list_T *l, typval_T *tv));
412static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
413static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
414static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000415static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
417static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000418static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000419static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
420static void set_ref_in_list __ARGS((list_T *l, int copyID));
421static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static void dict_unref __ARGS((dict_T *d));
423static void dict_free __ARGS((dict_T *d));
424static dictitem_T *dictitem_alloc __ARGS((char_u *key));
425static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
426static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
427static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000428static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static int dict_add __ARGS((dict_T *d, dictitem_T *item));
430static long dict_len __ARGS((dict_T *d));
431static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
432static char_u *dict2string __ARGS((typval_T *tv));
433static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
435static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
436static char_u *string_quote __ARGS((char_u *str, int function));
437static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
438static int find_internal_func __ARGS((char_u *name));
439static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
440static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
441static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000442static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443
444static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
468static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000494static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000496static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000509static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000537static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000554static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000558#ifdef vim_mkdir
559static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
560#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000566static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000584static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000589static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000590static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
593#ifdef HAVE_STRFTIME
594static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
595#endif
596static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000608static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000624static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000625
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
627static int get_env_len __ARGS((char_u **arg));
628static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000629static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000630static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
631#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
632#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
633 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000634static 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 +0000635static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000636static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000637static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
638static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static typval_T *alloc_tv __ARGS((void));
640static typval_T *alloc_string_tv __ARGS((char_u *string));
641static void free_tv __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000642static 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 Moolenaar87e25fd2005-07-27 21:13:01 +0000685static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000686static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000687static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void func_free __ARGS((ufunc_T *fp));
689static void func_unref __ARGS((char_u *name));
690static void func_ref __ARGS((char_u *name));
691static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
692static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
693
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000694/* Character used as separated in autoload function/variable names. */
695#define AUTOLOAD_CHAR '#'
696
Bram Moolenaar33570922005-01-25 22:26:29 +0000697/*
698 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000699 */
700 void
701eval_init()
702{
Bram Moolenaar33570922005-01-25 22:26:29 +0000703 int i;
704 struct vimvar *p;
705
706 init_var_dict(&globvardict, &globvars_var);
707 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000708 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000709 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000710
711 for (i = 0; i < VV_LEN; ++i)
712 {
713 p = &vimvars[i];
714 STRCPY(p->vv_di.di_key, p->vv_name);
715 if (p->vv_flags & VV_RO)
716 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
717 else if (p->vv_flags & VV_RO_SBX)
718 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
719 else
720 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000721
722 /* add to v: scope dict, unless the value is not always available */
723 if (p->vv_type != VAR_UNKNOWN)
724 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000725 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000726 /* add to compat scope dict */
727 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000728 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000729}
730
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000731#if defined(EXITFREE) || defined(PROTO)
732 void
733eval_clear()
734{
735 int i;
736 struct vimvar *p;
737
738 for (i = 0; i < VV_LEN; ++i)
739 {
740 p = &vimvars[i];
741 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000742 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000743 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000744 p->vv_di.di_tv.vval.v_string = NULL;
745 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000746 }
747 hash_clear(&vimvarht);
748 hash_clear(&compat_hashtab);
749
750 /* script-local variables */
751 for (i = 1; i <= ga_scripts.ga_len; ++i)
752 vars_clear(&SCRIPT_VARS(i));
753 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000754 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000755
756 /* global variables */
757 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000758
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000759 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000760 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000761 hash_clear(&func_hashtab);
762
763 /* unreferenced lists and dicts */
764 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000765}
766#endif
767
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 * Return the name of the executed function.
770 */
771 char_u *
772func_name(cookie)
773 void *cookie;
774{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000775 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776}
777
778/*
779 * Return the address holding the next breakpoint line for a funccall cookie.
780 */
781 linenr_T *
782func_breakpoint(cookie)
783 void *cookie;
784{
Bram Moolenaar33570922005-01-25 22:26:29 +0000785 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786}
787
788/*
789 * Return the address holding the debug tick for a funccall cookie.
790 */
791 int *
792func_dbg_tick(cookie)
793 void *cookie;
794{
Bram Moolenaar33570922005-01-25 22:26:29 +0000795 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797
798/*
799 * Return the nesting level for a funccall cookie.
800 */
801 int
802func_level(cookie)
803 void *cookie;
804{
Bram Moolenaar33570922005-01-25 22:26:29 +0000805 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806}
807
808/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000809funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
811/*
812 * Return TRUE when a function was ended by a ":return" command.
813 */
814 int
815current_func_returned()
816{
817 return current_funccal->returned;
818}
819
820
821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 * Set an internal variable to a string value. Creates the variable if it does
823 * not already exist.
824 */
825 void
826set_internal_string_var(name, value)
827 char_u *name;
828 char_u *value;
829{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000830 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000831 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
833 val = vim_strsave(value);
834 if (val != NULL)
835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000836 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000837 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000839 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000840 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 }
842 }
843}
844
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000845static lval_T *redir_lval = NULL;
846static char_u *redir_endp = NULL;
847static char_u *redir_varname = NULL;
848
849/*
850 * Start recording command output to a variable
851 * Returns OK if successfully completed the setup. FAIL otherwise.
852 */
853 int
854var_redir_start(name, append)
855 char_u *name;
856 int append; /* append to an existing variable */
857{
858 int save_emsg;
859 int err;
860 typval_T tv;
861
862 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000863 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000864 {
865 EMSG(_(e_invarg));
866 return FAIL;
867 }
868
869 redir_varname = vim_strsave(name);
870 if (redir_varname == NULL)
871 return FAIL;
872
873 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
874 if (redir_lval == NULL)
875 {
876 var_redir_stop();
877 return FAIL;
878 }
879
880 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000881 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
882 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000883 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
884 {
885 if (redir_endp != NULL && *redir_endp != NUL)
886 /* Trailing characters are present after the variable name */
887 EMSG(_(e_trailing));
888 else
889 EMSG(_(e_invarg));
890 var_redir_stop();
891 return FAIL;
892 }
893
894 /* check if we can write to the variable: set it to or append an empty
895 * string */
896 save_emsg = did_emsg;
897 did_emsg = FALSE;
898 tv.v_type = VAR_STRING;
899 tv.vval.v_string = (char_u *)"";
900 if (append)
901 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
902 else
903 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
904 err = did_emsg;
905 did_emsg += save_emsg;
906 if (err)
907 {
908 var_redir_stop();
909 return FAIL;
910 }
911 if (redir_lval->ll_newkey != NULL)
912 {
913 /* Dictionary item was created, don't do it again. */
914 vim_free(redir_lval->ll_newkey);
915 redir_lval->ll_newkey = NULL;
916 }
917
918 return OK;
919}
920
921/*
922 * Append "value[len]" to the variable set by var_redir_start().
923 */
924 void
925var_redir_str(value, len)
926 char_u *value;
927 int len;
928{
929 char_u *val;
930 typval_T tv;
931 int save_emsg;
932 int err;
933
934 if (redir_lval == NULL)
935 return;
936
937 if (len == -1)
938 /* Append the entire string */
939 val = vim_strsave(value);
940 else
941 /* Append only the specified number of characters */
942 val = vim_strnsave(value, len);
943 if (val == NULL)
944 return;
945
946 tv.v_type = VAR_STRING;
947 tv.vval.v_string = val;
948
949 save_emsg = did_emsg;
950 did_emsg = FALSE;
951 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
952 err = did_emsg;
953 did_emsg += save_emsg;
954 if (err)
955 var_redir_stop();
956
957 vim_free(tv.vval.v_string);
958}
959
960/*
961 * Stop redirecting command output to a variable.
962 */
963 void
964var_redir_stop()
965{
966 if (redir_lval != NULL)
967 {
968 clear_lval(redir_lval);
969 vim_free(redir_lval);
970 redir_lval = NULL;
971 }
972 vim_free(redir_varname);
973 redir_varname = NULL;
974}
975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976# if defined(FEAT_MBYTE) || defined(PROTO)
977 int
978eval_charconvert(enc_from, enc_to, fname_from, fname_to)
979 char_u *enc_from;
980 char_u *enc_to;
981 char_u *fname_from;
982 char_u *fname_to;
983{
984 int err = FALSE;
985
986 set_vim_var_string(VV_CC_FROM, enc_from, -1);
987 set_vim_var_string(VV_CC_TO, enc_to, -1);
988 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
989 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
990 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
991 err = TRUE;
992 set_vim_var_string(VV_CC_FROM, NULL, -1);
993 set_vim_var_string(VV_CC_TO, NULL, -1);
994 set_vim_var_string(VV_FNAME_IN, NULL, -1);
995 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
996
997 if (err)
998 return FAIL;
999 return OK;
1000}
1001# endif
1002
1003# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1004 int
1005eval_printexpr(fname, args)
1006 char_u *fname;
1007 char_u *args;
1008{
1009 int err = FALSE;
1010
1011 set_vim_var_string(VV_FNAME_IN, fname, -1);
1012 set_vim_var_string(VV_CMDARG, args, -1);
1013 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1014 err = TRUE;
1015 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1016 set_vim_var_string(VV_CMDARG, NULL, -1);
1017
1018 if (err)
1019 {
1020 mch_remove(fname);
1021 return FAIL;
1022 }
1023 return OK;
1024}
1025# endif
1026
1027# if defined(FEAT_DIFF) || defined(PROTO)
1028 void
1029eval_diff(origfile, newfile, outfile)
1030 char_u *origfile;
1031 char_u *newfile;
1032 char_u *outfile;
1033{
1034 int err = FALSE;
1035
1036 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1037 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1038 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1039 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1040 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1041 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1042 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1043}
1044
1045 void
1046eval_patch(origfile, difffile, outfile)
1047 char_u *origfile;
1048 char_u *difffile;
1049 char_u *outfile;
1050{
1051 int err;
1052
1053 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1054 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1055 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1056 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1057 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1058 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1059 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1060}
1061# endif
1062
1063/*
1064 * Top level evaluation function, returning a boolean.
1065 * Sets "error" to TRUE if there was an error.
1066 * Return TRUE or FALSE.
1067 */
1068 int
1069eval_to_bool(arg, error, nextcmd, skip)
1070 char_u *arg;
1071 int *error;
1072 char_u **nextcmd;
1073 int skip; /* only parse, don't execute */
1074{
Bram Moolenaar33570922005-01-25 22:26:29 +00001075 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 int retval = FALSE;
1077
1078 if (skip)
1079 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001080 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 else
1083 {
1084 *error = FALSE;
1085 if (!skip)
1086 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001087 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001088 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090 }
1091 if (skip)
1092 --emsg_skip;
1093
1094 return retval;
1095}
1096
1097/*
1098 * Top level evaluation function, returning a string. If "skip" is TRUE,
1099 * only parsing to "nextcmd" is done, without reporting errors. Return
1100 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1101 */
1102 char_u *
1103eval_to_string_skip(arg, nextcmd, skip)
1104 char_u *arg;
1105 char_u **nextcmd;
1106 int skip; /* only parse, don't execute */
1107{
Bram Moolenaar33570922005-01-25 22:26:29 +00001108 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 char_u *retval;
1110
1111 if (skip)
1112 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001113 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 retval = NULL;
1115 else
1116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001117 retval = vim_strsave(get_tv_string(&tv));
1118 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
1120 if (skip)
1121 --emsg_skip;
1122
1123 return retval;
1124}
1125
1126/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001127 * Skip over an expression at "*pp".
1128 * Return FAIL for an error, OK otherwise.
1129 */
1130 int
1131skip_expr(pp)
1132 char_u **pp;
1133{
Bram Moolenaar33570922005-01-25 22:26:29 +00001134 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001135
1136 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001137 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001138}
1139
1140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 * Top level evaluation function, returning a string.
1142 * Return pointer to allocated memory, or NULL for failure.
1143 */
1144 char_u *
1145eval_to_string(arg, nextcmd)
1146 char_u *arg;
1147 char_u **nextcmd;
1148{
Bram Moolenaar33570922005-01-25 22:26:29 +00001149 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 char_u *retval;
1151
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001152 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 retval = NULL;
1154 else
1155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001156 retval = vim_strsave(get_tv_string(&tv));
1157 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 }
1159
1160 return retval;
1161}
1162
1163/*
1164 * Call eval_to_string() with "sandbox" set and not using local variables.
1165 */
1166 char_u *
1167eval_to_string_safe(arg, nextcmd)
1168 char_u *arg;
1169 char_u **nextcmd;
1170{
1171 char_u *retval;
1172 void *save_funccalp;
1173
1174 save_funccalp = save_funccal();
1175 ++sandbox;
1176 retval = eval_to_string(arg, nextcmd);
1177 --sandbox;
1178 restore_funccal(save_funccalp);
1179 return retval;
1180}
1181
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182/*
1183 * Top level evaluation function, returning a number.
1184 * Evaluates "expr" silently.
1185 * Returns -1 for an error.
1186 */
1187 int
1188eval_to_number(expr)
1189 char_u *expr;
1190{
Bram Moolenaar33570922005-01-25 22:26:29 +00001191 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001193 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
1195 ++emsg_off;
1196
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001197 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 retval = -1;
1199 else
1200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001201 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 }
1204 --emsg_off;
1205
1206 return retval;
1207}
1208
Bram Moolenaara40058a2005-07-11 22:42:07 +00001209/*
1210 * Prepare v: variable "idx" to be used.
1211 * Save the current typeval in "save_tv".
1212 * When not used yet add the variable to the v: hashtable.
1213 */
1214 static void
1215prepare_vimvar(idx, save_tv)
1216 int idx;
1217 typval_T *save_tv;
1218{
1219 *save_tv = vimvars[idx].vv_tv;
1220 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1221 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1222}
1223
1224/*
1225 * Restore v: variable "idx" to typeval "save_tv".
1226 * When no longer defined, remove the variable from the v: hashtable.
1227 */
1228 static void
1229restore_vimvar(idx, save_tv)
1230 int idx;
1231 typval_T *save_tv;
1232{
1233 hashitem_T *hi;
1234
1235 clear_tv(&vimvars[idx].vv_tv);
1236 vimvars[idx].vv_tv = *save_tv;
1237 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1238 {
1239 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1240 if (HASHITEM_EMPTY(hi))
1241 EMSG2(_(e_intern2), "restore_vimvar()");
1242 else
1243 hash_remove(&vimvarht, hi);
1244 }
1245}
1246
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001247#if defined(FEAT_SYN_HL) || defined(PROTO)
1248/*
1249 * Evaluate an expression to a list with suggestions.
1250 * For the "expr:" part of 'spellsuggest'.
1251 */
1252 list_T *
1253eval_spell_expr(badword, expr)
1254 char_u *badword;
1255 char_u *expr;
1256{
1257 typval_T save_val;
1258 typval_T rettv;
1259 list_T *list = NULL;
1260 char_u *p = skipwhite(expr);
1261
1262 /* Set "v:val" to the bad word. */
1263 prepare_vimvar(VV_VAL, &save_val);
1264 vimvars[VV_VAL].vv_type = VAR_STRING;
1265 vimvars[VV_VAL].vv_str = badword;
1266 if (p_verbose == 0)
1267 ++emsg_off;
1268
1269 if (eval1(&p, &rettv, TRUE) == OK)
1270 {
1271 if (rettv.v_type != VAR_LIST)
1272 clear_tv(&rettv);
1273 else
1274 list = rettv.vval.v_list;
1275 }
1276
1277 if (p_verbose == 0)
1278 --emsg_off;
1279 vimvars[VV_VAL].vv_str = NULL;
1280 restore_vimvar(VV_VAL, &save_val);
1281
1282 return list;
1283}
1284
1285/*
1286 * "list" is supposed to contain two items: a word and a number. Return the
1287 * word in "pp" and the number as the return value.
1288 * Return -1 if anything isn't right.
1289 * Used to get the good word and score from the eval_spell_expr() result.
1290 */
1291 int
1292get_spellword(list, pp)
1293 list_T *list;
1294 char_u **pp;
1295{
1296 listitem_T *li;
1297
1298 li = list->lv_first;
1299 if (li == NULL)
1300 return -1;
1301 *pp = get_tv_string(&li->li_tv);
1302
1303 li = li->li_next;
1304 if (li == NULL)
1305 return -1;
1306 return get_tv_number(&li->li_tv);
1307}
1308#endif
1309
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001310/*
1311 * Top level evaluation function,
1312 */
1313 typval_T *
1314eval_expr(arg, nextcmd)
1315 char_u *arg;
1316 char_u **nextcmd;
1317{
1318 typval_T *tv;
1319
1320 tv = (typval_T *)alloc(sizeof(typval_T));
1321 if (!tv)
1322 return NULL;
1323
1324 if (eval0(arg, tv, nextcmd, TRUE) == FAIL)
1325 {
1326 vim_free(tv);
1327 return NULL;
1328 }
1329
1330 return tv;
1331}
1332
1333
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1335/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001336 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001338 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001340 static int
1341call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *func;
1343 int argc;
1344 char_u **argv;
1345 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
Bram Moolenaar33570922005-01-25 22:26:29 +00001348 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 long n;
1350 int len;
1351 int i;
1352 int doesrange;
1353 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001354 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
Bram Moolenaar33570922005-01-25 22:26:29 +00001356 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001358 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
1360 for (i = 0; i < argc; i++)
1361 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001362 /* Pass a NULL or empty argument as an empty string */
1363 if (argv[i] == NULL || *argv[i] == NUL)
1364 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001365 argvars[i].v_type = VAR_STRING;
1366 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001367 continue;
1368 }
1369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 /* Recognize a number argument, the others must be strings. */
1371 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1372 if (len != 0 && len == (int)STRLEN(argv[i]))
1373 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001374 argvars[i].v_type = VAR_NUMBER;
1375 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 }
1377 else
1378 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001379 argvars[i].v_type = VAR_STRING;
1380 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382 }
1383
1384 if (safe)
1385 {
1386 save_funccalp = save_funccal();
1387 ++sandbox;
1388 }
1389
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001390 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1391 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001393 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 if (safe)
1395 {
1396 --sandbox;
1397 restore_funccal(save_funccalp);
1398 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001399 vim_free(argvars);
1400
1401 if (ret == FAIL)
1402 clear_tv(rettv);
1403
1404 return ret;
1405}
1406
1407/*
1408 * Call some vimL function and return the result as a string
1409 * Uses argv[argc] for the function arguments.
1410 */
1411 void *
1412call_func_retstr(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 char_u *retval = NULL;
1420
1421 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1422 return NULL;
1423
1424 retval = vim_strsave(get_tv_string(&rettv));
1425
1426 clear_tv(&rettv);
1427
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 return retval;
1429}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001430
1431/*
1432 * Call some vimL function and return the result as a list
1433 * Uses argv[argc] for the function arguments.
1434 */
1435 void *
1436call_func_retlist(func, argc, argv, safe)
1437 char_u *func;
1438 int argc;
1439 char_u **argv;
1440 int safe; /* use the sandbox */
1441{
1442 typval_T rettv;
1443
1444 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1445 return NULL;
1446
1447 if (rettv.v_type != VAR_LIST)
1448 {
1449 clear_tv(&rettv);
1450 return NULL;
1451 }
1452
1453 return rettv.vval.v_list;
1454}
1455
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456#endif
1457
1458/*
1459 * Save the current function call pointer, and set it to NULL.
1460 * Used when executing autocommands and for ":source".
1461 */
1462 void *
1463save_funccal()
1464{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001465 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 current_funccal = NULL;
1468 return (void *)fc;
1469}
1470
1471 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001472restore_funccal(vfc)
1473 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001475 funccall_T *fc = (funccall_T *)vfc;
1476
1477 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478}
1479
Bram Moolenaar05159a02005-02-26 23:04:13 +00001480#if defined(FEAT_PROFILE) || defined(PROTO)
1481/*
1482 * Prepare profiling for entering a child or something else that is not
1483 * counted for the script/function itself.
1484 * Should always be called in pair with prof_child_exit().
1485 */
1486 void
1487prof_child_enter(tm)
1488 proftime_T *tm; /* place to store waittime */
1489{
1490 funccall_T *fc = current_funccal;
1491
1492 if (fc != NULL && fc->func->uf_profiling)
1493 profile_start(&fc->prof_child);
1494 script_prof_save(tm);
1495}
1496
1497/*
1498 * Take care of time spent in a child.
1499 * Should always be called after prof_child_enter().
1500 */
1501 void
1502prof_child_exit(tm)
1503 proftime_T *tm; /* where waittime was stored */
1504{
1505 funccall_T *fc = current_funccal;
1506
1507 if (fc != NULL && fc->func->uf_profiling)
1508 {
1509 profile_end(&fc->prof_child);
1510 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1511 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1512 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1513 }
1514 script_prof_restore(tm);
1515}
1516#endif
1517
1518
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519#ifdef FEAT_FOLDING
1520/*
1521 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1522 * it in "*cp". Doesn't give error messages.
1523 */
1524 int
1525eval_foldexpr(arg, cp)
1526 char_u *arg;
1527 int *cp;
1528{
Bram Moolenaar33570922005-01-25 22:26:29 +00001529 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 int retval;
1531 char_u *s;
1532
1533 ++emsg_off;
1534 ++sandbox;
1535 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001536 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 retval = 0;
1538 else
1539 {
1540 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001541 if (tv.v_type == VAR_NUMBER)
1542 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001543 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 retval = 0;
1545 else
1546 {
1547 /* If the result is a string, check if there is a non-digit before
1548 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001549 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 if (!VIM_ISDIGIT(*s) && *s != '-')
1551 *cp = *s++;
1552 retval = atol((char *)s);
1553 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001554 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 }
1556 --emsg_off;
1557 --sandbox;
1558
1559 return retval;
1560}
1561#endif
1562
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001564 * ":let" list all variable values
1565 * ":let var1 var2" list variable values
1566 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001567 * ":let var += expr" assignment command.
1568 * ":let var -= expr" assignment command.
1569 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001570 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 */
1572 void
1573ex_let(eap)
1574 exarg_T *eap;
1575{
1576 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001577 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001578 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001580 int var_count = 0;
1581 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001582 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001584 expr = skip_var_list(arg, &var_count, &semicolon);
1585 if (expr == NULL)
1586 return;
1587 expr = vim_strchr(expr, '=');
1588 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001590 /*
1591 * ":let" without "=": list variables
1592 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001593 if (*arg == '[')
1594 EMSG(_(e_invarg));
1595 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001596 /* ":let var1 var2" */
1597 arg = list_arg_vars(eap, arg);
1598 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001600 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001601 list_glob_vars();
1602 list_buf_vars();
1603 list_win_vars();
1604 list_vim_vars();
1605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 eap->nextcmd = check_nextcmd(arg);
1607 }
1608 else
1609 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001610 op[0] = '=';
1611 op[1] = NUL;
1612 if (expr > arg)
1613 {
1614 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1615 op[0] = expr[-1]; /* +=, -= or .= */
1616 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001617 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 if (eap->skip)
1620 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001621 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 if (eap->skip)
1623 {
1624 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001625 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 --emsg_skip;
1627 }
1628 else if (i != FAIL)
1629 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001630 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001631 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 }
1634 }
1635}
1636
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001637/*
1638 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1639 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001640 * When "nextchars" is not NULL it points to a string with characters that
1641 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1642 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001643 * Returns OK or FAIL;
1644 */
1645 static int
1646ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1647 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001648 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001649 int copy; /* copy values from "tv", don't move */
1650 int semicolon; /* from skip_var_list() */
1651 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001652 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001653{
1654 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001655 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001657 listitem_T *item;
1658 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659
1660 if (*arg != '[')
1661 {
1662 /*
1663 * ":let var = expr" or ":for var in list"
1664 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001665 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666 return FAIL;
1667 return OK;
1668 }
1669
1670 /*
1671 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1672 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001673 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674 {
1675 EMSG(_(e_listreq));
1676 return FAIL;
1677 }
1678
1679 i = list_len(l);
1680 if (semicolon == 0 && var_count < i)
1681 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001682 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001683 return FAIL;
1684 }
1685 if (var_count - semicolon > i)
1686 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001687 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 return FAIL;
1689 }
1690
1691 item = l->lv_first;
1692 while (*arg != ']')
1693 {
1694 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001695 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 item = item->li_next;
1697 if (arg == NULL)
1698 return FAIL;
1699
1700 arg = skipwhite(arg);
1701 if (*arg == ';')
1702 {
1703 /* Put the rest of the list (may be empty) in the var after ';'.
1704 * Create a new list for this. */
1705 l = list_alloc();
1706 if (l == NULL)
1707 return FAIL;
1708 while (item != NULL)
1709 {
1710 list_append_tv(l, &item->li_tv);
1711 item = item->li_next;
1712 }
1713
1714 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001715 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 ltv.vval.v_list = l;
1717 l->lv_refcount = 1;
1718
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1720 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001721 clear_tv(&ltv);
1722 if (arg == NULL)
1723 return FAIL;
1724 break;
1725 }
1726 else if (*arg != ',' && *arg != ']')
1727 {
1728 EMSG2(_(e_intern2), "ex_let_vars()");
1729 return FAIL;
1730 }
1731 }
1732
1733 return OK;
1734}
1735
1736/*
1737 * Skip over assignable variable "var" or list of variables "[var, var]".
1738 * Used for ":let varvar = expr" and ":for varvar in expr".
1739 * For "[var, var]" increment "*var_count" for each variable.
1740 * for "[var, var; var]" set "semicolon".
1741 * Return NULL for an error.
1742 */
1743 static char_u *
1744skip_var_list(arg, var_count, semicolon)
1745 char_u *arg;
1746 int *var_count;
1747 int *semicolon;
1748{
1749 char_u *p, *s;
1750
1751 if (*arg == '[')
1752 {
1753 /* "[var, var]": find the matching ']'. */
1754 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001755 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001756 {
1757 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1758 s = skip_var_one(p);
1759 if (s == p)
1760 {
1761 EMSG2(_(e_invarg2), p);
1762 return NULL;
1763 }
1764 ++*var_count;
1765
1766 p = skipwhite(s);
1767 if (*p == ']')
1768 break;
1769 else if (*p == ';')
1770 {
1771 if (*semicolon == 1)
1772 {
1773 EMSG(_("Double ; in list of variables"));
1774 return NULL;
1775 }
1776 *semicolon = 1;
1777 }
1778 else if (*p != ',')
1779 {
1780 EMSG2(_(e_invarg2), p);
1781 return NULL;
1782 }
1783 }
1784 return p + 1;
1785 }
1786 else
1787 return skip_var_one(arg);
1788}
1789
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001790/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001791 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1792 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001793 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 static char_u *
1795skip_var_one(arg)
1796 char_u *arg;
1797{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001798 if (*arg == '@' && arg[1] != NUL)
1799 return arg + 2;
1800 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1801 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802}
1803
Bram Moolenaara7043832005-01-21 11:56:39 +00001804/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001805 * List variables for hashtab "ht" with prefix "prefix".
1806 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001807 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001809list_hashtable_vars(ht, prefix, empty)
1810 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001811 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001812 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001813{
Bram Moolenaar33570922005-01-25 22:26:29 +00001814 hashitem_T *hi;
1815 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001816 int todo;
1817
1818 todo = ht->ht_used;
1819 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1820 {
1821 if (!HASHITEM_EMPTY(hi))
1822 {
1823 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001824 di = HI2DI(hi);
1825 if (empty || di->di_tv.v_type != VAR_STRING
1826 || di->di_tv.vval.v_string != NULL)
1827 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001828 }
1829 }
1830}
1831
1832/*
1833 * List global variables.
1834 */
1835 static void
1836list_glob_vars()
1837{
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001839}
1840
1841/*
1842 * List buffer variables.
1843 */
1844 static void
1845list_buf_vars()
1846{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001847 char_u numbuf[NUMBUFLEN];
1848
Bram Moolenaar33570922005-01-25 22:26:29 +00001849 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001850
1851 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1852 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001853}
1854
1855/*
1856 * List window variables.
1857 */
1858 static void
1859list_win_vars()
1860{
Bram Moolenaar33570922005-01-25 22:26:29 +00001861 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001862}
1863
1864/*
1865 * List Vim variables.
1866 */
1867 static void
1868list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869{
Bram Moolenaar33570922005-01-25 22:26:29 +00001870 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871}
1872
1873/*
1874 * List variables in "arg".
1875 */
1876 static char_u *
1877list_arg_vars(eap, arg)
1878 exarg_T *eap;
1879 char_u *arg;
1880{
1881 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001882 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001884 char_u *name_start;
1885 char_u *arg_subsc;
1886 char_u *tofree;
1887 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888
1889 while (!ends_excmd(*arg) && !got_int)
1890 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001891 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001893 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001894 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1895 {
1896 emsg_severe = TRUE;
1897 EMSG(_(e_trailing));
1898 break;
1899 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001901 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001903 /* get_name_len() takes care of expanding curly braces */
1904 name_start = name = arg;
1905 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1906 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001908 /* This is mainly to keep test 49 working: when expanding
1909 * curly braces fails overrule the exception error message. */
1910 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001912 emsg_severe = TRUE;
1913 EMSG2(_(e_invarg2), arg);
1914 break;
1915 }
1916 error = TRUE;
1917 }
1918 else
1919 {
1920 if (tofree != NULL)
1921 name = tofree;
1922 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 else
1925 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001926 /* handle d.key, l[idx], f(expr) */
1927 arg_subsc = arg;
1928 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001929 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001930 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001931 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001932 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001933 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001934 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001935 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001936 case 'g': list_glob_vars(); break;
1937 case 'b': list_buf_vars(); break;
1938 case 'w': list_win_vars(); break;
1939 case 'v': list_vim_vars(); break;
1940 default:
1941 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001942 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001943 }
1944 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001945 {
1946 char_u numbuf[NUMBUFLEN];
1947 char_u *tf;
1948 int c;
1949 char_u *s;
1950
1951 s = echo_string(&tv, &tf, numbuf);
1952 c = *arg;
1953 *arg = NUL;
1954 list_one_var_a((char_u *)"",
1955 arg == arg_subsc ? name : name_start,
1956 tv.v_type, s == NULL ? (char_u *)"" : s);
1957 *arg = c;
1958 vim_free(tf);
1959 }
1960 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001961 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 }
1963 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001964
1965 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001966 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001967
1968 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001969 }
1970
1971 return arg;
1972}
1973
1974/*
1975 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1976 * Returns a pointer to the char just after the var name.
1977 * Returns NULL if there is an error.
1978 */
1979 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001982 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001983 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001985 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986{
1987 int c1;
1988 char_u *name;
1989 char_u *p;
1990 char_u *arg_end = NULL;
1991 int len;
1992 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001994
1995 /*
1996 * ":let $VAR = expr": Set environment variable.
1997 */
1998 if (*arg == '$')
1999 {
2000 /* Find the end of the name. */
2001 ++arg;
2002 name = arg;
2003 len = get_env_len(&arg);
2004 if (len == 0)
2005 EMSG2(_(e_invarg2), name - 1);
2006 else
2007 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002008 if (op != NULL && (*op == '+' || *op == '-'))
2009 EMSG2(_(e_letwrong), op);
2010 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 EMSG(_(e_letunexp));
2013 else
2014 {
2015 c1 = name[len];
2016 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002017 p = get_tv_string_chk(tv);
2018 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002019 {
2020 int mustfree = FALSE;
2021 char_u *s = vim_getenv(name, &mustfree);
2022
2023 if (s != NULL)
2024 {
2025 p = tofree = concat_str(s, p);
2026 if (mustfree)
2027 vim_free(s);
2028 }
2029 }
2030 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002031 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002032 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002033 if (STRICMP(name, "HOME") == 0)
2034 init_homedir();
2035 else if (didset_vim && STRICMP(name, "VIM") == 0)
2036 didset_vim = FALSE;
2037 else if (didset_vimruntime
2038 && STRICMP(name, "VIMRUNTIME") == 0)
2039 didset_vimruntime = FALSE;
2040 arg_end = arg;
2041 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002043 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002044 }
2045 }
2046 }
2047
2048 /*
2049 * ":let &option = expr": Set option value.
2050 * ":let &l:option = expr": Set local option value.
2051 * ":let &g:option = expr": Set global option value.
2052 */
2053 else if (*arg == '&')
2054 {
2055 /* Find the end of the name. */
2056 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002057 if (p == NULL || (endchars != NULL
2058 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002059 EMSG(_(e_letunexp));
2060 else
2061 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002062 long n;
2063 int opt_type;
2064 long numval;
2065 char_u *stringval = NULL;
2066 char_u *s;
2067
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002068 c1 = *p;
2069 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002070
2071 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002072 s = get_tv_string_chk(tv); /* != NULL if number or string */
2073 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002074 {
2075 opt_type = get_option_value(arg, &numval,
2076 &stringval, opt_flags);
2077 if ((opt_type == 1 && *op == '.')
2078 || (opt_type == 0 && *op != '.'))
2079 EMSG2(_(e_letwrong), op);
2080 else
2081 {
2082 if (opt_type == 1) /* number */
2083 {
2084 if (*op == '+')
2085 n = numval + n;
2086 else
2087 n = numval - n;
2088 }
2089 else if (opt_type == 0 && stringval != NULL) /* string */
2090 {
2091 s = concat_str(stringval, s);
2092 vim_free(stringval);
2093 stringval = s;
2094 }
2095 }
2096 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002097 if (s != NULL)
2098 {
2099 set_option_value(arg, n, s, opt_flags);
2100 arg_end = p;
2101 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002102 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002103 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002104 }
2105 }
2106
2107 /*
2108 * ":let @r = expr": Set register contents.
2109 */
2110 else if (*arg == '@')
2111 {
2112 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002113 if (op != NULL && (*op == '+' || *op == '-'))
2114 EMSG2(_(e_letwrong), op);
2115 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002116 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002117 EMSG(_(e_letunexp));
2118 else
2119 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002120 char_u *tofree = NULL;
2121 char_u *s;
2122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002123 p = get_tv_string_chk(tv);
2124 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002125 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002126 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002127 if (s != NULL)
2128 {
2129 p = tofree = concat_str(s, p);
2130 vim_free(s);
2131 }
2132 }
2133 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002134 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002135 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002136 arg_end = arg + 1;
2137 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002138 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002139 }
2140 }
2141
2142 /*
2143 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002144 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002145 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002146 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002148 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002150 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002153 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2154 EMSG(_(e_letunexp));
2155 else
2156 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002157 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002158 arg_end = p;
2159 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002161 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162 }
2163
2164 else
2165 EMSG2(_(e_invarg2), arg);
2166
2167 return arg_end;
2168}
2169
2170/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002171 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2172 */
2173 static int
2174check_changedtick(arg)
2175 char_u *arg;
2176{
2177 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2178 {
2179 EMSG2(_(e_readonlyvar), arg);
2180 return TRUE;
2181 }
2182 return FALSE;
2183}
2184
2185/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 * Get an lval: variable, Dict item or List item that can be assigned a value
2187 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2188 * "name.key", "name.key[expr]" etc.
2189 * Indexing only works if "name" is an existing List or Dictionary.
2190 * "name" points to the start of the name.
2191 * If "rettv" is not NULL it points to the value to be assigned.
2192 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2193 * wrong; must end in space or cmd separator.
2194 *
2195 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002196 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 */
2199 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002200get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002202 typval_T *rettv;
2203 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002204 int unlet;
2205 int skip;
2206 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002207 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002210 char_u *expr_start, *expr_end;
2211 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002212 dictitem_T *v;
2213 typval_T var1;
2214 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002216 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002217 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002218 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002219 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002221 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002222 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002223
2224 if (skip)
2225 {
2226 /* When skipping just find the end of the name. */
2227 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002228 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002229 }
2230
2231 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002232 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002233 if (expr_start != NULL)
2234 {
2235 /* Don't expand the name when we already know there is an error. */
2236 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2237 && *p != '[' && *p != '.')
2238 {
2239 EMSG(_(e_trailing));
2240 return NULL;
2241 }
2242
2243 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2244 if (lp->ll_exp_name == NULL)
2245 {
2246 /* Report an invalid expression in braces, unless the
2247 * expression evaluation has been cancelled due to an
2248 * aborting error, an interrupt, or an exception. */
2249 if (!aborting() && !quiet)
2250 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002251 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252 EMSG2(_(e_invarg2), name);
2253 return NULL;
2254 }
2255 }
2256 lp->ll_name = lp->ll_exp_name;
2257 }
2258 else
2259 lp->ll_name = name;
2260
2261 /* Without [idx] or .key we are done. */
2262 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2263 return p;
2264
2265 cc = *p;
2266 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268 if (v == NULL && !quiet)
2269 EMSG2(_(e_undefvar), lp->ll_name);
2270 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 if (v == NULL)
2272 return NULL;
2273
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002274 /*
2275 * Loop until no more [idx] or .key is following.
2276 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002277 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002278 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002280 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2281 && !(lp->ll_tv->v_type == VAR_DICT
2282 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 if (!quiet)
2285 EMSG(_("E689: Can only index a List or Dictionary"));
2286 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002290 if (!quiet)
2291 EMSG(_("E708: [:] must come last"));
2292 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002294
Bram Moolenaar8c711452005-01-14 21:53:12 +00002295 len = -1;
2296 if (*p == '.')
2297 {
2298 key = p + 1;
2299 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2300 ;
2301 if (len == 0)
2302 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002303 if (!quiet)
2304 EMSG(_(e_emptykey));
2305 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002306 }
2307 p = key + len;
2308 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002309 else
2310 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002311 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002312 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002313 if (*p == ':')
2314 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002315 else
2316 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002317 empty1 = FALSE;
2318 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002320 if (get_tv_string_chk(&var1) == NULL)
2321 {
2322 /* not a number or string */
2323 clear_tv(&var1);
2324 return NULL;
2325 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002326 }
2327
2328 /* Optionally get the second index [ :expr]. */
2329 if (*p == ':')
2330 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002332 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002333 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002334 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002335 if (!empty1)
2336 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002337 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002338 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002339 if (rettv != NULL && (rettv->v_type != VAR_LIST
2340 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002341 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 if (!quiet)
2343 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002344 if (!empty1)
2345 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002346 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002347 }
2348 p = skipwhite(p + 1);
2349 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002350 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002351 else
2352 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002353 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002354 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2355 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002356 if (!empty1)
2357 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002358 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002359 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002360 if (get_tv_string_chk(&var2) == NULL)
2361 {
2362 /* not a number or string */
2363 if (!empty1)
2364 clear_tv(&var1);
2365 clear_tv(&var2);
2366 return NULL;
2367 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002368 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002370 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002371 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002372 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002373
Bram Moolenaar8c711452005-01-14 21:53:12 +00002374 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002375 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 if (!quiet)
2377 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002378 if (!empty1)
2379 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002380 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002381 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002383 }
2384
2385 /* Skip to past ']'. */
2386 ++p;
2387 }
2388
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002390 {
2391 if (len == -1)
2392 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002395 if (*key == NUL)
2396 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 if (!quiet)
2398 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002399 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002400 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002401 }
2402 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 lp->ll_list = NULL;
2404 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002405 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002408 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002409 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002410 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002413 if (len == -1)
2414 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002416 }
2417 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002418 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002419 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002420 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002421 if (len == -1)
2422 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002424 p = NULL;
2425 break;
2426 }
2427 if (len == -1)
2428 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002430 }
2431 else
2432 {
2433 /*
2434 * Get the number and item for the only or first index of the List.
2435 */
2436 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438 else
2439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002441 clear_tv(&var1);
2442 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 lp->ll_list = lp->ll_tv->vval.v_list;
2445 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2446 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002447 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 if (!quiet)
2449 EMSGN(_(e_listidx), lp->ll_n1);
2450 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002451 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002452 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002453 }
2454
2455 /*
2456 * May need to find the item or absolute index for the second
2457 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 * When no index given: "lp->ll_empty2" is TRUE.
2459 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002464 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002466 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002468 if (ni == NULL)
2469 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 if (!quiet)
2471 EMSGN(_(e_listidx), lp->ll_n2);
2472 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002473 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 }
2476
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2478 if (lp->ll_n1 < 0)
2479 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2480 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002481 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 if (!quiet)
2483 EMSGN(_(e_listidx), lp->ll_n2);
2484 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002485 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002486 }
2487
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002489 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 }
2491
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 return p;
2493}
2494
2495/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002496 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 */
2498 static void
2499clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002500 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501{
2502 vim_free(lp->ll_exp_name);
2503 vim_free(lp->ll_newkey);
2504}
2505
2506/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002507 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002509 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 */
2511 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002512set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002513 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002515 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002517 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518{
2519 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002520 listitem_T *ni;
2521 listitem_T *ri;
2522 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523
2524 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 cc = *endp;
2529 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002530 if (op != NULL && *op != '=')
2531 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002534 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002535 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2536 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537 {
2538 if (tv_op(&tv, rettv, op) == OK)
2539 set_var(lp->ll_name, &tv, FALSE);
2540 clear_tv(&tv);
2541 }
2542 }
2543 else
2544 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002548 else if (tv_check_lock(lp->ll_newkey == NULL
2549 ? lp->ll_tv->v_lock
2550 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2551 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 else if (lp->ll_range)
2553 {
2554 /*
2555 * Assign the List values to the list items.
2556 */
2557 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002558 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002559 if (op != NULL && *op != '=')
2560 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2561 else
2562 {
2563 clear_tv(&lp->ll_li->li_tv);
2564 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2565 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 ri = ri->li_next;
2567 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2568 break;
2569 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 /* Need to add an empty item. */
2572 ni = listitem_alloc();
2573 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002574 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 ri = NULL;
2576 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002577 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002579 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 ni->li_tv.vval.v_number = 0;
2581 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002582 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 lp->ll_li = lp->ll_li->li_next;
2584 ++lp->ll_n1;
2585 }
2586 if (ri != NULL)
2587 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002588 else if (lp->ll_empty2
2589 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 : lp->ll_n1 != lp->ll_n2)
2591 EMSG(_("E711: List value has not enough items"));
2592 }
2593 else
2594 {
2595 /*
2596 * Assign to a List or Dictionary item.
2597 */
2598 if (lp->ll_newkey != NULL)
2599 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002600 if (op != NULL && *op != '=')
2601 {
2602 EMSG2(_(e_letwrong), op);
2603 return;
2604 }
2605
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002607 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 if (di == NULL)
2609 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002610 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2611 {
2612 vim_free(di);
2613 return;
2614 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002616 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002617 else if (op != NULL && *op != '=')
2618 {
2619 tv_op(lp->ll_tv, rettv, op);
2620 return;
2621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002624
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 /*
2626 * Assign the value to the variable or list item.
2627 */
2628 if (copy)
2629 copy_tv(rettv, lp->ll_tv);
2630 else
2631 {
2632 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002633 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635 }
2636 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637}
2638
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002639/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002640 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2641 * Returns OK or FAIL.
2642 */
2643 static int
2644tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002645 typval_T *tv1;
2646 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002647 char_u *op;
2648{
2649 long n;
2650 char_u numbuf[NUMBUFLEN];
2651 char_u *s;
2652
2653 /* Can't do anything with a Funcref or a Dict on the right. */
2654 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2655 {
2656 switch (tv1->v_type)
2657 {
2658 case VAR_DICT:
2659 case VAR_FUNC:
2660 break;
2661
2662 case VAR_LIST:
2663 if (*op != '+' || tv2->v_type != VAR_LIST)
2664 break;
2665 /* List += List */
2666 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2667 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2668 return OK;
2669
2670 case VAR_NUMBER:
2671 case VAR_STRING:
2672 if (tv2->v_type == VAR_LIST)
2673 break;
2674 if (*op == '+' || *op == '-')
2675 {
2676 /* nr += nr or nr -= nr*/
2677 n = get_tv_number(tv1);
2678 if (*op == '+')
2679 n += get_tv_number(tv2);
2680 else
2681 n -= get_tv_number(tv2);
2682 clear_tv(tv1);
2683 tv1->v_type = VAR_NUMBER;
2684 tv1->vval.v_number = n;
2685 }
2686 else
2687 {
2688 /* str .= str */
2689 s = get_tv_string(tv1);
2690 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2691 clear_tv(tv1);
2692 tv1->v_type = VAR_STRING;
2693 tv1->vval.v_string = s;
2694 }
2695 return OK;
2696 }
2697 }
2698
2699 EMSG2(_(e_letwrong), op);
2700 return FAIL;
2701}
2702
2703/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002704 * Add a watcher to a list.
2705 */
2706 static void
2707list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002708 list_T *l;
2709 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002710{
2711 lw->lw_next = l->lv_watch;
2712 l->lv_watch = lw;
2713}
2714
2715/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002716 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002717 * No warning when it isn't found...
2718 */
2719 static void
2720list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002721 list_T *l;
2722 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002723{
Bram Moolenaar33570922005-01-25 22:26:29 +00002724 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002725
2726 lwp = &l->lv_watch;
2727 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2728 {
2729 if (lw == lwrem)
2730 {
2731 *lwp = lw->lw_next;
2732 break;
2733 }
2734 lwp = &lw->lw_next;
2735 }
2736}
2737
2738/*
2739 * Just before removing an item from a list: advance watchers to the next
2740 * item.
2741 */
2742 static void
2743list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002744 list_T *l;
2745 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002746{
Bram Moolenaar33570922005-01-25 22:26:29 +00002747 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002748
2749 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2750 if (lw->lw_item == item)
2751 lw->lw_item = item->li_next;
2752}
2753
2754/*
2755 * Evaluate the expression used in a ":for var in expr" command.
2756 * "arg" points to "var".
2757 * Set "*errp" to TRUE for an error, FALSE otherwise;
2758 * Return a pointer that holds the info. Null when there is an error.
2759 */
2760 void *
2761eval_for_line(arg, errp, nextcmdp, skip)
2762 char_u *arg;
2763 int *errp;
2764 char_u **nextcmdp;
2765 int skip;
2766{
Bram Moolenaar33570922005-01-25 22:26:29 +00002767 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002768 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002769 typval_T tv;
2770 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002771
2772 *errp = TRUE; /* default: there is an error */
2773
Bram Moolenaar33570922005-01-25 22:26:29 +00002774 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002775 if (fi == NULL)
2776 return NULL;
2777
2778 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2779 if (expr == NULL)
2780 return fi;
2781
2782 expr = skipwhite(expr);
2783 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2784 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002785 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002786 return fi;
2787 }
2788
2789 if (skip)
2790 ++emsg_skip;
2791 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2792 {
2793 *errp = FALSE;
2794 if (!skip)
2795 {
2796 l = tv.vval.v_list;
2797 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002798 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002799 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002800 clear_tv(&tv);
2801 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002802 else
2803 {
2804 fi->fi_list = l;
2805 list_add_watch(l, &fi->fi_lw);
2806 fi->fi_lw.lw_item = l->lv_first;
2807 }
2808 }
2809 }
2810 if (skip)
2811 --emsg_skip;
2812
2813 return fi;
2814}
2815
2816/*
2817 * Use the first item in a ":for" list. Advance to the next.
2818 * Assign the values to the variable (list). "arg" points to the first one.
2819 * Return TRUE when a valid item was found, FALSE when at end of list or
2820 * something wrong.
2821 */
2822 int
2823next_for_item(fi_void, arg)
2824 void *fi_void;
2825 char_u *arg;
2826{
Bram Moolenaar33570922005-01-25 22:26:29 +00002827 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002828 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002829 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002830
2831 item = fi->fi_lw.lw_item;
2832 if (item == NULL)
2833 result = FALSE;
2834 else
2835 {
2836 fi->fi_lw.lw_item = item->li_next;
2837 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2838 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2839 }
2840 return result;
2841}
2842
2843/*
2844 * Free the structure used to store info used by ":for".
2845 */
2846 void
2847free_for_info(fi_void)
2848 void *fi_void;
2849{
Bram Moolenaar33570922005-01-25 22:26:29 +00002850 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002851
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002852 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002853 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002854 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002855 list_unref(fi->fi_list);
2856 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002857 vim_free(fi);
2858}
2859
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2861
2862 void
2863set_context_for_expression(xp, arg, cmdidx)
2864 expand_T *xp;
2865 char_u *arg;
2866 cmdidx_T cmdidx;
2867{
2868 int got_eq = FALSE;
2869 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002870 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002872 if (cmdidx == CMD_let)
2873 {
2874 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002875 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002876 {
2877 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002878 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002879 {
2880 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002882 if (vim_iswhite(*p))
2883 break;
2884 }
2885 return;
2886 }
2887 }
2888 else
2889 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2890 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 while ((xp->xp_pattern = vim_strpbrk(arg,
2892 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2893 {
2894 c = *xp->xp_pattern;
2895 if (c == '&')
2896 {
2897 c = xp->xp_pattern[1];
2898 if (c == '&')
2899 {
2900 ++xp->xp_pattern;
2901 xp->xp_context = cmdidx != CMD_let || got_eq
2902 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2903 }
2904 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002907 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2908 xp->xp_pattern += 2;
2909
2910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 }
2912 else if (c == '$')
2913 {
2914 /* environment variable */
2915 xp->xp_context = EXPAND_ENV_VARS;
2916 }
2917 else if (c == '=')
2918 {
2919 got_eq = TRUE;
2920 xp->xp_context = EXPAND_EXPRESSION;
2921 }
2922 else if (c == '<'
2923 && xp->xp_context == EXPAND_FUNCTIONS
2924 && vim_strchr(xp->xp_pattern, '(') == NULL)
2925 {
2926 /* Function name can start with "<SNR>" */
2927 break;
2928 }
2929 else if (cmdidx != CMD_let || got_eq)
2930 {
2931 if (c == '"') /* string */
2932 {
2933 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2934 if (c == '\\' && xp->xp_pattern[1] != NUL)
2935 ++xp->xp_pattern;
2936 xp->xp_context = EXPAND_NOTHING;
2937 }
2938 else if (c == '\'') /* literal string */
2939 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002940 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2942 /* skip */ ;
2943 xp->xp_context = EXPAND_NOTHING;
2944 }
2945 else if (c == '|')
2946 {
2947 if (xp->xp_pattern[1] == '|')
2948 {
2949 ++xp->xp_pattern;
2950 xp->xp_context = EXPAND_EXPRESSION;
2951 }
2952 else
2953 xp->xp_context = EXPAND_COMMANDS;
2954 }
2955 else
2956 xp->xp_context = EXPAND_EXPRESSION;
2957 }
2958 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002959 /* Doesn't look like something valid, expand as an expression
2960 * anyway. */
2961 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 arg = xp->xp_pattern;
2963 if (*arg != NUL)
2964 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2965 /* skip */ ;
2966 }
2967 xp->xp_pattern = arg;
2968}
2969
2970#endif /* FEAT_CMDL_COMPL */
2971
2972/*
2973 * ":1,25call func(arg1, arg2)" function call.
2974 */
2975 void
2976ex_call(eap)
2977 exarg_T *eap;
2978{
2979 char_u *arg = eap->arg;
2980 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002984 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 linenr_T lnum;
2986 int doesrange;
2987 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002988 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002990 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2991 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002992 if (tofree == NULL)
2993 return;
2994
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002995 /* Increase refcount on dictionary, it could get deleted when evaluating
2996 * the arguments. */
2997 if (fudi.fd_dict != NULL)
2998 ++fudi.fd_dict->dv_refcount;
2999
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3001 len = STRLEN(tofree);
3002 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
Bram Moolenaar532c7802005-01-27 14:44:31 +00003004 /* Skip white space to allow ":call func ()". Not good, but required for
3005 * backward compatibility. */
3006 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003007 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
3009 if (*startarg != '(')
3010 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003011 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 goto end;
3013 }
3014
3015 /*
3016 * When skipping, evaluate the function once, to find the end of the
3017 * arguments.
3018 * When the function takes a range, this is discovered after the first
3019 * call, and the loop is broken.
3020 */
3021 if (eap->skip)
3022 {
3023 ++emsg_skip;
3024 lnum = eap->line2; /* do it once, also with an invalid range */
3025 }
3026 else
3027 lnum = eap->line1;
3028 for ( ; lnum <= eap->line2; ++lnum)
3029 {
3030 if (!eap->skip && eap->addr_count > 0)
3031 {
3032 curwin->w_cursor.lnum = lnum;
3033 curwin->w_cursor.col = 0;
3034 }
3035 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003037 eap->line1, eap->line2, &doesrange,
3038 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 {
3040 failed = TRUE;
3041 break;
3042 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003043 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 if (doesrange || eap->skip)
3045 break;
3046 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003047 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003048 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003049 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 if (aborting())
3051 break;
3052 }
3053 if (eap->skip)
3054 --emsg_skip;
3055
3056 if (!failed)
3057 {
3058 /* Check for trailing illegal characters and a following command. */
3059 if (!ends_excmd(*arg))
3060 {
3061 emsg_severe = TRUE;
3062 EMSG(_(e_trailing));
3063 }
3064 else
3065 eap->nextcmd = check_nextcmd(arg);
3066 }
3067
3068end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003069 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071}
3072
3073/*
3074 * ":unlet[!] var1 ... " command.
3075 */
3076 void
3077ex_unlet(eap)
3078 exarg_T *eap;
3079{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003080 ex_unletlock(eap, eap->arg, 0);
3081}
3082
3083/*
3084 * ":lockvar" and ":unlockvar" commands
3085 */
3086 void
3087ex_lockvar(eap)
3088 exarg_T *eap;
3089{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003091 int deep = 2;
3092
3093 if (eap->forceit)
3094 deep = -1;
3095 else if (vim_isdigit(*arg))
3096 {
3097 deep = getdigits(&arg);
3098 arg = skipwhite(arg);
3099 }
3100
3101 ex_unletlock(eap, arg, deep);
3102}
3103
3104/*
3105 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3106 */
3107 static void
3108ex_unletlock(eap, argstart, deep)
3109 exarg_T *eap;
3110 char_u *argstart;
3111 int deep;
3112{
3113 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003116 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117
3118 do
3119 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003120 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003121 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3122 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003123 if (lv.ll_name == NULL)
3124 error = TRUE; /* error but continue parsing */
3125 if (name_end == NULL || (!vim_iswhite(*name_end)
3126 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003128 if (name_end != NULL)
3129 {
3130 emsg_severe = TRUE;
3131 EMSG(_(e_trailing));
3132 }
3133 if (!(eap->skip || error))
3134 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 break;
3136 }
3137
3138 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003139 {
3140 if (eap->cmdidx == CMD_unlet)
3141 {
3142 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3143 error = TRUE;
3144 }
3145 else
3146 {
3147 if (do_lock_var(&lv, name_end, deep,
3148 eap->cmdidx == CMD_lockvar) == FAIL)
3149 error = TRUE;
3150 }
3151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003153 if (!eap->skip)
3154 clear_lval(&lv);
3155
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 arg = skipwhite(name_end);
3157 } while (!ends_excmd(*arg));
3158
3159 eap->nextcmd = check_nextcmd(arg);
3160}
3161
Bram Moolenaar8c711452005-01-14 21:53:12 +00003162 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003163do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003164 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003165 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003166 int forceit;
3167{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003168 int ret = OK;
3169 int cc;
3170
3171 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003172 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003173 cc = *name_end;
3174 *name_end = NUL;
3175
3176 /* Normal name or expanded name. */
3177 if (check_changedtick(lp->ll_name))
3178 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003179 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003180 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003181 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003182 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003183 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3184 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003185 else if (lp->ll_range)
3186 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003187 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003188
3189 /* Delete a range of List items. */
3190 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3191 {
3192 li = lp->ll_li->li_next;
3193 listitem_remove(lp->ll_list, lp->ll_li);
3194 lp->ll_li = li;
3195 ++lp->ll_n1;
3196 }
3197 }
3198 else
3199 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003200 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003201 /* unlet a List item. */
3202 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003203 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003204 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003205 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003206 }
3207
3208 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003209}
3210
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211/*
3212 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003213 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 */
3215 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003216do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003218 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219{
Bram Moolenaar33570922005-01-25 22:26:29 +00003220 hashtab_T *ht;
3221 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003222 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223
Bram Moolenaar33570922005-01-25 22:26:29 +00003224 ht = find_var_ht(name, &varname);
3225 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 hi = hash_find(ht, varname);
3228 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003229 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003230 if (var_check_ro(HI2DI(hi)->di_flags, name))
3231 return FAIL;
3232 delete_var(ht, hi);
3233 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003236 if (forceit)
3237 return OK;
3238 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 return FAIL;
3240}
3241
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003242/*
3243 * Lock or unlock variable indicated by "lp".
3244 * "deep" is the levels to go (-1 for unlimited);
3245 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3246 */
3247 static int
3248do_lock_var(lp, name_end, deep, lock)
3249 lval_T *lp;
3250 char_u *name_end;
3251 int deep;
3252 int lock;
3253{
3254 int ret = OK;
3255 int cc;
3256 dictitem_T *di;
3257
3258 if (deep == 0) /* nothing to do */
3259 return OK;
3260
3261 if (lp->ll_tv == NULL)
3262 {
3263 cc = *name_end;
3264 *name_end = NUL;
3265
3266 /* Normal name or expanded name. */
3267 if (check_changedtick(lp->ll_name))
3268 ret = FAIL;
3269 else
3270 {
3271 di = find_var(lp->ll_name, NULL);
3272 if (di == NULL)
3273 ret = FAIL;
3274 else
3275 {
3276 if (lock)
3277 di->di_flags |= DI_FLAGS_LOCK;
3278 else
3279 di->di_flags &= ~DI_FLAGS_LOCK;
3280 item_lock(&di->di_tv, deep, lock);
3281 }
3282 }
3283 *name_end = cc;
3284 }
3285 else if (lp->ll_range)
3286 {
3287 listitem_T *li = lp->ll_li;
3288
3289 /* (un)lock a range of List items. */
3290 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3291 {
3292 item_lock(&li->li_tv, deep, lock);
3293 li = li->li_next;
3294 ++lp->ll_n1;
3295 }
3296 }
3297 else if (lp->ll_list != NULL)
3298 /* (un)lock a List item. */
3299 item_lock(&lp->ll_li->li_tv, deep, lock);
3300 else
3301 /* un(lock) a Dictionary item. */
3302 item_lock(&lp->ll_di->di_tv, deep, lock);
3303
3304 return ret;
3305}
3306
3307/*
3308 * Lock or unlock an item. "deep" is nr of levels to go.
3309 */
3310 static void
3311item_lock(tv, deep, lock)
3312 typval_T *tv;
3313 int deep;
3314 int lock;
3315{
3316 static int recurse = 0;
3317 list_T *l;
3318 listitem_T *li;
3319 dict_T *d;
3320 hashitem_T *hi;
3321 int todo;
3322
3323 if (recurse >= DICT_MAXNEST)
3324 {
3325 EMSG(_("E743: variable nested too deep for (un)lock"));
3326 return;
3327 }
3328 if (deep == 0)
3329 return;
3330 ++recurse;
3331
3332 /* lock/unlock the item itself */
3333 if (lock)
3334 tv->v_lock |= VAR_LOCKED;
3335 else
3336 tv->v_lock &= ~VAR_LOCKED;
3337
3338 switch (tv->v_type)
3339 {
3340 case VAR_LIST:
3341 if ((l = tv->vval.v_list) != NULL)
3342 {
3343 if (lock)
3344 l->lv_lock |= VAR_LOCKED;
3345 else
3346 l->lv_lock &= ~VAR_LOCKED;
3347 if (deep < 0 || deep > 1)
3348 /* recursive: lock/unlock the items the List contains */
3349 for (li = l->lv_first; li != NULL; li = li->li_next)
3350 item_lock(&li->li_tv, deep - 1, lock);
3351 }
3352 break;
3353 case VAR_DICT:
3354 if ((d = tv->vval.v_dict) != NULL)
3355 {
3356 if (lock)
3357 d->dv_lock |= VAR_LOCKED;
3358 else
3359 d->dv_lock &= ~VAR_LOCKED;
3360 if (deep < 0 || deep > 1)
3361 {
3362 /* recursive: lock/unlock the items the List contains */
3363 todo = d->dv_hashtab.ht_used;
3364 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3365 {
3366 if (!HASHITEM_EMPTY(hi))
3367 {
3368 --todo;
3369 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3370 }
3371 }
3372 }
3373 }
3374 }
3375 --recurse;
3376}
3377
Bram Moolenaara40058a2005-07-11 22:42:07 +00003378/*
3379 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3380 * it refers to a List or Dictionary that is locked.
3381 */
3382 static int
3383tv_islocked(tv)
3384 typval_T *tv;
3385{
3386 return (tv->v_lock & VAR_LOCKED)
3387 || (tv->v_type == VAR_LIST
3388 && tv->vval.v_list != NULL
3389 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3390 || (tv->v_type == VAR_DICT
3391 && tv->vval.v_dict != NULL
3392 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3393}
3394
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3396/*
3397 * Delete all "menutrans_" variables.
3398 */
3399 void
3400del_menutrans_vars()
3401{
Bram Moolenaar33570922005-01-25 22:26:29 +00003402 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003403 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
Bram Moolenaar33570922005-01-25 22:26:29 +00003405 hash_lock(&globvarht);
3406 todo = globvarht.ht_used;
3407 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003408 {
3409 if (!HASHITEM_EMPTY(hi))
3410 {
3411 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003412 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3413 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003414 }
3415 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003416 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417}
3418#endif
3419
3420#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3421
3422/*
3423 * Local string buffer for the next two functions to store a variable name
3424 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3425 * get_user_var_name().
3426 */
3427
3428static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3429
3430static char_u *varnamebuf = NULL;
3431static int varnamebuflen = 0;
3432
3433/*
3434 * Function to concatenate a prefix and a variable name.
3435 */
3436 static char_u *
3437cat_prefix_varname(prefix, name)
3438 int prefix;
3439 char_u *name;
3440{
3441 int len;
3442
3443 len = (int)STRLEN(name) + 3;
3444 if (len > varnamebuflen)
3445 {
3446 vim_free(varnamebuf);
3447 len += 10; /* some additional space */
3448 varnamebuf = alloc(len);
3449 if (varnamebuf == NULL)
3450 {
3451 varnamebuflen = 0;
3452 return NULL;
3453 }
3454 varnamebuflen = len;
3455 }
3456 *varnamebuf = prefix;
3457 varnamebuf[1] = ':';
3458 STRCPY(varnamebuf + 2, name);
3459 return varnamebuf;
3460}
3461
3462/*
3463 * Function given to ExpandGeneric() to obtain the list of user defined
3464 * (global/buffer/window/built-in) variable names.
3465 */
3466/*ARGSUSED*/
3467 char_u *
3468get_user_var_name(xp, idx)
3469 expand_T *xp;
3470 int idx;
3471{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003472 static long_u gdone;
3473 static long_u bdone;
3474 static long_u wdone;
3475 static int vidx;
3476 static hashitem_T *hi;
3477 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
3479 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003480 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003481
3482 /* Global variables */
3483 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003485 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003486 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003487 else
3488 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003489 while (HASHITEM_EMPTY(hi))
3490 ++hi;
3491 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3492 return cat_prefix_varname('g', hi->hi_key);
3493 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003495
3496 /* b: variables */
3497 ht = &curbuf->b_vars.dv_hashtab;
3498 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003500 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003501 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003502 else
3503 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003504 while (HASHITEM_EMPTY(hi))
3505 ++hi;
3506 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003508 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003510 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 return (char_u *)"b:changedtick";
3512 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003513
3514 /* w: variables */
3515 ht = &curwin->w_vars.dv_hashtab;
3516 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003518 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003519 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003520 else
3521 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003522 while (HASHITEM_EMPTY(hi))
3523 ++hi;
3524 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003526
3527 /* v: variables */
3528 if (vidx < VV_LEN)
3529 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
3531 vim_free(varnamebuf);
3532 varnamebuf = NULL;
3533 varnamebuflen = 0;
3534 return NULL;
3535}
3536
3537#endif /* FEAT_CMDL_COMPL */
3538
3539/*
3540 * types for expressions.
3541 */
3542typedef enum
3543{
3544 TYPE_UNKNOWN = 0
3545 , TYPE_EQUAL /* == */
3546 , TYPE_NEQUAL /* != */
3547 , TYPE_GREATER /* > */
3548 , TYPE_GEQUAL /* >= */
3549 , TYPE_SMALLER /* < */
3550 , TYPE_SEQUAL /* <= */
3551 , TYPE_MATCH /* =~ */
3552 , TYPE_NOMATCH /* !~ */
3553} exptype_T;
3554
3555/*
3556 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003557 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3559 */
3560
3561/*
3562 * Handle zero level expression.
3563 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003564 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 * Return OK or FAIL.
3566 */
3567 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003568eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 char_u **nextcmd;
3572 int evaluate;
3573{
3574 int ret;
3575 char_u *p;
3576
3577 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003578 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (ret == FAIL || !ends_excmd(*p))
3580 {
3581 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003582 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 /*
3584 * Report the invalid expression unless the expression evaluation has
3585 * been cancelled due to an aborting error, an interrupt, or an
3586 * exception.
3587 */
3588 if (!aborting())
3589 EMSG2(_(e_invexpr2), arg);
3590 ret = FAIL;
3591 }
3592 if (nextcmd != NULL)
3593 *nextcmd = check_nextcmd(p);
3594
3595 return ret;
3596}
3597
3598/*
3599 * Handle top level expression:
3600 * expr1 ? expr0 : expr0
3601 *
3602 * "arg" must point to the first non-white of the expression.
3603 * "arg" is advanced to the next non-white after the recognized expression.
3604 *
3605 * Return OK or FAIL.
3606 */
3607 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003608eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003610 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 int evaluate;
3612{
3613 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003614 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615
3616 /*
3617 * Get the first variable.
3618 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003619 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 return FAIL;
3621
3622 if ((*arg)[0] == '?')
3623 {
3624 result = FALSE;
3625 if (evaluate)
3626 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003627 int error = FALSE;
3628
3629 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003631 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003632 if (error)
3633 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 }
3635
3636 /*
3637 * Get the second variable.
3638 */
3639 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003640 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 return FAIL;
3642
3643 /*
3644 * Check for the ":".
3645 */
3646 if ((*arg)[0] != ':')
3647 {
3648 EMSG(_("E109: Missing ':' after '?'"));
3649 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003650 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 return FAIL;
3652 }
3653
3654 /*
3655 * Get the third variable.
3656 */
3657 *arg = skipwhite(*arg + 1);
3658 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3659 {
3660 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003661 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 return FAIL;
3663 }
3664 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003665 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
3667
3668 return OK;
3669}
3670
3671/*
3672 * Handle first level expression:
3673 * expr2 || expr2 || expr2 logical OR
3674 *
3675 * "arg" must point to the first non-white of the expression.
3676 * "arg" is advanced to the next non-white after the recognized expression.
3677 *
3678 * Return OK or FAIL.
3679 */
3680 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003681eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 int evaluate;
3685{
Bram Moolenaar33570922005-01-25 22:26:29 +00003686 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 long result;
3688 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003689 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
3691 /*
3692 * Get the first variable.
3693 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003694 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 return FAIL;
3696
3697 /*
3698 * Repeat until there is no following "||".
3699 */
3700 first = TRUE;
3701 result = FALSE;
3702 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3703 {
3704 if (evaluate && first)
3705 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003706 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003708 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003709 if (error)
3710 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 first = FALSE;
3712 }
3713
3714 /*
3715 * Get the second variable.
3716 */
3717 *arg = skipwhite(*arg + 2);
3718 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3719 return FAIL;
3720
3721 /*
3722 * Compute the result.
3723 */
3724 if (evaluate && !result)
3725 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003726 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003728 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003729 if (error)
3730 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732 if (evaluate)
3733 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003734 rettv->v_type = VAR_NUMBER;
3735 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737 }
3738
3739 return OK;
3740}
3741
3742/*
3743 * Handle second level expression:
3744 * expr3 && expr3 && expr3 logical AND
3745 *
3746 * "arg" must point to the first non-white of the expression.
3747 * "arg" is advanced to the next non-white after the recognized expression.
3748 *
3749 * Return OK or FAIL.
3750 */
3751 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003752eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 int evaluate;
3756{
Bram Moolenaar33570922005-01-25 22:26:29 +00003757 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 long result;
3759 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003760 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761
3762 /*
3763 * Get the first variable.
3764 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003765 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 return FAIL;
3767
3768 /*
3769 * Repeat until there is no following "&&".
3770 */
3771 first = TRUE;
3772 result = TRUE;
3773 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3774 {
3775 if (evaluate && first)
3776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003777 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003779 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003780 if (error)
3781 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 first = FALSE;
3783 }
3784
3785 /*
3786 * Get the second variable.
3787 */
3788 *arg = skipwhite(*arg + 2);
3789 if (eval4(arg, &var2, evaluate && result) == FAIL)
3790 return FAIL;
3791
3792 /*
3793 * Compute the result.
3794 */
3795 if (evaluate && result)
3796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003797 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003799 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003800 if (error)
3801 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
3803 if (evaluate)
3804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003805 rettv->v_type = VAR_NUMBER;
3806 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 }
3808 }
3809
3810 return OK;
3811}
3812
3813/*
3814 * Handle third level expression:
3815 * var1 == var2
3816 * var1 =~ var2
3817 * var1 != var2
3818 * var1 !~ var2
3819 * var1 > var2
3820 * var1 >= var2
3821 * var1 < var2
3822 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003823 * var1 is var2
3824 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 *
3826 * "arg" must point to the first non-white of the expression.
3827 * "arg" is advanced to the next non-white after the recognized expression.
3828 *
3829 * Return OK or FAIL.
3830 */
3831 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003832eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 int evaluate;
3836{
Bram Moolenaar33570922005-01-25 22:26:29 +00003837 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 char_u *p;
3839 int i;
3840 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003841 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 int len = 2;
3843 long n1, n2;
3844 char_u *s1, *s2;
3845 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3846 regmatch_T regmatch;
3847 int ic;
3848 char_u *save_cpo;
3849
3850 /*
3851 * Get the first variable.
3852 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003853 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 return FAIL;
3855
3856 p = *arg;
3857 switch (p[0])
3858 {
3859 case '=': if (p[1] == '=')
3860 type = TYPE_EQUAL;
3861 else if (p[1] == '~')
3862 type = TYPE_MATCH;
3863 break;
3864 case '!': if (p[1] == '=')
3865 type = TYPE_NEQUAL;
3866 else if (p[1] == '~')
3867 type = TYPE_NOMATCH;
3868 break;
3869 case '>': if (p[1] != '=')
3870 {
3871 type = TYPE_GREATER;
3872 len = 1;
3873 }
3874 else
3875 type = TYPE_GEQUAL;
3876 break;
3877 case '<': if (p[1] != '=')
3878 {
3879 type = TYPE_SMALLER;
3880 len = 1;
3881 }
3882 else
3883 type = TYPE_SEQUAL;
3884 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003885 case 'i': if (p[1] == 's')
3886 {
3887 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3888 len = 5;
3889 if (!vim_isIDc(p[len]))
3890 {
3891 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3892 type_is = TRUE;
3893 }
3894 }
3895 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
3897
3898 /*
3899 * If there is a comparitive operator, use it.
3900 */
3901 if (type != TYPE_UNKNOWN)
3902 {
3903 /* extra question mark appended: ignore case */
3904 if (p[len] == '?')
3905 {
3906 ic = TRUE;
3907 ++len;
3908 }
3909 /* extra '#' appended: match case */
3910 else if (p[len] == '#')
3911 {
3912 ic = FALSE;
3913 ++len;
3914 }
3915 /* nothing appened: use 'ignorecase' */
3916 else
3917 ic = p_ic;
3918
3919 /*
3920 * Get the second variable.
3921 */
3922 *arg = skipwhite(p + len);
3923 if (eval5(arg, &var2, evaluate) == FAIL)
3924 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003925 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 return FAIL;
3927 }
3928
3929 if (evaluate)
3930 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003931 if (type_is && rettv->v_type != var2.v_type)
3932 {
3933 /* For "is" a different type always means FALSE, for "notis"
3934 * it means TRUE. */
3935 n1 = (type == TYPE_NEQUAL);
3936 }
3937 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3938 {
3939 if (type_is)
3940 {
3941 n1 = (rettv->v_type == var2.v_type
3942 && rettv->vval.v_list == var2.vval.v_list);
3943 if (type == TYPE_NEQUAL)
3944 n1 = !n1;
3945 }
3946 else if (rettv->v_type != var2.v_type
3947 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3948 {
3949 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003950 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003951 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003952 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003953 clear_tv(rettv);
3954 clear_tv(&var2);
3955 return FAIL;
3956 }
3957 else
3958 {
3959 /* Compare two Lists for being equal or unequal. */
3960 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3961 if (type == TYPE_NEQUAL)
3962 n1 = !n1;
3963 }
3964 }
3965
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003966 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3967 {
3968 if (type_is)
3969 {
3970 n1 = (rettv->v_type == var2.v_type
3971 && rettv->vval.v_dict == var2.vval.v_dict);
3972 if (type == TYPE_NEQUAL)
3973 n1 = !n1;
3974 }
3975 else if (rettv->v_type != var2.v_type
3976 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3977 {
3978 if (rettv->v_type != var2.v_type)
3979 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3980 else
3981 EMSG(_("E736: Invalid operation for Dictionary"));
3982 clear_tv(rettv);
3983 clear_tv(&var2);
3984 return FAIL;
3985 }
3986 else
3987 {
3988 /* Compare two Dictionaries for being equal or unequal. */
3989 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3990 if (type == TYPE_NEQUAL)
3991 n1 = !n1;
3992 }
3993 }
3994
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003995 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3996 {
3997 if (rettv->v_type != var2.v_type
3998 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3999 {
4000 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004001 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004002 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004003 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004004 clear_tv(rettv);
4005 clear_tv(&var2);
4006 return FAIL;
4007 }
4008 else
4009 {
4010 /* Compare two Funcrefs for being equal or unequal. */
4011 if (rettv->vval.v_string == NULL
4012 || var2.vval.v_string == NULL)
4013 n1 = FALSE;
4014 else
4015 n1 = STRCMP(rettv->vval.v_string,
4016 var2.vval.v_string) == 0;
4017 if (type == TYPE_NEQUAL)
4018 n1 = !n1;
4019 }
4020 }
4021
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 /*
4023 * If one of the two variables is a number, compare as a number.
4024 * When using "=~" or "!~", always compare as string.
4025 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004026 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4028 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004029 n1 = get_tv_number(rettv);
4030 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 switch (type)
4032 {
4033 case TYPE_EQUAL: n1 = (n1 == n2); break;
4034 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4035 case TYPE_GREATER: n1 = (n1 > n2); break;
4036 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4037 case TYPE_SMALLER: n1 = (n1 < n2); break;
4038 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4039 case TYPE_UNKNOWN:
4040 case TYPE_MATCH:
4041 case TYPE_NOMATCH: break; /* avoid gcc warning */
4042 }
4043 }
4044 else
4045 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004046 s1 = get_tv_string_buf(rettv, buf1);
4047 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4049 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4050 else
4051 i = 0;
4052 n1 = FALSE;
4053 switch (type)
4054 {
4055 case TYPE_EQUAL: n1 = (i == 0); break;
4056 case TYPE_NEQUAL: n1 = (i != 0); break;
4057 case TYPE_GREATER: n1 = (i > 0); break;
4058 case TYPE_GEQUAL: n1 = (i >= 0); break;
4059 case TYPE_SMALLER: n1 = (i < 0); break;
4060 case TYPE_SEQUAL: n1 = (i <= 0); break;
4061
4062 case TYPE_MATCH:
4063 case TYPE_NOMATCH:
4064 /* avoid 'l' flag in 'cpoptions' */
4065 save_cpo = p_cpo;
4066 p_cpo = (char_u *)"";
4067 regmatch.regprog = vim_regcomp(s2,
4068 RE_MAGIC + RE_STRING);
4069 regmatch.rm_ic = ic;
4070 if (regmatch.regprog != NULL)
4071 {
4072 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4073 vim_free(regmatch.regprog);
4074 if (type == TYPE_NOMATCH)
4075 n1 = !n1;
4076 }
4077 p_cpo = save_cpo;
4078 break;
4079
4080 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4081 }
4082 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004083 clear_tv(rettv);
4084 clear_tv(&var2);
4085 rettv->v_type = VAR_NUMBER;
4086 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088 }
4089
4090 return OK;
4091}
4092
4093/*
4094 * Handle fourth level expression:
4095 * + number addition
4096 * - number subtraction
4097 * . string concatenation
4098 *
4099 * "arg" must point to the first non-white of the expression.
4100 * "arg" is advanced to the next non-white after the recognized expression.
4101 *
4102 * Return OK or FAIL.
4103 */
4104 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004105eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 int evaluate;
4109{
Bram Moolenaar33570922005-01-25 22:26:29 +00004110 typval_T var2;
4111 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 int op;
4113 long n1, n2;
4114 char_u *s1, *s2;
4115 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4116 char_u *p;
4117
4118 /*
4119 * Get the first variable.
4120 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 return FAIL;
4123
4124 /*
4125 * Repeat computing, until no '+', '-' or '.' is following.
4126 */
4127 for (;;)
4128 {
4129 op = **arg;
4130 if (op != '+' && op != '-' && op != '.')
4131 break;
4132
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004133 if (op != '+' || rettv->v_type != VAR_LIST)
4134 {
4135 /* For "list + ...", an illegal use of the first operand as
4136 * a number cannot be determined before evaluating the 2nd
4137 * operand: if this is also a list, all is ok.
4138 * For "something . ...", "something - ..." or "non-list + ...",
4139 * we know that the first operand needs to be a string or number
4140 * without evaluating the 2nd operand. So check before to avoid
4141 * side effects after an error. */
4142 if (evaluate && get_tv_string_chk(rettv) == NULL)
4143 {
4144 clear_tv(rettv);
4145 return FAIL;
4146 }
4147 }
4148
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 /*
4150 * Get the second variable.
4151 */
4152 *arg = skipwhite(*arg + 1);
4153 if (eval6(arg, &var2, evaluate) == FAIL)
4154 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 return FAIL;
4157 }
4158
4159 if (evaluate)
4160 {
4161 /*
4162 * Compute the result.
4163 */
4164 if (op == '.')
4165 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004166 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4167 s2 = get_tv_string_buf_chk(&var2, buf2);
4168 if (s2 == NULL) /* type error ? */
4169 {
4170 clear_tv(rettv);
4171 clear_tv(&var2);
4172 return FAIL;
4173 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004174 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004175 clear_tv(rettv);
4176 rettv->v_type = VAR_STRING;
4177 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004179 else if (op == '+' && rettv->v_type == VAR_LIST
4180 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004181 {
4182 /* concatenate Lists */
4183 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4184 &var3) == FAIL)
4185 {
4186 clear_tv(rettv);
4187 clear_tv(&var2);
4188 return FAIL;
4189 }
4190 clear_tv(rettv);
4191 *rettv = var3;
4192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 else
4194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004195 int error = FALSE;
4196
4197 n1 = get_tv_number_chk(rettv, &error);
4198 if (error)
4199 {
4200 /* This can only happen for "list + non-list".
4201 * For "non-list + ..." or "something - ...", we returned
4202 * before evaluating the 2nd operand. */
4203 clear_tv(rettv);
4204 return FAIL;
4205 }
4206 n2 = get_tv_number_chk(&var2, &error);
4207 if (error)
4208 {
4209 clear_tv(rettv);
4210 clear_tv(&var2);
4211 return FAIL;
4212 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 if (op == '+')
4215 n1 = n1 + n2;
4216 else
4217 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218 rettv->v_type = VAR_NUMBER;
4219 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004221 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
4223 }
4224 return OK;
4225}
4226
4227/*
4228 * Handle fifth level expression:
4229 * * number multiplication
4230 * / number division
4231 * % number modulo
4232 *
4233 * "arg" must point to the first non-white of the expression.
4234 * "arg" is advanced to the next non-white after the recognized expression.
4235 *
4236 * Return OK or FAIL.
4237 */
4238 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 int evaluate;
4243{
Bram Moolenaar33570922005-01-25 22:26:29 +00004244 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 int op;
4246 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004247 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
4249 /*
4250 * Get the first variable.
4251 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 return FAIL;
4254
4255 /*
4256 * Repeat computing, until no '*', '/' or '%' is following.
4257 */
4258 for (;;)
4259 {
4260 op = **arg;
4261 if (op != '*' && op != '/' && op != '%')
4262 break;
4263
4264 if (evaluate)
4265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004268 if (error)
4269 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271 else
4272 n1 = 0;
4273
4274 /*
4275 * Get the second variable.
4276 */
4277 *arg = skipwhite(*arg + 1);
4278 if (eval7(arg, &var2, evaluate) == FAIL)
4279 return FAIL;
4280
4281 if (evaluate)
4282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004285 if (error)
4286 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
4288 /*
4289 * Compute the result.
4290 */
4291 if (op == '*')
4292 n1 = n1 * n2;
4293 else if (op == '/')
4294 {
4295 if (n2 == 0) /* give an error message? */
4296 n1 = 0x7fffffffL;
4297 else
4298 n1 = n1 / n2;
4299 }
4300 else
4301 {
4302 if (n2 == 0) /* give an error message? */
4303 n1 = 0;
4304 else
4305 n1 = n1 % n2;
4306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004307 rettv->v_type = VAR_NUMBER;
4308 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
4310 }
4311
4312 return OK;
4313}
4314
4315/*
4316 * Handle sixth level expression:
4317 * number number constant
4318 * "string" string contstant
4319 * 'string' literal string contstant
4320 * &option-name option value
4321 * @r register contents
4322 * identifier variable value
4323 * function() function call
4324 * $VAR environment variable
4325 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004326 * [expr, expr] List
4327 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 *
4329 * Also handle:
4330 * ! in front logical NOT
4331 * - in front unary minus
4332 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004333 * trailing [] subscript in String or List
4334 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 *
4336 * "arg" must point to the first non-white of the expression.
4337 * "arg" is advanced to the next non-white after the recognized expression.
4338 *
4339 * Return OK or FAIL.
4340 */
4341 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 int evaluate;
4346{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 long n;
4348 int len;
4349 char_u *s;
4350 int val;
4351 char_u *start_leader, *end_leader;
4352 int ret = OK;
4353 char_u *alias;
4354
4355 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004357 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004359 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
4361 /*
4362 * Skip '!' and '-' characters. They are handled later.
4363 */
4364 start_leader = *arg;
4365 while (**arg == '!' || **arg == '-' || **arg == '+')
4366 *arg = skipwhite(*arg + 1);
4367 end_leader = *arg;
4368
4369 switch (**arg)
4370 {
4371 /*
4372 * Number constant.
4373 */
4374 case '0':
4375 case '1':
4376 case '2':
4377 case '3':
4378 case '4':
4379 case '5':
4380 case '6':
4381 case '7':
4382 case '8':
4383 case '9':
4384 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4385 *arg += len;
4386 if (evaluate)
4387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004388 rettv->v_type = VAR_NUMBER;
4389 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 }
4391 break;
4392
4393 /*
4394 * String constant: "string".
4395 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 break;
4398
4399 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004400 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004402 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004403 break;
4404
4405 /*
4406 * List: [expr, expr]
4407 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004408 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 break;
4410
4411 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004412 * Dictionary: {key: val, key: val}
4413 */
4414 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4415 break;
4416
4417 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004418 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004420 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 break;
4422
4423 /*
4424 * Environment variable: $VAR.
4425 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004426 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 break;
4428
4429 /*
4430 * Register contents: @r.
4431 */
4432 case '@': ++*arg;
4433 if (evaluate)
4434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004435 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004436 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438 if (**arg != NUL)
4439 ++*arg;
4440 break;
4441
4442 /*
4443 * nested expression: (expression).
4444 */
4445 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004446 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 if (**arg == ')')
4448 ++*arg;
4449 else if (ret == OK)
4450 {
4451 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004452 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 ret = FAIL;
4454 }
4455 break;
4456
Bram Moolenaar8c711452005-01-14 21:53:12 +00004457 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 break;
4459 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004460
4461 if (ret == NOTDONE)
4462 {
4463 /*
4464 * Must be a variable or function name.
4465 * Can also be a curly-braces kind of name: {expr}.
4466 */
4467 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004468 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004469 if (alias != NULL)
4470 s = alias;
4471
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004472 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004473 ret = FAIL;
4474 else
4475 {
4476 if (**arg == '(') /* recursive! */
4477 {
4478 /* If "s" is the name of a variable of type VAR_FUNC
4479 * use its contents. */
4480 s = deref_func_name(s, &len);
4481
4482 /* Invoke the function. */
4483 ret = get_func_tv(s, len, rettv, arg,
4484 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004485 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004486 /* Stop the expression evaluation when immediately
4487 * aborting on error, or when an interrupt occurred or
4488 * an exception was thrown but not caught. */
4489 if (aborting())
4490 {
4491 if (ret == OK)
4492 clear_tv(rettv);
4493 ret = FAIL;
4494 }
4495 }
4496 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004497 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004498 else
4499 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004500 }
4501
4502 if (alias != NULL)
4503 vim_free(alias);
4504 }
4505
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 *arg = skipwhite(*arg);
4507
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004508 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4509 * expr(expr). */
4510 if (ret == OK)
4511 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512
4513 /*
4514 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4515 */
4516 if (ret == OK && evaluate && end_leader > start_leader)
4517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004518 int error = FALSE;
4519
4520 val = get_tv_number_chk(rettv, &error);
4521 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004523 clear_tv(rettv);
4524 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004526 else
4527 {
4528 while (end_leader > start_leader)
4529 {
4530 --end_leader;
4531 if (*end_leader == '!')
4532 val = !val;
4533 else if (*end_leader == '-')
4534 val = -val;
4535 }
4536 clear_tv(rettv);
4537 rettv->v_type = VAR_NUMBER;
4538 rettv->vval.v_number = val;
4539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 }
4541
4542 return ret;
4543}
4544
4545/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004546 * Evaluate an "[expr]" or "[expr:expr]" index.
4547 * "*arg" points to the '['.
4548 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4549 */
4550 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004551eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004552 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004553 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004554 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004555 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004556{
4557 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004558 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004559 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004560 long len = -1;
4561 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004562 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004563 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004564
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004565 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004566 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004567 if (verbose)
4568 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004569 return FAIL;
4570 }
4571
Bram Moolenaar8c711452005-01-14 21:53:12 +00004572 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004573 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004574 /*
4575 * dict.name
4576 */
4577 key = *arg + 1;
4578 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4579 ;
4580 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004581 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004582 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 }
4584 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004585 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004586 /*
4587 * something[idx]
4588 *
4589 * Get the (first) variable from inside the [].
4590 */
4591 *arg = skipwhite(*arg + 1);
4592 if (**arg == ':')
4593 empty1 = TRUE;
4594 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4595 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004596 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4597 {
4598 /* not a number or string */
4599 clear_tv(&var1);
4600 return FAIL;
4601 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004602
4603 /*
4604 * Get the second variable from inside the [:].
4605 */
4606 if (**arg == ':')
4607 {
4608 range = TRUE;
4609 *arg = skipwhite(*arg + 1);
4610 if (**arg == ']')
4611 empty2 = TRUE;
4612 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004614 if (!empty1)
4615 clear_tv(&var1);
4616 return FAIL;
4617 }
4618 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4619 {
4620 /* not a number or string */
4621 if (!empty1)
4622 clear_tv(&var1);
4623 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004624 return FAIL;
4625 }
4626 }
4627
4628 /* Check for the ']'. */
4629 if (**arg != ']')
4630 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004631 if (verbose)
4632 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004633 clear_tv(&var1);
4634 if (range)
4635 clear_tv(&var2);
4636 return FAIL;
4637 }
4638 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004639 }
4640
4641 if (evaluate)
4642 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004643 n1 = 0;
4644 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004646 n1 = get_tv_number(&var1);
4647 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004648 }
4649 if (range)
4650 {
4651 if (empty2)
4652 n2 = -1;
4653 else
4654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004655 n2 = get_tv_number(&var2);
4656 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004657 }
4658 }
4659
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004660 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004661 {
4662 case VAR_NUMBER:
4663 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004664 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004665 len = (long)STRLEN(s);
4666 if (range)
4667 {
4668 /* The resulting variable is a substring. If the indexes
4669 * are out of range the result is empty. */
4670 if (n1 < 0)
4671 {
4672 n1 = len + n1;
4673 if (n1 < 0)
4674 n1 = 0;
4675 }
4676 if (n2 < 0)
4677 n2 = len + n2;
4678 else if (n2 >= len)
4679 n2 = len;
4680 if (n1 >= len || n2 < 0 || n1 > n2)
4681 s = NULL;
4682 else
4683 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4684 }
4685 else
4686 {
4687 /* The resulting variable is a string of a single
4688 * character. If the index is too big or negative the
4689 * result is empty. */
4690 if (n1 >= len || n1 < 0)
4691 s = NULL;
4692 else
4693 s = vim_strnsave(s + n1, 1);
4694 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004695 clear_tv(rettv);
4696 rettv->v_type = VAR_STRING;
4697 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004698 break;
4699
4700 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004701 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004702 if (n1 < 0)
4703 n1 = len + n1;
4704 if (!empty1 && (n1 < 0 || n1 >= len))
4705 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004706 if (verbose)
4707 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004708 return FAIL;
4709 }
4710 if (range)
4711 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004712 list_T *l;
4713 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004714
4715 if (n2 < 0)
4716 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004717 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004719 if (verbose)
4720 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 return FAIL;
4722 }
4723 l = list_alloc();
4724 if (l == NULL)
4725 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004726 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004727 n1 <= n2; ++n1)
4728 {
4729 if (list_append_tv(l, &item->li_tv) == FAIL)
4730 {
4731 list_free(l);
4732 return FAIL;
4733 }
4734 item = item->li_next;
4735 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004736 clear_tv(rettv);
4737 rettv->v_type = VAR_LIST;
4738 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004739 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004740 }
4741 else
4742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004743 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004744 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004745 clear_tv(rettv);
4746 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747 }
4748 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004749
4750 case VAR_DICT:
4751 if (range)
4752 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004753 if (verbose)
4754 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004755 if (len == -1)
4756 clear_tv(&var1);
4757 return FAIL;
4758 }
4759 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004760 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004761
4762 if (len == -1)
4763 {
4764 key = get_tv_string(&var1);
4765 if (*key == NUL)
4766 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004767 if (verbose)
4768 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004769 clear_tv(&var1);
4770 return FAIL;
4771 }
4772 }
4773
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004774 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004775
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004776 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004777 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004778 if (len == -1)
4779 clear_tv(&var1);
4780 if (item == NULL)
4781 return FAIL;
4782
4783 copy_tv(&item->di_tv, &var1);
4784 clear_tv(rettv);
4785 *rettv = var1;
4786 }
4787 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004788 }
4789 }
4790
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004791 return OK;
4792}
4793
4794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 * Get an option value.
4796 * "arg" points to the '&' or '+' before the option name.
4797 * "arg" is advanced to character after the option name.
4798 * Return OK or FAIL.
4799 */
4800 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004801get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004803 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 int evaluate;
4805{
4806 char_u *option_end;
4807 long numval;
4808 char_u *stringval;
4809 int opt_type;
4810 int c;
4811 int working = (**arg == '+'); /* has("+option") */
4812 int ret = OK;
4813 int opt_flags;
4814
4815 /*
4816 * Isolate the option name and find its value.
4817 */
4818 option_end = find_option_end(arg, &opt_flags);
4819 if (option_end == NULL)
4820 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 EMSG2(_("E112: Option name missing: %s"), *arg);
4823 return FAIL;
4824 }
4825
4826 if (!evaluate)
4827 {
4828 *arg = option_end;
4829 return OK;
4830 }
4831
4832 c = *option_end;
4833 *option_end = NUL;
4834 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004835 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836
4837 if (opt_type == -3) /* invalid name */
4838 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 EMSG2(_("E113: Unknown option: %s"), *arg);
4841 ret = FAIL;
4842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004843 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 {
4845 if (opt_type == -2) /* hidden string option */
4846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004847 rettv->v_type = VAR_STRING;
4848 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 }
4850 else if (opt_type == -1) /* hidden number option */
4851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 rettv->v_type = VAR_NUMBER;
4853 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
4855 else if (opt_type == 1) /* number option */
4856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004857 rettv->v_type = VAR_NUMBER;
4858 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 }
4860 else /* string option */
4861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004862 rettv->v_type = VAR_STRING;
4863 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 }
4865 }
4866 else if (working && (opt_type == -2 || opt_type == -1))
4867 ret = FAIL;
4868
4869 *option_end = c; /* put back for error messages */
4870 *arg = option_end;
4871
4872 return ret;
4873}
4874
4875/*
4876 * Allocate a variable for a string constant.
4877 * Return OK or FAIL.
4878 */
4879 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 int evaluate;
4884{
4885 char_u *p;
4886 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 int extra = 0;
4888
4889 /*
4890 * Find the end of the string, skipping backslashed characters.
4891 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004892 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
4894 if (*p == '\\' && p[1] != NUL)
4895 {
4896 ++p;
4897 /* A "\<x>" form occupies at least 4 characters, and produces up
4898 * to 6 characters: reserve space for 2 extra */
4899 if (*p == '<')
4900 extra += 2;
4901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903
4904 if (*p != '"')
4905 {
4906 EMSG2(_("E114: Missing quote: %s"), *arg);
4907 return FAIL;
4908 }
4909
4910 /* If only parsing, set *arg and return here */
4911 if (!evaluate)
4912 {
4913 *arg = p + 1;
4914 return OK;
4915 }
4916
4917 /*
4918 * Copy the string into allocated memory, handling backslashed
4919 * characters.
4920 */
4921 name = alloc((unsigned)(p - *arg + extra));
4922 if (name == NULL)
4923 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004924 rettv->v_type = VAR_STRING;
4925 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
Bram Moolenaar8c711452005-01-14 21:53:12 +00004927 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
4929 if (*p == '\\')
4930 {
4931 switch (*++p)
4932 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004933 case 'b': *name++ = BS; ++p; break;
4934 case 'e': *name++ = ESC; ++p; break;
4935 case 'f': *name++ = FF; ++p; break;
4936 case 'n': *name++ = NL; ++p; break;
4937 case 'r': *name++ = CAR; ++p; break;
4938 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
4940 case 'X': /* hex: "\x1", "\x12" */
4941 case 'x':
4942 case 'u': /* Unicode: "\u0023" */
4943 case 'U':
4944 if (vim_isxdigit(p[1]))
4945 {
4946 int n, nr;
4947 int c = toupper(*p);
4948
4949 if (c == 'X')
4950 n = 2;
4951 else
4952 n = 4;
4953 nr = 0;
4954 while (--n >= 0 && vim_isxdigit(p[1]))
4955 {
4956 ++p;
4957 nr = (nr << 4) + hex2nr(*p);
4958 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004959 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960#ifdef FEAT_MBYTE
4961 /* For "\u" store the number according to
4962 * 'encoding'. */
4963 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004964 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 else
4966#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004967 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 break;
4970
4971 /* octal: "\1", "\12", "\123" */
4972 case '0':
4973 case '1':
4974 case '2':
4975 case '3':
4976 case '4':
4977 case '5':
4978 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004979 case '7': *name = *p++ - '0';
4980 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 *name = (*name << 3) + *p++ - '0';
4983 if (*p >= '0' && *p <= '7')
4984 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 break;
4988
4989 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004990 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 if (extra != 0)
4992 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004993 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 break;
4995 }
4996 /* FALLTHROUGH */
4997
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 break;
5000 }
5001 }
5002 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005003 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005006 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 *arg = p + 1;
5008
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 return OK;
5010}
5011
5012/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005013 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 * Return OK or FAIL.
5015 */
5016 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005017get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 int evaluate;
5021{
5022 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005023 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005024 int reduce = 0;
5025
5026 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005028 */
5029 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5030 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005032 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005033 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005034 break;
5035 ++reduce;
5036 ++p;
5037 }
5038 }
5039
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 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005043 return FAIL;
5044 }
5045
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005047 if (!evaluate)
5048 {
5049 *arg = p + 1;
5050 return OK;
5051 }
5052
5053 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005055 */
5056 str = alloc((unsigned)((p - *arg) - reduce));
5057 if (str == NULL)
5058 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059 rettv->v_type = VAR_STRING;
5060 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005061
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005063 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005064 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005065 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005067 break;
5068 ++p;
5069 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005071 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005072 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005073 *arg = p + 1;
5074
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005075 return OK;
5076}
5077
5078/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005079 * Allocate a variable for a List and fill it from "*arg".
5080 * Return OK or FAIL.
5081 */
5082 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005084 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005085 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005086 int evaluate;
5087{
Bram Moolenaar33570922005-01-25 22:26:29 +00005088 list_T *l = NULL;
5089 typval_T tv;
5090 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005091
5092 if (evaluate)
5093 {
5094 l = list_alloc();
5095 if (l == NULL)
5096 return FAIL;
5097 }
5098
5099 *arg = skipwhite(*arg + 1);
5100 while (**arg != ']' && **arg != NUL)
5101 {
5102 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5103 goto failret;
5104 if (evaluate)
5105 {
5106 item = listitem_alloc();
5107 if (item != NULL)
5108 {
5109 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005110 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005111 list_append(l, item);
5112 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005113 else
5114 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005115 }
5116
5117 if (**arg == ']')
5118 break;
5119 if (**arg != ',')
5120 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005122 goto failret;
5123 }
5124 *arg = skipwhite(*arg + 1);
5125 }
5126
5127 if (**arg != ']')
5128 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005130failret:
5131 if (evaluate)
5132 list_free(l);
5133 return FAIL;
5134 }
5135
5136 *arg = skipwhite(*arg + 1);
5137 if (evaluate)
5138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005139 rettv->v_type = VAR_LIST;
5140 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141 ++l->lv_refcount;
5142 }
5143
5144 return OK;
5145}
5146
5147/*
5148 * Allocate an empty header for a list.
5149 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005150 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005151list_alloc()
5152{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005153 list_T *l;
5154
5155 l = (list_T *)alloc_clear(sizeof(list_T));
5156 if (l != NULL)
5157 {
5158 /* Prepend the list to the list of lists for garbage collection. */
5159 if (first_list != NULL)
5160 first_list->lv_used_prev = l;
5161 l->lv_used_prev = NULL;
5162 l->lv_used_next = first_list;
5163 first_list = l;
5164 }
5165 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005166}
5167
5168/*
5169 * Unreference a list: decrement the reference count and free it when it
5170 * becomes zero.
5171 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005172 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005173list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005174 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005175{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005176 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5177 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005178}
5179
5180/*
5181 * Free a list, including all items it points to.
5182 * Ignores the reference count.
5183 */
5184 static void
5185list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005186 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187{
Bram Moolenaar33570922005-01-25 22:26:29 +00005188 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005189
Bram Moolenaard9fba312005-06-26 22:34:35 +00005190 /* Avoid that recursive reference to the list frees us again. */
5191 l->lv_refcount = DEL_REFCOUNT;
5192
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005193 /* Remove the list from the list of lists for garbage collection. */
5194 if (l->lv_used_prev == NULL)
5195 first_list = l->lv_used_next;
5196 else
5197 l->lv_used_prev->lv_used_next = l->lv_used_next;
5198 if (l->lv_used_next != NULL)
5199 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5200
Bram Moolenaard9fba312005-06-26 22:34:35 +00005201 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005202 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005203 /* Remove the item before deleting it. */
5204 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005205 listitem_free(item);
5206 }
5207 vim_free(l);
5208}
5209
5210/*
5211 * Allocate a list item.
5212 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005213 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005214listitem_alloc()
5215{
Bram Moolenaar33570922005-01-25 22:26:29 +00005216 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005217}
5218
5219/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005220 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005221 */
5222 static void
5223listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005224 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005226 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005227 vim_free(item);
5228}
5229
5230/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005231 * Remove a list item from a List and free it. Also clears the value.
5232 */
5233 static void
5234listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005235 list_T *l;
5236 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005237{
5238 list_remove(l, item, item);
5239 listitem_free(item);
5240}
5241
5242/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243 * Get the number of items in a list.
5244 */
5245 static long
5246list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005247 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005248{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249 if (l == NULL)
5250 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005251 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252}
5253
5254/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005255 * Return TRUE when two lists have exactly the same values.
5256 */
5257 static int
5258list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005259 list_T *l1;
5260 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005261 int ic; /* ignore case for strings */
5262{
Bram Moolenaar33570922005-01-25 22:26:29 +00005263 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005264
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005265 if (list_len(l1) != list_len(l2))
5266 return FALSE;
5267
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005268 for (item1 = l1->lv_first, item2 = l2->lv_first;
5269 item1 != NULL && item2 != NULL;
5270 item1 = item1->li_next, item2 = item2->li_next)
5271 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5272 return FALSE;
5273 return item1 == NULL && item2 == NULL;
5274}
5275
5276/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005277 * Return TRUE when two dictionaries have exactly the same key/values.
5278 */
5279 static int
5280dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005281 dict_T *d1;
5282 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005283 int ic; /* ignore case for strings */
5284{
Bram Moolenaar33570922005-01-25 22:26:29 +00005285 hashitem_T *hi;
5286 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005287 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005288
5289 if (dict_len(d1) != dict_len(d2))
5290 return FALSE;
5291
Bram Moolenaar33570922005-01-25 22:26:29 +00005292 todo = d1->dv_hashtab.ht_used;
5293 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005294 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005295 if (!HASHITEM_EMPTY(hi))
5296 {
5297 item2 = dict_find(d2, hi->hi_key, -1);
5298 if (item2 == NULL)
5299 return FALSE;
5300 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5301 return FALSE;
5302 --todo;
5303 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005304 }
5305 return TRUE;
5306}
5307
5308/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005309 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005310 * Compares the items just like "==" would compare them, but strings and
5311 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005312 */
5313 static int
5314tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005315 typval_T *tv1;
5316 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005317 int ic; /* ignore case */
5318{
5319 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005321
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005322 if (tv1->v_type != tv2->v_type)
5323 return FALSE;
5324
5325 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005326 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005327 case VAR_LIST:
5328 /* recursive! */
5329 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5330
5331 case VAR_DICT:
5332 /* recursive! */
5333 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5334
5335 case VAR_FUNC:
5336 return (tv1->vval.v_string != NULL
5337 && tv2->vval.v_string != NULL
5338 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5339
5340 case VAR_NUMBER:
5341 return tv1->vval.v_number == tv2->vval.v_number;
5342
5343 case VAR_STRING:
5344 s1 = get_tv_string_buf(tv1, buf1);
5345 s2 = get_tv_string_buf(tv2, buf2);
5346 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005347 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005348
5349 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005350 return TRUE;
5351}
5352
5353/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 * Locate item with index "n" in list "l" and return it.
5355 * A negative index is counted from the end; -1 is the last item.
5356 * Returns NULL when "n" is out of range.
5357 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005358 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005360 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 long n;
5362{
Bram Moolenaar33570922005-01-25 22:26:29 +00005363 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364 long idx;
5365
5366 if (l == NULL)
5367 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005368
5369 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005371 n = l->lv_len + n;
5372
5373 /* Check for index out of range. */
5374 if (n < 0 || n >= l->lv_len)
5375 return NULL;
5376
5377 /* When there is a cached index may start search from there. */
5378 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005380 if (n < l->lv_idx / 2)
5381 {
5382 /* closest to the start of the list */
5383 item = l->lv_first;
5384 idx = 0;
5385 }
5386 else if (n > (l->lv_idx + l->lv_len) / 2)
5387 {
5388 /* closest to the end of the list */
5389 item = l->lv_last;
5390 idx = l->lv_len - 1;
5391 }
5392 else
5393 {
5394 /* closest to the cached index */
5395 item = l->lv_idx_item;
5396 idx = l->lv_idx;
5397 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 }
5399 else
5400 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005401 if (n < l->lv_len / 2)
5402 {
5403 /* closest to the start of the list */
5404 item = l->lv_first;
5405 idx = 0;
5406 }
5407 else
5408 {
5409 /* closest to the end of the list */
5410 item = l->lv_last;
5411 idx = l->lv_len - 1;
5412 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005414
5415 while (n > idx)
5416 {
5417 /* search forward */
5418 item = item->li_next;
5419 ++idx;
5420 }
5421 while (n < idx)
5422 {
5423 /* search backward */
5424 item = item->li_prev;
5425 --idx;
5426 }
5427
5428 /* cache the used index */
5429 l->lv_idx = idx;
5430 l->lv_idx_item = item;
5431
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 return item;
5433}
5434
5435/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005436 * Locate "item" list "l" and return its index.
5437 * Returns -1 when "item" is not in the list.
5438 */
5439 static long
5440list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005441 list_T *l;
5442 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005443{
5444 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005445 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005446
5447 if (l == NULL)
5448 return -1;
5449 idx = 0;
5450 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5451 ++idx;
5452 if (li == NULL)
5453 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005454 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005455}
5456
5457/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 * Append item "item" to the end of list "l".
5459 */
5460 static void
5461list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005462 list_T *l;
5463 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464{
5465 if (l->lv_last == NULL)
5466 {
5467 /* empty list */
5468 l->lv_first = item;
5469 l->lv_last = item;
5470 item->li_prev = NULL;
5471 }
5472 else
5473 {
5474 l->lv_last->li_next = item;
5475 item->li_prev = l->lv_last;
5476 l->lv_last = item;
5477 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005478 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 item->li_next = NULL;
5480}
5481
5482/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005483 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005484 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 */
5486 static int
5487list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005488 list_T *l;
5489 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005491 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492
Bram Moolenaar05159a02005-02-26 23:04:13 +00005493 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005495 copy_tv(tv, &li->li_tv);
5496 list_append(l, li);
5497 return OK;
5498}
5499
5500/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005501 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005502 * Return FAIL when out of memory.
5503 */
5504 int
5505list_append_dict(list, dict)
5506 list_T *list;
5507 dict_T *dict;
5508{
5509 listitem_T *li = listitem_alloc();
5510
5511 if (li == NULL)
5512 return FAIL;
5513 li->li_tv.v_type = VAR_DICT;
5514 li->li_tv.v_lock = 0;
5515 li->li_tv.vval.v_dict = dict;
5516 list_append(list, li);
5517 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 return OK;
5519}
5520
5521/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005522 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005523 * If "item" is NULL append at the end.
5524 * Return FAIL when out of memory.
5525 */
5526 static int
5527list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005528 list_T *l;
5529 typval_T *tv;
5530 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005531{
Bram Moolenaar33570922005-01-25 22:26:29 +00005532 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005533
5534 if (ni == NULL)
5535 return FAIL;
5536 copy_tv(tv, &ni->li_tv);
5537 if (item == NULL)
5538 /* Append new item at end of list. */
5539 list_append(l, ni);
5540 else
5541 {
5542 /* Insert new item before existing item. */
5543 ni->li_prev = item->li_prev;
5544 ni->li_next = item;
5545 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005546 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005547 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005548 ++l->lv_idx;
5549 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005550 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005551 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005552 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005553 l->lv_idx_item = NULL;
5554 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005555 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005556 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005557 }
5558 return OK;
5559}
5560
5561/*
5562 * Extend "l1" with "l2".
5563 * If "bef" is NULL append at the end, otherwise insert before this item.
5564 * Returns FAIL when out of memory.
5565 */
5566 static int
5567list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005568 list_T *l1;
5569 list_T *l2;
5570 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005571{
Bram Moolenaar33570922005-01-25 22:26:29 +00005572 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005573
5574 for (item = l2->lv_first; item != NULL; item = item->li_next)
5575 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5576 return FAIL;
5577 return OK;
5578}
5579
5580/*
5581 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5582 * Return FAIL when out of memory.
5583 */
5584 static int
5585list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005586 list_T *l1;
5587 list_T *l2;
5588 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005589{
Bram Moolenaar33570922005-01-25 22:26:29 +00005590 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005591
5592 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005593 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005594 if (l == NULL)
5595 return FAIL;
5596 tv->v_type = VAR_LIST;
5597 tv->vval.v_list = l;
5598
5599 /* append all items from the second list */
5600 return list_extend(l, l2, NULL);
5601}
5602
5603/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005604 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005605 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005606 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005607 * Returns NULL when out of memory.
5608 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005609 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005610list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005611 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005613 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005614{
Bram Moolenaar33570922005-01-25 22:26:29 +00005615 list_T *copy;
5616 listitem_T *item;
5617 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618
5619 if (orig == NULL)
5620 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005621
5622 copy = list_alloc();
5623 if (copy != NULL)
5624 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005625 if (copyID != 0)
5626 {
5627 /* Do this before adding the items, because one of the items may
5628 * refer back to this list. */
5629 orig->lv_copyID = copyID;
5630 orig->lv_copylist = copy;
5631 }
5632 for (item = orig->lv_first; item != NULL && !got_int;
5633 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 {
5635 ni = listitem_alloc();
5636 if (ni == NULL)
5637 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005638 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005639 {
5640 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5641 {
5642 vim_free(ni);
5643 break;
5644 }
5645 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648 list_append(copy, ni);
5649 }
5650 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005651 if (item != NULL)
5652 {
5653 list_unref(copy);
5654 copy = NULL;
5655 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005656 }
5657
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005658 return copy;
5659}
5660
5661/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005662 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005663 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005664 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005665 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005666list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005667 list_T *l;
5668 listitem_T *item;
5669 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005670{
Bram Moolenaar33570922005-01-25 22:26:29 +00005671 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005672
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005673 /* notify watchers */
5674 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005675 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005676 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005677 list_fix_watch(l, ip);
5678 if (ip == item2)
5679 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005680 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005681
5682 if (item2->li_next == NULL)
5683 l->lv_last = item->li_prev;
5684 else
5685 item2->li_next->li_prev = item->li_prev;
5686 if (item->li_prev == NULL)
5687 l->lv_first = item2->li_next;
5688 else
5689 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005690 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005691}
5692
5693/*
5694 * Return an allocated string with the string representation of a list.
5695 * May return NULL.
5696 */
5697 static char_u *
5698list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005699 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005700{
5701 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702
5703 if (tv->vval.v_list == NULL)
5704 return NULL;
5705 ga_init2(&ga, (int)sizeof(char), 80);
5706 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005707 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5708 {
5709 vim_free(ga.ga_data);
5710 return NULL;
5711 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005712 ga_append(&ga, ']');
5713 ga_append(&ga, NUL);
5714 return (char_u *)ga.ga_data;
5715}
5716
5717/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005718 * Join list "l" into a string in "*gap", using separator "sep".
5719 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005720 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005721 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005722 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005723list_join(gap, l, sep, echo)
5724 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005725 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005726 char_u *sep;
5727 int echo;
5728{
5729 int first = TRUE;
5730 char_u *tofree;
5731 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005733 char_u *s;
5734
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005735 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005736 {
5737 if (first)
5738 first = FALSE;
5739 else
5740 ga_concat(gap, sep);
5741
5742 if (echo)
5743 s = echo_string(&item->li_tv, &tofree, numbuf);
5744 else
5745 s = tv2string(&item->li_tv, &tofree, numbuf);
5746 if (s != NULL)
5747 ga_concat(gap, s);
5748 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005749 if (s == NULL)
5750 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005751 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005752 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005753}
5754
5755/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005756 * Garbage collection for lists and dictionaries.
5757 *
5758 * We use reference counts to be able to free most items right away when they
5759 * are no longer used. But for composite items it's possible that it becomes
5760 * unused while the reference count is > 0: When there is a recursive
5761 * reference. Example:
5762 * :let l = [1, 2, 3]
5763 * :let d = {9: l}
5764 * :let l[1] = d
5765 *
5766 * Since this is quite unusual we handle this with garbage collection: every
5767 * once in a while find out which lists and dicts are not referenced from any
5768 * variable.
5769 *
5770 * Here is a good reference text about garbage collection (refers to Python
5771 * but it applies to all reference-counting mechanisms):
5772 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005773 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005774
5775/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005776 * Do garbage collection for lists and dicts.
5777 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005778 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005779 int
5780garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005781{
5782 dict_T *dd;
5783 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005784 int copyID = ++current_copyID;
5785 buf_T *buf;
5786 win_T *wp;
5787 int i;
5788 funccall_T *fc;
5789 int did_free = FALSE;
5790
5791 /*
5792 * 1. Go through all accessible variables and mark all lists and dicts
5793 * with copyID.
5794 */
5795 /* script-local variables */
5796 for (i = 1; i <= ga_scripts.ga_len; ++i)
5797 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5798
5799 /* buffer-local variables */
5800 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5801 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5802
5803 /* window-local variables */
5804 FOR_ALL_WINDOWS(wp)
5805 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5806
5807 /* global variables */
5808 set_ref_in_ht(&globvarht, copyID);
5809
5810 /* function-local variables */
5811 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5812 {
5813 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5814 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5815 }
5816
5817 /*
5818 * 2. Go through the list of dicts and free items without the copyID.
5819 */
5820 for (dd = first_dict; dd != NULL; )
5821 if (dd->dv_copyID != copyID)
5822 {
5823 dict_free(dd);
5824 did_free = TRUE;
5825
5826 /* restart, next dict may also have been freed */
5827 dd = first_dict;
5828 }
5829 else
5830 dd = dd->dv_used_next;
5831
5832 /*
5833 * 3. Go through the list of lists and free items without the copyID.
5834 */
5835 for (ll = first_list; ll != NULL; )
5836 if (ll->lv_copyID != copyID)
5837 {
5838 list_free(ll);
5839 did_free = TRUE;
5840
5841 /* restart, next dict may also have been freed */
5842 ll = first_list;
5843 }
5844 else
5845 ll = ll->lv_used_next;
5846
5847 return did_free;
5848}
5849
5850/*
5851 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5852 */
5853 static void
5854set_ref_in_ht(ht, copyID)
5855 hashtab_T *ht;
5856 int copyID;
5857{
5858 int todo;
5859 hashitem_T *hi;
5860
5861 todo = ht->ht_used;
5862 for (hi = ht->ht_array; todo > 0; ++hi)
5863 if (!HASHITEM_EMPTY(hi))
5864 {
5865 --todo;
5866 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5867 }
5868}
5869
5870/*
5871 * Mark all lists and dicts referenced through list "l" with "copyID".
5872 */
5873 static void
5874set_ref_in_list(l, copyID)
5875 list_T *l;
5876 int copyID;
5877{
5878 listitem_T *li;
5879
5880 for (li = l->lv_first; li != NULL; li = li->li_next)
5881 set_ref_in_item(&li->li_tv, copyID);
5882}
5883
5884/*
5885 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5886 */
5887 static void
5888set_ref_in_item(tv, copyID)
5889 typval_T *tv;
5890 int copyID;
5891{
5892 dict_T *dd;
5893 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005894
5895 switch (tv->v_type)
5896 {
5897 case VAR_DICT:
5898 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005899 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005900 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005901 /* Didn't see this dict yet. */
5902 dd->dv_copyID = copyID;
5903 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005904 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005905 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005906
5907 case VAR_LIST:
5908 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005909 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005910 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005911 /* Didn't see this list yet. */
5912 ll->lv_copyID = copyID;
5913 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005914 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005915 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005916 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005917 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005918}
5919
5920/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005921 * Allocate an empty header for a dictionary.
5922 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005923 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005924dict_alloc()
5925{
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005927
Bram Moolenaar33570922005-01-25 22:26:29 +00005928 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005929 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005930 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005931 /* Add the list to the hashtable for garbage collection. */
5932 if (first_dict != NULL)
5933 first_dict->dv_used_prev = d;
5934 d->dv_used_next = first_dict;
5935 d->dv_used_prev = NULL;
5936
Bram Moolenaar33570922005-01-25 22:26:29 +00005937 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005938 d->dv_lock = 0;
5939 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005940 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005941 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005942 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943}
5944
5945/*
5946 * Unreference a Dictionary: decrement the reference count and free it when it
5947 * becomes zero.
5948 */
5949 static void
5950dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005952{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005953 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
5954 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005955}
5956
5957/*
5958 * Free a Dictionary, including all items it contains.
5959 * Ignores the reference count.
5960 */
5961 static void
5962dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005963 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005964{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005965 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005966 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005967 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005968
Bram Moolenaard9fba312005-06-26 22:34:35 +00005969 /* Avoid that recursive reference to the dict frees us again. */
5970 d->dv_refcount = DEL_REFCOUNT;
5971
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005972 /* Remove the dict from the list of dicts for garbage collection. */
5973 if (d->dv_used_prev == NULL)
5974 first_dict = d->dv_used_next;
5975 else
5976 d->dv_used_prev->dv_used_next = d->dv_used_next;
5977 if (d->dv_used_next != NULL)
5978 d->dv_used_next->dv_used_prev = d->dv_used_prev;
5979
5980 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005981 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00005982 todo = d->dv_hashtab.ht_used;
5983 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005984 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005985 if (!HASHITEM_EMPTY(hi))
5986 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005987 /* Remove the item before deleting it, just in case there is
5988 * something recursive causing trouble. */
5989 di = HI2DI(hi);
5990 hash_remove(&d->dv_hashtab, hi);
5991 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005992 --todo;
5993 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005994 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005996 vim_free(d);
5997}
5998
5999/*
6000 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006001 * The "key" is copied to the new item.
6002 * Note that the value of the item "di_tv" still needs to be initialized!
6003 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006004 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006006dictitem_alloc(key)
6007 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006008{
Bram Moolenaar33570922005-01-25 22:26:29 +00006009 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006010
Bram Moolenaar33570922005-01-25 22:26:29 +00006011 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006012 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006013 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006014 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006015 di->di_flags = 0;
6016 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006017 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006018}
6019
6020/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006021 * Make a copy of a Dictionary item.
6022 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006024dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006025 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006026{
Bram Moolenaar33570922005-01-25 22:26:29 +00006027 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006028
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006030 if (di != NULL)
6031 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006032 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006033 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006034 copy_tv(&org->di_tv, &di->di_tv);
6035 }
6036 return di;
6037}
6038
6039/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006040 * Remove item "item" from Dictionary "dict" and free it.
6041 */
6042 static void
6043dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006044 dict_T *dict;
6045 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006046{
Bram Moolenaar33570922005-01-25 22:26:29 +00006047 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006048
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006050 if (HASHITEM_EMPTY(hi))
6051 EMSG2(_(e_intern2), "dictitem_remove()");
6052 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006054 dictitem_free(item);
6055}
6056
6057/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006058 * Free a dict item. Also clears the value.
6059 */
6060 static void
6061dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006063{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006064 clear_tv(&item->di_tv);
6065 vim_free(item);
6066}
6067
6068/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006069 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6070 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006071 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006072 * Returns NULL when out of memory.
6073 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006074 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006075dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006076 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006077 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006078 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006079{
Bram Moolenaar33570922005-01-25 22:26:29 +00006080 dict_T *copy;
6081 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006082 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006083 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006084
6085 if (orig == NULL)
6086 return NULL;
6087
6088 copy = dict_alloc();
6089 if (copy != NULL)
6090 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006091 if (copyID != 0)
6092 {
6093 orig->dv_copyID = copyID;
6094 orig->dv_copydict = copy;
6095 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006096 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006097 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006098 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006099 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006100 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006101 --todo;
6102
6103 di = dictitem_alloc(hi->hi_key);
6104 if (di == NULL)
6105 break;
6106 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006107 {
6108 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6109 copyID) == FAIL)
6110 {
6111 vim_free(di);
6112 break;
6113 }
6114 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006115 else
6116 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6117 if (dict_add(copy, di) == FAIL)
6118 {
6119 dictitem_free(di);
6120 break;
6121 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006122 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006123 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006124
Bram Moolenaare9a41262005-01-15 22:18:47 +00006125 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006126 if (todo > 0)
6127 {
6128 dict_unref(copy);
6129 copy = NULL;
6130 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006131 }
6132
6133 return copy;
6134}
6135
6136/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006137 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006138 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006139 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006140 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006141dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006142 dict_T *d;
6143 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006144{
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006146}
6147
Bram Moolenaar8c711452005-01-14 21:53:12 +00006148/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006149 * Add a number or string entry to dictionary "d".
6150 * When "str" is NULL use number "nr", otherwise use "str".
6151 * Returns FAIL when out of memory and when key already exists.
6152 */
6153 int
6154dict_add_nr_str(d, key, nr, str)
6155 dict_T *d;
6156 char *key;
6157 long nr;
6158 char_u *str;
6159{
6160 dictitem_T *item;
6161
6162 item = dictitem_alloc((char_u *)key);
6163 if (item == NULL)
6164 return FAIL;
6165 item->di_tv.v_lock = 0;
6166 if (str == NULL)
6167 {
6168 item->di_tv.v_type = VAR_NUMBER;
6169 item->di_tv.vval.v_number = nr;
6170 }
6171 else
6172 {
6173 item->di_tv.v_type = VAR_STRING;
6174 item->di_tv.vval.v_string = vim_strsave(str);
6175 }
6176 if (dict_add(d, item) == FAIL)
6177 {
6178 dictitem_free(item);
6179 return FAIL;
6180 }
6181 return OK;
6182}
6183
6184/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006185 * Get the number of items in a Dictionary.
6186 */
6187 static long
6188dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006189 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006190{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006191 if (d == NULL)
6192 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006193 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006194}
6195
6196/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006197 * Find item "key[len]" in Dictionary "d".
6198 * If "len" is negative use strlen(key).
6199 * Returns NULL when not found.
6200 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006201 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006202dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006203 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006204 char_u *key;
6205 int len;
6206{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006207#define AKEYLEN 200
6208 char_u buf[AKEYLEN];
6209 char_u *akey;
6210 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006211 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006212
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006213 if (len < 0)
6214 akey = key;
6215 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006216 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006217 tofree = akey = vim_strnsave(key, len);
6218 if (akey == NULL)
6219 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006220 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006221 else
6222 {
6223 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006224 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006225 akey = buf;
6226 }
6227
Bram Moolenaar33570922005-01-25 22:26:29 +00006228 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006229 vim_free(tofree);
6230 if (HASHITEM_EMPTY(hi))
6231 return NULL;
6232 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006233}
6234
6235/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006236 * Get a string item from a dictionary in allocated memory.
6237 * Returns NULL if the entry doesn't exist or out of memory.
6238 */
6239 char_u *
6240get_dict_string(d, key)
6241 dict_T *d;
6242 char_u *key;
6243{
6244 dictitem_T *di;
6245
6246 di = dict_find(d, key, -1);
6247 if (di == NULL)
6248 return NULL;
6249 return vim_strsave(get_tv_string(&di->di_tv));
6250}
6251
6252/*
6253 * Get a number item from a dictionary.
6254 * Returns 0 if the entry doesn't exist or out of memory.
6255 */
6256 long
6257get_dict_number(d, key)
6258 dict_T *d;
6259 char_u *key;
6260{
6261 dictitem_T *di;
6262
6263 di = dict_find(d, key, -1);
6264 if (di == NULL)
6265 return 0;
6266 return get_tv_number(&di->di_tv);
6267}
6268
6269/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006270 * Return an allocated string with the string representation of a Dictionary.
6271 * May return NULL.
6272 */
6273 static char_u *
6274dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006275 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006276{
6277 garray_T ga;
6278 int first = TRUE;
6279 char_u *tofree;
6280 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006281 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006282 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006283 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006284 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006285
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006286 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006287 return NULL;
6288 ga_init2(&ga, (int)sizeof(char), 80);
6289 ga_append(&ga, '{');
6290
Bram Moolenaar33570922005-01-25 22:26:29 +00006291 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006292 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006293 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006294 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006295 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006296 --todo;
6297
6298 if (first)
6299 first = FALSE;
6300 else
6301 ga_concat(&ga, (char_u *)", ");
6302
6303 tofree = string_quote(hi->hi_key, FALSE);
6304 if (tofree != NULL)
6305 {
6306 ga_concat(&ga, tofree);
6307 vim_free(tofree);
6308 }
6309 ga_concat(&ga, (char_u *)": ");
6310 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6311 if (s != NULL)
6312 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006313 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006314 if (s == NULL)
6315 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006316 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006317 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006318 if (todo > 0)
6319 {
6320 vim_free(ga.ga_data);
6321 return NULL;
6322 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006323
6324 ga_append(&ga, '}');
6325 ga_append(&ga, NUL);
6326 return (char_u *)ga.ga_data;
6327}
6328
6329/*
6330 * Allocate a variable for a Dictionary and fill it from "*arg".
6331 * Return OK or FAIL. Returns NOTDONE for {expr}.
6332 */
6333 static int
6334get_dict_tv(arg, rettv, evaluate)
6335 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006336 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006337 int evaluate;
6338{
Bram Moolenaar33570922005-01-25 22:26:29 +00006339 dict_T *d = NULL;
6340 typval_T tvkey;
6341 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006342 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006343 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006344 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006345 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006346
6347 /*
6348 * First check if it's not a curly-braces thing: {expr}.
6349 * Must do this without evaluating, otherwise a function may be called
6350 * twice. Unfortunately this means we need to call eval1() twice for the
6351 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006352 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006353 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006354 if (*start != '}')
6355 {
6356 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6357 return FAIL;
6358 if (*start == '}')
6359 return NOTDONE;
6360 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006361
6362 if (evaluate)
6363 {
6364 d = dict_alloc();
6365 if (d == NULL)
6366 return FAIL;
6367 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006368 tvkey.v_type = VAR_UNKNOWN;
6369 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006370
6371 *arg = skipwhite(*arg + 1);
6372 while (**arg != '}' && **arg != NUL)
6373 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006374 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006375 goto failret;
6376 if (**arg != ':')
6377 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006378 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006379 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006380 goto failret;
6381 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006382 key = get_tv_string_buf_chk(&tvkey, buf);
6383 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006385 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6386 if (key != NULL)
6387 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006388 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006389 goto failret;
6390 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006391
6392 *arg = skipwhite(*arg + 1);
6393 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6394 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006395 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006396 goto failret;
6397 }
6398 if (evaluate)
6399 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006400 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006401 if (item != NULL)
6402 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006403 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006404 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006405 clear_tv(&tv);
6406 goto failret;
6407 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006408 item = dictitem_alloc(key);
6409 clear_tv(&tvkey);
6410 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006411 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006412 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006413 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006414 if (dict_add(d, item) == FAIL)
6415 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006416 }
6417 }
6418
6419 if (**arg == '}')
6420 break;
6421 if (**arg != ',')
6422 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006423 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006424 goto failret;
6425 }
6426 *arg = skipwhite(*arg + 1);
6427 }
6428
6429 if (**arg != '}')
6430 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006431 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006432failret:
6433 if (evaluate)
6434 dict_free(d);
6435 return FAIL;
6436 }
6437
6438 *arg = skipwhite(*arg + 1);
6439 if (evaluate)
6440 {
6441 rettv->v_type = VAR_DICT;
6442 rettv->vval.v_dict = d;
6443 ++d->dv_refcount;
6444 }
6445
6446 return OK;
6447}
6448
Bram Moolenaar8c711452005-01-14 21:53:12 +00006449/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006450 * Return a string with the string representation of a variable.
6451 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006452 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006453 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006454 * May return NULL;
6455 */
6456 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006457echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006462 static int recurse = 0;
6463 char_u *r = NULL;
6464
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006466 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006467 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006468 *tofree = NULL;
6469 return NULL;
6470 }
6471 ++recurse;
6472
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006473 switch (tv->v_type)
6474 {
6475 case VAR_FUNC:
6476 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006477 r = tv->vval.v_string;
6478 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006479 case VAR_LIST:
6480 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006481 r = *tofree;
6482 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006483 case VAR_DICT:
6484 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006485 r = *tofree;
6486 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006487 case VAR_STRING:
6488 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006489 *tofree = NULL;
6490 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006491 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006492 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006493 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006494 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006495 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006496
6497 --recurse;
6498 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006499}
6500
6501/*
6502 * Return a string with the string representation of a variable.
6503 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6504 * "numbuf" is used for a number.
6505 * Puts quotes around strings, so that they can be parsed back by eval().
6506 * May return NULL;
6507 */
6508 static char_u *
6509tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006510 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006511 char_u **tofree;
6512 char_u *numbuf;
6513{
6514 switch (tv->v_type)
6515 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006516 case VAR_FUNC:
6517 *tofree = string_quote(tv->vval.v_string, TRUE);
6518 return *tofree;
6519 case VAR_STRING:
6520 *tofree = string_quote(tv->vval.v_string, FALSE);
6521 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006522 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006523 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006524 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006525 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006526 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006527 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006529 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530}
6531
6532/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006533 * Return string "str" in ' quotes, doubling ' characters.
6534 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006535 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006536 */
6537 static char_u *
6538string_quote(str, function)
6539 char_u *str;
6540 int function;
6541{
Bram Moolenaar33570922005-01-25 22:26:29 +00006542 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006543 char_u *p, *r, *s;
6544
Bram Moolenaar33570922005-01-25 22:26:29 +00006545 len = (function ? 13 : 3);
6546 if (str != NULL)
6547 {
6548 len += STRLEN(str);
6549 for (p = str; *p != NUL; mb_ptr_adv(p))
6550 if (*p == '\'')
6551 ++len;
6552 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006553 s = r = alloc(len);
6554 if (r != NULL)
6555 {
6556 if (function)
6557 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006558 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006559 r += 10;
6560 }
6561 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006562 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 if (str != NULL)
6564 for (p = str; *p != NUL; )
6565 {
6566 if (*p == '\'')
6567 *r++ = '\'';
6568 MB_COPY_CHAR(p, r);
6569 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006570 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006571 if (function)
6572 *r++ = ')';
6573 *r++ = NUL;
6574 }
6575 return s;
6576}
6577
6578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 * Get the value of an environment variable.
6580 * "arg" is pointing to the '$'. It is advanced to after the name.
6581 * If the environment variable was not set, silently assume it is empty.
6582 * Always return OK.
6583 */
6584 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006585get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 int evaluate;
6589{
6590 char_u *string = NULL;
6591 int len;
6592 int cc;
6593 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006594 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595
6596 ++*arg;
6597 name = *arg;
6598 len = get_env_len(arg);
6599 if (evaluate)
6600 {
6601 if (len != 0)
6602 {
6603 cc = name[len];
6604 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006605 /* first try vim_getenv(), fast for normal environment vars */
6606 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006608 {
6609 if (!mustfree)
6610 string = vim_strsave(string);
6611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 else
6613 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006614 if (mustfree)
6615 vim_free(string);
6616
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 /* next try expanding things like $VIM and ${HOME} */
6618 string = expand_env_save(name - 1);
6619 if (string != NULL && *string == '$')
6620 {
6621 vim_free(string);
6622 string = NULL;
6623 }
6624 }
6625 name[len] = cc;
6626 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006627 rettv->v_type = VAR_STRING;
6628 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 }
6630
6631 return OK;
6632}
6633
6634/*
6635 * Array with names and number of arguments of all internal functions
6636 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6637 */
6638static struct fst
6639{
6640 char *f_name; /* function name */
6641 char f_min_argc; /* minimal number of arguments */
6642 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006643 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006644 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645} functions[] =
6646{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006647 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 {"append", 2, 2, f_append},
6649 {"argc", 0, 0, f_argc},
6650 {"argidx", 0, 0, f_argidx},
6651 {"argv", 1, 1, f_argv},
6652 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006653 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 {"bufexists", 1, 1, f_bufexists},
6655 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6656 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6657 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6658 {"buflisted", 1, 1, f_buflisted},
6659 {"bufloaded", 1, 1, f_bufloaded},
6660 {"bufname", 1, 1, f_bufname},
6661 {"bufnr", 1, 1, f_bufnr},
6662 {"bufwinnr", 1, 1, f_bufwinnr},
6663 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006664 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006665 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 {"char2nr", 1, 1, f_char2nr},
6667 {"cindent", 1, 1, f_cindent},
6668 {"col", 1, 1, f_col},
6669 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006671 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 {"cscope_connection",0,3, f_cscope_connection},
6673 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006674 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 {"delete", 1, 1, f_delete},
6676 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006677 {"diff_filler", 1, 1, f_diff_filler},
6678 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006679 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006681 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 {"eventhandler", 0, 0, f_eventhandler},
6683 {"executable", 1, 1, f_executable},
6684 {"exists", 1, 1, f_exists},
6685 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006686 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6688 {"filereadable", 1, 1, f_filereadable},
6689 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006690 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006691 {"finddir", 1, 3, f_finddir},
6692 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 {"fnamemodify", 2, 2, f_fnamemodify},
6694 {"foldclosed", 1, 1, f_foldclosed},
6695 {"foldclosedend", 1, 1, f_foldclosedend},
6696 {"foldlevel", 1, 1, f_foldlevel},
6697 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006698 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006701 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006702 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006703 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {"getbufvar", 2, 2, f_getbufvar},
6705 {"getchar", 0, 1, f_getchar},
6706 {"getcharmod", 0, 0, f_getcharmod},
6707 {"getcmdline", 0, 0, f_getcmdline},
6708 {"getcmdpos", 0, 0, f_getcmdpos},
6709 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006710 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006711 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 {"getfsize", 1, 1, f_getfsize},
6713 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006714 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006715 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006716 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006717 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 {"getregtype", 0, 1, f_getregtype},
6719 {"getwinposx", 0, 0, f_getwinposx},
6720 {"getwinposy", 0, 0, f_getwinposy},
6721 {"getwinvar", 2, 2, f_getwinvar},
6722 {"glob", 1, 1, f_glob},
6723 {"globpath", 2, 2, f_globpath},
6724 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006725 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 {"hasmapto", 1, 2, f_hasmapto},
6727 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6728 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6729 {"histadd", 2, 2, f_histadd},
6730 {"histdel", 1, 2, f_histdel},
6731 {"histget", 1, 2, f_histget},
6732 {"histnr", 1, 1, f_histnr},
6733 {"hlID", 1, 1, f_hlID},
6734 {"hlexists", 1, 1, f_hlexists},
6735 {"hostname", 0, 0, f_hostname},
6736 {"iconv", 3, 3, f_iconv},
6737 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006738 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 {"input", 1, 2, f_input},
6740 {"inputdialog", 1, 3, f_inputdialog},
6741 {"inputrestore", 0, 0, f_inputrestore},
6742 {"inputsave", 0, 0, f_inputsave},
6743 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006744 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006746 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006747 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006748 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006749 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006751 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 {"libcall", 3, 3, f_libcall},
6753 {"libcallnr", 3, 3, f_libcallnr},
6754 {"line", 1, 1, f_line},
6755 {"line2byte", 1, 1, f_line2byte},
6756 {"lispindent", 1, 1, f_lispindent},
6757 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006758 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 {"maparg", 1, 2, f_maparg},
6760 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006761 {"match", 2, 4, f_match},
6762 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006763 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006764 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006765 {"max", 1, 1, f_max},
6766 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006767#ifdef vim_mkdir
6768 {"mkdir", 1, 3, f_mkdir},
6769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 {"mode", 0, 0, f_mode},
6771 {"nextnonblank", 1, 1, f_nextnonblank},
6772 {"nr2char", 1, 1, f_nr2char},
6773 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006774 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006775 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 {"remote_expr", 2, 3, f_remote_expr},
6777 {"remote_foreground", 1, 1, f_remote_foreground},
6778 {"remote_peek", 1, 2, f_remote_peek},
6779 {"remote_read", 1, 1, f_remote_read},
6780 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006781 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006783 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006785 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 {"search", 1, 2, f_search},
6787 {"searchpair", 3, 5, f_searchpair},
6788 {"server2client", 2, 2, f_server2client},
6789 {"serverlist", 0, 0, f_serverlist},
6790 {"setbufvar", 3, 3, f_setbufvar},
6791 {"setcmdpos", 1, 1, f_setcmdpos},
6792 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006793 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 {"setreg", 2, 3, f_setreg},
6795 {"setwinvar", 3, 3, f_setwinvar},
6796 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006797 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006798 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006799 {"spellbadword", 0, 0, f_spellbadword},
6800 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006801 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802#ifdef HAVE_STRFTIME
6803 {"strftime", 1, 2, f_strftime},
6804#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006805 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006806 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {"strlen", 1, 1, f_strlen},
6808 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006809 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 {"strtrans", 1, 1, f_strtrans},
6811 {"submatch", 1, 1, f_submatch},
6812 {"substitute", 4, 4, f_substitute},
6813 {"synID", 3, 3, f_synID},
6814 {"synIDattr", 2, 3, f_synIDattr},
6815 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006816 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006817 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 {"tempname", 0, 0, f_tempname},
6819 {"tolower", 1, 1, f_tolower},
6820 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006821 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006823 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 {"virtcol", 1, 1, f_virtcol},
6825 {"visualmode", 0, 1, f_visualmode},
6826 {"winbufnr", 1, 1, f_winbufnr},
6827 {"wincol", 0, 0, f_wincol},
6828 {"winheight", 1, 1, f_winheight},
6829 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006830 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 {"winrestcmd", 0, 0, f_winrestcmd},
6832 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006833 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834};
6835
6836#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6837
6838/*
6839 * Function given to ExpandGeneric() to obtain the list of internal
6840 * or user defined function names.
6841 */
6842 char_u *
6843get_function_name(xp, idx)
6844 expand_T *xp;
6845 int idx;
6846{
6847 static int intidx = -1;
6848 char_u *name;
6849
6850 if (idx == 0)
6851 intidx = -1;
6852 if (intidx < 0)
6853 {
6854 name = get_user_func_name(xp, idx);
6855 if (name != NULL)
6856 return name;
6857 }
6858 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6859 {
6860 STRCPY(IObuff, functions[intidx].f_name);
6861 STRCAT(IObuff, "(");
6862 if (functions[intidx].f_max_argc == 0)
6863 STRCAT(IObuff, ")");
6864 return IObuff;
6865 }
6866
6867 return NULL;
6868}
6869
6870/*
6871 * Function given to ExpandGeneric() to obtain the list of internal or
6872 * user defined variable or function names.
6873 */
6874/*ARGSUSED*/
6875 char_u *
6876get_expr_name(xp, idx)
6877 expand_T *xp;
6878 int idx;
6879{
6880 static int intidx = -1;
6881 char_u *name;
6882
6883 if (idx == 0)
6884 intidx = -1;
6885 if (intidx < 0)
6886 {
6887 name = get_function_name(xp, idx);
6888 if (name != NULL)
6889 return name;
6890 }
6891 return get_user_var_name(xp, ++intidx);
6892}
6893
6894#endif /* FEAT_CMDL_COMPL */
6895
6896/*
6897 * Find internal function in table above.
6898 * Return index, or -1 if not found
6899 */
6900 static int
6901find_internal_func(name)
6902 char_u *name; /* name of the function */
6903{
6904 int first = 0;
6905 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6906 int cmp;
6907 int x;
6908
6909 /*
6910 * Find the function name in the table. Binary search.
6911 */
6912 while (first <= last)
6913 {
6914 x = first + ((unsigned)(last - first) >> 1);
6915 cmp = STRCMP(name, functions[x].f_name);
6916 if (cmp < 0)
6917 last = x - 1;
6918 else if (cmp > 0)
6919 first = x + 1;
6920 else
6921 return x;
6922 }
6923 return -1;
6924}
6925
6926/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006927 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6928 * name it contains, otherwise return "name".
6929 */
6930 static char_u *
6931deref_func_name(name, lenp)
6932 char_u *name;
6933 int *lenp;
6934{
Bram Moolenaar33570922005-01-25 22:26:29 +00006935 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006936 int cc;
6937
6938 cc = name[*lenp];
6939 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006940 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006941 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006942 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006943 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006944 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006945 {
6946 *lenp = 0;
6947 return (char_u *)""; /* just in case */
6948 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006949 *lenp = STRLEN(v->di_tv.vval.v_string);
6950 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006951 }
6952
6953 return name;
6954}
6955
6956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 * Allocate a variable for the result of a function.
6958 * Return OK or FAIL.
6959 */
6960 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006961get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6962 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 char_u *name; /* name of the function */
6964 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 char_u **arg; /* argument, pointing to the '(' */
6967 linenr_T firstline; /* first line of range */
6968 linenr_T lastline; /* last line of range */
6969 int *doesrange; /* return: function handled range */
6970 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006971 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972{
6973 char_u *argp;
6974 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006975 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 int argcount = 0; /* number of arguments found */
6977
6978 /*
6979 * Get the arguments.
6980 */
6981 argp = *arg;
6982 while (argcount < MAX_FUNC_ARGS)
6983 {
6984 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6985 if (*argp == ')' || *argp == ',' || *argp == NUL)
6986 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6988 {
6989 ret = FAIL;
6990 break;
6991 }
6992 ++argcount;
6993 if (*argp != ',')
6994 break;
6995 }
6996 if (*argp == ')')
6997 ++argp;
6998 else
6999 ret = FAIL;
7000
7001 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007002 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007003 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007005 {
7006 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007007 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007008 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007009 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011
7012 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007013 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014
7015 *arg = skipwhite(argp);
7016 return ret;
7017}
7018
7019
7020/*
7021 * Call a function with its resolved parameters
7022 * Return OK or FAIL.
7023 */
7024 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007025call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007026 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 char_u *name; /* name of the function */
7028 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007029 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007031 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 linenr_T firstline; /* first line of range */
7033 linenr_T lastline; /* last line of range */
7034 int *doesrange; /* return: function handled range */
7035 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007036 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037{
7038 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039#define ERROR_UNKNOWN 0
7040#define ERROR_TOOMANY 1
7041#define ERROR_TOOFEW 2
7042#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007043#define ERROR_DICT 4
7044#define ERROR_NONE 5
7045#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 int error = ERROR_NONE;
7047 int i;
7048 int llen;
7049 ufunc_T *fp;
7050 int cc;
7051#define FLEN_FIXED 40
7052 char_u fname_buf[FLEN_FIXED + 1];
7053 char_u *fname;
7054
7055 /*
7056 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7057 * Change <SNR>123_name() to K_SNR 123_name().
7058 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7059 */
7060 cc = name[len];
7061 name[len] = NUL;
7062 llen = eval_fname_script(name);
7063 if (llen > 0)
7064 {
7065 fname_buf[0] = K_SPECIAL;
7066 fname_buf[1] = KS_EXTRA;
7067 fname_buf[2] = (int)KE_SNR;
7068 i = 3;
7069 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7070 {
7071 if (current_SID <= 0)
7072 error = ERROR_SCRIPT;
7073 else
7074 {
7075 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7076 i = (int)STRLEN(fname_buf);
7077 }
7078 }
7079 if (i + STRLEN(name + llen) < FLEN_FIXED)
7080 {
7081 STRCPY(fname_buf + i, name + llen);
7082 fname = fname_buf;
7083 }
7084 else
7085 {
7086 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7087 if (fname == NULL)
7088 error = ERROR_OTHER;
7089 else
7090 {
7091 mch_memmove(fname, fname_buf, (size_t)i);
7092 STRCPY(fname + i, name + llen);
7093 }
7094 }
7095 }
7096 else
7097 fname = name;
7098
7099 *doesrange = FALSE;
7100
7101
7102 /* execute the function if no errors detected and executing */
7103 if (evaluate && error == ERROR_NONE)
7104 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007105 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 error = ERROR_UNKNOWN;
7107
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007108 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {
7110 /*
7111 * User defined function.
7112 */
7113 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007114
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007116 /* Trigger FuncUndefined event, may load the function. */
7117 if (fp == NULL
7118 && apply_autocmds(EVENT_FUNCUNDEFINED,
7119 fname, fname, TRUE, NULL)
7120 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007122 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 fp = find_func(fname);
7124 }
7125#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007126 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007127 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007128 {
7129 /* loaded a package, search for the function again */
7130 fp = find_func(fname);
7131 }
7132
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 if (fp != NULL)
7134 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007135 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007137 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007139 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007141 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007142 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 else
7144 {
7145 /*
7146 * Call the user function.
7147 * Save and restore search patterns, script variables and
7148 * redo buffer.
7149 */
7150 save_search_patterns();
7151 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007152 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007153 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007154 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007155 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7156 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7157 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007158 /* Function was unreferenced while being used, free it
7159 * now. */
7160 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 restoreRedobuff();
7162 restore_search_patterns();
7163 error = ERROR_NONE;
7164 }
7165 }
7166 }
7167 else
7168 {
7169 /*
7170 * Find the function name in the table, call its implementation.
7171 */
7172 i = find_internal_func(fname);
7173 if (i >= 0)
7174 {
7175 if (argcount < functions[i].f_min_argc)
7176 error = ERROR_TOOFEW;
7177 else if (argcount > functions[i].f_max_argc)
7178 error = ERROR_TOOMANY;
7179 else
7180 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007181 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007182 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 error = ERROR_NONE;
7184 }
7185 }
7186 }
7187 /*
7188 * The function call (or "FuncUndefined" autocommand sequence) might
7189 * have been aborted by an error, an interrupt, or an explicitly thrown
7190 * exception that has not been caught so far. This situation can be
7191 * tested for by calling aborting(). For an error in an internal
7192 * function or for the "E132" error in call_user_func(), however, the
7193 * throw point at which the "force_abort" flag (temporarily reset by
7194 * emsg()) is normally updated has not been reached yet. We need to
7195 * update that flag first to make aborting() reliable.
7196 */
7197 update_force_abort();
7198 }
7199 if (error == ERROR_NONE)
7200 ret = OK;
7201
7202 /*
7203 * Report an error unless the argument evaluation or function call has been
7204 * cancelled due to an aborting error, an interrupt, or an exception.
7205 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206 if (!aborting())
7207 {
7208 switch (error)
7209 {
7210 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007211 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212 break;
7213 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007214 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215 break;
7216 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007217 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218 name);
7219 break;
7220 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007221 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222 name);
7223 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007224 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007225 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007226 name);
7227 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228 }
7229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230
7231 name[len] = cc;
7232 if (fname != name && fname != fname_buf)
7233 vim_free(fname);
7234
7235 return ret;
7236}
7237
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007238/*
7239 * Give an error message with a function name. Handle <SNR> things.
7240 */
7241 static void
7242emsg_funcname(msg, name)
7243 char *msg;
7244 char_u *name;
7245{
7246 char_u *p;
7247
7248 if (*name == K_SPECIAL)
7249 p = concat_str((char_u *)"<SNR>", name + 3);
7250 else
7251 p = name;
7252 EMSG2(_(msg), p);
7253 if (p != name)
7254 vim_free(p);
7255}
7256
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257/*********************************************
7258 * Implementation of the built-in functions
7259 */
7260
7261/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007262 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 */
7264 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007265f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007266 typval_T *argvars;
7267 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268{
Bram Moolenaar33570922005-01-25 22:26:29 +00007269 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007271 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007272 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007274 if ((l = argvars[0].vval.v_list) != NULL
7275 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7276 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007277 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007278 }
7279 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007280 EMSG(_(e_listreq));
7281}
7282
7283/*
7284 * "append(lnum, string/list)" function
7285 */
7286 static void
7287f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007288 typval_T *argvars;
7289 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007290{
7291 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007292 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007293 list_T *l = NULL;
7294 listitem_T *li = NULL;
7295 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007296 long added = 0;
7297
Bram Moolenaar0d660222005-01-07 21:51:51 +00007298 lnum = get_tv_lnum(argvars);
7299 if (lnum >= 0
7300 && lnum <= curbuf->b_ml.ml_line_count
7301 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007302 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007303 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007304 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007305 l = argvars[1].vval.v_list;
7306 if (l == NULL)
7307 return;
7308 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007309 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007310 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007311 for (;;)
7312 {
7313 if (l == NULL)
7314 tv = &argvars[1]; /* append a string */
7315 else if (li == NULL)
7316 break; /* end of list */
7317 else
7318 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007319 line = get_tv_string_chk(tv);
7320 if (line == NULL) /* type error */
7321 {
7322 rettv->vval.v_number = 1; /* Failed */
7323 break;
7324 }
7325 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007326 ++added;
7327 if (l == NULL)
7328 break;
7329 li = li->li_next;
7330 }
7331
7332 appended_lines_mark(lnum, added);
7333 if (curwin->w_cursor.lnum > lnum)
7334 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007336 else
7337 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338}
7339
7340/*
7341 * "argc()" function
7342 */
7343/* ARGSUSED */
7344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007345f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 typval_T *argvars;
7347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007349 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350}
7351
7352/*
7353 * "argidx()" function
7354 */
7355/* ARGSUSED */
7356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007357f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007358 typval_T *argvars;
7359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007361 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362}
7363
7364/*
7365 * "argv(nr)" function
7366 */
7367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007368f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007369 typval_T *argvars;
7370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371{
7372 int idx;
7373
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007374 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007376 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007378 rettv->vval.v_string = NULL;
7379 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380}
7381
7382/*
7383 * "browse(save, title, initdir, default)" function
7384 */
7385/* ARGSUSED */
7386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007387f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007388 typval_T *argvars;
7389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
7391#ifdef FEAT_BROWSE
7392 int save;
7393 char_u *title;
7394 char_u *initdir;
7395 char_u *defname;
7396 char_u buf[NUMBUFLEN];
7397 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007398 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007400 save = get_tv_number_chk(&argvars[0], &error);
7401 title = get_tv_string_chk(&argvars[1]);
7402 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7403 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007405 if (error || title == NULL || initdir == NULL || defname == NULL)
7406 rettv->vval.v_string = NULL;
7407 else
7408 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007409 do_browse(save ? BROWSE_SAVE : 0,
7410 title, defname, NULL, initdir, NULL, curbuf);
7411#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007412 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007413#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007414 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007415}
7416
7417/*
7418 * "browsedir(title, initdir)" function
7419 */
7420/* ARGSUSED */
7421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007422f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007423 typval_T *argvars;
7424 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007425{
7426#ifdef FEAT_BROWSE
7427 char_u *title;
7428 char_u *initdir;
7429 char_u buf[NUMBUFLEN];
7430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007431 title = get_tv_string_chk(&argvars[0]);
7432 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007433
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007434 if (title == NULL || initdir == NULL)
7435 rettv->vval.v_string = NULL;
7436 else
7437 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007438 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007440 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007442 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443}
7444
Bram Moolenaar33570922005-01-25 22:26:29 +00007445static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007446
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447/*
7448 * Find a buffer by number or exact name.
7449 */
7450 static buf_T *
7451find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007452 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453{
7454 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007456 if (avar->v_type == VAR_NUMBER)
7457 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007458 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007460 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007461 if (buf == NULL)
7462 {
7463 /* No full path name match, try a match with a URL or a "nofile"
7464 * buffer, these don't use the full path. */
7465 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7466 if (buf->b_fname != NULL
7467 && (path_with_url(buf->b_fname)
7468#ifdef FEAT_QUICKFIX
7469 || bt_nofile(buf)
7470#endif
7471 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007472 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007473 break;
7474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 }
7476 return buf;
7477}
7478
7479/*
7480 * "bufexists(expr)" function
7481 */
7482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007483f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007484 typval_T *argvars;
7485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007487 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488}
7489
7490/*
7491 * "buflisted(expr)" function
7492 */
7493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007494f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007495 typval_T *argvars;
7496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497{
7498 buf_T *buf;
7499
7500 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007501 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502}
7503
7504/*
7505 * "bufloaded(expr)" function
7506 */
7507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007508f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007509 typval_T *argvars;
7510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511{
7512 buf_T *buf;
7513
7514 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007515 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516}
7517
Bram Moolenaar33570922005-01-25 22:26:29 +00007518static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007519
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520/*
7521 * Get buffer by number or pattern.
7522 */
7523 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007524get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007525 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007527 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 int save_magic;
7529 char_u *save_cpo;
7530 buf_T *buf;
7531
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007532 if (tv->v_type == VAR_NUMBER)
7533 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007534 if (tv->v_type != VAR_STRING)
7535 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 if (name == NULL || *name == NUL)
7537 return curbuf;
7538 if (name[0] == '$' && name[1] == NUL)
7539 return lastbuf;
7540
7541 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7542 save_magic = p_magic;
7543 p_magic = TRUE;
7544 save_cpo = p_cpo;
7545 p_cpo = (char_u *)"";
7546
7547 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7548 TRUE, FALSE));
7549
7550 p_magic = save_magic;
7551 p_cpo = save_cpo;
7552
7553 /* If not found, try expanding the name, like done for bufexists(). */
7554 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007555 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556
7557 return buf;
7558}
7559
7560/*
7561 * "bufname(expr)" function
7562 */
7563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007564f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007565 typval_T *argvars;
7566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
7568 buf_T *buf;
7569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007570 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007572 buf = get_buf_tv(&argvars[0]);
7573 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007575 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007577 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 --emsg_off;
7579}
7580
7581/*
7582 * "bufnr(expr)" function
7583 */
7584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007585f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007586 typval_T *argvars;
7587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588{
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 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007595 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007597 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 --emsg_off;
7599}
7600
7601/*
7602 * "bufwinnr(nr)" function
7603 */
7604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007605f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 typval_T *argvars;
7607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608{
7609#ifdef FEAT_WINDOWS
7610 win_T *wp;
7611 int winnr = 0;
7612#endif
7613 buf_T *buf;
7614
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007615 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007617 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618#ifdef FEAT_WINDOWS
7619 for (wp = firstwin; wp; wp = wp->w_next)
7620 {
7621 ++winnr;
7622 if (wp->w_buffer == buf)
7623 break;
7624 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007625 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007627 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628#endif
7629 --emsg_off;
7630}
7631
7632/*
7633 * "byte2line(byte)" function
7634 */
7635/*ARGSUSED*/
7636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007637f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007638 typval_T *argvars;
7639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640{
7641#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007642 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643#else
7644 long boff = 0;
7645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007646 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007648 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007650 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 (linenr_T)0, &boff);
7652#endif
7653}
7654
7655/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007656 * "byteidx()" function
7657 */
7658/*ARGSUSED*/
7659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007660f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 typval_T *argvars;
7662 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007663{
7664#ifdef FEAT_MBYTE
7665 char_u *t;
7666#endif
7667 char_u *str;
7668 long idx;
7669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007670 str = get_tv_string_chk(&argvars[0]);
7671 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007672 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007673 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007674 return;
7675
7676#ifdef FEAT_MBYTE
7677 t = str;
7678 for ( ; idx > 0; idx--)
7679 {
7680 if (*t == NUL) /* EOL reached */
7681 return;
7682 t += mb_ptr2len_check(t);
7683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007684 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007685#else
7686 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007688#endif
7689}
7690
7691/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007692 * "call(func, arglist)" function
7693 */
7694 static void
7695f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007696 typval_T *argvars;
7697 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007698{
7699 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007701 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007702 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007703 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007704 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007705
7706 rettv->vval.v_number = 0;
7707 if (argvars[1].v_type != VAR_LIST)
7708 {
7709 EMSG(_(e_listreq));
7710 return;
7711 }
7712 if (argvars[1].vval.v_list == NULL)
7713 return;
7714
7715 if (argvars[0].v_type == VAR_FUNC)
7716 func = argvars[0].vval.v_string;
7717 else
7718 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007719 if (*func == NUL)
7720 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007721
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722 if (argvars[2].v_type != VAR_UNKNOWN)
7723 {
7724 if (argvars[2].v_type != VAR_DICT)
7725 {
7726 EMSG(_(e_dictreq));
7727 return;
7728 }
7729 selfdict = argvars[2].vval.v_dict;
7730 }
7731
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007732 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7733 item = item->li_next)
7734 {
7735 if (argc == MAX_FUNC_ARGS)
7736 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007737 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007738 break;
7739 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007740 /* Make a copy of each argument. This is needed to be able to set
7741 * v_lock to VAR_FIXED in the copy without changing the original list.
7742 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007743 copy_tv(&item->li_tv, &argv[argc++]);
7744 }
7745
7746 if (item == NULL)
7747 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007748 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7749 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007750
7751 /* Free the arguments. */
7752 while (argc > 0)
7753 clear_tv(&argv[--argc]);
7754}
7755
7756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 * "char2nr(string)" function
7758 */
7759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007760f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007761 typval_T *argvars;
7762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763{
7764#ifdef FEAT_MBYTE
7765 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007766 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 else
7768#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007769 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770}
7771
7772/*
7773 * "cindent(lnum)" function
7774 */
7775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007777 typval_T *argvars;
7778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
7780#ifdef FEAT_CINDENT
7781 pos_T pos;
7782 linenr_T lnum;
7783
7784 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007785 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7787 {
7788 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007789 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 curwin->w_cursor = pos;
7791 }
7792 else
7793#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795}
7796
7797/*
7798 * "col(string)" function
7799 */
7800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007801f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007802 typval_T *argvars;
7803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804{
7805 colnr_T col = 0;
7806 pos_T *fp;
7807
7808 fp = var2fpos(&argvars[0], FALSE);
7809 if (fp != NULL)
7810 {
7811 if (fp->col == MAXCOL)
7812 {
7813 /* '> can be MAXCOL, get the length of the line then */
7814 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7815 col = STRLEN(ml_get(fp->lnum)) + 1;
7816 else
7817 col = MAXCOL;
7818 }
7819 else
7820 {
7821 col = fp->col + 1;
7822#ifdef FEAT_VIRTUALEDIT
7823 /* col(".") when the cursor is on the NUL at the end of the line
7824 * because of "coladd" can be seen as an extra column. */
7825 if (virtual_active() && fp == &curwin->w_cursor)
7826 {
7827 char_u *p = ml_get_cursor();
7828
7829 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7830 curwin->w_virtcol - curwin->w_cursor.coladd))
7831 {
7832# ifdef FEAT_MBYTE
7833 int l;
7834
7835 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7836 col += l;
7837# else
7838 if (*p != NUL && p[1] == NUL)
7839 ++col;
7840# endif
7841 }
7842 }
7843#endif
7844 }
7845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007846 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847}
7848
7849/*
7850 * "confirm(message, buttons[, default [, type]])" function
7851 */
7852/*ARGSUSED*/
7853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007854f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007855 typval_T *argvars;
7856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857{
7858#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7859 char_u *message;
7860 char_u *buttons = NULL;
7861 char_u buf[NUMBUFLEN];
7862 char_u buf2[NUMBUFLEN];
7863 int def = 1;
7864 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007865 char_u *typestr;
7866 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007868 message = get_tv_string_chk(&argvars[0]);
7869 if (message == NULL)
7870 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007871 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007873 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7874 if (buttons == NULL)
7875 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007876 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007878 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007879 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007881 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7882 if (typestr == NULL)
7883 error = TRUE;
7884 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007886 switch (TOUPPER_ASC(*typestr))
7887 {
7888 case 'E': type = VIM_ERROR; break;
7889 case 'Q': type = VIM_QUESTION; break;
7890 case 'I': type = VIM_INFO; break;
7891 case 'W': type = VIM_WARNING; break;
7892 case 'G': type = VIM_GENERIC; break;
7893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 }
7895 }
7896 }
7897 }
7898
7899 if (buttons == NULL || *buttons == NUL)
7900 buttons = (char_u *)_("&Ok");
7901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007902 if (error)
7903 rettv->vval.v_number = 0;
7904 else
7905 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 def, NULL);
7907#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007908 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909#endif
7910}
7911
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007912/*
7913 * "copy()" function
7914 */
7915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007916f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007917 typval_T *argvars;
7918 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007919{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007920 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007921}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922
7923/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007924 * "count()" function
7925 */
7926 static void
7927f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007928 typval_T *argvars;
7929 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007930{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007931 long n = 0;
7932 int ic = FALSE;
7933
Bram Moolenaare9a41262005-01-15 22:18:47 +00007934 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007935 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007936 listitem_T *li;
7937 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007938 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007939
Bram Moolenaare9a41262005-01-15 22:18:47 +00007940 if ((l = argvars[0].vval.v_list) != NULL)
7941 {
7942 li = l->lv_first;
7943 if (argvars[2].v_type != VAR_UNKNOWN)
7944 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007945 int error = FALSE;
7946
7947 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007948 if (argvars[3].v_type != VAR_UNKNOWN)
7949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007950 idx = get_tv_number_chk(&argvars[3], &error);
7951 if (!error)
7952 {
7953 li = list_find(l, idx);
7954 if (li == NULL)
7955 EMSGN(_(e_listidx), idx);
7956 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007957 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007958 if (error)
7959 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007960 }
7961
7962 for ( ; li != NULL; li = li->li_next)
7963 if (tv_equal(&li->li_tv, &argvars[1], ic))
7964 ++n;
7965 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007966 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007967 else if (argvars[0].v_type == VAR_DICT)
7968 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007969 int todo;
7970 dict_T *d;
7971 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007972
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007973 if ((d = argvars[0].vval.v_dict) != NULL)
7974 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007975 int error = FALSE;
7976
Bram Moolenaare9a41262005-01-15 22:18:47 +00007977 if (argvars[2].v_type != VAR_UNKNOWN)
7978 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007979 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007980 if (argvars[3].v_type != VAR_UNKNOWN)
7981 EMSG(_(e_invarg));
7982 }
7983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007984 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007985 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007986 {
7987 if (!HASHITEM_EMPTY(hi))
7988 {
7989 --todo;
7990 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7991 ++n;
7992 }
7993 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007994 }
7995 }
7996 else
7997 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007998 rettv->vval.v_number = n;
7999}
8000
8001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8003 *
8004 * Checks the existence of a cscope connection.
8005 */
8006/*ARGSUSED*/
8007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008008f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008009 typval_T *argvars;
8010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011{
8012#ifdef FEAT_CSCOPE
8013 int num = 0;
8014 char_u *dbpath = NULL;
8015 char_u *prepend = NULL;
8016 char_u buf[NUMBUFLEN];
8017
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008018 if (argvars[0].v_type != VAR_UNKNOWN
8019 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008021 num = (int)get_tv_number(&argvars[0]);
8022 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008023 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008024 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 }
8026
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008027 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008029 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030#endif
8031}
8032
8033/*
8034 * "cursor(lnum, col)" function
8035 *
8036 * Moves the cursor to the specified line and column
8037 */
8038/*ARGSUSED*/
8039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008040f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008041 typval_T *argvars;
8042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043{
8044 long line, col;
8045
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008046 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008047 col = get_tv_number_chk(&argvars[1], NULL);
8048 if (line < 0 || col < 0)
8049 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 if (line > 0)
8051 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 if (col > 0)
8053 curwin->w_cursor.col = col - 1;
8054#ifdef FEAT_VIRTUALEDIT
8055 curwin->w_cursor.coladd = 0;
8056#endif
8057
8058 /* Make sure the cursor is in a valid position. */
8059 check_cursor();
8060#ifdef FEAT_MBYTE
8061 /* Correct cursor for multi-byte character. */
8062 if (has_mbyte)
8063 mb_adjust_cursor();
8064#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008065
8066 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067}
8068
8069/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008070 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 */
8072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008073f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008074 typval_T *argvars;
8075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008077 int noref = 0;
8078
8079 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008080 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008081 if (noref < 0 || noref > 1)
8082 EMSG(_(e_invarg));
8083 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008084 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085}
8086
8087/*
8088 * "delete()" function
8089 */
8090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008091f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008092 typval_T *argvars;
8093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094{
8095 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008096 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008098 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099}
8100
8101/*
8102 * "did_filetype()" function
8103 */
8104/*ARGSUSED*/
8105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008106f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008107 typval_T *argvars;
8108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109{
8110#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008111 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008113 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114#endif
8115}
8116
8117/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008118 * "diff_filler()" function
8119 */
8120/*ARGSUSED*/
8121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008122f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008123 typval_T *argvars;
8124 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008125{
8126#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008127 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008128#endif
8129}
8130
8131/*
8132 * "diff_hlID()" function
8133 */
8134/*ARGSUSED*/
8135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008136f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008137 typval_T *argvars;
8138 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008139{
8140#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008141 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008142 static linenr_T prev_lnum = 0;
8143 static int changedtick = 0;
8144 static int fnum = 0;
8145 static int change_start = 0;
8146 static int change_end = 0;
8147 static enum hlf_value hlID = 0;
8148 int filler_lines;
8149 int col;
8150
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008151 if (lnum < 0) /* ignore type error in {lnum} arg */
8152 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008153 if (lnum != prev_lnum
8154 || changedtick != curbuf->b_changedtick
8155 || fnum != curbuf->b_fnum)
8156 {
8157 /* New line, buffer, change: need to get the values. */
8158 filler_lines = diff_check(curwin, lnum);
8159 if (filler_lines < 0)
8160 {
8161 if (filler_lines == -1)
8162 {
8163 change_start = MAXCOL;
8164 change_end = -1;
8165 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8166 hlID = HLF_ADD; /* added line */
8167 else
8168 hlID = HLF_CHD; /* changed line */
8169 }
8170 else
8171 hlID = HLF_ADD; /* added line */
8172 }
8173 else
8174 hlID = (enum hlf_value)0;
8175 prev_lnum = lnum;
8176 changedtick = curbuf->b_changedtick;
8177 fnum = curbuf->b_fnum;
8178 }
8179
8180 if (hlID == HLF_CHD || hlID == HLF_TXD)
8181 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008182 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008183 if (col >= change_start && col <= change_end)
8184 hlID = HLF_TXD; /* changed text */
8185 else
8186 hlID = HLF_CHD; /* changed line */
8187 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008188 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008189#endif
8190}
8191
8192/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008193 * "empty({expr})" function
8194 */
8195 static void
8196f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008197 typval_T *argvars;
8198 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008199{
8200 int n;
8201
8202 switch (argvars[0].v_type)
8203 {
8204 case VAR_STRING:
8205 case VAR_FUNC:
8206 n = argvars[0].vval.v_string == NULL
8207 || *argvars[0].vval.v_string == NUL;
8208 break;
8209 case VAR_NUMBER:
8210 n = argvars[0].vval.v_number == 0;
8211 break;
8212 case VAR_LIST:
8213 n = argvars[0].vval.v_list == NULL
8214 || argvars[0].vval.v_list->lv_first == NULL;
8215 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008216 case VAR_DICT:
8217 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008218 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008219 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008220 default:
8221 EMSG2(_(e_intern2), "f_empty()");
8222 n = 0;
8223 }
8224
8225 rettv->vval.v_number = n;
8226}
8227
8228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 * "escape({string}, {chars})" function
8230 */
8231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008232f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008233 typval_T *argvars;
8234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235{
8236 char_u buf[NUMBUFLEN];
8237
Bram Moolenaar758711c2005-02-02 23:11:38 +00008238 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8239 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008240 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241}
8242
8243/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008244 * "eval()" function
8245 */
8246/*ARGSUSED*/
8247 static void
8248f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008249 typval_T *argvars;
8250 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008251{
8252 char_u *s;
8253
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008254 s = get_tv_string_chk(&argvars[0]);
8255 if (s != NULL)
8256 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008257
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008258 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8259 {
8260 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008261 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008262 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008263 else if (*s != NUL)
8264 EMSG(_(e_trailing));
8265}
8266
8267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 * "eventhandler()" function
8269 */
8270/*ARGSUSED*/
8271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008272f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008273 typval_T *argvars;
8274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008276 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277}
8278
8279/*
8280 * "executable()" function
8281 */
8282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008283f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008284 typval_T *argvars;
8285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008287 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288}
8289
8290/*
8291 * "exists()" function
8292 */
8293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008294f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008295 typval_T *argvars;
8296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297{
8298 char_u *p;
8299 char_u *name;
8300 int n = FALSE;
8301 int len = 0;
8302
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008303 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 if (*p == '$') /* environment variable */
8305 {
8306 /* first try "normal" environment variables (fast) */
8307 if (mch_getenv(p + 1) != NULL)
8308 n = TRUE;
8309 else
8310 {
8311 /* try expanding things like $VIM and ${HOME} */
8312 p = expand_env_save(p);
8313 if (p != NULL && *p != '$')
8314 n = TRUE;
8315 vim_free(p);
8316 }
8317 }
8318 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008319 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 else if (*p == '*') /* internal or user defined function */
8321 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008322 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 }
8324 else if (*p == ':')
8325 {
8326 n = cmd_exists(p + 1);
8327 }
8328 else if (*p == '#')
8329 {
8330#ifdef FEAT_AUTOCMD
8331 name = p + 1;
8332 p = vim_strchr(name, '#');
8333 if (p != NULL)
8334 n = au_exists(name, p, p + 1);
8335 else
8336 n = au_exists(name, name + STRLEN(name), NULL);
8337#endif
8338 }
8339 else /* internal variable */
8340 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008341 char_u *tofree;
8342 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008344 /* get_name_len() takes care of expanding curly braces */
8345 name = p;
8346 len = get_name_len(&p, &tofree, TRUE, FALSE);
8347 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008349 if (tofree != NULL)
8350 name = tofree;
8351 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8352 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008354 /* handle d.key, l[idx], f(expr) */
8355 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8356 if (n)
8357 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 }
8359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008361 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 }
8363
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008364 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365}
8366
8367/*
8368 * "expand()" function
8369 */
8370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008371f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008372 typval_T *argvars;
8373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374{
8375 char_u *s;
8376 int len;
8377 char_u *errormsg;
8378 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8379 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008380 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008382 rettv->v_type = VAR_STRING;
8383 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 if (*s == '%' || *s == '#' || *s == '<')
8385 {
8386 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008387 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 --emsg_off;
8389 }
8390 else
8391 {
8392 /* When the optional second argument is non-zero, don't remove matches
8393 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008394 if (argvars[1].v_type != VAR_UNKNOWN
8395 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008397 if (!error)
8398 {
8399 ExpandInit(&xpc);
8400 xpc.xp_context = EXPAND_FILES;
8401 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8402 ExpandCleanup(&xpc);
8403 }
8404 else
8405 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 }
8407}
8408
8409/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008410 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008411 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008412 */
8413 static void
8414f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008415 typval_T *argvars;
8416 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008417{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008418 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008419 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008420 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008421 list_T *l1, *l2;
8422 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008423 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008424 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008425
Bram Moolenaare9a41262005-01-15 22:18:47 +00008426 l1 = argvars[0].vval.v_list;
8427 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008428 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8429 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008430 {
8431 if (argvars[2].v_type != VAR_UNKNOWN)
8432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008433 before = get_tv_number_chk(&argvars[2], &error);
8434 if (error)
8435 return; /* type error; errmsg already given */
8436
Bram Moolenaar758711c2005-02-02 23:11:38 +00008437 if (before == l1->lv_len)
8438 item = NULL;
8439 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008440 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008441 item = list_find(l1, before);
8442 if (item == NULL)
8443 {
8444 EMSGN(_(e_listidx), before);
8445 return;
8446 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008447 }
8448 }
8449 else
8450 item = NULL;
8451 list_extend(l1, l2, item);
8452
Bram Moolenaare9a41262005-01-15 22:18:47 +00008453 copy_tv(&argvars[0], rettv);
8454 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008455 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008456 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8457 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008458 dict_T *d1, *d2;
8459 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008460 char_u *action;
8461 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008462 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008463 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008464
8465 d1 = argvars[0].vval.v_dict;
8466 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008467 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8468 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008469 {
8470 /* Check the third argument. */
8471 if (argvars[2].v_type != VAR_UNKNOWN)
8472 {
8473 static char *(av[]) = {"keep", "force", "error"};
8474
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008475 action = get_tv_string_chk(&argvars[2]);
8476 if (action == NULL)
8477 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008478 for (i = 0; i < 3; ++i)
8479 if (STRCMP(action, av[i]) == 0)
8480 break;
8481 if (i == 3)
8482 {
8483 EMSGN(_(e_invarg2), action);
8484 return;
8485 }
8486 }
8487 else
8488 action = (char_u *)"force";
8489
8490 /* Go over all entries in the second dict and add them to the
8491 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008492 todo = d2->dv_hashtab.ht_used;
8493 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008494 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008495 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008496 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008497 --todo;
8498 di1 = dict_find(d1, hi2->hi_key, -1);
8499 if (di1 == NULL)
8500 {
8501 di1 = dictitem_copy(HI2DI(hi2));
8502 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8503 dictitem_free(di1);
8504 }
8505 else if (*action == 'e')
8506 {
8507 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8508 break;
8509 }
8510 else if (*action == 'f')
8511 {
8512 clear_tv(&di1->di_tv);
8513 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8514 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008515 }
8516 }
8517
Bram Moolenaare9a41262005-01-15 22:18:47 +00008518 copy_tv(&argvars[0], rettv);
8519 }
8520 }
8521 else
8522 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008523}
8524
8525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 * "filereadable()" function
8527 */
8528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008529f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008530 typval_T *argvars;
8531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
8533 FILE *fd;
8534 char_u *p;
8535 int n;
8536
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008537 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8539 {
8540 n = TRUE;
8541 fclose(fd);
8542 }
8543 else
8544 n = FALSE;
8545
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008546 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547}
8548
8549/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008550 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 * rights to write into.
8552 */
8553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008554f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008555 typval_T *argvars;
8556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008558 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559}
8560
Bram Moolenaar33570922005-01-25 22:26:29 +00008561static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008562
8563 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008564findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008565 typval_T *argvars;
8566 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008567 int dir;
8568{
8569#ifdef FEAT_SEARCHPATH
8570 char_u *fname;
8571 char_u *fresult = NULL;
8572 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8573 char_u *p;
8574 char_u pathbuf[NUMBUFLEN];
8575 int count = 1;
8576 int first = TRUE;
8577
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008578 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008579
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008580 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008581 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008582 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8583 if (p == NULL)
8584 count = -1; /* error */
8585 else
8586 {
8587 if (*p != NUL)
8588 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008589
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008590 if (argvars[2].v_type != VAR_UNKNOWN)
8591 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8592 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008593 }
8594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008595 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008597 do
8598 {
8599 vim_free(fresult);
8600 fresult = find_file_in_path_option(first ? fname : NULL,
8601 first ? (int)STRLEN(fname) : 0,
8602 0, first, path, dir, NULL);
8603 first = FALSE;
8604 } while (--count > 0 && fresult != NULL);
8605 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008606
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008607 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008608#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008609 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008610#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008611 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008612}
8613
Bram Moolenaar33570922005-01-25 22:26:29 +00008614static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8615static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008616
8617/*
8618 * Implementation of map() and filter().
8619 */
8620 static void
8621filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008622 typval_T *argvars;
8623 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008624 int map;
8625{
8626 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008627 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008628 listitem_T *li, *nli;
8629 list_T *l = NULL;
8630 dictitem_T *di;
8631 hashtab_T *ht;
8632 hashitem_T *hi;
8633 dict_T *d = NULL;
8634 typval_T save_val;
8635 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008636 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008637 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008638 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8639
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008640
8641 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008642 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008643 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008644 if ((l = argvars[0].vval.v_list) == NULL
8645 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008646 return;
8647 }
8648 else if (argvars[0].v_type == VAR_DICT)
8649 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008650 if ((d = argvars[0].vval.v_dict) == NULL
8651 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008652 return;
8653 }
8654 else
8655 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008656 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008657 return;
8658 }
8659
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008660 expr = get_tv_string_buf_chk(&argvars[1], buf);
8661 /* On type errors, the preceding call has already displayed an error
8662 * message. Avoid a misleading error message for an empty string that
8663 * was not passed as argument. */
8664 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008665 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008666 prepare_vimvar(VV_VAL, &save_val);
8667 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008668
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008669 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008670 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008671 prepare_vimvar(VV_KEY, &save_key);
8672 vimvars[VV_KEY].vv_type = VAR_STRING;
8673
8674 ht = &d->dv_hashtab;
8675 hash_lock(ht);
8676 todo = ht->ht_used;
8677 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008678 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008679 if (!HASHITEM_EMPTY(hi))
8680 {
8681 --todo;
8682 di = HI2DI(hi);
8683 if (tv_check_lock(di->di_tv.v_lock, msg))
8684 break;
8685 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8686 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8687 break;
8688 if (!map && rem)
8689 dictitem_remove(d, di);
8690 clear_tv(&vimvars[VV_KEY].vv_tv);
8691 }
8692 }
8693 hash_unlock(ht);
8694
8695 restore_vimvar(VV_KEY, &save_key);
8696 }
8697 else
8698 {
8699 for (li = l->lv_first; li != NULL; li = nli)
8700 {
8701 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008702 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008703 nli = li->li_next;
8704 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008705 break;
8706 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008707 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008708 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008709 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008711 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008712 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008713
8714 copy_tv(&argvars[0], rettv);
8715}
8716
8717 static int
8718filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008719 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008720 char_u *expr;
8721 int map;
8722 int *remp;
8723{
Bram Moolenaar33570922005-01-25 22:26:29 +00008724 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008725 char_u *s;
8726
Bram Moolenaar33570922005-01-25 22:26:29 +00008727 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008728 s = expr;
8729 if (eval1(&s, &rettv, TRUE) == FAIL)
8730 return FAIL;
8731 if (*s != NUL) /* check for trailing chars after expr */
8732 {
8733 EMSG2(_(e_invexpr2), s);
8734 return FAIL;
8735 }
8736 if (map)
8737 {
8738 /* map(): replace the list item value */
8739 clear_tv(tv);
8740 *tv = rettv;
8741 }
8742 else
8743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008744 int error = FALSE;
8745
Bram Moolenaare9a41262005-01-15 22:18:47 +00008746 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008747 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008748 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008749 /* On type error, nothing has been removed; return FAIL to stop the
8750 * loop. The error message was given by get_tv_number_chk(). */
8751 if (error)
8752 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008753 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008754 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008755 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008756}
8757
8758/*
8759 * "filter()" function
8760 */
8761 static void
8762f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008763 typval_T *argvars;
8764 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008765{
8766 filter_map(argvars, rettv, FALSE);
8767}
8768
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008769/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008770 * "finddir({fname}[, {path}[, {count}]])" function
8771 */
8772 static void
8773f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008774 typval_T *argvars;
8775 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008776{
8777 findfilendir(argvars, rettv, TRUE);
8778}
8779
8780/*
8781 * "findfile({fname}[, {path}[, {count}]])" function
8782 */
8783 static void
8784f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008785 typval_T *argvars;
8786 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008787{
8788 findfilendir(argvars, rettv, FALSE);
8789}
8790
8791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 * "fnamemodify({fname}, {mods})" function
8793 */
8794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008795f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008796 typval_T *argvars;
8797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798{
8799 char_u *fname;
8800 char_u *mods;
8801 int usedlen = 0;
8802 int len;
8803 char_u *fbuf = NULL;
8804 char_u buf[NUMBUFLEN];
8805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008806 fname = get_tv_string_chk(&argvars[0]);
8807 mods = get_tv_string_buf_chk(&argvars[1], buf);
8808 if (fname == NULL || mods == NULL)
8809 fname = NULL;
8810 else
8811 {
8812 len = (int)STRLEN(fname);
8813 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008816 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008818 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008820 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 vim_free(fbuf);
8822}
8823
Bram Moolenaar33570922005-01-25 22:26:29 +00008824static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
8826/*
8827 * "foldclosed()" function
8828 */
8829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008830foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008831 typval_T *argvars;
8832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 int end;
8834{
8835#ifdef FEAT_FOLDING
8836 linenr_T lnum;
8837 linenr_T first, last;
8838
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008839 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8841 {
8842 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8843 {
8844 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008845 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008847 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 return;
8849 }
8850 }
8851#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008852 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853}
8854
8855/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008856 * "foldclosed()" function
8857 */
8858 static void
8859f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008860 typval_T *argvars;
8861 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008862{
8863 foldclosed_both(argvars, rettv, FALSE);
8864}
8865
8866/*
8867 * "foldclosedend()" function
8868 */
8869 static void
8870f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008871 typval_T *argvars;
8872 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008873{
8874 foldclosed_both(argvars, rettv, TRUE);
8875}
8876
8877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 * "foldlevel()" function
8879 */
8880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008881f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008882 typval_T *argvars;
8883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884{
8885#ifdef FEAT_FOLDING
8886 linenr_T lnum;
8887
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008888 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008890 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 else
8892#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008893 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894}
8895
8896/*
8897 * "foldtext()" function
8898 */
8899/*ARGSUSED*/
8900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008901f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008902 typval_T *argvars;
8903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904{
8905#ifdef FEAT_FOLDING
8906 linenr_T lnum;
8907 char_u *s;
8908 char_u *r;
8909 int len;
8910 char *txt;
8911#endif
8912
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 rettv->v_type = VAR_STRING;
8914 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008916 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8917 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8918 <= curbuf->b_ml.ml_line_count
8919 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 {
8921 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008922 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8923 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 {
8925 if (!linewhite(lnum))
8926 break;
8927 ++lnum;
8928 }
8929
8930 /* Find interesting text in this line. */
8931 s = skipwhite(ml_get(lnum));
8932 /* skip C comment-start */
8933 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008934 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008936 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008937 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008938 {
8939 s = skipwhite(ml_get(lnum + 1));
8940 if (*s == '*')
8941 s = skipwhite(s + 1);
8942 }
8943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 txt = _("+-%s%3ld lines: ");
8945 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008946 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 + 20 /* for %3ld */
8948 + STRLEN(s))); /* concatenated */
8949 if (r != NULL)
8950 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008951 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8952 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8953 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 len = (int)STRLEN(r);
8955 STRCAT(r, s);
8956 /* remove 'foldmarker' and 'commentstring' */
8957 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 }
8960 }
8961#endif
8962}
8963
8964/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008965 * "foldtextresult(lnum)" function
8966 */
8967/*ARGSUSED*/
8968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008970 typval_T *argvars;
8971 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008972{
8973#ifdef FEAT_FOLDING
8974 linenr_T lnum;
8975 char_u *text;
8976 char_u buf[51];
8977 foldinfo_T foldinfo;
8978 int fold_count;
8979#endif
8980
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981 rettv->v_type = VAR_STRING;
8982 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008983#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008984 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 /* treat illegal types and illegal string values for {lnum} the same */
8986 if (lnum < 0)
8987 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008988 fold_count = foldedCount(curwin, lnum, &foldinfo);
8989 if (fold_count > 0)
8990 {
8991 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8992 &foldinfo, buf);
8993 if (text == buf)
8994 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008996 }
8997#endif
8998}
8999
9000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 * "foreground()" function
9002 */
9003/*ARGSUSED*/
9004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009005f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009006 typval_T *argvars;
9007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009009 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010#ifdef FEAT_GUI
9011 if (gui.in_use)
9012 gui_mch_set_foreground();
9013#else
9014# ifdef WIN32
9015 win32_set_foreground();
9016# endif
9017#endif
9018}
9019
9020/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009021 * "function()" function
9022 */
9023/*ARGSUSED*/
9024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009025f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009026 typval_T *argvars;
9027 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009028{
9029 char_u *s;
9030
Bram Moolenaara7043832005-01-21 11:56:39 +00009031 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009032 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009033 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009034 EMSG2(_(e_invarg2), s);
9035 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009036 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009037 else
9038 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039 rettv->vval.v_string = vim_strsave(s);
9040 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009041 }
9042}
9043
9044/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009045 * "garbagecollect()" function
9046 */
9047/*ARGSUSED*/
9048 static void
9049f_garbagecollect(argvars, rettv)
9050 typval_T *argvars;
9051 typval_T *rettv;
9052{
9053 garbage_collect();
9054}
9055
9056/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009057 * "get()" function
9058 */
9059 static void
9060f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009061 typval_T *argvars;
9062 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009063{
Bram Moolenaar33570922005-01-25 22:26:29 +00009064 listitem_T *li;
9065 list_T *l;
9066 dictitem_T *di;
9067 dict_T *d;
9068 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009069
Bram Moolenaare9a41262005-01-15 22:18:47 +00009070 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009071 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009072 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009074 int error = FALSE;
9075
9076 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9077 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009078 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009079 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009080 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009081 else if (argvars[0].v_type == VAR_DICT)
9082 {
9083 if ((d = argvars[0].vval.v_dict) != NULL)
9084 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009085 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009086 if (di != NULL)
9087 tv = &di->di_tv;
9088 }
9089 }
9090 else
9091 EMSG2(_(e_listdictarg), "get()");
9092
9093 if (tv == NULL)
9094 {
9095 if (argvars[2].v_type == VAR_UNKNOWN)
9096 rettv->vval.v_number = 0;
9097 else
9098 copy_tv(&argvars[2], rettv);
9099 }
9100 else
9101 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009102}
9103
Bram Moolenaar342337a2005-07-21 21:11:17 +00009104static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009105
9106/*
9107 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009108 * Return a range (from start to end) of lines in rettv from the specified
9109 * buffer.
9110 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009111 */
9112 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009113get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009114 buf_T *buf;
9115 linenr_T start;
9116 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009117 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009118 typval_T *rettv;
9119{
9120 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009121 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009122 listitem_T *li;
9123
Bram Moolenaar342337a2005-07-21 21:11:17 +00009124 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009125 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009126 l = list_alloc();
9127 if (l == NULL)
9128 return;
9129
9130 rettv->vval.v_list = l;
9131 rettv->v_type = VAR_LIST;
9132 ++l->lv_refcount;
9133 }
9134 else
9135 rettv->vval.v_number = 0;
9136
9137 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9138 return;
9139
9140 if (!retlist)
9141 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009142 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9143 p = ml_get_buf(buf, start, FALSE);
9144 else
9145 p = (char_u *)"";
9146
9147 rettv->v_type = VAR_STRING;
9148 rettv->vval.v_string = vim_strsave(p);
9149 }
9150 else
9151 {
9152 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009153 return;
9154
9155 if (start < 1)
9156 start = 1;
9157 if (end > buf->b_ml.ml_line_count)
9158 end = buf->b_ml.ml_line_count;
9159 while (start <= end)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009160 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009161 li = listitem_alloc();
9162 if (li == NULL)
9163 break;
9164 list_append(l, li);
9165 li->li_tv.v_type = VAR_STRING;
9166 li->li_tv.v_lock = 0;
9167 li->li_tv.vval.v_string =
9168 vim_strsave(ml_get_buf(buf, start++, FALSE));
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009169 }
9170 }
9171}
9172
9173/*
9174 * "getbufline()" function
9175 */
9176 static void
9177f_getbufline(argvars, rettv)
9178 typval_T *argvars;
9179 typval_T *rettv;
9180{
9181 linenr_T lnum;
9182 linenr_T end;
9183 buf_T *buf;
9184
9185 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9186 ++emsg_off;
9187 buf = get_buf_tv(&argvars[0]);
9188 --emsg_off;
9189
Bram Moolenaar342337a2005-07-21 21:11:17 +00009190 lnum = get_tv_lnum(&argvars[1]);
9191 if (argvars[2].v_type == VAR_UNKNOWN)
9192 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009193 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009194 end = get_tv_lnum(&argvars[2]);
9195 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009196}
9197
Bram Moolenaar0d660222005-01-07 21:51:51 +00009198/*
9199 * "getbufvar()" function
9200 */
9201 static void
9202f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009203 typval_T *argvars;
9204 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009205{
9206 buf_T *buf;
9207 buf_T *save_curbuf;
9208 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009209 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009211 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9212 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009213 ++emsg_off;
9214 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009215
9216 rettv->v_type = VAR_STRING;
9217 rettv->vval.v_string = NULL;
9218
9219 if (buf != NULL && varname != NULL)
9220 {
9221 if (*varname == '&') /* buffer-local-option */
9222 {
9223 /* set curbuf to be our buf, temporarily */
9224 save_curbuf = curbuf;
9225 curbuf = buf;
9226
9227 get_option_tv(&varname, rettv, TRUE);
9228
9229 /* restore previous notion of curbuf */
9230 curbuf = save_curbuf;
9231 }
9232 else
9233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009234 if (*varname == NUL)
9235 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9236 * scope prefix before the NUL byte is required by
9237 * find_var_in_ht(). */
9238 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009239 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009240 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009241 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009242 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009243 }
9244 }
9245
9246 --emsg_off;
9247}
9248
9249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 * "getchar()" function
9251 */
9252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009253f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009254 typval_T *argvars;
9255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256{
9257 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259
9260 ++no_mapping;
9261 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009262 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 /* getchar(): blocking wait. */
9264 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009265 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 /* getchar(1): only check if char avail */
9267 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009268 else if (error || vpeekc() == NUL)
9269 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 n = 0;
9271 else
9272 /* getchar(0) and char avail: return char */
9273 n = safe_vgetc();
9274 --no_mapping;
9275 --allow_keys;
9276
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009277 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 if (IS_SPECIAL(n) || mod_mask != 0)
9279 {
9280 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9281 int i = 0;
9282
9283 /* Turn a special key into three bytes, plus modifier. */
9284 if (mod_mask != 0)
9285 {
9286 temp[i++] = K_SPECIAL;
9287 temp[i++] = KS_MODIFIER;
9288 temp[i++] = mod_mask;
9289 }
9290 if (IS_SPECIAL(n))
9291 {
9292 temp[i++] = K_SPECIAL;
9293 temp[i++] = K_SECOND(n);
9294 temp[i++] = K_THIRD(n);
9295 }
9296#ifdef FEAT_MBYTE
9297 else if (has_mbyte)
9298 i += (*mb_char2bytes)(n, temp + i);
9299#endif
9300 else
9301 temp[i++] = n;
9302 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303 rettv->v_type = VAR_STRING;
9304 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 }
9306}
9307
9308/*
9309 * "getcharmod()" function
9310 */
9311/*ARGSUSED*/
9312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009313f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009314 typval_T *argvars;
9315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009317 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318}
9319
9320/*
9321 * "getcmdline()" function
9322 */
9323/*ARGSUSED*/
9324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009325f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009326 typval_T *argvars;
9327 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009329 rettv->v_type = VAR_STRING;
9330 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331}
9332
9333/*
9334 * "getcmdpos()" function
9335 */
9336/*ARGSUSED*/
9337 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009338f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009339 typval_T *argvars;
9340 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009342 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343}
9344
9345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 * "getcwd()" function
9347 */
9348/*ARGSUSED*/
9349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009350f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009351 typval_T *argvars;
9352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353{
9354 char_u cwd[MAXPATHL];
9355
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009356 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 else
9360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009361 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009363 if (rettv->vval.v_string != NULL)
9364 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365#endif
9366 }
9367}
9368
9369/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009370 * "getfontname()" function
9371 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009372/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009374f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009375 typval_T *argvars;
9376 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009377{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009378 rettv->v_type = VAR_STRING;
9379 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009380#ifdef FEAT_GUI
9381 if (gui.in_use)
9382 {
9383 GuiFont font;
9384 char_u *name = NULL;
9385
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009386 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009387 {
9388 /* Get the "Normal" font. Either the name saved by
9389 * hl_set_font_name() or from the font ID. */
9390 font = gui.norm_font;
9391 name = hl_get_font_name();
9392 }
9393 else
9394 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009395 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009396 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9397 return;
9398 font = gui_mch_get_font(name, FALSE);
9399 if (font == NOFONT)
9400 return; /* Invalid font name, return empty string. */
9401 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009402 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009403 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009404 gui_mch_free_font(font);
9405 }
9406#endif
9407}
9408
9409/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009410 * "getfperm({fname})" function
9411 */
9412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009414 typval_T *argvars;
9415 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009416{
9417 char_u *fname;
9418 struct stat st;
9419 char_u *perm = NULL;
9420 char_u flags[] = "rwx";
9421 int i;
9422
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009423 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009424
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009425 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009426 if (mch_stat((char *)fname, &st) >= 0)
9427 {
9428 perm = vim_strsave((char_u *)"---------");
9429 if (perm != NULL)
9430 {
9431 for (i = 0; i < 9; i++)
9432 {
9433 if (st.st_mode & (1 << (8 - i)))
9434 perm[i] = flags[i % 3];
9435 }
9436 }
9437 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009439}
9440
9441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 * "getfsize({fname})" function
9443 */
9444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009446 typval_T *argvars;
9447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448{
9449 char_u *fname;
9450 struct stat st;
9451
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009454 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455
9456 if (mch_stat((char *)fname, &st) >= 0)
9457 {
9458 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 }
9463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465}
9466
9467/*
9468 * "getftime({fname})" function
9469 */
9470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009472 typval_T *argvars;
9473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474{
9475 char_u *fname;
9476 struct stat st;
9477
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009478 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479
9480 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009481 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009483 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484}
9485
9486/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009487 * "getftype({fname})" function
9488 */
9489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009491 typval_T *argvars;
9492 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009493{
9494 char_u *fname;
9495 struct stat st;
9496 char_u *type = NULL;
9497 char *t;
9498
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009499 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009500
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009501 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009502 if (mch_lstat((char *)fname, &st) >= 0)
9503 {
9504#ifdef S_ISREG
9505 if (S_ISREG(st.st_mode))
9506 t = "file";
9507 else if (S_ISDIR(st.st_mode))
9508 t = "dir";
9509# ifdef S_ISLNK
9510 else if (S_ISLNK(st.st_mode))
9511 t = "link";
9512# endif
9513# ifdef S_ISBLK
9514 else if (S_ISBLK(st.st_mode))
9515 t = "bdev";
9516# endif
9517# ifdef S_ISCHR
9518 else if (S_ISCHR(st.st_mode))
9519 t = "cdev";
9520# endif
9521# ifdef S_ISFIFO
9522 else if (S_ISFIFO(st.st_mode))
9523 t = "fifo";
9524# endif
9525# ifdef S_ISSOCK
9526 else if (S_ISSOCK(st.st_mode))
9527 t = "fifo";
9528# endif
9529 else
9530 t = "other";
9531#else
9532# ifdef S_IFMT
9533 switch (st.st_mode & S_IFMT)
9534 {
9535 case S_IFREG: t = "file"; break;
9536 case S_IFDIR: t = "dir"; break;
9537# ifdef S_IFLNK
9538 case S_IFLNK: t = "link"; break;
9539# endif
9540# ifdef S_IFBLK
9541 case S_IFBLK: t = "bdev"; break;
9542# endif
9543# ifdef S_IFCHR
9544 case S_IFCHR: t = "cdev"; break;
9545# endif
9546# ifdef S_IFIFO
9547 case S_IFIFO: t = "fifo"; break;
9548# endif
9549# ifdef S_IFSOCK
9550 case S_IFSOCK: t = "socket"; break;
9551# endif
9552 default: t = "other";
9553 }
9554# else
9555 if (mch_isdir(fname))
9556 t = "dir";
9557 else
9558 t = "file";
9559# endif
9560#endif
9561 type = vim_strsave((char_u *)t);
9562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009563 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009564}
9565
9566/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009567 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009568 */
9569 static void
9570f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009571 typval_T *argvars;
9572 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009573{
9574 linenr_T lnum;
9575 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009576 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009577
9578 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009579 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009580 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009581 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009582 retlist = FALSE;
9583 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009584 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009585 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009586 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009587 retlist = TRUE;
9588 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009589
Bram Moolenaar342337a2005-07-21 21:11:17 +00009590 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009591}
9592
9593/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009594 * "getqflist()" function
9595 */
9596/*ARGSUSED*/
9597 static void
9598f_getqflist(argvars, rettv)
9599 typval_T *argvars;
9600 typval_T *rettv;
9601{
9602#ifdef FEAT_QUICKFIX
9603 list_T *l;
9604#endif
9605
9606 rettv->vval.v_number = FALSE;
9607#ifdef FEAT_QUICKFIX
9608 l = list_alloc();
9609 if (l != NULL)
9610 {
9611 if (get_errorlist(l) != FAIL)
9612 {
9613 rettv->vval.v_list = l;
9614 rettv->v_type = VAR_LIST;
9615 ++l->lv_refcount;
9616 }
9617 else
9618 list_free(l);
9619 }
9620#endif
9621}
9622
9623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 * "getreg()" function
9625 */
9626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009627f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009628 typval_T *argvars;
9629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630{
9631 char_u *strregname;
9632 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009633 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009634 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009636 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009637 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009638 strregname = get_tv_string_chk(&argvars[0]);
9639 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009640 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009641 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009644 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 regname = (strregname == NULL ? '"' : *strregname);
9646 if (regname == 0)
9647 regname = '"';
9648
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009650 rettv->vval.v_string = error ? NULL :
9651 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652}
9653
9654/*
9655 * "getregtype()" function
9656 */
9657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009659 typval_T *argvars;
9660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661{
9662 char_u *strregname;
9663 int regname;
9664 char_u buf[NUMBUFLEN + 2];
9665 long reglen = 0;
9666
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009667 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009668 {
9669 strregname = get_tv_string_chk(&argvars[0]);
9670 if (strregname == NULL) /* type error; errmsg already given */
9671 {
9672 rettv->v_type = VAR_STRING;
9673 rettv->vval.v_string = NULL;
9674 return;
9675 }
9676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 else
9678 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009679 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680
9681 regname = (strregname == NULL ? '"' : *strregname);
9682 if (regname == 0)
9683 regname = '"';
9684
9685 buf[0] = NUL;
9686 buf[1] = NUL;
9687 switch (get_reg_type(regname, &reglen))
9688 {
9689 case MLINE: buf[0] = 'V'; break;
9690 case MCHAR: buf[0] = 'v'; break;
9691#ifdef FEAT_VISUAL
9692 case MBLOCK:
9693 buf[0] = Ctrl_V;
9694 sprintf((char *)buf + 1, "%ld", reglen + 1);
9695 break;
9696#endif
9697 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009698 rettv->v_type = VAR_STRING;
9699 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700}
9701
9702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 * "getwinposx()" function
9704 */
9705/*ARGSUSED*/
9706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009708 typval_T *argvars;
9709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712#ifdef FEAT_GUI
9713 if (gui.in_use)
9714 {
9715 int x, y;
9716
9717 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 }
9720#endif
9721}
9722
9723/*
9724 * "getwinposy()" function
9725 */
9726/*ARGSUSED*/
9727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009728f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009729 typval_T *argvars;
9730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009732 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733#ifdef FEAT_GUI
9734 if (gui.in_use)
9735 {
9736 int x, y;
9737
9738 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009739 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 }
9741#endif
9742}
9743
Bram Moolenaara40058a2005-07-11 22:42:07 +00009744static win_T *find_win_by_nr __ARGS((typval_T *vp));
9745
9746 static win_T *
9747find_win_by_nr(vp)
9748 typval_T *vp;
9749{
9750#ifdef FEAT_WINDOWS
9751 win_T *wp;
9752#endif
9753 int nr;
9754
9755 nr = get_tv_number_chk(vp, NULL);
9756
9757#ifdef FEAT_WINDOWS
9758 if (nr < 0)
9759 return NULL;
9760 if (nr == 0)
9761 return curwin;
9762
9763 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9764 if (--nr <= 0)
9765 break;
9766 return wp;
9767#else
9768 if (nr == 0 || nr == 1)
9769 return curwin;
9770 return NULL;
9771#endif
9772}
9773
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774/*
9775 * "getwinvar()" function
9776 */
9777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009778f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009779 typval_T *argvars;
9780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781{
9782 win_T *win, *oldcurwin;
9783 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009784 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009787 varname = get_tv_string_chk(&argvars[1]);
9788 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009790 rettv->v_type = VAR_STRING;
9791 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792
9793 if (win != NULL && varname != NULL)
9794 {
9795 if (*varname == '&') /* window-local-option */
9796 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009797 /* Set curwin to be our win, temporarily. Also set curbuf, so
9798 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 oldcurwin = curwin;
9800 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009801 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804
9805 /* restore previous notion of curwin */
9806 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009807 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 }
9809 else
9810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009811 if (*varname == NUL)
9812 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9813 * scope prefix before the NUL byte is required by
9814 * find_var_in_ht(). */
9815 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009817 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009819 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 }
9821 }
9822
9823 --emsg_off;
9824}
9825
9826/*
9827 * "glob()" function
9828 */
9829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009831 typval_T *argvars;
9832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833{
9834 expand_T xpc;
9835
9836 ExpandInit(&xpc);
9837 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009838 rettv->v_type = VAR_STRING;
9839 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9841 ExpandCleanup(&xpc);
9842}
9843
9844/*
9845 * "globpath()" function
9846 */
9847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009848f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009849 typval_T *argvars;
9850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851{
9852 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009853 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009855 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009856 if (file == NULL)
9857 rettv->vval.v_string = NULL;
9858 else
9859 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860}
9861
9862/*
9863 * "has()" function
9864 */
9865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009866f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009867 typval_T *argvars;
9868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869{
9870 int i;
9871 char_u *name;
9872 int n = FALSE;
9873 static char *(has_list[]) =
9874 {
9875#ifdef AMIGA
9876 "amiga",
9877# ifdef FEAT_ARP
9878 "arp",
9879# endif
9880#endif
9881#ifdef __BEOS__
9882 "beos",
9883#endif
9884#ifdef MSDOS
9885# ifdef DJGPP
9886 "dos32",
9887# else
9888 "dos16",
9889# endif
9890#endif
9891#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9892 "mac",
9893#endif
9894#if defined(MACOS_X_UNIX)
9895 "macunix",
9896#endif
9897#ifdef OS2
9898 "os2",
9899#endif
9900#ifdef __QNX__
9901 "qnx",
9902#endif
9903#ifdef RISCOS
9904 "riscos",
9905#endif
9906#ifdef UNIX
9907 "unix",
9908#endif
9909#ifdef VMS
9910 "vms",
9911#endif
9912#ifdef WIN16
9913 "win16",
9914#endif
9915#ifdef WIN32
9916 "win32",
9917#endif
9918#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9919 "win32unix",
9920#endif
9921#ifdef WIN64
9922 "win64",
9923#endif
9924#ifdef EBCDIC
9925 "ebcdic",
9926#endif
9927#ifndef CASE_INSENSITIVE_FILENAME
9928 "fname_case",
9929#endif
9930#ifdef FEAT_ARABIC
9931 "arabic",
9932#endif
9933#ifdef FEAT_AUTOCMD
9934 "autocmd",
9935#endif
9936#ifdef FEAT_BEVAL
9937 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +00009938# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
9939 "balloon_multiline",
9940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941#endif
9942#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9943 "builtin_terms",
9944# ifdef ALL_BUILTIN_TCAPS
9945 "all_builtin_terms",
9946# endif
9947#endif
9948#ifdef FEAT_BYTEOFF
9949 "byte_offset",
9950#endif
9951#ifdef FEAT_CINDENT
9952 "cindent",
9953#endif
9954#ifdef FEAT_CLIENTSERVER
9955 "clientserver",
9956#endif
9957#ifdef FEAT_CLIPBOARD
9958 "clipboard",
9959#endif
9960#ifdef FEAT_CMDL_COMPL
9961 "cmdline_compl",
9962#endif
9963#ifdef FEAT_CMDHIST
9964 "cmdline_hist",
9965#endif
9966#ifdef FEAT_COMMENTS
9967 "comments",
9968#endif
9969#ifdef FEAT_CRYPT
9970 "cryptv",
9971#endif
9972#ifdef FEAT_CSCOPE
9973 "cscope",
9974#endif
9975#ifdef DEBUG
9976 "debug",
9977#endif
9978#ifdef FEAT_CON_DIALOG
9979 "dialog_con",
9980#endif
9981#ifdef FEAT_GUI_DIALOG
9982 "dialog_gui",
9983#endif
9984#ifdef FEAT_DIFF
9985 "diff",
9986#endif
9987#ifdef FEAT_DIGRAPHS
9988 "digraphs",
9989#endif
9990#ifdef FEAT_DND
9991 "dnd",
9992#endif
9993#ifdef FEAT_EMACS_TAGS
9994 "emacs_tags",
9995#endif
9996 "eval", /* always present, of course! */
9997#ifdef FEAT_EX_EXTRA
9998 "ex_extra",
9999#endif
10000#ifdef FEAT_SEARCH_EXTRA
10001 "extra_search",
10002#endif
10003#ifdef FEAT_FKMAP
10004 "farsi",
10005#endif
10006#ifdef FEAT_SEARCHPATH
10007 "file_in_path",
10008#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010009#if defined(UNIX) && !defined(USE_SYSTEM)
10010 "filterpipe",
10011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012#ifdef FEAT_FIND_ID
10013 "find_in_path",
10014#endif
10015#ifdef FEAT_FOLDING
10016 "folding",
10017#endif
10018#ifdef FEAT_FOOTER
10019 "footer",
10020#endif
10021#if !defined(USE_SYSTEM) && defined(UNIX)
10022 "fork",
10023#endif
10024#ifdef FEAT_GETTEXT
10025 "gettext",
10026#endif
10027#ifdef FEAT_GUI
10028 "gui",
10029#endif
10030#ifdef FEAT_GUI_ATHENA
10031# ifdef FEAT_GUI_NEXTAW
10032 "gui_neXtaw",
10033# else
10034 "gui_athena",
10035# endif
10036#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +000010037#ifdef FEAT_GUI_KDE
10038 "gui_kde",
10039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040#ifdef FEAT_GUI_GTK
10041 "gui_gtk",
10042# ifdef HAVE_GTK2
10043 "gui_gtk2",
10044# endif
10045#endif
10046#ifdef FEAT_GUI_MAC
10047 "gui_mac",
10048#endif
10049#ifdef FEAT_GUI_MOTIF
10050 "gui_motif",
10051#endif
10052#ifdef FEAT_GUI_PHOTON
10053 "gui_photon",
10054#endif
10055#ifdef FEAT_GUI_W16
10056 "gui_win16",
10057#endif
10058#ifdef FEAT_GUI_W32
10059 "gui_win32",
10060#endif
10061#ifdef FEAT_HANGULIN
10062 "hangul_input",
10063#endif
10064#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10065 "iconv",
10066#endif
10067#ifdef FEAT_INS_EXPAND
10068 "insert_expand",
10069#endif
10070#ifdef FEAT_JUMPLIST
10071 "jumplist",
10072#endif
10073#ifdef FEAT_KEYMAP
10074 "keymap",
10075#endif
10076#ifdef FEAT_LANGMAP
10077 "langmap",
10078#endif
10079#ifdef FEAT_LIBCALL
10080 "libcall",
10081#endif
10082#ifdef FEAT_LINEBREAK
10083 "linebreak",
10084#endif
10085#ifdef FEAT_LISP
10086 "lispindent",
10087#endif
10088#ifdef FEAT_LISTCMDS
10089 "listcmds",
10090#endif
10091#ifdef FEAT_LOCALMAP
10092 "localmap",
10093#endif
10094#ifdef FEAT_MENU
10095 "menu",
10096#endif
10097#ifdef FEAT_SESSION
10098 "mksession",
10099#endif
10100#ifdef FEAT_MODIFY_FNAME
10101 "modify_fname",
10102#endif
10103#ifdef FEAT_MOUSE
10104 "mouse",
10105#endif
10106#ifdef FEAT_MOUSESHAPE
10107 "mouseshape",
10108#endif
10109#if defined(UNIX) || defined(VMS)
10110# ifdef FEAT_MOUSE_DEC
10111 "mouse_dec",
10112# endif
10113# ifdef FEAT_MOUSE_GPM
10114 "mouse_gpm",
10115# endif
10116# ifdef FEAT_MOUSE_JSB
10117 "mouse_jsbterm",
10118# endif
10119# ifdef FEAT_MOUSE_NET
10120 "mouse_netterm",
10121# endif
10122# ifdef FEAT_MOUSE_PTERM
10123 "mouse_pterm",
10124# endif
10125# ifdef FEAT_MOUSE_XTERM
10126 "mouse_xterm",
10127# endif
10128#endif
10129#ifdef FEAT_MBYTE
10130 "multi_byte",
10131#endif
10132#ifdef FEAT_MBYTE_IME
10133 "multi_byte_ime",
10134#endif
10135#ifdef FEAT_MULTI_LANG
10136 "multi_lang",
10137#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010138#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010139#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010140 "mzscheme",
10141#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143#ifdef FEAT_OLE
10144 "ole",
10145#endif
10146#ifdef FEAT_OSFILETYPE
10147 "osfiletype",
10148#endif
10149#ifdef FEAT_PATH_EXTRA
10150 "path_extra",
10151#endif
10152#ifdef FEAT_PERL
10153#ifndef DYNAMIC_PERL
10154 "perl",
10155#endif
10156#endif
10157#ifdef FEAT_PYTHON
10158#ifndef DYNAMIC_PYTHON
10159 "python",
10160#endif
10161#endif
10162#ifdef FEAT_POSTSCRIPT
10163 "postscript",
10164#endif
10165#ifdef FEAT_PRINTER
10166 "printer",
10167#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010168#ifdef FEAT_PROFILE
10169 "profile",
10170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171#ifdef FEAT_QUICKFIX
10172 "quickfix",
10173#endif
10174#ifdef FEAT_RIGHTLEFT
10175 "rightleft",
10176#endif
10177#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10178 "ruby",
10179#endif
10180#ifdef FEAT_SCROLLBIND
10181 "scrollbind",
10182#endif
10183#ifdef FEAT_CMDL_INFO
10184 "showcmd",
10185 "cmdline_info",
10186#endif
10187#ifdef FEAT_SIGNS
10188 "signs",
10189#endif
10190#ifdef FEAT_SMARTINDENT
10191 "smartindent",
10192#endif
10193#ifdef FEAT_SNIFF
10194 "sniff",
10195#endif
10196#ifdef FEAT_STL_OPT
10197 "statusline",
10198#endif
10199#ifdef FEAT_SUN_WORKSHOP
10200 "sun_workshop",
10201#endif
10202#ifdef FEAT_NETBEANS_INTG
10203 "netbeans_intg",
10204#endif
10205#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010206 "spell",
10207#endif
10208#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 "syntax",
10210#endif
10211#if defined(USE_SYSTEM) || !defined(UNIX)
10212 "system",
10213#endif
10214#ifdef FEAT_TAG_BINS
10215 "tag_binary",
10216#endif
10217#ifdef FEAT_TAG_OLDSTATIC
10218 "tag_old_static",
10219#endif
10220#ifdef FEAT_TAG_ANYWHITE
10221 "tag_any_white",
10222#endif
10223#ifdef FEAT_TCL
10224# ifndef DYNAMIC_TCL
10225 "tcl",
10226# endif
10227#endif
10228#ifdef TERMINFO
10229 "terminfo",
10230#endif
10231#ifdef FEAT_TERMRESPONSE
10232 "termresponse",
10233#endif
10234#ifdef FEAT_TEXTOBJ
10235 "textobjects",
10236#endif
10237#ifdef HAVE_TGETENT
10238 "tgetent",
10239#endif
10240#ifdef FEAT_TITLE
10241 "title",
10242#endif
10243#ifdef FEAT_TOOLBAR
10244 "toolbar",
10245#endif
10246#ifdef FEAT_USR_CMDS
10247 "user-commands", /* was accidentally included in 5.4 */
10248 "user_commands",
10249#endif
10250#ifdef FEAT_VIMINFO
10251 "viminfo",
10252#endif
10253#ifdef FEAT_VERTSPLIT
10254 "vertsplit",
10255#endif
10256#ifdef FEAT_VIRTUALEDIT
10257 "virtualedit",
10258#endif
10259#ifdef FEAT_VISUAL
10260 "visual",
10261#endif
10262#ifdef FEAT_VISUALEXTRA
10263 "visualextra",
10264#endif
10265#ifdef FEAT_VREPLACE
10266 "vreplace",
10267#endif
10268#ifdef FEAT_WILDIGN
10269 "wildignore",
10270#endif
10271#ifdef FEAT_WILDMENU
10272 "wildmenu",
10273#endif
10274#ifdef FEAT_WINDOWS
10275 "windows",
10276#endif
10277#ifdef FEAT_WAK
10278 "winaltkeys",
10279#endif
10280#ifdef FEAT_WRITEBACKUP
10281 "writebackup",
10282#endif
10283#ifdef FEAT_XIM
10284 "xim",
10285#endif
10286#ifdef FEAT_XFONTSET
10287 "xfontset",
10288#endif
10289#ifdef USE_XSMP
10290 "xsmp",
10291#endif
10292#ifdef USE_XSMP_INTERACT
10293 "xsmp_interact",
10294#endif
10295#ifdef FEAT_XCLIPBOARD
10296 "xterm_clipboard",
10297#endif
10298#ifdef FEAT_XTERM_SAVE
10299 "xterm_save",
10300#endif
10301#if defined(UNIX) && defined(FEAT_X11)
10302 "X11",
10303#endif
10304 NULL
10305 };
10306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010307 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 for (i = 0; has_list[i] != NULL; ++i)
10309 if (STRICMP(name, has_list[i]) == 0)
10310 {
10311 n = TRUE;
10312 break;
10313 }
10314
10315 if (n == FALSE)
10316 {
10317 if (STRNICMP(name, "patch", 5) == 0)
10318 n = has_patch(atoi((char *)name + 5));
10319 else if (STRICMP(name, "vim_starting") == 0)
10320 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010321#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10322 else if (STRICMP(name, "balloon_multiline") == 0)
10323 n = multiline_balloon_available();
10324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325#ifdef DYNAMIC_TCL
10326 else if (STRICMP(name, "tcl") == 0)
10327 n = tcl_enabled(FALSE);
10328#endif
10329#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10330 else if (STRICMP(name, "iconv") == 0)
10331 n = iconv_enabled(FALSE);
10332#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010333#ifdef DYNAMIC_MZSCHEME
10334 else if (STRICMP(name, "mzscheme") == 0)
10335 n = mzscheme_enabled(FALSE);
10336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337#ifdef DYNAMIC_RUBY
10338 else if (STRICMP(name, "ruby") == 0)
10339 n = ruby_enabled(FALSE);
10340#endif
10341#ifdef DYNAMIC_PYTHON
10342 else if (STRICMP(name, "python") == 0)
10343 n = python_enabled(FALSE);
10344#endif
10345#ifdef DYNAMIC_PERL
10346 else if (STRICMP(name, "perl") == 0)
10347 n = perl_enabled(FALSE);
10348#endif
10349#ifdef FEAT_GUI
10350 else if (STRICMP(name, "gui_running") == 0)
10351 n = (gui.in_use || gui.starting);
10352# ifdef FEAT_GUI_W32
10353 else if (STRICMP(name, "gui_win32s") == 0)
10354 n = gui_is_win32s();
10355# endif
10356# ifdef FEAT_BROWSE
10357 else if (STRICMP(name, "browse") == 0)
10358 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10359# endif
10360#endif
10361#ifdef FEAT_SYN_HL
10362 else if (STRICMP(name, "syntax_items") == 0)
10363 n = syntax_present(curbuf);
10364#endif
10365#if defined(WIN3264)
10366 else if (STRICMP(name, "win95") == 0)
10367 n = mch_windows95();
10368#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010369#ifdef FEAT_NETBEANS_INTG
10370 else if (STRICMP(name, "netbeans_enabled") == 0)
10371 n = usingNetbeans;
10372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 }
10374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010375 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376}
10377
10378/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010379 * "has_key()" function
10380 */
10381 static void
10382f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010383 typval_T *argvars;
10384 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010385{
10386 rettv->vval.v_number = 0;
10387 if (argvars[0].v_type != VAR_DICT)
10388 {
10389 EMSG(_(e_dictreq));
10390 return;
10391 }
10392 if (argvars[0].vval.v_dict == NULL)
10393 return;
10394
10395 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010396 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010397}
10398
10399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 * "hasmapto()" function
10401 */
10402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010403f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010404 typval_T *argvars;
10405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406{
10407 char_u *name;
10408 char_u *mode;
10409 char_u buf[NUMBUFLEN];
10410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010411 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010412 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 mode = (char_u *)"nvo";
10414 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010415 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416
10417 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010418 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010420 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421}
10422
10423/*
10424 * "histadd()" function
10425 */
10426/*ARGSUSED*/
10427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010428f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010429 typval_T *argvars;
10430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431{
10432#ifdef FEAT_CMDHIST
10433 int histype;
10434 char_u *str;
10435 char_u buf[NUMBUFLEN];
10436#endif
10437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010438 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 if (check_restricted() || check_secure())
10440 return;
10441#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010442 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10443 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 if (histype >= 0)
10445 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010446 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 if (*str != NUL)
10448 {
10449 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010450 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 return;
10452 }
10453 }
10454#endif
10455}
10456
10457/*
10458 * "histdel()" function
10459 */
10460/*ARGSUSED*/
10461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010462f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010463 typval_T *argvars;
10464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465{
10466#ifdef FEAT_CMDHIST
10467 int n;
10468 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010469 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010471 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10472 if (str == NULL)
10473 n = 0;
10474 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010476 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010477 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010479 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010480 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 else
10482 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010483 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010484 get_tv_string_buf(&argvars[1], buf));
10485 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010487 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488#endif
10489}
10490
10491/*
10492 * "histget()" function
10493 */
10494/*ARGSUSED*/
10495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010496f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010497 typval_T *argvars;
10498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499{
10500#ifdef FEAT_CMDHIST
10501 int type;
10502 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010503 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010505 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10506 if (str == NULL)
10507 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010509 {
10510 type = get_histtype(str);
10511 if (argvars[1].v_type == VAR_UNKNOWN)
10512 idx = get_history_idx(type);
10513 else
10514 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10515 /* -1 on type error */
10516 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010519 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010521 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522}
10523
10524/*
10525 * "histnr()" function
10526 */
10527/*ARGSUSED*/
10528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010530 typval_T *argvars;
10531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532{
10533 int i;
10534
10535#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 char_u *history = get_tv_string_chk(&argvars[0]);
10537
10538 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 if (i >= HIST_CMD && i < HIST_COUNT)
10540 i = get_history_idx(i);
10541 else
10542#endif
10543 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010544 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545}
10546
10547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 * "highlightID(name)" function
10549 */
10550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010551f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010552 typval_T *argvars;
10553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556}
10557
10558/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010559 * "highlight_exists()" function
10560 */
10561 static void
10562f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010563 typval_T *argvars;
10564 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010565{
10566 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10567}
10568
10569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 * "hostname()" function
10571 */
10572/*ARGSUSED*/
10573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010574f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010575 typval_T *argvars;
10576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577{
10578 char_u hostname[256];
10579
10580 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010581 rettv->v_type = VAR_STRING;
10582 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583}
10584
10585/*
10586 * iconv() function
10587 */
10588/*ARGSUSED*/
10589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010590f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010591 typval_T *argvars;
10592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593{
10594#ifdef FEAT_MBYTE
10595 char_u buf1[NUMBUFLEN];
10596 char_u buf2[NUMBUFLEN];
10597 char_u *from, *to, *str;
10598 vimconv_T vimconv;
10599#endif
10600
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010601 rettv->v_type = VAR_STRING;
10602 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603
10604#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010605 str = get_tv_string(&argvars[0]);
10606 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10607 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 vimconv.vc_type = CONV_NONE;
10609 convert_setup(&vimconv, from, to);
10610
10611 /* If the encodings are equal, no conversion needed. */
10612 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010613 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010615 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616
10617 convert_setup(&vimconv, NULL, NULL);
10618 vim_free(from);
10619 vim_free(to);
10620#endif
10621}
10622
10623/*
10624 * "indent()" function
10625 */
10626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010627f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010628 typval_T *argvars;
10629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630{
10631 linenr_T lnum;
10632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010633 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010635 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010637 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638}
10639
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010640/*
10641 * "index()" function
10642 */
10643 static void
10644f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010645 typval_T *argvars;
10646 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010647{
Bram Moolenaar33570922005-01-25 22:26:29 +000010648 list_T *l;
10649 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010650 long idx = 0;
10651 int ic = FALSE;
10652
10653 rettv->vval.v_number = -1;
10654 if (argvars[0].v_type != VAR_LIST)
10655 {
10656 EMSG(_(e_listreq));
10657 return;
10658 }
10659 l = argvars[0].vval.v_list;
10660 if (l != NULL)
10661 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010662 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010663 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010665 int error = FALSE;
10666
Bram Moolenaar758711c2005-02-02 23:11:38 +000010667 /* Start at specified item. Use the cached index that list_find()
10668 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010669 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010670 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010671 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010672 ic = get_tv_number_chk(&argvars[3], &error);
10673 if (error)
10674 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010675 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010676
Bram Moolenaar758711c2005-02-02 23:11:38 +000010677 for ( ; item != NULL; item = item->li_next, ++idx)
10678 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010679 {
10680 rettv->vval.v_number = idx;
10681 break;
10682 }
10683 }
10684}
10685
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686static int inputsecret_flag = 0;
10687
10688/*
10689 * "input()" function
10690 * Also handles inputsecret() when inputsecret is set.
10691 */
10692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010693f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010694 typval_T *argvars;
10695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010697 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698 char_u *p = NULL;
10699 int c;
10700 char_u buf[NUMBUFLEN];
10701 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010702 char_u *defstr = (char_u *)"";
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#ifdef NO_CONSOLE_INPUT
10707 /* While starting up, there is no place to enter text. */
10708 if (no_console_input())
10709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010710 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 return;
10712 }
10713#endif
10714
10715 cmd_silent = FALSE; /* Want to see the prompt. */
10716 if (prompt != NULL)
10717 {
10718 /* Only the part of the message after the last NL is considered as
10719 * prompt for the command line */
10720 p = vim_strrchr(prompt, '\n');
10721 if (p == NULL)
10722 p = prompt;
10723 else
10724 {
10725 ++p;
10726 c = *p;
10727 *p = NUL;
10728 msg_start();
10729 msg_clr_eos();
10730 msg_puts_attr(prompt, echo_attr);
10731 msg_didout = FALSE;
10732 msg_starthere();
10733 *p = c;
10734 }
10735 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010737 if (argvars[1].v_type != VAR_UNKNOWN)
10738 {
10739 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10740 if (defstr != NULL)
10741 stuffReadbuffSpec(defstr);
10742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010744 if (defstr != NULL)
10745 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10747
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010748 /* since the user typed this, no need to wait for return */
10749 need_wait_return = FALSE;
10750 msg_didout = FALSE;
10751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 cmd_silent = cmd_silent_save;
10753}
10754
10755/*
10756 * "inputdialog()" function
10757 */
10758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010759f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010760 typval_T *argvars;
10761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762{
10763#if defined(FEAT_GUI_TEXTDIALOG)
10764 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10765 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10766 {
10767 char_u *message;
10768 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010769 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010771 message = get_tv_string_chk(&argvars[0]);
10772 if (argvars[1].v_type != VAR_UNKNOWN
10773 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010774 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 else
10776 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010777 if (message != NULL && defstr != NULL
10778 && do_dialog(VIM_QUESTION, NULL, message,
10779 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010780 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 else
10782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010783 if (message != NULL && defstr != NULL
10784 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010785 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010786 rettv->vval.v_string = vim_strsave(
10787 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010789 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010791 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 }
10793 else
10794#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010795 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796}
10797
10798static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10799
10800/*
10801 * "inputrestore()" function
10802 */
10803/*ARGSUSED*/
10804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010806 typval_T *argvars;
10807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808{
10809 if (ga_userinput.ga_len > 0)
10810 {
10811 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10813 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010814 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 }
10816 else if (p_verbose > 1)
10817 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010818 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 }
10821}
10822
10823/*
10824 * "inputsave()" function
10825 */
10826/*ARGSUSED*/
10827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010828f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010829 typval_T *argvars;
10830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831{
10832 /* Add an entry to the stack of typehead storage. */
10833 if (ga_grow(&ga_userinput, 1) == OK)
10834 {
10835 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10836 + ga_userinput.ga_len);
10837 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 }
10840 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010841 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842}
10843
10844/*
10845 * "inputsecret()" function
10846 */
10847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010848f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010849 typval_T *argvars;
10850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851{
10852 ++cmdline_star;
10853 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010854 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 --cmdline_star;
10856 --inputsecret_flag;
10857}
10858
10859/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010860 * "insert()" function
10861 */
10862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010863f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010864 typval_T *argvars;
10865 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010866{
10867 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010868 listitem_T *item;
10869 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010870 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010871
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010872 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010873 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010874 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010875 else if ((l = argvars[0].vval.v_list) != NULL
10876 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010877 {
10878 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010879 before = get_tv_number_chk(&argvars[2], &error);
10880 if (error)
10881 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010882
Bram Moolenaar758711c2005-02-02 23:11:38 +000010883 if (before == l->lv_len)
10884 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010885 else
10886 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010887 item = list_find(l, before);
10888 if (item == NULL)
10889 {
10890 EMSGN(_(e_listidx), before);
10891 l = NULL;
10892 }
10893 }
10894 if (l != NULL)
10895 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010896 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010897 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010898 }
10899 }
10900}
10901
10902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 * "isdirectory()" function
10904 */
10905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010906f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010907 typval_T *argvars;
10908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010910 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911}
10912
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010913/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010914 * "islocked()" function
10915 */
10916 static void
10917f_islocked(argvars, rettv)
10918 typval_T *argvars;
10919 typval_T *rettv;
10920{
10921 lval_T lv;
10922 char_u *end;
10923 dictitem_T *di;
10924
10925 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010926 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10927 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010928 if (end != NULL && lv.ll_name != NULL)
10929 {
10930 if (*end != NUL)
10931 EMSG(_(e_trailing));
10932 else
10933 {
10934 if (lv.ll_tv == NULL)
10935 {
10936 if (check_changedtick(lv.ll_name))
10937 rettv->vval.v_number = 1; /* always locked */
10938 else
10939 {
10940 di = find_var(lv.ll_name, NULL);
10941 if (di != NULL)
10942 {
10943 /* Consider a variable locked when:
10944 * 1. the variable itself is locked
10945 * 2. the value of the variable is locked.
10946 * 3. the List or Dict value is locked.
10947 */
10948 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10949 || tv_islocked(&di->di_tv));
10950 }
10951 }
10952 }
10953 else if (lv.ll_range)
10954 EMSG(_("E745: Range not allowed"));
10955 else if (lv.ll_newkey != NULL)
10956 EMSG2(_(e_dictkey), lv.ll_newkey);
10957 else if (lv.ll_list != NULL)
10958 /* List item. */
10959 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10960 else
10961 /* Dictionary item. */
10962 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10963 }
10964 }
10965
10966 clear_lval(&lv);
10967}
10968
Bram Moolenaar33570922005-01-25 22:26:29 +000010969static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010970
10971/*
10972 * Turn a dict into a list:
10973 * "what" == 0: list of keys
10974 * "what" == 1: list of values
10975 * "what" == 2: list of items
10976 */
10977 static void
10978dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010979 typval_T *argvars;
10980 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010981 int what;
10982{
Bram Moolenaar33570922005-01-25 22:26:29 +000010983 list_T *l;
10984 list_T *l2;
10985 dictitem_T *di;
10986 hashitem_T *hi;
10987 listitem_T *li;
10988 listitem_T *li2;
10989 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010990 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010991
10992 rettv->vval.v_number = 0;
10993 if (argvars[0].v_type != VAR_DICT)
10994 {
10995 EMSG(_(e_dictreq));
10996 return;
10997 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010998 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010999 return;
11000
11001 l = list_alloc();
11002 if (l == NULL)
11003 return;
11004 rettv->v_type = VAR_LIST;
11005 rettv->vval.v_list = l;
11006 ++l->lv_refcount;
11007
Bram Moolenaar33570922005-01-25 22:26:29 +000011008 todo = d->dv_hashtab.ht_used;
11009 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011010 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011011 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011012 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011013 --todo;
11014 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011015
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011016 li = listitem_alloc();
11017 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011018 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011019 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011020
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011021 if (what == 0)
11022 {
11023 /* keys() */
11024 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011025 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011026 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11027 }
11028 else if (what == 1)
11029 {
11030 /* values() */
11031 copy_tv(&di->di_tv, &li->li_tv);
11032 }
11033 else
11034 {
11035 /* items() */
11036 l2 = list_alloc();
11037 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011038 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011039 li->li_tv.vval.v_list = l2;
11040 if (l2 == NULL)
11041 break;
11042 ++l2->lv_refcount;
11043
11044 li2 = listitem_alloc();
11045 if (li2 == NULL)
11046 break;
11047 list_append(l2, li2);
11048 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011049 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011050 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11051
11052 li2 = listitem_alloc();
11053 if (li2 == NULL)
11054 break;
11055 list_append(l2, li2);
11056 copy_tv(&di->di_tv, &li2->li_tv);
11057 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011058 }
11059 }
11060}
11061
11062/*
11063 * "items(dict)" function
11064 */
11065 static void
11066f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011067 typval_T *argvars;
11068 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011069{
11070 dict_list(argvars, rettv, 2);
11071}
11072
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011074 * "join()" function
11075 */
11076 static void
11077f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011078 typval_T *argvars;
11079 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011080{
11081 garray_T ga;
11082 char_u *sep;
11083
11084 rettv->vval.v_number = 0;
11085 if (argvars[0].v_type != VAR_LIST)
11086 {
11087 EMSG(_(e_listreq));
11088 return;
11089 }
11090 if (argvars[0].vval.v_list == NULL)
11091 return;
11092 if (argvars[1].v_type == VAR_UNKNOWN)
11093 sep = (char_u *)" ";
11094 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011096
11097 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011098
11099 if (sep != NULL)
11100 {
11101 ga_init2(&ga, (int)sizeof(char), 80);
11102 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11103 ga_append(&ga, NUL);
11104 rettv->vval.v_string = (char_u *)ga.ga_data;
11105 }
11106 else
11107 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011108}
11109
11110/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011111 * "keys()" function
11112 */
11113 static void
11114f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011115 typval_T *argvars;
11116 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011117{
11118 dict_list(argvars, rettv, 0);
11119}
11120
11121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 * "last_buffer_nr()" function.
11123 */
11124/*ARGSUSED*/
11125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011126f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011127 typval_T *argvars;
11128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129{
11130 int n = 0;
11131 buf_T *buf;
11132
11133 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11134 if (n < buf->b_fnum)
11135 n = buf->b_fnum;
11136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011138}
11139
11140/*
11141 * "len()" function
11142 */
11143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011145 typval_T *argvars;
11146 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011147{
11148 switch (argvars[0].v_type)
11149 {
11150 case VAR_STRING:
11151 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152 rettv->vval.v_number = (varnumber_T)STRLEN(
11153 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011154 break;
11155 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011156 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011157 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011158 case VAR_DICT:
11159 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11160 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011161 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011162 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011163 break;
11164 }
11165}
11166
Bram Moolenaar33570922005-01-25 22:26:29 +000011167static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011168
11169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011170libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011171 typval_T *argvars;
11172 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011173 int type;
11174{
11175#ifdef FEAT_LIBCALL
11176 char_u *string_in;
11177 char_u **string_result;
11178 int nr_result;
11179#endif
11180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011181 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011182 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011183 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011184 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011185 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011186
11187 if (check_restricted() || check_secure())
11188 return;
11189
11190#ifdef FEAT_LIBCALL
11191 /* The first two args must be strings, otherwise its meaningless */
11192 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11193 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011194 string_in = NULL;
11195 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011196 string_in = argvars[2].vval.v_string;
11197 if (type == VAR_NUMBER)
11198 string_result = NULL;
11199 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011200 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011201 if (mch_libcall(argvars[0].vval.v_string,
11202 argvars[1].vval.v_string,
11203 string_in,
11204 argvars[2].vval.v_number,
11205 string_result,
11206 &nr_result) == OK
11207 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011208 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011209 }
11210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211}
11212
11213/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011214 * "libcall()" function
11215 */
11216 static void
11217f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011218 typval_T *argvars;
11219 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011220{
11221 libcall_common(argvars, rettv, VAR_STRING);
11222}
11223
11224/*
11225 * "libcallnr()" function
11226 */
11227 static void
11228f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011229 typval_T *argvars;
11230 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011231{
11232 libcall_common(argvars, rettv, VAR_NUMBER);
11233}
11234
11235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 * "line(string)" function
11237 */
11238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011239f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011240 typval_T *argvars;
11241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242{
11243 linenr_T lnum = 0;
11244 pos_T *fp;
11245
11246 fp = var2fpos(&argvars[0], TRUE);
11247 if (fp != NULL)
11248 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011249 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250}
11251
11252/*
11253 * "line2byte(lnum)" function
11254 */
11255/*ARGSUSED*/
11256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011257f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011258 typval_T *argvars;
11259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260{
11261#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011262 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263#else
11264 linenr_T lnum;
11265
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011266 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011270 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11271 if (rettv->vval.v_number >= 0)
11272 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273#endif
11274}
11275
11276/*
11277 * "lispindent(lnum)" function
11278 */
11279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011281 typval_T *argvars;
11282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283{
11284#ifdef FEAT_LISP
11285 pos_T pos;
11286 linenr_T lnum;
11287
11288 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011289 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11291 {
11292 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011293 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 curwin->w_cursor = pos;
11295 }
11296 else
11297#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299}
11300
11301/*
11302 * "localtime()" function
11303 */
11304/*ARGSUSED*/
11305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011306f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011307 typval_T *argvars;
11308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011310 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311}
11312
Bram Moolenaar33570922005-01-25 22:26:29 +000011313static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314
11315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011316get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011317 typval_T *argvars;
11318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 int exact;
11320{
11321 char_u *keys;
11322 char_u *which;
11323 char_u buf[NUMBUFLEN];
11324 char_u *keys_buf = NULL;
11325 char_u *rhs;
11326 int mode;
11327 garray_T ga;
11328
11329 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330 rettv->v_type = VAR_STRING;
11331 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011333 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 if (*keys == NUL)
11335 return;
11336
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011337 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011338 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 else
11340 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011341 if (which == NULL)
11342 return;
11343
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 mode = get_map_mode(&which, 0);
11345
11346 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011347 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348 vim_free(keys_buf);
11349 if (rhs != NULL)
11350 {
11351 ga_init(&ga);
11352 ga.ga_itemsize = 1;
11353 ga.ga_growsize = 40;
11354
11355 while (*rhs != NUL)
11356 ga_concat(&ga, str2special(&rhs, FALSE));
11357
11358 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 }
11361}
11362
11363/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011364 * "map()" function
11365 */
11366 static void
11367f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011368 typval_T *argvars;
11369 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011370{
11371 filter_map(argvars, rettv, TRUE);
11372}
11373
11374/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011375 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376 */
11377 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011378f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011379 typval_T *argvars;
11380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011382 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383}
11384
11385/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011386 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387 */
11388 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011389f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011390 typval_T *argvars;
11391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011393 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394}
11395
Bram Moolenaar33570922005-01-25 22:26:29 +000011396static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397
11398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011399find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011400 typval_T *argvars;
11401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 int type;
11403{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011404 char_u *str = NULL;
11405 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 char_u *pat;
11407 regmatch_T regmatch;
11408 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011409 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 char_u *save_cpo;
11411 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011412 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011413 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011414 list_T *l = NULL;
11415 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011416 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011417 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418
11419 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11420 save_cpo = p_cpo;
11421 p_cpo = (char_u *)"";
11422
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011423 rettv->vval.v_number = -1;
11424 if (type == 3)
11425 {
11426 /* return empty list when there are no matches */
11427 if ((rettv->vval.v_list = list_alloc()) == NULL)
11428 goto theend;
11429 rettv->v_type = VAR_LIST;
11430 ++rettv->vval.v_list->lv_refcount;
11431 }
11432 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011434 rettv->v_type = VAR_STRING;
11435 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011438 if (argvars[0].v_type == VAR_LIST)
11439 {
11440 if ((l = argvars[0].vval.v_list) == NULL)
11441 goto theend;
11442 li = l->lv_first;
11443 }
11444 else
11445 expr = str = get_tv_string(&argvars[0]);
11446
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011447 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11448 if (pat == NULL)
11449 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011450
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011451 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011453 int error = FALSE;
11454
11455 start = get_tv_number_chk(&argvars[2], &error);
11456 if (error)
11457 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011458 if (l != NULL)
11459 {
11460 li = list_find(l, start);
11461 if (li == NULL)
11462 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011463 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011464 }
11465 else
11466 {
11467 if (start < 0)
11468 start = 0;
11469 if (start > (long)STRLEN(str))
11470 goto theend;
11471 str += start;
11472 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011473
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011474 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011475 nth = get_tv_number_chk(&argvars[3], &error);
11476 if (error)
11477 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478 }
11479
11480 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11481 if (regmatch.regprog != NULL)
11482 {
11483 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011484
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011485 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011486 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011487 if (l != NULL)
11488 {
11489 if (li == NULL)
11490 {
11491 match = FALSE;
11492 break;
11493 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011494 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011495 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011496 if (str == NULL)
11497 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011498 }
11499
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011500 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011501
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011502 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011503 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011504 if (l == NULL && !match)
11505 break;
11506
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011507 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011508 if (l != NULL)
11509 {
11510 li = li->li_next;
11511 ++idx;
11512 }
11513 else
11514 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011515#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011516 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011517#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011518 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011519#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011520 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011521 }
11522
11523 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011525 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011526 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011527 int i;
11528
11529 /* return list with matched string and submatches */
11530 for (i = 0; i < NSUBEXP; ++i)
11531 {
11532 if (regmatch.endp[i] == NULL)
11533 break;
11534 li = listitem_alloc();
11535 if (li == NULL)
11536 break;
11537 li->li_tv.v_type = VAR_STRING;
11538 li->li_tv.v_lock = 0;
11539 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11540 (int)(regmatch.endp[i] - regmatch.startp[i]));
11541 list_append(rettv->vval.v_list, li);
11542 }
11543 }
11544 else if (type == 2)
11545 {
11546 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011547 if (l != NULL)
11548 copy_tv(&li->li_tv, rettv);
11549 else
11550 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011552 }
11553 else if (l != NULL)
11554 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555 else
11556 {
11557 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559 (varnumber_T)(regmatch.startp[0] - str);
11560 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011561 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564 }
11565 }
11566 vim_free(regmatch.regprog);
11567 }
11568
11569theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011570 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571 p_cpo = save_cpo;
11572}
11573
11574/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011575 * "match()" function
11576 */
11577 static void
11578f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011579 typval_T *argvars;
11580 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011581{
11582 find_some_match(argvars, rettv, 1);
11583}
11584
11585/*
11586 * "matchend()" function
11587 */
11588 static void
11589f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011590 typval_T *argvars;
11591 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011592{
11593 find_some_match(argvars, rettv, 0);
11594}
11595
11596/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011597 * "matchlist()" function
11598 */
11599 static void
11600f_matchlist(argvars, rettv)
11601 typval_T *argvars;
11602 typval_T *rettv;
11603{
11604 find_some_match(argvars, rettv, 3);
11605}
11606
11607/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011608 * "matchstr()" function
11609 */
11610 static void
11611f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011612 typval_T *argvars;
11613 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011614{
11615 find_some_match(argvars, rettv, 2);
11616}
11617
Bram Moolenaar33570922005-01-25 22:26:29 +000011618static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011619
11620 static void
11621max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011622 typval_T *argvars;
11623 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011624 int domax;
11625{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011626 long n = 0;
11627 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011628 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011629
11630 if (argvars[0].v_type == VAR_LIST)
11631 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011632 list_T *l;
11633 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011634
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011635 l = argvars[0].vval.v_list;
11636 if (l != NULL)
11637 {
11638 li = l->lv_first;
11639 if (li != NULL)
11640 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011641 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011642 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011643 {
11644 li = li->li_next;
11645 if (li == NULL)
11646 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011647 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011648 if (domax ? i > n : i < n)
11649 n = i;
11650 }
11651 }
11652 }
11653 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011654 else if (argvars[0].v_type == VAR_DICT)
11655 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011656 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011657 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011658 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011659 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011660
11661 d = argvars[0].vval.v_dict;
11662 if (d != NULL)
11663 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011664 todo = d->dv_hashtab.ht_used;
11665 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011666 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011667 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011668 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011669 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011670 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011671 if (first)
11672 {
11673 n = i;
11674 first = FALSE;
11675 }
11676 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011677 n = i;
11678 }
11679 }
11680 }
11681 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011682 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011683 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011684 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011685}
11686
11687/*
11688 * "max()" function
11689 */
11690 static void
11691f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011692 typval_T *argvars;
11693 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011694{
11695 max_min(argvars, rettv, TRUE);
11696}
11697
11698/*
11699 * "min()" function
11700 */
11701 static void
11702f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011703 typval_T *argvars;
11704 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011705{
11706 max_min(argvars, rettv, FALSE);
11707}
11708
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011709static int mkdir_recurse __ARGS((char_u *dir, int prot));
11710
11711/*
11712 * Create the directory in which "dir" is located, and higher levels when
11713 * needed.
11714 */
11715 static int
11716mkdir_recurse(dir, prot)
11717 char_u *dir;
11718 int prot;
11719{
11720 char_u *p;
11721 char_u *updir;
11722 int r = FAIL;
11723
11724 /* Get end of directory name in "dir".
11725 * We're done when it's "/" or "c:/". */
11726 p = gettail_sep(dir);
11727 if (p <= get_past_head(dir))
11728 return OK;
11729
11730 /* If the directory exists we're done. Otherwise: create it.*/
11731 updir = vim_strnsave(dir, (int)(p - dir));
11732 if (updir == NULL)
11733 return FAIL;
11734 if (mch_isdir(updir))
11735 r = OK;
11736 else if (mkdir_recurse(updir, prot) == OK)
11737 r = vim_mkdir_emsg(updir, prot);
11738 vim_free(updir);
11739 return r;
11740}
11741
11742#ifdef vim_mkdir
11743/*
11744 * "mkdir()" function
11745 */
11746 static void
11747f_mkdir(argvars, rettv)
11748 typval_T *argvars;
11749 typval_T *rettv;
11750{
11751 char_u *dir;
11752 char_u buf[NUMBUFLEN];
11753 int prot = 0755;
11754
11755 rettv->vval.v_number = FAIL;
11756 if (check_restricted() || check_secure())
11757 return;
11758
11759 dir = get_tv_string_buf(&argvars[0], buf);
11760 if (argvars[1].v_type != VAR_UNKNOWN)
11761 {
11762 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011763 prot = get_tv_number_chk(&argvars[2], NULL);
11764 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011765 mkdir_recurse(dir, prot);
11766 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011767 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011768}
11769#endif
11770
Bram Moolenaar0d660222005-01-07 21:51:51 +000011771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 * "mode()" function
11773 */
11774/*ARGSUSED*/
11775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011776f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011777 typval_T *argvars;
11778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779{
11780 char_u buf[2];
11781
11782#ifdef FEAT_VISUAL
11783 if (VIsual_active)
11784 {
11785 if (VIsual_select)
11786 buf[0] = VIsual_mode + 's' - 'v';
11787 else
11788 buf[0] = VIsual_mode;
11789 }
11790 else
11791#endif
11792 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11793 buf[0] = 'r';
11794 else if (State & INSERT)
11795 {
11796 if (State & REPLACE_FLAG)
11797 buf[0] = 'R';
11798 else
11799 buf[0] = 'i';
11800 }
11801 else if (State & CMDLINE)
11802 buf[0] = 'c';
11803 else
11804 buf[0] = 'n';
11805
11806 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011807 rettv->vval.v_string = vim_strsave(buf);
11808 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809}
11810
11811/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011812 * "nextnonblank()" function
11813 */
11814 static void
11815f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011816 typval_T *argvars;
11817 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011818{
11819 linenr_T lnum;
11820
11821 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011823 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011824 {
11825 lnum = 0;
11826 break;
11827 }
11828 if (*skipwhite(ml_get(lnum)) != NUL)
11829 break;
11830 }
11831 rettv->vval.v_number = lnum;
11832}
11833
11834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835 * "nr2char()" function
11836 */
11837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011838f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011839 typval_T *argvars;
11840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841{
11842 char_u buf[NUMBUFLEN];
11843
11844#ifdef FEAT_MBYTE
11845 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847 else
11848#endif
11849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011850 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851 buf[1] = NUL;
11852 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853 rettv->v_type = VAR_STRING;
11854 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011855}
11856
11857/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011858 * "prevnonblank()" function
11859 */
11860 static void
11861f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011862 typval_T *argvars;
11863 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011864{
11865 linenr_T lnum;
11866
11867 lnum = get_tv_lnum(argvars);
11868 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11869 lnum = 0;
11870 else
11871 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11872 --lnum;
11873 rettv->vval.v_number = lnum;
11874}
11875
Bram Moolenaar8c711452005-01-14 21:53:12 +000011876/*
11877 * "range()" function
11878 */
11879 static void
11880f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011881 typval_T *argvars;
11882 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011883{
11884 long start;
11885 long end;
11886 long stride = 1;
11887 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011888 list_T *l;
11889 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011890 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011892 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011893 if (argvars[1].v_type == VAR_UNKNOWN)
11894 {
11895 end = start - 1;
11896 start = 0;
11897 }
11898 else
11899 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011900 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011901 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011902 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011903 }
11904
11905 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011906 if (error)
11907 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000011908 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011909 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000011910 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011911 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011912 else
11913 {
11914 l = list_alloc();
11915 if (l != NULL)
11916 {
11917 rettv->v_type = VAR_LIST;
11918 rettv->vval.v_list = l;
11919 ++l->lv_refcount;
11920
11921 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11922 {
11923 li = listitem_alloc();
11924 if (li == NULL)
11925 break;
11926 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011927 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011928 li->li_tv.vval.v_number = i;
11929 list_append(l, li);
11930 }
11931 }
11932 }
11933}
11934
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011935/*
11936 * "readfile()" function
11937 */
11938 static void
11939f_readfile(argvars, rettv)
11940 typval_T *argvars;
11941 typval_T *rettv;
11942{
11943 int binary = FALSE;
11944 char_u *fname;
11945 FILE *fd;
11946 list_T *l;
11947 listitem_T *li;
11948#define FREAD_SIZE 200 /* optimized for text lines */
11949 char_u buf[FREAD_SIZE];
11950 int readlen; /* size of last fread() */
11951 int buflen; /* nr of valid chars in buf[] */
11952 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11953 int tolist; /* first byte in buf[] still to be put in list */
11954 int chop; /* how many CR to chop off */
11955 char_u *prev = NULL; /* previously read bytes, if any */
11956 int prevlen = 0; /* length of "prev" if not NULL */
11957 char_u *s;
11958 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011959 long maxline = MAXLNUM;
11960 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011961
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011962 if (argvars[1].v_type != VAR_UNKNOWN)
11963 {
11964 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11965 binary = TRUE;
11966 if (argvars[2].v_type != VAR_UNKNOWN)
11967 maxline = get_tv_number(&argvars[2]);
11968 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011969
11970 l = list_alloc();
11971 if (l == NULL)
11972 return;
11973 rettv->v_type = VAR_LIST;
11974 rettv->vval.v_list = l;
11975 l->lv_refcount = 1;
11976
11977 /* Always open the file in binary mode, library functions have a mind of
11978 * their own about CR-LF conversion. */
11979 fname = get_tv_string(&argvars[0]);
11980 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11981 {
11982 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11983 return;
11984 }
11985
11986 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011987 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011988 {
11989 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11990 buflen = filtd + readlen;
11991 tolist = 0;
11992 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11993 {
11994 if (buf[filtd] == '\n' || readlen <= 0)
11995 {
11996 /* Only when in binary mode add an empty list item when the
11997 * last line ends in a '\n'. */
11998 if (!binary && readlen == 0 && filtd == 0)
11999 break;
12000
12001 /* Found end-of-line or end-of-file: add a text line to the
12002 * list. */
12003 chop = 0;
12004 if (!binary)
12005 while (filtd - chop - 1 >= tolist
12006 && buf[filtd - chop - 1] == '\r')
12007 ++chop;
12008 len = filtd - tolist - chop;
12009 if (prev == NULL)
12010 s = vim_strnsave(buf + tolist, len);
12011 else
12012 {
12013 s = alloc((unsigned)(prevlen + len + 1));
12014 if (s != NULL)
12015 {
12016 mch_memmove(s, prev, prevlen);
12017 vim_free(prev);
12018 prev = NULL;
12019 mch_memmove(s + prevlen, buf + tolist, len);
12020 s[prevlen + len] = NUL;
12021 }
12022 }
12023 tolist = filtd + 1;
12024
12025 li = listitem_alloc();
12026 if (li == NULL)
12027 {
12028 vim_free(s);
12029 break;
12030 }
12031 li->li_tv.v_type = VAR_STRING;
12032 li->li_tv.v_lock = 0;
12033 li->li_tv.vval.v_string = s;
12034 list_append(l, li);
12035
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012036 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012037 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012038 if (readlen <= 0)
12039 break;
12040 }
12041 else if (buf[filtd] == NUL)
12042 buf[filtd] = '\n';
12043 }
12044 if (readlen <= 0)
12045 break;
12046
12047 if (tolist == 0)
12048 {
12049 /* "buf" is full, need to move text to an allocated buffer */
12050 if (prev == NULL)
12051 {
12052 prev = vim_strnsave(buf, buflen);
12053 prevlen = buflen;
12054 }
12055 else
12056 {
12057 s = alloc((unsigned)(prevlen + buflen));
12058 if (s != NULL)
12059 {
12060 mch_memmove(s, prev, prevlen);
12061 mch_memmove(s + prevlen, buf, buflen);
12062 vim_free(prev);
12063 prev = s;
12064 prevlen += buflen;
12065 }
12066 }
12067 filtd = 0;
12068 }
12069 else
12070 {
12071 mch_memmove(buf, buf + tolist, buflen - tolist);
12072 filtd -= tolist;
12073 }
12074 }
12075
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012076 /*
12077 * For a negative line count use only the lines at the end of the file,
12078 * free the rest.
12079 */
12080 if (maxline < 0)
12081 while (cnt > -maxline)
12082 {
12083 listitem_remove(l, l->lv_first);
12084 --cnt;
12085 }
12086
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012087 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012088 fclose(fd);
12089}
12090
12091
Bram Moolenaar0d660222005-01-07 21:51:51 +000012092#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12093static void make_connection __ARGS((void));
12094static int check_connection __ARGS((void));
12095
12096 static void
12097make_connection()
12098{
12099 if (X_DISPLAY == NULL
12100# ifdef FEAT_GUI
12101 && !gui.in_use
12102# endif
12103 )
12104 {
12105 x_force_connect = TRUE;
12106 setup_term_clip();
12107 x_force_connect = FALSE;
12108 }
12109}
12110
12111 static int
12112check_connection()
12113{
12114 make_connection();
12115 if (X_DISPLAY == NULL)
12116 {
12117 EMSG(_("E240: No connection to Vim server"));
12118 return FAIL;
12119 }
12120 return OK;
12121}
12122#endif
12123
12124#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012125static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012126
12127 static void
12128remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012129 typval_T *argvars;
12130 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012131 int expr;
12132{
12133 char_u *server_name;
12134 char_u *keys;
12135 char_u *r = NULL;
12136 char_u buf[NUMBUFLEN];
12137# ifdef WIN32
12138 HWND w;
12139# else
12140 Window w;
12141# endif
12142
12143 if (check_restricted() || check_secure())
12144 return;
12145
12146# ifdef FEAT_X11
12147 if (check_connection() == FAIL)
12148 return;
12149# endif
12150
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012151 server_name = get_tv_string_chk(&argvars[0]);
12152 if (server_name == NULL)
12153 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012154 keys = get_tv_string_buf(&argvars[1], buf);
12155# ifdef WIN32
12156 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12157# else
12158 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12159 < 0)
12160# endif
12161 {
12162 if (r != NULL)
12163 EMSG(r); /* sending worked but evaluation failed */
12164 else
12165 EMSG2(_("E241: Unable to send to %s"), server_name);
12166 return;
12167 }
12168
12169 rettv->vval.v_string = r;
12170
12171 if (argvars[2].v_type != VAR_UNKNOWN)
12172 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012173 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012174 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012175 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012176
12177 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012178 v.di_tv.v_type = VAR_STRING;
12179 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012180 idvar = get_tv_string_chk(&argvars[2]);
12181 if (idvar != NULL)
12182 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012183 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012184 }
12185}
12186#endif
12187
12188/*
12189 * "remote_expr()" function
12190 */
12191/*ARGSUSED*/
12192 static void
12193f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012194 typval_T *argvars;
12195 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012196{
12197 rettv->v_type = VAR_STRING;
12198 rettv->vval.v_string = NULL;
12199#ifdef FEAT_CLIENTSERVER
12200 remote_common(argvars, rettv, TRUE);
12201#endif
12202}
12203
12204/*
12205 * "remote_foreground()" function
12206 */
12207/*ARGSUSED*/
12208 static void
12209f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012210 typval_T *argvars;
12211 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012212{
12213 rettv->vval.v_number = 0;
12214#ifdef FEAT_CLIENTSERVER
12215# ifdef WIN32
12216 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012217 {
12218 char_u *server_name = get_tv_string_chk(&argvars[0]);
12219
12220 if (server_name != NULL)
12221 serverForeground(server_name);
12222 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012223# else
12224 /* Send a foreground() expression to the server. */
12225 argvars[1].v_type = VAR_STRING;
12226 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12227 argvars[2].v_type = VAR_UNKNOWN;
12228 remote_common(argvars, rettv, TRUE);
12229 vim_free(argvars[1].vval.v_string);
12230# endif
12231#endif
12232}
12233
12234/*ARGSUSED*/
12235 static void
12236f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012237 typval_T *argvars;
12238 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012239{
12240#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012241 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012242 char_u *s = NULL;
12243# ifdef WIN32
12244 int n = 0;
12245# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012246 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012247
12248 if (check_restricted() || check_secure())
12249 {
12250 rettv->vval.v_number = -1;
12251 return;
12252 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012253 serverid = get_tv_string_chk(&argvars[0]);
12254 if (serverid == NULL)
12255 {
12256 rettv->vval.v_number = -1;
12257 return; /* type error; errmsg already given */
12258 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012259# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012260 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012261 if (n == 0)
12262 rettv->vval.v_number = -1;
12263 else
12264 {
12265 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12266 rettv->vval.v_number = (s != NULL);
12267 }
12268# else
12269 rettv->vval.v_number = 0;
12270 if (check_connection() == FAIL)
12271 return;
12272
12273 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012274 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012275# endif
12276
12277 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012279 char_u *retvar;
12280
Bram Moolenaar33570922005-01-25 22:26:29 +000012281 v.di_tv.v_type = VAR_STRING;
12282 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012283 retvar = get_tv_string_chk(&argvars[1]);
12284 if (retvar != NULL)
12285 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012286 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012287 }
12288#else
12289 rettv->vval.v_number = -1;
12290#endif
12291}
12292
12293/*ARGSUSED*/
12294 static void
12295f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012296 typval_T *argvars;
12297 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012298{
12299 char_u *r = NULL;
12300
12301#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012302 char_u *serverid = get_tv_string_chk(&argvars[0]);
12303
12304 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012305 {
12306# ifdef WIN32
12307 /* The server's HWND is encoded in the 'id' parameter */
12308 int n = 0;
12309
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012310 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012311 if (n != 0)
12312 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12313 if (r == NULL)
12314# else
12315 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012316 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012317# endif
12318 EMSG(_("E277: Unable to read a server reply"));
12319 }
12320#endif
12321 rettv->v_type = VAR_STRING;
12322 rettv->vval.v_string = r;
12323}
12324
12325/*
12326 * "remote_send()" function
12327 */
12328/*ARGSUSED*/
12329 static void
12330f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012331 typval_T *argvars;
12332 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012333{
12334 rettv->v_type = VAR_STRING;
12335 rettv->vval.v_string = NULL;
12336#ifdef FEAT_CLIENTSERVER
12337 remote_common(argvars, rettv, FALSE);
12338#endif
12339}
12340
12341/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012342 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012343 */
12344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012345f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012346 typval_T *argvars;
12347 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012348{
Bram Moolenaar33570922005-01-25 22:26:29 +000012349 list_T *l;
12350 listitem_T *item, *item2;
12351 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012352 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012353 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012354 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012355 dict_T *d;
12356 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012357
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012358 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012359 if (argvars[0].v_type == VAR_DICT)
12360 {
12361 if (argvars[2].v_type != VAR_UNKNOWN)
12362 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012363 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012364 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012365 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012366 key = get_tv_string_chk(&argvars[1]);
12367 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012369 di = dict_find(d, key, -1);
12370 if (di == NULL)
12371 EMSG2(_(e_dictkey), key);
12372 else
12373 {
12374 *rettv = di->di_tv;
12375 init_tv(&di->di_tv);
12376 dictitem_remove(d, di);
12377 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012378 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012379 }
12380 }
12381 else if (argvars[0].v_type != VAR_LIST)
12382 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012383 else if ((l = argvars[0].vval.v_list) != NULL
12384 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012386 int error = FALSE;
12387
12388 idx = get_tv_number_chk(&argvars[1], &error);
12389 if (error)
12390 ; /* type error: do nothing, errmsg already given */
12391 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012392 EMSGN(_(e_listidx), idx);
12393 else
12394 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012395 if (argvars[2].v_type == VAR_UNKNOWN)
12396 {
12397 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012398 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012399 *rettv = item->li_tv;
12400 vim_free(item);
12401 }
12402 else
12403 {
12404 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012405 end = get_tv_number_chk(&argvars[2], &error);
12406 if (error)
12407 ; /* type error: do nothing */
12408 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012409 EMSGN(_(e_listidx), end);
12410 else
12411 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012412 int cnt = 0;
12413
12414 for (li = item; li != NULL; li = li->li_next)
12415 {
12416 ++cnt;
12417 if (li == item2)
12418 break;
12419 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012420 if (li == NULL) /* didn't find "item2" after "item" */
12421 EMSG(_(e_invrange));
12422 else
12423 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012424 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012425 l = list_alloc();
12426 if (l != NULL)
12427 {
12428 rettv->v_type = VAR_LIST;
12429 rettv->vval.v_list = l;
12430 l->lv_first = item;
12431 l->lv_last = item2;
12432 l->lv_refcount = 1;
12433 item->li_prev = NULL;
12434 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012435 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012436 }
12437 }
12438 }
12439 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012440 }
12441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442}
12443
12444/*
12445 * "rename({from}, {to})" function
12446 */
12447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012448f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012449 typval_T *argvars;
12450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451{
12452 char_u buf[NUMBUFLEN];
12453
12454 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012455 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012456 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012457 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12458 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459}
12460
12461/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012462 * "repeat()" function
12463 */
12464/*ARGSUSED*/
12465 static void
12466f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012467 typval_T *argvars;
12468 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012469{
12470 char_u *p;
12471 int n;
12472 int slen;
12473 int len;
12474 char_u *r;
12475 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012476 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012477
12478 n = get_tv_number(&argvars[1]);
12479 if (argvars[0].v_type == VAR_LIST)
12480 {
12481 l = list_alloc();
12482 if (l != NULL && argvars[0].vval.v_list != NULL)
12483 {
12484 l->lv_refcount = 1;
12485 while (n-- > 0)
12486 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12487 break;
12488 }
12489 rettv->v_type = VAR_LIST;
12490 rettv->vval.v_list = l;
12491 }
12492 else
12493 {
12494 p = get_tv_string(&argvars[0]);
12495 rettv->v_type = VAR_STRING;
12496 rettv->vval.v_string = NULL;
12497
12498 slen = (int)STRLEN(p);
12499 len = slen * n;
12500 if (len <= 0)
12501 return;
12502
12503 r = alloc(len + 1);
12504 if (r != NULL)
12505 {
12506 for (i = 0; i < n; i++)
12507 mch_memmove(r + i * slen, p, (size_t)slen);
12508 r[len] = NUL;
12509 }
12510
12511 rettv->vval.v_string = r;
12512 }
12513}
12514
12515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012516 * "resolve()" function
12517 */
12518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012519f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012520 typval_T *argvars;
12521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522{
12523 char_u *p;
12524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012525 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526#ifdef FEAT_SHORTCUT
12527 {
12528 char_u *v = NULL;
12529
12530 v = mch_resolve_shortcut(p);
12531 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012532 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012534 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535 }
12536#else
12537# ifdef HAVE_READLINK
12538 {
12539 char_u buf[MAXPATHL + 1];
12540 char_u *cpy;
12541 int len;
12542 char_u *remain = NULL;
12543 char_u *q;
12544 int is_relative_to_current = FALSE;
12545 int has_trailing_pathsep = FALSE;
12546 int limit = 100;
12547
12548 p = vim_strsave(p);
12549
12550 if (p[0] == '.' && (vim_ispathsep(p[1])
12551 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12552 is_relative_to_current = TRUE;
12553
12554 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012555 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 has_trailing_pathsep = TRUE;
12557
12558 q = getnextcomp(p);
12559 if (*q != NUL)
12560 {
12561 /* Separate the first path component in "p", and keep the
12562 * remainder (beginning with the path separator). */
12563 remain = vim_strsave(q - 1);
12564 q[-1] = NUL;
12565 }
12566
12567 for (;;)
12568 {
12569 for (;;)
12570 {
12571 len = readlink((char *)p, (char *)buf, MAXPATHL);
12572 if (len <= 0)
12573 break;
12574 buf[len] = NUL;
12575
12576 if (limit-- == 0)
12577 {
12578 vim_free(p);
12579 vim_free(remain);
12580 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012581 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012582 goto fail;
12583 }
12584
12585 /* Ensure that the result will have a trailing path separator
12586 * if the argument has one. */
12587 if (remain == NULL && has_trailing_pathsep)
12588 add_pathsep(buf);
12589
12590 /* Separate the first path component in the link value and
12591 * concatenate the remainders. */
12592 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12593 if (*q != NUL)
12594 {
12595 if (remain == NULL)
12596 remain = vim_strsave(q - 1);
12597 else
12598 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012599 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600 if (cpy != NULL)
12601 {
12602 STRCAT(cpy, remain);
12603 vim_free(remain);
12604 remain = cpy;
12605 }
12606 }
12607 q[-1] = NUL;
12608 }
12609
12610 q = gettail(p);
12611 if (q > p && *q == NUL)
12612 {
12613 /* Ignore trailing path separator. */
12614 q[-1] = NUL;
12615 q = gettail(p);
12616 }
12617 if (q > p && !mch_isFullName(buf))
12618 {
12619 /* symlink is relative to directory of argument */
12620 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12621 if (cpy != NULL)
12622 {
12623 STRCPY(cpy, p);
12624 STRCPY(gettail(cpy), buf);
12625 vim_free(p);
12626 p = cpy;
12627 }
12628 }
12629 else
12630 {
12631 vim_free(p);
12632 p = vim_strsave(buf);
12633 }
12634 }
12635
12636 if (remain == NULL)
12637 break;
12638
12639 /* Append the first path component of "remain" to "p". */
12640 q = getnextcomp(remain + 1);
12641 len = q - remain - (*q != NUL);
12642 cpy = vim_strnsave(p, STRLEN(p) + len);
12643 if (cpy != NULL)
12644 {
12645 STRNCAT(cpy, remain, len);
12646 vim_free(p);
12647 p = cpy;
12648 }
12649 /* Shorten "remain". */
12650 if (*q != NUL)
12651 STRCPY(remain, q - 1);
12652 else
12653 {
12654 vim_free(remain);
12655 remain = NULL;
12656 }
12657 }
12658
12659 /* If the result is a relative path name, make it explicitly relative to
12660 * the current directory if and only if the argument had this form. */
12661 if (!vim_ispathsep(*p))
12662 {
12663 if (is_relative_to_current
12664 && *p != NUL
12665 && !(p[0] == '.'
12666 && (p[1] == NUL
12667 || vim_ispathsep(p[1])
12668 || (p[1] == '.'
12669 && (p[2] == NUL
12670 || vim_ispathsep(p[2]))))))
12671 {
12672 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012673 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 if (cpy != NULL)
12675 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676 vim_free(p);
12677 p = cpy;
12678 }
12679 }
12680 else if (!is_relative_to_current)
12681 {
12682 /* Strip leading "./". */
12683 q = p;
12684 while (q[0] == '.' && vim_ispathsep(q[1]))
12685 q += 2;
12686 if (q > p)
12687 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12688 }
12689 }
12690
12691 /* Ensure that the result will have no trailing path separator
12692 * if the argument had none. But keep "/" or "//". */
12693 if (!has_trailing_pathsep)
12694 {
12695 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012696 if (after_pathsep(p, q))
12697 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698 }
12699
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012700 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701 }
12702# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012703 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704# endif
12705#endif
12706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012707 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708
12709#ifdef HAVE_READLINK
12710fail:
12711#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713}
12714
12715/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012716 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717 */
12718 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012719f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012720 typval_T *argvars;
12721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722{
Bram Moolenaar33570922005-01-25 22:26:29 +000012723 list_T *l;
12724 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725
Bram Moolenaar0d660222005-01-07 21:51:51 +000012726 rettv->vval.v_number = 0;
12727 if (argvars[0].v_type != VAR_LIST)
12728 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012729 else if ((l = argvars[0].vval.v_list) != NULL
12730 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012731 {
12732 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012733 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012734 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012735 while (li != NULL)
12736 {
12737 ni = li->li_prev;
12738 list_append(l, li);
12739 li = ni;
12740 }
12741 rettv->vval.v_list = l;
12742 rettv->v_type = VAR_LIST;
12743 ++l->lv_refcount;
12744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745}
12746
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012747#define SP_NOMOVE 1 /* don't move cursor */
12748#define SP_REPEAT 2 /* repeat to find outer pair */
12749#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000012750#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012751
Bram Moolenaar33570922005-01-25 22:26:29 +000012752static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012753
12754/*
12755 * Get flags for a search function.
12756 * Possibly sets "p_ws".
12757 * Returns BACKWARD, FORWARD or zero (for an error).
12758 */
12759 static int
12760get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012761 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012762 int *flagsp;
12763{
12764 int dir = FORWARD;
12765 char_u *flags;
12766 char_u nbuf[NUMBUFLEN];
12767 int mask;
12768
12769 if (varp->v_type != VAR_UNKNOWN)
12770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012771 flags = get_tv_string_buf_chk(varp, nbuf);
12772 if (flags == NULL)
12773 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012774 while (*flags != NUL)
12775 {
12776 switch (*flags)
12777 {
12778 case 'b': dir = BACKWARD; break;
12779 case 'w': p_ws = TRUE; break;
12780 case 'W': p_ws = FALSE; break;
12781 default: mask = 0;
12782 if (flagsp != NULL)
12783 switch (*flags)
12784 {
12785 case 'n': mask = SP_NOMOVE; break;
12786 case 'r': mask = SP_REPEAT; break;
12787 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012788 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012789 }
12790 if (mask == 0)
12791 {
12792 EMSG2(_(e_invarg2), flags);
12793 dir = 0;
12794 }
12795 else
12796 *flagsp |= mask;
12797 }
12798 if (dir == 0)
12799 break;
12800 ++flags;
12801 }
12802 }
12803 return dir;
12804}
12805
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806/*
12807 * "search()" function
12808 */
12809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012811 typval_T *argvars;
12812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813{
12814 char_u *pat;
12815 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012816 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817 int save_p_ws = p_ws;
12818 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012819 int flags = 0;
12820
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012821 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012824 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12825 if (dir == 0)
12826 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012827 /*
12828 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
12829 * Check to make sure only those flags are set.
12830 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
12831 * flags cannot be set. Check for that condition also.
12832 */
12833 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
12834 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012836 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012837 goto theend;
12838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012840 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12842 SEARCH_KEEP, RE_SEARCH) != FAIL)
12843 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012844 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012845 if (flags & SP_SETPCMARK)
12846 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847 curwin->w_cursor = pos;
12848 /* "/$" will put the cursor after the end of the line, may need to
12849 * correct that here */
12850 check_cursor();
12851 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012852
12853 /* If 'n' flag is used: restore cursor position. */
12854 if (flags & SP_NOMOVE)
12855 curwin->w_cursor = save_cursor;
12856theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857 p_ws = save_p_ws;
12858}
12859
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860/*
12861 * "searchpair()" function
12862 */
12863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012864f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012865 typval_T *argvars;
12866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867{
12868 char_u *spat, *mpat, *epat;
12869 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871 int dir;
12872 int flags = 0;
12873 char_u nbuf1[NUMBUFLEN];
12874 char_u nbuf2[NUMBUFLEN];
12875 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012880 spat = get_tv_string_chk(&argvars[0]);
12881 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
12882 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
12883 if (spat == NULL || mpat == NULL || epat == NULL)
12884 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886 /* Handle the optional fourth argument: flags */
12887 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012888 if (dir == 0)
12889 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012890 /*
12891 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
12892 */
12893 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
12894 {
12895 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
12896 goto theend;
12897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898
12899 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012900 if (argvars[3].v_type == VAR_UNKNOWN
12901 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 skip = (char_u *)"";
12903 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012904 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
12905 if (skip == NULL)
12906 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012908 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
12909
12910theend:
12911 p_ws = save_p_ws;
12912}
12913
12914/*
12915 * Search for a start/middle/end thing.
12916 * Used by searchpair(), see its documentation for the details.
12917 * Returns 0 or -1 for no match,
12918 */
12919 long
12920do_searchpair(spat, mpat, epat, dir, skip, flags)
12921 char_u *spat; /* start pattern */
12922 char_u *mpat; /* middle pattern */
12923 char_u *epat; /* end pattern */
12924 int dir; /* BACKWARD or FORWARD */
12925 char_u *skip; /* skip expression */
12926 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
12927{
12928 char_u *save_cpo;
12929 char_u *pat, *pat2 = NULL, *pat3 = NULL;
12930 long retval = 0;
12931 pos_T pos;
12932 pos_T firstpos;
12933 pos_T foundpos;
12934 pos_T save_cursor;
12935 pos_T save_pos;
12936 int n;
12937 int r;
12938 int nest = 1;
12939 int err;
12940
12941 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12942 save_cpo = p_cpo;
12943 p_cpo = (char_u *)"";
12944
12945 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12946 * start/middle/end (pat3, for the top pair). */
12947 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12948 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12949 if (pat2 == NULL || pat3 == NULL)
12950 goto theend;
12951 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12952 if (*mpat == NUL)
12953 STRCPY(pat3, pat2);
12954 else
12955 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12956 spat, epat, mpat);
12957
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958 save_cursor = curwin->w_cursor;
12959 pos = curwin->w_cursor;
12960 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012961 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962 pat = pat3;
12963 for (;;)
12964 {
12965 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12966 SEARCH_KEEP, RE_SEARCH);
12967 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12968 /* didn't find it or found the first match again: FAIL */
12969 break;
12970
12971 if (firstpos.lnum == 0)
12972 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012973 if (equalpos(pos, foundpos))
12974 {
12975 /* Found the same position again. Can happen with a pattern that
12976 * has "\zs" at the end and searching backwards. Advance one
12977 * character and try again. */
12978 if (dir == BACKWARD)
12979 decl(&pos);
12980 else
12981 incl(&pos);
12982 }
12983 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984
12985 /* If the skip pattern matches, ignore this match. */
12986 if (*skip != NUL)
12987 {
12988 save_pos = curwin->w_cursor;
12989 curwin->w_cursor = pos;
12990 r = eval_to_bool(skip, &err, NULL, FALSE);
12991 curwin->w_cursor = save_pos;
12992 if (err)
12993 {
12994 /* Evaluating {skip} caused an error, break here. */
12995 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012996 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997 break;
12998 }
12999 if (r)
13000 continue;
13001 }
13002
13003 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13004 {
13005 /* Found end when searching backwards or start when searching
13006 * forward: nested pair. */
13007 ++nest;
13008 pat = pat2; /* nested, don't search for middle */
13009 }
13010 else
13011 {
13012 /* Found end when searching forward or start when searching
13013 * backward: end of (nested) pair; or found middle in outer pair. */
13014 if (--nest == 1)
13015 pat = pat3; /* outer level, search for middle */
13016 }
13017
13018 if (nest == 0)
13019 {
13020 /* Found the match: return matchcount or line number. */
13021 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013022 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013024 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013025 if (flags & SP_SETPCMARK)
13026 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027 curwin->w_cursor = pos;
13028 if (!(flags & SP_REPEAT))
13029 break;
13030 nest = 1; /* search for next unmatched */
13031 }
13032 }
13033
13034 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013035 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036 curwin->w_cursor = save_cursor;
13037
13038theend:
13039 vim_free(pat2);
13040 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013042
13043 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044}
13045
Bram Moolenaar0d660222005-01-07 21:51:51 +000013046/*ARGSUSED*/
13047 static void
13048f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013049 typval_T *argvars;
13050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013052#ifdef FEAT_CLIENTSERVER
13053 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013054 char_u *server = get_tv_string_chk(&argvars[0]);
13055 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056
Bram Moolenaar0d660222005-01-07 21:51:51 +000013057 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013058 if (server == NULL || reply == NULL)
13059 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013060 if (check_restricted() || check_secure())
13061 return;
13062# ifdef FEAT_X11
13063 if (check_connection() == FAIL)
13064 return;
13065# endif
13066
13067 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013069 EMSG(_("E258: Unable to send to client"));
13070 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013072 rettv->vval.v_number = 0;
13073#else
13074 rettv->vval.v_number = -1;
13075#endif
13076}
13077
13078/*ARGSUSED*/
13079 static void
13080f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013081 typval_T *argvars;
13082 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013083{
13084 char_u *r = NULL;
13085
13086#ifdef FEAT_CLIENTSERVER
13087# ifdef WIN32
13088 r = serverGetVimNames();
13089# else
13090 make_connection();
13091 if (X_DISPLAY != NULL)
13092 r = serverGetVimNames(X_DISPLAY);
13093# endif
13094#endif
13095 rettv->v_type = VAR_STRING;
13096 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097}
13098
13099/*
13100 * "setbufvar()" function
13101 */
13102/*ARGSUSED*/
13103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013104f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013105 typval_T *argvars;
13106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107{
13108 buf_T *buf;
13109#ifdef FEAT_AUTOCMD
13110 aco_save_T aco;
13111#else
13112 buf_T *save_curbuf;
13113#endif
13114 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013115 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116 char_u nbuf[NUMBUFLEN];
13117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013118 rettv->vval.v_number = 0;
13119
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120 if (check_restricted() || check_secure())
13121 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013122 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13123 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013124 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125 varp = &argvars[2];
13126
13127 if (buf != NULL && varname != NULL && varp != NULL)
13128 {
13129 /* set curbuf to be our buf, temporarily */
13130#ifdef FEAT_AUTOCMD
13131 aucmd_prepbuf(&aco, buf);
13132#else
13133 save_curbuf = curbuf;
13134 curbuf = buf;
13135#endif
13136
13137 if (*varname == '&')
13138 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013139 long numval;
13140 char_u *strval;
13141 int error = FALSE;
13142
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013144 numval = get_tv_number_chk(varp, &error);
13145 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013146 if (!error && strval != NULL)
13147 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148 }
13149 else
13150 {
13151 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13152 if (bufvarname != NULL)
13153 {
13154 STRCPY(bufvarname, "b:");
13155 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013156 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 vim_free(bufvarname);
13158 }
13159 }
13160
13161 /* reset notion of buffer */
13162#ifdef FEAT_AUTOCMD
13163 aucmd_restbuf(&aco);
13164#else
13165 curbuf = save_curbuf;
13166#endif
13167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168}
13169
13170/*
13171 * "setcmdpos()" function
13172 */
13173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013174f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013175 typval_T *argvars;
13176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013178 int pos = (int)get_tv_number(&argvars[0]) - 1;
13179
13180 if (pos >= 0)
13181 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182}
13183
13184/*
13185 * "setline()" function
13186 */
13187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013188f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013189 typval_T *argvars;
13190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191{
13192 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013193 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013194 list_T *l = NULL;
13195 listitem_T *li = NULL;
13196 long added = 0;
13197 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013199 lnum = get_tv_lnum(&argvars[0]);
13200 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013202 l = argvars[1].vval.v_list;
13203 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013205 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013206 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013207
13208 rettv->vval.v_number = 0; /* OK */
13209 for (;;)
13210 {
13211 if (l != NULL)
13212 {
13213 /* list argument, get next string */
13214 if (li == NULL)
13215 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013216 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013217 li = li->li_next;
13218 }
13219
13220 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013221 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013222 break;
13223 if (lnum <= curbuf->b_ml.ml_line_count)
13224 {
13225 /* existing line, replace it */
13226 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13227 {
13228 changed_bytes(lnum, 0);
13229 check_cursor_col();
13230 rettv->vval.v_number = 0; /* OK */
13231 }
13232 }
13233 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13234 {
13235 /* lnum is one past the last line, append the line */
13236 ++added;
13237 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13238 rettv->vval.v_number = 0; /* OK */
13239 }
13240
13241 if (l == NULL) /* only one string argument */
13242 break;
13243 ++lnum;
13244 }
13245
13246 if (added > 0)
13247 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248}
13249
13250/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013251 * "setqflist()" function
13252 */
13253/*ARGSUSED*/
13254 static void
13255f_setqflist(argvars, rettv)
13256 typval_T *argvars;
13257 typval_T *rettv;
13258{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013259 char_u *act;
13260 int action = ' ';
13261
Bram Moolenaar2641f772005-03-25 21:58:17 +000013262 rettv->vval.v_number = -1;
13263
13264#ifdef FEAT_QUICKFIX
13265 if (argvars[0].v_type != VAR_LIST)
13266 EMSG(_(e_listreq));
13267 else
13268 {
13269 list_T *l = argvars[0].vval.v_list;
13270
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013271 if (argvars[1].v_type == VAR_STRING)
13272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013273 act = get_tv_string_chk(&argvars[1]);
13274 if (act == NULL)
13275 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013276 if (*act == 'a' || *act == 'r')
13277 action = *act;
13278 }
13279
13280 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013281 rettv->vval.v_number = 0;
13282 }
13283#endif
13284}
13285
13286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 * "setreg()" function
13288 */
13289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013290f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013291 typval_T *argvars;
13292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293{
13294 int regname;
13295 char_u *strregname;
13296 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013297 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 int append;
13299 char_u yank_type;
13300 long block_len;
13301
13302 block_len = -1;
13303 yank_type = MAUTO;
13304 append = FALSE;
13305
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013306 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013307 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013309 if (strregname == NULL)
13310 return; /* type error; errmsg already given */
13311 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312 if (regname == 0 || regname == '@')
13313 regname = '"';
13314 else if (regname == '=')
13315 return;
13316
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013317 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013319 stropt = get_tv_string_chk(&argvars[2]);
13320 if (stropt == NULL)
13321 return; /* type error */
13322 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323 switch (*stropt)
13324 {
13325 case 'a': case 'A': /* append */
13326 append = TRUE;
13327 break;
13328 case 'v': case 'c': /* character-wise selection */
13329 yank_type = MCHAR;
13330 break;
13331 case 'V': case 'l': /* line-wise selection */
13332 yank_type = MLINE;
13333 break;
13334#ifdef FEAT_VISUAL
13335 case 'b': case Ctrl_V: /* block-wise selection */
13336 yank_type = MBLOCK;
13337 if (VIM_ISDIGIT(stropt[1]))
13338 {
13339 ++stropt;
13340 block_len = getdigits(&stropt) - 1;
13341 --stropt;
13342 }
13343 break;
13344#endif
13345 }
13346 }
13347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013348 strval = get_tv_string_chk(&argvars[1]);
13349 if (strval != NULL)
13350 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013352 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353}
13354
13355
13356/*
13357 * "setwinvar(expr)" function
13358 */
13359/*ARGSUSED*/
13360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013361f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013362 typval_T *argvars;
13363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364{
13365 win_T *win;
13366#ifdef FEAT_WINDOWS
13367 win_T *save_curwin;
13368#endif
13369 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013370 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371 char_u nbuf[NUMBUFLEN];
13372
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013373 rettv->vval.v_number = 0;
13374
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375 if (check_restricted() || check_secure())
13376 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013378 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 varp = &argvars[2];
13380
13381 if (win != NULL && varname != NULL && varp != NULL)
13382 {
13383#ifdef FEAT_WINDOWS
13384 /* set curwin to be our win, temporarily */
13385 save_curwin = curwin;
13386 curwin = win;
13387 curbuf = curwin->w_buffer;
13388#endif
13389
13390 if (*varname == '&')
13391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013392 long numval;
13393 char_u *strval;
13394 int error = FALSE;
13395
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013397 numval = get_tv_number_chk(varp, &error);
13398 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013399 if (!error && strval != NULL)
13400 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401 }
13402 else
13403 {
13404 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13405 if (winvarname != NULL)
13406 {
13407 STRCPY(winvarname, "w:");
13408 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013409 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410 vim_free(winvarname);
13411 }
13412 }
13413
13414#ifdef FEAT_WINDOWS
13415 /* Restore current window, if it's still valid (autocomands can make
13416 * it invalid). */
13417 if (win_valid(save_curwin))
13418 {
13419 curwin = save_curwin;
13420 curbuf = curwin->w_buffer;
13421 }
13422#endif
13423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424}
13425
13426/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013427 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428 */
13429 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013430f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013431 typval_T *argvars;
13432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013434 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435
Bram Moolenaar0d660222005-01-07 21:51:51 +000013436 p = get_tv_string(&argvars[0]);
13437 rettv->vval.v_string = vim_strsave(p);
13438 simplify_filename(rettv->vval.v_string); /* simplify in place */
13439 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440}
13441
Bram Moolenaar0d660222005-01-07 21:51:51 +000013442static int
13443#ifdef __BORLANDC__
13444 _RTLENTRYF
13445#endif
13446 item_compare __ARGS((const void *s1, const void *s2));
13447static int
13448#ifdef __BORLANDC__
13449 _RTLENTRYF
13450#endif
13451 item_compare2 __ARGS((const void *s1, const void *s2));
13452
13453static int item_compare_ic;
13454static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013455static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013456#define ITEM_COMPARE_FAIL 999
13457
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013459 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013461 static int
13462#ifdef __BORLANDC__
13463_RTLENTRYF
13464#endif
13465item_compare(s1, s2)
13466 const void *s1;
13467 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013469 char_u *p1, *p2;
13470 char_u *tofree1, *tofree2;
13471 int res;
13472 char_u numbuf1[NUMBUFLEN];
13473 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474
Bram Moolenaar33570922005-01-25 22:26:29 +000013475 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13476 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013477 if (item_compare_ic)
13478 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013480 res = STRCMP(p1, p2);
13481 vim_free(tofree1);
13482 vim_free(tofree2);
13483 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484}
13485
13486 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013487#ifdef __BORLANDC__
13488_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013490item_compare2(s1, s2)
13491 const void *s1;
13492 const void *s2;
13493{
13494 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013495 typval_T rettv;
13496 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013497 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013499 /* shortcut after failure in previous call; compare all items equal */
13500 if (item_compare_func_err)
13501 return 0;
13502
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013503 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13504 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013505 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13506 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013507
13508 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13509 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013510 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013511 clear_tv(&argv[0]);
13512 clear_tv(&argv[1]);
13513
13514 if (res == FAIL)
13515 res = ITEM_COMPARE_FAIL;
13516 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013517 /* return value has wrong type */
13518 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13519 if (item_compare_func_err)
13520 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013521 clear_tv(&rettv);
13522 return res;
13523}
13524
13525/*
13526 * "sort({list})" function
13527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013529f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013530 typval_T *argvars;
13531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532{
Bram Moolenaar33570922005-01-25 22:26:29 +000013533 list_T *l;
13534 listitem_T *li;
13535 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013536 long len;
13537 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538
Bram Moolenaar0d660222005-01-07 21:51:51 +000013539 rettv->vval.v_number = 0;
13540 if (argvars[0].v_type != VAR_LIST)
13541 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542 else
13543 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013544 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013545 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013546 return;
13547 rettv->vval.v_list = l;
13548 rettv->v_type = VAR_LIST;
13549 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550
Bram Moolenaar0d660222005-01-07 21:51:51 +000013551 len = list_len(l);
13552 if (len <= 1)
13553 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554
Bram Moolenaar0d660222005-01-07 21:51:51 +000013555 item_compare_ic = FALSE;
13556 item_compare_func = NULL;
13557 if (argvars[1].v_type != VAR_UNKNOWN)
13558 {
13559 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013560 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013561 else
13562 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013563 int error = FALSE;
13564
13565 i = get_tv_number_chk(&argvars[1], &error);
13566 if (error)
13567 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013568 if (i == 1)
13569 item_compare_ic = TRUE;
13570 else
13571 item_compare_func = get_tv_string(&argvars[1]);
13572 }
13573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574
Bram Moolenaar0d660222005-01-07 21:51:51 +000013575 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013576 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013577 if (ptrs == NULL)
13578 return;
13579 i = 0;
13580 for (li = l->lv_first; li != NULL; li = li->li_next)
13581 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013583 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013584 /* test the compare function */
13585 if (item_compare_func != NULL
13586 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13587 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013588 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013590 {
13591 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013592 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013593 item_compare_func == NULL ? item_compare : item_compare2);
13594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013595 if (!item_compare_func_err)
13596 {
13597 /* Clear the List and append the items in the sorted order. */
13598 l->lv_first = l->lv_last = NULL;
13599 l->lv_len = 0;
13600 for (i = 0; i < len; ++i)
13601 list_append(l, ptrs[i]);
13602 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013603 }
13604
13605 vim_free(ptrs);
13606 }
13607}
13608
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013609/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013610 * "soundfold({word})" function
13611 */
13612 static void
13613f_soundfold(argvars, rettv)
13614 typval_T *argvars;
13615 typval_T *rettv;
13616{
13617 char_u *s;
13618
13619 rettv->v_type = VAR_STRING;
13620 s = get_tv_string(&argvars[0]);
13621#ifdef FEAT_SYN_HL
13622 rettv->vval.v_string = eval_soundfold(s);
13623#else
13624 rettv->vval.v_string = vim_strsave(s);
13625#endif
13626}
13627
13628/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013629 * "spellbadword()" function
13630 */
13631/* ARGSUSED */
13632 static void
13633f_spellbadword(argvars, rettv)
13634 typval_T *argvars;
13635 typval_T *rettv;
13636{
13637 int attr;
13638 char_u *ptr;
13639 int len;
13640
13641 rettv->vval.v_string = NULL;
13642 rettv->v_type = VAR_STRING;
13643
13644#ifdef FEAT_SYN_HL
13645 /* Find the start of the badly spelled word. */
13646 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
13647 return;
13648
13649 /* Get the length of the word and copy it. */
13650 ptr = ml_get_cursor();
Bram Moolenaar51ac12f2005-07-02 23:21:11 +000013651 len = spell_check(curwin, ptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013652 rettv->vval.v_string = vim_strnsave(ptr, len);
13653#endif
13654}
13655
13656/*
13657 * "spellsuggest()" function
13658 */
13659 static void
13660f_spellsuggest(argvars, rettv)
13661 typval_T *argvars;
13662 typval_T *rettv;
13663{
13664 char_u *str;
13665 int maxcount;
13666 garray_T ga;
13667 list_T *l;
13668 listitem_T *li;
13669 int i;
13670
13671 l = list_alloc();
13672 if (l == NULL)
13673 return;
13674 rettv->v_type = VAR_LIST;
13675 rettv->vval.v_list = l;
13676 ++l->lv_refcount;
13677
13678#ifdef FEAT_SYN_HL
13679 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13680 {
13681 str = get_tv_string(&argvars[0]);
13682 if (argvars[1].v_type != VAR_UNKNOWN)
13683 {
13684 maxcount = get_tv_number(&argvars[1]);
13685 if (maxcount <= 0)
13686 return;
13687 }
13688 else
13689 maxcount = 25;
13690
13691 spell_suggest_list(&ga, str, maxcount);
13692
13693 for (i = 0; i < ga.ga_len; ++i)
13694 {
13695 str = ((char_u **)ga.ga_data)[i];
13696
13697 li = listitem_alloc();
13698 if (li == NULL)
13699 vim_free(str);
13700 else
13701 {
13702 li->li_tv.v_type = VAR_STRING;
13703 li->li_tv.v_lock = 0;
13704 li->li_tv.vval.v_string = str;
13705 list_append(l, li);
13706 }
13707 }
13708 ga_clear(&ga);
13709 }
13710#endif
13711}
13712
Bram Moolenaar0d660222005-01-07 21:51:51 +000013713 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013714f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013715 typval_T *argvars;
13716 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013717{
13718 char_u *str;
13719 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013720 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013721 regmatch_T regmatch;
13722 char_u patbuf[NUMBUFLEN];
13723 char_u *save_cpo;
13724 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013725 listitem_T *ni;
13726 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013727 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013728 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013729 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013730
13731 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13732 save_cpo = p_cpo;
13733 p_cpo = (char_u *)"";
13734
13735 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013736 if (argvars[1].v_type != VAR_UNKNOWN)
13737 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013738 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13739 if (pat == NULL)
13740 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013741 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013742 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013743 }
13744 if (pat == NULL || *pat == NUL)
13745 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013746
13747 l = list_alloc();
13748 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013750 rettv->v_type = VAR_LIST;
13751 rettv->vval.v_list = l;
13752 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013753 if (typeerr)
13754 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755
Bram Moolenaar0d660222005-01-07 21:51:51 +000013756 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13757 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013759 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013760 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013761 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013762 if (*str == NUL)
13763 match = FALSE; /* empty item at the end */
13764 else
13765 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013766 if (match)
13767 end = regmatch.startp[0];
13768 else
13769 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013770 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13771 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013772 {
13773 ni = listitem_alloc();
13774 if (ni == NULL)
13775 break;
13776 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013777 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013778 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13779 list_append(l, ni);
13780 }
13781 if (!match)
13782 break;
13783 /* Advance to just after the match. */
13784 if (regmatch.endp[0] > str)
13785 col = 0;
13786 else
13787 {
13788 /* Don't get stuck at the same match. */
13789#ifdef FEAT_MBYTE
13790 col = mb_ptr2len_check(regmatch.endp[0]);
13791#else
13792 col = 1;
13793#endif
13794 }
13795 str = regmatch.endp[0];
13796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797
Bram Moolenaar0d660222005-01-07 21:51:51 +000013798 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800
Bram Moolenaar0d660222005-01-07 21:51:51 +000013801 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802}
13803
13804#ifdef HAVE_STRFTIME
13805/*
13806 * "strftime({format}[, {time}])" function
13807 */
13808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013809f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013810 typval_T *argvars;
13811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812{
13813 char_u result_buf[256];
13814 struct tm *curtime;
13815 time_t seconds;
13816 char_u *p;
13817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013818 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013820 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013821 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822 seconds = time(NULL);
13823 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013824 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 curtime = localtime(&seconds);
13826 /* MSVC returns NULL for an invalid value of seconds. */
13827 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013828 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 else
13830 {
13831# ifdef FEAT_MBYTE
13832 vimconv_T conv;
13833 char_u *enc;
13834
13835 conv.vc_type = CONV_NONE;
13836 enc = enc_locale();
13837 convert_setup(&conv, p_enc, enc);
13838 if (conv.vc_type != CONV_NONE)
13839 p = string_convert(&conv, p, NULL);
13840# endif
13841 if (p != NULL)
13842 (void)strftime((char *)result_buf, sizeof(result_buf),
13843 (char *)p, curtime);
13844 else
13845 result_buf[0] = NUL;
13846
13847# ifdef FEAT_MBYTE
13848 if (conv.vc_type != CONV_NONE)
13849 vim_free(p);
13850 convert_setup(&conv, enc, p_enc);
13851 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013852 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 else
13854# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013855 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856
13857# ifdef FEAT_MBYTE
13858 /* Release conversion descriptors */
13859 convert_setup(&conv, NULL, NULL);
13860 vim_free(enc);
13861# endif
13862 }
13863}
13864#endif
13865
13866/*
13867 * "stridx()" function
13868 */
13869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013870f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013871 typval_T *argvars;
13872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873{
13874 char_u buf[NUMBUFLEN];
13875 char_u *needle;
13876 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000013877 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000013879 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013881 needle = get_tv_string_chk(&argvars[1]);
13882 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000013883 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013884 if (needle == NULL || haystack == NULL)
13885 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886
Bram Moolenaar33570922005-01-25 22:26:29 +000013887 if (argvars[2].v_type != VAR_UNKNOWN)
13888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013889 int error = FALSE;
13890
13891 start_idx = get_tv_number_chk(&argvars[2], &error);
13892 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000013893 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013894 if (start_idx >= 0)
13895 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000013896 }
13897
13898 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13899 if (pos != NULL)
13900 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901}
13902
13903/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013904 * "string()" function
13905 */
13906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013907f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013908 typval_T *argvars;
13909 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013910{
13911 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013912 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013913
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013914 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013915 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013916 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013917 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918}
13919
13920/*
13921 * "strlen()" function
13922 */
13923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013924f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013925 typval_T *argvars;
13926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013928 rettv->vval.v_number = (varnumber_T)(STRLEN(
13929 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930}
13931
13932/*
13933 * "strpart()" function
13934 */
13935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013936f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013937 typval_T *argvars;
13938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939{
13940 char_u *p;
13941 int n;
13942 int len;
13943 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013944 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013946 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 slen = (int)STRLEN(p);
13948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013949 n = get_tv_number_chk(&argvars[1], &error);
13950 if (error)
13951 len = 0;
13952 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013953 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 else
13955 len = slen - n; /* default len: all bytes that are available. */
13956
13957 /*
13958 * Only return the overlap between the specified part and the actual
13959 * string.
13960 */
13961 if (n < 0)
13962 {
13963 len += n;
13964 n = 0;
13965 }
13966 else if (n > slen)
13967 n = slen;
13968 if (len < 0)
13969 len = 0;
13970 else if (n + len > slen)
13971 len = slen - n;
13972
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013973 rettv->v_type = VAR_STRING;
13974 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975}
13976
13977/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013978 * "strridx()" function
13979 */
13980 static void
13981f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013982 typval_T *argvars;
13983 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013984{
13985 char_u buf[NUMBUFLEN];
13986 char_u *needle;
13987 char_u *haystack;
13988 char_u *rest;
13989 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013990 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013992 needle = get_tv_string_chk(&argvars[1]);
13993 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013994 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013995
13996 rettv->vval.v_number = -1;
13997 if (needle == NULL || haystack == NULL)
13998 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013999 if (argvars[2].v_type != VAR_UNKNOWN)
14000 {
14001 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014002 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014003 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014004 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014005 }
14006 else
14007 end_idx = haystack_len;
14008
Bram Moolenaar0d660222005-01-07 21:51:51 +000014009 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014010 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014011 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014012 lastmatch = haystack + end_idx;
14013 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014014 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014015 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014016 for (rest = haystack; *rest != '\0'; ++rest)
14017 {
14018 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014019 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014020 break;
14021 lastmatch = rest;
14022 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014023 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014024
14025 if (lastmatch == NULL)
14026 rettv->vval.v_number = -1;
14027 else
14028 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14029}
14030
14031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032 * "strtrans()" function
14033 */
14034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014035f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014036 typval_T *argvars;
14037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014039 rettv->v_type = VAR_STRING;
14040 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041}
14042
14043/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014044 * "submatch()" function
14045 */
14046 static void
14047f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014048 typval_T *argvars;
14049 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014050{
14051 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014052 rettv->vval.v_string =
14053 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014054}
14055
14056/*
14057 * "substitute()" function
14058 */
14059 static void
14060f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014061 typval_T *argvars;
14062 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014063{
14064 char_u patbuf[NUMBUFLEN];
14065 char_u subbuf[NUMBUFLEN];
14066 char_u flagsbuf[NUMBUFLEN];
14067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014068 char_u *str = get_tv_string_chk(&argvars[0]);
14069 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14070 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14071 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14072
Bram Moolenaar0d660222005-01-07 21:51:51 +000014073 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014074 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14075 rettv->vval.v_string = NULL;
14076 else
14077 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014078}
14079
14080/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014081 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014082 */
14083/*ARGSUSED*/
14084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014085f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014086 typval_T *argvars;
14087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088{
14089 int id = 0;
14090#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014091 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092 long col;
14093 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014094 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014096 lnum = get_tv_lnum(argvars); /* -1 on type error */
14097 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14098 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014100 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014101 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
14102 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103#endif
14104
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014105 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106}
14107
14108/*
14109 * "synIDattr(id, what [, mode])" function
14110 */
14111/*ARGSUSED*/
14112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014113f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014114 typval_T *argvars;
14115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116{
14117 char_u *p = NULL;
14118#ifdef FEAT_SYN_HL
14119 int id;
14120 char_u *what;
14121 char_u *mode;
14122 char_u modebuf[NUMBUFLEN];
14123 int modec;
14124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014125 id = get_tv_number(&argvars[0]);
14126 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014127 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014129 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130 modec = TOLOWER_ASC(mode[0]);
14131 if (modec != 't' && modec != 'c'
14132#ifdef FEAT_GUI
14133 && modec != 'g'
14134#endif
14135 )
14136 modec = 0; /* replace invalid with current */
14137 }
14138 else
14139 {
14140#ifdef FEAT_GUI
14141 if (gui.in_use)
14142 modec = 'g';
14143 else
14144#endif
14145 if (t_colors > 1)
14146 modec = 'c';
14147 else
14148 modec = 't';
14149 }
14150
14151
14152 switch (TOLOWER_ASC(what[0]))
14153 {
14154 case 'b':
14155 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14156 p = highlight_color(id, what, modec);
14157 else /* bold */
14158 p = highlight_has_attr(id, HL_BOLD, modec);
14159 break;
14160
14161 case 'f': /* fg[#] */
14162 p = highlight_color(id, what, modec);
14163 break;
14164
14165 case 'i':
14166 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14167 p = highlight_has_attr(id, HL_INVERSE, modec);
14168 else /* italic */
14169 p = highlight_has_attr(id, HL_ITALIC, modec);
14170 break;
14171
14172 case 'n': /* name */
14173 p = get_highlight_name(NULL, id - 1);
14174 break;
14175
14176 case 'r': /* reverse */
14177 p = highlight_has_attr(id, HL_INVERSE, modec);
14178 break;
14179
14180 case 's': /* standout */
14181 p = highlight_has_attr(id, HL_STANDOUT, modec);
14182 break;
14183
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014184 case 'u':
14185 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14186 /* underline */
14187 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14188 else
14189 /* undercurl */
14190 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191 break;
14192 }
14193
14194 if (p != NULL)
14195 p = vim_strsave(p);
14196#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014197 rettv->v_type = VAR_STRING;
14198 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199}
14200
14201/*
14202 * "synIDtrans(id)" function
14203 */
14204/*ARGSUSED*/
14205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014206f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014207 typval_T *argvars;
14208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014209{
14210 int id;
14211
14212#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014213 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214
14215 if (id > 0)
14216 id = syn_get_final_id(id);
14217 else
14218#endif
14219 id = 0;
14220
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014221 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222}
14223
14224/*
14225 * "system()" function
14226 */
14227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014228f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014229 typval_T *argvars;
14230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014232 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014234 char_u *infile = NULL;
14235 char_u buf[NUMBUFLEN];
14236 int err = FALSE;
14237 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014239 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014240 {
14241 /*
14242 * Write the string to a temp file, to be used for input of the shell
14243 * command.
14244 */
14245 if ((infile = vim_tempname('i')) == NULL)
14246 {
14247 EMSG(_(e_notmp));
14248 return;
14249 }
14250
14251 fd = mch_fopen((char *)infile, WRITEBIN);
14252 if (fd == NULL)
14253 {
14254 EMSG2(_(e_notopen), infile);
14255 goto done;
14256 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014257 p = get_tv_string_buf_chk(&argvars[1], buf);
14258 if (p == NULL)
14259 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014260 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14261 err = TRUE;
14262 if (fclose(fd) != 0)
14263 err = TRUE;
14264 if (err)
14265 {
14266 EMSG(_("E677: Error writing temp file"));
14267 goto done;
14268 }
14269 }
14270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014271 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014272
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273#ifdef USE_CR
14274 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014275 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276 {
14277 char_u *s;
14278
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014279 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 {
14281 if (*s == CAR)
14282 *s = NL;
14283 }
14284 }
14285#else
14286# ifdef USE_CRNL
14287 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014288 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289 {
14290 char_u *s, *d;
14291
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014292 d = res;
14293 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294 {
14295 if (s[0] == CAR && s[1] == NL)
14296 ++s;
14297 *d++ = *s;
14298 }
14299 *d = NUL;
14300 }
14301# endif
14302#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014303
14304done:
14305 if (infile != NULL)
14306 {
14307 mch_remove(infile);
14308 vim_free(infile);
14309 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014310 rettv->v_type = VAR_STRING;
14311 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312}
14313
14314/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014315 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014316 */
14317 static void
14318f_taglist(argvars, rettv)
14319 typval_T *argvars;
14320 typval_T *rettv;
14321{
14322 char_u *tag_pattern;
14323 list_T *l;
14324
14325 tag_pattern = get_tv_string(&argvars[0]);
14326
14327 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014328 if (*tag_pattern == NUL)
14329 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014330
14331 l = list_alloc();
14332 if (l != NULL)
14333 {
14334 if (get_tags(l, tag_pattern) != FAIL)
14335 {
14336 rettv->vval.v_list = l;
14337 rettv->v_type = VAR_LIST;
14338 ++l->lv_refcount;
14339 }
14340 else
14341 list_free(l);
14342 }
14343}
14344
14345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 * "tempname()" function
14347 */
14348/*ARGSUSED*/
14349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014350f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014351 typval_T *argvars;
14352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353{
14354 static int x = 'A';
14355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014356 rettv->v_type = VAR_STRING;
14357 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358
14359 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14360 * names. Skip 'I' and 'O', they are used for shell redirection. */
14361 do
14362 {
14363 if (x == 'Z')
14364 x = '0';
14365 else if (x == '9')
14366 x = 'A';
14367 else
14368 {
14369#ifdef EBCDIC
14370 if (x == 'I')
14371 x = 'J';
14372 else if (x == 'R')
14373 x = 'S';
14374 else
14375#endif
14376 ++x;
14377 }
14378 } while (x == 'I' || x == 'O');
14379}
14380
14381/*
14382 * "tolower(string)" function
14383 */
14384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014385f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014386 typval_T *argvars;
14387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388{
14389 char_u *p;
14390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014391 p = vim_strsave(get_tv_string(&argvars[0]));
14392 rettv->v_type = VAR_STRING;
14393 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394
14395 if (p != NULL)
14396 while (*p != NUL)
14397 {
14398#ifdef FEAT_MBYTE
14399 int l;
14400
14401 if (enc_utf8)
14402 {
14403 int c, lc;
14404
14405 c = utf_ptr2char(p);
14406 lc = utf_tolower(c);
14407 l = utf_ptr2len_check(p);
14408 /* TODO: reallocate string when byte count changes. */
14409 if (utf_char2len(lc) == l)
14410 utf_char2bytes(lc, p);
14411 p += l;
14412 }
14413 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
14414 p += l; /* skip multi-byte character */
14415 else
14416#endif
14417 {
14418 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14419 ++p;
14420 }
14421 }
14422}
14423
14424/*
14425 * "toupper(string)" function
14426 */
14427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014428f_toupper(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 Moolenaarc70646c2005-01-04 21:52:38 +000014432 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014433 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434}
14435
14436/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014437 * "tr(string, fromstr, tostr)" function
14438 */
14439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014440f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014441 typval_T *argvars;
14442 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014443{
14444 char_u *instr;
14445 char_u *fromstr;
14446 char_u *tostr;
14447 char_u *p;
14448#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014449 int inlen;
14450 int fromlen;
14451 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014452 int idx;
14453 char_u *cpstr;
14454 int cplen;
14455 int first = TRUE;
14456#endif
14457 char_u buf[NUMBUFLEN];
14458 char_u buf2[NUMBUFLEN];
14459 garray_T ga;
14460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014462 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14463 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014464
14465 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014466 rettv->v_type = VAR_STRING;
14467 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014468 if (fromstr == NULL || tostr == NULL)
14469 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014470 ga_init2(&ga, (int)sizeof(char), 80);
14471
14472#ifdef FEAT_MBYTE
14473 if (!has_mbyte)
14474#endif
14475 /* not multi-byte: fromstr and tostr must be the same length */
14476 if (STRLEN(fromstr) != STRLEN(tostr))
14477 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014478#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014479error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014480#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014481 EMSG2(_(e_invarg2), fromstr);
14482 ga_clear(&ga);
14483 return;
14484 }
14485
14486 /* fromstr and tostr have to contain the same number of chars */
14487 while (*instr != NUL)
14488 {
14489#ifdef FEAT_MBYTE
14490 if (has_mbyte)
14491 {
14492 inlen = mb_ptr2len_check(instr);
14493 cpstr = instr;
14494 cplen = inlen;
14495 idx = 0;
14496 for (p = fromstr; *p != NUL; p += fromlen)
14497 {
14498 fromlen = mb_ptr2len_check(p);
14499 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14500 {
14501 for (p = tostr; *p != NUL; p += tolen)
14502 {
14503 tolen = mb_ptr2len_check(p);
14504 if (idx-- == 0)
14505 {
14506 cplen = tolen;
14507 cpstr = p;
14508 break;
14509 }
14510 }
14511 if (*p == NUL) /* tostr is shorter than fromstr */
14512 goto error;
14513 break;
14514 }
14515 ++idx;
14516 }
14517
14518 if (first && cpstr == instr)
14519 {
14520 /* Check that fromstr and tostr have the same number of
14521 * (multi-byte) characters. Done only once when a character
14522 * of instr doesn't appear in fromstr. */
14523 first = FALSE;
14524 for (p = tostr; *p != NUL; p += tolen)
14525 {
14526 tolen = mb_ptr2len_check(p);
14527 --idx;
14528 }
14529 if (idx != 0)
14530 goto error;
14531 }
14532
14533 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014534 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014535 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014536
14537 instr += inlen;
14538 }
14539 else
14540#endif
14541 {
14542 /* When not using multi-byte chars we can do it faster. */
14543 p = vim_strchr(fromstr, *instr);
14544 if (p != NULL)
14545 ga_append(&ga, tostr[p - fromstr]);
14546 else
14547 ga_append(&ga, *instr);
14548 ++instr;
14549 }
14550 }
14551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014552 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014553}
14554
14555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556 * "type(expr)" function
14557 */
14558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014559f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014560 typval_T *argvars;
14561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014563 int n;
14564
14565 switch (argvars[0].v_type)
14566 {
14567 case VAR_NUMBER: n = 0; break;
14568 case VAR_STRING: n = 1; break;
14569 case VAR_FUNC: n = 2; break;
14570 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014571 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014572 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14573 }
14574 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575}
14576
14577/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014578 * "values(dict)" function
14579 */
14580 static void
14581f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014582 typval_T *argvars;
14583 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014584{
14585 dict_list(argvars, rettv, 1);
14586}
14587
14588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589 * "virtcol(string)" function
14590 */
14591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014592f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014593 typval_T *argvars;
14594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595{
14596 colnr_T vcol = 0;
14597 pos_T *fp;
14598
14599 fp = var2fpos(&argvars[0], FALSE);
14600 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14601 {
14602 getvvcol(curwin, fp, NULL, NULL, &vcol);
14603 ++vcol;
14604 }
14605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014606 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607}
14608
14609/*
14610 * "visualmode()" function
14611 */
14612/*ARGSUSED*/
14613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014614f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014615 typval_T *argvars;
14616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617{
14618#ifdef FEAT_VISUAL
14619 char_u str[2];
14620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014621 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 str[0] = curbuf->b_visual_mode_eval;
14623 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014624 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625
14626 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014627 if ((argvars[0].v_type == VAR_NUMBER
14628 && argvars[0].vval.v_number != 0)
14629 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014630 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631 curbuf->b_visual_mode_eval = NUL;
14632#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014633 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634#endif
14635}
14636
14637/*
14638 * "winbufnr(nr)" function
14639 */
14640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014641f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014642 typval_T *argvars;
14643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644{
14645 win_T *wp;
14646
14647 wp = find_win_by_nr(&argvars[0]);
14648 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014649 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014651 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652}
14653
14654/*
14655 * "wincol()" function
14656 */
14657/*ARGSUSED*/
14658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014659f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014660 typval_T *argvars;
14661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662{
14663 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014664 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665}
14666
14667/*
14668 * "winheight(nr)" function
14669 */
14670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014671f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014672 typval_T *argvars;
14673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674{
14675 win_T *wp;
14676
14677 wp = find_win_by_nr(&argvars[0]);
14678 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014679 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014681 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682}
14683
14684/*
14685 * "winline()" function
14686 */
14687/*ARGSUSED*/
14688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014689f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014690 typval_T *argvars;
14691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014692{
14693 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014694 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695}
14696
14697/*
14698 * "winnr()" function
14699 */
14700/* ARGSUSED */
14701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014702f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014703 typval_T *argvars;
14704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014705{
14706 int nr = 1;
14707#ifdef FEAT_WINDOWS
14708 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014709 win_T *twin = curwin;
14710 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014712 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014713 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014714 arg = get_tv_string_chk(&argvars[0]);
14715 if (arg == NULL)
14716 nr = 0; /* type error; errmsg already given */
14717 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014718 twin = lastwin;
14719 else if (STRCMP(arg, "#") == 0)
14720 {
14721 twin = prevwin;
14722 if (prevwin == NULL)
14723 nr = 0;
14724 }
14725 else
14726 {
14727 EMSG2(_(e_invexpr2), arg);
14728 nr = 0;
14729 }
14730 }
14731
14732 if (nr > 0)
14733 for (wp = firstwin; wp != twin; wp = wp->w_next)
14734 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014736 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737}
14738
14739/*
14740 * "winrestcmd()" function
14741 */
14742/* ARGSUSED */
14743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014744f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014745 typval_T *argvars;
14746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747{
14748#ifdef FEAT_WINDOWS
14749 win_T *wp;
14750 int winnr = 1;
14751 garray_T ga;
14752 char_u buf[50];
14753
14754 ga_init2(&ga, (int)sizeof(char), 70);
14755 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14756 {
14757 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14758 ga_concat(&ga, buf);
14759# ifdef FEAT_VERTSPLIT
14760 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14761 ga_concat(&ga, buf);
14762# endif
14763 ++winnr;
14764 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014765 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014767 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014769 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014771 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014772}
14773
14774/*
14775 * "winwidth(nr)" function
14776 */
14777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014778f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014779 typval_T *argvars;
14780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781{
14782 win_T *wp;
14783
14784 wp = find_win_by_nr(&argvars[0]);
14785 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014786 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787 else
14788#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014789 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014791 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792#endif
14793}
14794
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014796 * "writefile()" function
14797 */
14798 static void
14799f_writefile(argvars, rettv)
14800 typval_T *argvars;
14801 typval_T *rettv;
14802{
14803 int binary = FALSE;
14804 char_u *fname;
14805 FILE *fd;
14806 listitem_T *li;
14807 char_u *s;
14808 int ret = 0;
14809 int c;
14810
14811 if (argvars[0].v_type != VAR_LIST)
14812 {
14813 EMSG2(_(e_listarg), "writefile()");
14814 return;
14815 }
14816 if (argvars[0].vval.v_list == NULL)
14817 return;
14818
14819 if (argvars[2].v_type != VAR_UNKNOWN
14820 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
14821 binary = TRUE;
14822
14823 /* Always open the file in binary mode, library functions have a mind of
14824 * their own about CR-LF conversion. */
14825 fname = get_tv_string(&argvars[1]);
14826 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
14827 {
14828 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14829 ret = -1;
14830 }
14831 else
14832 {
14833 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
14834 li = li->li_next)
14835 {
14836 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
14837 {
14838 if (*s == '\n')
14839 c = putc(NUL, fd);
14840 else
14841 c = putc(*s, fd);
14842 if (c == EOF)
14843 {
14844 ret = -1;
14845 break;
14846 }
14847 }
14848 if (!binary || li->li_next != NULL)
14849 if (putc('\n', fd) == EOF)
14850 {
14851 ret = -1;
14852 break;
14853 }
14854 if (ret < 0)
14855 {
14856 EMSG(_(e_write));
14857 break;
14858 }
14859 }
14860 fclose(fd);
14861 }
14862
14863 rettv->vval.v_number = ret;
14864}
14865
14866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867 * Translate a String variable into a position.
14868 */
14869 static pos_T *
14870var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000014871 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872 int lnum; /* TRUE when $ is last line */
14873{
14874 char_u *name;
14875 static pos_T pos;
14876 pos_T *pp;
14877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014878 name = get_tv_string_chk(varp);
14879 if (name == NULL)
14880 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881 if (name[0] == '.') /* cursor */
14882 return &curwin->w_cursor;
14883 if (name[0] == '\'') /* mark */
14884 {
14885 pp = getmark(name[1], FALSE);
14886 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
14887 return NULL;
14888 return pp;
14889 }
14890 if (name[0] == '$') /* last column or line */
14891 {
14892 if (lnum)
14893 {
14894 pos.lnum = curbuf->b_ml.ml_line_count;
14895 pos.col = 0;
14896 }
14897 else
14898 {
14899 pos.lnum = curwin->w_cursor.lnum;
14900 pos.col = (colnr_T)STRLEN(ml_get_curline());
14901 }
14902 return &pos;
14903 }
14904 return NULL;
14905}
14906
14907/*
14908 * Get the length of an environment variable name.
14909 * Advance "arg" to the first character after the name.
14910 * Return 0 for error.
14911 */
14912 static int
14913get_env_len(arg)
14914 char_u **arg;
14915{
14916 char_u *p;
14917 int len;
14918
14919 for (p = *arg; vim_isIDc(*p); ++p)
14920 ;
14921 if (p == *arg) /* no name found */
14922 return 0;
14923
14924 len = (int)(p - *arg);
14925 *arg = p;
14926 return len;
14927}
14928
14929/*
14930 * Get the length of the name of a function or internal variable.
14931 * "arg" is advanced to the first non-white character after the name.
14932 * Return 0 if something is wrong.
14933 */
14934 static int
14935get_id_len(arg)
14936 char_u **arg;
14937{
14938 char_u *p;
14939 int len;
14940
14941 /* Find the end of the name. */
14942 for (p = *arg; eval_isnamec(*p); ++p)
14943 ;
14944 if (p == *arg) /* no name found */
14945 return 0;
14946
14947 len = (int)(p - *arg);
14948 *arg = skipwhite(p);
14949
14950 return len;
14951}
14952
14953/*
Bram Moolenaara7043832005-01-21 11:56:39 +000014954 * Get the length of the name of a variable or function.
14955 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014957 * Return -1 if curly braces expansion failed.
14958 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959 * If the name contains 'magic' {}'s, expand them and return the
14960 * expanded name in an allocated string via 'alias' - caller must free.
14961 */
14962 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014963get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964 char_u **arg;
14965 char_u **alias;
14966 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014967 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968{
14969 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970 char_u *p;
14971 char_u *expr_start;
14972 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973
14974 *alias = NULL; /* default to no alias */
14975
14976 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
14977 && (*arg)[2] == (int)KE_SNR)
14978 {
14979 /* hard coded <SNR>, already translated */
14980 *arg += 3;
14981 return get_id_len(arg) + 3;
14982 }
14983 len = eval_fname_script(*arg);
14984 if (len > 0)
14985 {
14986 /* literal "<SID>", "s:" or "<SNR>" */
14987 *arg += len;
14988 }
14989
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014991 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014993 p = find_name_end(*arg, &expr_start, &expr_end,
14994 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995 if (expr_start != NULL)
14996 {
14997 char_u *temp_string;
14998
14999 if (!evaluate)
15000 {
15001 len += (int)(p - *arg);
15002 *arg = skipwhite(p);
15003 return len;
15004 }
15005
15006 /*
15007 * Include any <SID> etc in the expanded string:
15008 * Thus the -len here.
15009 */
15010 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15011 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015012 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013 *alias = temp_string;
15014 *arg = skipwhite(p);
15015 return (int)STRLEN(temp_string);
15016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017
15018 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015019 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 EMSG2(_(e_invexpr2), *arg);
15021
15022 return len;
15023}
15024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015025/*
15026 * Find the end of a variable or function name, taking care of magic braces.
15027 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15028 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015029 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015030 * Return a pointer to just after the name. Equal to "arg" if there is no
15031 * valid name.
15032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015034find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035 char_u *arg;
15036 char_u **expr_start;
15037 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015038 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015040 int mb_nest = 0;
15041 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042 char_u *p;
15043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015044 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015046 *expr_start = NULL;
15047 *expr_end = NULL;
15048 }
15049
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015050 /* Quick check for valid starting character. */
15051 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15052 return arg;
15053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015054 for (p = arg; *p != NUL
15055 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015056 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015057 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015058 || mb_nest != 0
15059 || br_nest != 0); ++p)
15060 {
15061 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015062 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015063 if (*p == '[')
15064 ++br_nest;
15065 else if (*p == ']')
15066 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015067 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015068 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015069 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015070 if (*p == '{')
15071 {
15072 mb_nest++;
15073 if (expr_start != NULL && *expr_start == NULL)
15074 *expr_start = p;
15075 }
15076 else if (*p == '}')
15077 {
15078 mb_nest--;
15079 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15080 *expr_end = p;
15081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083 }
15084
15085 return p;
15086}
15087
15088/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015089 * Expands out the 'magic' {}'s in a variable/function name.
15090 * Note that this can call itself recursively, to deal with
15091 * constructs like foo{bar}{baz}{bam}
15092 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15093 * "in_start" ^
15094 * "expr_start" ^
15095 * "expr_end" ^
15096 * "in_end" ^
15097 *
15098 * Returns a new allocated string, which the caller must free.
15099 * Returns NULL for failure.
15100 */
15101 static char_u *
15102make_expanded_name(in_start, expr_start, expr_end, in_end)
15103 char_u *in_start;
15104 char_u *expr_start;
15105 char_u *expr_end;
15106 char_u *in_end;
15107{
15108 char_u c1;
15109 char_u *retval = NULL;
15110 char_u *temp_result;
15111 char_u *nextcmd = NULL;
15112
15113 if (expr_end == NULL || in_end == NULL)
15114 return NULL;
15115 *expr_start = NUL;
15116 *expr_end = NUL;
15117 c1 = *in_end;
15118 *in_end = NUL;
15119
15120 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15121 if (temp_result != NULL && nextcmd == NULL)
15122 {
15123 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15124 + (in_end - expr_end) + 1));
15125 if (retval != NULL)
15126 {
15127 STRCPY(retval, in_start);
15128 STRCAT(retval, temp_result);
15129 STRCAT(retval, expr_end + 1);
15130 }
15131 }
15132 vim_free(temp_result);
15133
15134 *in_end = c1; /* put char back for error messages */
15135 *expr_start = '{';
15136 *expr_end = '}';
15137
15138 if (retval != NULL)
15139 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015140 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015141 if (expr_start != NULL)
15142 {
15143 /* Further expansion! */
15144 temp_result = make_expanded_name(retval, expr_start,
15145 expr_end, temp_result);
15146 vim_free(retval);
15147 retval = temp_result;
15148 }
15149 }
15150
15151 return retval;
15152}
15153
15154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015156 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157 */
15158 static int
15159eval_isnamec(c)
15160 int c;
15161{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015162 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15163}
15164
15165/*
15166 * Return TRUE if character "c" can be used as the first character in a
15167 * variable or function name (excluding '{' and '}').
15168 */
15169 static int
15170eval_isnamec1(c)
15171 int c;
15172{
15173 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015174}
15175
15176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177 * Set number v: variable to "val".
15178 */
15179 void
15180set_vim_var_nr(idx, val)
15181 int idx;
15182 long val;
15183{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015184 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185}
15186
15187/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015188 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189 */
15190 long
15191get_vim_var_nr(idx)
15192 int idx;
15193{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015194 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195}
15196
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015197#if defined(FEAT_AUTOCMD) || defined(PROTO)
15198/*
15199 * Get string v: variable value. Uses a static buffer, can only be used once.
15200 */
15201 char_u *
15202get_vim_var_str(idx)
15203 int idx;
15204{
15205 return get_tv_string(&vimvars[idx].vv_tv);
15206}
15207#endif
15208
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209/*
15210 * Set v:count, v:count1 and v:prevcount.
15211 */
15212 void
15213set_vcount(count, count1)
15214 long count;
15215 long count1;
15216{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015217 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15218 vimvars[VV_COUNT].vv_nr = count;
15219 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015220}
15221
15222/*
15223 * Set string v: variable to a copy of "val".
15224 */
15225 void
15226set_vim_var_string(idx, val, len)
15227 int idx;
15228 char_u *val;
15229 int len; /* length of "val" to use or -1 (whole string) */
15230{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015231 /* Need to do this (at least) once, since we can't initialize a union.
15232 * Will always be invoked when "v:progname" is set. */
15233 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15234
Bram Moolenaare9a41262005-01-15 22:18:47 +000015235 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015237 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015238 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015239 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015241 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242}
15243
15244/*
15245 * Set v:register if needed.
15246 */
15247 void
15248set_reg_var(c)
15249 int c;
15250{
15251 char_u regname;
15252
15253 if (c == 0 || c == ' ')
15254 regname = '"';
15255 else
15256 regname = c;
15257 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015258 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259 set_vim_var_string(VV_REG, &regname, 1);
15260}
15261
15262/*
15263 * Get or set v:exception. If "oldval" == NULL, return the current value.
15264 * Otherwise, restore the value to "oldval" and return NULL.
15265 * Must always be called in pairs to save and restore v:exception! Does not
15266 * take care of memory allocations.
15267 */
15268 char_u *
15269v_exception(oldval)
15270 char_u *oldval;
15271{
15272 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015273 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274
Bram Moolenaare9a41262005-01-15 22:18:47 +000015275 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276 return NULL;
15277}
15278
15279/*
15280 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15281 * Otherwise, restore the value to "oldval" and return NULL.
15282 * Must always be called in pairs to save and restore v:throwpoint! Does not
15283 * take care of memory allocations.
15284 */
15285 char_u *
15286v_throwpoint(oldval)
15287 char_u *oldval;
15288{
15289 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015290 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291
Bram Moolenaare9a41262005-01-15 22:18:47 +000015292 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015293 return NULL;
15294}
15295
15296#if defined(FEAT_AUTOCMD) || defined(PROTO)
15297/*
15298 * Set v:cmdarg.
15299 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15300 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15301 * Must always be called in pairs!
15302 */
15303 char_u *
15304set_cmdarg(eap, oldarg)
15305 exarg_T *eap;
15306 char_u *oldarg;
15307{
15308 char_u *oldval;
15309 char_u *newval;
15310 unsigned len;
15311
Bram Moolenaare9a41262005-01-15 22:18:47 +000015312 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015313 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015315 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015316 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015317 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318 }
15319
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015320 if (eap->force_bin == FORCE_BIN)
15321 len = 6;
15322 else if (eap->force_bin == FORCE_NOBIN)
15323 len = 8;
15324 else
15325 len = 0;
15326 if (eap->force_ff != 0)
15327 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15328# ifdef FEAT_MBYTE
15329 if (eap->force_enc != 0)
15330 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15331# endif
15332
15333 newval = alloc(len + 1);
15334 if (newval == NULL)
15335 return NULL;
15336
15337 if (eap->force_bin == FORCE_BIN)
15338 sprintf((char *)newval, " ++bin");
15339 else if (eap->force_bin == FORCE_NOBIN)
15340 sprintf((char *)newval, " ++nobin");
15341 else
15342 *newval = NUL;
15343 if (eap->force_ff != 0)
15344 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15345 eap->cmd + eap->force_ff);
15346# ifdef FEAT_MBYTE
15347 if (eap->force_enc != 0)
15348 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15349 eap->cmd + eap->force_enc);
15350# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015351 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015352 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353}
15354#endif
15355
15356/*
15357 * Get the value of internal variable "name".
15358 * Return OK or FAIL.
15359 */
15360 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015361get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 char_u *name;
15363 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015364 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015365 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366{
15367 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015368 typval_T *tv = NULL;
15369 typval_T atv;
15370 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372
15373 /* truncate the name, so that we can use strcmp() */
15374 cc = name[len];
15375 name[len] = NUL;
15376
15377 /*
15378 * Check for "b:changedtick".
15379 */
15380 if (STRCMP(name, "b:changedtick") == 0)
15381 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015382 atv.v_type = VAR_NUMBER;
15383 atv.vval.v_number = curbuf->b_changedtick;
15384 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385 }
15386
15387 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388 * Check for user-defined variables.
15389 */
15390 else
15391 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015392 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015394 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 }
15396
Bram Moolenaare9a41262005-01-15 22:18:47 +000015397 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015399 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015400 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401 ret = FAIL;
15402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015403 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015404 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405
15406 name[len] = cc;
15407
15408 return ret;
15409}
15410
15411/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015412 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15413 * Also handle function call with Funcref variable: func(expr)
15414 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15415 */
15416 static int
15417handle_subscript(arg, rettv, evaluate, verbose)
15418 char_u **arg;
15419 typval_T *rettv;
15420 int evaluate; /* do more than finding the end */
15421 int verbose; /* give error messages */
15422{
15423 int ret = OK;
15424 dict_T *selfdict = NULL;
15425 char_u *s;
15426 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015427 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015428
15429 while (ret == OK
15430 && (**arg == '['
15431 || (**arg == '.' && rettv->v_type == VAR_DICT)
15432 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15433 && !vim_iswhite(*(*arg - 1)))
15434 {
15435 if (**arg == '(')
15436 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015437 /* need to copy the funcref so that we can clear rettv */
15438 functv = *rettv;
15439 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015440
15441 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015442 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015443 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015444 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15445 &len, evaluate, selfdict);
15446
15447 /* Clear the funcref afterwards, so that deleting it while
15448 * evaluating the arguments is possible (see test55). */
15449 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015450
15451 /* Stop the expression evaluation when immediately aborting on
15452 * error, or when an interrupt occurred or an exception was thrown
15453 * but not caught. */
15454 if (aborting())
15455 {
15456 if (ret == OK)
15457 clear_tv(rettv);
15458 ret = FAIL;
15459 }
15460 dict_unref(selfdict);
15461 selfdict = NULL;
15462 }
15463 else /* **arg == '[' || **arg == '.' */
15464 {
15465 dict_unref(selfdict);
15466 if (rettv->v_type == VAR_DICT)
15467 {
15468 selfdict = rettv->vval.v_dict;
15469 if (selfdict != NULL)
15470 ++selfdict->dv_refcount;
15471 }
15472 else
15473 selfdict = NULL;
15474 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15475 {
15476 clear_tv(rettv);
15477 ret = FAIL;
15478 }
15479 }
15480 }
15481 dict_unref(selfdict);
15482 return ret;
15483}
15484
15485/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015486 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15487 * value).
15488 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015489 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015490alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015491{
Bram Moolenaar33570922005-01-25 22:26:29 +000015492 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015493}
15494
15495/*
15496 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497 * The string "s" must have been allocated, it is consumed.
15498 * Return NULL for out of memory, the variable otherwise.
15499 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015500 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015501alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502 char_u *s;
15503{
Bram Moolenaar33570922005-01-25 22:26:29 +000015504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015506 rettv = alloc_tv();
15507 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015509 rettv->v_type = VAR_STRING;
15510 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 }
15512 else
15513 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015514 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515}
15516
15517/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015518 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519 */
15520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015521free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015522 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523{
15524 if (varp != NULL)
15525 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015526 switch (varp->v_type)
15527 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015528 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015529 func_unref(varp->vval.v_string);
15530 /*FALLTHROUGH*/
15531 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015532 vim_free(varp->vval.v_string);
15533 break;
15534 case VAR_LIST:
15535 list_unref(varp->vval.v_list);
15536 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015537 case VAR_DICT:
15538 dict_unref(varp->vval.v_dict);
15539 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015540 case VAR_NUMBER:
15541 case VAR_UNKNOWN:
15542 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015543 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015544 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015545 break;
15546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547 vim_free(varp);
15548 }
15549}
15550
15551/*
15552 * Free the memory for a variable value and set the value to NULL or 0.
15553 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015554 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015555clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015556 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557{
15558 if (varp != NULL)
15559 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015560 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015562 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015563 func_unref(varp->vval.v_string);
15564 /*FALLTHROUGH*/
15565 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015566 vim_free(varp->vval.v_string);
15567 varp->vval.v_string = NULL;
15568 break;
15569 case VAR_LIST:
15570 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015571 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015572 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015573 case VAR_DICT:
15574 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015575 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015576 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015577 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015578 varp->vval.v_number = 0;
15579 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015580 case VAR_UNKNOWN:
15581 break;
15582 default:
15583 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015585 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586 }
15587}
15588
15589/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015590 * Set the value of a variable to NULL without freeing items.
15591 */
15592 static void
15593init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015594 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015595{
15596 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015597 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015598}
15599
15600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601 * Get the number value of a variable.
15602 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015603 * For incompatible types, return 0.
15604 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15605 * caller of incompatible types: it sets *denote to TRUE if "denote"
15606 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 */
15608 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015609get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015610 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015612 int error = FALSE;
15613
15614 return get_tv_number_chk(varp, &error); /* return 0L on error */
15615}
15616
15617 static long
15618get_tv_number_chk(varp, denote)
15619 typval_T *varp;
15620 int *denote;
15621{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015622 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015624 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015626 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015627 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015628 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015629 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015630 break;
15631 case VAR_STRING:
15632 if (varp->vval.v_string != NULL)
15633 vim_str2nr(varp->vval.v_string, NULL, NULL,
15634 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015635 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015636 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015637 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015638 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015639 case VAR_DICT:
15640 EMSG(_("E728: Using a Dictionary as a number"));
15641 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015642 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015643 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015644 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015646 if (denote == NULL) /* useful for values that must be unsigned */
15647 n = -1;
15648 else
15649 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015650 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651}
15652
15653/*
15654 * Get the lnum from the first argument. Also accepts ".", "$", etc.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015655 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015656 */
15657 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015658get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015659 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660{
Bram Moolenaar33570922005-01-25 22:26:29 +000015661 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662 linenr_T lnum;
15663
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015664 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 if (lnum == 0) /* no valid number, try using line() */
15666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015667 rettv.v_type = VAR_NUMBER;
15668 f_line(argvars, &rettv);
15669 lnum = rettv.vval.v_number;
15670 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 }
15672 return lnum;
15673}
15674
15675/*
15676 * Get the string value of a variable.
15677 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015678 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15679 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680 * If the String variable has never been set, return an empty string.
15681 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015682 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15683 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684 */
15685 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015686get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015687 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688{
15689 static char_u mybuf[NUMBUFLEN];
15690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015691 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692}
15693
15694 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015695get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015696 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015697 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015699 char_u *res = get_tv_string_buf_chk(varp, buf);
15700
15701 return res != NULL ? res : (char_u *)"";
15702}
15703
15704 static char_u *
15705get_tv_string_chk(varp)
15706 typval_T *varp;
15707{
15708 static char_u mybuf[NUMBUFLEN];
15709
15710 return get_tv_string_buf_chk(varp, mybuf);
15711}
15712
15713 static char_u *
15714get_tv_string_buf_chk(varp, buf)
15715 typval_T *varp;
15716 char_u *buf;
15717{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015718 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015720 case VAR_NUMBER:
15721 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15722 return buf;
15723 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015724 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015725 break;
15726 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015727 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015728 break;
15729 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015730 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015731 break;
15732 case VAR_STRING:
15733 if (varp->vval.v_string != NULL)
15734 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015735 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015736 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015737 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015738 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015740 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741}
15742
15743/*
15744 * Find variable "name" in the list of variables.
15745 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015746 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015747 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015748 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015750 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015751find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015753 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015756 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757
Bram Moolenaara7043832005-01-21 11:56:39 +000015758 ht = find_var_ht(name, &varname);
15759 if (htp != NULL)
15760 *htp = ht;
15761 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015763 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764}
15765
15766/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015767 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000015768 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015770 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015771find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000015772 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000015773 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015774 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000015775{
Bram Moolenaar33570922005-01-25 22:26:29 +000015776 hashitem_T *hi;
15777
15778 if (*varname == NUL)
15779 {
15780 /* Must be something like "s:", otherwise "ht" would be NULL. */
15781 switch (varname[-2])
15782 {
15783 case 's': return &SCRIPT_SV(current_SID).sv_var;
15784 case 'g': return &globvars_var;
15785 case 'v': return &vimvars_var;
15786 case 'b': return &curbuf->b_bufvar;
15787 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015788 case 'l': return current_funccal == NULL
15789 ? NULL : &current_funccal->l_vars_var;
15790 case 'a': return current_funccal == NULL
15791 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000015792 }
15793 return NULL;
15794 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015795
15796 hi = hash_find(ht, varname);
15797 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015798 {
15799 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015800 * worked find the variable again. Don't auto-load a script if it was
15801 * loaded already, otherwise it would be loaded every time when
15802 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015803 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015804 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015805 hi = hash_find(ht, varname);
15806 if (HASHITEM_EMPTY(hi))
15807 return NULL;
15808 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015809 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015810}
15811
15812/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015813 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015814 * Set "varname" to the start of name without ':'.
15815 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015816 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015817find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818 char_u *name;
15819 char_u **varname;
15820{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015821 hashitem_T *hi;
15822
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823 if (name[1] != ':')
15824 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015825 /* The name must not start with a colon or #. */
15826 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827 return NULL;
15828 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015829
15830 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015831 hi = hash_find(&compat_hashtab, name);
15832 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000015833 return &compat_hashtab;
15834
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015836 return &globvarht; /* global variable */
15837 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838 }
15839 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015840 if (*name == 'g') /* global variable */
15841 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015842 /* There must be no ':' or '#' in the rest of the name, unless g: is used
15843 */
15844 if (vim_strchr(name + 2, ':') != NULL
15845 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015846 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015847 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015848 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015850 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000015851 if (*name == 'v') /* v: variable */
15852 return &vimvarht;
15853 if (*name == 'a' && current_funccal != NULL) /* function argument */
15854 return &current_funccal->l_avars.dv_hashtab;
15855 if (*name == 'l' && current_funccal != NULL) /* local function variable */
15856 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 if (*name == 's' /* script variable */
15858 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
15859 return &SCRIPT_VARS(current_SID);
15860 return NULL;
15861}
15862
15863/*
15864 * Get the string value of a (global/local) variable.
15865 * Returns NULL when it doesn't exist.
15866 */
15867 char_u *
15868get_var_value(name)
15869 char_u *name;
15870{
Bram Moolenaar33570922005-01-25 22:26:29 +000015871 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872
Bram Moolenaara7043832005-01-21 11:56:39 +000015873 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874 if (v == NULL)
15875 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015876 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877}
15878
15879/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015880 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881 * sourcing this script and when executing functions defined in the script.
15882 */
15883 void
15884new_script_vars(id)
15885 scid_T id;
15886{
Bram Moolenaara7043832005-01-21 11:56:39 +000015887 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000015888 hashtab_T *ht;
15889 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000015890
Bram Moolenaar071d4272004-06-13 20:20:40 +000015891 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
15892 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015893 /* Re-allocating ga_data means that an ht_array pointing to
15894 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000015895 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000015896 for (i = 1; i <= ga_scripts.ga_len; ++i)
15897 {
15898 ht = &SCRIPT_VARS(i);
15899 if (ht->ht_mask == HT_INIT_SIZE - 1)
15900 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000015901 sv = &SCRIPT_SV(i);
15902 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000015903 }
15904
Bram Moolenaar071d4272004-06-13 20:20:40 +000015905 while (ga_scripts.ga_len < id)
15906 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015907 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
15908 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015909 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015910 }
15911 }
15912}
15913
15914/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015915 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
15916 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015917 */
15918 void
Bram Moolenaar33570922005-01-25 22:26:29 +000015919init_var_dict(dict, dict_var)
15920 dict_T *dict;
15921 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922{
Bram Moolenaar33570922005-01-25 22:26:29 +000015923 hash_init(&dict->dv_hashtab);
15924 dict->dv_refcount = 99999;
15925 dict_var->di_tv.vval.v_dict = dict;
15926 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015927 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015928 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15929 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930}
15931
15932/*
15933 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000015934 * Frees all allocated variables and the value they contain.
15935 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015936 */
15937 void
Bram Moolenaara7043832005-01-21 11:56:39 +000015938vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000015939 hashtab_T *ht;
15940{
15941 vars_clear_ext(ht, TRUE);
15942}
15943
15944/*
15945 * Like vars_clear(), but only free the value if "free_val" is TRUE.
15946 */
15947 static void
15948vars_clear_ext(ht, free_val)
15949 hashtab_T *ht;
15950 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951{
Bram Moolenaara7043832005-01-21 11:56:39 +000015952 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000015953 hashitem_T *hi;
15954 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015955
Bram Moolenaar33570922005-01-25 22:26:29 +000015956 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000015957 todo = ht->ht_used;
15958 for (hi = ht->ht_array; todo > 0; ++hi)
15959 {
15960 if (!HASHITEM_EMPTY(hi))
15961 {
15962 --todo;
15963
Bram Moolenaar33570922005-01-25 22:26:29 +000015964 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000015965 * ht_array might change then. hash_clear() takes care of it
15966 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015967 v = HI2DI(hi);
15968 if (free_val)
15969 clear_tv(&v->di_tv);
15970 if ((v->di_flags & DI_FLAGS_FIX) == 0)
15971 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000015972 }
15973 }
15974 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015975 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015976}
15977
Bram Moolenaara7043832005-01-21 11:56:39 +000015978/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015979 * Delete a variable from hashtab "ht" at item "hi".
15980 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000015981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015982 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000015983delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000015984 hashtab_T *ht;
15985 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986{
Bram Moolenaar33570922005-01-25 22:26:29 +000015987 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015988
15989 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000015990 clear_tv(&di->di_tv);
15991 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992}
15993
15994/*
15995 * List the value of one internal variable.
15996 */
15997 static void
15998list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000015999 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000 char_u *prefix;
16001{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016002 char_u *tofree;
16003 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016004 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016005
Bram Moolenaar33570922005-01-25 22:26:29 +000016006 s = echo_string(&v->di_tv, &tofree, numbuf);
16007 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016008 s == NULL ? (char_u *)"" : s);
16009 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010}
16011
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012 static void
16013list_one_var_a(prefix, name, type, string)
16014 char_u *prefix;
16015 char_u *name;
16016 int type;
16017 char_u *string;
16018{
16019 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16020 if (name != NULL) /* "a:" vars don't have a name stored */
16021 msg_puts(name);
16022 msg_putchar(' ');
16023 msg_advance(22);
16024 if (type == VAR_NUMBER)
16025 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016026 else if (type == VAR_FUNC)
16027 msg_putchar('*');
16028 else if (type == VAR_LIST)
16029 {
16030 msg_putchar('[');
16031 if (*string == '[')
16032 ++string;
16033 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016034 else if (type == VAR_DICT)
16035 {
16036 msg_putchar('{');
16037 if (*string == '{')
16038 ++string;
16039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040 else
16041 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016042
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016044
16045 if (type == VAR_FUNC)
16046 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047}
16048
16049/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016050 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051 * If the variable already exists, the value is updated.
16052 * Otherwise the variable is created.
16053 */
16054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016055set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016057 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016058 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059{
Bram Moolenaar33570922005-01-25 22:26:29 +000016060 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016062 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016063 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016065 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016066 {
16067 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16068 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16069 ? name[2] : name[0]))
16070 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016071 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016072 return;
16073 }
16074 if (function_exists(name))
16075 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016076 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016077 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016078 return;
16079 }
16080 }
16081
Bram Moolenaara7043832005-01-21 11:56:39 +000016082 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016083 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016084 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016085 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016086 return;
16087 }
16088
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016089 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016090 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016092 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016093 if (var_check_ro(v->di_flags, name)
16094 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016095 return;
16096 if (v->di_tv.v_type != tv->v_type
16097 && !((v->di_tv.v_type == VAR_STRING
16098 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016099 && (tv->v_type == VAR_STRING
16100 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016101 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016102 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016103 return;
16104 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016105
16106 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016107 * Handle setting internal v: variables separately: we don't change
16108 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016109 */
16110 if (ht == &vimvarht)
16111 {
16112 if (v->di_tv.v_type == VAR_STRING)
16113 {
16114 vim_free(v->di_tv.vval.v_string);
16115 if (copy || tv->v_type != VAR_STRING)
16116 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16117 else
16118 {
16119 /* Take over the string to avoid an extra alloc/free. */
16120 v->di_tv.vval.v_string = tv->vval.v_string;
16121 tv->vval.v_string = NULL;
16122 }
16123 }
16124 else if (v->di_tv.v_type != VAR_NUMBER)
16125 EMSG2(_(e_intern2), "set_var()");
16126 else
16127 v->di_tv.vval.v_number = get_tv_number(tv);
16128 return;
16129 }
16130
16131 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132 }
16133 else /* add a new variable */
16134 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016135 /* Make sure the variable name is valid. */
16136 for (p = varname; *p != NUL; ++p)
16137 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16138 {
16139 EMSG2(_(e_illvar), varname);
16140 return;
16141 }
16142
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016143 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16144 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016145 if (v == NULL)
16146 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016147 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016148 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016150 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016151 return;
16152 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016153 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016156 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016157 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016158 else
16159 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016160 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016161 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016162 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164}
16165
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016166/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016167 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16168 * Also give an error message.
16169 */
16170 static int
16171var_check_ro(flags, name)
16172 int flags;
16173 char_u *name;
16174{
16175 if (flags & DI_FLAGS_RO)
16176 {
16177 EMSG2(_(e_readonlyvar), name);
16178 return TRUE;
16179 }
16180 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16181 {
16182 EMSG2(_(e_readonlysbx), name);
16183 return TRUE;
16184 }
16185 return FALSE;
16186}
16187
16188/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016189 * Return TRUE if typeval "tv" is set to be locked (immutable).
16190 * Also give an error message, using "name".
16191 */
16192 static int
16193tv_check_lock(lock, name)
16194 int lock;
16195 char_u *name;
16196{
16197 if (lock & VAR_LOCKED)
16198 {
16199 EMSG2(_("E741: Value is locked: %s"),
16200 name == NULL ? (char_u *)_("Unknown") : name);
16201 return TRUE;
16202 }
16203 if (lock & VAR_FIXED)
16204 {
16205 EMSG2(_("E742: Cannot change value of %s"),
16206 name == NULL ? (char_u *)_("Unknown") : name);
16207 return TRUE;
16208 }
16209 return FALSE;
16210}
16211
16212/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016213 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016214 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016215 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016218copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016219 typval_T *from;
16220 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016222 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016223 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016224 switch (from->v_type)
16225 {
16226 case VAR_NUMBER:
16227 to->vval.v_number = from->vval.v_number;
16228 break;
16229 case VAR_STRING:
16230 case VAR_FUNC:
16231 if (from->vval.v_string == NULL)
16232 to->vval.v_string = NULL;
16233 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016234 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016235 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016236 if (from->v_type == VAR_FUNC)
16237 func_ref(to->vval.v_string);
16238 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016239 break;
16240 case VAR_LIST:
16241 if (from->vval.v_list == NULL)
16242 to->vval.v_list = NULL;
16243 else
16244 {
16245 to->vval.v_list = from->vval.v_list;
16246 ++to->vval.v_list->lv_refcount;
16247 }
16248 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016249 case VAR_DICT:
16250 if (from->vval.v_dict == NULL)
16251 to->vval.v_dict = NULL;
16252 else
16253 {
16254 to->vval.v_dict = from->vval.v_dict;
16255 ++to->vval.v_dict->dv_refcount;
16256 }
16257 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016258 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016259 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016260 break;
16261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262}
16263
16264/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016265 * Make a copy of an item.
16266 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016267 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16268 * reference to an already copied list/dict can be used.
16269 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016270 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016271 static int
16272item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016273 typval_T *from;
16274 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016275 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016276 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016277{
16278 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016279 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016280
Bram Moolenaar33570922005-01-25 22:26:29 +000016281 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016282 {
16283 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016284 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016285 }
16286 ++recurse;
16287
16288 switch (from->v_type)
16289 {
16290 case VAR_NUMBER:
16291 case VAR_STRING:
16292 case VAR_FUNC:
16293 copy_tv(from, to);
16294 break;
16295 case VAR_LIST:
16296 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016297 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016298 if (from->vval.v_list == NULL)
16299 to->vval.v_list = NULL;
16300 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16301 {
16302 /* use the copy made earlier */
16303 to->vval.v_list = from->vval.v_list->lv_copylist;
16304 ++to->vval.v_list->lv_refcount;
16305 }
16306 else
16307 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16308 if (to->vval.v_list == NULL)
16309 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016310 break;
16311 case VAR_DICT:
16312 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016313 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016314 if (from->vval.v_dict == NULL)
16315 to->vval.v_dict = NULL;
16316 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16317 {
16318 /* use the copy made earlier */
16319 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16320 ++to->vval.v_dict->dv_refcount;
16321 }
16322 else
16323 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16324 if (to->vval.v_dict == NULL)
16325 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016326 break;
16327 default:
16328 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016329 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016330 }
16331 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016332 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016333}
16334
16335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336 * ":echo expr1 ..." print each argument separated with a space, add a
16337 * newline at the end.
16338 * ":echon expr1 ..." print each argument plain.
16339 */
16340 void
16341ex_echo(eap)
16342 exarg_T *eap;
16343{
16344 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016345 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016346 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347 char_u *p;
16348 int needclr = TRUE;
16349 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016350 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351
16352 if (eap->skip)
16353 ++emsg_skip;
16354 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16355 {
16356 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016357 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358 {
16359 /*
16360 * Report the invalid expression unless the expression evaluation
16361 * has been cancelled due to an aborting error, an interrupt, or an
16362 * exception.
16363 */
16364 if (!aborting())
16365 EMSG2(_(e_invexpr2), p);
16366 break;
16367 }
16368 if (!eap->skip)
16369 {
16370 if (atstart)
16371 {
16372 atstart = FALSE;
16373 /* Call msg_start() after eval1(), evaluating the expression
16374 * may cause a message to appear. */
16375 if (eap->cmdidx == CMD_echo)
16376 msg_start();
16377 }
16378 else if (eap->cmdidx == CMD_echo)
16379 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016380 p = echo_string(&rettv, &tofree, numbuf);
16381 if (p != NULL)
16382 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016384 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016386 if (*p != TAB && needclr)
16387 {
16388 /* remove any text still there from the command */
16389 msg_clr_eos();
16390 needclr = FALSE;
16391 }
16392 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393 }
16394 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016395 {
16396#ifdef FEAT_MBYTE
16397 if (has_mbyte)
16398 {
16399 int i = (*mb_ptr2len_check)(p);
16400
16401 (void)msg_outtrans_len_attr(p, i, echo_attr);
16402 p += i - 1;
16403 }
16404 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016406 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016409 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016411 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016412 arg = skipwhite(arg);
16413 }
16414 eap->nextcmd = check_nextcmd(arg);
16415
16416 if (eap->skip)
16417 --emsg_skip;
16418 else
16419 {
16420 /* remove text that may still be there from the command */
16421 if (needclr)
16422 msg_clr_eos();
16423 if (eap->cmdidx == CMD_echo)
16424 msg_end();
16425 }
16426}
16427
16428/*
16429 * ":echohl {name}".
16430 */
16431 void
16432ex_echohl(eap)
16433 exarg_T *eap;
16434{
16435 int id;
16436
16437 id = syn_name2id(eap->arg);
16438 if (id == 0)
16439 echo_attr = 0;
16440 else
16441 echo_attr = syn_id2attr(id);
16442}
16443
16444/*
16445 * ":execute expr1 ..." execute the result of an expression.
16446 * ":echomsg expr1 ..." Print a message
16447 * ":echoerr expr1 ..." Print an error
16448 * Each gets spaces around each argument and a newline at the end for
16449 * echo commands
16450 */
16451 void
16452ex_execute(eap)
16453 exarg_T *eap;
16454{
16455 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016456 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457 int ret = OK;
16458 char_u *p;
16459 garray_T ga;
16460 int len;
16461 int save_did_emsg;
16462
16463 ga_init2(&ga, 1, 80);
16464
16465 if (eap->skip)
16466 ++emsg_skip;
16467 while (*arg != NUL && *arg != '|' && *arg != '\n')
16468 {
16469 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016470 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 {
16472 /*
16473 * Report the invalid expression unless the expression evaluation
16474 * has been cancelled due to an aborting error, an interrupt, or an
16475 * exception.
16476 */
16477 if (!aborting())
16478 EMSG2(_(e_invexpr2), p);
16479 ret = FAIL;
16480 break;
16481 }
16482
16483 if (!eap->skip)
16484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016485 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486 len = (int)STRLEN(p);
16487 if (ga_grow(&ga, len + 2) == FAIL)
16488 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016489 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016490 ret = FAIL;
16491 break;
16492 }
16493 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496 ga.ga_len += len;
16497 }
16498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016499 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016500 arg = skipwhite(arg);
16501 }
16502
16503 if (ret != FAIL && ga.ga_data != NULL)
16504 {
16505 if (eap->cmdidx == CMD_echomsg)
16506 MSG_ATTR(ga.ga_data, echo_attr);
16507 else if (eap->cmdidx == CMD_echoerr)
16508 {
16509 /* We don't want to abort following commands, restore did_emsg. */
16510 save_did_emsg = did_emsg;
16511 EMSG((char_u *)ga.ga_data);
16512 if (!force_abort)
16513 did_emsg = save_did_emsg;
16514 }
16515 else if (eap->cmdidx == CMD_execute)
16516 do_cmdline((char_u *)ga.ga_data,
16517 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16518 }
16519
16520 ga_clear(&ga);
16521
16522 if (eap->skip)
16523 --emsg_skip;
16524
16525 eap->nextcmd = check_nextcmd(arg);
16526}
16527
16528/*
16529 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16530 * "arg" points to the "&" or '+' when called, to "option" when returning.
16531 * Returns NULL when no option name found. Otherwise pointer to the char
16532 * after the option name.
16533 */
16534 static char_u *
16535find_option_end(arg, opt_flags)
16536 char_u **arg;
16537 int *opt_flags;
16538{
16539 char_u *p = *arg;
16540
16541 ++p;
16542 if (*p == 'g' && p[1] == ':')
16543 {
16544 *opt_flags = OPT_GLOBAL;
16545 p += 2;
16546 }
16547 else if (*p == 'l' && p[1] == ':')
16548 {
16549 *opt_flags = OPT_LOCAL;
16550 p += 2;
16551 }
16552 else
16553 *opt_flags = 0;
16554
16555 if (!ASCII_ISALPHA(*p))
16556 return NULL;
16557 *arg = p;
16558
16559 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16560 p += 4; /* termcap option */
16561 else
16562 while (ASCII_ISALPHA(*p))
16563 ++p;
16564 return p;
16565}
16566
16567/*
16568 * ":function"
16569 */
16570 void
16571ex_function(eap)
16572 exarg_T *eap;
16573{
16574 char_u *theline;
16575 int j;
16576 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016577 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578 char_u *name = NULL;
16579 char_u *p;
16580 char_u *arg;
16581 garray_T newargs;
16582 garray_T newlines;
16583 int varargs = FALSE;
16584 int mustend = FALSE;
16585 int flags = 0;
16586 ufunc_T *fp;
16587 int indent;
16588 int nesting;
16589 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016590 dictitem_T *v;
16591 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016592 static int func_nr = 0; /* number for nameless function */
16593 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016594 hashtab_T *ht;
16595 int todo;
16596 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597
16598 /*
16599 * ":function" without argument: list functions.
16600 */
16601 if (ends_excmd(*eap->arg))
16602 {
16603 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016604 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016605 todo = func_hashtab.ht_used;
16606 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016607 {
16608 if (!HASHITEM_EMPTY(hi))
16609 {
16610 --todo;
16611 fp = HI2UF(hi);
16612 if (!isdigit(*fp->uf_name))
16613 list_func_head(fp, FALSE);
16614 }
16615 }
16616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016617 eap->nextcmd = check_nextcmd(eap->arg);
16618 return;
16619 }
16620
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016621 /*
16622 * Get the function name. There are these situations:
16623 * func normal function name
16624 * "name" == func, "fudi.fd_dict" == NULL
16625 * dict.func new dictionary entry
16626 * "name" == NULL, "fudi.fd_dict" set,
16627 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16628 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016629 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016630 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16631 * dict.func existing dict entry that's not a Funcref
16632 * "name" == NULL, "fudi.fd_dict" set,
16633 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016636 name = trans_function_name(&p, eap->skip, 0, &fudi);
16637 paren = (vim_strchr(p, '(') != NULL);
16638 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639 {
16640 /*
16641 * Return on an invalid expression in braces, unless the expression
16642 * evaluation has been cancelled due to an aborting error, an
16643 * interrupt, or an exception.
16644 */
16645 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016646 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016647 if (!eap->skip && fudi.fd_newkey != NULL)
16648 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016649 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652 else
16653 eap->skip = TRUE;
16654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655 /* An error in a function call during evaluation of an expression in magic
16656 * braces should not cause the function not to be defined. */
16657 saved_did_emsg = did_emsg;
16658 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659
16660 /*
16661 * ":function func" with only function name: list function.
16662 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016663 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664 {
16665 if (!ends_excmd(*skipwhite(p)))
16666 {
16667 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016668 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669 }
16670 eap->nextcmd = check_nextcmd(p);
16671 if (eap->nextcmd != NULL)
16672 *p = NUL;
16673 if (!eap->skip && !got_int)
16674 {
16675 fp = find_func(name);
16676 if (fp != NULL)
16677 {
16678 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016679 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680 {
16681 msg_putchar('\n');
16682 msg_outnum((long)(j + 1));
16683 if (j < 9)
16684 msg_putchar(' ');
16685 if (j < 99)
16686 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016687 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016688 out_flush(); /* show a line at a time */
16689 ui_breakcheck();
16690 }
16691 if (!got_int)
16692 {
16693 msg_putchar('\n');
16694 msg_puts((char_u *)" endfunction");
16695 }
16696 }
16697 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016698 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016700 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016701 }
16702
16703 /*
16704 * ":function name(arg1, arg2)" Define function.
16705 */
16706 p = skipwhite(p);
16707 if (*p != '(')
16708 {
16709 if (!eap->skip)
16710 {
16711 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016712 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016713 }
16714 /* attempt to continue by skipping some text */
16715 if (vim_strchr(p, '(') != NULL)
16716 p = vim_strchr(p, '(');
16717 }
16718 p = skipwhite(p + 1);
16719
16720 ga_init2(&newargs, (int)sizeof(char_u *), 3);
16721 ga_init2(&newlines, (int)sizeof(char_u *), 3);
16722
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016723 if (!eap->skip)
16724 {
16725 /* Check the name of the function. */
16726 if (name != NULL)
16727 arg = name;
16728 else
16729 arg = fudi.fd_newkey;
16730 if (arg != NULL)
16731 {
16732 if (*arg == K_SPECIAL)
16733 j = 3;
16734 else
16735 j = 0;
16736 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
16737 : eval_isnamec(arg[j])))
16738 ++j;
16739 if (arg[j] != NUL)
16740 emsg_funcname(_(e_invarg2), arg);
16741 }
16742 }
16743
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744 /*
16745 * Isolate the arguments: "arg1, arg2, ...)"
16746 */
16747 while (*p != ')')
16748 {
16749 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
16750 {
16751 varargs = TRUE;
16752 p += 3;
16753 mustend = TRUE;
16754 }
16755 else
16756 {
16757 arg = p;
16758 while (ASCII_ISALNUM(*p) || *p == '_')
16759 ++p;
16760 if (arg == p || isdigit(*arg)
16761 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
16762 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
16763 {
16764 if (!eap->skip)
16765 EMSG2(_("E125: Illegal argument: %s"), arg);
16766 break;
16767 }
16768 if (ga_grow(&newargs, 1) == FAIL)
16769 goto erret;
16770 c = *p;
16771 *p = NUL;
16772 arg = vim_strsave(arg);
16773 if (arg == NULL)
16774 goto erret;
16775 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
16776 *p = c;
16777 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778 if (*p == ',')
16779 ++p;
16780 else
16781 mustend = TRUE;
16782 }
16783 p = skipwhite(p);
16784 if (mustend && *p != ')')
16785 {
16786 if (!eap->skip)
16787 EMSG2(_(e_invarg2), eap->arg);
16788 break;
16789 }
16790 }
16791 ++p; /* skip the ')' */
16792
Bram Moolenaare9a41262005-01-15 22:18:47 +000016793 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794 for (;;)
16795 {
16796 p = skipwhite(p);
16797 if (STRNCMP(p, "range", 5) == 0)
16798 {
16799 flags |= FC_RANGE;
16800 p += 5;
16801 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016802 else if (STRNCMP(p, "dict", 4) == 0)
16803 {
16804 flags |= FC_DICT;
16805 p += 4;
16806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807 else if (STRNCMP(p, "abort", 5) == 0)
16808 {
16809 flags |= FC_ABORT;
16810 p += 5;
16811 }
16812 else
16813 break;
16814 }
16815
16816 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
16817 EMSG(_(e_trailing));
16818
16819 /*
16820 * Read the body of the function, until ":endfunction" is found.
16821 */
16822 if (KeyTyped)
16823 {
16824 /* Check if the function already exists, don't let the user type the
16825 * whole function before telling him it doesn't work! For a script we
16826 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016827 if (!eap->skip && !eap->forceit)
16828 {
16829 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
16830 EMSG(_(e_funcdict));
16831 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016832 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016835 if (!eap->skip && did_emsg)
16836 goto erret;
16837
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838 msg_putchar('\n'); /* don't overwrite the function name */
16839 cmdline_row = msg_row;
16840 }
16841
16842 indent = 2;
16843 nesting = 0;
16844 for (;;)
16845 {
16846 msg_scroll = TRUE;
16847 need_wait_return = FALSE;
16848 if (eap->getline == NULL)
16849 theline = getcmdline(':', 0L, indent);
16850 else
16851 theline = eap->getline(':', eap->cookie, indent);
16852 if (KeyTyped)
16853 lines_left = Rows - 1;
16854 if (theline == NULL)
16855 {
16856 EMSG(_("E126: Missing :endfunction"));
16857 goto erret;
16858 }
16859
16860 if (skip_until != NULL)
16861 {
16862 /* between ":append" and "." and between ":python <<EOF" and "EOF"
16863 * don't check for ":endfunc". */
16864 if (STRCMP(theline, skip_until) == 0)
16865 {
16866 vim_free(skip_until);
16867 skip_until = NULL;
16868 }
16869 }
16870 else
16871 {
16872 /* skip ':' and blanks*/
16873 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
16874 ;
16875
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016876 /* Check for "endfunction". */
16877 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 {
16879 vim_free(theline);
16880 break;
16881 }
16882
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016883 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 * at "end". */
16885 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
16886 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016887 else if (STRNCMP(p, "if", 2) == 0
16888 || STRNCMP(p, "wh", 2) == 0
16889 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890 || STRNCMP(p, "try", 3) == 0)
16891 indent += 2;
16892
16893 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016894 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016895 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016896 if (*p == '!')
16897 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898 p += eval_fname_script(p);
16899 if (ASCII_ISALPHA(*p))
16900 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016901 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902 if (*skipwhite(p) == '(')
16903 {
16904 ++nesting;
16905 indent += 2;
16906 }
16907 }
16908 }
16909
16910 /* Check for ":append" or ":insert". */
16911 p = skip_range(p, NULL);
16912 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
16913 || (p[0] == 'i'
16914 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
16915 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
16916 skip_until = vim_strsave((char_u *)".");
16917
16918 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
16919 arg = skipwhite(skiptowhite(p));
16920 if (arg[0] == '<' && arg[1] =='<'
16921 && ((p[0] == 'p' && p[1] == 'y'
16922 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
16923 || (p[0] == 'p' && p[1] == 'e'
16924 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
16925 || (p[0] == 't' && p[1] == 'c'
16926 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
16927 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
16928 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000016929 || (p[0] == 'm' && p[1] == 'z'
16930 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016931 ))
16932 {
16933 /* ":python <<" continues until a dot, like ":append" */
16934 p = skipwhite(arg + 2);
16935 if (*p == NUL)
16936 skip_until = vim_strsave((char_u *)".");
16937 else
16938 skip_until = vim_strsave(p);
16939 }
16940 }
16941
16942 /* Add the line to the function. */
16943 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016944 {
16945 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016947 }
16948
16949 /* Copy the line to newly allocated memory. get_one_sourceline()
16950 * allocates 250 bytes per line, this saves 80% on average. The cost
16951 * is an extra alloc/free. */
16952 p = vim_strsave(theline);
16953 if (p != NULL)
16954 {
16955 vim_free(theline);
16956 theline = p;
16957 }
16958
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
16960 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961 }
16962
16963 /* Don't define the function when skipping commands or when an error was
16964 * detected. */
16965 if (eap->skip || did_emsg)
16966 goto erret;
16967
16968 /*
16969 * If there are no errors, add the function
16970 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016971 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016972 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016973 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000016974 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016975 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016976 emsg_funcname("E707: Function name conflicts with variable: %s",
16977 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016978 goto erret;
16979 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016980
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016981 fp = find_func(name);
16982 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016984 if (!eap->forceit)
16985 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016986 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016987 goto erret;
16988 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016989 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016990 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016991 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016992 name);
16993 goto erret;
16994 }
16995 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016996 ga_clear_strings(&(fp->uf_args));
16997 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016998 vim_free(name);
16999 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 }
17002 else
17003 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017004 char numbuf[20];
17005
17006 fp = NULL;
17007 if (fudi.fd_newkey == NULL && !eap->forceit)
17008 {
17009 EMSG(_(e_funcdict));
17010 goto erret;
17011 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017012 if (fudi.fd_di == NULL)
17013 {
17014 /* Can't add a function to a locked dictionary */
17015 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17016 goto erret;
17017 }
17018 /* Can't change an existing function if it is locked */
17019 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17020 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017021
17022 /* Give the function a sequential number. Can only be used with a
17023 * Funcref! */
17024 vim_free(name);
17025 sprintf(numbuf, "%d", ++func_nr);
17026 name = vim_strsave((char_u *)numbuf);
17027 if (name == NULL)
17028 goto erret;
17029 }
17030
17031 if (fp == NULL)
17032 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017033 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017034 {
17035 int slen, plen;
17036 char_u *scriptname;
17037
17038 /* Check that the autoload name matches the script name. */
17039 j = FAIL;
17040 if (sourcing_name != NULL)
17041 {
17042 scriptname = autoload_name(name);
17043 if (scriptname != NULL)
17044 {
17045 p = vim_strchr(scriptname, '/');
17046 plen = STRLEN(p);
17047 slen = STRLEN(sourcing_name);
17048 if (slen > plen && fnamecmp(p,
17049 sourcing_name + slen - plen) == 0)
17050 j = OK;
17051 vim_free(scriptname);
17052 }
17053 }
17054 if (j == FAIL)
17055 {
17056 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17057 goto erret;
17058 }
17059 }
17060
17061 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 if (fp == NULL)
17063 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017064
17065 if (fudi.fd_dict != NULL)
17066 {
17067 if (fudi.fd_di == NULL)
17068 {
17069 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017070 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017071 if (fudi.fd_di == NULL)
17072 {
17073 vim_free(fp);
17074 goto erret;
17075 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017076 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17077 {
17078 vim_free(fudi.fd_di);
17079 goto erret;
17080 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017081 }
17082 else
17083 /* overwrite existing dict entry */
17084 clear_tv(&fudi.fd_di->di_tv);
17085 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017086 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017087 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017088 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017089 }
17090
Bram Moolenaar071d4272004-06-13 20:20:40 +000017091 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017092 STRCPY(fp->uf_name, name);
17093 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017095 fp->uf_args = newargs;
17096 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017097#ifdef FEAT_PROFILE
17098 fp->uf_tml_count = NULL;
17099 fp->uf_tml_total = NULL;
17100 fp->uf_tml_self = NULL;
17101 fp->uf_profiling = FALSE;
17102 if (prof_def_func())
17103 func_do_profile(fp);
17104#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017105 fp->uf_varargs = varargs;
17106 fp->uf_flags = flags;
17107 fp->uf_calls = 0;
17108 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017109 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110
17111erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112 ga_clear_strings(&newargs);
17113 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017114ret_free:
17115 vim_free(skip_until);
17116 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119}
17120
17121/*
17122 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017123 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017125 * flags:
17126 * TFN_INT: internal function name OK
17127 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128 * Advances "pp" to just after the function name (if no error).
17129 */
17130 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017131trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132 char_u **pp;
17133 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017134 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017135 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017137 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138 char_u *start;
17139 char_u *end;
17140 int lead;
17141 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017143 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017144
17145 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017146 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017148
17149 /* Check for hard coded <SNR>: already translated function ID (from a user
17150 * command). */
17151 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17152 && (*pp)[2] == (int)KE_SNR)
17153 {
17154 *pp += 3;
17155 len = get_id_len(pp) + 3;
17156 return vim_strnsave(start, len);
17157 }
17158
17159 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17160 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017162 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017164
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017165 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17166 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017167 if (end == start)
17168 {
17169 if (!skip)
17170 EMSG(_("E129: Function name required"));
17171 goto theend;
17172 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017173 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017174 {
17175 /*
17176 * Report an invalid expression in braces, unless the expression
17177 * evaluation has been cancelled due to an aborting error, an
17178 * interrupt, or an exception.
17179 */
17180 if (!aborting())
17181 {
17182 if (end != NULL)
17183 EMSG2(_(e_invarg2), start);
17184 }
17185 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017186 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017187 goto theend;
17188 }
17189
17190 if (lv.ll_tv != NULL)
17191 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017192 if (fdp != NULL)
17193 {
17194 fdp->fd_dict = lv.ll_dict;
17195 fdp->fd_newkey = lv.ll_newkey;
17196 lv.ll_newkey = NULL;
17197 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017198 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017199 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17200 {
17201 name = vim_strsave(lv.ll_tv->vval.v_string);
17202 *pp = end;
17203 }
17204 else
17205 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017206 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17207 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017208 EMSG(_(e_funcref));
17209 else
17210 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017211 name = NULL;
17212 }
17213 goto theend;
17214 }
17215
17216 if (lv.ll_name == NULL)
17217 {
17218 /* Error found, but continue after the function name. */
17219 *pp = end;
17220 goto theend;
17221 }
17222
17223 if (lv.ll_exp_name != NULL)
17224 len = STRLEN(lv.ll_exp_name);
17225 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017226 {
17227 if (lead == 2) /* skip over "s:" */
17228 lv.ll_name += 2;
17229 len = (int)(end - lv.ll_name);
17230 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017231
17232 /*
17233 * Copy the function name to allocated memory.
17234 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17235 * Accept <SNR>123_name() outside a script.
17236 */
17237 if (skip)
17238 lead = 0; /* do nothing */
17239 else if (lead > 0)
17240 {
17241 lead = 3;
17242 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17243 {
17244 if (current_SID <= 0)
17245 {
17246 EMSG(_(e_usingsid));
17247 goto theend;
17248 }
17249 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17250 lead += (int)STRLEN(sid_buf);
17251 }
17252 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017253 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017254 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017255 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017256 goto theend;
17257 }
17258 name = alloc((unsigned)(len + lead + 1));
17259 if (name != NULL)
17260 {
17261 if (lead > 0)
17262 {
17263 name[0] = K_SPECIAL;
17264 name[1] = KS_EXTRA;
17265 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017266 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017267 STRCPY(name + 3, sid_buf);
17268 }
17269 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17270 name[len + lead] = NUL;
17271 }
17272 *pp = end;
17273
17274theend:
17275 clear_lval(&lv);
17276 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277}
17278
17279/*
17280 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17281 * Return 2 if "p" starts with "s:".
17282 * Return 0 otherwise.
17283 */
17284 static int
17285eval_fname_script(p)
17286 char_u *p;
17287{
17288 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17289 || STRNICMP(p + 1, "SNR>", 4) == 0))
17290 return 5;
17291 if (p[0] == 's' && p[1] == ':')
17292 return 2;
17293 return 0;
17294}
17295
17296/*
17297 * Return TRUE if "p" starts with "<SID>" or "s:".
17298 * Only works if eval_fname_script() returned non-zero for "p"!
17299 */
17300 static int
17301eval_fname_sid(p)
17302 char_u *p;
17303{
17304 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17305}
17306
17307/*
17308 * List the head of the function: "name(arg1, arg2)".
17309 */
17310 static void
17311list_func_head(fp, indent)
17312 ufunc_T *fp;
17313 int indent;
17314{
17315 int j;
17316
17317 msg_start();
17318 if (indent)
17319 MSG_PUTS(" ");
17320 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017321 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322 {
17323 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017324 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017325 }
17326 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017327 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017329 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 {
17331 if (j)
17332 MSG_PUTS(", ");
17333 msg_puts(FUNCARG(fp, j));
17334 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017335 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336 {
17337 if (j)
17338 MSG_PUTS(", ");
17339 MSG_PUTS("...");
17340 }
17341 msg_putchar(')');
17342}
17343
17344/*
17345 * Find a function by name, return pointer to it in ufuncs.
17346 * Return NULL for unknown function.
17347 */
17348 static ufunc_T *
17349find_func(name)
17350 char_u *name;
17351{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017352 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017354 hi = hash_find(&func_hashtab, name);
17355 if (!HASHITEM_EMPTY(hi))
17356 return HI2UF(hi);
17357 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358}
17359
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017360#if defined(EXITFREE) || defined(PROTO)
17361 void
17362free_all_functions()
17363{
17364 hashitem_T *hi;
17365
17366 /* Need to start all over every time, because func_free() may change the
17367 * hash table. */
17368 while (func_hashtab.ht_used > 0)
17369 for (hi = func_hashtab.ht_array; ; ++hi)
17370 if (!HASHITEM_EMPTY(hi))
17371 {
17372 func_free(HI2UF(hi));
17373 break;
17374 }
17375}
17376#endif
17377
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017378/*
17379 * Return TRUE if a function "name" exists.
17380 */
17381 static int
17382function_exists(name)
17383 char_u *name;
17384{
17385 char_u *p = name;
17386 int n = FALSE;
17387
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017388 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017389 if (p != NULL)
17390 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017391 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017392 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017393 else
17394 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017395 vim_free(p);
17396 }
17397 return n;
17398}
17399
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017400/*
17401 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017402 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017403 */
17404 static int
17405builtin_function(name)
17406 char_u *name;
17407{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017408 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17409 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017410}
17411
Bram Moolenaar05159a02005-02-26 23:04:13 +000017412#if defined(FEAT_PROFILE) || defined(PROTO)
17413/*
17414 * Start profiling function "fp".
17415 */
17416 static void
17417func_do_profile(fp)
17418 ufunc_T *fp;
17419{
17420 fp->uf_tm_count = 0;
17421 profile_zero(&fp->uf_tm_self);
17422 profile_zero(&fp->uf_tm_total);
17423 if (fp->uf_tml_count == NULL)
17424 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17425 (sizeof(int) * fp->uf_lines.ga_len));
17426 if (fp->uf_tml_total == NULL)
17427 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17428 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17429 if (fp->uf_tml_self == NULL)
17430 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17431 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17432 fp->uf_tml_idx = -1;
17433 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17434 || fp->uf_tml_self == NULL)
17435 return; /* out of memory */
17436
17437 fp->uf_profiling = TRUE;
17438}
17439
17440/*
17441 * Dump the profiling results for all functions in file "fd".
17442 */
17443 void
17444func_dump_profile(fd)
17445 FILE *fd;
17446{
17447 hashitem_T *hi;
17448 int todo;
17449 ufunc_T *fp;
17450 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017451 ufunc_T **sorttab;
17452 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017453
17454 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017455 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17456
Bram Moolenaar05159a02005-02-26 23:04:13 +000017457 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17458 {
17459 if (!HASHITEM_EMPTY(hi))
17460 {
17461 --todo;
17462 fp = HI2UF(hi);
17463 if (fp->uf_profiling)
17464 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017465 if (sorttab != NULL)
17466 sorttab[st_len++] = fp;
17467
Bram Moolenaar05159a02005-02-26 23:04:13 +000017468 if (fp->uf_name[0] == K_SPECIAL)
17469 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17470 else
17471 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17472 if (fp->uf_tm_count == 1)
17473 fprintf(fd, "Called 1 time\n");
17474 else
17475 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17476 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17477 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17478 fprintf(fd, "\n");
17479 fprintf(fd, "count total (s) self (s)\n");
17480
17481 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17482 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017483 prof_func_line(fd, fp->uf_tml_count[i],
17484 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017485 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17486 }
17487 fprintf(fd, "\n");
17488 }
17489 }
17490 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017491
17492 if (sorttab != NULL && st_len > 0)
17493 {
17494 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17495 prof_total_cmp);
17496 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17497 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17498 prof_self_cmp);
17499 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17500 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017501}
Bram Moolenaar73830342005-02-28 22:48:19 +000017502
17503 static void
17504prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17505 FILE *fd;
17506 ufunc_T **sorttab;
17507 int st_len;
17508 char *title;
17509 int prefer_self; /* when equal print only self time */
17510{
17511 int i;
17512 ufunc_T *fp;
17513
17514 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17515 fprintf(fd, "count total (s) self (s) function\n");
17516 for (i = 0; i < 20 && i < st_len; ++i)
17517 {
17518 fp = sorttab[i];
17519 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17520 prefer_self);
17521 if (fp->uf_name[0] == K_SPECIAL)
17522 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17523 else
17524 fprintf(fd, " %s()\n", fp->uf_name);
17525 }
17526 fprintf(fd, "\n");
17527}
17528
17529/*
17530 * Print the count and times for one function or function line.
17531 */
17532 static void
17533prof_func_line(fd, count, total, self, prefer_self)
17534 FILE *fd;
17535 int count;
17536 proftime_T *total;
17537 proftime_T *self;
17538 int prefer_self; /* when equal print only self time */
17539{
17540 if (count > 0)
17541 {
17542 fprintf(fd, "%5d ", count);
17543 if (prefer_self && profile_equal(total, self))
17544 fprintf(fd, " ");
17545 else
17546 fprintf(fd, "%s ", profile_msg(total));
17547 if (!prefer_self && profile_equal(total, self))
17548 fprintf(fd, " ");
17549 else
17550 fprintf(fd, "%s ", profile_msg(self));
17551 }
17552 else
17553 fprintf(fd, " ");
17554}
17555
17556/*
17557 * Compare function for total time sorting.
17558 */
17559 static int
17560#ifdef __BORLANDC__
17561_RTLENTRYF
17562#endif
17563prof_total_cmp(s1, s2)
17564 const void *s1;
17565 const void *s2;
17566{
17567 ufunc_T *p1, *p2;
17568
17569 p1 = *(ufunc_T **)s1;
17570 p2 = *(ufunc_T **)s2;
17571 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17572}
17573
17574/*
17575 * Compare function for self time sorting.
17576 */
17577 static int
17578#ifdef __BORLANDC__
17579_RTLENTRYF
17580#endif
17581prof_self_cmp(s1, s2)
17582 const void *s1;
17583 const void *s2;
17584{
17585 ufunc_T *p1, *p2;
17586
17587 p1 = *(ufunc_T **)s1;
17588 p2 = *(ufunc_T **)s2;
17589 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17590}
17591
Bram Moolenaar05159a02005-02-26 23:04:13 +000017592#endif
17593
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017594/* The names of packages that once were loaded is remembered. */
17595static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
17596
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017597/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017598 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017599 * Return TRUE if a package was loaded.
17600 */
17601 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017602script_autoload(name, reload)
17603 char_u *name;
17604 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017605{
17606 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017607 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017608 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017609 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017610
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017611 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017612 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017613 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017614 return FALSE;
17615
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017616 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017617
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017618 /* Find the name in the list of previously loaded package names. Skip
17619 * "autoload/", it's always the same. */
17620 for (i = 0; i < ga_loaded.ga_len; ++i)
17621 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
17622 break;
17623 if (!reload && i < ga_loaded.ga_len)
17624 ret = FALSE; /* was loaded already */
17625 else
17626 {
17627 /* Remember the name if it wasn't loaded already. */
17628 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
17629 {
17630 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
17631 tofree = NULL;
17632 }
17633
17634 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
17635 if (cmd_runtime(scriptname, FALSE) == OK)
17636 ret = TRUE;
17637 }
17638
17639 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017640 return ret;
17641}
17642
17643/*
17644 * Return the autoload script name for a function or variable name.
17645 * Returns NULL when out of memory.
17646 */
17647 static char_u *
17648autoload_name(name)
17649 char_u *name;
17650{
17651 char_u *p;
17652 char_u *scriptname;
17653
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017654 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017655 scriptname = alloc((unsigned)(STRLEN(name) + 14));
17656 if (scriptname == NULL)
17657 return FALSE;
17658 STRCPY(scriptname, "autoload/");
17659 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017660 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017661 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017662 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017663 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017664 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017665}
17666
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
17668
17669/*
17670 * Function given to ExpandGeneric() to obtain the list of user defined
17671 * function names.
17672 */
17673 char_u *
17674get_user_func_name(xp, idx)
17675 expand_T *xp;
17676 int idx;
17677{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017678 static long_u done;
17679 static hashitem_T *hi;
17680 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681
17682 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017684 done = 0;
17685 hi = func_hashtab.ht_array;
17686 }
17687 if (done < func_hashtab.ht_used)
17688 {
17689 if (done++ > 0)
17690 ++hi;
17691 while (HASHITEM_EMPTY(hi))
17692 ++hi;
17693 fp = HI2UF(hi);
17694
17695 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
17696 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697
17698 cat_func_name(IObuff, fp);
17699 if (xp->xp_context != EXPAND_USER_FUNC)
17700 {
17701 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017702 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703 STRCAT(IObuff, ")");
17704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 return IObuff;
17706 }
17707 return NULL;
17708}
17709
17710#endif /* FEAT_CMDL_COMPL */
17711
17712/*
17713 * Copy the function name of "fp" to buffer "buf".
17714 * "buf" must be able to hold the function name plus three bytes.
17715 * Takes care of script-local function names.
17716 */
17717 static void
17718cat_func_name(buf, fp)
17719 char_u *buf;
17720 ufunc_T *fp;
17721{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017722 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723 {
17724 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017725 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 }
17727 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017728 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729}
17730
17731/*
17732 * ":delfunction {name}"
17733 */
17734 void
17735ex_delfunction(eap)
17736 exarg_T *eap;
17737{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017738 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739 char_u *p;
17740 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017741 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742
17743 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017744 name = trans_function_name(&p, eap->skip, 0, &fudi);
17745 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017747 {
17748 if (fudi.fd_dict != NULL && !eap->skip)
17749 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752 if (!ends_excmd(*skipwhite(p)))
17753 {
17754 vim_free(name);
17755 EMSG(_(e_trailing));
17756 return;
17757 }
17758 eap->nextcmd = check_nextcmd(p);
17759 if (eap->nextcmd != NULL)
17760 *p = NUL;
17761
17762 if (!eap->skip)
17763 fp = find_func(name);
17764 vim_free(name);
17765
17766 if (!eap->skip)
17767 {
17768 if (fp == NULL)
17769 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017770 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771 return;
17772 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017773 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 {
17775 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
17776 return;
17777 }
17778
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017779 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017781 /* Delete the dict item that refers to the function, it will
17782 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017783 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017785 else
17786 func_free(fp);
17787 }
17788}
17789
17790/*
17791 * Free a function and remove it from the list of functions.
17792 */
17793 static void
17794func_free(fp)
17795 ufunc_T *fp;
17796{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017797 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017798
17799 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017800 ga_clear_strings(&(fp->uf_args));
17801 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000017802#ifdef FEAT_PROFILE
17803 vim_free(fp->uf_tml_count);
17804 vim_free(fp->uf_tml_total);
17805 vim_free(fp->uf_tml_self);
17806#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017807
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017808 /* remove the function from the function hashtable */
17809 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
17810 if (HASHITEM_EMPTY(hi))
17811 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017812 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017813 hash_remove(&func_hashtab, hi);
17814
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017815 vim_free(fp);
17816}
17817
17818/*
17819 * Unreference a Function: decrement the reference count and free it when it
17820 * becomes zero. Only for numbered functions.
17821 */
17822 static void
17823func_unref(name)
17824 char_u *name;
17825{
17826 ufunc_T *fp;
17827
17828 if (name != NULL && isdigit(*name))
17829 {
17830 fp = find_func(name);
17831 if (fp == NULL)
17832 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017833 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017834 {
17835 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017836 * when "uf_calls" becomes zero. */
17837 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017838 func_free(fp);
17839 }
17840 }
17841}
17842
17843/*
17844 * Count a reference to a Function.
17845 */
17846 static void
17847func_ref(name)
17848 char_u *name;
17849{
17850 ufunc_T *fp;
17851
17852 if (name != NULL && isdigit(*name))
17853 {
17854 fp = find_func(name);
17855 if (fp == NULL)
17856 EMSG2(_(e_intern2), "func_ref()");
17857 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017858 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859 }
17860}
17861
17862/*
17863 * Call a user function.
17864 */
17865 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000017866call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 ufunc_T *fp; /* pointer to function */
17868 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000017869 typval_T *argvars; /* arguments */
17870 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871 linenr_T firstline; /* first line of range */
17872 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000017873 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874{
Bram Moolenaar33570922005-01-25 22:26:29 +000017875 char_u *save_sourcing_name;
17876 linenr_T save_sourcing_lnum;
17877 scid_T save_current_SID;
17878 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000017879 int save_did_emsg;
17880 static int depth = 0;
17881 dictitem_T *v;
17882 int fixvar_idx = 0; /* index in fixvar[] */
17883 int i;
17884 int ai;
17885 char_u numbuf[NUMBUFLEN];
17886 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017887#ifdef FEAT_PROFILE
17888 proftime_T wait_start;
17889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890
17891 /* If depth of calling is getting too high, don't execute the function */
17892 if (depth >= p_mfd)
17893 {
17894 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017895 rettv->v_type = VAR_NUMBER;
17896 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897 return;
17898 }
17899 ++depth;
17900
17901 line_breakcheck(); /* check for CTRL-C hit */
17902
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017903 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000017904 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017906 fc.rettv = rettv;
17907 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908 fc.linenr = 0;
17909 fc.returned = FALSE;
17910 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017912 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017913 fc.dbg_tick = debug_tick;
17914
Bram Moolenaar33570922005-01-25 22:26:29 +000017915 /*
17916 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
17917 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
17918 * each argument variable and saves a lot of time.
17919 */
17920 /*
17921 * Init l: variables.
17922 */
17923 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000017924 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017925 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017926 /* Set l:self to "selfdict". */
17927 v = &fc.fixvar[fixvar_idx++].var;
17928 STRCPY(v->di_key, "self");
17929 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
17930 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
17931 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017932 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000017933 v->di_tv.vval.v_dict = selfdict;
17934 ++selfdict->dv_refcount;
17935 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017936
Bram Moolenaar33570922005-01-25 22:26:29 +000017937 /*
17938 * Init a: variables.
17939 * Set a:0 to "argcount".
17940 * Set a:000 to a list with room for the "..." arguments.
17941 */
17942 init_var_dict(&fc.l_avars, &fc.l_avars_var);
17943 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017944 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000017945 v = &fc.fixvar[fixvar_idx++].var;
17946 STRCPY(v->di_key, "000");
17947 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17948 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17949 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017950 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017951 v->di_tv.vval.v_list = &fc.l_varlist;
17952 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
17953 fc.l_varlist.lv_refcount = 99999;
17954
17955 /*
17956 * Set a:firstline to "firstline" and a:lastline to "lastline".
17957 * Set a:name to named arguments.
17958 * Set a:N to the "..." arguments.
17959 */
17960 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
17961 (varnumber_T)firstline);
17962 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
17963 (varnumber_T)lastline);
17964 for (i = 0; i < argcount; ++i)
17965 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017966 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017967 if (ai < 0)
17968 /* named argument a:name */
17969 name = FUNCARG(fp, i);
17970 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017971 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017972 /* "..." argument a:1, a:2, etc. */
17973 sprintf((char *)numbuf, "%d", ai + 1);
17974 name = numbuf;
17975 }
17976 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
17977 {
17978 v = &fc.fixvar[fixvar_idx++].var;
17979 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17980 }
17981 else
17982 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017983 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17984 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000017985 if (v == NULL)
17986 break;
17987 v->di_flags = DI_FLAGS_RO;
17988 }
17989 STRCPY(v->di_key, name);
17990 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17991
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017992 /* Note: the values are copied directly to avoid alloc/free.
17993 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017994 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017995 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017996
17997 if (ai >= 0 && ai < MAX_FUNC_ARGS)
17998 {
17999 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18000 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018001 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018002 }
18003 }
18004
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005 /* Don't redraw while executing the function. */
18006 ++RedrawingDisabled;
18007 save_sourcing_name = sourcing_name;
18008 save_sourcing_lnum = sourcing_lnum;
18009 sourcing_lnum = 1;
18010 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018011 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018012 if (sourcing_name != NULL)
18013 {
18014 if (save_sourcing_name != NULL
18015 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18016 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18017 else
18018 STRCPY(sourcing_name, "function ");
18019 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18020
18021 if (p_verbose >= 12)
18022 {
18023 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018024 verbose_enter_scroll();
18025
Bram Moolenaar555b2802005-05-19 21:08:39 +000018026 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027 if (p_verbose >= 14)
18028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018030 char_u numbuf[NUMBUFLEN];
18031 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032
18033 msg_puts((char_u *)"(");
18034 for (i = 0; i < argcount; ++i)
18035 {
18036 if (i > 0)
18037 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018038 if (argvars[i].v_type == VAR_NUMBER)
18039 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 else
18041 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018042 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018043 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018045 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 }
18047 }
18048 msg_puts((char_u *)")");
18049 }
18050 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018051
18052 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 --no_wait_return;
18054 }
18055 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018056#ifdef FEAT_PROFILE
18057 if (do_profiling)
18058 {
18059 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18060 func_do_profile(fp);
18061 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018062 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018063 {
18064 ++fp->uf_tm_count;
18065 profile_start(&fp->uf_tm_start);
18066 profile_zero(&fp->uf_tm_children);
18067 }
18068 script_prof_save(&wait_start);
18069 }
18070#endif
18071
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018073 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 save_did_emsg = did_emsg;
18075 did_emsg = FALSE;
18076
18077 /* call do_cmdline() to execute the lines */
18078 do_cmdline(NULL, get_func_line, (void *)&fc,
18079 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18080
18081 --RedrawingDisabled;
18082
18083 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018084 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018086 clear_tv(rettv);
18087 rettv->v_type = VAR_NUMBER;
18088 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089 }
18090
Bram Moolenaar05159a02005-02-26 23:04:13 +000018091#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018092 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018093 {
18094 profile_end(&fp->uf_tm_start);
18095 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18096 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18097 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18098 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018099 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018100 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018101 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18102 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018103 }
18104 }
18105#endif
18106
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107 /* when being verbose, mention the return value */
18108 if (p_verbose >= 12)
18109 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018111 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018114 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018115 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018116 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18117 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018118 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018120 char_u buf[MSG_BUF_LEN];
18121 char_u numbuf[NUMBUFLEN];
18122 char_u *tofree;
18123
Bram Moolenaar555b2802005-05-19 21:08:39 +000018124 /* The value may be very long. Skip the middle part, so that we
18125 * have some idea how it starts and ends. smsg() would always
18126 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018127 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018128 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018129 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018130 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 }
18132 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018133
18134 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135 --no_wait_return;
18136 }
18137
18138 vim_free(sourcing_name);
18139 sourcing_name = save_sourcing_name;
18140 sourcing_lnum = save_sourcing_lnum;
18141 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018142#ifdef FEAT_PROFILE
18143 if (do_profiling)
18144 script_prof_restore(&wait_start);
18145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146
18147 if (p_verbose >= 12 && sourcing_name != NULL)
18148 {
18149 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018150 verbose_enter_scroll();
18151
Bram Moolenaar555b2802005-05-19 21:08:39 +000018152 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018154
18155 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156 --no_wait_return;
18157 }
18158
18159 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018160 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161
Bram Moolenaar33570922005-01-25 22:26:29 +000018162 /* The a: variables typevals were not alloced, only free the allocated
18163 * variables. */
18164 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18165
18166 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167 --depth;
18168}
18169
18170/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018171 * Add a number variable "name" to dict "dp" with value "nr".
18172 */
18173 static void
18174add_nr_var(dp, v, name, nr)
18175 dict_T *dp;
18176 dictitem_T *v;
18177 char *name;
18178 varnumber_T nr;
18179{
18180 STRCPY(v->di_key, name);
18181 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18182 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18183 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018184 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018185 v->di_tv.vval.v_number = nr;
18186}
18187
18188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189 * ":return [expr]"
18190 */
18191 void
18192ex_return(eap)
18193 exarg_T *eap;
18194{
18195 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018196 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197 int returning = FALSE;
18198
18199 if (current_funccal == NULL)
18200 {
18201 EMSG(_("E133: :return not inside a function"));
18202 return;
18203 }
18204
18205 if (eap->skip)
18206 ++emsg_skip;
18207
18208 eap->nextcmd = NULL;
18209 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018210 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018211 {
18212 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018213 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018215 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 }
18217 /* It's safer to return also on error. */
18218 else if (!eap->skip)
18219 {
18220 /*
18221 * Return unless the expression evaluation has been cancelled due to an
18222 * aborting error, an interrupt, or an exception.
18223 */
18224 if (!aborting())
18225 returning = do_return(eap, FALSE, TRUE, NULL);
18226 }
18227
18228 /* When skipping or the return gets pending, advance to the next command
18229 * in this line (!returning). Otherwise, ignore the rest of the line.
18230 * Following lines will be ignored by get_func_line(). */
18231 if (returning)
18232 eap->nextcmd = NULL;
18233 else if (eap->nextcmd == NULL) /* no argument */
18234 eap->nextcmd = check_nextcmd(arg);
18235
18236 if (eap->skip)
18237 --emsg_skip;
18238}
18239
18240/*
18241 * Return from a function. Possibly makes the return pending. Also called
18242 * for a pending return at the ":endtry" or after returning from an extra
18243 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018244 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018245 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246 * FALSE when the return gets pending.
18247 */
18248 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018249do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250 exarg_T *eap;
18251 int reanimate;
18252 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018253 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254{
18255 int idx;
18256 struct condstack *cstack = eap->cstack;
18257
18258 if (reanimate)
18259 /* Undo the return. */
18260 current_funccal->returned = FALSE;
18261
18262 /*
18263 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18264 * not in its finally clause (which then is to be executed next) is found.
18265 * In this case, make the ":return" pending for execution at the ":endtry".
18266 * Otherwise, return normally.
18267 */
18268 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18269 if (idx >= 0)
18270 {
18271 cstack->cs_pending[idx] = CSTP_RETURN;
18272
18273 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018274 /* A pending return again gets pending. "rettv" points to an
18275 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018277 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278 else
18279 {
18280 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018281 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018283 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018285 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286 {
18287 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018288 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018289 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290 else
18291 EMSG(_(e_outofmem));
18292 }
18293 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018294 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295
18296 if (reanimate)
18297 {
18298 /* The pending return value could be overwritten by a ":return"
18299 * without argument in a finally clause; reset the default
18300 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018301 current_funccal->rettv->v_type = VAR_NUMBER;
18302 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303 }
18304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018305 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306 }
18307 else
18308 {
18309 current_funccal->returned = TRUE;
18310
18311 /* If the return is carried out now, store the return value. For
18312 * a return immediately after reanimation, the value is already
18313 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018314 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018316 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018317 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018319 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320 }
18321 }
18322
18323 return idx < 0;
18324}
18325
18326/*
18327 * Free the variable with a pending return value.
18328 */
18329 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018330discard_pending_return(rettv)
18331 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332{
Bram Moolenaar33570922005-01-25 22:26:29 +000018333 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334}
18335
18336/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018337 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338 * is an allocated string. Used by report_pending() for verbose messages.
18339 */
18340 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018341get_return_cmd(rettv)
18342 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018344 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018345 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018346 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018348 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018349 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018350 if (s == NULL)
18351 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018352
18353 STRCPY(IObuff, ":return ");
18354 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18355 if (STRLEN(s) + 8 >= IOSIZE)
18356 STRCPY(IObuff + IOSIZE - 4, "...");
18357 vim_free(tofree);
18358 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359}
18360
18361/*
18362 * Get next function line.
18363 * Called by do_cmdline() to get the next line.
18364 * Returns allocated string, or NULL for end of function.
18365 */
18366/* ARGSUSED */
18367 char_u *
18368get_func_line(c, cookie, indent)
18369 int c; /* not used */
18370 void *cookie;
18371 int indent; /* not used */
18372{
Bram Moolenaar33570922005-01-25 22:26:29 +000018373 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018374 ufunc_T *fp = fcp->func;
18375 char_u *retval;
18376 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377
18378 /* If breakpoints have been added/deleted need to check for it. */
18379 if (fcp->dbg_tick != debug_tick)
18380 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018381 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 sourcing_lnum);
18383 fcp->dbg_tick = debug_tick;
18384 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018385#ifdef FEAT_PROFILE
18386 if (do_profiling)
18387 func_line_end(cookie);
18388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389
Bram Moolenaar05159a02005-02-26 23:04:13 +000018390 gap = &fp->uf_lines;
18391 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392 retval = NULL;
18393 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18394 retval = NULL;
18395 else
18396 {
18397 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18398 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018399#ifdef FEAT_PROFILE
18400 if (do_profiling)
18401 func_line_start(cookie);
18402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403 }
18404
18405 /* Did we encounter a breakpoint? */
18406 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18407 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018408 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018410 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411 sourcing_lnum);
18412 fcp->dbg_tick = debug_tick;
18413 }
18414
18415 return retval;
18416}
18417
Bram Moolenaar05159a02005-02-26 23:04:13 +000018418#if defined(FEAT_PROFILE) || defined(PROTO)
18419/*
18420 * Called when starting to read a function line.
18421 * "sourcing_lnum" must be correct!
18422 * When skipping lines it may not actually be executed, but we won't find out
18423 * until later and we need to store the time now.
18424 */
18425 void
18426func_line_start(cookie)
18427 void *cookie;
18428{
18429 funccall_T *fcp = (funccall_T *)cookie;
18430 ufunc_T *fp = fcp->func;
18431
18432 if (fp->uf_profiling && sourcing_lnum >= 1
18433 && sourcing_lnum <= fp->uf_lines.ga_len)
18434 {
18435 fp->uf_tml_idx = sourcing_lnum - 1;
18436 fp->uf_tml_execed = FALSE;
18437 profile_start(&fp->uf_tml_start);
18438 profile_zero(&fp->uf_tml_children);
18439 profile_get_wait(&fp->uf_tml_wait);
18440 }
18441}
18442
18443/*
18444 * Called when actually executing a function line.
18445 */
18446 void
18447func_line_exec(cookie)
18448 void *cookie;
18449{
18450 funccall_T *fcp = (funccall_T *)cookie;
18451 ufunc_T *fp = fcp->func;
18452
18453 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18454 fp->uf_tml_execed = TRUE;
18455}
18456
18457/*
18458 * Called when done with a function line.
18459 */
18460 void
18461func_line_end(cookie)
18462 void *cookie;
18463{
18464 funccall_T *fcp = (funccall_T *)cookie;
18465 ufunc_T *fp = fcp->func;
18466
18467 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18468 {
18469 if (fp->uf_tml_execed)
18470 {
18471 ++fp->uf_tml_count[fp->uf_tml_idx];
18472 profile_end(&fp->uf_tml_start);
18473 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18474 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18475 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18476 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18477 }
18478 fp->uf_tml_idx = -1;
18479 }
18480}
18481#endif
18482
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483/*
18484 * Return TRUE if the currently active function should be ended, because a
18485 * return was encountered or an error occured. Used inside a ":while".
18486 */
18487 int
18488func_has_ended(cookie)
18489 void *cookie;
18490{
Bram Moolenaar33570922005-01-25 22:26:29 +000018491 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492
18493 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18494 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018495 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496 || fcp->returned);
18497}
18498
18499/*
18500 * return TRUE if cookie indicates a function which "abort"s on errors.
18501 */
18502 int
18503func_has_abort(cookie)
18504 void *cookie;
18505{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018506 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018507}
18508
18509#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18510typedef enum
18511{
18512 VAR_FLAVOUR_DEFAULT,
18513 VAR_FLAVOUR_SESSION,
18514 VAR_FLAVOUR_VIMINFO
18515} var_flavour_T;
18516
18517static var_flavour_T var_flavour __ARGS((char_u *varname));
18518
18519 static var_flavour_T
18520var_flavour(varname)
18521 char_u *varname;
18522{
18523 char_u *p = varname;
18524
18525 if (ASCII_ISUPPER(*p))
18526 {
18527 while (*(++p))
18528 if (ASCII_ISLOWER(*p))
18529 return VAR_FLAVOUR_SESSION;
18530 return VAR_FLAVOUR_VIMINFO;
18531 }
18532 else
18533 return VAR_FLAVOUR_DEFAULT;
18534}
18535#endif
18536
18537#if defined(FEAT_VIMINFO) || defined(PROTO)
18538/*
18539 * Restore global vars that start with a capital from the viminfo file
18540 */
18541 int
18542read_viminfo_varlist(virp, writing)
18543 vir_T *virp;
18544 int writing;
18545{
18546 char_u *tab;
18547 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018548 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549
18550 if (!writing && (find_viminfo_parameter('!') != NULL))
18551 {
18552 tab = vim_strchr(virp->vir_line + 1, '\t');
18553 if (tab != NULL)
18554 {
18555 *tab++ = '\0'; /* isolate the variable name */
18556 if (*tab == 'S') /* string var */
18557 is_string = TRUE;
18558
18559 tab = vim_strchr(tab, '\t');
18560 if (tab != NULL)
18561 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562 if (is_string)
18563 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018564 tv.v_type = VAR_STRING;
18565 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 }
18568 else
18569 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018570 tv.v_type = VAR_NUMBER;
18571 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018573 set_var(virp->vir_line + 1, &tv, FALSE);
18574 if (is_string)
18575 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 }
18577 }
18578 }
18579
18580 return viminfo_readline(virp);
18581}
18582
18583/*
18584 * Write global vars that start with a capital to the viminfo file
18585 */
18586 void
18587write_viminfo_varlist(fp)
18588 FILE *fp;
18589{
Bram Moolenaar33570922005-01-25 22:26:29 +000018590 hashitem_T *hi;
18591 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018592 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018593 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018594 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018595 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018596 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597
18598 if (find_viminfo_parameter('!') == NULL)
18599 return;
18600
18601 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018602
Bram Moolenaar33570922005-01-25 22:26:29 +000018603 todo = globvarht.ht_used;
18604 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018606 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018608 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018609 this_var = HI2DI(hi);
18610 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018611 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018612 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018613 {
18614 case VAR_STRING: s = "STR"; break;
18615 case VAR_NUMBER: s = "NUM"; break;
18616 default: continue;
18617 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018618 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018619 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18620 if (p != NULL)
18621 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018622 vim_free(tofree);
18623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 }
18625 }
18626}
18627#endif
18628
18629#if defined(FEAT_SESSION) || defined(PROTO)
18630 int
18631store_session_globals(fd)
18632 FILE *fd;
18633{
Bram Moolenaar33570922005-01-25 22:26:29 +000018634 hashitem_T *hi;
18635 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018636 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637 char_u *p, *t;
18638
Bram Moolenaar33570922005-01-25 22:26:29 +000018639 todo = globvarht.ht_used;
18640 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018642 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018644 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018645 this_var = HI2DI(hi);
18646 if ((this_var->di_tv.v_type == VAR_NUMBER
18647 || this_var->di_tv.v_type == VAR_STRING)
18648 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018649 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018650 /* Escape special characters with a backslash. Turn a LF and
18651 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018652 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018653 (char_u *)"\\\"\n\r");
18654 if (p == NULL) /* out of memory */
18655 break;
18656 for (t = p; *t != NUL; ++t)
18657 if (*t == '\n')
18658 *t = 'n';
18659 else if (*t == '\r')
18660 *t = 'r';
18661 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000018662 this_var->di_key,
18663 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18664 : ' ',
18665 p,
18666 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18667 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000018668 || put_eol(fd) == FAIL)
18669 {
18670 vim_free(p);
18671 return FAIL;
18672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673 vim_free(p);
18674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 }
18676 }
18677 return OK;
18678}
18679#endif
18680
18681#endif /* FEAT_EVAL */
18682
18683#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
18684
18685
18686#ifdef WIN3264
18687/*
18688 * Functions for ":8" filename modifier: get 8.3 version of a filename.
18689 */
18690static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18691static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
18692static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18693
18694/*
18695 * Get the short pathname of a file.
18696 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
18697 */
18698 static int
18699get_short_pathname(fnamep, bufp, fnamelen)
18700 char_u **fnamep;
18701 char_u **bufp;
18702 int *fnamelen;
18703{
18704 int l,len;
18705 char_u *newbuf;
18706
18707 len = *fnamelen;
18708
18709 l = GetShortPathName(*fnamep, *fnamep, len);
18710 if (l > len - 1)
18711 {
18712 /* If that doesn't work (not enough space), then save the string
18713 * and try again with a new buffer big enough
18714 */
18715 newbuf = vim_strnsave(*fnamep, l);
18716 if (newbuf == NULL)
18717 return 0;
18718
18719 vim_free(*bufp);
18720 *fnamep = *bufp = newbuf;
18721
18722 l = GetShortPathName(*fnamep,*fnamep,l+1);
18723
18724 /* Really should always succeed, as the buffer is big enough */
18725 }
18726
18727 *fnamelen = l;
18728 return 1;
18729}
18730
18731/*
18732 * Create a short path name. Returns the length of the buffer it needs.
18733 * Doesn't copy over the end of the buffer passed in.
18734 */
18735 static int
18736shortpath_for_invalid_fname(fname, bufp, fnamelen)
18737 char_u **fname;
18738 char_u **bufp;
18739 int *fnamelen;
18740{
18741 char_u *s, *p, *pbuf2, *pbuf3;
18742 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018743 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744
18745 /* Make a copy */
18746 len2 = *fnamelen;
18747 pbuf2 = vim_strnsave(*fname, len2);
18748 pbuf3 = NULL;
18749
18750 s = pbuf2 + len2 - 1; /* Find the end */
18751 slen = 1;
18752 plen = len2;
18753
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018754 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755 {
18756 --s;
18757 ++slen;
18758 --plen;
18759 }
18760
18761 do
18762 {
18763 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018764 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 {
18766 --s;
18767 ++slen;
18768 --plen;
18769 }
18770 if (s <= pbuf2)
18771 break;
18772
18773 /* Remeber the character that is about to be blatted */
18774 ch = *s;
18775 *s = 0; /* get_short_pathname requires a null-terminated string */
18776
18777 /* Try it in situ */
18778 p = pbuf2;
18779 if (!get_short_pathname(&p, &pbuf3, &plen))
18780 {
18781 vim_free(pbuf2);
18782 return -1;
18783 }
18784 *s = ch; /* Preserve the string */
18785 } while (plen == 0);
18786
18787 if (plen > 0)
18788 {
18789 /* Remeber the length of the new string. */
18790 *fnamelen = len = plen + slen;
18791 vim_free(*bufp);
18792 if (len > len2)
18793 {
18794 /* If there's not enough space in the currently allocated string,
18795 * then copy it to a buffer big enough.
18796 */
18797 *fname= *bufp = vim_strnsave(p, len);
18798 if (*fname == NULL)
18799 return -1;
18800 }
18801 else
18802 {
18803 /* Transfer pbuf2 to being the main buffer (it's big enough) */
18804 *fname = *bufp = pbuf2;
18805 if (p != pbuf2)
18806 strncpy(*fname, p, plen);
18807 pbuf2 = NULL;
18808 }
18809 /* Concat the next bit */
18810 strncpy(*fname + plen, s, slen);
18811 (*fname)[len] = '\0';
18812 }
18813 vim_free(pbuf3);
18814 vim_free(pbuf2);
18815 return 0;
18816}
18817
18818/*
18819 * Get a pathname for a partial path.
18820 */
18821 static int
18822shortpath_for_partial(fnamep, bufp, fnamelen)
18823 char_u **fnamep;
18824 char_u **bufp;
18825 int *fnamelen;
18826{
18827 int sepcount, len, tflen;
18828 char_u *p;
18829 char_u *pbuf, *tfname;
18830 int hasTilde;
18831
18832 /* Count up the path seperators from the RHS.. so we know which part
18833 * of the path to return.
18834 */
18835 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018836 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 if (vim_ispathsep(*p))
18838 ++sepcount;
18839
18840 /* Need full path first (use expand_env() to remove a "~/") */
18841 hasTilde = (**fnamep == '~');
18842 if (hasTilde)
18843 pbuf = tfname = expand_env_save(*fnamep);
18844 else
18845 pbuf = tfname = FullName_save(*fnamep, FALSE);
18846
18847 len = tflen = STRLEN(tfname);
18848
18849 if (!get_short_pathname(&tfname, &pbuf, &len))
18850 return -1;
18851
18852 if (len == 0)
18853 {
18854 /* Don't have a valid filename, so shorten the rest of the
18855 * path if we can. This CAN give us invalid 8.3 filenames, but
18856 * there's not a lot of point in guessing what it might be.
18857 */
18858 len = tflen;
18859 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
18860 return -1;
18861 }
18862
18863 /* Count the paths backward to find the beginning of the desired string. */
18864 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018865 {
18866#ifdef FEAT_MBYTE
18867 if (has_mbyte)
18868 p -= mb_head_off(tfname, p);
18869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870 if (vim_ispathsep(*p))
18871 {
18872 if (sepcount == 0 || (hasTilde && sepcount == 1))
18873 break;
18874 else
18875 sepcount --;
18876 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878 if (hasTilde)
18879 {
18880 --p;
18881 if (p >= tfname)
18882 *p = '~';
18883 else
18884 return -1;
18885 }
18886 else
18887 ++p;
18888
18889 /* Copy in the string - p indexes into tfname - allocated at pbuf */
18890 vim_free(*bufp);
18891 *fnamelen = (int)STRLEN(p);
18892 *bufp = pbuf;
18893 *fnamep = p;
18894
18895 return 0;
18896}
18897#endif /* WIN3264 */
18898
18899/*
18900 * Adjust a filename, according to a string of modifiers.
18901 * *fnamep must be NUL terminated when called. When returning, the length is
18902 * determined by *fnamelen.
18903 * Returns valid flags.
18904 * When there is an error, *fnamep is set to NULL.
18905 */
18906 int
18907modify_fname(src, usedlen, fnamep, bufp, fnamelen)
18908 char_u *src; /* string with modifiers */
18909 int *usedlen; /* characters after src that are used */
18910 char_u **fnamep; /* file name so far */
18911 char_u **bufp; /* buffer for allocated file name or NULL */
18912 int *fnamelen; /* length of fnamep */
18913{
18914 int valid = 0;
18915 char_u *tail;
18916 char_u *s, *p, *pbuf;
18917 char_u dirname[MAXPATHL];
18918 int c;
18919 int has_fullname = 0;
18920#ifdef WIN3264
18921 int has_shortname = 0;
18922#endif
18923
18924repeat:
18925 /* ":p" - full path/file_name */
18926 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
18927 {
18928 has_fullname = 1;
18929
18930 valid |= VALID_PATH;
18931 *usedlen += 2;
18932
18933 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
18934 if ((*fnamep)[0] == '~'
18935#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
18936 && ((*fnamep)[1] == '/'
18937# ifdef BACKSLASH_IN_FILENAME
18938 || (*fnamep)[1] == '\\'
18939# endif
18940 || (*fnamep)[1] == NUL)
18941
18942#endif
18943 )
18944 {
18945 *fnamep = expand_env_save(*fnamep);
18946 vim_free(*bufp); /* free any allocated file name */
18947 *bufp = *fnamep;
18948 if (*fnamep == NULL)
18949 return -1;
18950 }
18951
18952 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018953 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954 {
18955 if (vim_ispathsep(*p)
18956 && p[1] == '.'
18957 && (p[2] == NUL
18958 || vim_ispathsep(p[2])
18959 || (p[2] == '.'
18960 && (p[3] == NUL || vim_ispathsep(p[3])))))
18961 break;
18962 }
18963
18964 /* FullName_save() is slow, don't use it when not needed. */
18965 if (*p != NUL || !vim_isAbsName(*fnamep))
18966 {
18967 *fnamep = FullName_save(*fnamep, *p != NUL);
18968 vim_free(*bufp); /* free any allocated file name */
18969 *bufp = *fnamep;
18970 if (*fnamep == NULL)
18971 return -1;
18972 }
18973
18974 /* Append a path separator to a directory. */
18975 if (mch_isdir(*fnamep))
18976 {
18977 /* Make room for one or two extra characters. */
18978 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
18979 vim_free(*bufp); /* free any allocated file name */
18980 *bufp = *fnamep;
18981 if (*fnamep == NULL)
18982 return -1;
18983 add_pathsep(*fnamep);
18984 }
18985 }
18986
18987 /* ":." - path relative to the current directory */
18988 /* ":~" - path relative to the home directory */
18989 /* ":8" - shortname path - postponed till after */
18990 while (src[*usedlen] == ':'
18991 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
18992 {
18993 *usedlen += 2;
18994 if (c == '8')
18995 {
18996#ifdef WIN3264
18997 has_shortname = 1; /* Postpone this. */
18998#endif
18999 continue;
19000 }
19001 pbuf = NULL;
19002 /* Need full path first (use expand_env() to remove a "~/") */
19003 if (!has_fullname)
19004 {
19005 if (c == '.' && **fnamep == '~')
19006 p = pbuf = expand_env_save(*fnamep);
19007 else
19008 p = pbuf = FullName_save(*fnamep, FALSE);
19009 }
19010 else
19011 p = *fnamep;
19012
19013 has_fullname = 0;
19014
19015 if (p != NULL)
19016 {
19017 if (c == '.')
19018 {
19019 mch_dirname(dirname, MAXPATHL);
19020 s = shorten_fname(p, dirname);
19021 if (s != NULL)
19022 {
19023 *fnamep = s;
19024 if (pbuf != NULL)
19025 {
19026 vim_free(*bufp); /* free any allocated file name */
19027 *bufp = pbuf;
19028 pbuf = NULL;
19029 }
19030 }
19031 }
19032 else
19033 {
19034 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19035 /* Only replace it when it starts with '~' */
19036 if (*dirname == '~')
19037 {
19038 s = vim_strsave(dirname);
19039 if (s != NULL)
19040 {
19041 *fnamep = s;
19042 vim_free(*bufp);
19043 *bufp = s;
19044 }
19045 }
19046 }
19047 vim_free(pbuf);
19048 }
19049 }
19050
19051 tail = gettail(*fnamep);
19052 *fnamelen = (int)STRLEN(*fnamep);
19053
19054 /* ":h" - head, remove "/file_name", can be repeated */
19055 /* Don't remove the first "/" or "c:\" */
19056 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19057 {
19058 valid |= VALID_HEAD;
19059 *usedlen += 2;
19060 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019061 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062 --tail;
19063 *fnamelen = (int)(tail - *fnamep);
19064#ifdef VMS
19065 if (*fnamelen > 0)
19066 *fnamelen += 1; /* the path separator is part of the path */
19067#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019068 while (tail > s && !after_pathsep(s, tail))
19069 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070 }
19071
19072 /* ":8" - shortname */
19073 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19074 {
19075 *usedlen += 2;
19076#ifdef WIN3264
19077 has_shortname = 1;
19078#endif
19079 }
19080
19081#ifdef WIN3264
19082 /* Check shortname after we have done 'heads' and before we do 'tails'
19083 */
19084 if (has_shortname)
19085 {
19086 pbuf = NULL;
19087 /* Copy the string if it is shortened by :h */
19088 if (*fnamelen < (int)STRLEN(*fnamep))
19089 {
19090 p = vim_strnsave(*fnamep, *fnamelen);
19091 if (p == 0)
19092 return -1;
19093 vim_free(*bufp);
19094 *bufp = *fnamep = p;
19095 }
19096
19097 /* Split into two implementations - makes it easier. First is where
19098 * there isn't a full name already, second is where there is.
19099 */
19100 if (!has_fullname && !vim_isAbsName(*fnamep))
19101 {
19102 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19103 return -1;
19104 }
19105 else
19106 {
19107 int l;
19108
19109 /* Simple case, already have the full-name
19110 * Nearly always shorter, so try first time. */
19111 l = *fnamelen;
19112 if (!get_short_pathname(fnamep, bufp, &l))
19113 return -1;
19114
19115 if (l == 0)
19116 {
19117 /* Couldn't find the filename.. search the paths.
19118 */
19119 l = *fnamelen;
19120 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19121 return -1;
19122 }
19123 *fnamelen = l;
19124 }
19125 }
19126#endif /* WIN3264 */
19127
19128 /* ":t" - tail, just the basename */
19129 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19130 {
19131 *usedlen += 2;
19132 *fnamelen -= (int)(tail - *fnamep);
19133 *fnamep = tail;
19134 }
19135
19136 /* ":e" - extension, can be repeated */
19137 /* ":r" - root, without extension, can be repeated */
19138 while (src[*usedlen] == ':'
19139 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19140 {
19141 /* find a '.' in the tail:
19142 * - for second :e: before the current fname
19143 * - otherwise: The last '.'
19144 */
19145 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19146 s = *fnamep - 2;
19147 else
19148 s = *fnamep + *fnamelen - 1;
19149 for ( ; s > tail; --s)
19150 if (s[0] == '.')
19151 break;
19152 if (src[*usedlen + 1] == 'e') /* :e */
19153 {
19154 if (s > tail)
19155 {
19156 *fnamelen += (int)(*fnamep - (s + 1));
19157 *fnamep = s + 1;
19158#ifdef VMS
19159 /* cut version from the extension */
19160 s = *fnamep + *fnamelen - 1;
19161 for ( ; s > *fnamep; --s)
19162 if (s[0] == ';')
19163 break;
19164 if (s > *fnamep)
19165 *fnamelen = s - *fnamep;
19166#endif
19167 }
19168 else if (*fnamep <= tail)
19169 *fnamelen = 0;
19170 }
19171 else /* :r */
19172 {
19173 if (s > tail) /* remove one extension */
19174 *fnamelen = (int)(s - *fnamep);
19175 }
19176 *usedlen += 2;
19177 }
19178
19179 /* ":s?pat?foo?" - substitute */
19180 /* ":gs?pat?foo?" - global substitute */
19181 if (src[*usedlen] == ':'
19182 && (src[*usedlen + 1] == 's'
19183 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19184 {
19185 char_u *str;
19186 char_u *pat;
19187 char_u *sub;
19188 int sep;
19189 char_u *flags;
19190 int didit = FALSE;
19191
19192 flags = (char_u *)"";
19193 s = src + *usedlen + 2;
19194 if (src[*usedlen + 1] == 'g')
19195 {
19196 flags = (char_u *)"g";
19197 ++s;
19198 }
19199
19200 sep = *s++;
19201 if (sep)
19202 {
19203 /* find end of pattern */
19204 p = vim_strchr(s, sep);
19205 if (p != NULL)
19206 {
19207 pat = vim_strnsave(s, (int)(p - s));
19208 if (pat != NULL)
19209 {
19210 s = p + 1;
19211 /* find end of substitution */
19212 p = vim_strchr(s, sep);
19213 if (p != NULL)
19214 {
19215 sub = vim_strnsave(s, (int)(p - s));
19216 str = vim_strnsave(*fnamep, *fnamelen);
19217 if (sub != NULL && str != NULL)
19218 {
19219 *usedlen = (int)(p + 1 - src);
19220 s = do_string_sub(str, pat, sub, flags);
19221 if (s != NULL)
19222 {
19223 *fnamep = s;
19224 *fnamelen = (int)STRLEN(s);
19225 vim_free(*bufp);
19226 *bufp = s;
19227 didit = TRUE;
19228 }
19229 }
19230 vim_free(sub);
19231 vim_free(str);
19232 }
19233 vim_free(pat);
19234 }
19235 }
19236 /* after using ":s", repeat all the modifiers */
19237 if (didit)
19238 goto repeat;
19239 }
19240 }
19241
19242 return valid;
19243}
19244
19245/*
19246 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19247 * "flags" can be "g" to do a global substitute.
19248 * Returns an allocated string, NULL for error.
19249 */
19250 char_u *
19251do_string_sub(str, pat, sub, flags)
19252 char_u *str;
19253 char_u *pat;
19254 char_u *sub;
19255 char_u *flags;
19256{
19257 int sublen;
19258 regmatch_T regmatch;
19259 int i;
19260 int do_all;
19261 char_u *tail;
19262 garray_T ga;
19263 char_u *ret;
19264 char_u *save_cpo;
19265
19266 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19267 save_cpo = p_cpo;
19268 p_cpo = (char_u *)"";
19269
19270 ga_init2(&ga, 1, 200);
19271
19272 do_all = (flags[0] == 'g');
19273
19274 regmatch.rm_ic = p_ic;
19275 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19276 if (regmatch.regprog != NULL)
19277 {
19278 tail = str;
19279 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19280 {
19281 /*
19282 * Get some space for a temporary buffer to do the substitution
19283 * into. It will contain:
19284 * - The text up to where the match is.
19285 * - The substituted text.
19286 * - The text after the match.
19287 */
19288 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19289 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19290 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19291 {
19292 ga_clear(&ga);
19293 break;
19294 }
19295
19296 /* copy the text up to where the match is */
19297 i = (int)(regmatch.startp[0] - tail);
19298 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19299 /* add the substituted text */
19300 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19301 + ga.ga_len + i, TRUE, TRUE, FALSE);
19302 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303 /* avoid getting stuck on a match with an empty string */
19304 if (tail == regmatch.endp[0])
19305 {
19306 if (*tail == NUL)
19307 break;
19308 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19309 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310 }
19311 else
19312 {
19313 tail = regmatch.endp[0];
19314 if (*tail == NUL)
19315 break;
19316 }
19317 if (!do_all)
19318 break;
19319 }
19320
19321 if (ga.ga_data != NULL)
19322 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19323
19324 vim_free(regmatch.regprog);
19325 }
19326
19327 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19328 ga_clear(&ga);
19329 p_cpo = save_cpo;
19330
19331 return ret;
19332}
19333
19334#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */