blob: ae36e6f30a4a2a94bdf5ea472773fc0f3d0309d4 [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));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000646static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static char_u *get_tv_string __ARGS((typval_T *varp));
648static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000649static char_u *get_tv_string_chk __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000650static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000652static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
654static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
655static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
656static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
657static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
658static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
659static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000660static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000662static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
664static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
665static int eval_fname_script __ARGS((char_u *p));
666static int eval_fname_sid __ARGS((char_u *p));
667static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static ufunc_T *find_func __ARGS((char_u *name));
669static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000670static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000671#ifdef FEAT_PROFILE
672static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000673static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
674static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
675static int
676# ifdef __BORLANDC__
677 _RTLENTRYF
678# endif
679 prof_total_cmp __ARGS((const void *s1, const void *s2));
680static int
681# ifdef __BORLANDC__
682 _RTLENTRYF
683# endif
684 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000685#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000686static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000687static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000688static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static void func_free __ARGS((ufunc_T *fp));
690static void func_unref __ARGS((char_u *name));
691static void func_ref __ARGS((char_u *name));
692static 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));
693static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
694
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000695/* Character used as separated in autoload function/variable names. */
696#define AUTOLOAD_CHAR '#'
697
Bram Moolenaar33570922005-01-25 22:26:29 +0000698/*
699 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000700 */
701 void
702eval_init()
703{
Bram Moolenaar33570922005-01-25 22:26:29 +0000704 int i;
705 struct vimvar *p;
706
707 init_var_dict(&globvardict, &globvars_var);
708 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000709 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000710 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000711
712 for (i = 0; i < VV_LEN; ++i)
713 {
714 p = &vimvars[i];
715 STRCPY(p->vv_di.di_key, p->vv_name);
716 if (p->vv_flags & VV_RO)
717 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
718 else if (p->vv_flags & VV_RO_SBX)
719 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
720 else
721 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000722
723 /* add to v: scope dict, unless the value is not always available */
724 if (p->vv_type != VAR_UNKNOWN)
725 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000726 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000727 /* add to compat scope dict */
728 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000729 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000730}
731
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000732#if defined(EXITFREE) || defined(PROTO)
733 void
734eval_clear()
735{
736 int i;
737 struct vimvar *p;
738
739 for (i = 0; i < VV_LEN; ++i)
740 {
741 p = &vimvars[i];
742 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000743 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000744 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000745 p->vv_di.di_tv.vval.v_string = NULL;
746 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000747 }
748 hash_clear(&vimvarht);
749 hash_clear(&compat_hashtab);
750
751 /* script-local variables */
752 for (i = 1; i <= ga_scripts.ga_len; ++i)
753 vars_clear(&SCRIPT_VARS(i));
754 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000755 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000756
757 /* global variables */
758 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000759
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000760 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000761 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000762 hash_clear(&func_hashtab);
763
764 /* unreferenced lists and dicts */
765 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000766}
767#endif
768
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 * Return the name of the executed function.
771 */
772 char_u *
773func_name(cookie)
774 void *cookie;
775{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000776 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777}
778
779/*
780 * Return the address holding the next breakpoint line for a funccall cookie.
781 */
782 linenr_T *
783func_breakpoint(cookie)
784 void *cookie;
785{
Bram Moolenaar33570922005-01-25 22:26:29 +0000786 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787}
788
789/*
790 * Return the address holding the debug tick for a funccall cookie.
791 */
792 int *
793func_dbg_tick(cookie)
794 void *cookie;
795{
Bram Moolenaar33570922005-01-25 22:26:29 +0000796 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797}
798
799/*
800 * Return the nesting level for a funccall cookie.
801 */
802 int
803func_level(cookie)
804 void *cookie;
805{
Bram Moolenaar33570922005-01-25 22:26:29 +0000806 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807}
808
809/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000810funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812/*
813 * Return TRUE when a function was ended by a ":return" command.
814 */
815 int
816current_func_returned()
817{
818 return current_funccal->returned;
819}
820
821
822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 * Set an internal variable to a string value. Creates the variable if it does
824 * not already exist.
825 */
826 void
827set_internal_string_var(name, value)
828 char_u *name;
829 char_u *value;
830{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000831 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000832 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
834 val = vim_strsave(value);
835 if (val != NULL)
836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000837 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000838 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000840 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000841 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 }
843 }
844}
845
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000846static lval_T *redir_lval = NULL;
847static char_u *redir_endp = NULL;
848static char_u *redir_varname = NULL;
849
850/*
851 * Start recording command output to a variable
852 * Returns OK if successfully completed the setup. FAIL otherwise.
853 */
854 int
855var_redir_start(name, append)
856 char_u *name;
857 int append; /* append to an existing variable */
858{
859 int save_emsg;
860 int err;
861 typval_T tv;
862
863 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000864 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000865 {
866 EMSG(_(e_invarg));
867 return FAIL;
868 }
869
870 redir_varname = vim_strsave(name);
871 if (redir_varname == NULL)
872 return FAIL;
873
874 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
875 if (redir_lval == NULL)
876 {
877 var_redir_stop();
878 return FAIL;
879 }
880
881 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000882 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
883 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000884 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
885 {
886 if (redir_endp != NULL && *redir_endp != NUL)
887 /* Trailing characters are present after the variable name */
888 EMSG(_(e_trailing));
889 else
890 EMSG(_(e_invarg));
891 var_redir_stop();
892 return FAIL;
893 }
894
895 /* check if we can write to the variable: set it to or append an empty
896 * string */
897 save_emsg = did_emsg;
898 did_emsg = FALSE;
899 tv.v_type = VAR_STRING;
900 tv.vval.v_string = (char_u *)"";
901 if (append)
902 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
903 else
904 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
905 err = did_emsg;
906 did_emsg += save_emsg;
907 if (err)
908 {
909 var_redir_stop();
910 return FAIL;
911 }
912 if (redir_lval->ll_newkey != NULL)
913 {
914 /* Dictionary item was created, don't do it again. */
915 vim_free(redir_lval->ll_newkey);
916 redir_lval->ll_newkey = NULL;
917 }
918
919 return OK;
920}
921
922/*
923 * Append "value[len]" to the variable set by var_redir_start().
924 */
925 void
926var_redir_str(value, len)
927 char_u *value;
928 int len;
929{
930 char_u *val;
931 typval_T tv;
932 int save_emsg;
933 int err;
934
935 if (redir_lval == NULL)
936 return;
937
938 if (len == -1)
939 /* Append the entire string */
940 val = vim_strsave(value);
941 else
942 /* Append only the specified number of characters */
943 val = vim_strnsave(value, len);
944 if (val == NULL)
945 return;
946
947 tv.v_type = VAR_STRING;
948 tv.vval.v_string = val;
949
950 save_emsg = did_emsg;
951 did_emsg = FALSE;
952 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
953 err = did_emsg;
954 did_emsg += save_emsg;
955 if (err)
956 var_redir_stop();
957
958 vim_free(tv.vval.v_string);
959}
960
961/*
962 * Stop redirecting command output to a variable.
963 */
964 void
965var_redir_stop()
966{
967 if (redir_lval != NULL)
968 {
969 clear_lval(redir_lval);
970 vim_free(redir_lval);
971 redir_lval = NULL;
972 }
973 vim_free(redir_varname);
974 redir_varname = NULL;
975}
976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977# if defined(FEAT_MBYTE) || defined(PROTO)
978 int
979eval_charconvert(enc_from, enc_to, fname_from, fname_to)
980 char_u *enc_from;
981 char_u *enc_to;
982 char_u *fname_from;
983 char_u *fname_to;
984{
985 int err = FALSE;
986
987 set_vim_var_string(VV_CC_FROM, enc_from, -1);
988 set_vim_var_string(VV_CC_TO, enc_to, -1);
989 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
990 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
991 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
992 err = TRUE;
993 set_vim_var_string(VV_CC_FROM, NULL, -1);
994 set_vim_var_string(VV_CC_TO, NULL, -1);
995 set_vim_var_string(VV_FNAME_IN, NULL, -1);
996 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
997
998 if (err)
999 return FAIL;
1000 return OK;
1001}
1002# endif
1003
1004# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1005 int
1006eval_printexpr(fname, args)
1007 char_u *fname;
1008 char_u *args;
1009{
1010 int err = FALSE;
1011
1012 set_vim_var_string(VV_FNAME_IN, fname, -1);
1013 set_vim_var_string(VV_CMDARG, args, -1);
1014 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1015 err = TRUE;
1016 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1017 set_vim_var_string(VV_CMDARG, NULL, -1);
1018
1019 if (err)
1020 {
1021 mch_remove(fname);
1022 return FAIL;
1023 }
1024 return OK;
1025}
1026# endif
1027
1028# if defined(FEAT_DIFF) || defined(PROTO)
1029 void
1030eval_diff(origfile, newfile, outfile)
1031 char_u *origfile;
1032 char_u *newfile;
1033 char_u *outfile;
1034{
1035 int err = FALSE;
1036
1037 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1038 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1039 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1040 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1041 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1042 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1043 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1044}
1045
1046 void
1047eval_patch(origfile, difffile, outfile)
1048 char_u *origfile;
1049 char_u *difffile;
1050 char_u *outfile;
1051{
1052 int err;
1053
1054 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1055 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1056 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1057 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1058 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1059 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1060 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1061}
1062# endif
1063
1064/*
1065 * Top level evaluation function, returning a boolean.
1066 * Sets "error" to TRUE if there was an error.
1067 * Return TRUE or FALSE.
1068 */
1069 int
1070eval_to_bool(arg, error, nextcmd, skip)
1071 char_u *arg;
1072 int *error;
1073 char_u **nextcmd;
1074 int skip; /* only parse, don't execute */
1075{
Bram Moolenaar33570922005-01-25 22:26:29 +00001076 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 int retval = FALSE;
1078
1079 if (skip)
1080 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001081 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 else
1084 {
1085 *error = FALSE;
1086 if (!skip)
1087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001088 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001089 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 }
1091 }
1092 if (skip)
1093 --emsg_skip;
1094
1095 return retval;
1096}
1097
1098/*
1099 * Top level evaluation function, returning a string. If "skip" is TRUE,
1100 * only parsing to "nextcmd" is done, without reporting errors. Return
1101 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1102 */
1103 char_u *
1104eval_to_string_skip(arg, nextcmd, skip)
1105 char_u *arg;
1106 char_u **nextcmd;
1107 int skip; /* only parse, don't execute */
1108{
Bram Moolenaar33570922005-01-25 22:26:29 +00001109 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 char_u *retval;
1111
1112 if (skip)
1113 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001114 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 retval = NULL;
1116 else
1117 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001118 retval = vim_strsave(get_tv_string(&tv));
1119 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 }
1121 if (skip)
1122 --emsg_skip;
1123
1124 return retval;
1125}
1126
1127/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001128 * Skip over an expression at "*pp".
1129 * Return FAIL for an error, OK otherwise.
1130 */
1131 int
1132skip_expr(pp)
1133 char_u **pp;
1134{
Bram Moolenaar33570922005-01-25 22:26:29 +00001135 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001136
1137 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001138 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001139}
1140
1141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 * Top level evaluation function, returning a string.
1143 * Return pointer to allocated memory, or NULL for failure.
1144 */
1145 char_u *
1146eval_to_string(arg, nextcmd)
1147 char_u *arg;
1148 char_u **nextcmd;
1149{
Bram Moolenaar33570922005-01-25 22:26:29 +00001150 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 char_u *retval;
1152
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 retval = NULL;
1155 else
1156 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001157 retval = vim_strsave(get_tv_string(&tv));
1158 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 }
1160
1161 return retval;
1162}
1163
1164/*
1165 * Call eval_to_string() with "sandbox" set and not using local variables.
1166 */
1167 char_u *
1168eval_to_string_safe(arg, nextcmd)
1169 char_u *arg;
1170 char_u **nextcmd;
1171{
1172 char_u *retval;
1173 void *save_funccalp;
1174
1175 save_funccalp = save_funccal();
1176 ++sandbox;
1177 retval = eval_to_string(arg, nextcmd);
1178 --sandbox;
1179 restore_funccal(save_funccalp);
1180 return retval;
1181}
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183/*
1184 * Top level evaluation function, returning a number.
1185 * Evaluates "expr" silently.
1186 * Returns -1 for an error.
1187 */
1188 int
1189eval_to_number(expr)
1190 char_u *expr;
1191{
Bram Moolenaar33570922005-01-25 22:26:29 +00001192 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001194 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195
1196 ++emsg_off;
1197
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001198 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 retval = -1;
1200 else
1201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001202 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001203 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205 --emsg_off;
1206
1207 return retval;
1208}
1209
Bram Moolenaara40058a2005-07-11 22:42:07 +00001210/*
1211 * Prepare v: variable "idx" to be used.
1212 * Save the current typeval in "save_tv".
1213 * When not used yet add the variable to the v: hashtable.
1214 */
1215 static void
1216prepare_vimvar(idx, save_tv)
1217 int idx;
1218 typval_T *save_tv;
1219{
1220 *save_tv = vimvars[idx].vv_tv;
1221 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1222 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1223}
1224
1225/*
1226 * Restore v: variable "idx" to typeval "save_tv".
1227 * When no longer defined, remove the variable from the v: hashtable.
1228 */
1229 static void
1230restore_vimvar(idx, save_tv)
1231 int idx;
1232 typval_T *save_tv;
1233{
1234 hashitem_T *hi;
1235
1236 clear_tv(&vimvars[idx].vv_tv);
1237 vimvars[idx].vv_tv = *save_tv;
1238 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1239 {
1240 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1241 if (HASHITEM_EMPTY(hi))
1242 EMSG2(_(e_intern2), "restore_vimvar()");
1243 else
1244 hash_remove(&vimvarht, hi);
1245 }
1246}
1247
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001248#if defined(FEAT_SYN_HL) || defined(PROTO)
1249/*
1250 * Evaluate an expression to a list with suggestions.
1251 * For the "expr:" part of 'spellsuggest'.
1252 */
1253 list_T *
1254eval_spell_expr(badword, expr)
1255 char_u *badword;
1256 char_u *expr;
1257{
1258 typval_T save_val;
1259 typval_T rettv;
1260 list_T *list = NULL;
1261 char_u *p = skipwhite(expr);
1262
1263 /* Set "v:val" to the bad word. */
1264 prepare_vimvar(VV_VAL, &save_val);
1265 vimvars[VV_VAL].vv_type = VAR_STRING;
1266 vimvars[VV_VAL].vv_str = badword;
1267 if (p_verbose == 0)
1268 ++emsg_off;
1269
1270 if (eval1(&p, &rettv, TRUE) == OK)
1271 {
1272 if (rettv.v_type != VAR_LIST)
1273 clear_tv(&rettv);
1274 else
1275 list = rettv.vval.v_list;
1276 }
1277
1278 if (p_verbose == 0)
1279 --emsg_off;
1280 vimvars[VV_VAL].vv_str = NULL;
1281 restore_vimvar(VV_VAL, &save_val);
1282
1283 return list;
1284}
1285
1286/*
1287 * "list" is supposed to contain two items: a word and a number. Return the
1288 * word in "pp" and the number as the return value.
1289 * Return -1 if anything isn't right.
1290 * Used to get the good word and score from the eval_spell_expr() result.
1291 */
1292 int
1293get_spellword(list, pp)
1294 list_T *list;
1295 char_u **pp;
1296{
1297 listitem_T *li;
1298
1299 li = list->lv_first;
1300 if (li == NULL)
1301 return -1;
1302 *pp = get_tv_string(&li->li_tv);
1303
1304 li = li->li_next;
1305 if (li == NULL)
1306 return -1;
1307 return get_tv_number(&li->li_tv);
1308}
1309#endif
1310
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001311/*
1312 * Top level evaluation function,
1313 */
1314 typval_T *
1315eval_expr(arg, nextcmd)
1316 char_u *arg;
1317 char_u **nextcmd;
1318{
1319 typval_T *tv;
1320
1321 tv = (typval_T *)alloc(sizeof(typval_T));
1322 if (!tv)
1323 return NULL;
1324
1325 if (eval0(arg, tv, nextcmd, TRUE) == FAIL)
1326 {
1327 vim_free(tv);
1328 return NULL;
1329 }
1330
1331 return tv;
1332}
1333
1334
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1336/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001337 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001339 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001341 static int
1342call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 char_u *func;
1344 int argc;
1345 char_u **argv;
1346 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
Bram Moolenaar33570922005-01-25 22:26:29 +00001349 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 long n;
1351 int len;
1352 int i;
1353 int doesrange;
1354 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001355 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
Bram Moolenaar33570922005-01-25 22:26:29 +00001357 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001359 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
1361 for (i = 0; i < argc; i++)
1362 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001363 /* Pass a NULL or empty argument as an empty string */
1364 if (argv[i] == NULL || *argv[i] == NUL)
1365 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001366 argvars[i].v_type = VAR_STRING;
1367 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001368 continue;
1369 }
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 /* Recognize a number argument, the others must be strings. */
1372 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1373 if (len != 0 && len == (int)STRLEN(argv[i]))
1374 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001375 argvars[i].v_type = VAR_NUMBER;
1376 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 }
1378 else
1379 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001380 argvars[i].v_type = VAR_STRING;
1381 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
1383 }
1384
1385 if (safe)
1386 {
1387 save_funccalp = save_funccal();
1388 ++sandbox;
1389 }
1390
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001391 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1392 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001394 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if (safe)
1396 {
1397 --sandbox;
1398 restore_funccal(save_funccalp);
1399 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001400 vim_free(argvars);
1401
1402 if (ret == FAIL)
1403 clear_tv(rettv);
1404
1405 return ret;
1406}
1407
1408/*
1409 * Call some vimL function and return the result as a string
1410 * Uses argv[argc] for the function arguments.
1411 */
1412 void *
1413call_func_retstr(func, argc, argv, safe)
1414 char_u *func;
1415 int argc;
1416 char_u **argv;
1417 int safe; /* use the sandbox */
1418{
1419 typval_T rettv;
1420 char_u *retval = NULL;
1421
1422 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1423 return NULL;
1424
1425 retval = vim_strsave(get_tv_string(&rettv));
1426
1427 clear_tv(&rettv);
1428
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 return retval;
1430}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001431
1432/*
1433 * Call some vimL function and return the result as a list
1434 * Uses argv[argc] for the function arguments.
1435 */
1436 void *
1437call_func_retlist(func, argc, argv, safe)
1438 char_u *func;
1439 int argc;
1440 char_u **argv;
1441 int safe; /* use the sandbox */
1442{
1443 typval_T rettv;
1444
1445 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1446 return NULL;
1447
1448 if (rettv.v_type != VAR_LIST)
1449 {
1450 clear_tv(&rettv);
1451 return NULL;
1452 }
1453
1454 return rettv.vval.v_list;
1455}
1456
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457#endif
1458
1459/*
1460 * Save the current function call pointer, and set it to NULL.
1461 * Used when executing autocommands and for ":source".
1462 */
1463 void *
1464save_funccal()
1465{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001466 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 current_funccal = NULL;
1469 return (void *)fc;
1470}
1471
1472 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001473restore_funccal(vfc)
1474 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001476 funccall_T *fc = (funccall_T *)vfc;
1477
1478 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479}
1480
Bram Moolenaar05159a02005-02-26 23:04:13 +00001481#if defined(FEAT_PROFILE) || defined(PROTO)
1482/*
1483 * Prepare profiling for entering a child or something else that is not
1484 * counted for the script/function itself.
1485 * Should always be called in pair with prof_child_exit().
1486 */
1487 void
1488prof_child_enter(tm)
1489 proftime_T *tm; /* place to store waittime */
1490{
1491 funccall_T *fc = current_funccal;
1492
1493 if (fc != NULL && fc->func->uf_profiling)
1494 profile_start(&fc->prof_child);
1495 script_prof_save(tm);
1496}
1497
1498/*
1499 * Take care of time spent in a child.
1500 * Should always be called after prof_child_enter().
1501 */
1502 void
1503prof_child_exit(tm)
1504 proftime_T *tm; /* where waittime was stored */
1505{
1506 funccall_T *fc = current_funccal;
1507
1508 if (fc != NULL && fc->func->uf_profiling)
1509 {
1510 profile_end(&fc->prof_child);
1511 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1512 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1513 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1514 }
1515 script_prof_restore(tm);
1516}
1517#endif
1518
1519
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520#ifdef FEAT_FOLDING
1521/*
1522 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1523 * it in "*cp". Doesn't give error messages.
1524 */
1525 int
1526eval_foldexpr(arg, cp)
1527 char_u *arg;
1528 int *cp;
1529{
Bram Moolenaar33570922005-01-25 22:26:29 +00001530 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 int retval;
1532 char_u *s;
1533
1534 ++emsg_off;
1535 ++sandbox;
1536 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001537 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 retval = 0;
1539 else
1540 {
1541 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001542 if (tv.v_type == VAR_NUMBER)
1543 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001544 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 retval = 0;
1546 else
1547 {
1548 /* If the result is a string, check if there is a non-digit before
1549 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001550 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 if (!VIM_ISDIGIT(*s) && *s != '-')
1552 *cp = *s++;
1553 retval = atol((char *)s);
1554 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001555 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 }
1557 --emsg_off;
1558 --sandbox;
1559
1560 return retval;
1561}
1562#endif
1563
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001565 * ":let" list all variable values
1566 * ":let var1 var2" list variable values
1567 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001568 * ":let var += expr" assignment command.
1569 * ":let var -= expr" assignment command.
1570 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001571 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 */
1573 void
1574ex_let(eap)
1575 exarg_T *eap;
1576{
1577 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001578 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001579 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001581 int var_count = 0;
1582 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001583 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001585 expr = skip_var_list(arg, &var_count, &semicolon);
1586 if (expr == NULL)
1587 return;
1588 expr = vim_strchr(expr, '=');
1589 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001591 /*
1592 * ":let" without "=": list variables
1593 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001594 if (*arg == '[')
1595 EMSG(_(e_invarg));
1596 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001597 /* ":let var1 var2" */
1598 arg = list_arg_vars(eap, arg);
1599 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001600 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001601 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001602 list_glob_vars();
1603 list_buf_vars();
1604 list_win_vars();
1605 list_vim_vars();
1606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 eap->nextcmd = check_nextcmd(arg);
1608 }
1609 else
1610 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001611 op[0] = '=';
1612 op[1] = NUL;
1613 if (expr > arg)
1614 {
1615 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1616 op[0] = expr[-1]; /* +=, -= or .= */
1617 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001618 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 if (eap->skip)
1621 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001622 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (eap->skip)
1624 {
1625 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001626 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 --emsg_skip;
1628 }
1629 else if (i != FAIL)
1630 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001631 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001632 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001633 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
1635 }
1636}
1637
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001638/*
1639 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1640 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001641 * When "nextchars" is not NULL it points to a string with characters that
1642 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1643 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001644 * Returns OK or FAIL;
1645 */
1646 static int
1647ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1648 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001649 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001650 int copy; /* copy values from "tv", don't move */
1651 int semicolon; /* from skip_var_list() */
1652 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001653 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654{
1655 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001656 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001657 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001658 listitem_T *item;
1659 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660
1661 if (*arg != '[')
1662 {
1663 /*
1664 * ":let var = expr" or ":for var in list"
1665 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001666 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667 return FAIL;
1668 return OK;
1669 }
1670
1671 /*
1672 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1673 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001674 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675 {
1676 EMSG(_(e_listreq));
1677 return FAIL;
1678 }
1679
1680 i = list_len(l);
1681 if (semicolon == 0 && var_count < i)
1682 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001683 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001684 return FAIL;
1685 }
1686 if (var_count - semicolon > i)
1687 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001688 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689 return FAIL;
1690 }
1691
1692 item = l->lv_first;
1693 while (*arg != ']')
1694 {
1695 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001696 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 item = item->li_next;
1698 if (arg == NULL)
1699 return FAIL;
1700
1701 arg = skipwhite(arg);
1702 if (*arg == ';')
1703 {
1704 /* Put the rest of the list (may be empty) in the var after ';'.
1705 * Create a new list for this. */
1706 l = list_alloc();
1707 if (l == NULL)
1708 return FAIL;
1709 while (item != NULL)
1710 {
1711 list_append_tv(l, &item->li_tv);
1712 item = item->li_next;
1713 }
1714
1715 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001716 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717 ltv.vval.v_list = l;
1718 l->lv_refcount = 1;
1719
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001720 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1721 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001722 clear_tv(&ltv);
1723 if (arg == NULL)
1724 return FAIL;
1725 break;
1726 }
1727 else if (*arg != ',' && *arg != ']')
1728 {
1729 EMSG2(_(e_intern2), "ex_let_vars()");
1730 return FAIL;
1731 }
1732 }
1733
1734 return OK;
1735}
1736
1737/*
1738 * Skip over assignable variable "var" or list of variables "[var, var]".
1739 * Used for ":let varvar = expr" and ":for varvar in expr".
1740 * For "[var, var]" increment "*var_count" for each variable.
1741 * for "[var, var; var]" set "semicolon".
1742 * Return NULL for an error.
1743 */
1744 static char_u *
1745skip_var_list(arg, var_count, semicolon)
1746 char_u *arg;
1747 int *var_count;
1748 int *semicolon;
1749{
1750 char_u *p, *s;
1751
1752 if (*arg == '[')
1753 {
1754 /* "[var, var]": find the matching ']'. */
1755 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001756 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001757 {
1758 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1759 s = skip_var_one(p);
1760 if (s == p)
1761 {
1762 EMSG2(_(e_invarg2), p);
1763 return NULL;
1764 }
1765 ++*var_count;
1766
1767 p = skipwhite(s);
1768 if (*p == ']')
1769 break;
1770 else if (*p == ';')
1771 {
1772 if (*semicolon == 1)
1773 {
1774 EMSG(_("Double ; in list of variables"));
1775 return NULL;
1776 }
1777 *semicolon = 1;
1778 }
1779 else if (*p != ',')
1780 {
1781 EMSG2(_(e_invarg2), p);
1782 return NULL;
1783 }
1784 }
1785 return p + 1;
1786 }
1787 else
1788 return skip_var_one(arg);
1789}
1790
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001791/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001792 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1793 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001794 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 static char_u *
1796skip_var_one(arg)
1797 char_u *arg;
1798{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001799 if (*arg == '@' && arg[1] != NUL)
1800 return arg + 2;
1801 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1802 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001803}
1804
Bram Moolenaara7043832005-01-21 11:56:39 +00001805/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001806 * List variables for hashtab "ht" with prefix "prefix".
1807 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001808 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001809 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001810list_hashtable_vars(ht, prefix, empty)
1811 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001812 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001813 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001814{
Bram Moolenaar33570922005-01-25 22:26:29 +00001815 hashitem_T *hi;
1816 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001817 int todo;
1818
1819 todo = ht->ht_used;
1820 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1821 {
1822 if (!HASHITEM_EMPTY(hi))
1823 {
1824 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001825 di = HI2DI(hi);
1826 if (empty || di->di_tv.v_type != VAR_STRING
1827 || di->di_tv.vval.v_string != NULL)
1828 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001829 }
1830 }
1831}
1832
1833/*
1834 * List global variables.
1835 */
1836 static void
1837list_glob_vars()
1838{
Bram Moolenaar33570922005-01-25 22:26:29 +00001839 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001840}
1841
1842/*
1843 * List buffer variables.
1844 */
1845 static void
1846list_buf_vars()
1847{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001848 char_u numbuf[NUMBUFLEN];
1849
Bram Moolenaar33570922005-01-25 22:26:29 +00001850 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001851
1852 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1853 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001854}
1855
1856/*
1857 * List window variables.
1858 */
1859 static void
1860list_win_vars()
1861{
Bram Moolenaar33570922005-01-25 22:26:29 +00001862 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001863}
1864
1865/*
1866 * List Vim variables.
1867 */
1868 static void
1869list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870{
Bram Moolenaar33570922005-01-25 22:26:29 +00001871 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872}
1873
1874/*
1875 * List variables in "arg".
1876 */
1877 static char_u *
1878list_arg_vars(eap, arg)
1879 exarg_T *eap;
1880 char_u *arg;
1881{
1882 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001883 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001885 char_u *name_start;
1886 char_u *arg_subsc;
1887 char_u *tofree;
1888 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889
1890 while (!ends_excmd(*arg) && !got_int)
1891 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001892 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001894 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001895 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1896 {
1897 emsg_severe = TRUE;
1898 EMSG(_(e_trailing));
1899 break;
1900 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001902 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001904 /* get_name_len() takes care of expanding curly braces */
1905 name_start = name = arg;
1906 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1907 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001909 /* This is mainly to keep test 49 working: when expanding
1910 * curly braces fails overrule the exception error message. */
1911 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001912 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001913 emsg_severe = TRUE;
1914 EMSG2(_(e_invarg2), arg);
1915 break;
1916 }
1917 error = TRUE;
1918 }
1919 else
1920 {
1921 if (tofree != NULL)
1922 name = tofree;
1923 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 else
1926 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001927 /* handle d.key, l[idx], f(expr) */
1928 arg_subsc = arg;
1929 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001930 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001932 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001933 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001934 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001935 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001936 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001937 case 'g': list_glob_vars(); break;
1938 case 'b': list_buf_vars(); break;
1939 case 'w': list_win_vars(); break;
1940 case 'v': list_vim_vars(); break;
1941 default:
1942 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001943 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001944 }
1945 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001946 {
1947 char_u numbuf[NUMBUFLEN];
1948 char_u *tf;
1949 int c;
1950 char_u *s;
1951
1952 s = echo_string(&tv, &tf, numbuf);
1953 c = *arg;
1954 *arg = NUL;
1955 list_one_var_a((char_u *)"",
1956 arg == arg_subsc ? name : name_start,
1957 tv.v_type, s == NULL ? (char_u *)"" : s);
1958 *arg = c;
1959 vim_free(tf);
1960 }
1961 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001962 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 }
1964 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001965
1966 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001967 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001968
1969 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001970 }
1971
1972 return arg;
1973}
1974
1975/*
1976 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1977 * Returns a pointer to the char just after the var name.
1978 * Returns NULL if there is an error.
1979 */
1980 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001982 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001983 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001984 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001986 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001987{
1988 int c1;
1989 char_u *name;
1990 char_u *p;
1991 char_u *arg_end = NULL;
1992 int len;
1993 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001994 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001995
1996 /*
1997 * ":let $VAR = expr": Set environment variable.
1998 */
1999 if (*arg == '$')
2000 {
2001 /* Find the end of the name. */
2002 ++arg;
2003 name = arg;
2004 len = get_env_len(&arg);
2005 if (len == 0)
2006 EMSG2(_(e_invarg2), name - 1);
2007 else
2008 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002009 if (op != NULL && (*op == '+' || *op == '-'))
2010 EMSG2(_(e_letwrong), op);
2011 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002013 EMSG(_(e_letunexp));
2014 else
2015 {
2016 c1 = name[len];
2017 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002018 p = get_tv_string_chk(tv);
2019 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002020 {
2021 int mustfree = FALSE;
2022 char_u *s = vim_getenv(name, &mustfree);
2023
2024 if (s != NULL)
2025 {
2026 p = tofree = concat_str(s, p);
2027 if (mustfree)
2028 vim_free(s);
2029 }
2030 }
2031 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002032 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002033 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002034 if (STRICMP(name, "HOME") == 0)
2035 init_homedir();
2036 else if (didset_vim && STRICMP(name, "VIM") == 0)
2037 didset_vim = FALSE;
2038 else if (didset_vimruntime
2039 && STRICMP(name, "VIMRUNTIME") == 0)
2040 didset_vimruntime = FALSE;
2041 arg_end = arg;
2042 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002043 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002044 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002045 }
2046 }
2047 }
2048
2049 /*
2050 * ":let &option = expr": Set option value.
2051 * ":let &l:option = expr": Set local option value.
2052 * ":let &g:option = expr": Set global option value.
2053 */
2054 else if (*arg == '&')
2055 {
2056 /* Find the end of the name. */
2057 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002058 if (p == NULL || (endchars != NULL
2059 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002060 EMSG(_(e_letunexp));
2061 else
2062 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002063 long n;
2064 int opt_type;
2065 long numval;
2066 char_u *stringval = NULL;
2067 char_u *s;
2068
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002069 c1 = *p;
2070 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002071
2072 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002073 s = get_tv_string_chk(tv); /* != NULL if number or string */
2074 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002075 {
2076 opt_type = get_option_value(arg, &numval,
2077 &stringval, opt_flags);
2078 if ((opt_type == 1 && *op == '.')
2079 || (opt_type == 0 && *op != '.'))
2080 EMSG2(_(e_letwrong), op);
2081 else
2082 {
2083 if (opt_type == 1) /* number */
2084 {
2085 if (*op == '+')
2086 n = numval + n;
2087 else
2088 n = numval - n;
2089 }
2090 else if (opt_type == 0 && stringval != NULL) /* string */
2091 {
2092 s = concat_str(stringval, s);
2093 vim_free(stringval);
2094 stringval = s;
2095 }
2096 }
2097 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002098 if (s != NULL)
2099 {
2100 set_option_value(arg, n, s, opt_flags);
2101 arg_end = p;
2102 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002103 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002104 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002105 }
2106 }
2107
2108 /*
2109 * ":let @r = expr": Set register contents.
2110 */
2111 else if (*arg == '@')
2112 {
2113 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002114 if (op != NULL && (*op == '+' || *op == '-'))
2115 EMSG2(_(e_letwrong), op);
2116 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002117 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002118 EMSG(_(e_letunexp));
2119 else
2120 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002121 char_u *tofree = NULL;
2122 char_u *s;
2123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002124 p = get_tv_string_chk(tv);
2125 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002126 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002127 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002128 if (s != NULL)
2129 {
2130 p = tofree = concat_str(s, p);
2131 vim_free(s);
2132 }
2133 }
2134 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002135 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002136 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002137 arg_end = arg + 1;
2138 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002139 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140 }
2141 }
2142
2143 /*
2144 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002145 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002146 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002147 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002148 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002149 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002151 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002152 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002154 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2155 EMSG(_(e_letunexp));
2156 else
2157 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002158 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002159 arg_end = p;
2160 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002163 }
2164
2165 else
2166 EMSG2(_(e_invarg2), arg);
2167
2168 return arg_end;
2169}
2170
2171/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002172 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2173 */
2174 static int
2175check_changedtick(arg)
2176 char_u *arg;
2177{
2178 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2179 {
2180 EMSG2(_(e_readonlyvar), arg);
2181 return TRUE;
2182 }
2183 return FALSE;
2184}
2185
2186/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 * Get an lval: variable, Dict item or List item that can be assigned a value
2188 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2189 * "name.key", "name.key[expr]" etc.
2190 * Indexing only works if "name" is an existing List or Dictionary.
2191 * "name" points to the start of the name.
2192 * If "rettv" is not NULL it points to the value to be assigned.
2193 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2194 * wrong; must end in space or cmd separator.
2195 *
2196 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002197 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002198 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 */
2200 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002201get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002203 typval_T *rettv;
2204 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002205 int unlet;
2206 int skip;
2207 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002208 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 char_u *expr_start, *expr_end;
2212 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002213 dictitem_T *v;
2214 typval_T var1;
2215 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002217 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002218 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002219 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002220 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002222 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002223 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002224
2225 if (skip)
2226 {
2227 /* When skipping just find the end of the name. */
2228 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002229 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002230 }
2231
2232 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002233 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 if (expr_start != NULL)
2235 {
2236 /* Don't expand the name when we already know there is an error. */
2237 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2238 && *p != '[' && *p != '.')
2239 {
2240 EMSG(_(e_trailing));
2241 return NULL;
2242 }
2243
2244 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2245 if (lp->ll_exp_name == NULL)
2246 {
2247 /* Report an invalid expression in braces, unless the
2248 * expression evaluation has been cancelled due to an
2249 * aborting error, an interrupt, or an exception. */
2250 if (!aborting() && !quiet)
2251 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002252 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002253 EMSG2(_(e_invarg2), name);
2254 return NULL;
2255 }
2256 }
2257 lp->ll_name = lp->ll_exp_name;
2258 }
2259 else
2260 lp->ll_name = name;
2261
2262 /* Without [idx] or .key we are done. */
2263 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2264 return p;
2265
2266 cc = *p;
2267 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002268 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269 if (v == NULL && !quiet)
2270 EMSG2(_(e_undefvar), lp->ll_name);
2271 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 if (v == NULL)
2273 return NULL;
2274
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002275 /*
2276 * Loop until no more [idx] or .key is following.
2277 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002278 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2282 && !(lp->ll_tv->v_type == VAR_DICT
2283 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002285 if (!quiet)
2286 EMSG(_("E689: Can only index a List or Dictionary"));
2287 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291 if (!quiet)
2292 EMSG(_("E708: [:] must come last"));
2293 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002295
Bram Moolenaar8c711452005-01-14 21:53:12 +00002296 len = -1;
2297 if (*p == '.')
2298 {
2299 key = p + 1;
2300 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2301 ;
2302 if (len == 0)
2303 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 if (!quiet)
2305 EMSG(_(e_emptykey));
2306 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002307 }
2308 p = key + len;
2309 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002310 else
2311 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002312 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002313 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002314 if (*p == ':')
2315 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002316 else
2317 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002318 empty1 = FALSE;
2319 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002320 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002321 if (get_tv_string_chk(&var1) == NULL)
2322 {
2323 /* not a number or string */
2324 clear_tv(&var1);
2325 return NULL;
2326 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002327 }
2328
2329 /* Optionally get the second index [ :expr]. */
2330 if (*p == ':')
2331 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002333 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002334 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002336 if (!empty1)
2337 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002339 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 if (rettv != NULL && (rettv->v_type != VAR_LIST
2341 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002342 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002343 if (!quiet)
2344 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002345 if (!empty1)
2346 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002347 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002348 }
2349 p = skipwhite(p + 1);
2350 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002351 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002352 else
2353 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002355 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2356 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002357 if (!empty1)
2358 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002360 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002361 if (get_tv_string_chk(&var2) == NULL)
2362 {
2363 /* not a number or string */
2364 if (!empty1)
2365 clear_tv(&var1);
2366 clear_tv(&var2);
2367 return NULL;
2368 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002369 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002371 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002372 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002374
Bram Moolenaar8c711452005-01-14 21:53:12 +00002375 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002376 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 if (!quiet)
2378 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002379 if (!empty1)
2380 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002381 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002382 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002384 }
2385
2386 /* Skip to past ']'. */
2387 ++p;
2388 }
2389
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002391 {
2392 if (len == -1)
2393 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002395 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002396 if (*key == NUL)
2397 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002398 if (!quiet)
2399 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002400 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002402 }
2403 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 lp->ll_list = NULL;
2405 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002406 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002407 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002408 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002409 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002410 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002411 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002413 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002414 if (len == -1)
2415 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002416 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002417 }
2418 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002419 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002420 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002422 if (len == -1)
2423 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002425 p = NULL;
2426 break;
2427 }
2428 if (len == -1)
2429 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002431 }
2432 else
2433 {
2434 /*
2435 * Get the number and item for the only or first index of the List.
2436 */
2437 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002439 else
2440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002441 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002442 clear_tv(&var1);
2443 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 lp->ll_list = lp->ll_tv->vval.v_list;
2446 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2447 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 if (!quiet)
2450 EMSGN(_(e_listidx), lp->ll_n1);
2451 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 }
2455
2456 /*
2457 * May need to find the item or absolute index for the second
2458 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 * When no index given: "lp->ll_empty2" is TRUE.
2460 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002461 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002464 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002465 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002467 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 if (ni == NULL)
2470 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 if (!quiet)
2472 EMSGN(_(e_listidx), lp->ll_n2);
2473 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002476 }
2477
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2479 if (lp->ll_n1 < 0)
2480 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2481 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002482 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 if (!quiet)
2484 EMSGN(_(e_listidx), lp->ll_n2);
2485 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002486 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002487 }
2488
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002490 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 }
2492
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 return p;
2494}
2495
2496/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002497 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 */
2499 static void
2500clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002501 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502{
2503 vim_free(lp->ll_exp_name);
2504 vim_free(lp->ll_newkey);
2505}
2506
2507/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002508 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 */
2512 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002513set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002514 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002516 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002518 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519{
2520 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002521 listitem_T *ni;
2522 listitem_T *ri;
2523 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524
2525 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 cc = *endp;
2530 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002531 if (op != NULL && *op != '=')
2532 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002534
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002535 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002536 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2537 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002538 {
2539 if (tv_op(&tv, rettv, op) == OK)
2540 set_var(lp->ll_name, &tv, FALSE);
2541 clear_tv(&tv);
2542 }
2543 }
2544 else
2545 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002549 else if (tv_check_lock(lp->ll_newkey == NULL
2550 ? lp->ll_tv->v_lock
2551 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2552 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 else if (lp->ll_range)
2554 {
2555 /*
2556 * Assign the List values to the list items.
2557 */
2558 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002559 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002560 if (op != NULL && *op != '=')
2561 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2562 else
2563 {
2564 clear_tv(&lp->ll_li->li_tv);
2565 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2566 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 ri = ri->li_next;
2568 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2569 break;
2570 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002571 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 /* Need to add an empty item. */
2573 ni = listitem_alloc();
2574 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002575 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 ri = NULL;
2577 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002578 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002580 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 ni->li_tv.vval.v_number = 0;
2582 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002583 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 lp->ll_li = lp->ll_li->li_next;
2585 ++lp->ll_n1;
2586 }
2587 if (ri != NULL)
2588 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002589 else if (lp->ll_empty2
2590 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 : lp->ll_n1 != lp->ll_n2)
2592 EMSG(_("E711: List value has not enough items"));
2593 }
2594 else
2595 {
2596 /*
2597 * Assign to a List or Dictionary item.
2598 */
2599 if (lp->ll_newkey != NULL)
2600 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002601 if (op != NULL && *op != '=')
2602 {
2603 EMSG2(_(e_letwrong), op);
2604 return;
2605 }
2606
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002608 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (di == NULL)
2610 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002611 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2612 {
2613 vim_free(di);
2614 return;
2615 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002617 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002618 else if (op != NULL && *op != '=')
2619 {
2620 tv_op(lp->ll_tv, rettv, op);
2621 return;
2622 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002623 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002625
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 /*
2627 * Assign the value to the variable or list item.
2628 */
2629 if (copy)
2630 copy_tv(rettv, lp->ll_tv);
2631 else
2632 {
2633 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002634 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 }
2637 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002638}
2639
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002640/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002641 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2642 * Returns OK or FAIL.
2643 */
2644 static int
2645tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002646 typval_T *tv1;
2647 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002648 char_u *op;
2649{
2650 long n;
2651 char_u numbuf[NUMBUFLEN];
2652 char_u *s;
2653
2654 /* Can't do anything with a Funcref or a Dict on the right. */
2655 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2656 {
2657 switch (tv1->v_type)
2658 {
2659 case VAR_DICT:
2660 case VAR_FUNC:
2661 break;
2662
2663 case VAR_LIST:
2664 if (*op != '+' || tv2->v_type != VAR_LIST)
2665 break;
2666 /* List += List */
2667 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2668 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2669 return OK;
2670
2671 case VAR_NUMBER:
2672 case VAR_STRING:
2673 if (tv2->v_type == VAR_LIST)
2674 break;
2675 if (*op == '+' || *op == '-')
2676 {
2677 /* nr += nr or nr -= nr*/
2678 n = get_tv_number(tv1);
2679 if (*op == '+')
2680 n += get_tv_number(tv2);
2681 else
2682 n -= get_tv_number(tv2);
2683 clear_tv(tv1);
2684 tv1->v_type = VAR_NUMBER;
2685 tv1->vval.v_number = n;
2686 }
2687 else
2688 {
2689 /* str .= str */
2690 s = get_tv_string(tv1);
2691 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2692 clear_tv(tv1);
2693 tv1->v_type = VAR_STRING;
2694 tv1->vval.v_string = s;
2695 }
2696 return OK;
2697 }
2698 }
2699
2700 EMSG2(_(e_letwrong), op);
2701 return FAIL;
2702}
2703
2704/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002705 * Add a watcher to a list.
2706 */
2707 static void
2708list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002709 list_T *l;
2710 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002711{
2712 lw->lw_next = l->lv_watch;
2713 l->lv_watch = lw;
2714}
2715
2716/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002717 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002718 * No warning when it isn't found...
2719 */
2720 static void
2721list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002722 list_T *l;
2723 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002724{
Bram Moolenaar33570922005-01-25 22:26:29 +00002725 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002726
2727 lwp = &l->lv_watch;
2728 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2729 {
2730 if (lw == lwrem)
2731 {
2732 *lwp = lw->lw_next;
2733 break;
2734 }
2735 lwp = &lw->lw_next;
2736 }
2737}
2738
2739/*
2740 * Just before removing an item from a list: advance watchers to the next
2741 * item.
2742 */
2743 static void
2744list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002745 list_T *l;
2746 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002747{
Bram Moolenaar33570922005-01-25 22:26:29 +00002748 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002749
2750 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2751 if (lw->lw_item == item)
2752 lw->lw_item = item->li_next;
2753}
2754
2755/*
2756 * Evaluate the expression used in a ":for var in expr" command.
2757 * "arg" points to "var".
2758 * Set "*errp" to TRUE for an error, FALSE otherwise;
2759 * Return a pointer that holds the info. Null when there is an error.
2760 */
2761 void *
2762eval_for_line(arg, errp, nextcmdp, skip)
2763 char_u *arg;
2764 int *errp;
2765 char_u **nextcmdp;
2766 int skip;
2767{
Bram Moolenaar33570922005-01-25 22:26:29 +00002768 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002769 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002770 typval_T tv;
2771 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002772
2773 *errp = TRUE; /* default: there is an error */
2774
Bram Moolenaar33570922005-01-25 22:26:29 +00002775 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002776 if (fi == NULL)
2777 return NULL;
2778
2779 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2780 if (expr == NULL)
2781 return fi;
2782
2783 expr = skipwhite(expr);
2784 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2785 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002786 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002787 return fi;
2788 }
2789
2790 if (skip)
2791 ++emsg_skip;
2792 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2793 {
2794 *errp = FALSE;
2795 if (!skip)
2796 {
2797 l = tv.vval.v_list;
2798 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002799 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002800 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002801 clear_tv(&tv);
2802 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002803 else
2804 {
2805 fi->fi_list = l;
2806 list_add_watch(l, &fi->fi_lw);
2807 fi->fi_lw.lw_item = l->lv_first;
2808 }
2809 }
2810 }
2811 if (skip)
2812 --emsg_skip;
2813
2814 return fi;
2815}
2816
2817/*
2818 * Use the first item in a ":for" list. Advance to the next.
2819 * Assign the values to the variable (list). "arg" points to the first one.
2820 * Return TRUE when a valid item was found, FALSE when at end of list or
2821 * something wrong.
2822 */
2823 int
2824next_for_item(fi_void, arg)
2825 void *fi_void;
2826 char_u *arg;
2827{
Bram Moolenaar33570922005-01-25 22:26:29 +00002828 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002829 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002830 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002831
2832 item = fi->fi_lw.lw_item;
2833 if (item == NULL)
2834 result = FALSE;
2835 else
2836 {
2837 fi->fi_lw.lw_item = item->li_next;
2838 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2839 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2840 }
2841 return result;
2842}
2843
2844/*
2845 * Free the structure used to store info used by ":for".
2846 */
2847 void
2848free_for_info(fi_void)
2849 void *fi_void;
2850{
Bram Moolenaar33570922005-01-25 22:26:29 +00002851 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002852
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002853 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002854 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002855 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002856 list_unref(fi->fi_list);
2857 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002858 vim_free(fi);
2859}
2860
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2862
2863 void
2864set_context_for_expression(xp, arg, cmdidx)
2865 expand_T *xp;
2866 char_u *arg;
2867 cmdidx_T cmdidx;
2868{
2869 int got_eq = FALSE;
2870 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002871 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002873 if (cmdidx == CMD_let)
2874 {
2875 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002876 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002877 {
2878 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002879 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002880 {
2881 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883 if (vim_iswhite(*p))
2884 break;
2885 }
2886 return;
2887 }
2888 }
2889 else
2890 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2891 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 while ((xp->xp_pattern = vim_strpbrk(arg,
2893 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2894 {
2895 c = *xp->xp_pattern;
2896 if (c == '&')
2897 {
2898 c = xp->xp_pattern[1];
2899 if (c == '&')
2900 {
2901 ++xp->xp_pattern;
2902 xp->xp_context = cmdidx != CMD_let || got_eq
2903 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2904 }
2905 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002908 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2909 xp->xp_pattern += 2;
2910
2911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
2913 else if (c == '$')
2914 {
2915 /* environment variable */
2916 xp->xp_context = EXPAND_ENV_VARS;
2917 }
2918 else if (c == '=')
2919 {
2920 got_eq = TRUE;
2921 xp->xp_context = EXPAND_EXPRESSION;
2922 }
2923 else if (c == '<'
2924 && xp->xp_context == EXPAND_FUNCTIONS
2925 && vim_strchr(xp->xp_pattern, '(') == NULL)
2926 {
2927 /* Function name can start with "<SNR>" */
2928 break;
2929 }
2930 else if (cmdidx != CMD_let || got_eq)
2931 {
2932 if (c == '"') /* string */
2933 {
2934 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2935 if (c == '\\' && xp->xp_pattern[1] != NUL)
2936 ++xp->xp_pattern;
2937 xp->xp_context = EXPAND_NOTHING;
2938 }
2939 else if (c == '\'') /* literal string */
2940 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002941 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2943 /* skip */ ;
2944 xp->xp_context = EXPAND_NOTHING;
2945 }
2946 else if (c == '|')
2947 {
2948 if (xp->xp_pattern[1] == '|')
2949 {
2950 ++xp->xp_pattern;
2951 xp->xp_context = EXPAND_EXPRESSION;
2952 }
2953 else
2954 xp->xp_context = EXPAND_COMMANDS;
2955 }
2956 else
2957 xp->xp_context = EXPAND_EXPRESSION;
2958 }
2959 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002960 /* Doesn't look like something valid, expand as an expression
2961 * anyway. */
2962 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 arg = xp->xp_pattern;
2964 if (*arg != NUL)
2965 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2966 /* skip */ ;
2967 }
2968 xp->xp_pattern = arg;
2969}
2970
2971#endif /* FEAT_CMDL_COMPL */
2972
2973/*
2974 * ":1,25call func(arg1, arg2)" function call.
2975 */
2976 void
2977ex_call(eap)
2978 exarg_T *eap;
2979{
2980 char_u *arg = eap->arg;
2981 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002983 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002985 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 linenr_T lnum;
2987 int doesrange;
2988 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002989 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002991 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2992 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002993 if (tofree == NULL)
2994 return;
2995
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002996 /* Increase refcount on dictionary, it could get deleted when evaluating
2997 * the arguments. */
2998 if (fudi.fd_dict != NULL)
2999 ++fudi.fd_dict->dv_refcount;
3000
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003001 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3002 len = STRLEN(tofree);
3003 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
Bram Moolenaar532c7802005-01-27 14:44:31 +00003005 /* Skip white space to allow ":call func ()". Not good, but required for
3006 * backward compatibility. */
3007 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003008 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009
3010 if (*startarg != '(')
3011 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003012 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 goto end;
3014 }
3015
3016 /*
3017 * When skipping, evaluate the function once, to find the end of the
3018 * arguments.
3019 * When the function takes a range, this is discovered after the first
3020 * call, and the loop is broken.
3021 */
3022 if (eap->skip)
3023 {
3024 ++emsg_skip;
3025 lnum = eap->line2; /* do it once, also with an invalid range */
3026 }
3027 else
3028 lnum = eap->line1;
3029 for ( ; lnum <= eap->line2; ++lnum)
3030 {
3031 if (!eap->skip && eap->addr_count > 0)
3032 {
3033 curwin->w_cursor.lnum = lnum;
3034 curwin->w_cursor.col = 0;
3035 }
3036 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003037 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003038 eap->line1, eap->line2, &doesrange,
3039 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 {
3041 failed = TRUE;
3042 break;
3043 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003044 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 if (doesrange || eap->skip)
3046 break;
3047 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003048 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003049 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003050 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 if (aborting())
3052 break;
3053 }
3054 if (eap->skip)
3055 --emsg_skip;
3056
3057 if (!failed)
3058 {
3059 /* Check for trailing illegal characters and a following command. */
3060 if (!ends_excmd(*arg))
3061 {
3062 emsg_severe = TRUE;
3063 EMSG(_(e_trailing));
3064 }
3065 else
3066 eap->nextcmd = check_nextcmd(arg);
3067 }
3068
3069end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003070 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072}
3073
3074/*
3075 * ":unlet[!] var1 ... " command.
3076 */
3077 void
3078ex_unlet(eap)
3079 exarg_T *eap;
3080{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003081 ex_unletlock(eap, eap->arg, 0);
3082}
3083
3084/*
3085 * ":lockvar" and ":unlockvar" commands
3086 */
3087 void
3088ex_lockvar(eap)
3089 exarg_T *eap;
3090{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003092 int deep = 2;
3093
3094 if (eap->forceit)
3095 deep = -1;
3096 else if (vim_isdigit(*arg))
3097 {
3098 deep = getdigits(&arg);
3099 arg = skipwhite(arg);
3100 }
3101
3102 ex_unletlock(eap, arg, deep);
3103}
3104
3105/*
3106 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3107 */
3108 static void
3109ex_unletlock(eap, argstart, deep)
3110 exarg_T *eap;
3111 char_u *argstart;
3112 int deep;
3113{
3114 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003117 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118
3119 do
3120 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003121 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003122 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3123 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003124 if (lv.ll_name == NULL)
3125 error = TRUE; /* error but continue parsing */
3126 if (name_end == NULL || (!vim_iswhite(*name_end)
3127 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003129 if (name_end != NULL)
3130 {
3131 emsg_severe = TRUE;
3132 EMSG(_(e_trailing));
3133 }
3134 if (!(eap->skip || error))
3135 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 break;
3137 }
3138
3139 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003140 {
3141 if (eap->cmdidx == CMD_unlet)
3142 {
3143 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3144 error = TRUE;
3145 }
3146 else
3147 {
3148 if (do_lock_var(&lv, name_end, deep,
3149 eap->cmdidx == CMD_lockvar) == FAIL)
3150 error = TRUE;
3151 }
3152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003154 if (!eap->skip)
3155 clear_lval(&lv);
3156
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 arg = skipwhite(name_end);
3158 } while (!ends_excmd(*arg));
3159
3160 eap->nextcmd = check_nextcmd(arg);
3161}
3162
Bram Moolenaar8c711452005-01-14 21:53:12 +00003163 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003164do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003165 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003166 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003167 int forceit;
3168{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003169 int ret = OK;
3170 int cc;
3171
3172 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003173 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003174 cc = *name_end;
3175 *name_end = NUL;
3176
3177 /* Normal name or expanded name. */
3178 if (check_changedtick(lp->ll_name))
3179 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003180 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003181 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003182 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003183 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003184 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3185 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003186 else if (lp->ll_range)
3187 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003189
3190 /* Delete a range of List items. */
3191 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3192 {
3193 li = lp->ll_li->li_next;
3194 listitem_remove(lp->ll_list, lp->ll_li);
3195 lp->ll_li = li;
3196 ++lp->ll_n1;
3197 }
3198 }
3199 else
3200 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003201 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003202 /* unlet a List item. */
3203 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003204 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003205 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003206 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003207 }
3208
3209 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003210}
3211
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212/*
3213 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003214 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 */
3216 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003217do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003219 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220{
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 hashtab_T *ht;
3222 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003223 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224
Bram Moolenaar33570922005-01-25 22:26:29 +00003225 ht = find_var_ht(name, &varname);
3226 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003228 hi = hash_find(ht, varname);
3229 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003230 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003231 if (var_check_ro(HI2DI(hi)->di_flags, name))
3232 return FAIL;
3233 delete_var(ht, hi);
3234 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003237 if (forceit)
3238 return OK;
3239 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 return FAIL;
3241}
3242
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003243/*
3244 * Lock or unlock variable indicated by "lp".
3245 * "deep" is the levels to go (-1 for unlimited);
3246 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3247 */
3248 static int
3249do_lock_var(lp, name_end, deep, lock)
3250 lval_T *lp;
3251 char_u *name_end;
3252 int deep;
3253 int lock;
3254{
3255 int ret = OK;
3256 int cc;
3257 dictitem_T *di;
3258
3259 if (deep == 0) /* nothing to do */
3260 return OK;
3261
3262 if (lp->ll_tv == NULL)
3263 {
3264 cc = *name_end;
3265 *name_end = NUL;
3266
3267 /* Normal name or expanded name. */
3268 if (check_changedtick(lp->ll_name))
3269 ret = FAIL;
3270 else
3271 {
3272 di = find_var(lp->ll_name, NULL);
3273 if (di == NULL)
3274 ret = FAIL;
3275 else
3276 {
3277 if (lock)
3278 di->di_flags |= DI_FLAGS_LOCK;
3279 else
3280 di->di_flags &= ~DI_FLAGS_LOCK;
3281 item_lock(&di->di_tv, deep, lock);
3282 }
3283 }
3284 *name_end = cc;
3285 }
3286 else if (lp->ll_range)
3287 {
3288 listitem_T *li = lp->ll_li;
3289
3290 /* (un)lock a range of List items. */
3291 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3292 {
3293 item_lock(&li->li_tv, deep, lock);
3294 li = li->li_next;
3295 ++lp->ll_n1;
3296 }
3297 }
3298 else if (lp->ll_list != NULL)
3299 /* (un)lock a List item. */
3300 item_lock(&lp->ll_li->li_tv, deep, lock);
3301 else
3302 /* un(lock) a Dictionary item. */
3303 item_lock(&lp->ll_di->di_tv, deep, lock);
3304
3305 return ret;
3306}
3307
3308/*
3309 * Lock or unlock an item. "deep" is nr of levels to go.
3310 */
3311 static void
3312item_lock(tv, deep, lock)
3313 typval_T *tv;
3314 int deep;
3315 int lock;
3316{
3317 static int recurse = 0;
3318 list_T *l;
3319 listitem_T *li;
3320 dict_T *d;
3321 hashitem_T *hi;
3322 int todo;
3323
3324 if (recurse >= DICT_MAXNEST)
3325 {
3326 EMSG(_("E743: variable nested too deep for (un)lock"));
3327 return;
3328 }
3329 if (deep == 0)
3330 return;
3331 ++recurse;
3332
3333 /* lock/unlock the item itself */
3334 if (lock)
3335 tv->v_lock |= VAR_LOCKED;
3336 else
3337 tv->v_lock &= ~VAR_LOCKED;
3338
3339 switch (tv->v_type)
3340 {
3341 case VAR_LIST:
3342 if ((l = tv->vval.v_list) != NULL)
3343 {
3344 if (lock)
3345 l->lv_lock |= VAR_LOCKED;
3346 else
3347 l->lv_lock &= ~VAR_LOCKED;
3348 if (deep < 0 || deep > 1)
3349 /* recursive: lock/unlock the items the List contains */
3350 for (li = l->lv_first; li != NULL; li = li->li_next)
3351 item_lock(&li->li_tv, deep - 1, lock);
3352 }
3353 break;
3354 case VAR_DICT:
3355 if ((d = tv->vval.v_dict) != NULL)
3356 {
3357 if (lock)
3358 d->dv_lock |= VAR_LOCKED;
3359 else
3360 d->dv_lock &= ~VAR_LOCKED;
3361 if (deep < 0 || deep > 1)
3362 {
3363 /* recursive: lock/unlock the items the List contains */
3364 todo = d->dv_hashtab.ht_used;
3365 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3366 {
3367 if (!HASHITEM_EMPTY(hi))
3368 {
3369 --todo;
3370 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3371 }
3372 }
3373 }
3374 }
3375 }
3376 --recurse;
3377}
3378
Bram Moolenaara40058a2005-07-11 22:42:07 +00003379/*
3380 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3381 * it refers to a List or Dictionary that is locked.
3382 */
3383 static int
3384tv_islocked(tv)
3385 typval_T *tv;
3386{
3387 return (tv->v_lock & VAR_LOCKED)
3388 || (tv->v_type == VAR_LIST
3389 && tv->vval.v_list != NULL
3390 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3391 || (tv->v_type == VAR_DICT
3392 && tv->vval.v_dict != NULL
3393 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3394}
3395
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3397/*
3398 * Delete all "menutrans_" variables.
3399 */
3400 void
3401del_menutrans_vars()
3402{
Bram Moolenaar33570922005-01-25 22:26:29 +00003403 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003404 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
Bram Moolenaar33570922005-01-25 22:26:29 +00003406 hash_lock(&globvarht);
3407 todo = globvarht.ht_used;
3408 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003409 {
3410 if (!HASHITEM_EMPTY(hi))
3411 {
3412 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003413 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3414 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003415 }
3416 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003417 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418}
3419#endif
3420
3421#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3422
3423/*
3424 * Local string buffer for the next two functions to store a variable name
3425 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3426 * get_user_var_name().
3427 */
3428
3429static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3430
3431static char_u *varnamebuf = NULL;
3432static int varnamebuflen = 0;
3433
3434/*
3435 * Function to concatenate a prefix and a variable name.
3436 */
3437 static char_u *
3438cat_prefix_varname(prefix, name)
3439 int prefix;
3440 char_u *name;
3441{
3442 int len;
3443
3444 len = (int)STRLEN(name) + 3;
3445 if (len > varnamebuflen)
3446 {
3447 vim_free(varnamebuf);
3448 len += 10; /* some additional space */
3449 varnamebuf = alloc(len);
3450 if (varnamebuf == NULL)
3451 {
3452 varnamebuflen = 0;
3453 return NULL;
3454 }
3455 varnamebuflen = len;
3456 }
3457 *varnamebuf = prefix;
3458 varnamebuf[1] = ':';
3459 STRCPY(varnamebuf + 2, name);
3460 return varnamebuf;
3461}
3462
3463/*
3464 * Function given to ExpandGeneric() to obtain the list of user defined
3465 * (global/buffer/window/built-in) variable names.
3466 */
3467/*ARGSUSED*/
3468 char_u *
3469get_user_var_name(xp, idx)
3470 expand_T *xp;
3471 int idx;
3472{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003473 static long_u gdone;
3474 static long_u bdone;
3475 static long_u wdone;
3476 static int vidx;
3477 static hashitem_T *hi;
3478 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479
3480 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003481 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003482
3483 /* Global variables */
3484 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003486 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003487 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003488 else
3489 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003490 while (HASHITEM_EMPTY(hi))
3491 ++hi;
3492 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3493 return cat_prefix_varname('g', hi->hi_key);
3494 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003496
3497 /* b: variables */
3498 ht = &curbuf->b_vars.dv_hashtab;
3499 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003501 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003502 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003503 else
3504 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003505 while (HASHITEM_EMPTY(hi))
3506 ++hi;
3507 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003509 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003511 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 return (char_u *)"b:changedtick";
3513 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003514
3515 /* w: variables */
3516 ht = &curwin->w_vars.dv_hashtab;
3517 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003519 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003520 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003521 else
3522 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003523 while (HASHITEM_EMPTY(hi))
3524 ++hi;
3525 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003527
3528 /* v: variables */
3529 if (vidx < VV_LEN)
3530 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
3532 vim_free(varnamebuf);
3533 varnamebuf = NULL;
3534 varnamebuflen = 0;
3535 return NULL;
3536}
3537
3538#endif /* FEAT_CMDL_COMPL */
3539
3540/*
3541 * types for expressions.
3542 */
3543typedef enum
3544{
3545 TYPE_UNKNOWN = 0
3546 , TYPE_EQUAL /* == */
3547 , TYPE_NEQUAL /* != */
3548 , TYPE_GREATER /* > */
3549 , TYPE_GEQUAL /* >= */
3550 , TYPE_SMALLER /* < */
3551 , TYPE_SEQUAL /* <= */
3552 , TYPE_MATCH /* =~ */
3553 , TYPE_NOMATCH /* !~ */
3554} exptype_T;
3555
3556/*
3557 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003558 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3560 */
3561
3562/*
3563 * Handle zero level expression.
3564 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003565 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 * Return OK or FAIL.
3567 */
3568 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003569eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 char_u **nextcmd;
3573 int evaluate;
3574{
3575 int ret;
3576 char_u *p;
3577
3578 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003579 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 if (ret == FAIL || !ends_excmd(*p))
3581 {
3582 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003583 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 /*
3585 * Report the invalid expression unless the expression evaluation has
3586 * been cancelled due to an aborting error, an interrupt, or an
3587 * exception.
3588 */
3589 if (!aborting())
3590 EMSG2(_(e_invexpr2), arg);
3591 ret = FAIL;
3592 }
3593 if (nextcmd != NULL)
3594 *nextcmd = check_nextcmd(p);
3595
3596 return ret;
3597}
3598
3599/*
3600 * Handle top level expression:
3601 * expr1 ? expr0 : expr0
3602 *
3603 * "arg" must point to the first non-white of the expression.
3604 * "arg" is advanced to the next non-white after the recognized expression.
3605 *
3606 * Return OK or FAIL.
3607 */
3608 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003609eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 int evaluate;
3613{
3614 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003615 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
3617 /*
3618 * Get the first variable.
3619 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003620 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 return FAIL;
3622
3623 if ((*arg)[0] == '?')
3624 {
3625 result = FALSE;
3626 if (evaluate)
3627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003628 int error = FALSE;
3629
3630 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003632 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003633 if (error)
3634 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 }
3636
3637 /*
3638 * Get the second variable.
3639 */
3640 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003641 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 return FAIL;
3643
3644 /*
3645 * Check for the ":".
3646 */
3647 if ((*arg)[0] != ':')
3648 {
3649 EMSG(_("E109: Missing ':' after '?'"));
3650 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003651 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 return FAIL;
3653 }
3654
3655 /*
3656 * Get the third variable.
3657 */
3658 *arg = skipwhite(*arg + 1);
3659 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3660 {
3661 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003662 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 return FAIL;
3664 }
3665 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003666 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
3668
3669 return OK;
3670}
3671
3672/*
3673 * Handle first level expression:
3674 * expr2 || expr2 || expr2 logical OR
3675 *
3676 * "arg" must point to the first non-white of the expression.
3677 * "arg" is advanced to the next non-white after the recognized expression.
3678 *
3679 * Return OK or FAIL.
3680 */
3681 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003682eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 int evaluate;
3686{
Bram Moolenaar33570922005-01-25 22:26:29 +00003687 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 long result;
3689 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003690 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691
3692 /*
3693 * Get the first variable.
3694 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003695 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 return FAIL;
3697
3698 /*
3699 * Repeat until there is no following "||".
3700 */
3701 first = TRUE;
3702 result = FALSE;
3703 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3704 {
3705 if (evaluate && first)
3706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003707 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003709 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003710 if (error)
3711 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 first = FALSE;
3713 }
3714
3715 /*
3716 * Get the second variable.
3717 */
3718 *arg = skipwhite(*arg + 2);
3719 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3720 return FAIL;
3721
3722 /*
3723 * Compute the result.
3724 */
3725 if (evaluate && !result)
3726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003727 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003729 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003730 if (error)
3731 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 }
3733 if (evaluate)
3734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003735 rettv->v_type = VAR_NUMBER;
3736 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
3738 }
3739
3740 return OK;
3741}
3742
3743/*
3744 * Handle second level expression:
3745 * expr3 && expr3 && expr3 logical AND
3746 *
3747 * "arg" must point to the first non-white of the expression.
3748 * "arg" is advanced to the next non-white after the recognized expression.
3749 *
3750 * Return OK or FAIL.
3751 */
3752 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003753eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 int evaluate;
3757{
Bram Moolenaar33570922005-01-25 22:26:29 +00003758 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 long result;
3760 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003761 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
3763 /*
3764 * Get the first variable.
3765 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003766 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 return FAIL;
3768
3769 /*
3770 * Repeat until there is no following "&&".
3771 */
3772 first = TRUE;
3773 result = TRUE;
3774 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3775 {
3776 if (evaluate && first)
3777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003778 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003780 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003781 if (error)
3782 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 first = FALSE;
3784 }
3785
3786 /*
3787 * Get the second variable.
3788 */
3789 *arg = skipwhite(*arg + 2);
3790 if (eval4(arg, &var2, evaluate && result) == FAIL)
3791 return FAIL;
3792
3793 /*
3794 * Compute the result.
3795 */
3796 if (evaluate && result)
3797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003798 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003800 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003801 if (error)
3802 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804 if (evaluate)
3805 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003806 rettv->v_type = VAR_NUMBER;
3807 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809 }
3810
3811 return OK;
3812}
3813
3814/*
3815 * Handle third level expression:
3816 * var1 == var2
3817 * var1 =~ var2
3818 * var1 != var2
3819 * var1 !~ var2
3820 * var1 > var2
3821 * var1 >= var2
3822 * var1 < var2
3823 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003824 * var1 is var2
3825 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 *
3827 * "arg" must point to the first non-white of the expression.
3828 * "arg" is advanced to the next non-white after the recognized expression.
3829 *
3830 * Return OK or FAIL.
3831 */
3832 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003833eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 int evaluate;
3837{
Bram Moolenaar33570922005-01-25 22:26:29 +00003838 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 char_u *p;
3840 int i;
3841 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003842 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 int len = 2;
3844 long n1, n2;
3845 char_u *s1, *s2;
3846 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3847 regmatch_T regmatch;
3848 int ic;
3849 char_u *save_cpo;
3850
3851 /*
3852 * Get the first variable.
3853 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003854 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 return FAIL;
3856
3857 p = *arg;
3858 switch (p[0])
3859 {
3860 case '=': if (p[1] == '=')
3861 type = TYPE_EQUAL;
3862 else if (p[1] == '~')
3863 type = TYPE_MATCH;
3864 break;
3865 case '!': if (p[1] == '=')
3866 type = TYPE_NEQUAL;
3867 else if (p[1] == '~')
3868 type = TYPE_NOMATCH;
3869 break;
3870 case '>': if (p[1] != '=')
3871 {
3872 type = TYPE_GREATER;
3873 len = 1;
3874 }
3875 else
3876 type = TYPE_GEQUAL;
3877 break;
3878 case '<': if (p[1] != '=')
3879 {
3880 type = TYPE_SMALLER;
3881 len = 1;
3882 }
3883 else
3884 type = TYPE_SEQUAL;
3885 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003886 case 'i': if (p[1] == 's')
3887 {
3888 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3889 len = 5;
3890 if (!vim_isIDc(p[len]))
3891 {
3892 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3893 type_is = TRUE;
3894 }
3895 }
3896 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
3898
3899 /*
3900 * If there is a comparitive operator, use it.
3901 */
3902 if (type != TYPE_UNKNOWN)
3903 {
3904 /* extra question mark appended: ignore case */
3905 if (p[len] == '?')
3906 {
3907 ic = TRUE;
3908 ++len;
3909 }
3910 /* extra '#' appended: match case */
3911 else if (p[len] == '#')
3912 {
3913 ic = FALSE;
3914 ++len;
3915 }
3916 /* nothing appened: use 'ignorecase' */
3917 else
3918 ic = p_ic;
3919
3920 /*
3921 * Get the second variable.
3922 */
3923 *arg = skipwhite(p + len);
3924 if (eval5(arg, &var2, evaluate) == FAIL)
3925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003926 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 return FAIL;
3928 }
3929
3930 if (evaluate)
3931 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003932 if (type_is && rettv->v_type != var2.v_type)
3933 {
3934 /* For "is" a different type always means FALSE, for "notis"
3935 * it means TRUE. */
3936 n1 = (type == TYPE_NEQUAL);
3937 }
3938 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3939 {
3940 if (type_is)
3941 {
3942 n1 = (rettv->v_type == var2.v_type
3943 && rettv->vval.v_list == var2.vval.v_list);
3944 if (type == TYPE_NEQUAL)
3945 n1 = !n1;
3946 }
3947 else if (rettv->v_type != var2.v_type
3948 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3949 {
3950 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003951 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003952 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003953 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003954 clear_tv(rettv);
3955 clear_tv(&var2);
3956 return FAIL;
3957 }
3958 else
3959 {
3960 /* Compare two Lists for being equal or unequal. */
3961 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3962 if (type == TYPE_NEQUAL)
3963 n1 = !n1;
3964 }
3965 }
3966
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003967 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3968 {
3969 if (type_is)
3970 {
3971 n1 = (rettv->v_type == var2.v_type
3972 && rettv->vval.v_dict == var2.vval.v_dict);
3973 if (type == TYPE_NEQUAL)
3974 n1 = !n1;
3975 }
3976 else if (rettv->v_type != var2.v_type
3977 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3978 {
3979 if (rettv->v_type != var2.v_type)
3980 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3981 else
3982 EMSG(_("E736: Invalid operation for Dictionary"));
3983 clear_tv(rettv);
3984 clear_tv(&var2);
3985 return FAIL;
3986 }
3987 else
3988 {
3989 /* Compare two Dictionaries for being equal or unequal. */
3990 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3991 if (type == TYPE_NEQUAL)
3992 n1 = !n1;
3993 }
3994 }
3995
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003996 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3997 {
3998 if (rettv->v_type != var2.v_type
3999 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4000 {
4001 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004002 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004003 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004004 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004005 clear_tv(rettv);
4006 clear_tv(&var2);
4007 return FAIL;
4008 }
4009 else
4010 {
4011 /* Compare two Funcrefs for being equal or unequal. */
4012 if (rettv->vval.v_string == NULL
4013 || var2.vval.v_string == NULL)
4014 n1 = FALSE;
4015 else
4016 n1 = STRCMP(rettv->vval.v_string,
4017 var2.vval.v_string) == 0;
4018 if (type == TYPE_NEQUAL)
4019 n1 = !n1;
4020 }
4021 }
4022
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 /*
4024 * If one of the two variables is a number, compare as a number.
4025 * When using "=~" or "!~", always compare as string.
4026 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004027 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004030 n1 = get_tv_number(rettv);
4031 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 switch (type)
4033 {
4034 case TYPE_EQUAL: n1 = (n1 == n2); break;
4035 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4036 case TYPE_GREATER: n1 = (n1 > n2); break;
4037 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4038 case TYPE_SMALLER: n1 = (n1 < n2); break;
4039 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4040 case TYPE_UNKNOWN:
4041 case TYPE_MATCH:
4042 case TYPE_NOMATCH: break; /* avoid gcc warning */
4043 }
4044 }
4045 else
4046 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004047 s1 = get_tv_string_buf(rettv, buf1);
4048 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4050 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4051 else
4052 i = 0;
4053 n1 = FALSE;
4054 switch (type)
4055 {
4056 case TYPE_EQUAL: n1 = (i == 0); break;
4057 case TYPE_NEQUAL: n1 = (i != 0); break;
4058 case TYPE_GREATER: n1 = (i > 0); break;
4059 case TYPE_GEQUAL: n1 = (i >= 0); break;
4060 case TYPE_SMALLER: n1 = (i < 0); break;
4061 case TYPE_SEQUAL: n1 = (i <= 0); break;
4062
4063 case TYPE_MATCH:
4064 case TYPE_NOMATCH:
4065 /* avoid 'l' flag in 'cpoptions' */
4066 save_cpo = p_cpo;
4067 p_cpo = (char_u *)"";
4068 regmatch.regprog = vim_regcomp(s2,
4069 RE_MAGIC + RE_STRING);
4070 regmatch.rm_ic = ic;
4071 if (regmatch.regprog != NULL)
4072 {
4073 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4074 vim_free(regmatch.regprog);
4075 if (type == TYPE_NOMATCH)
4076 n1 = !n1;
4077 }
4078 p_cpo = save_cpo;
4079 break;
4080
4081 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4082 }
4083 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004084 clear_tv(rettv);
4085 clear_tv(&var2);
4086 rettv->v_type = VAR_NUMBER;
4087 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 }
4089 }
4090
4091 return OK;
4092}
4093
4094/*
4095 * Handle fourth level expression:
4096 * + number addition
4097 * - number subtraction
4098 * . string concatenation
4099 *
4100 * "arg" must point to the first non-white of the expression.
4101 * "arg" is advanced to the next non-white after the recognized expression.
4102 *
4103 * Return OK or FAIL.
4104 */
4105 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 int evaluate;
4110{
Bram Moolenaar33570922005-01-25 22:26:29 +00004111 typval_T var2;
4112 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 int op;
4114 long n1, n2;
4115 char_u *s1, *s2;
4116 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4117 char_u *p;
4118
4119 /*
4120 * Get the first variable.
4121 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return FAIL;
4124
4125 /*
4126 * Repeat computing, until no '+', '-' or '.' is following.
4127 */
4128 for (;;)
4129 {
4130 op = **arg;
4131 if (op != '+' && op != '-' && op != '.')
4132 break;
4133
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004134 if (op != '+' || rettv->v_type != VAR_LIST)
4135 {
4136 /* For "list + ...", an illegal use of the first operand as
4137 * a number cannot be determined before evaluating the 2nd
4138 * operand: if this is also a list, all is ok.
4139 * For "something . ...", "something - ..." or "non-list + ...",
4140 * we know that the first operand needs to be a string or number
4141 * without evaluating the 2nd operand. So check before to avoid
4142 * side effects after an error. */
4143 if (evaluate && get_tv_string_chk(rettv) == NULL)
4144 {
4145 clear_tv(rettv);
4146 return FAIL;
4147 }
4148 }
4149
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 /*
4151 * Get the second variable.
4152 */
4153 *arg = skipwhite(*arg + 1);
4154 if (eval6(arg, &var2, evaluate) == FAIL)
4155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 return FAIL;
4158 }
4159
4160 if (evaluate)
4161 {
4162 /*
4163 * Compute the result.
4164 */
4165 if (op == '.')
4166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4168 s2 = get_tv_string_buf_chk(&var2, buf2);
4169 if (s2 == NULL) /* type error ? */
4170 {
4171 clear_tv(rettv);
4172 clear_tv(&var2);
4173 return FAIL;
4174 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004175 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 clear_tv(rettv);
4177 rettv->v_type = VAR_STRING;
4178 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004180 else if (op == '+' && rettv->v_type == VAR_LIST
4181 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004182 {
4183 /* concatenate Lists */
4184 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4185 &var3) == FAIL)
4186 {
4187 clear_tv(rettv);
4188 clear_tv(&var2);
4189 return FAIL;
4190 }
4191 clear_tv(rettv);
4192 *rettv = var3;
4193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 else
4195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004196 int error = FALSE;
4197
4198 n1 = get_tv_number_chk(rettv, &error);
4199 if (error)
4200 {
4201 /* This can only happen for "list + non-list".
4202 * For "non-list + ..." or "something - ...", we returned
4203 * before evaluating the 2nd operand. */
4204 clear_tv(rettv);
4205 return FAIL;
4206 }
4207 n2 = get_tv_number_chk(&var2, &error);
4208 if (error)
4209 {
4210 clear_tv(rettv);
4211 clear_tv(&var2);
4212 return FAIL;
4213 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004214 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 if (op == '+')
4216 n1 = n1 + n2;
4217 else
4218 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219 rettv->v_type = VAR_NUMBER;
4220 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224 }
4225 return OK;
4226}
4227
4228/*
4229 * Handle fifth level expression:
4230 * * number multiplication
4231 * / number division
4232 * % number modulo
4233 *
4234 * "arg" must point to the first non-white of the expression.
4235 * "arg" is advanced to the next non-white after the recognized expression.
4236 *
4237 * Return OK or FAIL.
4238 */
4239 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 int evaluate;
4244{
Bram Moolenaar33570922005-01-25 22:26:29 +00004245 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 int op;
4247 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004248 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249
4250 /*
4251 * Get the first variable.
4252 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 return FAIL;
4255
4256 /*
4257 * Repeat computing, until no '*', '/' or '%' is following.
4258 */
4259 for (;;)
4260 {
4261 op = **arg;
4262 if (op != '*' && op != '/' && op != '%')
4263 break;
4264
4265 if (evaluate)
4266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004267 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (error)
4270 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272 else
4273 n1 = 0;
4274
4275 /*
4276 * Get the second variable.
4277 */
4278 *arg = skipwhite(*arg + 1);
4279 if (eval7(arg, &var2, evaluate) == FAIL)
4280 return FAIL;
4281
4282 if (evaluate)
4283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004284 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004285 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004286 if (error)
4287 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288
4289 /*
4290 * Compute the result.
4291 */
4292 if (op == '*')
4293 n1 = n1 * n2;
4294 else if (op == '/')
4295 {
4296 if (n2 == 0) /* give an error message? */
4297 n1 = 0x7fffffffL;
4298 else
4299 n1 = n1 / n2;
4300 }
4301 else
4302 {
4303 if (n2 == 0) /* give an error message? */
4304 n1 = 0;
4305 else
4306 n1 = n1 % n2;
4307 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004308 rettv->v_type = VAR_NUMBER;
4309 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 }
4311 }
4312
4313 return OK;
4314}
4315
4316/*
4317 * Handle sixth level expression:
4318 * number number constant
4319 * "string" string contstant
4320 * 'string' literal string contstant
4321 * &option-name option value
4322 * @r register contents
4323 * identifier variable value
4324 * function() function call
4325 * $VAR environment variable
4326 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004327 * [expr, expr] List
4328 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 *
4330 * Also handle:
4331 * ! in front logical NOT
4332 * - in front unary minus
4333 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004334 * trailing [] subscript in String or List
4335 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 *
4337 * "arg" must point to the first non-white of the expression.
4338 * "arg" is advanced to the next non-white after the recognized expression.
4339 *
4340 * Return OK or FAIL.
4341 */
4342 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004343eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 int evaluate;
4347{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 long n;
4349 int len;
4350 char_u *s;
4351 int val;
4352 char_u *start_leader, *end_leader;
4353 int ret = OK;
4354 char_u *alias;
4355
4356 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004357 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004358 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361
4362 /*
4363 * Skip '!' and '-' characters. They are handled later.
4364 */
4365 start_leader = *arg;
4366 while (**arg == '!' || **arg == '-' || **arg == '+')
4367 *arg = skipwhite(*arg + 1);
4368 end_leader = *arg;
4369
4370 switch (**arg)
4371 {
4372 /*
4373 * Number constant.
4374 */
4375 case '0':
4376 case '1':
4377 case '2':
4378 case '3':
4379 case '4':
4380 case '5':
4381 case '6':
4382 case '7':
4383 case '8':
4384 case '9':
4385 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4386 *arg += len;
4387 if (evaluate)
4388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004389 rettv->v_type = VAR_NUMBER;
4390 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 }
4392 break;
4393
4394 /*
4395 * String constant: "string".
4396 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004397 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 break;
4399
4400 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004401 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004403 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004404 break;
4405
4406 /*
4407 * List: [expr, expr]
4408 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004409 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 break;
4411
4412 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004413 * Dictionary: {key: val, key: val}
4414 */
4415 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4416 break;
4417
4418 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004419 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004421 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 break;
4423
4424 /*
4425 * Environment variable: $VAR.
4426 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004427 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 break;
4429
4430 /*
4431 * Register contents: @r.
4432 */
4433 case '@': ++*arg;
4434 if (evaluate)
4435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004436 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004437 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
4439 if (**arg != NUL)
4440 ++*arg;
4441 break;
4442
4443 /*
4444 * nested expression: (expression).
4445 */
4446 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004447 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 if (**arg == ')')
4449 ++*arg;
4450 else if (ret == OK)
4451 {
4452 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004453 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 ret = FAIL;
4455 }
4456 break;
4457
Bram Moolenaar8c711452005-01-14 21:53:12 +00004458 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 break;
4460 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004461
4462 if (ret == NOTDONE)
4463 {
4464 /*
4465 * Must be a variable or function name.
4466 * Can also be a curly-braces kind of name: {expr}.
4467 */
4468 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004469 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004470 if (alias != NULL)
4471 s = alias;
4472
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004473 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004474 ret = FAIL;
4475 else
4476 {
4477 if (**arg == '(') /* recursive! */
4478 {
4479 /* If "s" is the name of a variable of type VAR_FUNC
4480 * use its contents. */
4481 s = deref_func_name(s, &len);
4482
4483 /* Invoke the function. */
4484 ret = get_func_tv(s, len, rettv, arg,
4485 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004486 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004487 /* Stop the expression evaluation when immediately
4488 * aborting on error, or when an interrupt occurred or
4489 * an exception was thrown but not caught. */
4490 if (aborting())
4491 {
4492 if (ret == OK)
4493 clear_tv(rettv);
4494 ret = FAIL;
4495 }
4496 }
4497 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004498 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004499 else
4500 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004501 }
4502
4503 if (alias != NULL)
4504 vim_free(alias);
4505 }
4506
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 *arg = skipwhite(*arg);
4508
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004509 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4510 * expr(expr). */
4511 if (ret == OK)
4512 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
4514 /*
4515 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4516 */
4517 if (ret == OK && evaluate && end_leader > start_leader)
4518 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004519 int error = FALSE;
4520
4521 val = get_tv_number_chk(rettv, &error);
4522 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004524 clear_tv(rettv);
4525 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004527 else
4528 {
4529 while (end_leader > start_leader)
4530 {
4531 --end_leader;
4532 if (*end_leader == '!')
4533 val = !val;
4534 else if (*end_leader == '-')
4535 val = -val;
4536 }
4537 clear_tv(rettv);
4538 rettv->v_type = VAR_NUMBER;
4539 rettv->vval.v_number = val;
4540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 }
4542
4543 return ret;
4544}
4545
4546/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004547 * Evaluate an "[expr]" or "[expr:expr]" index.
4548 * "*arg" points to the '['.
4549 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4550 */
4551 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004552eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004553 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004554 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004555 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004556 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004557{
4558 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004559 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004560 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004561 long len = -1;
4562 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004563 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004564 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004565
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004566 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004567 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004568 if (verbose)
4569 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004570 return FAIL;
4571 }
4572
Bram Moolenaar8c711452005-01-14 21:53:12 +00004573 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004574 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004575 /*
4576 * dict.name
4577 */
4578 key = *arg + 1;
4579 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4580 ;
4581 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004582 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004583 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004584 }
4585 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004586 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004587 /*
4588 * something[idx]
4589 *
4590 * Get the (first) variable from inside the [].
4591 */
4592 *arg = skipwhite(*arg + 1);
4593 if (**arg == ':')
4594 empty1 = TRUE;
4595 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4596 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004597 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4598 {
4599 /* not a number or string */
4600 clear_tv(&var1);
4601 return FAIL;
4602 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004603
4604 /*
4605 * Get the second variable from inside the [:].
4606 */
4607 if (**arg == ':')
4608 {
4609 range = TRUE;
4610 *arg = skipwhite(*arg + 1);
4611 if (**arg == ']')
4612 empty2 = TRUE;
4613 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004615 if (!empty1)
4616 clear_tv(&var1);
4617 return FAIL;
4618 }
4619 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4620 {
4621 /* not a number or string */
4622 if (!empty1)
4623 clear_tv(&var1);
4624 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004625 return FAIL;
4626 }
4627 }
4628
4629 /* Check for the ']'. */
4630 if (**arg != ']')
4631 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004632 if (verbose)
4633 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004634 clear_tv(&var1);
4635 if (range)
4636 clear_tv(&var2);
4637 return FAIL;
4638 }
4639 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004640 }
4641
4642 if (evaluate)
4643 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004644 n1 = 0;
4645 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004647 n1 = get_tv_number(&var1);
4648 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004649 }
4650 if (range)
4651 {
4652 if (empty2)
4653 n2 = -1;
4654 else
4655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004656 n2 = get_tv_number(&var2);
4657 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004658 }
4659 }
4660
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004661 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004662 {
4663 case VAR_NUMBER:
4664 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004665 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004666 len = (long)STRLEN(s);
4667 if (range)
4668 {
4669 /* The resulting variable is a substring. If the indexes
4670 * are out of range the result is empty. */
4671 if (n1 < 0)
4672 {
4673 n1 = len + n1;
4674 if (n1 < 0)
4675 n1 = 0;
4676 }
4677 if (n2 < 0)
4678 n2 = len + n2;
4679 else if (n2 >= len)
4680 n2 = len;
4681 if (n1 >= len || n2 < 0 || n1 > n2)
4682 s = NULL;
4683 else
4684 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4685 }
4686 else
4687 {
4688 /* The resulting variable is a string of a single
4689 * character. If the index is too big or negative the
4690 * result is empty. */
4691 if (n1 >= len || n1 < 0)
4692 s = NULL;
4693 else
4694 s = vim_strnsave(s + n1, 1);
4695 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004696 clear_tv(rettv);
4697 rettv->v_type = VAR_STRING;
4698 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004699 break;
4700
4701 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004702 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004703 if (n1 < 0)
4704 n1 = len + n1;
4705 if (!empty1 && (n1 < 0 || n1 >= len))
4706 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004707 if (verbose)
4708 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004709 return FAIL;
4710 }
4711 if (range)
4712 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004713 list_T *l;
4714 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004715
4716 if (n2 < 0)
4717 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004718 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004720 if (verbose)
4721 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722 return FAIL;
4723 }
4724 l = list_alloc();
4725 if (l == NULL)
4726 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004727 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004728 n1 <= n2; ++n1)
4729 {
4730 if (list_append_tv(l, &item->li_tv) == FAIL)
4731 {
4732 list_free(l);
4733 return FAIL;
4734 }
4735 item = item->li_next;
4736 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004737 clear_tv(rettv);
4738 rettv->v_type = VAR_LIST;
4739 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004740 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004741 }
4742 else
4743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004744 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004746 clear_tv(rettv);
4747 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004748 }
4749 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004750
4751 case VAR_DICT:
4752 if (range)
4753 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004754 if (verbose)
4755 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004756 if (len == -1)
4757 clear_tv(&var1);
4758 return FAIL;
4759 }
4760 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004761 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004762
4763 if (len == -1)
4764 {
4765 key = get_tv_string(&var1);
4766 if (*key == NUL)
4767 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004768 if (verbose)
4769 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004770 clear_tv(&var1);
4771 return FAIL;
4772 }
4773 }
4774
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004775 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004776
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004777 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004778 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004779 if (len == -1)
4780 clear_tv(&var1);
4781 if (item == NULL)
4782 return FAIL;
4783
4784 copy_tv(&item->di_tv, &var1);
4785 clear_tv(rettv);
4786 *rettv = var1;
4787 }
4788 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004789 }
4790 }
4791
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004792 return OK;
4793}
4794
4795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 * Get an option value.
4797 * "arg" points to the '&' or '+' before the option name.
4798 * "arg" is advanced to character after the option name.
4799 * Return OK or FAIL.
4800 */
4801 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004802get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004804 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 int evaluate;
4806{
4807 char_u *option_end;
4808 long numval;
4809 char_u *stringval;
4810 int opt_type;
4811 int c;
4812 int working = (**arg == '+'); /* has("+option") */
4813 int ret = OK;
4814 int opt_flags;
4815
4816 /*
4817 * Isolate the option name and find its value.
4818 */
4819 option_end = find_option_end(arg, &opt_flags);
4820 if (option_end == NULL)
4821 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004822 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 EMSG2(_("E112: Option name missing: %s"), *arg);
4824 return FAIL;
4825 }
4826
4827 if (!evaluate)
4828 {
4829 *arg = option_end;
4830 return OK;
4831 }
4832
4833 c = *option_end;
4834 *option_end = NUL;
4835 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837
4838 if (opt_type == -3) /* invalid name */
4839 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004840 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 EMSG2(_("E113: Unknown option: %s"), *arg);
4842 ret = FAIL;
4843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004844 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 {
4846 if (opt_type == -2) /* hidden string option */
4847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004848 rettv->v_type = VAR_STRING;
4849 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 }
4851 else if (opt_type == -1) /* hidden number option */
4852 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004853 rettv->v_type = VAR_NUMBER;
4854 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856 else if (opt_type == 1) /* number option */
4857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004858 rettv->v_type = VAR_NUMBER;
4859 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 }
4861 else /* string option */
4862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004863 rettv->v_type = VAR_STRING;
4864 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 }
4866 }
4867 else if (working && (opt_type == -2 || opt_type == -1))
4868 ret = FAIL;
4869
4870 *option_end = c; /* put back for error messages */
4871 *arg = option_end;
4872
4873 return ret;
4874}
4875
4876/*
4877 * Allocate a variable for a string constant.
4878 * Return OK or FAIL.
4879 */
4880 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004881get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 int evaluate;
4885{
4886 char_u *p;
4887 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 int extra = 0;
4889
4890 /*
4891 * Find the end of the string, skipping backslashed characters.
4892 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004893 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 {
4895 if (*p == '\\' && p[1] != NUL)
4896 {
4897 ++p;
4898 /* A "\<x>" form occupies at least 4 characters, and produces up
4899 * to 6 characters: reserve space for 2 extra */
4900 if (*p == '<')
4901 extra += 2;
4902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
4904
4905 if (*p != '"')
4906 {
4907 EMSG2(_("E114: Missing quote: %s"), *arg);
4908 return FAIL;
4909 }
4910
4911 /* If only parsing, set *arg and return here */
4912 if (!evaluate)
4913 {
4914 *arg = p + 1;
4915 return OK;
4916 }
4917
4918 /*
4919 * Copy the string into allocated memory, handling backslashed
4920 * characters.
4921 */
4922 name = alloc((unsigned)(p - *arg + extra));
4923 if (name == NULL)
4924 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004925 rettv->v_type = VAR_STRING;
4926 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
Bram Moolenaar8c711452005-01-14 21:53:12 +00004928 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 {
4930 if (*p == '\\')
4931 {
4932 switch (*++p)
4933 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004934 case 'b': *name++ = BS; ++p; break;
4935 case 'e': *name++ = ESC; ++p; break;
4936 case 'f': *name++ = FF; ++p; break;
4937 case 'n': *name++ = NL; ++p; break;
4938 case 'r': *name++ = CAR; ++p; break;
4939 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
4941 case 'X': /* hex: "\x1", "\x12" */
4942 case 'x':
4943 case 'u': /* Unicode: "\u0023" */
4944 case 'U':
4945 if (vim_isxdigit(p[1]))
4946 {
4947 int n, nr;
4948 int c = toupper(*p);
4949
4950 if (c == 'X')
4951 n = 2;
4952 else
4953 n = 4;
4954 nr = 0;
4955 while (--n >= 0 && vim_isxdigit(p[1]))
4956 {
4957 ++p;
4958 nr = (nr << 4) + hex2nr(*p);
4959 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004960 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961#ifdef FEAT_MBYTE
4962 /* For "\u" store the number according to
4963 * 'encoding'. */
4964 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004965 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 else
4967#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 break;
4971
4972 /* octal: "\1", "\12", "\123" */
4973 case '0':
4974 case '1':
4975 case '2':
4976 case '3':
4977 case '4':
4978 case '5':
4979 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980 case '7': *name = *p++ - '0';
4981 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 *name = (*name << 3) + *p++ - '0';
4984 if (*p >= '0' && *p <= '7')
4985 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 break;
4989
4990 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004991 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 if (extra != 0)
4993 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 break;
4996 }
4997 /* FALLTHROUGH */
4998
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 break;
5001 }
5002 }
5003 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005004 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005007 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 *arg = p + 1;
5009
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 return OK;
5011}
5012
5013/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 * Return OK or FAIL.
5016 */
5017 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005018get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 int evaluate;
5022{
5023 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005024 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005025 int reduce = 0;
5026
5027 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005028 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005029 */
5030 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5031 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005033 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005034 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005035 break;
5036 ++reduce;
5037 ++p;
5038 }
5039 }
5040
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005042 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005044 return FAIL;
5045 }
5046
Bram Moolenaar8c711452005-01-14 21:53:12 +00005047 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005048 if (!evaluate)
5049 {
5050 *arg = p + 1;
5051 return OK;
5052 }
5053
5054 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005056 */
5057 str = alloc((unsigned)((p - *arg) - reduce));
5058 if (str == NULL)
5059 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005060 rettv->v_type = VAR_STRING;
5061 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005062
Bram Moolenaar8c711452005-01-14 21:53:12 +00005063 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005064 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005066 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005068 break;
5069 ++p;
5070 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005072 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005073 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005074 *arg = p + 1;
5075
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005076 return OK;
5077}
5078
5079/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005080 * Allocate a variable for a List and fill it from "*arg".
5081 * Return OK or FAIL.
5082 */
5083 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005085 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005086 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005087 int evaluate;
5088{
Bram Moolenaar33570922005-01-25 22:26:29 +00005089 list_T *l = NULL;
5090 typval_T tv;
5091 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005092
5093 if (evaluate)
5094 {
5095 l = list_alloc();
5096 if (l == NULL)
5097 return FAIL;
5098 }
5099
5100 *arg = skipwhite(*arg + 1);
5101 while (**arg != ']' && **arg != NUL)
5102 {
5103 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5104 goto failret;
5105 if (evaluate)
5106 {
5107 item = listitem_alloc();
5108 if (item != NULL)
5109 {
5110 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005111 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005112 list_append(l, item);
5113 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005114 else
5115 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005116 }
5117
5118 if (**arg == ']')
5119 break;
5120 if (**arg != ',')
5121 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005122 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005123 goto failret;
5124 }
5125 *arg = skipwhite(*arg + 1);
5126 }
5127
5128 if (**arg != ']')
5129 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005131failret:
5132 if (evaluate)
5133 list_free(l);
5134 return FAIL;
5135 }
5136
5137 *arg = skipwhite(*arg + 1);
5138 if (evaluate)
5139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005140 rettv->v_type = VAR_LIST;
5141 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005142 ++l->lv_refcount;
5143 }
5144
5145 return OK;
5146}
5147
5148/*
5149 * Allocate an empty header for a list.
5150 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005151 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005152list_alloc()
5153{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005154 list_T *l;
5155
5156 l = (list_T *)alloc_clear(sizeof(list_T));
5157 if (l != NULL)
5158 {
5159 /* Prepend the list to the list of lists for garbage collection. */
5160 if (first_list != NULL)
5161 first_list->lv_used_prev = l;
5162 l->lv_used_prev = NULL;
5163 l->lv_used_next = first_list;
5164 first_list = l;
5165 }
5166 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005167}
5168
5169/*
5170 * Unreference a list: decrement the reference count and free it when it
5171 * becomes zero.
5172 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005173 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005175 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005176{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005177 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5178 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179}
5180
5181/*
5182 * Free a list, including all items it points to.
5183 * Ignores the reference count.
5184 */
5185 static void
5186list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005187 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005188{
Bram Moolenaar33570922005-01-25 22:26:29 +00005189 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005190
Bram Moolenaard9fba312005-06-26 22:34:35 +00005191 /* Avoid that recursive reference to the list frees us again. */
5192 l->lv_refcount = DEL_REFCOUNT;
5193
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005194 /* Remove the list from the list of lists for garbage collection. */
5195 if (l->lv_used_prev == NULL)
5196 first_list = l->lv_used_next;
5197 else
5198 l->lv_used_prev->lv_used_next = l->lv_used_next;
5199 if (l->lv_used_next != NULL)
5200 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5201
Bram Moolenaard9fba312005-06-26 22:34:35 +00005202 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005203 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005204 /* Remove the item before deleting it. */
5205 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005206 listitem_free(item);
5207 }
5208 vim_free(l);
5209}
5210
5211/*
5212 * Allocate a list item.
5213 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005214 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005215listitem_alloc()
5216{
Bram Moolenaar33570922005-01-25 22:26:29 +00005217 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005218}
5219
5220/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005221 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005222 */
5223 static void
5224listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005225 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005227 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228 vim_free(item);
5229}
5230
5231/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005232 * Remove a list item from a List and free it. Also clears the value.
5233 */
5234 static void
5235listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005236 list_T *l;
5237 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005238{
5239 list_remove(l, item, item);
5240 listitem_free(item);
5241}
5242
5243/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244 * Get the number of items in a list.
5245 */
5246 static long
5247list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005248 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 if (l == NULL)
5251 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005252 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253}
5254
5255/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005256 * Return TRUE when two lists have exactly the same values.
5257 */
5258 static int
5259list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005260 list_T *l1;
5261 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005262 int ic; /* ignore case for strings */
5263{
Bram Moolenaar33570922005-01-25 22:26:29 +00005264 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005265
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005266 if (list_len(l1) != list_len(l2))
5267 return FALSE;
5268
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005269 for (item1 = l1->lv_first, item2 = l2->lv_first;
5270 item1 != NULL && item2 != NULL;
5271 item1 = item1->li_next, item2 = item2->li_next)
5272 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5273 return FALSE;
5274 return item1 == NULL && item2 == NULL;
5275}
5276
5277/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005278 * Return TRUE when two dictionaries have exactly the same key/values.
5279 */
5280 static int
5281dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005282 dict_T *d1;
5283 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005284 int ic; /* ignore case for strings */
5285{
Bram Moolenaar33570922005-01-25 22:26:29 +00005286 hashitem_T *hi;
5287 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005288 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005289
5290 if (dict_len(d1) != dict_len(d2))
5291 return FALSE;
5292
Bram Moolenaar33570922005-01-25 22:26:29 +00005293 todo = d1->dv_hashtab.ht_used;
5294 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005295 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005296 if (!HASHITEM_EMPTY(hi))
5297 {
5298 item2 = dict_find(d2, hi->hi_key, -1);
5299 if (item2 == NULL)
5300 return FALSE;
5301 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5302 return FALSE;
5303 --todo;
5304 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005305 }
5306 return TRUE;
5307}
5308
5309/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005310 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005311 * Compares the items just like "==" would compare them, but strings and
5312 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005313 */
5314 static int
5315tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005316 typval_T *tv1;
5317 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005318 int ic; /* ignore case */
5319{
5320 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005322
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005323 if (tv1->v_type != tv2->v_type)
5324 return FALSE;
5325
5326 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005327 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005328 case VAR_LIST:
5329 /* recursive! */
5330 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5331
5332 case VAR_DICT:
5333 /* recursive! */
5334 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5335
5336 case VAR_FUNC:
5337 return (tv1->vval.v_string != NULL
5338 && tv2->vval.v_string != NULL
5339 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5340
5341 case VAR_NUMBER:
5342 return tv1->vval.v_number == tv2->vval.v_number;
5343
5344 case VAR_STRING:
5345 s1 = get_tv_string_buf(tv1, buf1);
5346 s2 = get_tv_string_buf(tv2, buf2);
5347 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005348 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005349
5350 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005351 return TRUE;
5352}
5353
5354/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 * Locate item with index "n" in list "l" and return it.
5356 * A negative index is counted from the end; -1 is the last item.
5357 * Returns NULL when "n" is out of range.
5358 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005359 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005361 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 long n;
5363{
Bram Moolenaar33570922005-01-25 22:26:29 +00005364 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 long idx;
5366
5367 if (l == NULL)
5368 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005369
5370 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005372 n = l->lv_len + n;
5373
5374 /* Check for index out of range. */
5375 if (n < 0 || n >= l->lv_len)
5376 return NULL;
5377
5378 /* When there is a cached index may start search from there. */
5379 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005381 if (n < l->lv_idx / 2)
5382 {
5383 /* closest to the start of the list */
5384 item = l->lv_first;
5385 idx = 0;
5386 }
5387 else if (n > (l->lv_idx + l->lv_len) / 2)
5388 {
5389 /* closest to the end of the list */
5390 item = l->lv_last;
5391 idx = l->lv_len - 1;
5392 }
5393 else
5394 {
5395 /* closest to the cached index */
5396 item = l->lv_idx_item;
5397 idx = l->lv_idx;
5398 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 }
5400 else
5401 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005402 if (n < l->lv_len / 2)
5403 {
5404 /* closest to the start of the list */
5405 item = l->lv_first;
5406 idx = 0;
5407 }
5408 else
5409 {
5410 /* closest to the end of the list */
5411 item = l->lv_last;
5412 idx = l->lv_len - 1;
5413 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005415
5416 while (n > idx)
5417 {
5418 /* search forward */
5419 item = item->li_next;
5420 ++idx;
5421 }
5422 while (n < idx)
5423 {
5424 /* search backward */
5425 item = item->li_prev;
5426 --idx;
5427 }
5428
5429 /* cache the used index */
5430 l->lv_idx = idx;
5431 l->lv_idx_item = item;
5432
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 return item;
5434}
5435
5436/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005437 * Locate "item" list "l" and return its index.
5438 * Returns -1 when "item" is not in the list.
5439 */
5440 static long
5441list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005442 list_T *l;
5443 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005444{
5445 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005446 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005447
5448 if (l == NULL)
5449 return -1;
5450 idx = 0;
5451 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5452 ++idx;
5453 if (li == NULL)
5454 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005455 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005456}
5457
5458/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 * Append item "item" to the end of list "l".
5460 */
5461 static void
5462list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005463 list_T *l;
5464 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465{
5466 if (l->lv_last == NULL)
5467 {
5468 /* empty list */
5469 l->lv_first = item;
5470 l->lv_last = item;
5471 item->li_prev = NULL;
5472 }
5473 else
5474 {
5475 l->lv_last->li_next = item;
5476 item->li_prev = l->lv_last;
5477 l->lv_last = item;
5478 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005479 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005480 item->li_next = NULL;
5481}
5482
5483/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005484 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005485 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486 */
5487 static int
5488list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005489 list_T *l;
5490 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005492 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493
Bram Moolenaar05159a02005-02-26 23:04:13 +00005494 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005496 copy_tv(tv, &li->li_tv);
5497 list_append(l, li);
5498 return OK;
5499}
5500
5501/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005502 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005503 * Return FAIL when out of memory.
5504 */
5505 int
5506list_append_dict(list, dict)
5507 list_T *list;
5508 dict_T *dict;
5509{
5510 listitem_T *li = listitem_alloc();
5511
5512 if (li == NULL)
5513 return FAIL;
5514 li->li_tv.v_type = VAR_DICT;
5515 li->li_tv.v_lock = 0;
5516 li->li_tv.vval.v_dict = dict;
5517 list_append(list, li);
5518 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 return OK;
5520}
5521
5522/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005523 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005524 * If "item" is NULL append at the end.
5525 * Return FAIL when out of memory.
5526 */
5527 static int
5528list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005529 list_T *l;
5530 typval_T *tv;
5531 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005532{
Bram Moolenaar33570922005-01-25 22:26:29 +00005533 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005534
5535 if (ni == NULL)
5536 return FAIL;
5537 copy_tv(tv, &ni->li_tv);
5538 if (item == NULL)
5539 /* Append new item at end of list. */
5540 list_append(l, ni);
5541 else
5542 {
5543 /* Insert new item before existing item. */
5544 ni->li_prev = item->li_prev;
5545 ni->li_next = item;
5546 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005547 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005548 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005549 ++l->lv_idx;
5550 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005551 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005552 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005553 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005554 l->lv_idx_item = NULL;
5555 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005556 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005557 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005558 }
5559 return OK;
5560}
5561
5562/*
5563 * Extend "l1" with "l2".
5564 * If "bef" is NULL append at the end, otherwise insert before this item.
5565 * Returns FAIL when out of memory.
5566 */
5567 static int
5568list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005569 list_T *l1;
5570 list_T *l2;
5571 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005572{
Bram Moolenaar33570922005-01-25 22:26:29 +00005573 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005574
5575 for (item = l2->lv_first; item != NULL; item = item->li_next)
5576 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5577 return FAIL;
5578 return OK;
5579}
5580
5581/*
5582 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5583 * Return FAIL when out of memory.
5584 */
5585 static int
5586list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005587 list_T *l1;
5588 list_T *l2;
5589 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005590{
Bram Moolenaar33570922005-01-25 22:26:29 +00005591 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005592
5593 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005594 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005595 if (l == NULL)
5596 return FAIL;
5597 tv->v_type = VAR_LIST;
5598 tv->vval.v_list = l;
5599
5600 /* append all items from the second list */
5601 return list_extend(l, l2, NULL);
5602}
5603
5604/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005605 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005606 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005607 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608 * Returns NULL when out of memory.
5609 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005610 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005611list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005612 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005614 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005615{
Bram Moolenaar33570922005-01-25 22:26:29 +00005616 list_T *copy;
5617 listitem_T *item;
5618 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619
5620 if (orig == NULL)
5621 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005622
5623 copy = list_alloc();
5624 if (copy != NULL)
5625 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005626 if (copyID != 0)
5627 {
5628 /* Do this before adding the items, because one of the items may
5629 * refer back to this list. */
5630 orig->lv_copyID = copyID;
5631 orig->lv_copylist = copy;
5632 }
5633 for (item = orig->lv_first; item != NULL && !got_int;
5634 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005635 {
5636 ni = listitem_alloc();
5637 if (ni == NULL)
5638 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005639 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005640 {
5641 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5642 {
5643 vim_free(ni);
5644 break;
5645 }
5646 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005647 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005648 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005649 list_append(copy, ni);
5650 }
5651 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005652 if (item != NULL)
5653 {
5654 list_unref(copy);
5655 copy = NULL;
5656 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005657 }
5658
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005659 return copy;
5660}
5661
5662/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005663 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005664 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005665 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005666 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005667list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005668 list_T *l;
5669 listitem_T *item;
5670 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005671{
Bram Moolenaar33570922005-01-25 22:26:29 +00005672 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005673
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005674 /* notify watchers */
5675 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005676 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005677 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005678 list_fix_watch(l, ip);
5679 if (ip == item2)
5680 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005681 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005682
5683 if (item2->li_next == NULL)
5684 l->lv_last = item->li_prev;
5685 else
5686 item2->li_next->li_prev = item->li_prev;
5687 if (item->li_prev == NULL)
5688 l->lv_first = item2->li_next;
5689 else
5690 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005691 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005692}
5693
5694/*
5695 * Return an allocated string with the string representation of a list.
5696 * May return NULL.
5697 */
5698 static char_u *
5699list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005700 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005701{
5702 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005703
5704 if (tv->vval.v_list == NULL)
5705 return NULL;
5706 ga_init2(&ga, (int)sizeof(char), 80);
5707 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005708 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5709 {
5710 vim_free(ga.ga_data);
5711 return NULL;
5712 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005713 ga_append(&ga, ']');
5714 ga_append(&ga, NUL);
5715 return (char_u *)ga.ga_data;
5716}
5717
5718/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005719 * Join list "l" into a string in "*gap", using separator "sep".
5720 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005721 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005722 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005723 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005724list_join(gap, l, sep, echo)
5725 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005726 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005727 char_u *sep;
5728 int echo;
5729{
5730 int first = TRUE;
5731 char_u *tofree;
5732 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005733 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005734 char_u *s;
5735
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005736 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005737 {
5738 if (first)
5739 first = FALSE;
5740 else
5741 ga_concat(gap, sep);
5742
5743 if (echo)
5744 s = echo_string(&item->li_tv, &tofree, numbuf);
5745 else
5746 s = tv2string(&item->li_tv, &tofree, numbuf);
5747 if (s != NULL)
5748 ga_concat(gap, s);
5749 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005750 if (s == NULL)
5751 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005753 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754}
5755
5756/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005757 * Garbage collection for lists and dictionaries.
5758 *
5759 * We use reference counts to be able to free most items right away when they
5760 * are no longer used. But for composite items it's possible that it becomes
5761 * unused while the reference count is > 0: When there is a recursive
5762 * reference. Example:
5763 * :let l = [1, 2, 3]
5764 * :let d = {9: l}
5765 * :let l[1] = d
5766 *
5767 * Since this is quite unusual we handle this with garbage collection: every
5768 * once in a while find out which lists and dicts are not referenced from any
5769 * variable.
5770 *
5771 * Here is a good reference text about garbage collection (refers to Python
5772 * but it applies to all reference-counting mechanisms):
5773 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005774 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005775
5776/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005777 * Do garbage collection for lists and dicts.
5778 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005779 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005780 int
5781garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005782{
5783 dict_T *dd;
5784 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005785 int copyID = ++current_copyID;
5786 buf_T *buf;
5787 win_T *wp;
5788 int i;
5789 funccall_T *fc;
5790 int did_free = FALSE;
5791
5792 /*
5793 * 1. Go through all accessible variables and mark all lists and dicts
5794 * with copyID.
5795 */
5796 /* script-local variables */
5797 for (i = 1; i <= ga_scripts.ga_len; ++i)
5798 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5799
5800 /* buffer-local variables */
5801 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5802 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5803
5804 /* window-local variables */
5805 FOR_ALL_WINDOWS(wp)
5806 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5807
5808 /* global variables */
5809 set_ref_in_ht(&globvarht, copyID);
5810
5811 /* function-local variables */
5812 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5813 {
5814 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5815 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5816 }
5817
5818 /*
5819 * 2. Go through the list of dicts and free items without the copyID.
5820 */
5821 for (dd = first_dict; dd != NULL; )
5822 if (dd->dv_copyID != copyID)
5823 {
5824 dict_free(dd);
5825 did_free = TRUE;
5826
5827 /* restart, next dict may also have been freed */
5828 dd = first_dict;
5829 }
5830 else
5831 dd = dd->dv_used_next;
5832
5833 /*
5834 * 3. Go through the list of lists and free items without the copyID.
5835 */
5836 for (ll = first_list; ll != NULL; )
5837 if (ll->lv_copyID != copyID)
5838 {
5839 list_free(ll);
5840 did_free = TRUE;
5841
5842 /* restart, next dict may also have been freed */
5843 ll = first_list;
5844 }
5845 else
5846 ll = ll->lv_used_next;
5847
5848 return did_free;
5849}
5850
5851/*
5852 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5853 */
5854 static void
5855set_ref_in_ht(ht, copyID)
5856 hashtab_T *ht;
5857 int copyID;
5858{
5859 int todo;
5860 hashitem_T *hi;
5861
5862 todo = ht->ht_used;
5863 for (hi = ht->ht_array; todo > 0; ++hi)
5864 if (!HASHITEM_EMPTY(hi))
5865 {
5866 --todo;
5867 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5868 }
5869}
5870
5871/*
5872 * Mark all lists and dicts referenced through list "l" with "copyID".
5873 */
5874 static void
5875set_ref_in_list(l, copyID)
5876 list_T *l;
5877 int copyID;
5878{
5879 listitem_T *li;
5880
5881 for (li = l->lv_first; li != NULL; li = li->li_next)
5882 set_ref_in_item(&li->li_tv, copyID);
5883}
5884
5885/*
5886 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5887 */
5888 static void
5889set_ref_in_item(tv, copyID)
5890 typval_T *tv;
5891 int copyID;
5892{
5893 dict_T *dd;
5894 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005895
5896 switch (tv->v_type)
5897 {
5898 case VAR_DICT:
5899 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005900 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005901 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005902 /* Didn't see this dict yet. */
5903 dd->dv_copyID = copyID;
5904 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005905 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005906 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005907
5908 case VAR_LIST:
5909 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005910 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005911 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005912 /* Didn't see this list yet. */
5913 ll->lv_copyID = copyID;
5914 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005915 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005916 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005917 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005918 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005919}
5920
5921/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005922 * Allocate an empty header for a dictionary.
5923 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005924 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005925dict_alloc()
5926{
Bram Moolenaar33570922005-01-25 22:26:29 +00005927 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005928
Bram Moolenaar33570922005-01-25 22:26:29 +00005929 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005930 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005931 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005932 /* Add the list to the hashtable for garbage collection. */
5933 if (first_dict != NULL)
5934 first_dict->dv_used_prev = d;
5935 d->dv_used_next = first_dict;
5936 d->dv_used_prev = NULL;
5937
Bram Moolenaar33570922005-01-25 22:26:29 +00005938 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005939 d->dv_lock = 0;
5940 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005941 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005942 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005943 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005944}
5945
5946/*
5947 * Unreference a Dictionary: decrement the reference count and free it when it
5948 * becomes zero.
5949 */
5950 static void
5951dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005952 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005954 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
5955 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005956}
5957
5958/*
5959 * Free a Dictionary, including all items it contains.
5960 * Ignores the reference count.
5961 */
5962 static void
5963dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005964 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005965{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005966 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005967 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005968 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005969
Bram Moolenaard9fba312005-06-26 22:34:35 +00005970 /* Avoid that recursive reference to the dict frees us again. */
5971 d->dv_refcount = DEL_REFCOUNT;
5972
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005973 /* Remove the dict from the list of dicts for garbage collection. */
5974 if (d->dv_used_prev == NULL)
5975 first_dict = d->dv_used_next;
5976 else
5977 d->dv_used_prev->dv_used_next = d->dv_used_next;
5978 if (d->dv_used_next != NULL)
5979 d->dv_used_next->dv_used_prev = d->dv_used_prev;
5980
5981 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005982 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00005983 todo = d->dv_hashtab.ht_used;
5984 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005985 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005986 if (!HASHITEM_EMPTY(hi))
5987 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005988 /* Remove the item before deleting it, just in case there is
5989 * something recursive causing trouble. */
5990 di = HI2DI(hi);
5991 hash_remove(&d->dv_hashtab, hi);
5992 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005993 --todo;
5994 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005995 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005996 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005997 vim_free(d);
5998}
5999
6000/*
6001 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006002 * The "key" is copied to the new item.
6003 * Note that the value of the item "di_tv" still needs to be initialized!
6004 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006005 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006006 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006007dictitem_alloc(key)
6008 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006009{
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006011
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006013 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006014 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006015 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006016 di->di_flags = 0;
6017 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006018 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006019}
6020
6021/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006022 * Make a copy of a Dictionary item.
6023 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006024 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006025dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006026 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006027{
Bram Moolenaar33570922005-01-25 22:26:29 +00006028 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006029
Bram Moolenaar33570922005-01-25 22:26:29 +00006030 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006031 if (di != NULL)
6032 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006033 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006034 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006035 copy_tv(&org->di_tv, &di->di_tv);
6036 }
6037 return di;
6038}
6039
6040/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006041 * Remove item "item" from Dictionary "dict" and free it.
6042 */
6043 static void
6044dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 dict_T *dict;
6046 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006047{
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006049
Bram Moolenaar33570922005-01-25 22:26:29 +00006050 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006051 if (HASHITEM_EMPTY(hi))
6052 EMSG2(_(e_intern2), "dictitem_remove()");
6053 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006054 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006055 dictitem_free(item);
6056}
6057
6058/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006059 * Free a dict item. Also clears the value.
6060 */
6061 static void
6062dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006063 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006064{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006065 clear_tv(&item->di_tv);
6066 vim_free(item);
6067}
6068
6069/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006070 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6071 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006072 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006073 * Returns NULL when out of memory.
6074 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006075 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006076dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006077 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006078 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006079 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006080{
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 dict_T *copy;
6082 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006083 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006084 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006085
6086 if (orig == NULL)
6087 return NULL;
6088
6089 copy = dict_alloc();
6090 if (copy != NULL)
6091 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006092 if (copyID != 0)
6093 {
6094 orig->dv_copyID = copyID;
6095 orig->dv_copydict = copy;
6096 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006097 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006098 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006099 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006100 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006101 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006102 --todo;
6103
6104 di = dictitem_alloc(hi->hi_key);
6105 if (di == NULL)
6106 break;
6107 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006108 {
6109 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6110 copyID) == FAIL)
6111 {
6112 vim_free(di);
6113 break;
6114 }
6115 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006116 else
6117 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6118 if (dict_add(copy, di) == FAIL)
6119 {
6120 dictitem_free(di);
6121 break;
6122 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006123 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006124 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006125
Bram Moolenaare9a41262005-01-15 22:18:47 +00006126 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006127 if (todo > 0)
6128 {
6129 dict_unref(copy);
6130 copy = NULL;
6131 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006132 }
6133
6134 return copy;
6135}
6136
6137/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006138 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006139 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006140 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006141 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006142dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 dict_T *d;
6144 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006145{
Bram Moolenaar33570922005-01-25 22:26:29 +00006146 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006147}
6148
Bram Moolenaar8c711452005-01-14 21:53:12 +00006149/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006150 * Add a number or string entry to dictionary "d".
6151 * When "str" is NULL use number "nr", otherwise use "str".
6152 * Returns FAIL when out of memory and when key already exists.
6153 */
6154 int
6155dict_add_nr_str(d, key, nr, str)
6156 dict_T *d;
6157 char *key;
6158 long nr;
6159 char_u *str;
6160{
6161 dictitem_T *item;
6162
6163 item = dictitem_alloc((char_u *)key);
6164 if (item == NULL)
6165 return FAIL;
6166 item->di_tv.v_lock = 0;
6167 if (str == NULL)
6168 {
6169 item->di_tv.v_type = VAR_NUMBER;
6170 item->di_tv.vval.v_number = nr;
6171 }
6172 else
6173 {
6174 item->di_tv.v_type = VAR_STRING;
6175 item->di_tv.vval.v_string = vim_strsave(str);
6176 }
6177 if (dict_add(d, item) == FAIL)
6178 {
6179 dictitem_free(item);
6180 return FAIL;
6181 }
6182 return OK;
6183}
6184
6185/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006186 * Get the number of items in a Dictionary.
6187 */
6188 static long
6189dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006190 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006191{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006192 if (d == NULL)
6193 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006194 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006195}
6196
6197/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006198 * Find item "key[len]" in Dictionary "d".
6199 * If "len" is negative use strlen(key).
6200 * Returns NULL when not found.
6201 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006202 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006203dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006204 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006205 char_u *key;
6206 int len;
6207{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006208#define AKEYLEN 200
6209 char_u buf[AKEYLEN];
6210 char_u *akey;
6211 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006212 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006213
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006214 if (len < 0)
6215 akey = key;
6216 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006217 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006218 tofree = akey = vim_strnsave(key, len);
6219 if (akey == NULL)
6220 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006221 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006222 else
6223 {
6224 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006225 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006226 akey = buf;
6227 }
6228
Bram Moolenaar33570922005-01-25 22:26:29 +00006229 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006230 vim_free(tofree);
6231 if (HASHITEM_EMPTY(hi))
6232 return NULL;
6233 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006234}
6235
6236/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006237 * Get a string item from a dictionary in allocated memory.
6238 * Returns NULL if the entry doesn't exist or out of memory.
6239 */
6240 char_u *
6241get_dict_string(d, key)
6242 dict_T *d;
6243 char_u *key;
6244{
6245 dictitem_T *di;
6246
6247 di = dict_find(d, key, -1);
6248 if (di == NULL)
6249 return NULL;
6250 return vim_strsave(get_tv_string(&di->di_tv));
6251}
6252
6253/*
6254 * Get a number item from a dictionary.
6255 * Returns 0 if the entry doesn't exist or out of memory.
6256 */
6257 long
6258get_dict_number(d, key)
6259 dict_T *d;
6260 char_u *key;
6261{
6262 dictitem_T *di;
6263
6264 di = dict_find(d, key, -1);
6265 if (di == NULL)
6266 return 0;
6267 return get_tv_number(&di->di_tv);
6268}
6269
6270/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006271 * Return an allocated string with the string representation of a Dictionary.
6272 * May return NULL.
6273 */
6274 static char_u *
6275dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006276 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006277{
6278 garray_T ga;
6279 int first = TRUE;
6280 char_u *tofree;
6281 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006282 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006283 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006285 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006286
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006287 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006288 return NULL;
6289 ga_init2(&ga, (int)sizeof(char), 80);
6290 ga_append(&ga, '{');
6291
Bram Moolenaar33570922005-01-25 22:26:29 +00006292 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006293 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006294 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006295 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006296 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006297 --todo;
6298
6299 if (first)
6300 first = FALSE;
6301 else
6302 ga_concat(&ga, (char_u *)", ");
6303
6304 tofree = string_quote(hi->hi_key, FALSE);
6305 if (tofree != NULL)
6306 {
6307 ga_concat(&ga, tofree);
6308 vim_free(tofree);
6309 }
6310 ga_concat(&ga, (char_u *)": ");
6311 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6312 if (s != NULL)
6313 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006314 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006315 if (s == NULL)
6316 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006317 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006318 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006319 if (todo > 0)
6320 {
6321 vim_free(ga.ga_data);
6322 return NULL;
6323 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006324
6325 ga_append(&ga, '}');
6326 ga_append(&ga, NUL);
6327 return (char_u *)ga.ga_data;
6328}
6329
6330/*
6331 * Allocate a variable for a Dictionary and fill it from "*arg".
6332 * Return OK or FAIL. Returns NOTDONE for {expr}.
6333 */
6334 static int
6335get_dict_tv(arg, rettv, evaluate)
6336 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006337 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006338 int evaluate;
6339{
Bram Moolenaar33570922005-01-25 22:26:29 +00006340 dict_T *d = NULL;
6341 typval_T tvkey;
6342 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006343 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006344 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006345 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006346 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006347
6348 /*
6349 * First check if it's not a curly-braces thing: {expr}.
6350 * Must do this without evaluating, otherwise a function may be called
6351 * twice. Unfortunately this means we need to call eval1() twice for the
6352 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006353 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006354 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006355 if (*start != '}')
6356 {
6357 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6358 return FAIL;
6359 if (*start == '}')
6360 return NOTDONE;
6361 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006362
6363 if (evaluate)
6364 {
6365 d = dict_alloc();
6366 if (d == NULL)
6367 return FAIL;
6368 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006369 tvkey.v_type = VAR_UNKNOWN;
6370 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006371
6372 *arg = skipwhite(*arg + 1);
6373 while (**arg != '}' && **arg != NUL)
6374 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006375 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006376 goto failret;
6377 if (**arg != ':')
6378 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006379 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006380 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006381 goto failret;
6382 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006383 key = get_tv_string_buf_chk(&tvkey, buf);
6384 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006386 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6387 if (key != NULL)
6388 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006389 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006390 goto failret;
6391 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006392
6393 *arg = skipwhite(*arg + 1);
6394 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6395 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006396 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006397 goto failret;
6398 }
6399 if (evaluate)
6400 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006401 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006402 if (item != NULL)
6403 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006404 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006405 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006406 clear_tv(&tv);
6407 goto failret;
6408 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006409 item = dictitem_alloc(key);
6410 clear_tv(&tvkey);
6411 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006412 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006413 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006414 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006415 if (dict_add(d, item) == FAIL)
6416 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006417 }
6418 }
6419
6420 if (**arg == '}')
6421 break;
6422 if (**arg != ',')
6423 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006424 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006425 goto failret;
6426 }
6427 *arg = skipwhite(*arg + 1);
6428 }
6429
6430 if (**arg != '}')
6431 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006432 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006433failret:
6434 if (evaluate)
6435 dict_free(d);
6436 return FAIL;
6437 }
6438
6439 *arg = skipwhite(*arg + 1);
6440 if (evaluate)
6441 {
6442 rettv->v_type = VAR_DICT;
6443 rettv->vval.v_dict = d;
6444 ++d->dv_refcount;
6445 }
6446
6447 return OK;
6448}
6449
Bram Moolenaar8c711452005-01-14 21:53:12 +00006450/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451 * Return a string with the string representation of a variable.
6452 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006453 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006454 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006455 * May return NULL;
6456 */
6457 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006458echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006459 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006460 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006461 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006462{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006463 static int recurse = 0;
6464 char_u *r = NULL;
6465
Bram Moolenaar33570922005-01-25 22:26:29 +00006466 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006467 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006468 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006469 *tofree = NULL;
6470 return NULL;
6471 }
6472 ++recurse;
6473
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006474 switch (tv->v_type)
6475 {
6476 case VAR_FUNC:
6477 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006478 r = tv->vval.v_string;
6479 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006480 case VAR_LIST:
6481 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006482 r = *tofree;
6483 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006484 case VAR_DICT:
6485 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006486 r = *tofree;
6487 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006488 case VAR_STRING:
6489 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006490 *tofree = NULL;
6491 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006492 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006493 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006494 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006495 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006496 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006497
6498 --recurse;
6499 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006500}
6501
6502/*
6503 * Return a string with the string representation of a variable.
6504 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6505 * "numbuf" is used for a number.
6506 * Puts quotes around strings, so that they can be parsed back by eval().
6507 * May return NULL;
6508 */
6509 static char_u *
6510tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006511 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006512 char_u **tofree;
6513 char_u *numbuf;
6514{
6515 switch (tv->v_type)
6516 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006517 case VAR_FUNC:
6518 *tofree = string_quote(tv->vval.v_string, TRUE);
6519 return *tofree;
6520 case VAR_STRING:
6521 *tofree = string_quote(tv->vval.v_string, FALSE);
6522 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006523 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006524 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006525 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006526 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006527 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006528 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006529 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006530 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531}
6532
6533/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006534 * Return string "str" in ' quotes, doubling ' characters.
6535 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006536 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006537 */
6538 static char_u *
6539string_quote(str, function)
6540 char_u *str;
6541 int function;
6542{
Bram Moolenaar33570922005-01-25 22:26:29 +00006543 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006544 char_u *p, *r, *s;
6545
Bram Moolenaar33570922005-01-25 22:26:29 +00006546 len = (function ? 13 : 3);
6547 if (str != NULL)
6548 {
6549 len += STRLEN(str);
6550 for (p = str; *p != NUL; mb_ptr_adv(p))
6551 if (*p == '\'')
6552 ++len;
6553 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006554 s = r = alloc(len);
6555 if (r != NULL)
6556 {
6557 if (function)
6558 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006559 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006560 r += 10;
6561 }
6562 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006563 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006564 if (str != NULL)
6565 for (p = str; *p != NUL; )
6566 {
6567 if (*p == '\'')
6568 *r++ = '\'';
6569 MB_COPY_CHAR(p, r);
6570 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006571 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006572 if (function)
6573 *r++ = ')';
6574 *r++ = NUL;
6575 }
6576 return s;
6577}
6578
6579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 * Get the value of an environment variable.
6581 * "arg" is pointing to the '$'. It is advanced to after the name.
6582 * If the environment variable was not set, silently assume it is empty.
6583 * Always return OK.
6584 */
6585 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006586get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 int evaluate;
6590{
6591 char_u *string = NULL;
6592 int len;
6593 int cc;
6594 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006595 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596
6597 ++*arg;
6598 name = *arg;
6599 len = get_env_len(arg);
6600 if (evaluate)
6601 {
6602 if (len != 0)
6603 {
6604 cc = name[len];
6605 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006606 /* first try vim_getenv(), fast for normal environment vars */
6607 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006609 {
6610 if (!mustfree)
6611 string = vim_strsave(string);
6612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 else
6614 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006615 if (mustfree)
6616 vim_free(string);
6617
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 /* next try expanding things like $VIM and ${HOME} */
6619 string = expand_env_save(name - 1);
6620 if (string != NULL && *string == '$')
6621 {
6622 vim_free(string);
6623 string = NULL;
6624 }
6625 }
6626 name[len] = cc;
6627 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006628 rettv->v_type = VAR_STRING;
6629 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 }
6631
6632 return OK;
6633}
6634
6635/*
6636 * Array with names and number of arguments of all internal functions
6637 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6638 */
6639static struct fst
6640{
6641 char *f_name; /* function name */
6642 char f_min_argc; /* minimal number of arguments */
6643 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006644 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646} functions[] =
6647{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006648 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 {"append", 2, 2, f_append},
6650 {"argc", 0, 0, f_argc},
6651 {"argidx", 0, 0, f_argidx},
6652 {"argv", 1, 1, f_argv},
6653 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006654 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 {"bufexists", 1, 1, f_bufexists},
6656 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6657 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6658 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6659 {"buflisted", 1, 1, f_buflisted},
6660 {"bufloaded", 1, 1, f_bufloaded},
6661 {"bufname", 1, 1, f_bufname},
6662 {"bufnr", 1, 1, f_bufnr},
6663 {"bufwinnr", 1, 1, f_bufwinnr},
6664 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006665 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006666 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 {"char2nr", 1, 1, f_char2nr},
6668 {"cindent", 1, 1, f_cindent},
6669 {"col", 1, 1, f_col},
6670 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006672 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 {"cscope_connection",0,3, f_cscope_connection},
6674 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006675 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 {"delete", 1, 1, f_delete},
6677 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006678 {"diff_filler", 1, 1, f_diff_filler},
6679 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006680 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006682 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 {"eventhandler", 0, 0, f_eventhandler},
6684 {"executable", 1, 1, f_executable},
6685 {"exists", 1, 1, f_exists},
6686 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006687 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6689 {"filereadable", 1, 1, f_filereadable},
6690 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006691 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006692 {"finddir", 1, 3, f_finddir},
6693 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 {"fnamemodify", 2, 2, f_fnamemodify},
6695 {"foldclosed", 1, 1, f_foldclosed},
6696 {"foldclosedend", 1, 1, f_foldclosedend},
6697 {"foldlevel", 1, 1, f_foldlevel},
6698 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006699 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006701 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006702 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006703 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006704 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 {"getbufvar", 2, 2, f_getbufvar},
6706 {"getchar", 0, 1, f_getchar},
6707 {"getcharmod", 0, 0, f_getcharmod},
6708 {"getcmdline", 0, 0, f_getcmdline},
6709 {"getcmdpos", 0, 0, f_getcmdpos},
6710 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006711 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006712 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 {"getfsize", 1, 1, f_getfsize},
6714 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006715 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006716 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006717 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006718 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 {"getregtype", 0, 1, f_getregtype},
6720 {"getwinposx", 0, 0, f_getwinposx},
6721 {"getwinposy", 0, 0, f_getwinposy},
6722 {"getwinvar", 2, 2, f_getwinvar},
6723 {"glob", 1, 1, f_glob},
6724 {"globpath", 2, 2, f_globpath},
6725 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006726 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 {"hasmapto", 1, 2, f_hasmapto},
6728 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6729 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6730 {"histadd", 2, 2, f_histadd},
6731 {"histdel", 1, 2, f_histdel},
6732 {"histget", 1, 2, f_histget},
6733 {"histnr", 1, 1, f_histnr},
6734 {"hlID", 1, 1, f_hlID},
6735 {"hlexists", 1, 1, f_hlexists},
6736 {"hostname", 0, 0, f_hostname},
6737 {"iconv", 3, 3, f_iconv},
6738 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006739 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 {"input", 1, 2, f_input},
6741 {"inputdialog", 1, 3, f_inputdialog},
6742 {"inputrestore", 0, 0, f_inputrestore},
6743 {"inputsave", 0, 0, f_inputsave},
6744 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006745 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006747 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006748 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006749 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006750 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006752 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 {"libcall", 3, 3, f_libcall},
6754 {"libcallnr", 3, 3, f_libcallnr},
6755 {"line", 1, 1, f_line},
6756 {"line2byte", 1, 1, f_line2byte},
6757 {"lispindent", 1, 1, f_lispindent},
6758 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006759 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 {"maparg", 1, 2, f_maparg},
6761 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006762 {"match", 2, 4, f_match},
6763 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006764 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006765 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006766 {"max", 1, 1, f_max},
6767 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006768#ifdef vim_mkdir
6769 {"mkdir", 1, 3, f_mkdir},
6770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 {"mode", 0, 0, f_mode},
6772 {"nextnonblank", 1, 1, f_nextnonblank},
6773 {"nr2char", 1, 1, f_nr2char},
6774 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006775 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006776 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {"remote_expr", 2, 3, f_remote_expr},
6778 {"remote_foreground", 1, 1, f_remote_foreground},
6779 {"remote_peek", 1, 2, f_remote_peek},
6780 {"remote_read", 1, 1, f_remote_read},
6781 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006782 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006784 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006786 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 {"search", 1, 2, f_search},
6788 {"searchpair", 3, 5, f_searchpair},
6789 {"server2client", 2, 2, f_server2client},
6790 {"serverlist", 0, 0, f_serverlist},
6791 {"setbufvar", 3, 3, f_setbufvar},
6792 {"setcmdpos", 1, 1, f_setcmdpos},
6793 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006794 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 {"setreg", 2, 3, f_setreg},
6796 {"setwinvar", 3, 3, f_setwinvar},
6797 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006798 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006799 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006800 {"spellbadword", 0, 0, f_spellbadword},
6801 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006802 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803#ifdef HAVE_STRFTIME
6804 {"strftime", 1, 2, f_strftime},
6805#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006806 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006807 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {"strlen", 1, 1, f_strlen},
6809 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006810 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {"strtrans", 1, 1, f_strtrans},
6812 {"submatch", 1, 1, f_submatch},
6813 {"substitute", 4, 4, f_substitute},
6814 {"synID", 3, 3, f_synID},
6815 {"synIDattr", 2, 3, f_synIDattr},
6816 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006817 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006818 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 {"tempname", 0, 0, f_tempname},
6820 {"tolower", 1, 1, f_tolower},
6821 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006822 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006824 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 {"virtcol", 1, 1, f_virtcol},
6826 {"visualmode", 0, 1, f_visualmode},
6827 {"winbufnr", 1, 1, f_winbufnr},
6828 {"wincol", 0, 0, f_wincol},
6829 {"winheight", 1, 1, f_winheight},
6830 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006831 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 {"winrestcmd", 0, 0, f_winrestcmd},
6833 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006834 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835};
6836
6837#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6838
6839/*
6840 * Function given to ExpandGeneric() to obtain the list of internal
6841 * or user defined function names.
6842 */
6843 char_u *
6844get_function_name(xp, idx)
6845 expand_T *xp;
6846 int idx;
6847{
6848 static int intidx = -1;
6849 char_u *name;
6850
6851 if (idx == 0)
6852 intidx = -1;
6853 if (intidx < 0)
6854 {
6855 name = get_user_func_name(xp, idx);
6856 if (name != NULL)
6857 return name;
6858 }
6859 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6860 {
6861 STRCPY(IObuff, functions[intidx].f_name);
6862 STRCAT(IObuff, "(");
6863 if (functions[intidx].f_max_argc == 0)
6864 STRCAT(IObuff, ")");
6865 return IObuff;
6866 }
6867
6868 return NULL;
6869}
6870
6871/*
6872 * Function given to ExpandGeneric() to obtain the list of internal or
6873 * user defined variable or function names.
6874 */
6875/*ARGSUSED*/
6876 char_u *
6877get_expr_name(xp, idx)
6878 expand_T *xp;
6879 int idx;
6880{
6881 static int intidx = -1;
6882 char_u *name;
6883
6884 if (idx == 0)
6885 intidx = -1;
6886 if (intidx < 0)
6887 {
6888 name = get_function_name(xp, idx);
6889 if (name != NULL)
6890 return name;
6891 }
6892 return get_user_var_name(xp, ++intidx);
6893}
6894
6895#endif /* FEAT_CMDL_COMPL */
6896
6897/*
6898 * Find internal function in table above.
6899 * Return index, or -1 if not found
6900 */
6901 static int
6902find_internal_func(name)
6903 char_u *name; /* name of the function */
6904{
6905 int first = 0;
6906 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6907 int cmp;
6908 int x;
6909
6910 /*
6911 * Find the function name in the table. Binary search.
6912 */
6913 while (first <= last)
6914 {
6915 x = first + ((unsigned)(last - first) >> 1);
6916 cmp = STRCMP(name, functions[x].f_name);
6917 if (cmp < 0)
6918 last = x - 1;
6919 else if (cmp > 0)
6920 first = x + 1;
6921 else
6922 return x;
6923 }
6924 return -1;
6925}
6926
6927/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006928 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6929 * name it contains, otherwise return "name".
6930 */
6931 static char_u *
6932deref_func_name(name, lenp)
6933 char_u *name;
6934 int *lenp;
6935{
Bram Moolenaar33570922005-01-25 22:26:29 +00006936 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006937 int cc;
6938
6939 cc = name[*lenp];
6940 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006941 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006942 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006943 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006944 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006945 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006946 {
6947 *lenp = 0;
6948 return (char_u *)""; /* just in case */
6949 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006950 *lenp = STRLEN(v->di_tv.vval.v_string);
6951 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006952 }
6953
6954 return name;
6955}
6956
6957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 * Allocate a variable for the result of a function.
6959 * Return OK or FAIL.
6960 */
6961 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006962get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6963 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 char_u *name; /* name of the function */
6965 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 char_u **arg; /* argument, pointing to the '(' */
6968 linenr_T firstline; /* first line of range */
6969 linenr_T lastline; /* last line of range */
6970 int *doesrange; /* return: function handled range */
6971 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006972 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
6974 char_u *argp;
6975 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006976 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 int argcount = 0; /* number of arguments found */
6978
6979 /*
6980 * Get the arguments.
6981 */
6982 argp = *arg;
6983 while (argcount < MAX_FUNC_ARGS)
6984 {
6985 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6986 if (*argp == ')' || *argp == ',' || *argp == NUL)
6987 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6989 {
6990 ret = FAIL;
6991 break;
6992 }
6993 ++argcount;
6994 if (*argp != ',')
6995 break;
6996 }
6997 if (*argp == ')')
6998 ++argp;
6999 else
7000 ret = FAIL;
7001
7002 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007003 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007004 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007006 {
7007 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007008 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007009 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007010 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012
7013 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007014 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
7016 *arg = skipwhite(argp);
7017 return ret;
7018}
7019
7020
7021/*
7022 * Call a function with its resolved parameters
7023 * Return OK or FAIL.
7024 */
7025 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007026call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007027 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 char_u *name; /* name of the function */
7029 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007030 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007032 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 linenr_T firstline; /* first line of range */
7034 linenr_T lastline; /* last line of range */
7035 int *doesrange; /* return: function handled range */
7036 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007037 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038{
7039 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040#define ERROR_UNKNOWN 0
7041#define ERROR_TOOMANY 1
7042#define ERROR_TOOFEW 2
7043#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007044#define ERROR_DICT 4
7045#define ERROR_NONE 5
7046#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 int error = ERROR_NONE;
7048 int i;
7049 int llen;
7050 ufunc_T *fp;
7051 int cc;
7052#define FLEN_FIXED 40
7053 char_u fname_buf[FLEN_FIXED + 1];
7054 char_u *fname;
7055
7056 /*
7057 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7058 * Change <SNR>123_name() to K_SNR 123_name().
7059 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7060 */
7061 cc = name[len];
7062 name[len] = NUL;
7063 llen = eval_fname_script(name);
7064 if (llen > 0)
7065 {
7066 fname_buf[0] = K_SPECIAL;
7067 fname_buf[1] = KS_EXTRA;
7068 fname_buf[2] = (int)KE_SNR;
7069 i = 3;
7070 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7071 {
7072 if (current_SID <= 0)
7073 error = ERROR_SCRIPT;
7074 else
7075 {
7076 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7077 i = (int)STRLEN(fname_buf);
7078 }
7079 }
7080 if (i + STRLEN(name + llen) < FLEN_FIXED)
7081 {
7082 STRCPY(fname_buf + i, name + llen);
7083 fname = fname_buf;
7084 }
7085 else
7086 {
7087 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7088 if (fname == NULL)
7089 error = ERROR_OTHER;
7090 else
7091 {
7092 mch_memmove(fname, fname_buf, (size_t)i);
7093 STRCPY(fname + i, name + llen);
7094 }
7095 }
7096 }
7097 else
7098 fname = name;
7099
7100 *doesrange = FALSE;
7101
7102
7103 /* execute the function if no errors detected and executing */
7104 if (evaluate && error == ERROR_NONE)
7105 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007106 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 error = ERROR_UNKNOWN;
7108
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007109 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 {
7111 /*
7112 * User defined function.
7113 */
7114 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007115
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007117 /* Trigger FuncUndefined event, may load the function. */
7118 if (fp == NULL
7119 && apply_autocmds(EVENT_FUNCUNDEFINED,
7120 fname, fname, TRUE, NULL)
7121 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007123 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 fp = find_func(fname);
7125 }
7126#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007127 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007128 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007129 {
7130 /* loaded a package, search for the function again */
7131 fp = find_func(fname);
7132 }
7133
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 if (fp != NULL)
7135 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007136 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007138 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007140 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007142 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007143 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 else
7145 {
7146 /*
7147 * Call the user function.
7148 * Save and restore search patterns, script variables and
7149 * redo buffer.
7150 */
7151 save_search_patterns();
7152 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007153 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007154 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007155 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007156 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7157 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7158 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007159 /* Function was unreferenced while being used, free it
7160 * now. */
7161 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 restoreRedobuff();
7163 restore_search_patterns();
7164 error = ERROR_NONE;
7165 }
7166 }
7167 }
7168 else
7169 {
7170 /*
7171 * Find the function name in the table, call its implementation.
7172 */
7173 i = find_internal_func(fname);
7174 if (i >= 0)
7175 {
7176 if (argcount < functions[i].f_min_argc)
7177 error = ERROR_TOOFEW;
7178 else if (argcount > functions[i].f_max_argc)
7179 error = ERROR_TOOMANY;
7180 else
7181 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007182 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007183 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 error = ERROR_NONE;
7185 }
7186 }
7187 }
7188 /*
7189 * The function call (or "FuncUndefined" autocommand sequence) might
7190 * have been aborted by an error, an interrupt, or an explicitly thrown
7191 * exception that has not been caught so far. This situation can be
7192 * tested for by calling aborting(). For an error in an internal
7193 * function or for the "E132" error in call_user_func(), however, the
7194 * throw point at which the "force_abort" flag (temporarily reset by
7195 * emsg()) is normally updated has not been reached yet. We need to
7196 * update that flag first to make aborting() reliable.
7197 */
7198 update_force_abort();
7199 }
7200 if (error == ERROR_NONE)
7201 ret = OK;
7202
7203 /*
7204 * Report an error unless the argument evaluation or function call has been
7205 * cancelled due to an aborting error, an interrupt, or an exception.
7206 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007207 if (!aborting())
7208 {
7209 switch (error)
7210 {
7211 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007212 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213 break;
7214 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007215 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007216 break;
7217 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007218 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219 name);
7220 break;
7221 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007222 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223 name);
7224 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007225 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007226 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007227 name);
7228 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229 }
7230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231
7232 name[len] = cc;
7233 if (fname != name && fname != fname_buf)
7234 vim_free(fname);
7235
7236 return ret;
7237}
7238
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007239/*
7240 * Give an error message with a function name. Handle <SNR> things.
7241 */
7242 static void
7243emsg_funcname(msg, name)
7244 char *msg;
7245 char_u *name;
7246{
7247 char_u *p;
7248
7249 if (*name == K_SPECIAL)
7250 p = concat_str((char_u *)"<SNR>", name + 3);
7251 else
7252 p = name;
7253 EMSG2(_(msg), p);
7254 if (p != name)
7255 vim_free(p);
7256}
7257
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258/*********************************************
7259 * Implementation of the built-in functions
7260 */
7261
7262/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007263 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 */
7265 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007266f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007267 typval_T *argvars;
7268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269{
Bram Moolenaar33570922005-01-25 22:26:29 +00007270 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007272 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007273 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007275 if ((l = argvars[0].vval.v_list) != NULL
7276 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7277 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007278 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007279 }
7280 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007281 EMSG(_(e_listreq));
7282}
7283
7284/*
7285 * "append(lnum, string/list)" function
7286 */
7287 static void
7288f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 typval_T *argvars;
7290 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007291{
7292 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007293 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007294 list_T *l = NULL;
7295 listitem_T *li = NULL;
7296 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007297 long added = 0;
7298
Bram Moolenaar0d660222005-01-07 21:51:51 +00007299 lnum = get_tv_lnum(argvars);
7300 if (lnum >= 0
7301 && lnum <= curbuf->b_ml.ml_line_count
7302 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007303 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007304 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007305 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007306 l = argvars[1].vval.v_list;
7307 if (l == NULL)
7308 return;
7309 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007311 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007312 for (;;)
7313 {
7314 if (l == NULL)
7315 tv = &argvars[1]; /* append a string */
7316 else if (li == NULL)
7317 break; /* end of list */
7318 else
7319 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007320 line = get_tv_string_chk(tv);
7321 if (line == NULL) /* type error */
7322 {
7323 rettv->vval.v_number = 1; /* Failed */
7324 break;
7325 }
7326 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007327 ++added;
7328 if (l == NULL)
7329 break;
7330 li = li->li_next;
7331 }
7332
7333 appended_lines_mark(lnum, added);
7334 if (curwin->w_cursor.lnum > lnum)
7335 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007337 else
7338 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339}
7340
7341/*
7342 * "argc()" function
7343 */
7344/* ARGSUSED */
7345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007346f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 typval_T *argvars;
7348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007350 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351}
7352
7353/*
7354 * "argidx()" function
7355 */
7356/* ARGSUSED */
7357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007358f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 typval_T *argvars;
7360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007362 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363}
7364
7365/*
7366 * "argv(nr)" function
7367 */
7368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007369f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007370 typval_T *argvars;
7371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372{
7373 int idx;
7374
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007375 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007377 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007379 rettv->vval.v_string = NULL;
7380 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381}
7382
7383/*
7384 * "browse(save, title, initdir, default)" function
7385 */
7386/* ARGSUSED */
7387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007388f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 typval_T *argvars;
7390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391{
7392#ifdef FEAT_BROWSE
7393 int save;
7394 char_u *title;
7395 char_u *initdir;
7396 char_u *defname;
7397 char_u buf[NUMBUFLEN];
7398 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007399 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007401 save = get_tv_number_chk(&argvars[0], &error);
7402 title = get_tv_string_chk(&argvars[1]);
7403 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7404 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007406 if (error || title == NULL || initdir == NULL || defname == NULL)
7407 rettv->vval.v_string = NULL;
7408 else
7409 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007410 do_browse(save ? BROWSE_SAVE : 0,
7411 title, defname, NULL, initdir, NULL, curbuf);
7412#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007413 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007414#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007415 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007416}
7417
7418/*
7419 * "browsedir(title, initdir)" function
7420 */
7421/* ARGSUSED */
7422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007423f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007424 typval_T *argvars;
7425 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007426{
7427#ifdef FEAT_BROWSE
7428 char_u *title;
7429 char_u *initdir;
7430 char_u buf[NUMBUFLEN];
7431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007432 title = get_tv_string_chk(&argvars[0]);
7433 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007435 if (title == NULL || initdir == NULL)
7436 rettv->vval.v_string = NULL;
7437 else
7438 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007439 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007441 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007443 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444}
7445
Bram Moolenaar33570922005-01-25 22:26:29 +00007446static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007447
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448/*
7449 * Find a buffer by number or exact name.
7450 */
7451 static buf_T *
7452find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454{
7455 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007457 if (avar->v_type == VAR_NUMBER)
7458 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007459 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007461 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007462 if (buf == NULL)
7463 {
7464 /* No full path name match, try a match with a URL or a "nofile"
7465 * buffer, these don't use the full path. */
7466 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7467 if (buf->b_fname != NULL
7468 && (path_with_url(buf->b_fname)
7469#ifdef FEAT_QUICKFIX
7470 || bt_nofile(buf)
7471#endif
7472 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007473 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007474 break;
7475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 }
7477 return buf;
7478}
7479
7480/*
7481 * "bufexists(expr)" function
7482 */
7483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007484f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007485 typval_T *argvars;
7486 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007488 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489}
7490
7491/*
7492 * "buflisted(expr)" function
7493 */
7494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007495f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007496 typval_T *argvars;
7497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498{
7499 buf_T *buf;
7500
7501 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007502 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503}
7504
7505/*
7506 * "bufloaded(expr)" function
7507 */
7508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007509f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007510 typval_T *argvars;
7511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512{
7513 buf_T *buf;
7514
7515 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007516 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517}
7518
Bram Moolenaar33570922005-01-25 22:26:29 +00007519static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007520
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521/*
7522 * Get buffer by number or pattern.
7523 */
7524 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007525get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007526 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007528 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 int save_magic;
7530 char_u *save_cpo;
7531 buf_T *buf;
7532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007533 if (tv->v_type == VAR_NUMBER)
7534 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007535 if (tv->v_type != VAR_STRING)
7536 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 if (name == NULL || *name == NUL)
7538 return curbuf;
7539 if (name[0] == '$' && name[1] == NUL)
7540 return lastbuf;
7541
7542 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7543 save_magic = p_magic;
7544 p_magic = TRUE;
7545 save_cpo = p_cpo;
7546 p_cpo = (char_u *)"";
7547
7548 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7549 TRUE, FALSE));
7550
7551 p_magic = save_magic;
7552 p_cpo = save_cpo;
7553
7554 /* If not found, try expanding the name, like done for bufexists(). */
7555 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007556 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557
7558 return buf;
7559}
7560
7561/*
7562 * "bufname(expr)" function
7563 */
7564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007565f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 typval_T *argvars;
7567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568{
7569 buf_T *buf;
7570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007571 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007573 buf = get_buf_tv(&argvars[0]);
7574 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007576 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007578 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 --emsg_off;
7580}
7581
7582/*
7583 * "bufnr(expr)" function
7584 */
7585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007586f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 typval_T *argvars;
7588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589{
7590 buf_T *buf;
7591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007592 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007594 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007596 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007598 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 --emsg_off;
7600}
7601
7602/*
7603 * "bufwinnr(nr)" function
7604 */
7605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007606f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007607 typval_T *argvars;
7608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609{
7610#ifdef FEAT_WINDOWS
7611 win_T *wp;
7612 int winnr = 0;
7613#endif
7614 buf_T *buf;
7615
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007616 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007618 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619#ifdef FEAT_WINDOWS
7620 for (wp = firstwin; wp; wp = wp->w_next)
7621 {
7622 ++winnr;
7623 if (wp->w_buffer == buf)
7624 break;
7625 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007626 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007628 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629#endif
7630 --emsg_off;
7631}
7632
7633/*
7634 * "byte2line(byte)" function
7635 */
7636/*ARGSUSED*/
7637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007638f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007639 typval_T *argvars;
7640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641{
7642#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007643 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644#else
7645 long boff = 0;
7646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007647 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007649 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007651 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 (linenr_T)0, &boff);
7653#endif
7654}
7655
7656/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007657 * "byteidx()" function
7658 */
7659/*ARGSUSED*/
7660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007661f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007662 typval_T *argvars;
7663 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007664{
7665#ifdef FEAT_MBYTE
7666 char_u *t;
7667#endif
7668 char_u *str;
7669 long idx;
7670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007671 str = get_tv_string_chk(&argvars[0]);
7672 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007674 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007675 return;
7676
7677#ifdef FEAT_MBYTE
7678 t = str;
7679 for ( ; idx > 0; idx--)
7680 {
7681 if (*t == NUL) /* EOL reached */
7682 return;
7683 t += mb_ptr2len_check(t);
7684 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007686#else
7687 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007688 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007689#endif
7690}
7691
7692/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007693 * "call(func, arglist)" function
7694 */
7695 static void
7696f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007697 typval_T *argvars;
7698 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007699{
7700 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007702 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007704 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007705 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007706
7707 rettv->vval.v_number = 0;
7708 if (argvars[1].v_type != VAR_LIST)
7709 {
7710 EMSG(_(e_listreq));
7711 return;
7712 }
7713 if (argvars[1].vval.v_list == NULL)
7714 return;
7715
7716 if (argvars[0].v_type == VAR_FUNC)
7717 func = argvars[0].vval.v_string;
7718 else
7719 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007720 if (*func == NUL)
7721 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007722
Bram Moolenaare9a41262005-01-15 22:18:47 +00007723 if (argvars[2].v_type != VAR_UNKNOWN)
7724 {
7725 if (argvars[2].v_type != VAR_DICT)
7726 {
7727 EMSG(_(e_dictreq));
7728 return;
7729 }
7730 selfdict = argvars[2].vval.v_dict;
7731 }
7732
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007733 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7734 item = item->li_next)
7735 {
7736 if (argc == MAX_FUNC_ARGS)
7737 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007738 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007739 break;
7740 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007741 /* Make a copy of each argument. This is needed to be able to set
7742 * v_lock to VAR_FIXED in the copy without changing the original list.
7743 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007744 copy_tv(&item->li_tv, &argv[argc++]);
7745 }
7746
7747 if (item == NULL)
7748 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007749 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7750 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007751
7752 /* Free the arguments. */
7753 while (argc > 0)
7754 clear_tv(&argv[--argc]);
7755}
7756
7757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 * "char2nr(string)" function
7759 */
7760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007761f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007762 typval_T *argvars;
7763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764{
7765#ifdef FEAT_MBYTE
7766 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007767 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 else
7769#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007770 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771}
7772
7773/*
7774 * "cindent(lnum)" function
7775 */
7776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007777f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007778 typval_T *argvars;
7779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780{
7781#ifdef FEAT_CINDENT
7782 pos_T pos;
7783 linenr_T lnum;
7784
7785 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007786 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7788 {
7789 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007790 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 curwin->w_cursor = pos;
7792 }
7793 else
7794#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007795 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796}
7797
7798/*
7799 * "col(string)" function
7800 */
7801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007802f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007803 typval_T *argvars;
7804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805{
7806 colnr_T col = 0;
7807 pos_T *fp;
7808
7809 fp = var2fpos(&argvars[0], FALSE);
7810 if (fp != NULL)
7811 {
7812 if (fp->col == MAXCOL)
7813 {
7814 /* '> can be MAXCOL, get the length of the line then */
7815 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7816 col = STRLEN(ml_get(fp->lnum)) + 1;
7817 else
7818 col = MAXCOL;
7819 }
7820 else
7821 {
7822 col = fp->col + 1;
7823#ifdef FEAT_VIRTUALEDIT
7824 /* col(".") when the cursor is on the NUL at the end of the line
7825 * because of "coladd" can be seen as an extra column. */
7826 if (virtual_active() && fp == &curwin->w_cursor)
7827 {
7828 char_u *p = ml_get_cursor();
7829
7830 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7831 curwin->w_virtcol - curwin->w_cursor.coladd))
7832 {
7833# ifdef FEAT_MBYTE
7834 int l;
7835
7836 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7837 col += l;
7838# else
7839 if (*p != NUL && p[1] == NUL)
7840 ++col;
7841# endif
7842 }
7843 }
7844#endif
7845 }
7846 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007847 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848}
7849
7850/*
7851 * "confirm(message, buttons[, default [, type]])" function
7852 */
7853/*ARGSUSED*/
7854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007855f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007856 typval_T *argvars;
7857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
7859#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7860 char_u *message;
7861 char_u *buttons = NULL;
7862 char_u buf[NUMBUFLEN];
7863 char_u buf2[NUMBUFLEN];
7864 int def = 1;
7865 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007866 char_u *typestr;
7867 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007869 message = get_tv_string_chk(&argvars[0]);
7870 if (message == NULL)
7871 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007872 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007874 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7875 if (buttons == NULL)
7876 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007877 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007879 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007880 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007882 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7883 if (typestr == NULL)
7884 error = TRUE;
7885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007887 switch (TOUPPER_ASC(*typestr))
7888 {
7889 case 'E': type = VIM_ERROR; break;
7890 case 'Q': type = VIM_QUESTION; break;
7891 case 'I': type = VIM_INFO; break;
7892 case 'W': type = VIM_WARNING; break;
7893 case 'G': type = VIM_GENERIC; break;
7894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 }
7896 }
7897 }
7898 }
7899
7900 if (buttons == NULL || *buttons == NUL)
7901 buttons = (char_u *)_("&Ok");
7902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007903 if (error)
7904 rettv->vval.v_number = 0;
7905 else
7906 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 def, NULL);
7908#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007909 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910#endif
7911}
7912
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913/*
7914 * "copy()" function
7915 */
7916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007917f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007918 typval_T *argvars;
7919 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007920{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007921 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007922}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923
7924/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007925 * "count()" function
7926 */
7927 static void
7928f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007929 typval_T *argvars;
7930 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007931{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007932 long n = 0;
7933 int ic = FALSE;
7934
Bram Moolenaare9a41262005-01-15 22:18:47 +00007935 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007936 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007937 listitem_T *li;
7938 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007939 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007940
Bram Moolenaare9a41262005-01-15 22:18:47 +00007941 if ((l = argvars[0].vval.v_list) != NULL)
7942 {
7943 li = l->lv_first;
7944 if (argvars[2].v_type != VAR_UNKNOWN)
7945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007946 int error = FALSE;
7947
7948 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007949 if (argvars[3].v_type != VAR_UNKNOWN)
7950 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007951 idx = get_tv_number_chk(&argvars[3], &error);
7952 if (!error)
7953 {
7954 li = list_find(l, idx);
7955 if (li == NULL)
7956 EMSGN(_(e_listidx), idx);
7957 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007958 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007959 if (error)
7960 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007961 }
7962
7963 for ( ; li != NULL; li = li->li_next)
7964 if (tv_equal(&li->li_tv, &argvars[1], ic))
7965 ++n;
7966 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007967 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007968 else if (argvars[0].v_type == VAR_DICT)
7969 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007970 int todo;
7971 dict_T *d;
7972 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007973
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007974 if ((d = argvars[0].vval.v_dict) != NULL)
7975 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007976 int error = FALSE;
7977
Bram Moolenaare9a41262005-01-15 22:18:47 +00007978 if (argvars[2].v_type != VAR_UNKNOWN)
7979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007980 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007981 if (argvars[3].v_type != VAR_UNKNOWN)
7982 EMSG(_(e_invarg));
7983 }
7984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007985 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007986 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007987 {
7988 if (!HASHITEM_EMPTY(hi))
7989 {
7990 --todo;
7991 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7992 ++n;
7993 }
7994 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007995 }
7996 }
7997 else
7998 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007999 rettv->vval.v_number = n;
8000}
8001
8002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8004 *
8005 * Checks the existence of a cscope connection.
8006 */
8007/*ARGSUSED*/
8008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008009f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008010 typval_T *argvars;
8011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012{
8013#ifdef FEAT_CSCOPE
8014 int num = 0;
8015 char_u *dbpath = NULL;
8016 char_u *prepend = NULL;
8017 char_u buf[NUMBUFLEN];
8018
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008019 if (argvars[0].v_type != VAR_UNKNOWN
8020 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008022 num = (int)get_tv_number(&argvars[0]);
8023 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008024 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008025 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 }
8027
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008028 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008030 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031#endif
8032}
8033
8034/*
8035 * "cursor(lnum, col)" function
8036 *
8037 * Moves the cursor to the specified line and column
8038 */
8039/*ARGSUSED*/
8040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008041f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008042 typval_T *argvars;
8043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044{
8045 long line, col;
8046
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008047 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008048 col = get_tv_number_chk(&argvars[1], NULL);
8049 if (line < 0 || col < 0)
8050 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 if (line > 0)
8052 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 if (col > 0)
8054 curwin->w_cursor.col = col - 1;
8055#ifdef FEAT_VIRTUALEDIT
8056 curwin->w_cursor.coladd = 0;
8057#endif
8058
8059 /* Make sure the cursor is in a valid position. */
8060 check_cursor();
8061#ifdef FEAT_MBYTE
8062 /* Correct cursor for multi-byte character. */
8063 if (has_mbyte)
8064 mb_adjust_cursor();
8065#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008066
8067 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068}
8069
8070/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008071 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 */
8073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008074f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008075 typval_T *argvars;
8076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008078 int noref = 0;
8079
8080 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008081 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008082 if (noref < 0 || noref > 1)
8083 EMSG(_(e_invarg));
8084 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008085 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086}
8087
8088/*
8089 * "delete()" function
8090 */
8091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008092f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008093 typval_T *argvars;
8094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095{
8096 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008097 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008099 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100}
8101
8102/*
8103 * "did_filetype()" function
8104 */
8105/*ARGSUSED*/
8106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008107f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008108 typval_T *argvars;
8109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110{
8111#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008112 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008114 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115#endif
8116}
8117
8118/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008119 * "diff_filler()" function
8120 */
8121/*ARGSUSED*/
8122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008123f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008124 typval_T *argvars;
8125 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008126{
8127#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008128 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008129#endif
8130}
8131
8132/*
8133 * "diff_hlID()" function
8134 */
8135/*ARGSUSED*/
8136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008137f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008138 typval_T *argvars;
8139 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008140{
8141#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008142 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008143 static linenr_T prev_lnum = 0;
8144 static int changedtick = 0;
8145 static int fnum = 0;
8146 static int change_start = 0;
8147 static int change_end = 0;
8148 static enum hlf_value hlID = 0;
8149 int filler_lines;
8150 int col;
8151
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008152 if (lnum < 0) /* ignore type error in {lnum} arg */
8153 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008154 if (lnum != prev_lnum
8155 || changedtick != curbuf->b_changedtick
8156 || fnum != curbuf->b_fnum)
8157 {
8158 /* New line, buffer, change: need to get the values. */
8159 filler_lines = diff_check(curwin, lnum);
8160 if (filler_lines < 0)
8161 {
8162 if (filler_lines == -1)
8163 {
8164 change_start = MAXCOL;
8165 change_end = -1;
8166 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8167 hlID = HLF_ADD; /* added line */
8168 else
8169 hlID = HLF_CHD; /* changed line */
8170 }
8171 else
8172 hlID = HLF_ADD; /* added line */
8173 }
8174 else
8175 hlID = (enum hlf_value)0;
8176 prev_lnum = lnum;
8177 changedtick = curbuf->b_changedtick;
8178 fnum = curbuf->b_fnum;
8179 }
8180
8181 if (hlID == HLF_CHD || hlID == HLF_TXD)
8182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008183 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008184 if (col >= change_start && col <= change_end)
8185 hlID = HLF_TXD; /* changed text */
8186 else
8187 hlID = HLF_CHD; /* changed line */
8188 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008189 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008190#endif
8191}
8192
8193/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008194 * "empty({expr})" function
8195 */
8196 static void
8197f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008198 typval_T *argvars;
8199 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008200{
8201 int n;
8202
8203 switch (argvars[0].v_type)
8204 {
8205 case VAR_STRING:
8206 case VAR_FUNC:
8207 n = argvars[0].vval.v_string == NULL
8208 || *argvars[0].vval.v_string == NUL;
8209 break;
8210 case VAR_NUMBER:
8211 n = argvars[0].vval.v_number == 0;
8212 break;
8213 case VAR_LIST:
8214 n = argvars[0].vval.v_list == NULL
8215 || argvars[0].vval.v_list->lv_first == NULL;
8216 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008217 case VAR_DICT:
8218 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008219 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008220 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008221 default:
8222 EMSG2(_(e_intern2), "f_empty()");
8223 n = 0;
8224 }
8225
8226 rettv->vval.v_number = n;
8227}
8228
8229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 * "escape({string}, {chars})" function
8231 */
8232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008233f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008234 typval_T *argvars;
8235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
8237 char_u buf[NUMBUFLEN];
8238
Bram Moolenaar758711c2005-02-02 23:11:38 +00008239 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8240 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008241 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242}
8243
8244/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008245 * "eval()" function
8246 */
8247/*ARGSUSED*/
8248 static void
8249f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008250 typval_T *argvars;
8251 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008252{
8253 char_u *s;
8254
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008255 s = get_tv_string_chk(&argvars[0]);
8256 if (s != NULL)
8257 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008258
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008259 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8260 {
8261 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008262 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008263 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008264 else if (*s != NUL)
8265 EMSG(_(e_trailing));
8266}
8267
8268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 * "eventhandler()" function
8270 */
8271/*ARGSUSED*/
8272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008273f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008274 typval_T *argvars;
8275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008277 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278}
8279
8280/*
8281 * "executable()" function
8282 */
8283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008284f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008285 typval_T *argvars;
8286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008288 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289}
8290
8291/*
8292 * "exists()" function
8293 */
8294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008295f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008296 typval_T *argvars;
8297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298{
8299 char_u *p;
8300 char_u *name;
8301 int n = FALSE;
8302 int len = 0;
8303
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008304 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 if (*p == '$') /* environment variable */
8306 {
8307 /* first try "normal" environment variables (fast) */
8308 if (mch_getenv(p + 1) != NULL)
8309 n = TRUE;
8310 else
8311 {
8312 /* try expanding things like $VIM and ${HOME} */
8313 p = expand_env_save(p);
8314 if (p != NULL && *p != '$')
8315 n = TRUE;
8316 vim_free(p);
8317 }
8318 }
8319 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008320 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 else if (*p == '*') /* internal or user defined function */
8322 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008323 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 }
8325 else if (*p == ':')
8326 {
8327 n = cmd_exists(p + 1);
8328 }
8329 else if (*p == '#')
8330 {
8331#ifdef FEAT_AUTOCMD
8332 name = p + 1;
8333 p = vim_strchr(name, '#');
8334 if (p != NULL)
8335 n = au_exists(name, p, p + 1);
8336 else
8337 n = au_exists(name, name + STRLEN(name), NULL);
8338#endif
8339 }
8340 else /* internal variable */
8341 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008342 char_u *tofree;
8343 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008345 /* get_name_len() takes care of expanding curly braces */
8346 name = p;
8347 len = get_name_len(&p, &tofree, TRUE, FALSE);
8348 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008350 if (tofree != NULL)
8351 name = tofree;
8352 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8353 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008355 /* handle d.key, l[idx], f(expr) */
8356 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8357 if (n)
8358 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 }
8360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008362 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 }
8364
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008365 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366}
8367
8368/*
8369 * "expand()" function
8370 */
8371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008372f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008373 typval_T *argvars;
8374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375{
8376 char_u *s;
8377 int len;
8378 char_u *errormsg;
8379 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8380 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008381 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008383 rettv->v_type = VAR_STRING;
8384 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 if (*s == '%' || *s == '#' || *s == '<')
8386 {
8387 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008388 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 --emsg_off;
8390 }
8391 else
8392 {
8393 /* When the optional second argument is non-zero, don't remove matches
8394 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008395 if (argvars[1].v_type != VAR_UNKNOWN
8396 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008398 if (!error)
8399 {
8400 ExpandInit(&xpc);
8401 xpc.xp_context = EXPAND_FILES;
8402 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8403 ExpandCleanup(&xpc);
8404 }
8405 else
8406 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 }
8408}
8409
8410/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008411 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008412 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008413 */
8414 static void
8415f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008416 typval_T *argvars;
8417 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008418{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008419 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008420 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008421 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008422 list_T *l1, *l2;
8423 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008424 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008425 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008426
Bram Moolenaare9a41262005-01-15 22:18:47 +00008427 l1 = argvars[0].vval.v_list;
8428 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008429 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8430 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008431 {
8432 if (argvars[2].v_type != VAR_UNKNOWN)
8433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008434 before = get_tv_number_chk(&argvars[2], &error);
8435 if (error)
8436 return; /* type error; errmsg already given */
8437
Bram Moolenaar758711c2005-02-02 23:11:38 +00008438 if (before == l1->lv_len)
8439 item = NULL;
8440 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008441 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008442 item = list_find(l1, before);
8443 if (item == NULL)
8444 {
8445 EMSGN(_(e_listidx), before);
8446 return;
8447 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008448 }
8449 }
8450 else
8451 item = NULL;
8452 list_extend(l1, l2, item);
8453
Bram Moolenaare9a41262005-01-15 22:18:47 +00008454 copy_tv(&argvars[0], rettv);
8455 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008456 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008457 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8458 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008459 dict_T *d1, *d2;
8460 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008461 char_u *action;
8462 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008463 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008464 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008465
8466 d1 = argvars[0].vval.v_dict;
8467 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008468 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8469 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008470 {
8471 /* Check the third argument. */
8472 if (argvars[2].v_type != VAR_UNKNOWN)
8473 {
8474 static char *(av[]) = {"keep", "force", "error"};
8475
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008476 action = get_tv_string_chk(&argvars[2]);
8477 if (action == NULL)
8478 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008479 for (i = 0; i < 3; ++i)
8480 if (STRCMP(action, av[i]) == 0)
8481 break;
8482 if (i == 3)
8483 {
8484 EMSGN(_(e_invarg2), action);
8485 return;
8486 }
8487 }
8488 else
8489 action = (char_u *)"force";
8490
8491 /* Go over all entries in the second dict and add them to the
8492 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008493 todo = d2->dv_hashtab.ht_used;
8494 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008495 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008496 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008497 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008498 --todo;
8499 di1 = dict_find(d1, hi2->hi_key, -1);
8500 if (di1 == NULL)
8501 {
8502 di1 = dictitem_copy(HI2DI(hi2));
8503 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8504 dictitem_free(di1);
8505 }
8506 else if (*action == 'e')
8507 {
8508 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8509 break;
8510 }
8511 else if (*action == 'f')
8512 {
8513 clear_tv(&di1->di_tv);
8514 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8515 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008516 }
8517 }
8518
Bram Moolenaare9a41262005-01-15 22:18:47 +00008519 copy_tv(&argvars[0], rettv);
8520 }
8521 }
8522 else
8523 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008524}
8525
8526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 * "filereadable()" function
8528 */
8529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008530f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008531 typval_T *argvars;
8532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533{
8534 FILE *fd;
8535 char_u *p;
8536 int n;
8537
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008538 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8540 {
8541 n = TRUE;
8542 fclose(fd);
8543 }
8544 else
8545 n = FALSE;
8546
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008547 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548}
8549
8550/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008551 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 * rights to write into.
8553 */
8554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008555f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008556 typval_T *argvars;
8557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008559 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560}
8561
Bram Moolenaar33570922005-01-25 22:26:29 +00008562static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008563
8564 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008565findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008566 typval_T *argvars;
8567 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008568 int dir;
8569{
8570#ifdef FEAT_SEARCHPATH
8571 char_u *fname;
8572 char_u *fresult = NULL;
8573 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8574 char_u *p;
8575 char_u pathbuf[NUMBUFLEN];
8576 int count = 1;
8577 int first = TRUE;
8578
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008580
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008581 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008583 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8584 if (p == NULL)
8585 count = -1; /* error */
8586 else
8587 {
8588 if (*p != NUL)
8589 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008590
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008591 if (argvars[2].v_type != VAR_UNKNOWN)
8592 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8593 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008594 }
8595
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008596 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008598 do
8599 {
8600 vim_free(fresult);
8601 fresult = find_file_in_path_option(first ? fname : NULL,
8602 first ? (int)STRLEN(fname) : 0,
8603 0, first, path, dir, NULL);
8604 first = FALSE;
8605 } while (--count > 0 && fresult != NULL);
8606 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008607
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008608 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008609#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008610 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008611#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008612 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008613}
8614
Bram Moolenaar33570922005-01-25 22:26:29 +00008615static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8616static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008617
8618/*
8619 * Implementation of map() and filter().
8620 */
8621 static void
8622filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008623 typval_T *argvars;
8624 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008625 int map;
8626{
8627 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008628 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008629 listitem_T *li, *nli;
8630 list_T *l = NULL;
8631 dictitem_T *di;
8632 hashtab_T *ht;
8633 hashitem_T *hi;
8634 dict_T *d = NULL;
8635 typval_T save_val;
8636 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008637 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008638 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008639 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8640
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008641
8642 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008643 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008644 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008645 if ((l = argvars[0].vval.v_list) == NULL
8646 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008647 return;
8648 }
8649 else if (argvars[0].v_type == VAR_DICT)
8650 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008651 if ((d = argvars[0].vval.v_dict) == NULL
8652 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008653 return;
8654 }
8655 else
8656 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008657 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008658 return;
8659 }
8660
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008661 expr = get_tv_string_buf_chk(&argvars[1], buf);
8662 /* On type errors, the preceding call has already displayed an error
8663 * message. Avoid a misleading error message for an empty string that
8664 * was not passed as argument. */
8665 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008667 prepare_vimvar(VV_VAL, &save_val);
8668 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008670 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008671 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008672 prepare_vimvar(VV_KEY, &save_key);
8673 vimvars[VV_KEY].vv_type = VAR_STRING;
8674
8675 ht = &d->dv_hashtab;
8676 hash_lock(ht);
8677 todo = ht->ht_used;
8678 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008680 if (!HASHITEM_EMPTY(hi))
8681 {
8682 --todo;
8683 di = HI2DI(hi);
8684 if (tv_check_lock(di->di_tv.v_lock, msg))
8685 break;
8686 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8687 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8688 break;
8689 if (!map && rem)
8690 dictitem_remove(d, di);
8691 clear_tv(&vimvars[VV_KEY].vv_tv);
8692 }
8693 }
8694 hash_unlock(ht);
8695
8696 restore_vimvar(VV_KEY, &save_key);
8697 }
8698 else
8699 {
8700 for (li = l->lv_first; li != NULL; li = nli)
8701 {
8702 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008703 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008704 nli = li->li_next;
8705 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008706 break;
8707 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008708 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008709 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008710 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008712 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008713 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008714
8715 copy_tv(&argvars[0], rettv);
8716}
8717
8718 static int
8719filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008720 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008721 char_u *expr;
8722 int map;
8723 int *remp;
8724{
Bram Moolenaar33570922005-01-25 22:26:29 +00008725 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008726 char_u *s;
8727
Bram Moolenaar33570922005-01-25 22:26:29 +00008728 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008729 s = expr;
8730 if (eval1(&s, &rettv, TRUE) == FAIL)
8731 return FAIL;
8732 if (*s != NUL) /* check for trailing chars after expr */
8733 {
8734 EMSG2(_(e_invexpr2), s);
8735 return FAIL;
8736 }
8737 if (map)
8738 {
8739 /* map(): replace the list item value */
8740 clear_tv(tv);
8741 *tv = rettv;
8742 }
8743 else
8744 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008745 int error = FALSE;
8746
Bram Moolenaare9a41262005-01-15 22:18:47 +00008747 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008748 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008749 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008750 /* On type error, nothing has been removed; return FAIL to stop the
8751 * loop. The error message was given by get_tv_number_chk(). */
8752 if (error)
8753 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008754 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008755 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008756 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008757}
8758
8759/*
8760 * "filter()" function
8761 */
8762 static void
8763f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008764 typval_T *argvars;
8765 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008766{
8767 filter_map(argvars, rettv, FALSE);
8768}
8769
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008770/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008771 * "finddir({fname}[, {path}[, {count}]])" function
8772 */
8773 static void
8774f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008775 typval_T *argvars;
8776 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008777{
8778 findfilendir(argvars, rettv, TRUE);
8779}
8780
8781/*
8782 * "findfile({fname}[, {path}[, {count}]])" function
8783 */
8784 static void
8785f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008786 typval_T *argvars;
8787 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008788{
8789 findfilendir(argvars, rettv, FALSE);
8790}
8791
8792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 * "fnamemodify({fname}, {mods})" function
8794 */
8795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008796f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008797 typval_T *argvars;
8798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799{
8800 char_u *fname;
8801 char_u *mods;
8802 int usedlen = 0;
8803 int len;
8804 char_u *fbuf = NULL;
8805 char_u buf[NUMBUFLEN];
8806
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008807 fname = get_tv_string_chk(&argvars[0]);
8808 mods = get_tv_string_buf_chk(&argvars[1], buf);
8809 if (fname == NULL || mods == NULL)
8810 fname = NULL;
8811 else
8812 {
8813 len = (int)STRLEN(fname);
8814 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008817 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008819 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 vim_free(fbuf);
8823}
8824
Bram Moolenaar33570922005-01-25 22:26:29 +00008825static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826
8827/*
8828 * "foldclosed()" function
8829 */
8830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008832 typval_T *argvars;
8833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 int end;
8835{
8836#ifdef FEAT_FOLDING
8837 linenr_T lnum;
8838 linenr_T first, last;
8839
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008840 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8842 {
8843 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8844 {
8845 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008846 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 return;
8850 }
8851 }
8852#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008853 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854}
8855
8856/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008857 * "foldclosed()" function
8858 */
8859 static void
8860f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008861 typval_T *argvars;
8862 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008863{
8864 foldclosed_both(argvars, rettv, FALSE);
8865}
8866
8867/*
8868 * "foldclosedend()" function
8869 */
8870 static void
8871f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 typval_T *argvars;
8873 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008874{
8875 foldclosed_both(argvars, rettv, TRUE);
8876}
8877
8878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 * "foldlevel()" function
8880 */
8881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008882f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008883 typval_T *argvars;
8884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885{
8886#ifdef FEAT_FOLDING
8887 linenr_T lnum;
8888
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008889 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 else
8893#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008894 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895}
8896
8897/*
8898 * "foldtext()" function
8899 */
8900/*ARGSUSED*/
8901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008902f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008903 typval_T *argvars;
8904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905{
8906#ifdef FEAT_FOLDING
8907 linenr_T lnum;
8908 char_u *s;
8909 char_u *r;
8910 int len;
8911 char *txt;
8912#endif
8913
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008914 rettv->v_type = VAR_STRING;
8915 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008917 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8918 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8919 <= curbuf->b_ml.ml_line_count
8920 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 {
8922 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008923 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8924 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 {
8926 if (!linewhite(lnum))
8927 break;
8928 ++lnum;
8929 }
8930
8931 /* Find interesting text in this line. */
8932 s = skipwhite(ml_get(lnum));
8933 /* skip C comment-start */
8934 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008935 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008937 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008938 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008939 {
8940 s = skipwhite(ml_get(lnum + 1));
8941 if (*s == '*')
8942 s = skipwhite(s + 1);
8943 }
8944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 txt = _("+-%s%3ld lines: ");
8946 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008947 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 + 20 /* for %3ld */
8949 + STRLEN(s))); /* concatenated */
8950 if (r != NULL)
8951 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008952 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8953 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8954 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 len = (int)STRLEN(r);
8956 STRCAT(r, s);
8957 /* remove 'foldmarker' and 'commentstring' */
8958 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 }
8961 }
8962#endif
8963}
8964
8965/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008966 * "foldtextresult(lnum)" function
8967 */
8968/*ARGSUSED*/
8969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008971 typval_T *argvars;
8972 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008973{
8974#ifdef FEAT_FOLDING
8975 linenr_T lnum;
8976 char_u *text;
8977 char_u buf[51];
8978 foldinfo_T foldinfo;
8979 int fold_count;
8980#endif
8981
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982 rettv->v_type = VAR_STRING;
8983 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008984#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008985 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008986 /* treat illegal types and illegal string values for {lnum} the same */
8987 if (lnum < 0)
8988 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008989 fold_count = foldedCount(curwin, lnum, &foldinfo);
8990 if (fold_count > 0)
8991 {
8992 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8993 &foldinfo, buf);
8994 if (text == buf)
8995 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008997 }
8998#endif
8999}
9000
9001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 * "foreground()" function
9003 */
9004/*ARGSUSED*/
9005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009007 typval_T *argvars;
9008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009010 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011#ifdef FEAT_GUI
9012 if (gui.in_use)
9013 gui_mch_set_foreground();
9014#else
9015# ifdef WIN32
9016 win32_set_foreground();
9017# endif
9018#endif
9019}
9020
9021/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009022 * "function()" function
9023 */
9024/*ARGSUSED*/
9025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009027 typval_T *argvars;
9028 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009029{
9030 char_u *s;
9031
Bram Moolenaara7043832005-01-21 11:56:39 +00009032 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009033 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009034 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009035 EMSG2(_(e_invarg2), s);
9036 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009037 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009038 else
9039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009040 rettv->vval.v_string = vim_strsave(s);
9041 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009042 }
9043}
9044
9045/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009046 * "garbagecollect()" function
9047 */
9048/*ARGSUSED*/
9049 static void
9050f_garbagecollect(argvars, rettv)
9051 typval_T *argvars;
9052 typval_T *rettv;
9053{
9054 garbage_collect();
9055}
9056
9057/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009058 * "get()" function
9059 */
9060 static void
9061f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 typval_T *argvars;
9063 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009064{
Bram Moolenaar33570922005-01-25 22:26:29 +00009065 listitem_T *li;
9066 list_T *l;
9067 dictitem_T *di;
9068 dict_T *d;
9069 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009070
Bram Moolenaare9a41262005-01-15 22:18:47 +00009071 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009072 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009073 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009075 int error = FALSE;
9076
9077 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9078 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009079 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009080 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009081 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009082 else if (argvars[0].v_type == VAR_DICT)
9083 {
9084 if ((d = argvars[0].vval.v_dict) != NULL)
9085 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009086 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009087 if (di != NULL)
9088 tv = &di->di_tv;
9089 }
9090 }
9091 else
9092 EMSG2(_(e_listdictarg), "get()");
9093
9094 if (tv == NULL)
9095 {
9096 if (argvars[2].v_type == VAR_UNKNOWN)
9097 rettv->vval.v_number = 0;
9098 else
9099 copy_tv(&argvars[2], rettv);
9100 }
9101 else
9102 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009103}
9104
Bram Moolenaar342337a2005-07-21 21:11:17 +00009105static 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 +00009106
9107/*
9108 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009109 * Return a range (from start to end) of lines in rettv from the specified
9110 * buffer.
9111 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009112 */
9113 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009114get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009115 buf_T *buf;
9116 linenr_T start;
9117 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009118 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009119 typval_T *rettv;
9120{
9121 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009122 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009123 listitem_T *li;
9124
Bram Moolenaar342337a2005-07-21 21:11:17 +00009125 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009126 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009127 l = list_alloc();
9128 if (l == NULL)
9129 return;
9130
9131 rettv->vval.v_list = l;
9132 rettv->v_type = VAR_LIST;
9133 ++l->lv_refcount;
9134 }
9135 else
9136 rettv->vval.v_number = 0;
9137
9138 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9139 return;
9140
9141 if (!retlist)
9142 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009143 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9144 p = ml_get_buf(buf, start, FALSE);
9145 else
9146 p = (char_u *)"";
9147
9148 rettv->v_type = VAR_STRING;
9149 rettv->vval.v_string = vim_strsave(p);
9150 }
9151 else
9152 {
9153 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009154 return;
9155
9156 if (start < 1)
9157 start = 1;
9158 if (end > buf->b_ml.ml_line_count)
9159 end = buf->b_ml.ml_line_count;
9160 while (start <= end)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009161 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009162 li = listitem_alloc();
9163 if (li == NULL)
9164 break;
9165 list_append(l, li);
9166 li->li_tv.v_type = VAR_STRING;
9167 li->li_tv.v_lock = 0;
9168 li->li_tv.vval.v_string =
9169 vim_strsave(ml_get_buf(buf, start++, FALSE));
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009170 }
9171 }
9172}
9173
9174/*
9175 * "getbufline()" function
9176 */
9177 static void
9178f_getbufline(argvars, rettv)
9179 typval_T *argvars;
9180 typval_T *rettv;
9181{
9182 linenr_T lnum;
9183 linenr_T end;
9184 buf_T *buf;
9185
9186 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9187 ++emsg_off;
9188 buf = get_buf_tv(&argvars[0]);
9189 --emsg_off;
9190
Bram Moolenaar661b1822005-07-28 22:36:45 +00009191 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009192 if (argvars[2].v_type == VAR_UNKNOWN)
9193 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009194 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009195 end = get_tv_lnum_buf(&argvars[2], buf);
9196
Bram Moolenaar342337a2005-07-21 21:11:17 +00009197 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009198}
9199
Bram Moolenaar0d660222005-01-07 21:51:51 +00009200/*
9201 * "getbufvar()" function
9202 */
9203 static void
9204f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009205 typval_T *argvars;
9206 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009207{
9208 buf_T *buf;
9209 buf_T *save_curbuf;
9210 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009211 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009213 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9214 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009215 ++emsg_off;
9216 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009217
9218 rettv->v_type = VAR_STRING;
9219 rettv->vval.v_string = NULL;
9220
9221 if (buf != NULL && varname != NULL)
9222 {
9223 if (*varname == '&') /* buffer-local-option */
9224 {
9225 /* set curbuf to be our buf, temporarily */
9226 save_curbuf = curbuf;
9227 curbuf = buf;
9228
9229 get_option_tv(&varname, rettv, TRUE);
9230
9231 /* restore previous notion of curbuf */
9232 curbuf = save_curbuf;
9233 }
9234 else
9235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009236 if (*varname == NUL)
9237 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9238 * scope prefix before the NUL byte is required by
9239 * find_var_in_ht(). */
9240 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009241 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009242 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009243 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009244 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009245 }
9246 }
9247
9248 --emsg_off;
9249}
9250
9251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 * "getchar()" function
9253 */
9254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009255f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009256 typval_T *argvars;
9257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258{
9259 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009260 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261
9262 ++no_mapping;
9263 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009264 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 /* getchar(): blocking wait. */
9266 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009267 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 /* getchar(1): only check if char avail */
9269 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009270 else if (error || vpeekc() == NUL)
9271 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 n = 0;
9273 else
9274 /* getchar(0) and char avail: return char */
9275 n = safe_vgetc();
9276 --no_mapping;
9277 --allow_keys;
9278
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009279 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 if (IS_SPECIAL(n) || mod_mask != 0)
9281 {
9282 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9283 int i = 0;
9284
9285 /* Turn a special key into three bytes, plus modifier. */
9286 if (mod_mask != 0)
9287 {
9288 temp[i++] = K_SPECIAL;
9289 temp[i++] = KS_MODIFIER;
9290 temp[i++] = mod_mask;
9291 }
9292 if (IS_SPECIAL(n))
9293 {
9294 temp[i++] = K_SPECIAL;
9295 temp[i++] = K_SECOND(n);
9296 temp[i++] = K_THIRD(n);
9297 }
9298#ifdef FEAT_MBYTE
9299 else if (has_mbyte)
9300 i += (*mb_char2bytes)(n, temp + i);
9301#endif
9302 else
9303 temp[i++] = n;
9304 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305 rettv->v_type = VAR_STRING;
9306 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 }
9308}
9309
9310/*
9311 * "getcharmod()" function
9312 */
9313/*ARGSUSED*/
9314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009315f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009316 typval_T *argvars;
9317 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009319 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320}
9321
9322/*
9323 * "getcmdline()" function
9324 */
9325/*ARGSUSED*/
9326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009327f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009328 typval_T *argvars;
9329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009331 rettv->v_type = VAR_STRING;
9332 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333}
9334
9335/*
9336 * "getcmdpos()" function
9337 */
9338/*ARGSUSED*/
9339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009340f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009341 typval_T *argvars;
9342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009344 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345}
9346
9347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 * "getcwd()" function
9349 */
9350/*ARGSUSED*/
9351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009353 typval_T *argvars;
9354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355{
9356 char_u cwd[MAXPATHL];
9357
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009360 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 else
9362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009363 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009365 if (rettv->vval.v_string != NULL)
9366 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367#endif
9368 }
9369}
9370
9371/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009372 * "getfontname()" function
9373 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009374/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009376f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009377 typval_T *argvars;
9378 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009379{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009380 rettv->v_type = VAR_STRING;
9381 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009382#ifdef FEAT_GUI
9383 if (gui.in_use)
9384 {
9385 GuiFont font;
9386 char_u *name = NULL;
9387
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009388 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009389 {
9390 /* Get the "Normal" font. Either the name saved by
9391 * hl_set_font_name() or from the font ID. */
9392 font = gui.norm_font;
9393 name = hl_get_font_name();
9394 }
9395 else
9396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009398 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9399 return;
9400 font = gui_mch_get_font(name, FALSE);
9401 if (font == NOFONT)
9402 return; /* Invalid font name, return empty string. */
9403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009405 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009406 gui_mch_free_font(font);
9407 }
9408#endif
9409}
9410
9411/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009412 * "getfperm({fname})" function
9413 */
9414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009415f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009416 typval_T *argvars;
9417 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009418{
9419 char_u *fname;
9420 struct stat st;
9421 char_u *perm = NULL;
9422 char_u flags[] = "rwx";
9423 int i;
9424
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009425 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009426
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009428 if (mch_stat((char *)fname, &st) >= 0)
9429 {
9430 perm = vim_strsave((char_u *)"---------");
9431 if (perm != NULL)
9432 {
9433 for (i = 0; i < 9; i++)
9434 {
9435 if (st.st_mode & (1 << (8 - i)))
9436 perm[i] = flags[i % 3];
9437 }
9438 }
9439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009440 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009441}
9442
9443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 * "getfsize({fname})" function
9445 */
9446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009447f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009448 typval_T *argvars;
9449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450{
9451 char_u *fname;
9452 struct stat st;
9453
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009454 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457
9458 if (mch_stat((char *)fname, &st) >= 0)
9459 {
9460 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009463 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 }
9465 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467}
9468
9469/*
9470 * "getftime({fname})" function
9471 */
9472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009473f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009474 typval_T *argvars;
9475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476{
9477 char_u *fname;
9478 struct stat st;
9479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009480 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481
9482 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009483 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009485 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486}
9487
9488/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009489 * "getftype({fname})" function
9490 */
9491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009493 typval_T *argvars;
9494 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009495{
9496 char_u *fname;
9497 struct stat st;
9498 char_u *type = NULL;
9499 char *t;
9500
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009501 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009502
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009503 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009504 if (mch_lstat((char *)fname, &st) >= 0)
9505 {
9506#ifdef S_ISREG
9507 if (S_ISREG(st.st_mode))
9508 t = "file";
9509 else if (S_ISDIR(st.st_mode))
9510 t = "dir";
9511# ifdef S_ISLNK
9512 else if (S_ISLNK(st.st_mode))
9513 t = "link";
9514# endif
9515# ifdef S_ISBLK
9516 else if (S_ISBLK(st.st_mode))
9517 t = "bdev";
9518# endif
9519# ifdef S_ISCHR
9520 else if (S_ISCHR(st.st_mode))
9521 t = "cdev";
9522# endif
9523# ifdef S_ISFIFO
9524 else if (S_ISFIFO(st.st_mode))
9525 t = "fifo";
9526# endif
9527# ifdef S_ISSOCK
9528 else if (S_ISSOCK(st.st_mode))
9529 t = "fifo";
9530# endif
9531 else
9532 t = "other";
9533#else
9534# ifdef S_IFMT
9535 switch (st.st_mode & S_IFMT)
9536 {
9537 case S_IFREG: t = "file"; break;
9538 case S_IFDIR: t = "dir"; break;
9539# ifdef S_IFLNK
9540 case S_IFLNK: t = "link"; break;
9541# endif
9542# ifdef S_IFBLK
9543 case S_IFBLK: t = "bdev"; break;
9544# endif
9545# ifdef S_IFCHR
9546 case S_IFCHR: t = "cdev"; break;
9547# endif
9548# ifdef S_IFIFO
9549 case S_IFIFO: t = "fifo"; break;
9550# endif
9551# ifdef S_IFSOCK
9552 case S_IFSOCK: t = "socket"; break;
9553# endif
9554 default: t = "other";
9555 }
9556# else
9557 if (mch_isdir(fname))
9558 t = "dir";
9559 else
9560 t = "file";
9561# endif
9562#endif
9563 type = vim_strsave((char_u *)t);
9564 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009566}
9567
9568/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009569 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009570 */
9571 static void
9572f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009573 typval_T *argvars;
9574 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009575{
9576 linenr_T lnum;
9577 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009578 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009579
9580 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009581 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009582 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009583 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009584 retlist = FALSE;
9585 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009586 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009587 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009588 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009589 retlist = TRUE;
9590 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009591
Bram Moolenaar342337a2005-07-21 21:11:17 +00009592 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009593}
9594
9595/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009596 * "getqflist()" function
9597 */
9598/*ARGSUSED*/
9599 static void
9600f_getqflist(argvars, rettv)
9601 typval_T *argvars;
9602 typval_T *rettv;
9603{
9604#ifdef FEAT_QUICKFIX
9605 list_T *l;
9606#endif
9607
9608 rettv->vval.v_number = FALSE;
9609#ifdef FEAT_QUICKFIX
9610 l = list_alloc();
9611 if (l != NULL)
9612 {
9613 if (get_errorlist(l) != FAIL)
9614 {
9615 rettv->vval.v_list = l;
9616 rettv->v_type = VAR_LIST;
9617 ++l->lv_refcount;
9618 }
9619 else
9620 list_free(l);
9621 }
9622#endif
9623}
9624
9625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 * "getreg()" function
9627 */
9628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009630 typval_T *argvars;
9631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632{
9633 char_u *strregname;
9634 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009635 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009636 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009638 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009639 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009640 strregname = get_tv_string_chk(&argvars[0]);
9641 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009642 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009643 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009646 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 regname = (strregname == NULL ? '"' : *strregname);
9648 if (regname == 0)
9649 regname = '"';
9650
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009652 rettv->vval.v_string = error ? NULL :
9653 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654}
9655
9656/*
9657 * "getregtype()" function
9658 */
9659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009661 typval_T *argvars;
9662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663{
9664 char_u *strregname;
9665 int regname;
9666 char_u buf[NUMBUFLEN + 2];
9667 long reglen = 0;
9668
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009669 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009670 {
9671 strregname = get_tv_string_chk(&argvars[0]);
9672 if (strregname == NULL) /* type error; errmsg already given */
9673 {
9674 rettv->v_type = VAR_STRING;
9675 rettv->vval.v_string = NULL;
9676 return;
9677 }
9678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 else
9680 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009681 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682
9683 regname = (strregname == NULL ? '"' : *strregname);
9684 if (regname == 0)
9685 regname = '"';
9686
9687 buf[0] = NUL;
9688 buf[1] = NUL;
9689 switch (get_reg_type(regname, &reglen))
9690 {
9691 case MLINE: buf[0] = 'V'; break;
9692 case MCHAR: buf[0] = 'v'; break;
9693#ifdef FEAT_VISUAL
9694 case MBLOCK:
9695 buf[0] = Ctrl_V;
9696 sprintf((char *)buf + 1, "%ld", reglen + 1);
9697 break;
9698#endif
9699 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 rettv->v_type = VAR_STRING;
9701 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702}
9703
9704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 * "getwinposx()" function
9706 */
9707/*ARGSUSED*/
9708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009710 typval_T *argvars;
9711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009713 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714#ifdef FEAT_GUI
9715 if (gui.in_use)
9716 {
9717 int x, y;
9718
9719 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 }
9722#endif
9723}
9724
9725/*
9726 * "getwinposy()" function
9727 */
9728/*ARGSUSED*/
9729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009730f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009731 typval_T *argvars;
9732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009734 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735#ifdef FEAT_GUI
9736 if (gui.in_use)
9737 {
9738 int x, y;
9739
9740 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009741 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 }
9743#endif
9744}
9745
Bram Moolenaara40058a2005-07-11 22:42:07 +00009746static win_T *find_win_by_nr __ARGS((typval_T *vp));
9747
9748 static win_T *
9749find_win_by_nr(vp)
9750 typval_T *vp;
9751{
9752#ifdef FEAT_WINDOWS
9753 win_T *wp;
9754#endif
9755 int nr;
9756
9757 nr = get_tv_number_chk(vp, NULL);
9758
9759#ifdef FEAT_WINDOWS
9760 if (nr < 0)
9761 return NULL;
9762 if (nr == 0)
9763 return curwin;
9764
9765 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9766 if (--nr <= 0)
9767 break;
9768 return wp;
9769#else
9770 if (nr == 0 || nr == 1)
9771 return curwin;
9772 return NULL;
9773#endif
9774}
9775
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776/*
9777 * "getwinvar()" function
9778 */
9779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009781 typval_T *argvars;
9782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783{
9784 win_T *win, *oldcurwin;
9785 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009786 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009789 varname = get_tv_string_chk(&argvars[1]);
9790 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792 rettv->v_type = VAR_STRING;
9793 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794
9795 if (win != NULL && varname != NULL)
9796 {
9797 if (*varname == '&') /* window-local-option */
9798 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009799 /* Set curwin to be our win, temporarily. Also set curbuf, so
9800 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 oldcurwin = curwin;
9802 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009803 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806
9807 /* restore previous notion of curwin */
9808 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009809 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 }
9811 else
9812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009813 if (*varname == NUL)
9814 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9815 * scope prefix before the NUL byte is required by
9816 * find_var_in_ht(). */
9817 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009819 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009821 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 }
9823 }
9824
9825 --emsg_off;
9826}
9827
9828/*
9829 * "glob()" function
9830 */
9831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009832f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009833 typval_T *argvars;
9834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835{
9836 expand_T xpc;
9837
9838 ExpandInit(&xpc);
9839 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009840 rettv->v_type = VAR_STRING;
9841 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9843 ExpandCleanup(&xpc);
9844}
9845
9846/*
9847 * "globpath()" function
9848 */
9849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009850f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009851 typval_T *argvars;
9852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853{
9854 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009855 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009857 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009858 if (file == NULL)
9859 rettv->vval.v_string = NULL;
9860 else
9861 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862}
9863
9864/*
9865 * "has()" function
9866 */
9867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009868f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009869 typval_T *argvars;
9870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871{
9872 int i;
9873 char_u *name;
9874 int n = FALSE;
9875 static char *(has_list[]) =
9876 {
9877#ifdef AMIGA
9878 "amiga",
9879# ifdef FEAT_ARP
9880 "arp",
9881# endif
9882#endif
9883#ifdef __BEOS__
9884 "beos",
9885#endif
9886#ifdef MSDOS
9887# ifdef DJGPP
9888 "dos32",
9889# else
9890 "dos16",
9891# endif
9892#endif
9893#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9894 "mac",
9895#endif
9896#if defined(MACOS_X_UNIX)
9897 "macunix",
9898#endif
9899#ifdef OS2
9900 "os2",
9901#endif
9902#ifdef __QNX__
9903 "qnx",
9904#endif
9905#ifdef RISCOS
9906 "riscos",
9907#endif
9908#ifdef UNIX
9909 "unix",
9910#endif
9911#ifdef VMS
9912 "vms",
9913#endif
9914#ifdef WIN16
9915 "win16",
9916#endif
9917#ifdef WIN32
9918 "win32",
9919#endif
9920#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9921 "win32unix",
9922#endif
9923#ifdef WIN64
9924 "win64",
9925#endif
9926#ifdef EBCDIC
9927 "ebcdic",
9928#endif
9929#ifndef CASE_INSENSITIVE_FILENAME
9930 "fname_case",
9931#endif
9932#ifdef FEAT_ARABIC
9933 "arabic",
9934#endif
9935#ifdef FEAT_AUTOCMD
9936 "autocmd",
9937#endif
9938#ifdef FEAT_BEVAL
9939 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +00009940# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
9941 "balloon_multiline",
9942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943#endif
9944#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9945 "builtin_terms",
9946# ifdef ALL_BUILTIN_TCAPS
9947 "all_builtin_terms",
9948# endif
9949#endif
9950#ifdef FEAT_BYTEOFF
9951 "byte_offset",
9952#endif
9953#ifdef FEAT_CINDENT
9954 "cindent",
9955#endif
9956#ifdef FEAT_CLIENTSERVER
9957 "clientserver",
9958#endif
9959#ifdef FEAT_CLIPBOARD
9960 "clipboard",
9961#endif
9962#ifdef FEAT_CMDL_COMPL
9963 "cmdline_compl",
9964#endif
9965#ifdef FEAT_CMDHIST
9966 "cmdline_hist",
9967#endif
9968#ifdef FEAT_COMMENTS
9969 "comments",
9970#endif
9971#ifdef FEAT_CRYPT
9972 "cryptv",
9973#endif
9974#ifdef FEAT_CSCOPE
9975 "cscope",
9976#endif
9977#ifdef DEBUG
9978 "debug",
9979#endif
9980#ifdef FEAT_CON_DIALOG
9981 "dialog_con",
9982#endif
9983#ifdef FEAT_GUI_DIALOG
9984 "dialog_gui",
9985#endif
9986#ifdef FEAT_DIFF
9987 "diff",
9988#endif
9989#ifdef FEAT_DIGRAPHS
9990 "digraphs",
9991#endif
9992#ifdef FEAT_DND
9993 "dnd",
9994#endif
9995#ifdef FEAT_EMACS_TAGS
9996 "emacs_tags",
9997#endif
9998 "eval", /* always present, of course! */
9999#ifdef FEAT_EX_EXTRA
10000 "ex_extra",
10001#endif
10002#ifdef FEAT_SEARCH_EXTRA
10003 "extra_search",
10004#endif
10005#ifdef FEAT_FKMAP
10006 "farsi",
10007#endif
10008#ifdef FEAT_SEARCHPATH
10009 "file_in_path",
10010#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010011#if defined(UNIX) && !defined(USE_SYSTEM)
10012 "filterpipe",
10013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014#ifdef FEAT_FIND_ID
10015 "find_in_path",
10016#endif
10017#ifdef FEAT_FOLDING
10018 "folding",
10019#endif
10020#ifdef FEAT_FOOTER
10021 "footer",
10022#endif
10023#if !defined(USE_SYSTEM) && defined(UNIX)
10024 "fork",
10025#endif
10026#ifdef FEAT_GETTEXT
10027 "gettext",
10028#endif
10029#ifdef FEAT_GUI
10030 "gui",
10031#endif
10032#ifdef FEAT_GUI_ATHENA
10033# ifdef FEAT_GUI_NEXTAW
10034 "gui_neXtaw",
10035# else
10036 "gui_athena",
10037# endif
10038#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +000010039#ifdef FEAT_GUI_KDE
10040 "gui_kde",
10041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042#ifdef FEAT_GUI_GTK
10043 "gui_gtk",
10044# ifdef HAVE_GTK2
10045 "gui_gtk2",
10046# endif
10047#endif
10048#ifdef FEAT_GUI_MAC
10049 "gui_mac",
10050#endif
10051#ifdef FEAT_GUI_MOTIF
10052 "gui_motif",
10053#endif
10054#ifdef FEAT_GUI_PHOTON
10055 "gui_photon",
10056#endif
10057#ifdef FEAT_GUI_W16
10058 "gui_win16",
10059#endif
10060#ifdef FEAT_GUI_W32
10061 "gui_win32",
10062#endif
10063#ifdef FEAT_HANGULIN
10064 "hangul_input",
10065#endif
10066#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10067 "iconv",
10068#endif
10069#ifdef FEAT_INS_EXPAND
10070 "insert_expand",
10071#endif
10072#ifdef FEAT_JUMPLIST
10073 "jumplist",
10074#endif
10075#ifdef FEAT_KEYMAP
10076 "keymap",
10077#endif
10078#ifdef FEAT_LANGMAP
10079 "langmap",
10080#endif
10081#ifdef FEAT_LIBCALL
10082 "libcall",
10083#endif
10084#ifdef FEAT_LINEBREAK
10085 "linebreak",
10086#endif
10087#ifdef FEAT_LISP
10088 "lispindent",
10089#endif
10090#ifdef FEAT_LISTCMDS
10091 "listcmds",
10092#endif
10093#ifdef FEAT_LOCALMAP
10094 "localmap",
10095#endif
10096#ifdef FEAT_MENU
10097 "menu",
10098#endif
10099#ifdef FEAT_SESSION
10100 "mksession",
10101#endif
10102#ifdef FEAT_MODIFY_FNAME
10103 "modify_fname",
10104#endif
10105#ifdef FEAT_MOUSE
10106 "mouse",
10107#endif
10108#ifdef FEAT_MOUSESHAPE
10109 "mouseshape",
10110#endif
10111#if defined(UNIX) || defined(VMS)
10112# ifdef FEAT_MOUSE_DEC
10113 "mouse_dec",
10114# endif
10115# ifdef FEAT_MOUSE_GPM
10116 "mouse_gpm",
10117# endif
10118# ifdef FEAT_MOUSE_JSB
10119 "mouse_jsbterm",
10120# endif
10121# ifdef FEAT_MOUSE_NET
10122 "mouse_netterm",
10123# endif
10124# ifdef FEAT_MOUSE_PTERM
10125 "mouse_pterm",
10126# endif
10127# ifdef FEAT_MOUSE_XTERM
10128 "mouse_xterm",
10129# endif
10130#endif
10131#ifdef FEAT_MBYTE
10132 "multi_byte",
10133#endif
10134#ifdef FEAT_MBYTE_IME
10135 "multi_byte_ime",
10136#endif
10137#ifdef FEAT_MULTI_LANG
10138 "multi_lang",
10139#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010140#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010141#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010142 "mzscheme",
10143#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145#ifdef FEAT_OLE
10146 "ole",
10147#endif
10148#ifdef FEAT_OSFILETYPE
10149 "osfiletype",
10150#endif
10151#ifdef FEAT_PATH_EXTRA
10152 "path_extra",
10153#endif
10154#ifdef FEAT_PERL
10155#ifndef DYNAMIC_PERL
10156 "perl",
10157#endif
10158#endif
10159#ifdef FEAT_PYTHON
10160#ifndef DYNAMIC_PYTHON
10161 "python",
10162#endif
10163#endif
10164#ifdef FEAT_POSTSCRIPT
10165 "postscript",
10166#endif
10167#ifdef FEAT_PRINTER
10168 "printer",
10169#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010170#ifdef FEAT_PROFILE
10171 "profile",
10172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173#ifdef FEAT_QUICKFIX
10174 "quickfix",
10175#endif
10176#ifdef FEAT_RIGHTLEFT
10177 "rightleft",
10178#endif
10179#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10180 "ruby",
10181#endif
10182#ifdef FEAT_SCROLLBIND
10183 "scrollbind",
10184#endif
10185#ifdef FEAT_CMDL_INFO
10186 "showcmd",
10187 "cmdline_info",
10188#endif
10189#ifdef FEAT_SIGNS
10190 "signs",
10191#endif
10192#ifdef FEAT_SMARTINDENT
10193 "smartindent",
10194#endif
10195#ifdef FEAT_SNIFF
10196 "sniff",
10197#endif
10198#ifdef FEAT_STL_OPT
10199 "statusline",
10200#endif
10201#ifdef FEAT_SUN_WORKSHOP
10202 "sun_workshop",
10203#endif
10204#ifdef FEAT_NETBEANS_INTG
10205 "netbeans_intg",
10206#endif
10207#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010208 "spell",
10209#endif
10210#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 "syntax",
10212#endif
10213#if defined(USE_SYSTEM) || !defined(UNIX)
10214 "system",
10215#endif
10216#ifdef FEAT_TAG_BINS
10217 "tag_binary",
10218#endif
10219#ifdef FEAT_TAG_OLDSTATIC
10220 "tag_old_static",
10221#endif
10222#ifdef FEAT_TAG_ANYWHITE
10223 "tag_any_white",
10224#endif
10225#ifdef FEAT_TCL
10226# ifndef DYNAMIC_TCL
10227 "tcl",
10228# endif
10229#endif
10230#ifdef TERMINFO
10231 "terminfo",
10232#endif
10233#ifdef FEAT_TERMRESPONSE
10234 "termresponse",
10235#endif
10236#ifdef FEAT_TEXTOBJ
10237 "textobjects",
10238#endif
10239#ifdef HAVE_TGETENT
10240 "tgetent",
10241#endif
10242#ifdef FEAT_TITLE
10243 "title",
10244#endif
10245#ifdef FEAT_TOOLBAR
10246 "toolbar",
10247#endif
10248#ifdef FEAT_USR_CMDS
10249 "user-commands", /* was accidentally included in 5.4 */
10250 "user_commands",
10251#endif
10252#ifdef FEAT_VIMINFO
10253 "viminfo",
10254#endif
10255#ifdef FEAT_VERTSPLIT
10256 "vertsplit",
10257#endif
10258#ifdef FEAT_VIRTUALEDIT
10259 "virtualedit",
10260#endif
10261#ifdef FEAT_VISUAL
10262 "visual",
10263#endif
10264#ifdef FEAT_VISUALEXTRA
10265 "visualextra",
10266#endif
10267#ifdef FEAT_VREPLACE
10268 "vreplace",
10269#endif
10270#ifdef FEAT_WILDIGN
10271 "wildignore",
10272#endif
10273#ifdef FEAT_WILDMENU
10274 "wildmenu",
10275#endif
10276#ifdef FEAT_WINDOWS
10277 "windows",
10278#endif
10279#ifdef FEAT_WAK
10280 "winaltkeys",
10281#endif
10282#ifdef FEAT_WRITEBACKUP
10283 "writebackup",
10284#endif
10285#ifdef FEAT_XIM
10286 "xim",
10287#endif
10288#ifdef FEAT_XFONTSET
10289 "xfontset",
10290#endif
10291#ifdef USE_XSMP
10292 "xsmp",
10293#endif
10294#ifdef USE_XSMP_INTERACT
10295 "xsmp_interact",
10296#endif
10297#ifdef FEAT_XCLIPBOARD
10298 "xterm_clipboard",
10299#endif
10300#ifdef FEAT_XTERM_SAVE
10301 "xterm_save",
10302#endif
10303#if defined(UNIX) && defined(FEAT_X11)
10304 "X11",
10305#endif
10306 NULL
10307 };
10308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010309 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 for (i = 0; has_list[i] != NULL; ++i)
10311 if (STRICMP(name, has_list[i]) == 0)
10312 {
10313 n = TRUE;
10314 break;
10315 }
10316
10317 if (n == FALSE)
10318 {
10319 if (STRNICMP(name, "patch", 5) == 0)
10320 n = has_patch(atoi((char *)name + 5));
10321 else if (STRICMP(name, "vim_starting") == 0)
10322 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010323#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10324 else if (STRICMP(name, "balloon_multiline") == 0)
10325 n = multiline_balloon_available();
10326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327#ifdef DYNAMIC_TCL
10328 else if (STRICMP(name, "tcl") == 0)
10329 n = tcl_enabled(FALSE);
10330#endif
10331#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10332 else if (STRICMP(name, "iconv") == 0)
10333 n = iconv_enabled(FALSE);
10334#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010335#ifdef DYNAMIC_MZSCHEME
10336 else if (STRICMP(name, "mzscheme") == 0)
10337 n = mzscheme_enabled(FALSE);
10338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339#ifdef DYNAMIC_RUBY
10340 else if (STRICMP(name, "ruby") == 0)
10341 n = ruby_enabled(FALSE);
10342#endif
10343#ifdef DYNAMIC_PYTHON
10344 else if (STRICMP(name, "python") == 0)
10345 n = python_enabled(FALSE);
10346#endif
10347#ifdef DYNAMIC_PERL
10348 else if (STRICMP(name, "perl") == 0)
10349 n = perl_enabled(FALSE);
10350#endif
10351#ifdef FEAT_GUI
10352 else if (STRICMP(name, "gui_running") == 0)
10353 n = (gui.in_use || gui.starting);
10354# ifdef FEAT_GUI_W32
10355 else if (STRICMP(name, "gui_win32s") == 0)
10356 n = gui_is_win32s();
10357# endif
10358# ifdef FEAT_BROWSE
10359 else if (STRICMP(name, "browse") == 0)
10360 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10361# endif
10362#endif
10363#ifdef FEAT_SYN_HL
10364 else if (STRICMP(name, "syntax_items") == 0)
10365 n = syntax_present(curbuf);
10366#endif
10367#if defined(WIN3264)
10368 else if (STRICMP(name, "win95") == 0)
10369 n = mch_windows95();
10370#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010371#ifdef FEAT_NETBEANS_INTG
10372 else if (STRICMP(name, "netbeans_enabled") == 0)
10373 n = usingNetbeans;
10374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 }
10376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010377 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378}
10379
10380/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010381 * "has_key()" function
10382 */
10383 static void
10384f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010385 typval_T *argvars;
10386 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010387{
10388 rettv->vval.v_number = 0;
10389 if (argvars[0].v_type != VAR_DICT)
10390 {
10391 EMSG(_(e_dictreq));
10392 return;
10393 }
10394 if (argvars[0].vval.v_dict == NULL)
10395 return;
10396
10397 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010398 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010399}
10400
10401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 * "hasmapto()" function
10403 */
10404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010405f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010406 typval_T *argvars;
10407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408{
10409 char_u *name;
10410 char_u *mode;
10411 char_u buf[NUMBUFLEN];
10412
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010413 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010414 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 mode = (char_u *)"nvo";
10416 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010417 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418
10419 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010420 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010422 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423}
10424
10425/*
10426 * "histadd()" function
10427 */
10428/*ARGSUSED*/
10429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010430f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010431 typval_T *argvars;
10432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433{
10434#ifdef FEAT_CMDHIST
10435 int histype;
10436 char_u *str;
10437 char_u buf[NUMBUFLEN];
10438#endif
10439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010440 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 if (check_restricted() || check_secure())
10442 return;
10443#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010444 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10445 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 if (histype >= 0)
10447 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010448 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 if (*str != NUL)
10450 {
10451 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010452 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 return;
10454 }
10455 }
10456#endif
10457}
10458
10459/*
10460 * "histdel()" function
10461 */
10462/*ARGSUSED*/
10463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010464f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010465 typval_T *argvars;
10466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467{
10468#ifdef FEAT_CMDHIST
10469 int n;
10470 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010471 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010473 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10474 if (str == NULL)
10475 n = 0;
10476 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010478 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010479 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010482 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 else
10484 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010485 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010486 get_tv_string_buf(&argvars[1], buf));
10487 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010489 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490#endif
10491}
10492
10493/*
10494 * "histget()" function
10495 */
10496/*ARGSUSED*/
10497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010498f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010499 typval_T *argvars;
10500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501{
10502#ifdef FEAT_CMDHIST
10503 int type;
10504 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010505 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010507 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10508 if (str == NULL)
10509 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010511 {
10512 type = get_histtype(str);
10513 if (argvars[1].v_type == VAR_UNKNOWN)
10514 idx = get_history_idx(type);
10515 else
10516 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10517 /* -1 on type error */
10518 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010521 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010523 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524}
10525
10526/*
10527 * "histnr()" function
10528 */
10529/*ARGSUSED*/
10530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010531f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010532 typval_T *argvars;
10533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534{
10535 int i;
10536
10537#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010538 char_u *history = get_tv_string_chk(&argvars[0]);
10539
10540 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 if (i >= HIST_CMD && i < HIST_COUNT)
10542 i = get_history_idx(i);
10543 else
10544#endif
10545 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547}
10548
10549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 * "highlightID(name)" function
10551 */
10552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010553f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010554 typval_T *argvars;
10555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010557 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558}
10559
10560/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010561 * "highlight_exists()" function
10562 */
10563 static void
10564f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010565 typval_T *argvars;
10566 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010567{
10568 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10569}
10570
10571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 * "hostname()" function
10573 */
10574/*ARGSUSED*/
10575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010576f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010577 typval_T *argvars;
10578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579{
10580 char_u hostname[256];
10581
10582 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010583 rettv->v_type = VAR_STRING;
10584 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585}
10586
10587/*
10588 * iconv() function
10589 */
10590/*ARGSUSED*/
10591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010592f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010593 typval_T *argvars;
10594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595{
10596#ifdef FEAT_MBYTE
10597 char_u buf1[NUMBUFLEN];
10598 char_u buf2[NUMBUFLEN];
10599 char_u *from, *to, *str;
10600 vimconv_T vimconv;
10601#endif
10602
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010603 rettv->v_type = VAR_STRING;
10604 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605
10606#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010607 str = get_tv_string(&argvars[0]);
10608 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10609 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 vimconv.vc_type = CONV_NONE;
10611 convert_setup(&vimconv, from, to);
10612
10613 /* If the encodings are equal, no conversion needed. */
10614 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010615 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010617 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618
10619 convert_setup(&vimconv, NULL, NULL);
10620 vim_free(from);
10621 vim_free(to);
10622#endif
10623}
10624
10625/*
10626 * "indent()" function
10627 */
10628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010629f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010630 typval_T *argvars;
10631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632{
10633 linenr_T lnum;
10634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010635 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010637 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010639 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640}
10641
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010642/*
10643 * "index()" function
10644 */
10645 static void
10646f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010647 typval_T *argvars;
10648 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010649{
Bram Moolenaar33570922005-01-25 22:26:29 +000010650 list_T *l;
10651 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010652 long idx = 0;
10653 int ic = FALSE;
10654
10655 rettv->vval.v_number = -1;
10656 if (argvars[0].v_type != VAR_LIST)
10657 {
10658 EMSG(_(e_listreq));
10659 return;
10660 }
10661 l = argvars[0].vval.v_list;
10662 if (l != NULL)
10663 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010664 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010665 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010667 int error = FALSE;
10668
Bram Moolenaar758711c2005-02-02 23:11:38 +000010669 /* Start at specified item. Use the cached index that list_find()
10670 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010671 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010672 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010673 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010674 ic = get_tv_number_chk(&argvars[3], &error);
10675 if (error)
10676 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010677 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010678
Bram Moolenaar758711c2005-02-02 23:11:38 +000010679 for ( ; item != NULL; item = item->li_next, ++idx)
10680 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010681 {
10682 rettv->vval.v_number = idx;
10683 break;
10684 }
10685 }
10686}
10687
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688static int inputsecret_flag = 0;
10689
10690/*
10691 * "input()" function
10692 * Also handles inputsecret() when inputsecret is set.
10693 */
10694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010696 typval_T *argvars;
10697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010699 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 char_u *p = NULL;
10701 int c;
10702 char_u buf[NUMBUFLEN];
10703 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010704 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010706 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707
10708#ifdef NO_CONSOLE_INPUT
10709 /* While starting up, there is no place to enter text. */
10710 if (no_console_input())
10711 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010712 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 return;
10714 }
10715#endif
10716
10717 cmd_silent = FALSE; /* Want to see the prompt. */
10718 if (prompt != NULL)
10719 {
10720 /* Only the part of the message after the last NL is considered as
10721 * prompt for the command line */
10722 p = vim_strrchr(prompt, '\n');
10723 if (p == NULL)
10724 p = prompt;
10725 else
10726 {
10727 ++p;
10728 c = *p;
10729 *p = NUL;
10730 msg_start();
10731 msg_clr_eos();
10732 msg_puts_attr(prompt, echo_attr);
10733 msg_didout = FALSE;
10734 msg_starthere();
10735 *p = c;
10736 }
10737 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010739 if (argvars[1].v_type != VAR_UNKNOWN)
10740 {
10741 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10742 if (defstr != NULL)
10743 stuffReadbuffSpec(defstr);
10744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010746 if (defstr != NULL)
10747 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010750 /* since the user typed this, no need to wait for return */
10751 need_wait_return = FALSE;
10752 msg_didout = FALSE;
10753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 cmd_silent = cmd_silent_save;
10755}
10756
10757/*
10758 * "inputdialog()" function
10759 */
10760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010761f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010762 typval_T *argvars;
10763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764{
10765#if defined(FEAT_GUI_TEXTDIALOG)
10766 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10767 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10768 {
10769 char_u *message;
10770 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010771 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010773 message = get_tv_string_chk(&argvars[0]);
10774 if (argvars[1].v_type != VAR_UNKNOWN
10775 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010776 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 else
10778 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010779 if (message != NULL && defstr != NULL
10780 && do_dialog(VIM_QUESTION, NULL, message,
10781 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010782 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 else
10784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010785 if (message != NULL && defstr != NULL
10786 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010787 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788 rettv->vval.v_string = vim_strsave(
10789 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010791 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010793 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 }
10795 else
10796#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010797 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798}
10799
10800static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10801
10802/*
10803 * "inputrestore()" function
10804 */
10805/*ARGSUSED*/
10806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010807f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010808 typval_T *argvars;
10809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810{
10811 if (ga_userinput.ga_len > 0)
10812 {
10813 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10815 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010816 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 }
10818 else if (p_verbose > 1)
10819 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010820 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010821 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 }
10823}
10824
10825/*
10826 * "inputsave()" function
10827 */
10828/*ARGSUSED*/
10829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010830f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010831 typval_T *argvars;
10832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833{
10834 /* Add an entry to the stack of typehead storage. */
10835 if (ga_grow(&ga_userinput, 1) == OK)
10836 {
10837 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10838 + ga_userinput.ga_len);
10839 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 }
10842 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010843 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844}
10845
10846/*
10847 * "inputsecret()" function
10848 */
10849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010850f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010851 typval_T *argvars;
10852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853{
10854 ++cmdline_star;
10855 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010856 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 --cmdline_star;
10858 --inputsecret_flag;
10859}
10860
10861/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010862 * "insert()" function
10863 */
10864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010865f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010866 typval_T *argvars;
10867 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010868{
10869 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010870 listitem_T *item;
10871 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010872 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010873
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010874 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010875 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010876 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010877 else if ((l = argvars[0].vval.v_list) != NULL
10878 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010879 {
10880 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010881 before = get_tv_number_chk(&argvars[2], &error);
10882 if (error)
10883 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010884
Bram Moolenaar758711c2005-02-02 23:11:38 +000010885 if (before == l->lv_len)
10886 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010887 else
10888 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010889 item = list_find(l, before);
10890 if (item == NULL)
10891 {
10892 EMSGN(_(e_listidx), before);
10893 l = NULL;
10894 }
10895 }
10896 if (l != NULL)
10897 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010898 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010899 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010900 }
10901 }
10902}
10903
10904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 * "isdirectory()" function
10906 */
10907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010909 typval_T *argvars;
10910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913}
10914
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010915/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010916 * "islocked()" function
10917 */
10918 static void
10919f_islocked(argvars, rettv)
10920 typval_T *argvars;
10921 typval_T *rettv;
10922{
10923 lval_T lv;
10924 char_u *end;
10925 dictitem_T *di;
10926
10927 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010928 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10929 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010930 if (end != NULL && lv.ll_name != NULL)
10931 {
10932 if (*end != NUL)
10933 EMSG(_(e_trailing));
10934 else
10935 {
10936 if (lv.ll_tv == NULL)
10937 {
10938 if (check_changedtick(lv.ll_name))
10939 rettv->vval.v_number = 1; /* always locked */
10940 else
10941 {
10942 di = find_var(lv.ll_name, NULL);
10943 if (di != NULL)
10944 {
10945 /* Consider a variable locked when:
10946 * 1. the variable itself is locked
10947 * 2. the value of the variable is locked.
10948 * 3. the List or Dict value is locked.
10949 */
10950 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10951 || tv_islocked(&di->di_tv));
10952 }
10953 }
10954 }
10955 else if (lv.ll_range)
10956 EMSG(_("E745: Range not allowed"));
10957 else if (lv.ll_newkey != NULL)
10958 EMSG2(_(e_dictkey), lv.ll_newkey);
10959 else if (lv.ll_list != NULL)
10960 /* List item. */
10961 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10962 else
10963 /* Dictionary item. */
10964 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10965 }
10966 }
10967
10968 clear_lval(&lv);
10969}
10970
Bram Moolenaar33570922005-01-25 22:26:29 +000010971static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010972
10973/*
10974 * Turn a dict into a list:
10975 * "what" == 0: list of keys
10976 * "what" == 1: list of values
10977 * "what" == 2: list of items
10978 */
10979 static void
10980dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010981 typval_T *argvars;
10982 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010983 int what;
10984{
Bram Moolenaar33570922005-01-25 22:26:29 +000010985 list_T *l;
10986 list_T *l2;
10987 dictitem_T *di;
10988 hashitem_T *hi;
10989 listitem_T *li;
10990 listitem_T *li2;
10991 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010992 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010993
10994 rettv->vval.v_number = 0;
10995 if (argvars[0].v_type != VAR_DICT)
10996 {
10997 EMSG(_(e_dictreq));
10998 return;
10999 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011000 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011001 return;
11002
11003 l = list_alloc();
11004 if (l == NULL)
11005 return;
11006 rettv->v_type = VAR_LIST;
11007 rettv->vval.v_list = l;
11008 ++l->lv_refcount;
11009
Bram Moolenaar33570922005-01-25 22:26:29 +000011010 todo = d->dv_hashtab.ht_used;
11011 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011012 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011013 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011014 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011015 --todo;
11016 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011017
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011018 li = listitem_alloc();
11019 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011020 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011021 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011022
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011023 if (what == 0)
11024 {
11025 /* keys() */
11026 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011027 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011028 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11029 }
11030 else if (what == 1)
11031 {
11032 /* values() */
11033 copy_tv(&di->di_tv, &li->li_tv);
11034 }
11035 else
11036 {
11037 /* items() */
11038 l2 = list_alloc();
11039 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011040 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011041 li->li_tv.vval.v_list = l2;
11042 if (l2 == NULL)
11043 break;
11044 ++l2->lv_refcount;
11045
11046 li2 = listitem_alloc();
11047 if (li2 == NULL)
11048 break;
11049 list_append(l2, li2);
11050 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011051 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011052 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11053
11054 li2 = listitem_alloc();
11055 if (li2 == NULL)
11056 break;
11057 list_append(l2, li2);
11058 copy_tv(&di->di_tv, &li2->li_tv);
11059 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011060 }
11061 }
11062}
11063
11064/*
11065 * "items(dict)" function
11066 */
11067 static void
11068f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011069 typval_T *argvars;
11070 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011071{
11072 dict_list(argvars, rettv, 2);
11073}
11074
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011076 * "join()" function
11077 */
11078 static void
11079f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011080 typval_T *argvars;
11081 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011082{
11083 garray_T ga;
11084 char_u *sep;
11085
11086 rettv->vval.v_number = 0;
11087 if (argvars[0].v_type != VAR_LIST)
11088 {
11089 EMSG(_(e_listreq));
11090 return;
11091 }
11092 if (argvars[0].vval.v_list == NULL)
11093 return;
11094 if (argvars[1].v_type == VAR_UNKNOWN)
11095 sep = (char_u *)" ";
11096 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011097 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011098
11099 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011100
11101 if (sep != NULL)
11102 {
11103 ga_init2(&ga, (int)sizeof(char), 80);
11104 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11105 ga_append(&ga, NUL);
11106 rettv->vval.v_string = (char_u *)ga.ga_data;
11107 }
11108 else
11109 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011110}
11111
11112/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011113 * "keys()" function
11114 */
11115 static void
11116f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011117 typval_T *argvars;
11118 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011119{
11120 dict_list(argvars, rettv, 0);
11121}
11122
11123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 * "last_buffer_nr()" function.
11125 */
11126/*ARGSUSED*/
11127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011128f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011129 typval_T *argvars;
11130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131{
11132 int n = 0;
11133 buf_T *buf;
11134
11135 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11136 if (n < buf->b_fnum)
11137 n = buf->b_fnum;
11138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011139 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011140}
11141
11142/*
11143 * "len()" function
11144 */
11145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011146f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011147 typval_T *argvars;
11148 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011149{
11150 switch (argvars[0].v_type)
11151 {
11152 case VAR_STRING:
11153 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011154 rettv->vval.v_number = (varnumber_T)STRLEN(
11155 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011156 break;
11157 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011159 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011160 case VAR_DICT:
11161 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11162 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011163 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011164 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011165 break;
11166 }
11167}
11168
Bram Moolenaar33570922005-01-25 22:26:29 +000011169static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011170
11171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011172libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011173 typval_T *argvars;
11174 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011175 int type;
11176{
11177#ifdef FEAT_LIBCALL
11178 char_u *string_in;
11179 char_u **string_result;
11180 int nr_result;
11181#endif
11182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011183 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011184 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011185 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011186 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011188
11189 if (check_restricted() || check_secure())
11190 return;
11191
11192#ifdef FEAT_LIBCALL
11193 /* The first two args must be strings, otherwise its meaningless */
11194 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11195 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011196 string_in = NULL;
11197 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011198 string_in = argvars[2].vval.v_string;
11199 if (type == VAR_NUMBER)
11200 string_result = NULL;
11201 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011202 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011203 if (mch_libcall(argvars[0].vval.v_string,
11204 argvars[1].vval.v_string,
11205 string_in,
11206 argvars[2].vval.v_number,
11207 string_result,
11208 &nr_result) == OK
11209 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011211 }
11212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213}
11214
11215/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011216 * "libcall()" function
11217 */
11218 static void
11219f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011220 typval_T *argvars;
11221 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011222{
11223 libcall_common(argvars, rettv, VAR_STRING);
11224}
11225
11226/*
11227 * "libcallnr()" function
11228 */
11229 static void
11230f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011231 typval_T *argvars;
11232 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011233{
11234 libcall_common(argvars, rettv, VAR_NUMBER);
11235}
11236
11237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 * "line(string)" function
11239 */
11240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011241f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011242 typval_T *argvars;
11243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244{
11245 linenr_T lnum = 0;
11246 pos_T *fp;
11247
11248 fp = var2fpos(&argvars[0], TRUE);
11249 if (fp != NULL)
11250 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011251 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252}
11253
11254/*
11255 * "line2byte(lnum)" function
11256 */
11257/*ARGSUSED*/
11258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011260 typval_T *argvars;
11261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262{
11263#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265#else
11266 linenr_T lnum;
11267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011270 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11273 if (rettv->vval.v_number >= 0)
11274 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275#endif
11276}
11277
11278/*
11279 * "lispindent(lnum)" function
11280 */
11281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011283 typval_T *argvars;
11284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285{
11286#ifdef FEAT_LISP
11287 pos_T pos;
11288 linenr_T lnum;
11289
11290 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011291 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11293 {
11294 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011295 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 curwin->w_cursor = pos;
11297 }
11298 else
11299#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011300 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301}
11302
11303/*
11304 * "localtime()" function
11305 */
11306/*ARGSUSED*/
11307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011309 typval_T *argvars;
11310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011312 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313}
11314
Bram Moolenaar33570922005-01-25 22:26:29 +000011315static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316
11317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011319 typval_T *argvars;
11320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 int exact;
11322{
11323 char_u *keys;
11324 char_u *which;
11325 char_u buf[NUMBUFLEN];
11326 char_u *keys_buf = NULL;
11327 char_u *rhs;
11328 int mode;
11329 garray_T ga;
11330
11331 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011332 rettv->v_type = VAR_STRING;
11333 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 if (*keys == NUL)
11337 return;
11338
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011339 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011340 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 else
11342 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011343 if (which == NULL)
11344 return;
11345
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 mode = get_map_mode(&which, 0);
11347
11348 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011349 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 vim_free(keys_buf);
11351 if (rhs != NULL)
11352 {
11353 ga_init(&ga);
11354 ga.ga_itemsize = 1;
11355 ga.ga_growsize = 40;
11356
11357 while (*rhs != NUL)
11358 ga_concat(&ga, str2special(&rhs, FALSE));
11359
11360 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362 }
11363}
11364
11365/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011366 * "map()" function
11367 */
11368 static void
11369f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011370 typval_T *argvars;
11371 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011372{
11373 filter_map(argvars, rettv, TRUE);
11374}
11375
11376/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011377 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 */
11379 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011380f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011381 typval_T *argvars;
11382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011384 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385}
11386
11387/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011388 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 */
11390 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011391f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011392 typval_T *argvars;
11393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011395 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396}
11397
Bram Moolenaar33570922005-01-25 22:26:29 +000011398static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399
11400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011402 typval_T *argvars;
11403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 int type;
11405{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011406 char_u *str = NULL;
11407 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 char_u *pat;
11409 regmatch_T regmatch;
11410 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011411 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 char_u *save_cpo;
11413 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011414 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011415 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011416 list_T *l = NULL;
11417 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011418 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011419 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420
11421 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11422 save_cpo = p_cpo;
11423 p_cpo = (char_u *)"";
11424
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011425 rettv->vval.v_number = -1;
11426 if (type == 3)
11427 {
11428 /* return empty list when there are no matches */
11429 if ((rettv->vval.v_list = list_alloc()) == NULL)
11430 goto theend;
11431 rettv->v_type = VAR_LIST;
11432 ++rettv->vval.v_list->lv_refcount;
11433 }
11434 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 rettv->v_type = VAR_STRING;
11437 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011440 if (argvars[0].v_type == VAR_LIST)
11441 {
11442 if ((l = argvars[0].vval.v_list) == NULL)
11443 goto theend;
11444 li = l->lv_first;
11445 }
11446 else
11447 expr = str = get_tv_string(&argvars[0]);
11448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011449 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11450 if (pat == NULL)
11451 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011452
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011453 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011455 int error = FALSE;
11456
11457 start = get_tv_number_chk(&argvars[2], &error);
11458 if (error)
11459 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011460 if (l != NULL)
11461 {
11462 li = list_find(l, start);
11463 if (li == NULL)
11464 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011465 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011466 }
11467 else
11468 {
11469 if (start < 0)
11470 start = 0;
11471 if (start > (long)STRLEN(str))
11472 goto theend;
11473 str += start;
11474 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011475
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011476 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011477 nth = get_tv_number_chk(&argvars[3], &error);
11478 if (error)
11479 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 }
11481
11482 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11483 if (regmatch.regprog != NULL)
11484 {
11485 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011486
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011487 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011488 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011489 if (l != NULL)
11490 {
11491 if (li == NULL)
11492 {
11493 match = FALSE;
11494 break;
11495 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011496 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011497 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011498 if (str == NULL)
11499 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011500 }
11501
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011502 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011503
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011504 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011505 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011506 if (l == NULL && !match)
11507 break;
11508
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011509 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011510 if (l != NULL)
11511 {
11512 li = li->li_next;
11513 ++idx;
11514 }
11515 else
11516 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011517#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011518 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011519#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011520 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011521#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011522 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011523 }
11524
11525 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011527 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011528 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011529 int i;
11530
11531 /* return list with matched string and submatches */
11532 for (i = 0; i < NSUBEXP; ++i)
11533 {
11534 if (regmatch.endp[i] == NULL)
11535 break;
11536 li = listitem_alloc();
11537 if (li == NULL)
11538 break;
11539 li->li_tv.v_type = VAR_STRING;
11540 li->li_tv.v_lock = 0;
11541 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11542 (int)(regmatch.endp[i] - regmatch.startp[i]));
11543 list_append(rettv->vval.v_list, li);
11544 }
11545 }
11546 else if (type == 2)
11547 {
11548 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011549 if (l != NULL)
11550 copy_tv(&li->li_tv, rettv);
11551 else
11552 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011554 }
11555 else if (l != NULL)
11556 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557 else
11558 {
11559 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 (varnumber_T)(regmatch.startp[0] - str);
11562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011565 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 }
11567 }
11568 vim_free(regmatch.regprog);
11569 }
11570
11571theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011572 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573 p_cpo = save_cpo;
11574}
11575
11576/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011577 * "match()" function
11578 */
11579 static void
11580f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011581 typval_T *argvars;
11582 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011583{
11584 find_some_match(argvars, rettv, 1);
11585}
11586
11587/*
11588 * "matchend()" function
11589 */
11590 static void
11591f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011592 typval_T *argvars;
11593 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011594{
11595 find_some_match(argvars, rettv, 0);
11596}
11597
11598/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011599 * "matchlist()" function
11600 */
11601 static void
11602f_matchlist(argvars, rettv)
11603 typval_T *argvars;
11604 typval_T *rettv;
11605{
11606 find_some_match(argvars, rettv, 3);
11607}
11608
11609/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011610 * "matchstr()" function
11611 */
11612 static void
11613f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011614 typval_T *argvars;
11615 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011616{
11617 find_some_match(argvars, rettv, 2);
11618}
11619
Bram Moolenaar33570922005-01-25 22:26:29 +000011620static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011621
11622 static void
11623max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011624 typval_T *argvars;
11625 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011626 int domax;
11627{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011628 long n = 0;
11629 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011630 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011631
11632 if (argvars[0].v_type == VAR_LIST)
11633 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011634 list_T *l;
11635 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011636
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011637 l = argvars[0].vval.v_list;
11638 if (l != NULL)
11639 {
11640 li = l->lv_first;
11641 if (li != NULL)
11642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011643 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011644 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011645 {
11646 li = li->li_next;
11647 if (li == NULL)
11648 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011649 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011650 if (domax ? i > n : i < n)
11651 n = i;
11652 }
11653 }
11654 }
11655 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011656 else if (argvars[0].v_type == VAR_DICT)
11657 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011658 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011659 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011660 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011661 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011662
11663 d = argvars[0].vval.v_dict;
11664 if (d != NULL)
11665 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011666 todo = d->dv_hashtab.ht_used;
11667 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011668 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011669 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011670 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011671 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011672 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011673 if (first)
11674 {
11675 n = i;
11676 first = FALSE;
11677 }
11678 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011679 n = i;
11680 }
11681 }
11682 }
11683 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011684 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011685 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011686 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011687}
11688
11689/*
11690 * "max()" function
11691 */
11692 static void
11693f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011694 typval_T *argvars;
11695 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011696{
11697 max_min(argvars, rettv, TRUE);
11698}
11699
11700/*
11701 * "min()" function
11702 */
11703 static void
11704f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011705 typval_T *argvars;
11706 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011707{
11708 max_min(argvars, rettv, FALSE);
11709}
11710
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011711static int mkdir_recurse __ARGS((char_u *dir, int prot));
11712
11713/*
11714 * Create the directory in which "dir" is located, and higher levels when
11715 * needed.
11716 */
11717 static int
11718mkdir_recurse(dir, prot)
11719 char_u *dir;
11720 int prot;
11721{
11722 char_u *p;
11723 char_u *updir;
11724 int r = FAIL;
11725
11726 /* Get end of directory name in "dir".
11727 * We're done when it's "/" or "c:/". */
11728 p = gettail_sep(dir);
11729 if (p <= get_past_head(dir))
11730 return OK;
11731
11732 /* If the directory exists we're done. Otherwise: create it.*/
11733 updir = vim_strnsave(dir, (int)(p - dir));
11734 if (updir == NULL)
11735 return FAIL;
11736 if (mch_isdir(updir))
11737 r = OK;
11738 else if (mkdir_recurse(updir, prot) == OK)
11739 r = vim_mkdir_emsg(updir, prot);
11740 vim_free(updir);
11741 return r;
11742}
11743
11744#ifdef vim_mkdir
11745/*
11746 * "mkdir()" function
11747 */
11748 static void
11749f_mkdir(argvars, rettv)
11750 typval_T *argvars;
11751 typval_T *rettv;
11752{
11753 char_u *dir;
11754 char_u buf[NUMBUFLEN];
11755 int prot = 0755;
11756
11757 rettv->vval.v_number = FAIL;
11758 if (check_restricted() || check_secure())
11759 return;
11760
11761 dir = get_tv_string_buf(&argvars[0], buf);
11762 if (argvars[1].v_type != VAR_UNKNOWN)
11763 {
11764 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011765 prot = get_tv_number_chk(&argvars[2], NULL);
11766 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011767 mkdir_recurse(dir, prot);
11768 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011769 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011770}
11771#endif
11772
Bram Moolenaar0d660222005-01-07 21:51:51 +000011773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 * "mode()" function
11775 */
11776/*ARGSUSED*/
11777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011778f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011779 typval_T *argvars;
11780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781{
11782 char_u buf[2];
11783
11784#ifdef FEAT_VISUAL
11785 if (VIsual_active)
11786 {
11787 if (VIsual_select)
11788 buf[0] = VIsual_mode + 's' - 'v';
11789 else
11790 buf[0] = VIsual_mode;
11791 }
11792 else
11793#endif
11794 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11795 buf[0] = 'r';
11796 else if (State & INSERT)
11797 {
11798 if (State & REPLACE_FLAG)
11799 buf[0] = 'R';
11800 else
11801 buf[0] = 'i';
11802 }
11803 else if (State & CMDLINE)
11804 buf[0] = 'c';
11805 else
11806 buf[0] = 'n';
11807
11808 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011809 rettv->vval.v_string = vim_strsave(buf);
11810 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811}
11812
11813/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011814 * "nextnonblank()" function
11815 */
11816 static void
11817f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011818 typval_T *argvars;
11819 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011820{
11821 linenr_T lnum;
11822
11823 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011825 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011826 {
11827 lnum = 0;
11828 break;
11829 }
11830 if (*skipwhite(ml_get(lnum)) != NUL)
11831 break;
11832 }
11833 rettv->vval.v_number = lnum;
11834}
11835
11836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 * "nr2char()" function
11838 */
11839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011840f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011841 typval_T *argvars;
11842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843{
11844 char_u buf[NUMBUFLEN];
11845
11846#ifdef FEAT_MBYTE
11847 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011848 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 else
11850#endif
11851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011852 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853 buf[1] = NUL;
11854 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855 rettv->v_type = VAR_STRING;
11856 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011857}
11858
11859/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011860 * "prevnonblank()" function
11861 */
11862 static void
11863f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011864 typval_T *argvars;
11865 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011866{
11867 linenr_T lnum;
11868
11869 lnum = get_tv_lnum(argvars);
11870 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11871 lnum = 0;
11872 else
11873 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11874 --lnum;
11875 rettv->vval.v_number = lnum;
11876}
11877
Bram Moolenaar8c711452005-01-14 21:53:12 +000011878/*
11879 * "range()" function
11880 */
11881 static void
11882f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011883 typval_T *argvars;
11884 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011885{
11886 long start;
11887 long end;
11888 long stride = 1;
11889 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011890 list_T *l;
11891 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011892 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011894 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011895 if (argvars[1].v_type == VAR_UNKNOWN)
11896 {
11897 end = start - 1;
11898 start = 0;
11899 }
11900 else
11901 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011902 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011903 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011904 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011905 }
11906
11907 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011908 if (error)
11909 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000011910 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011911 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000011912 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011913 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011914 else
11915 {
11916 l = list_alloc();
11917 if (l != NULL)
11918 {
11919 rettv->v_type = VAR_LIST;
11920 rettv->vval.v_list = l;
11921 ++l->lv_refcount;
11922
11923 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11924 {
11925 li = listitem_alloc();
11926 if (li == NULL)
11927 break;
11928 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011929 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011930 li->li_tv.vval.v_number = i;
11931 list_append(l, li);
11932 }
11933 }
11934 }
11935}
11936
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011937/*
11938 * "readfile()" function
11939 */
11940 static void
11941f_readfile(argvars, rettv)
11942 typval_T *argvars;
11943 typval_T *rettv;
11944{
11945 int binary = FALSE;
11946 char_u *fname;
11947 FILE *fd;
11948 list_T *l;
11949 listitem_T *li;
11950#define FREAD_SIZE 200 /* optimized for text lines */
11951 char_u buf[FREAD_SIZE];
11952 int readlen; /* size of last fread() */
11953 int buflen; /* nr of valid chars in buf[] */
11954 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11955 int tolist; /* first byte in buf[] still to be put in list */
11956 int chop; /* how many CR to chop off */
11957 char_u *prev = NULL; /* previously read bytes, if any */
11958 int prevlen = 0; /* length of "prev" if not NULL */
11959 char_u *s;
11960 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011961 long maxline = MAXLNUM;
11962 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011963
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011964 if (argvars[1].v_type != VAR_UNKNOWN)
11965 {
11966 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11967 binary = TRUE;
11968 if (argvars[2].v_type != VAR_UNKNOWN)
11969 maxline = get_tv_number(&argvars[2]);
11970 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011971
11972 l = list_alloc();
11973 if (l == NULL)
11974 return;
11975 rettv->v_type = VAR_LIST;
11976 rettv->vval.v_list = l;
11977 l->lv_refcount = 1;
11978
11979 /* Always open the file in binary mode, library functions have a mind of
11980 * their own about CR-LF conversion. */
11981 fname = get_tv_string(&argvars[0]);
11982 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11983 {
11984 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11985 return;
11986 }
11987
11988 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011989 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011990 {
11991 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11992 buflen = filtd + readlen;
11993 tolist = 0;
11994 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11995 {
11996 if (buf[filtd] == '\n' || readlen <= 0)
11997 {
11998 /* Only when in binary mode add an empty list item when the
11999 * last line ends in a '\n'. */
12000 if (!binary && readlen == 0 && filtd == 0)
12001 break;
12002
12003 /* Found end-of-line or end-of-file: add a text line to the
12004 * list. */
12005 chop = 0;
12006 if (!binary)
12007 while (filtd - chop - 1 >= tolist
12008 && buf[filtd - chop - 1] == '\r')
12009 ++chop;
12010 len = filtd - tolist - chop;
12011 if (prev == NULL)
12012 s = vim_strnsave(buf + tolist, len);
12013 else
12014 {
12015 s = alloc((unsigned)(prevlen + len + 1));
12016 if (s != NULL)
12017 {
12018 mch_memmove(s, prev, prevlen);
12019 vim_free(prev);
12020 prev = NULL;
12021 mch_memmove(s + prevlen, buf + tolist, len);
12022 s[prevlen + len] = NUL;
12023 }
12024 }
12025 tolist = filtd + 1;
12026
12027 li = listitem_alloc();
12028 if (li == NULL)
12029 {
12030 vim_free(s);
12031 break;
12032 }
12033 li->li_tv.v_type = VAR_STRING;
12034 li->li_tv.v_lock = 0;
12035 li->li_tv.vval.v_string = s;
12036 list_append(l, li);
12037
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012038 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012039 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012040 if (readlen <= 0)
12041 break;
12042 }
12043 else if (buf[filtd] == NUL)
12044 buf[filtd] = '\n';
12045 }
12046 if (readlen <= 0)
12047 break;
12048
12049 if (tolist == 0)
12050 {
12051 /* "buf" is full, need to move text to an allocated buffer */
12052 if (prev == NULL)
12053 {
12054 prev = vim_strnsave(buf, buflen);
12055 prevlen = buflen;
12056 }
12057 else
12058 {
12059 s = alloc((unsigned)(prevlen + buflen));
12060 if (s != NULL)
12061 {
12062 mch_memmove(s, prev, prevlen);
12063 mch_memmove(s + prevlen, buf, buflen);
12064 vim_free(prev);
12065 prev = s;
12066 prevlen += buflen;
12067 }
12068 }
12069 filtd = 0;
12070 }
12071 else
12072 {
12073 mch_memmove(buf, buf + tolist, buflen - tolist);
12074 filtd -= tolist;
12075 }
12076 }
12077
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012078 /*
12079 * For a negative line count use only the lines at the end of the file,
12080 * free the rest.
12081 */
12082 if (maxline < 0)
12083 while (cnt > -maxline)
12084 {
12085 listitem_remove(l, l->lv_first);
12086 --cnt;
12087 }
12088
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012089 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012090 fclose(fd);
12091}
12092
12093
Bram Moolenaar0d660222005-01-07 21:51:51 +000012094#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12095static void make_connection __ARGS((void));
12096static int check_connection __ARGS((void));
12097
12098 static void
12099make_connection()
12100{
12101 if (X_DISPLAY == NULL
12102# ifdef FEAT_GUI
12103 && !gui.in_use
12104# endif
12105 )
12106 {
12107 x_force_connect = TRUE;
12108 setup_term_clip();
12109 x_force_connect = FALSE;
12110 }
12111}
12112
12113 static int
12114check_connection()
12115{
12116 make_connection();
12117 if (X_DISPLAY == NULL)
12118 {
12119 EMSG(_("E240: No connection to Vim server"));
12120 return FAIL;
12121 }
12122 return OK;
12123}
12124#endif
12125
12126#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012127static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012128
12129 static void
12130remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012131 typval_T *argvars;
12132 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012133 int expr;
12134{
12135 char_u *server_name;
12136 char_u *keys;
12137 char_u *r = NULL;
12138 char_u buf[NUMBUFLEN];
12139# ifdef WIN32
12140 HWND w;
12141# else
12142 Window w;
12143# endif
12144
12145 if (check_restricted() || check_secure())
12146 return;
12147
12148# ifdef FEAT_X11
12149 if (check_connection() == FAIL)
12150 return;
12151# endif
12152
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012153 server_name = get_tv_string_chk(&argvars[0]);
12154 if (server_name == NULL)
12155 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012156 keys = get_tv_string_buf(&argvars[1], buf);
12157# ifdef WIN32
12158 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12159# else
12160 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12161 < 0)
12162# endif
12163 {
12164 if (r != NULL)
12165 EMSG(r); /* sending worked but evaluation failed */
12166 else
12167 EMSG2(_("E241: Unable to send to %s"), server_name);
12168 return;
12169 }
12170
12171 rettv->vval.v_string = r;
12172
12173 if (argvars[2].v_type != VAR_UNKNOWN)
12174 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012175 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012176 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012177 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012178
12179 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012180 v.di_tv.v_type = VAR_STRING;
12181 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012182 idvar = get_tv_string_chk(&argvars[2]);
12183 if (idvar != NULL)
12184 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012185 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012186 }
12187}
12188#endif
12189
12190/*
12191 * "remote_expr()" function
12192 */
12193/*ARGSUSED*/
12194 static void
12195f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012196 typval_T *argvars;
12197 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012198{
12199 rettv->v_type = VAR_STRING;
12200 rettv->vval.v_string = NULL;
12201#ifdef FEAT_CLIENTSERVER
12202 remote_common(argvars, rettv, TRUE);
12203#endif
12204}
12205
12206/*
12207 * "remote_foreground()" function
12208 */
12209/*ARGSUSED*/
12210 static void
12211f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012212 typval_T *argvars;
12213 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012214{
12215 rettv->vval.v_number = 0;
12216#ifdef FEAT_CLIENTSERVER
12217# ifdef WIN32
12218 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012219 {
12220 char_u *server_name = get_tv_string_chk(&argvars[0]);
12221
12222 if (server_name != NULL)
12223 serverForeground(server_name);
12224 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012225# else
12226 /* Send a foreground() expression to the server. */
12227 argvars[1].v_type = VAR_STRING;
12228 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12229 argvars[2].v_type = VAR_UNKNOWN;
12230 remote_common(argvars, rettv, TRUE);
12231 vim_free(argvars[1].vval.v_string);
12232# endif
12233#endif
12234}
12235
12236/*ARGSUSED*/
12237 static void
12238f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012239 typval_T *argvars;
12240 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012241{
12242#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012243 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012244 char_u *s = NULL;
12245# ifdef WIN32
12246 int n = 0;
12247# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012248 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012249
12250 if (check_restricted() || check_secure())
12251 {
12252 rettv->vval.v_number = -1;
12253 return;
12254 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012255 serverid = get_tv_string_chk(&argvars[0]);
12256 if (serverid == NULL)
12257 {
12258 rettv->vval.v_number = -1;
12259 return; /* type error; errmsg already given */
12260 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012261# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012262 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012263 if (n == 0)
12264 rettv->vval.v_number = -1;
12265 else
12266 {
12267 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12268 rettv->vval.v_number = (s != NULL);
12269 }
12270# else
12271 rettv->vval.v_number = 0;
12272 if (check_connection() == FAIL)
12273 return;
12274
12275 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012276 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012277# endif
12278
12279 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012281 char_u *retvar;
12282
Bram Moolenaar33570922005-01-25 22:26:29 +000012283 v.di_tv.v_type = VAR_STRING;
12284 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012285 retvar = get_tv_string_chk(&argvars[1]);
12286 if (retvar != NULL)
12287 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012288 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012289 }
12290#else
12291 rettv->vval.v_number = -1;
12292#endif
12293}
12294
12295/*ARGSUSED*/
12296 static void
12297f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012298 typval_T *argvars;
12299 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012300{
12301 char_u *r = NULL;
12302
12303#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012304 char_u *serverid = get_tv_string_chk(&argvars[0]);
12305
12306 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012307 {
12308# ifdef WIN32
12309 /* The server's HWND is encoded in the 'id' parameter */
12310 int n = 0;
12311
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012312 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012313 if (n != 0)
12314 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12315 if (r == NULL)
12316# else
12317 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012318 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012319# endif
12320 EMSG(_("E277: Unable to read a server reply"));
12321 }
12322#endif
12323 rettv->v_type = VAR_STRING;
12324 rettv->vval.v_string = r;
12325}
12326
12327/*
12328 * "remote_send()" function
12329 */
12330/*ARGSUSED*/
12331 static void
12332f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012333 typval_T *argvars;
12334 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012335{
12336 rettv->v_type = VAR_STRING;
12337 rettv->vval.v_string = NULL;
12338#ifdef FEAT_CLIENTSERVER
12339 remote_common(argvars, rettv, FALSE);
12340#endif
12341}
12342
12343/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012344 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012345 */
12346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012347f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012348 typval_T *argvars;
12349 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012350{
Bram Moolenaar33570922005-01-25 22:26:29 +000012351 list_T *l;
12352 listitem_T *item, *item2;
12353 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012354 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012355 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012356 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012357 dict_T *d;
12358 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012359
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012360 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012361 if (argvars[0].v_type == VAR_DICT)
12362 {
12363 if (argvars[2].v_type != VAR_UNKNOWN)
12364 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012365 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012366 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012368 key = get_tv_string_chk(&argvars[1]);
12369 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012371 di = dict_find(d, key, -1);
12372 if (di == NULL)
12373 EMSG2(_(e_dictkey), key);
12374 else
12375 {
12376 *rettv = di->di_tv;
12377 init_tv(&di->di_tv);
12378 dictitem_remove(d, di);
12379 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012380 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012381 }
12382 }
12383 else if (argvars[0].v_type != VAR_LIST)
12384 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012385 else if ((l = argvars[0].vval.v_list) != NULL
12386 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012388 int error = FALSE;
12389
12390 idx = get_tv_number_chk(&argvars[1], &error);
12391 if (error)
12392 ; /* type error: do nothing, errmsg already given */
12393 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012394 EMSGN(_(e_listidx), idx);
12395 else
12396 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012397 if (argvars[2].v_type == VAR_UNKNOWN)
12398 {
12399 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012400 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012401 *rettv = item->li_tv;
12402 vim_free(item);
12403 }
12404 else
12405 {
12406 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012407 end = get_tv_number_chk(&argvars[2], &error);
12408 if (error)
12409 ; /* type error: do nothing */
12410 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012411 EMSGN(_(e_listidx), end);
12412 else
12413 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012414 int cnt = 0;
12415
12416 for (li = item; li != NULL; li = li->li_next)
12417 {
12418 ++cnt;
12419 if (li == item2)
12420 break;
12421 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012422 if (li == NULL) /* didn't find "item2" after "item" */
12423 EMSG(_(e_invrange));
12424 else
12425 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012426 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012427 l = list_alloc();
12428 if (l != NULL)
12429 {
12430 rettv->v_type = VAR_LIST;
12431 rettv->vval.v_list = l;
12432 l->lv_first = item;
12433 l->lv_last = item2;
12434 l->lv_refcount = 1;
12435 item->li_prev = NULL;
12436 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012437 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012438 }
12439 }
12440 }
12441 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012442 }
12443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444}
12445
12446/*
12447 * "rename({from}, {to})" function
12448 */
12449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012450f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012451 typval_T *argvars;
12452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453{
12454 char_u buf[NUMBUFLEN];
12455
12456 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012457 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012459 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12460 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461}
12462
12463/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012464 * "repeat()" function
12465 */
12466/*ARGSUSED*/
12467 static void
12468f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012469 typval_T *argvars;
12470 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012471{
12472 char_u *p;
12473 int n;
12474 int slen;
12475 int len;
12476 char_u *r;
12477 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012478 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012479
12480 n = get_tv_number(&argvars[1]);
12481 if (argvars[0].v_type == VAR_LIST)
12482 {
12483 l = list_alloc();
12484 if (l != NULL && argvars[0].vval.v_list != NULL)
12485 {
12486 l->lv_refcount = 1;
12487 while (n-- > 0)
12488 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12489 break;
12490 }
12491 rettv->v_type = VAR_LIST;
12492 rettv->vval.v_list = l;
12493 }
12494 else
12495 {
12496 p = get_tv_string(&argvars[0]);
12497 rettv->v_type = VAR_STRING;
12498 rettv->vval.v_string = NULL;
12499
12500 slen = (int)STRLEN(p);
12501 len = slen * n;
12502 if (len <= 0)
12503 return;
12504
12505 r = alloc(len + 1);
12506 if (r != NULL)
12507 {
12508 for (i = 0; i < n; i++)
12509 mch_memmove(r + i * slen, p, (size_t)slen);
12510 r[len] = NUL;
12511 }
12512
12513 rettv->vval.v_string = r;
12514 }
12515}
12516
12517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518 * "resolve()" function
12519 */
12520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012521f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012522 typval_T *argvars;
12523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524{
12525 char_u *p;
12526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012527 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528#ifdef FEAT_SHORTCUT
12529 {
12530 char_u *v = NULL;
12531
12532 v = mch_resolve_shortcut(p);
12533 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012534 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012536 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537 }
12538#else
12539# ifdef HAVE_READLINK
12540 {
12541 char_u buf[MAXPATHL + 1];
12542 char_u *cpy;
12543 int len;
12544 char_u *remain = NULL;
12545 char_u *q;
12546 int is_relative_to_current = FALSE;
12547 int has_trailing_pathsep = FALSE;
12548 int limit = 100;
12549
12550 p = vim_strsave(p);
12551
12552 if (p[0] == '.' && (vim_ispathsep(p[1])
12553 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12554 is_relative_to_current = TRUE;
12555
12556 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012557 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558 has_trailing_pathsep = TRUE;
12559
12560 q = getnextcomp(p);
12561 if (*q != NUL)
12562 {
12563 /* Separate the first path component in "p", and keep the
12564 * remainder (beginning with the path separator). */
12565 remain = vim_strsave(q - 1);
12566 q[-1] = NUL;
12567 }
12568
12569 for (;;)
12570 {
12571 for (;;)
12572 {
12573 len = readlink((char *)p, (char *)buf, MAXPATHL);
12574 if (len <= 0)
12575 break;
12576 buf[len] = NUL;
12577
12578 if (limit-- == 0)
12579 {
12580 vim_free(p);
12581 vim_free(remain);
12582 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012583 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584 goto fail;
12585 }
12586
12587 /* Ensure that the result will have a trailing path separator
12588 * if the argument has one. */
12589 if (remain == NULL && has_trailing_pathsep)
12590 add_pathsep(buf);
12591
12592 /* Separate the first path component in the link value and
12593 * concatenate the remainders. */
12594 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12595 if (*q != NUL)
12596 {
12597 if (remain == NULL)
12598 remain = vim_strsave(q - 1);
12599 else
12600 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012601 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602 if (cpy != NULL)
12603 {
12604 STRCAT(cpy, remain);
12605 vim_free(remain);
12606 remain = cpy;
12607 }
12608 }
12609 q[-1] = NUL;
12610 }
12611
12612 q = gettail(p);
12613 if (q > p && *q == NUL)
12614 {
12615 /* Ignore trailing path separator. */
12616 q[-1] = NUL;
12617 q = gettail(p);
12618 }
12619 if (q > p && !mch_isFullName(buf))
12620 {
12621 /* symlink is relative to directory of argument */
12622 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12623 if (cpy != NULL)
12624 {
12625 STRCPY(cpy, p);
12626 STRCPY(gettail(cpy), buf);
12627 vim_free(p);
12628 p = cpy;
12629 }
12630 }
12631 else
12632 {
12633 vim_free(p);
12634 p = vim_strsave(buf);
12635 }
12636 }
12637
12638 if (remain == NULL)
12639 break;
12640
12641 /* Append the first path component of "remain" to "p". */
12642 q = getnextcomp(remain + 1);
12643 len = q - remain - (*q != NUL);
12644 cpy = vim_strnsave(p, STRLEN(p) + len);
12645 if (cpy != NULL)
12646 {
12647 STRNCAT(cpy, remain, len);
12648 vim_free(p);
12649 p = cpy;
12650 }
12651 /* Shorten "remain". */
12652 if (*q != NUL)
12653 STRCPY(remain, q - 1);
12654 else
12655 {
12656 vim_free(remain);
12657 remain = NULL;
12658 }
12659 }
12660
12661 /* If the result is a relative path name, make it explicitly relative to
12662 * the current directory if and only if the argument had this form. */
12663 if (!vim_ispathsep(*p))
12664 {
12665 if (is_relative_to_current
12666 && *p != NUL
12667 && !(p[0] == '.'
12668 && (p[1] == NUL
12669 || vim_ispathsep(p[1])
12670 || (p[1] == '.'
12671 && (p[2] == NUL
12672 || vim_ispathsep(p[2]))))))
12673 {
12674 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012675 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676 if (cpy != NULL)
12677 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678 vim_free(p);
12679 p = cpy;
12680 }
12681 }
12682 else if (!is_relative_to_current)
12683 {
12684 /* Strip leading "./". */
12685 q = p;
12686 while (q[0] == '.' && vim_ispathsep(q[1]))
12687 q += 2;
12688 if (q > p)
12689 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12690 }
12691 }
12692
12693 /* Ensure that the result will have no trailing path separator
12694 * if the argument had none. But keep "/" or "//". */
12695 if (!has_trailing_pathsep)
12696 {
12697 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012698 if (after_pathsep(p, q))
12699 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700 }
12701
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012702 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703 }
12704# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012705 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706# endif
12707#endif
12708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012709 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710
12711#ifdef HAVE_READLINK
12712fail:
12713#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715}
12716
12717/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012718 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 */
12720 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012721f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012722 typval_T *argvars;
12723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724{
Bram Moolenaar33570922005-01-25 22:26:29 +000012725 list_T *l;
12726 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727
Bram Moolenaar0d660222005-01-07 21:51:51 +000012728 rettv->vval.v_number = 0;
12729 if (argvars[0].v_type != VAR_LIST)
12730 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012731 else if ((l = argvars[0].vval.v_list) != NULL
12732 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012733 {
12734 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012735 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012736 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012737 while (li != NULL)
12738 {
12739 ni = li->li_prev;
12740 list_append(l, li);
12741 li = ni;
12742 }
12743 rettv->vval.v_list = l;
12744 rettv->v_type = VAR_LIST;
12745 ++l->lv_refcount;
12746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747}
12748
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012749#define SP_NOMOVE 1 /* don't move cursor */
12750#define SP_REPEAT 2 /* repeat to find outer pair */
12751#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000012752#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012753
Bram Moolenaar33570922005-01-25 22:26:29 +000012754static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012755
12756/*
12757 * Get flags for a search function.
12758 * Possibly sets "p_ws".
12759 * Returns BACKWARD, FORWARD or zero (for an error).
12760 */
12761 static int
12762get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012763 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012764 int *flagsp;
12765{
12766 int dir = FORWARD;
12767 char_u *flags;
12768 char_u nbuf[NUMBUFLEN];
12769 int mask;
12770
12771 if (varp->v_type != VAR_UNKNOWN)
12772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012773 flags = get_tv_string_buf_chk(varp, nbuf);
12774 if (flags == NULL)
12775 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012776 while (*flags != NUL)
12777 {
12778 switch (*flags)
12779 {
12780 case 'b': dir = BACKWARD; break;
12781 case 'w': p_ws = TRUE; break;
12782 case 'W': p_ws = FALSE; break;
12783 default: mask = 0;
12784 if (flagsp != NULL)
12785 switch (*flags)
12786 {
12787 case 'n': mask = SP_NOMOVE; break;
12788 case 'r': mask = SP_REPEAT; break;
12789 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012790 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012791 }
12792 if (mask == 0)
12793 {
12794 EMSG2(_(e_invarg2), flags);
12795 dir = 0;
12796 }
12797 else
12798 *flagsp |= mask;
12799 }
12800 if (dir == 0)
12801 break;
12802 ++flags;
12803 }
12804 }
12805 return dir;
12806}
12807
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808/*
12809 * "search()" function
12810 */
12811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012812f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012813 typval_T *argvars;
12814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815{
12816 char_u *pat;
12817 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012818 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 int save_p_ws = p_ws;
12820 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012821 int flags = 0;
12822
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012825 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012826 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12827 if (dir == 0)
12828 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012829 /*
12830 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
12831 * Check to make sure only those flags are set.
12832 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
12833 * flags cannot be set. Check for that condition also.
12834 */
12835 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
12836 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012837 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012838 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012839 goto theend;
12840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012842 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12844 SEARCH_KEEP, RE_SEARCH) != FAIL)
12845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012846 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012847 if (flags & SP_SETPCMARK)
12848 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849 curwin->w_cursor = pos;
12850 /* "/$" will put the cursor after the end of the line, may need to
12851 * correct that here */
12852 check_cursor();
12853 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012854
12855 /* If 'n' flag is used: restore cursor position. */
12856 if (flags & SP_NOMOVE)
12857 curwin->w_cursor = save_cursor;
12858theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859 p_ws = save_p_ws;
12860}
12861
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862/*
12863 * "searchpair()" function
12864 */
12865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012866f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012867 typval_T *argvars;
12868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869{
12870 char_u *spat, *mpat, *epat;
12871 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873 int dir;
12874 int flags = 0;
12875 char_u nbuf1[NUMBUFLEN];
12876 char_u nbuf2[NUMBUFLEN];
12877 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012882 spat = get_tv_string_chk(&argvars[0]);
12883 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
12884 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
12885 if (spat == NULL || mpat == NULL || epat == NULL)
12886 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888 /* Handle the optional fourth argument: flags */
12889 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012890 if (dir == 0)
12891 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012892 /*
12893 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
12894 */
12895 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
12896 {
12897 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
12898 goto theend;
12899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900
12901 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012902 if (argvars[3].v_type == VAR_UNKNOWN
12903 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904 skip = (char_u *)"";
12905 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012906 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
12907 if (skip == NULL)
12908 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012910 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
12911
12912theend:
12913 p_ws = save_p_ws;
12914}
12915
12916/*
12917 * Search for a start/middle/end thing.
12918 * Used by searchpair(), see its documentation for the details.
12919 * Returns 0 or -1 for no match,
12920 */
12921 long
12922do_searchpair(spat, mpat, epat, dir, skip, flags)
12923 char_u *spat; /* start pattern */
12924 char_u *mpat; /* middle pattern */
12925 char_u *epat; /* end pattern */
12926 int dir; /* BACKWARD or FORWARD */
12927 char_u *skip; /* skip expression */
12928 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
12929{
12930 char_u *save_cpo;
12931 char_u *pat, *pat2 = NULL, *pat3 = NULL;
12932 long retval = 0;
12933 pos_T pos;
12934 pos_T firstpos;
12935 pos_T foundpos;
12936 pos_T save_cursor;
12937 pos_T save_pos;
12938 int n;
12939 int r;
12940 int nest = 1;
12941 int err;
12942
12943 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12944 save_cpo = p_cpo;
12945 p_cpo = (char_u *)"";
12946
12947 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12948 * start/middle/end (pat3, for the top pair). */
12949 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12950 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12951 if (pat2 == NULL || pat3 == NULL)
12952 goto theend;
12953 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12954 if (*mpat == NUL)
12955 STRCPY(pat3, pat2);
12956 else
12957 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12958 spat, epat, mpat);
12959
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960 save_cursor = curwin->w_cursor;
12961 pos = curwin->w_cursor;
12962 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012963 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964 pat = pat3;
12965 for (;;)
12966 {
12967 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12968 SEARCH_KEEP, RE_SEARCH);
12969 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12970 /* didn't find it or found the first match again: FAIL */
12971 break;
12972
12973 if (firstpos.lnum == 0)
12974 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012975 if (equalpos(pos, foundpos))
12976 {
12977 /* Found the same position again. Can happen with a pattern that
12978 * has "\zs" at the end and searching backwards. Advance one
12979 * character and try again. */
12980 if (dir == BACKWARD)
12981 decl(&pos);
12982 else
12983 incl(&pos);
12984 }
12985 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986
12987 /* If the skip pattern matches, ignore this match. */
12988 if (*skip != NUL)
12989 {
12990 save_pos = curwin->w_cursor;
12991 curwin->w_cursor = pos;
12992 r = eval_to_bool(skip, &err, NULL, FALSE);
12993 curwin->w_cursor = save_pos;
12994 if (err)
12995 {
12996 /* Evaluating {skip} caused an error, break here. */
12997 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012998 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999 break;
13000 }
13001 if (r)
13002 continue;
13003 }
13004
13005 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13006 {
13007 /* Found end when searching backwards or start when searching
13008 * forward: nested pair. */
13009 ++nest;
13010 pat = pat2; /* nested, don't search for middle */
13011 }
13012 else
13013 {
13014 /* Found end when searching forward or start when searching
13015 * backward: end of (nested) pair; or found middle in outer pair. */
13016 if (--nest == 1)
13017 pat = pat3; /* outer level, search for middle */
13018 }
13019
13020 if (nest == 0)
13021 {
13022 /* Found the match: return matchcount or line number. */
13023 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013024 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013026 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013027 if (flags & SP_SETPCMARK)
13028 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 curwin->w_cursor = pos;
13030 if (!(flags & SP_REPEAT))
13031 break;
13032 nest = 1; /* search for next unmatched */
13033 }
13034 }
13035
13036 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013037 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038 curwin->w_cursor = save_cursor;
13039
13040theend:
13041 vim_free(pat2);
13042 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013044
13045 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046}
13047
Bram Moolenaar0d660222005-01-07 21:51:51 +000013048/*ARGSUSED*/
13049 static void
13050f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013051 typval_T *argvars;
13052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013054#ifdef FEAT_CLIENTSERVER
13055 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013056 char_u *server = get_tv_string_chk(&argvars[0]);
13057 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058
Bram Moolenaar0d660222005-01-07 21:51:51 +000013059 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013060 if (server == NULL || reply == NULL)
13061 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013062 if (check_restricted() || check_secure())
13063 return;
13064# ifdef FEAT_X11
13065 if (check_connection() == FAIL)
13066 return;
13067# endif
13068
13069 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013071 EMSG(_("E258: Unable to send to client"));
13072 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013074 rettv->vval.v_number = 0;
13075#else
13076 rettv->vval.v_number = -1;
13077#endif
13078}
13079
13080/*ARGSUSED*/
13081 static void
13082f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013083 typval_T *argvars;
13084 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013085{
13086 char_u *r = NULL;
13087
13088#ifdef FEAT_CLIENTSERVER
13089# ifdef WIN32
13090 r = serverGetVimNames();
13091# else
13092 make_connection();
13093 if (X_DISPLAY != NULL)
13094 r = serverGetVimNames(X_DISPLAY);
13095# endif
13096#endif
13097 rettv->v_type = VAR_STRING;
13098 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099}
13100
13101/*
13102 * "setbufvar()" function
13103 */
13104/*ARGSUSED*/
13105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013106f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013107 typval_T *argvars;
13108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109{
13110 buf_T *buf;
13111#ifdef FEAT_AUTOCMD
13112 aco_save_T aco;
13113#else
13114 buf_T *save_curbuf;
13115#endif
13116 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013117 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118 char_u nbuf[NUMBUFLEN];
13119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013120 rettv->vval.v_number = 0;
13121
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122 if (check_restricted() || check_secure())
13123 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013124 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13125 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013126 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127 varp = &argvars[2];
13128
13129 if (buf != NULL && varname != NULL && varp != NULL)
13130 {
13131 /* set curbuf to be our buf, temporarily */
13132#ifdef FEAT_AUTOCMD
13133 aucmd_prepbuf(&aco, buf);
13134#else
13135 save_curbuf = curbuf;
13136 curbuf = buf;
13137#endif
13138
13139 if (*varname == '&')
13140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013141 long numval;
13142 char_u *strval;
13143 int error = FALSE;
13144
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013146 numval = get_tv_number_chk(varp, &error);
13147 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013148 if (!error && strval != NULL)
13149 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150 }
13151 else
13152 {
13153 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13154 if (bufvarname != NULL)
13155 {
13156 STRCPY(bufvarname, "b:");
13157 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013158 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159 vim_free(bufvarname);
13160 }
13161 }
13162
13163 /* reset notion of buffer */
13164#ifdef FEAT_AUTOCMD
13165 aucmd_restbuf(&aco);
13166#else
13167 curbuf = save_curbuf;
13168#endif
13169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170}
13171
13172/*
13173 * "setcmdpos()" function
13174 */
13175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013176f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013177 typval_T *argvars;
13178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013180 int pos = (int)get_tv_number(&argvars[0]) - 1;
13181
13182 if (pos >= 0)
13183 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184}
13185
13186/*
13187 * "setline()" function
13188 */
13189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013190f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013191 typval_T *argvars;
13192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193{
13194 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013195 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013196 list_T *l = NULL;
13197 listitem_T *li = NULL;
13198 long added = 0;
13199 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013200
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013201 lnum = get_tv_lnum(&argvars[0]);
13202 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013204 l = argvars[1].vval.v_list;
13205 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013207 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013208 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013209
13210 rettv->vval.v_number = 0; /* OK */
13211 for (;;)
13212 {
13213 if (l != NULL)
13214 {
13215 /* list argument, get next string */
13216 if (li == NULL)
13217 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013218 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013219 li = li->li_next;
13220 }
13221
13222 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013223 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013224 break;
13225 if (lnum <= curbuf->b_ml.ml_line_count)
13226 {
13227 /* existing line, replace it */
13228 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13229 {
13230 changed_bytes(lnum, 0);
13231 check_cursor_col();
13232 rettv->vval.v_number = 0; /* OK */
13233 }
13234 }
13235 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13236 {
13237 /* lnum is one past the last line, append the line */
13238 ++added;
13239 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13240 rettv->vval.v_number = 0; /* OK */
13241 }
13242
13243 if (l == NULL) /* only one string argument */
13244 break;
13245 ++lnum;
13246 }
13247
13248 if (added > 0)
13249 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250}
13251
13252/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013253 * "setqflist()" function
13254 */
13255/*ARGSUSED*/
13256 static void
13257f_setqflist(argvars, rettv)
13258 typval_T *argvars;
13259 typval_T *rettv;
13260{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013261 char_u *act;
13262 int action = ' ';
13263
Bram Moolenaar2641f772005-03-25 21:58:17 +000013264 rettv->vval.v_number = -1;
13265
13266#ifdef FEAT_QUICKFIX
13267 if (argvars[0].v_type != VAR_LIST)
13268 EMSG(_(e_listreq));
13269 else
13270 {
13271 list_T *l = argvars[0].vval.v_list;
13272
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013273 if (argvars[1].v_type == VAR_STRING)
13274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013275 act = get_tv_string_chk(&argvars[1]);
13276 if (act == NULL)
13277 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013278 if (*act == 'a' || *act == 'r')
13279 action = *act;
13280 }
13281
13282 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013283 rettv->vval.v_number = 0;
13284 }
13285#endif
13286}
13287
13288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 * "setreg()" function
13290 */
13291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013292f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013293 typval_T *argvars;
13294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295{
13296 int regname;
13297 char_u *strregname;
13298 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013299 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300 int append;
13301 char_u yank_type;
13302 long block_len;
13303
13304 block_len = -1;
13305 yank_type = MAUTO;
13306 append = FALSE;
13307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013308 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013309 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013311 if (strregname == NULL)
13312 return; /* type error; errmsg already given */
13313 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314 if (regname == 0 || regname == '@')
13315 regname = '"';
13316 else if (regname == '=')
13317 return;
13318
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013319 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013321 stropt = get_tv_string_chk(&argvars[2]);
13322 if (stropt == NULL)
13323 return; /* type error */
13324 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325 switch (*stropt)
13326 {
13327 case 'a': case 'A': /* append */
13328 append = TRUE;
13329 break;
13330 case 'v': case 'c': /* character-wise selection */
13331 yank_type = MCHAR;
13332 break;
13333 case 'V': case 'l': /* line-wise selection */
13334 yank_type = MLINE;
13335 break;
13336#ifdef FEAT_VISUAL
13337 case 'b': case Ctrl_V: /* block-wise selection */
13338 yank_type = MBLOCK;
13339 if (VIM_ISDIGIT(stropt[1]))
13340 {
13341 ++stropt;
13342 block_len = getdigits(&stropt) - 1;
13343 --stropt;
13344 }
13345 break;
13346#endif
13347 }
13348 }
13349
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013350 strval = get_tv_string_chk(&argvars[1]);
13351 if (strval != NULL)
13352 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013354 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355}
13356
13357
13358/*
13359 * "setwinvar(expr)" function
13360 */
13361/*ARGSUSED*/
13362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013363f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013364 typval_T *argvars;
13365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366{
13367 win_T *win;
13368#ifdef FEAT_WINDOWS
13369 win_T *save_curwin;
13370#endif
13371 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013372 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373 char_u nbuf[NUMBUFLEN];
13374
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013375 rettv->vval.v_number = 0;
13376
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377 if (check_restricted() || check_secure())
13378 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013380 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381 varp = &argvars[2];
13382
13383 if (win != NULL && varname != NULL && varp != NULL)
13384 {
13385#ifdef FEAT_WINDOWS
13386 /* set curwin to be our win, temporarily */
13387 save_curwin = curwin;
13388 curwin = win;
13389 curbuf = curwin->w_buffer;
13390#endif
13391
13392 if (*varname == '&')
13393 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013394 long numval;
13395 char_u *strval;
13396 int error = FALSE;
13397
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013399 numval = get_tv_number_chk(varp, &error);
13400 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013401 if (!error && strval != NULL)
13402 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 }
13404 else
13405 {
13406 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13407 if (winvarname != NULL)
13408 {
13409 STRCPY(winvarname, "w:");
13410 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013411 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412 vim_free(winvarname);
13413 }
13414 }
13415
13416#ifdef FEAT_WINDOWS
13417 /* Restore current window, if it's still valid (autocomands can make
13418 * it invalid). */
13419 if (win_valid(save_curwin))
13420 {
13421 curwin = save_curwin;
13422 curbuf = curwin->w_buffer;
13423 }
13424#endif
13425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426}
13427
13428/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013429 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 */
13431 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013432f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013433 typval_T *argvars;
13434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013436 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437
Bram Moolenaar0d660222005-01-07 21:51:51 +000013438 p = get_tv_string(&argvars[0]);
13439 rettv->vval.v_string = vim_strsave(p);
13440 simplify_filename(rettv->vval.v_string); /* simplify in place */
13441 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442}
13443
Bram Moolenaar0d660222005-01-07 21:51:51 +000013444static int
13445#ifdef __BORLANDC__
13446 _RTLENTRYF
13447#endif
13448 item_compare __ARGS((const void *s1, const void *s2));
13449static int
13450#ifdef __BORLANDC__
13451 _RTLENTRYF
13452#endif
13453 item_compare2 __ARGS((const void *s1, const void *s2));
13454
13455static int item_compare_ic;
13456static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013457static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013458#define ITEM_COMPARE_FAIL 999
13459
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013461 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013463 static int
13464#ifdef __BORLANDC__
13465_RTLENTRYF
13466#endif
13467item_compare(s1, s2)
13468 const void *s1;
13469 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013471 char_u *p1, *p2;
13472 char_u *tofree1, *tofree2;
13473 int res;
13474 char_u numbuf1[NUMBUFLEN];
13475 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476
Bram Moolenaar33570922005-01-25 22:26:29 +000013477 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13478 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013479 if (item_compare_ic)
13480 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013482 res = STRCMP(p1, p2);
13483 vim_free(tofree1);
13484 vim_free(tofree2);
13485 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486}
13487
13488 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013489#ifdef __BORLANDC__
13490_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013492item_compare2(s1, s2)
13493 const void *s1;
13494 const void *s2;
13495{
13496 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013497 typval_T rettv;
13498 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013499 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013501 /* shortcut after failure in previous call; compare all items equal */
13502 if (item_compare_func_err)
13503 return 0;
13504
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013505 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13506 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013507 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13508 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013509
13510 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13511 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013512 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013513 clear_tv(&argv[0]);
13514 clear_tv(&argv[1]);
13515
13516 if (res == FAIL)
13517 res = ITEM_COMPARE_FAIL;
13518 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013519 /* return value has wrong type */
13520 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13521 if (item_compare_func_err)
13522 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013523 clear_tv(&rettv);
13524 return res;
13525}
13526
13527/*
13528 * "sort({list})" function
13529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013531f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013532 typval_T *argvars;
13533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534{
Bram Moolenaar33570922005-01-25 22:26:29 +000013535 list_T *l;
13536 listitem_T *li;
13537 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013538 long len;
13539 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540
Bram Moolenaar0d660222005-01-07 21:51:51 +000013541 rettv->vval.v_number = 0;
13542 if (argvars[0].v_type != VAR_LIST)
13543 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544 else
13545 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013546 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013547 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013548 return;
13549 rettv->vval.v_list = l;
13550 rettv->v_type = VAR_LIST;
13551 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552
Bram Moolenaar0d660222005-01-07 21:51:51 +000013553 len = list_len(l);
13554 if (len <= 1)
13555 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556
Bram Moolenaar0d660222005-01-07 21:51:51 +000013557 item_compare_ic = FALSE;
13558 item_compare_func = NULL;
13559 if (argvars[1].v_type != VAR_UNKNOWN)
13560 {
13561 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013562 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013563 else
13564 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013565 int error = FALSE;
13566
13567 i = get_tv_number_chk(&argvars[1], &error);
13568 if (error)
13569 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013570 if (i == 1)
13571 item_compare_ic = TRUE;
13572 else
13573 item_compare_func = get_tv_string(&argvars[1]);
13574 }
13575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576
Bram Moolenaar0d660222005-01-07 21:51:51 +000013577 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013578 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013579 if (ptrs == NULL)
13580 return;
13581 i = 0;
13582 for (li = l->lv_first; li != NULL; li = li->li_next)
13583 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013585 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013586 /* test the compare function */
13587 if (item_compare_func != NULL
13588 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13589 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013590 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013592 {
13593 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013594 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013595 item_compare_func == NULL ? item_compare : item_compare2);
13596
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013597 if (!item_compare_func_err)
13598 {
13599 /* Clear the List and append the items in the sorted order. */
13600 l->lv_first = l->lv_last = NULL;
13601 l->lv_len = 0;
13602 for (i = 0; i < len; ++i)
13603 list_append(l, ptrs[i]);
13604 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013605 }
13606
13607 vim_free(ptrs);
13608 }
13609}
13610
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013611/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013612 * "soundfold({word})" function
13613 */
13614 static void
13615f_soundfold(argvars, rettv)
13616 typval_T *argvars;
13617 typval_T *rettv;
13618{
13619 char_u *s;
13620
13621 rettv->v_type = VAR_STRING;
13622 s = get_tv_string(&argvars[0]);
13623#ifdef FEAT_SYN_HL
13624 rettv->vval.v_string = eval_soundfold(s);
13625#else
13626 rettv->vval.v_string = vim_strsave(s);
13627#endif
13628}
13629
13630/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013631 * "spellbadword()" function
13632 */
13633/* ARGSUSED */
13634 static void
13635f_spellbadword(argvars, rettv)
13636 typval_T *argvars;
13637 typval_T *rettv;
13638{
13639 int attr;
13640 char_u *ptr;
13641 int len;
13642
13643 rettv->vval.v_string = NULL;
13644 rettv->v_type = VAR_STRING;
13645
13646#ifdef FEAT_SYN_HL
13647 /* Find the start of the badly spelled word. */
13648 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
13649 return;
13650
13651 /* Get the length of the word and copy it. */
13652 ptr = ml_get_cursor();
Bram Moolenaar51ac12f2005-07-02 23:21:11 +000013653 len = spell_check(curwin, ptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013654 rettv->vval.v_string = vim_strnsave(ptr, len);
13655#endif
13656}
13657
13658/*
13659 * "spellsuggest()" function
13660 */
13661 static void
13662f_spellsuggest(argvars, rettv)
13663 typval_T *argvars;
13664 typval_T *rettv;
13665{
13666 char_u *str;
13667 int maxcount;
13668 garray_T ga;
13669 list_T *l;
13670 listitem_T *li;
13671 int i;
13672
13673 l = list_alloc();
13674 if (l == NULL)
13675 return;
13676 rettv->v_type = VAR_LIST;
13677 rettv->vval.v_list = l;
13678 ++l->lv_refcount;
13679
13680#ifdef FEAT_SYN_HL
13681 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13682 {
13683 str = get_tv_string(&argvars[0]);
13684 if (argvars[1].v_type != VAR_UNKNOWN)
13685 {
13686 maxcount = get_tv_number(&argvars[1]);
13687 if (maxcount <= 0)
13688 return;
13689 }
13690 else
13691 maxcount = 25;
13692
13693 spell_suggest_list(&ga, str, maxcount);
13694
13695 for (i = 0; i < ga.ga_len; ++i)
13696 {
13697 str = ((char_u **)ga.ga_data)[i];
13698
13699 li = listitem_alloc();
13700 if (li == NULL)
13701 vim_free(str);
13702 else
13703 {
13704 li->li_tv.v_type = VAR_STRING;
13705 li->li_tv.v_lock = 0;
13706 li->li_tv.vval.v_string = str;
13707 list_append(l, li);
13708 }
13709 }
13710 ga_clear(&ga);
13711 }
13712#endif
13713}
13714
Bram Moolenaar0d660222005-01-07 21:51:51 +000013715 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013716f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013717 typval_T *argvars;
13718 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013719{
13720 char_u *str;
13721 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013722 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013723 regmatch_T regmatch;
13724 char_u patbuf[NUMBUFLEN];
13725 char_u *save_cpo;
13726 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013727 listitem_T *ni;
13728 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013729 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013730 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013731 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013732
13733 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13734 save_cpo = p_cpo;
13735 p_cpo = (char_u *)"";
13736
13737 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013738 if (argvars[1].v_type != VAR_UNKNOWN)
13739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013740 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13741 if (pat == NULL)
13742 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013743 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013744 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013745 }
13746 if (pat == NULL || *pat == NUL)
13747 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013748
13749 l = list_alloc();
13750 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013752 rettv->v_type = VAR_LIST;
13753 rettv->vval.v_list = l;
13754 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013755 if (typeerr)
13756 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757
Bram Moolenaar0d660222005-01-07 21:51:51 +000013758 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13759 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013761 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013762 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013763 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013764 if (*str == NUL)
13765 match = FALSE; /* empty item at the end */
13766 else
13767 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013768 if (match)
13769 end = regmatch.startp[0];
13770 else
13771 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013772 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13773 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013774 {
13775 ni = listitem_alloc();
13776 if (ni == NULL)
13777 break;
13778 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013779 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013780 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13781 list_append(l, ni);
13782 }
13783 if (!match)
13784 break;
13785 /* Advance to just after the match. */
13786 if (regmatch.endp[0] > str)
13787 col = 0;
13788 else
13789 {
13790 /* Don't get stuck at the same match. */
13791#ifdef FEAT_MBYTE
13792 col = mb_ptr2len_check(regmatch.endp[0]);
13793#else
13794 col = 1;
13795#endif
13796 }
13797 str = regmatch.endp[0];
13798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799
Bram Moolenaar0d660222005-01-07 21:51:51 +000013800 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802
Bram Moolenaar0d660222005-01-07 21:51:51 +000013803 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804}
13805
13806#ifdef HAVE_STRFTIME
13807/*
13808 * "strftime({format}[, {time}])" function
13809 */
13810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013811f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013812 typval_T *argvars;
13813 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814{
13815 char_u result_buf[256];
13816 struct tm *curtime;
13817 time_t seconds;
13818 char_u *p;
13819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013820 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013822 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013823 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 seconds = time(NULL);
13825 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013826 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827 curtime = localtime(&seconds);
13828 /* MSVC returns NULL for an invalid value of seconds. */
13829 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013830 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 else
13832 {
13833# ifdef FEAT_MBYTE
13834 vimconv_T conv;
13835 char_u *enc;
13836
13837 conv.vc_type = CONV_NONE;
13838 enc = enc_locale();
13839 convert_setup(&conv, p_enc, enc);
13840 if (conv.vc_type != CONV_NONE)
13841 p = string_convert(&conv, p, NULL);
13842# endif
13843 if (p != NULL)
13844 (void)strftime((char *)result_buf, sizeof(result_buf),
13845 (char *)p, curtime);
13846 else
13847 result_buf[0] = NUL;
13848
13849# ifdef FEAT_MBYTE
13850 if (conv.vc_type != CONV_NONE)
13851 vim_free(p);
13852 convert_setup(&conv, enc, p_enc);
13853 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013854 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855 else
13856# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013857 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858
13859# ifdef FEAT_MBYTE
13860 /* Release conversion descriptors */
13861 convert_setup(&conv, NULL, NULL);
13862 vim_free(enc);
13863# endif
13864 }
13865}
13866#endif
13867
13868/*
13869 * "stridx()" function
13870 */
13871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013872f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013873 typval_T *argvars;
13874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875{
13876 char_u buf[NUMBUFLEN];
13877 char_u *needle;
13878 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000013879 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000013881 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013883 needle = get_tv_string_chk(&argvars[1]);
13884 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000013885 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013886 if (needle == NULL || haystack == NULL)
13887 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888
Bram Moolenaar33570922005-01-25 22:26:29 +000013889 if (argvars[2].v_type != VAR_UNKNOWN)
13890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013891 int error = FALSE;
13892
13893 start_idx = get_tv_number_chk(&argvars[2], &error);
13894 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000013895 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013896 if (start_idx >= 0)
13897 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000013898 }
13899
13900 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13901 if (pos != NULL)
13902 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903}
13904
13905/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013906 * "string()" function
13907 */
13908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013909f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013910 typval_T *argvars;
13911 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013912{
13913 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013914 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013915
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013916 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013917 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013918 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013919 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920}
13921
13922/*
13923 * "strlen()" function
13924 */
13925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013926f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013927 typval_T *argvars;
13928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013930 rettv->vval.v_number = (varnumber_T)(STRLEN(
13931 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932}
13933
13934/*
13935 * "strpart()" function
13936 */
13937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013938f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013939 typval_T *argvars;
13940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941{
13942 char_u *p;
13943 int n;
13944 int len;
13945 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013946 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013948 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949 slen = (int)STRLEN(p);
13950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013951 n = get_tv_number_chk(&argvars[1], &error);
13952 if (error)
13953 len = 0;
13954 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013955 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 else
13957 len = slen - n; /* default len: all bytes that are available. */
13958
13959 /*
13960 * Only return the overlap between the specified part and the actual
13961 * string.
13962 */
13963 if (n < 0)
13964 {
13965 len += n;
13966 n = 0;
13967 }
13968 else if (n > slen)
13969 n = slen;
13970 if (len < 0)
13971 len = 0;
13972 else if (n + len > slen)
13973 len = slen - n;
13974
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013975 rettv->v_type = VAR_STRING;
13976 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977}
13978
13979/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013980 * "strridx()" function
13981 */
13982 static void
13983f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013984 typval_T *argvars;
13985 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013986{
13987 char_u buf[NUMBUFLEN];
13988 char_u *needle;
13989 char_u *haystack;
13990 char_u *rest;
13991 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013992 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013994 needle = get_tv_string_chk(&argvars[1]);
13995 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013996 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013997
13998 rettv->vval.v_number = -1;
13999 if (needle == NULL || haystack == NULL)
14000 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014001 if (argvars[2].v_type != VAR_UNKNOWN)
14002 {
14003 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014004 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014005 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014006 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014007 }
14008 else
14009 end_idx = haystack_len;
14010
Bram Moolenaar0d660222005-01-07 21:51:51 +000014011 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014012 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014013 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014014 lastmatch = haystack + end_idx;
14015 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014016 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014017 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014018 for (rest = haystack; *rest != '\0'; ++rest)
14019 {
14020 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014021 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014022 break;
14023 lastmatch = rest;
14024 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014025 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014026
14027 if (lastmatch == NULL)
14028 rettv->vval.v_number = -1;
14029 else
14030 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14031}
14032
14033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014034 * "strtrans()" function
14035 */
14036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014037f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014038 typval_T *argvars;
14039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014041 rettv->v_type = VAR_STRING;
14042 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043}
14044
14045/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014046 * "submatch()" function
14047 */
14048 static void
14049f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014050 typval_T *argvars;
14051 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014052{
14053 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014054 rettv->vval.v_string =
14055 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014056}
14057
14058/*
14059 * "substitute()" function
14060 */
14061 static void
14062f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014063 typval_T *argvars;
14064 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014065{
14066 char_u patbuf[NUMBUFLEN];
14067 char_u subbuf[NUMBUFLEN];
14068 char_u flagsbuf[NUMBUFLEN];
14069
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014070 char_u *str = get_tv_string_chk(&argvars[0]);
14071 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14072 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14073 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14074
Bram Moolenaar0d660222005-01-07 21:51:51 +000014075 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014076 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14077 rettv->vval.v_string = NULL;
14078 else
14079 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014080}
14081
14082/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014083 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084 */
14085/*ARGSUSED*/
14086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014087f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014088 typval_T *argvars;
14089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090{
14091 int id = 0;
14092#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014093 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094 long col;
14095 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014096 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014098 lnum = get_tv_lnum(argvars); /* -1 on type error */
14099 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14100 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014102 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014103 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
14104 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105#endif
14106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014107 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108}
14109
14110/*
14111 * "synIDattr(id, what [, mode])" function
14112 */
14113/*ARGSUSED*/
14114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014115f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014116 typval_T *argvars;
14117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118{
14119 char_u *p = NULL;
14120#ifdef FEAT_SYN_HL
14121 int id;
14122 char_u *what;
14123 char_u *mode;
14124 char_u modebuf[NUMBUFLEN];
14125 int modec;
14126
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014127 id = get_tv_number(&argvars[0]);
14128 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014129 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014131 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132 modec = TOLOWER_ASC(mode[0]);
14133 if (modec != 't' && modec != 'c'
14134#ifdef FEAT_GUI
14135 && modec != 'g'
14136#endif
14137 )
14138 modec = 0; /* replace invalid with current */
14139 }
14140 else
14141 {
14142#ifdef FEAT_GUI
14143 if (gui.in_use)
14144 modec = 'g';
14145 else
14146#endif
14147 if (t_colors > 1)
14148 modec = 'c';
14149 else
14150 modec = 't';
14151 }
14152
14153
14154 switch (TOLOWER_ASC(what[0]))
14155 {
14156 case 'b':
14157 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14158 p = highlight_color(id, what, modec);
14159 else /* bold */
14160 p = highlight_has_attr(id, HL_BOLD, modec);
14161 break;
14162
14163 case 'f': /* fg[#] */
14164 p = highlight_color(id, what, modec);
14165 break;
14166
14167 case 'i':
14168 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14169 p = highlight_has_attr(id, HL_INVERSE, modec);
14170 else /* italic */
14171 p = highlight_has_attr(id, HL_ITALIC, modec);
14172 break;
14173
14174 case 'n': /* name */
14175 p = get_highlight_name(NULL, id - 1);
14176 break;
14177
14178 case 'r': /* reverse */
14179 p = highlight_has_attr(id, HL_INVERSE, modec);
14180 break;
14181
14182 case 's': /* standout */
14183 p = highlight_has_attr(id, HL_STANDOUT, modec);
14184 break;
14185
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014186 case 'u':
14187 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14188 /* underline */
14189 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14190 else
14191 /* undercurl */
14192 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193 break;
14194 }
14195
14196 if (p != NULL)
14197 p = vim_strsave(p);
14198#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014199 rettv->v_type = VAR_STRING;
14200 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201}
14202
14203/*
14204 * "synIDtrans(id)" function
14205 */
14206/*ARGSUSED*/
14207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014208f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014209 typval_T *argvars;
14210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211{
14212 int id;
14213
14214#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014215 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216
14217 if (id > 0)
14218 id = syn_get_final_id(id);
14219 else
14220#endif
14221 id = 0;
14222
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014223 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224}
14225
14226/*
14227 * "system()" function
14228 */
14229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014230f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014231 typval_T *argvars;
14232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014234 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014236 char_u *infile = NULL;
14237 char_u buf[NUMBUFLEN];
14238 int err = FALSE;
14239 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014241 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014242 {
14243 /*
14244 * Write the string to a temp file, to be used for input of the shell
14245 * command.
14246 */
14247 if ((infile = vim_tempname('i')) == NULL)
14248 {
14249 EMSG(_(e_notmp));
14250 return;
14251 }
14252
14253 fd = mch_fopen((char *)infile, WRITEBIN);
14254 if (fd == NULL)
14255 {
14256 EMSG2(_(e_notopen), infile);
14257 goto done;
14258 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014259 p = get_tv_string_buf_chk(&argvars[1], buf);
14260 if (p == NULL)
14261 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014262 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14263 err = TRUE;
14264 if (fclose(fd) != 0)
14265 err = TRUE;
14266 if (err)
14267 {
14268 EMSG(_("E677: Error writing temp file"));
14269 goto done;
14270 }
14271 }
14272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014273 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014274
Bram Moolenaar071d4272004-06-13 20:20:40 +000014275#ifdef USE_CR
14276 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014277 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 {
14279 char_u *s;
14280
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014281 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282 {
14283 if (*s == CAR)
14284 *s = NL;
14285 }
14286 }
14287#else
14288# ifdef USE_CRNL
14289 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014290 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 {
14292 char_u *s, *d;
14293
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014294 d = res;
14295 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296 {
14297 if (s[0] == CAR && s[1] == NL)
14298 ++s;
14299 *d++ = *s;
14300 }
14301 *d = NUL;
14302 }
14303# endif
14304#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014305
14306done:
14307 if (infile != NULL)
14308 {
14309 mch_remove(infile);
14310 vim_free(infile);
14311 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014312 rettv->v_type = VAR_STRING;
14313 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314}
14315
14316/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014317 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014318 */
14319 static void
14320f_taglist(argvars, rettv)
14321 typval_T *argvars;
14322 typval_T *rettv;
14323{
14324 char_u *tag_pattern;
14325 list_T *l;
14326
14327 tag_pattern = get_tv_string(&argvars[0]);
14328
14329 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014330 if (*tag_pattern == NUL)
14331 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014332
14333 l = list_alloc();
14334 if (l != NULL)
14335 {
14336 if (get_tags(l, tag_pattern) != FAIL)
14337 {
14338 rettv->vval.v_list = l;
14339 rettv->v_type = VAR_LIST;
14340 ++l->lv_refcount;
14341 }
14342 else
14343 list_free(l);
14344 }
14345}
14346
14347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 * "tempname()" function
14349 */
14350/*ARGSUSED*/
14351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014352f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014353 typval_T *argvars;
14354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355{
14356 static int x = 'A';
14357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014358 rettv->v_type = VAR_STRING;
14359 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360
14361 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14362 * names. Skip 'I' and 'O', they are used for shell redirection. */
14363 do
14364 {
14365 if (x == 'Z')
14366 x = '0';
14367 else if (x == '9')
14368 x = 'A';
14369 else
14370 {
14371#ifdef EBCDIC
14372 if (x == 'I')
14373 x = 'J';
14374 else if (x == 'R')
14375 x = 'S';
14376 else
14377#endif
14378 ++x;
14379 }
14380 } while (x == 'I' || x == 'O');
14381}
14382
14383/*
14384 * "tolower(string)" function
14385 */
14386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014387f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014388 typval_T *argvars;
14389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390{
14391 char_u *p;
14392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014393 p = vim_strsave(get_tv_string(&argvars[0]));
14394 rettv->v_type = VAR_STRING;
14395 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396
14397 if (p != NULL)
14398 while (*p != NUL)
14399 {
14400#ifdef FEAT_MBYTE
14401 int l;
14402
14403 if (enc_utf8)
14404 {
14405 int c, lc;
14406
14407 c = utf_ptr2char(p);
14408 lc = utf_tolower(c);
14409 l = utf_ptr2len_check(p);
14410 /* TODO: reallocate string when byte count changes. */
14411 if (utf_char2len(lc) == l)
14412 utf_char2bytes(lc, p);
14413 p += l;
14414 }
14415 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
14416 p += l; /* skip multi-byte character */
14417 else
14418#endif
14419 {
14420 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14421 ++p;
14422 }
14423 }
14424}
14425
14426/*
14427 * "toupper(string)" function
14428 */
14429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014430f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014431 typval_T *argvars;
14432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014434 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014435 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436}
14437
14438/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014439 * "tr(string, fromstr, tostr)" function
14440 */
14441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014442f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014443 typval_T *argvars;
14444 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014445{
14446 char_u *instr;
14447 char_u *fromstr;
14448 char_u *tostr;
14449 char_u *p;
14450#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014451 int inlen;
14452 int fromlen;
14453 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014454 int idx;
14455 char_u *cpstr;
14456 int cplen;
14457 int first = TRUE;
14458#endif
14459 char_u buf[NUMBUFLEN];
14460 char_u buf2[NUMBUFLEN];
14461 garray_T ga;
14462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014463 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014464 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14465 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014466
14467 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014468 rettv->v_type = VAR_STRING;
14469 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014470 if (fromstr == NULL || tostr == NULL)
14471 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014472 ga_init2(&ga, (int)sizeof(char), 80);
14473
14474#ifdef FEAT_MBYTE
14475 if (!has_mbyte)
14476#endif
14477 /* not multi-byte: fromstr and tostr must be the same length */
14478 if (STRLEN(fromstr) != STRLEN(tostr))
14479 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014480#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014481error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014482#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014483 EMSG2(_(e_invarg2), fromstr);
14484 ga_clear(&ga);
14485 return;
14486 }
14487
14488 /* fromstr and tostr have to contain the same number of chars */
14489 while (*instr != NUL)
14490 {
14491#ifdef FEAT_MBYTE
14492 if (has_mbyte)
14493 {
14494 inlen = mb_ptr2len_check(instr);
14495 cpstr = instr;
14496 cplen = inlen;
14497 idx = 0;
14498 for (p = fromstr; *p != NUL; p += fromlen)
14499 {
14500 fromlen = mb_ptr2len_check(p);
14501 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14502 {
14503 for (p = tostr; *p != NUL; p += tolen)
14504 {
14505 tolen = mb_ptr2len_check(p);
14506 if (idx-- == 0)
14507 {
14508 cplen = tolen;
14509 cpstr = p;
14510 break;
14511 }
14512 }
14513 if (*p == NUL) /* tostr is shorter than fromstr */
14514 goto error;
14515 break;
14516 }
14517 ++idx;
14518 }
14519
14520 if (first && cpstr == instr)
14521 {
14522 /* Check that fromstr and tostr have the same number of
14523 * (multi-byte) characters. Done only once when a character
14524 * of instr doesn't appear in fromstr. */
14525 first = FALSE;
14526 for (p = tostr; *p != NUL; p += tolen)
14527 {
14528 tolen = mb_ptr2len_check(p);
14529 --idx;
14530 }
14531 if (idx != 0)
14532 goto error;
14533 }
14534
14535 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014536 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014537 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014538
14539 instr += inlen;
14540 }
14541 else
14542#endif
14543 {
14544 /* When not using multi-byte chars we can do it faster. */
14545 p = vim_strchr(fromstr, *instr);
14546 if (p != NULL)
14547 ga_append(&ga, tostr[p - fromstr]);
14548 else
14549 ga_append(&ga, *instr);
14550 ++instr;
14551 }
14552 }
14553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014554 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014555}
14556
14557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558 * "type(expr)" function
14559 */
14560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014561f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014562 typval_T *argvars;
14563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014565 int n;
14566
14567 switch (argvars[0].v_type)
14568 {
14569 case VAR_NUMBER: n = 0; break;
14570 case VAR_STRING: n = 1; break;
14571 case VAR_FUNC: n = 2; break;
14572 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014573 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014574 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14575 }
14576 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577}
14578
14579/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014580 * "values(dict)" function
14581 */
14582 static void
14583f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014584 typval_T *argvars;
14585 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014586{
14587 dict_list(argvars, rettv, 1);
14588}
14589
14590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 * "virtcol(string)" function
14592 */
14593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014594f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014595 typval_T *argvars;
14596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597{
14598 colnr_T vcol = 0;
14599 pos_T *fp;
14600
14601 fp = var2fpos(&argvars[0], FALSE);
14602 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14603 {
14604 getvvcol(curwin, fp, NULL, NULL, &vcol);
14605 ++vcol;
14606 }
14607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014608 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609}
14610
14611/*
14612 * "visualmode()" function
14613 */
14614/*ARGSUSED*/
14615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014616f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014617 typval_T *argvars;
14618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619{
14620#ifdef FEAT_VISUAL
14621 char_u str[2];
14622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014623 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 str[0] = curbuf->b_visual_mode_eval;
14625 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014626 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627
14628 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014629 if ((argvars[0].v_type == VAR_NUMBER
14630 && argvars[0].vval.v_number != 0)
14631 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014632 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633 curbuf->b_visual_mode_eval = NUL;
14634#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014635 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636#endif
14637}
14638
14639/*
14640 * "winbufnr(nr)" function
14641 */
14642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014643f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014644 typval_T *argvars;
14645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646{
14647 win_T *wp;
14648
14649 wp = find_win_by_nr(&argvars[0]);
14650 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014651 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014653 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654}
14655
14656/*
14657 * "wincol()" function
14658 */
14659/*ARGSUSED*/
14660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014661f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014662 typval_T *argvars;
14663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664{
14665 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014666 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667}
14668
14669/*
14670 * "winheight(nr)" function
14671 */
14672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014673f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014674 typval_T *argvars;
14675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676{
14677 win_T *wp;
14678
14679 wp = find_win_by_nr(&argvars[0]);
14680 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014681 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014683 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684}
14685
14686/*
14687 * "winline()" function
14688 */
14689/*ARGSUSED*/
14690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014691f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014692 typval_T *argvars;
14693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014694{
14695 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014696 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697}
14698
14699/*
14700 * "winnr()" function
14701 */
14702/* ARGSUSED */
14703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014704f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014705 typval_T *argvars;
14706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707{
14708 int nr = 1;
14709#ifdef FEAT_WINDOWS
14710 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014711 win_T *twin = curwin;
14712 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014714 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014715 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014716 arg = get_tv_string_chk(&argvars[0]);
14717 if (arg == NULL)
14718 nr = 0; /* type error; errmsg already given */
14719 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014720 twin = lastwin;
14721 else if (STRCMP(arg, "#") == 0)
14722 {
14723 twin = prevwin;
14724 if (prevwin == NULL)
14725 nr = 0;
14726 }
14727 else
14728 {
14729 EMSG2(_(e_invexpr2), arg);
14730 nr = 0;
14731 }
14732 }
14733
14734 if (nr > 0)
14735 for (wp = firstwin; wp != twin; wp = wp->w_next)
14736 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014738 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739}
14740
14741/*
14742 * "winrestcmd()" function
14743 */
14744/* ARGSUSED */
14745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014746f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014747 typval_T *argvars;
14748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749{
14750#ifdef FEAT_WINDOWS
14751 win_T *wp;
14752 int winnr = 1;
14753 garray_T ga;
14754 char_u buf[50];
14755
14756 ga_init2(&ga, (int)sizeof(char), 70);
14757 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14758 {
14759 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14760 ga_concat(&ga, buf);
14761# ifdef FEAT_VERTSPLIT
14762 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14763 ga_concat(&ga, buf);
14764# endif
14765 ++winnr;
14766 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014767 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014769 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014771 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014772#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014773 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774}
14775
14776/*
14777 * "winwidth(nr)" function
14778 */
14779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014780f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014781 typval_T *argvars;
14782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014783{
14784 win_T *wp;
14785
14786 wp = find_win_by_nr(&argvars[0]);
14787 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014788 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789 else
14790#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014791 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014793 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794#endif
14795}
14796
Bram Moolenaar071d4272004-06-13 20:20:40 +000014797/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014798 * "writefile()" function
14799 */
14800 static void
14801f_writefile(argvars, rettv)
14802 typval_T *argvars;
14803 typval_T *rettv;
14804{
14805 int binary = FALSE;
14806 char_u *fname;
14807 FILE *fd;
14808 listitem_T *li;
14809 char_u *s;
14810 int ret = 0;
14811 int c;
14812
14813 if (argvars[0].v_type != VAR_LIST)
14814 {
14815 EMSG2(_(e_listarg), "writefile()");
14816 return;
14817 }
14818 if (argvars[0].vval.v_list == NULL)
14819 return;
14820
14821 if (argvars[2].v_type != VAR_UNKNOWN
14822 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
14823 binary = TRUE;
14824
14825 /* Always open the file in binary mode, library functions have a mind of
14826 * their own about CR-LF conversion. */
14827 fname = get_tv_string(&argvars[1]);
14828 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
14829 {
14830 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14831 ret = -1;
14832 }
14833 else
14834 {
14835 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
14836 li = li->li_next)
14837 {
14838 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
14839 {
14840 if (*s == '\n')
14841 c = putc(NUL, fd);
14842 else
14843 c = putc(*s, fd);
14844 if (c == EOF)
14845 {
14846 ret = -1;
14847 break;
14848 }
14849 }
14850 if (!binary || li->li_next != NULL)
14851 if (putc('\n', fd) == EOF)
14852 {
14853 ret = -1;
14854 break;
14855 }
14856 if (ret < 0)
14857 {
14858 EMSG(_(e_write));
14859 break;
14860 }
14861 }
14862 fclose(fd);
14863 }
14864
14865 rettv->vval.v_number = ret;
14866}
14867
14868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014869 * Translate a String variable into a position.
14870 */
14871 static pos_T *
14872var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000014873 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014874 int lnum; /* TRUE when $ is last line */
14875{
14876 char_u *name;
14877 static pos_T pos;
14878 pos_T *pp;
14879
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014880 name = get_tv_string_chk(varp);
14881 if (name == NULL)
14882 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883 if (name[0] == '.') /* cursor */
14884 return &curwin->w_cursor;
14885 if (name[0] == '\'') /* mark */
14886 {
14887 pp = getmark(name[1], FALSE);
14888 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
14889 return NULL;
14890 return pp;
14891 }
14892 if (name[0] == '$') /* last column or line */
14893 {
14894 if (lnum)
14895 {
14896 pos.lnum = curbuf->b_ml.ml_line_count;
14897 pos.col = 0;
14898 }
14899 else
14900 {
14901 pos.lnum = curwin->w_cursor.lnum;
14902 pos.col = (colnr_T)STRLEN(ml_get_curline());
14903 }
14904 return &pos;
14905 }
14906 return NULL;
14907}
14908
14909/*
14910 * Get the length of an environment variable name.
14911 * Advance "arg" to the first character after the name.
14912 * Return 0 for error.
14913 */
14914 static int
14915get_env_len(arg)
14916 char_u **arg;
14917{
14918 char_u *p;
14919 int len;
14920
14921 for (p = *arg; vim_isIDc(*p); ++p)
14922 ;
14923 if (p == *arg) /* no name found */
14924 return 0;
14925
14926 len = (int)(p - *arg);
14927 *arg = p;
14928 return len;
14929}
14930
14931/*
14932 * Get the length of the name of a function or internal variable.
14933 * "arg" is advanced to the first non-white character after the name.
14934 * Return 0 if something is wrong.
14935 */
14936 static int
14937get_id_len(arg)
14938 char_u **arg;
14939{
14940 char_u *p;
14941 int len;
14942
14943 /* Find the end of the name. */
14944 for (p = *arg; eval_isnamec(*p); ++p)
14945 ;
14946 if (p == *arg) /* no name found */
14947 return 0;
14948
14949 len = (int)(p - *arg);
14950 *arg = skipwhite(p);
14951
14952 return len;
14953}
14954
14955/*
Bram Moolenaara7043832005-01-21 11:56:39 +000014956 * Get the length of the name of a variable or function.
14957 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014959 * Return -1 if curly braces expansion failed.
14960 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961 * If the name contains 'magic' {}'s, expand them and return the
14962 * expanded name in an allocated string via 'alias' - caller must free.
14963 */
14964 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014965get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 char_u **arg;
14967 char_u **alias;
14968 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014969 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970{
14971 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 char_u *p;
14973 char_u *expr_start;
14974 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975
14976 *alias = NULL; /* default to no alias */
14977
14978 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
14979 && (*arg)[2] == (int)KE_SNR)
14980 {
14981 /* hard coded <SNR>, already translated */
14982 *arg += 3;
14983 return get_id_len(arg) + 3;
14984 }
14985 len = eval_fname_script(*arg);
14986 if (len > 0)
14987 {
14988 /* literal "<SID>", "s:" or "<SNR>" */
14989 *arg += len;
14990 }
14991
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014993 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014995 p = find_name_end(*arg, &expr_start, &expr_end,
14996 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997 if (expr_start != NULL)
14998 {
14999 char_u *temp_string;
15000
15001 if (!evaluate)
15002 {
15003 len += (int)(p - *arg);
15004 *arg = skipwhite(p);
15005 return len;
15006 }
15007
15008 /*
15009 * Include any <SID> etc in the expanded string:
15010 * Thus the -len here.
15011 */
15012 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15013 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015014 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015 *alias = temp_string;
15016 *arg = skipwhite(p);
15017 return (int)STRLEN(temp_string);
15018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015019
15020 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015021 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015022 EMSG2(_(e_invexpr2), *arg);
15023
15024 return len;
15025}
15026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015027/*
15028 * Find the end of a variable or function name, taking care of magic braces.
15029 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15030 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015031 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015032 * Return a pointer to just after the name. Equal to "arg" if there is no
15033 * valid name.
15034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015036find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037 char_u *arg;
15038 char_u **expr_start;
15039 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015040 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015042 int mb_nest = 0;
15043 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044 char_u *p;
15045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015046 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015048 *expr_start = NULL;
15049 *expr_end = NULL;
15050 }
15051
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015052 /* Quick check for valid starting character. */
15053 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15054 return arg;
15055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015056 for (p = arg; *p != NUL
15057 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015058 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015059 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015060 || mb_nest != 0
15061 || br_nest != 0); ++p)
15062 {
15063 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015065 if (*p == '[')
15066 ++br_nest;
15067 else if (*p == ']')
15068 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015069 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015070 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015072 if (*p == '{')
15073 {
15074 mb_nest++;
15075 if (expr_start != NULL && *expr_start == NULL)
15076 *expr_start = p;
15077 }
15078 else if (*p == '}')
15079 {
15080 mb_nest--;
15081 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15082 *expr_end = p;
15083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015085 }
15086
15087 return p;
15088}
15089
15090/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015091 * Expands out the 'magic' {}'s in a variable/function name.
15092 * Note that this can call itself recursively, to deal with
15093 * constructs like foo{bar}{baz}{bam}
15094 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15095 * "in_start" ^
15096 * "expr_start" ^
15097 * "expr_end" ^
15098 * "in_end" ^
15099 *
15100 * Returns a new allocated string, which the caller must free.
15101 * Returns NULL for failure.
15102 */
15103 static char_u *
15104make_expanded_name(in_start, expr_start, expr_end, in_end)
15105 char_u *in_start;
15106 char_u *expr_start;
15107 char_u *expr_end;
15108 char_u *in_end;
15109{
15110 char_u c1;
15111 char_u *retval = NULL;
15112 char_u *temp_result;
15113 char_u *nextcmd = NULL;
15114
15115 if (expr_end == NULL || in_end == NULL)
15116 return NULL;
15117 *expr_start = NUL;
15118 *expr_end = NUL;
15119 c1 = *in_end;
15120 *in_end = NUL;
15121
15122 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15123 if (temp_result != NULL && nextcmd == NULL)
15124 {
15125 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15126 + (in_end - expr_end) + 1));
15127 if (retval != NULL)
15128 {
15129 STRCPY(retval, in_start);
15130 STRCAT(retval, temp_result);
15131 STRCAT(retval, expr_end + 1);
15132 }
15133 }
15134 vim_free(temp_result);
15135
15136 *in_end = c1; /* put char back for error messages */
15137 *expr_start = '{';
15138 *expr_end = '}';
15139
15140 if (retval != NULL)
15141 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015142 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015143 if (expr_start != NULL)
15144 {
15145 /* Further expansion! */
15146 temp_result = make_expanded_name(retval, expr_start,
15147 expr_end, temp_result);
15148 vim_free(retval);
15149 retval = temp_result;
15150 }
15151 }
15152
15153 return retval;
15154}
15155
15156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015158 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159 */
15160 static int
15161eval_isnamec(c)
15162 int c;
15163{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015164 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15165}
15166
15167/*
15168 * Return TRUE if character "c" can be used as the first character in a
15169 * variable or function name (excluding '{' and '}').
15170 */
15171 static int
15172eval_isnamec1(c)
15173 int c;
15174{
15175 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176}
15177
15178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179 * Set number v: variable to "val".
15180 */
15181 void
15182set_vim_var_nr(idx, val)
15183 int idx;
15184 long val;
15185{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015186 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187}
15188
15189/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015190 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015191 */
15192 long
15193get_vim_var_nr(idx)
15194 int idx;
15195{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015196 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197}
15198
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015199#if defined(FEAT_AUTOCMD) || defined(PROTO)
15200/*
15201 * Get string v: variable value. Uses a static buffer, can only be used once.
15202 */
15203 char_u *
15204get_vim_var_str(idx)
15205 int idx;
15206{
15207 return get_tv_string(&vimvars[idx].vv_tv);
15208}
15209#endif
15210
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211/*
15212 * Set v:count, v:count1 and v:prevcount.
15213 */
15214 void
15215set_vcount(count, count1)
15216 long count;
15217 long count1;
15218{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015219 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15220 vimvars[VV_COUNT].vv_nr = count;
15221 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015222}
15223
15224/*
15225 * Set string v: variable to a copy of "val".
15226 */
15227 void
15228set_vim_var_string(idx, val, len)
15229 int idx;
15230 char_u *val;
15231 int len; /* length of "val" to use or -1 (whole string) */
15232{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015233 /* Need to do this (at least) once, since we can't initialize a union.
15234 * Will always be invoked when "v:progname" is set. */
15235 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15236
Bram Moolenaare9a41262005-01-15 22:18:47 +000015237 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015238 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015239 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015241 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015243 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244}
15245
15246/*
15247 * Set v:register if needed.
15248 */
15249 void
15250set_reg_var(c)
15251 int c;
15252{
15253 char_u regname;
15254
15255 if (c == 0 || c == ' ')
15256 regname = '"';
15257 else
15258 regname = c;
15259 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015260 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261 set_vim_var_string(VV_REG, &regname, 1);
15262}
15263
15264/*
15265 * Get or set v:exception. If "oldval" == NULL, return the current value.
15266 * Otherwise, restore the value to "oldval" and return NULL.
15267 * Must always be called in pairs to save and restore v:exception! Does not
15268 * take care of memory allocations.
15269 */
15270 char_u *
15271v_exception(oldval)
15272 char_u *oldval;
15273{
15274 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015275 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276
Bram Moolenaare9a41262005-01-15 22:18:47 +000015277 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278 return NULL;
15279}
15280
15281/*
15282 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15283 * Otherwise, restore the value to "oldval" and return NULL.
15284 * Must always be called in pairs to save and restore v:throwpoint! Does not
15285 * take care of memory allocations.
15286 */
15287 char_u *
15288v_throwpoint(oldval)
15289 char_u *oldval;
15290{
15291 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015292 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015293
Bram Moolenaare9a41262005-01-15 22:18:47 +000015294 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015295 return NULL;
15296}
15297
15298#if defined(FEAT_AUTOCMD) || defined(PROTO)
15299/*
15300 * Set v:cmdarg.
15301 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15302 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15303 * Must always be called in pairs!
15304 */
15305 char_u *
15306set_cmdarg(eap, oldarg)
15307 exarg_T *eap;
15308 char_u *oldarg;
15309{
15310 char_u *oldval;
15311 char_u *newval;
15312 unsigned len;
15313
Bram Moolenaare9a41262005-01-15 22:18:47 +000015314 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015315 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015316 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015317 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015318 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015319 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320 }
15321
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015322 if (eap->force_bin == FORCE_BIN)
15323 len = 6;
15324 else if (eap->force_bin == FORCE_NOBIN)
15325 len = 8;
15326 else
15327 len = 0;
15328 if (eap->force_ff != 0)
15329 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15330# ifdef FEAT_MBYTE
15331 if (eap->force_enc != 0)
15332 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15333# endif
15334
15335 newval = alloc(len + 1);
15336 if (newval == NULL)
15337 return NULL;
15338
15339 if (eap->force_bin == FORCE_BIN)
15340 sprintf((char *)newval, " ++bin");
15341 else if (eap->force_bin == FORCE_NOBIN)
15342 sprintf((char *)newval, " ++nobin");
15343 else
15344 *newval = NUL;
15345 if (eap->force_ff != 0)
15346 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15347 eap->cmd + eap->force_ff);
15348# ifdef FEAT_MBYTE
15349 if (eap->force_enc != 0)
15350 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15351 eap->cmd + eap->force_enc);
15352# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015353 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015354 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355}
15356#endif
15357
15358/*
15359 * Get the value of internal variable "name".
15360 * Return OK or FAIL.
15361 */
15362 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015363get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364 char_u *name;
15365 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015366 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015367 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368{
15369 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015370 typval_T *tv = NULL;
15371 typval_T atv;
15372 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374
15375 /* truncate the name, so that we can use strcmp() */
15376 cc = name[len];
15377 name[len] = NUL;
15378
15379 /*
15380 * Check for "b:changedtick".
15381 */
15382 if (STRCMP(name, "b:changedtick") == 0)
15383 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015384 atv.v_type = VAR_NUMBER;
15385 atv.vval.v_number = curbuf->b_changedtick;
15386 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 }
15388
15389 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390 * Check for user-defined variables.
15391 */
15392 else
15393 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015394 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015396 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397 }
15398
Bram Moolenaare9a41262005-01-15 22:18:47 +000015399 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015401 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015402 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 ret = FAIL;
15404 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015405 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015406 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407
15408 name[len] = cc;
15409
15410 return ret;
15411}
15412
15413/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015414 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15415 * Also handle function call with Funcref variable: func(expr)
15416 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15417 */
15418 static int
15419handle_subscript(arg, rettv, evaluate, verbose)
15420 char_u **arg;
15421 typval_T *rettv;
15422 int evaluate; /* do more than finding the end */
15423 int verbose; /* give error messages */
15424{
15425 int ret = OK;
15426 dict_T *selfdict = NULL;
15427 char_u *s;
15428 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015429 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015430
15431 while (ret == OK
15432 && (**arg == '['
15433 || (**arg == '.' && rettv->v_type == VAR_DICT)
15434 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15435 && !vim_iswhite(*(*arg - 1)))
15436 {
15437 if (**arg == '(')
15438 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015439 /* need to copy the funcref so that we can clear rettv */
15440 functv = *rettv;
15441 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015442
15443 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015444 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015445 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015446 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15447 &len, evaluate, selfdict);
15448
15449 /* Clear the funcref afterwards, so that deleting it while
15450 * evaluating the arguments is possible (see test55). */
15451 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015452
15453 /* Stop the expression evaluation when immediately aborting on
15454 * error, or when an interrupt occurred or an exception was thrown
15455 * but not caught. */
15456 if (aborting())
15457 {
15458 if (ret == OK)
15459 clear_tv(rettv);
15460 ret = FAIL;
15461 }
15462 dict_unref(selfdict);
15463 selfdict = NULL;
15464 }
15465 else /* **arg == '[' || **arg == '.' */
15466 {
15467 dict_unref(selfdict);
15468 if (rettv->v_type == VAR_DICT)
15469 {
15470 selfdict = rettv->vval.v_dict;
15471 if (selfdict != NULL)
15472 ++selfdict->dv_refcount;
15473 }
15474 else
15475 selfdict = NULL;
15476 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15477 {
15478 clear_tv(rettv);
15479 ret = FAIL;
15480 }
15481 }
15482 }
15483 dict_unref(selfdict);
15484 return ret;
15485}
15486
15487/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015488 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15489 * value).
15490 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015491 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015492alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015493{
Bram Moolenaar33570922005-01-25 22:26:29 +000015494 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015495}
15496
15497/*
15498 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499 * The string "s" must have been allocated, it is consumed.
15500 * Return NULL for out of memory, the variable otherwise.
15501 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015502 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015503alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504 char_u *s;
15505{
Bram Moolenaar33570922005-01-25 22:26:29 +000015506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015508 rettv = alloc_tv();
15509 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015511 rettv->v_type = VAR_STRING;
15512 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513 }
15514 else
15515 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015516 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517}
15518
15519/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015520 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521 */
15522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015523free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015524 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525{
15526 if (varp != NULL)
15527 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015528 switch (varp->v_type)
15529 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015530 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015531 func_unref(varp->vval.v_string);
15532 /*FALLTHROUGH*/
15533 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015534 vim_free(varp->vval.v_string);
15535 break;
15536 case VAR_LIST:
15537 list_unref(varp->vval.v_list);
15538 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015539 case VAR_DICT:
15540 dict_unref(varp->vval.v_dict);
15541 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015542 case VAR_NUMBER:
15543 case VAR_UNKNOWN:
15544 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015545 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015546 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015547 break;
15548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 vim_free(varp);
15550 }
15551}
15552
15553/*
15554 * Free the memory for a variable value and set the value to NULL or 0.
15555 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015556 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015557clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015558 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559{
15560 if (varp != NULL)
15561 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015562 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015564 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015565 func_unref(varp->vval.v_string);
15566 /*FALLTHROUGH*/
15567 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015568 vim_free(varp->vval.v_string);
15569 varp->vval.v_string = NULL;
15570 break;
15571 case VAR_LIST:
15572 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015573 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015574 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015575 case VAR_DICT:
15576 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015577 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015578 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015579 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015580 varp->vval.v_number = 0;
15581 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015582 case VAR_UNKNOWN:
15583 break;
15584 default:
15585 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015587 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 }
15589}
15590
15591/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015592 * Set the value of a variable to NULL without freeing items.
15593 */
15594 static void
15595init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015596 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015597{
15598 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015599 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015600}
15601
15602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 * Get the number value of a variable.
15604 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015605 * For incompatible types, return 0.
15606 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15607 * caller of incompatible types: it sets *denote to TRUE if "denote"
15608 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609 */
15610 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015611get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015612 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015614 int error = FALSE;
15615
15616 return get_tv_number_chk(varp, &error); /* return 0L on error */
15617}
15618
15619 static long
15620get_tv_number_chk(varp, denote)
15621 typval_T *varp;
15622 int *denote;
15623{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015624 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015626 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015628 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015629 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015630 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015631 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015632 break;
15633 case VAR_STRING:
15634 if (varp->vval.v_string != NULL)
15635 vim_str2nr(varp->vval.v_string, NULL, NULL,
15636 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015637 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015638 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015639 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015640 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015641 case VAR_DICT:
15642 EMSG(_("E728: Using a Dictionary as a number"));
15643 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015644 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015645 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015646 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015648 if (denote == NULL) /* useful for values that must be unsigned */
15649 n = -1;
15650 else
15651 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015652 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653}
15654
15655/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015656 * Get the lnum from the first argument.
15657 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015658 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659 */
15660 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015661get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015662 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663{
Bram Moolenaar33570922005-01-25 22:26:29 +000015664 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 linenr_T lnum;
15666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015667 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 if (lnum == 0) /* no valid number, try using line() */
15669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015670 rettv.v_type = VAR_NUMBER;
15671 f_line(argvars, &rettv);
15672 lnum = rettv.vval.v_number;
15673 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674 }
15675 return lnum;
15676}
15677
15678/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015679 * Get the lnum from the first argument.
15680 * Also accepts "$", then "buf" is used.
15681 * Returns 0 on error.
15682 */
15683 static linenr_T
15684get_tv_lnum_buf(argvars, buf)
15685 typval_T *argvars;
15686 buf_T *buf;
15687{
15688 if (argvars[0].v_type == VAR_STRING
15689 && argvars[0].vval.v_string != NULL
15690 && argvars[0].vval.v_string[0] == '$'
15691 && buf != NULL)
15692 return buf->b_ml.ml_line_count;
15693 return get_tv_number_chk(&argvars[0], NULL);
15694}
15695
15696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697 * Get the string value of a variable.
15698 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015699 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15700 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015701 * If the String variable has never been set, return an empty string.
15702 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015703 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15704 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705 */
15706 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015707get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015708 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709{
15710 static char_u mybuf[NUMBUFLEN];
15711
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015712 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713}
15714
15715 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015716get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015717 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015718 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015720 char_u *res = get_tv_string_buf_chk(varp, buf);
15721
15722 return res != NULL ? res : (char_u *)"";
15723}
15724
15725 static char_u *
15726get_tv_string_chk(varp)
15727 typval_T *varp;
15728{
15729 static char_u mybuf[NUMBUFLEN];
15730
15731 return get_tv_string_buf_chk(varp, mybuf);
15732}
15733
15734 static char_u *
15735get_tv_string_buf_chk(varp, buf)
15736 typval_T *varp;
15737 char_u *buf;
15738{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015739 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015741 case VAR_NUMBER:
15742 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15743 return buf;
15744 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015745 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015746 break;
15747 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015748 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015749 break;
15750 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015751 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015752 break;
15753 case VAR_STRING:
15754 if (varp->vval.v_string != NULL)
15755 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015756 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015757 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015758 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015759 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015761 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762}
15763
15764/*
15765 * Find variable "name" in the list of variables.
15766 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015767 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015768 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015769 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015771 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015772find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015774 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015777 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778
Bram Moolenaara7043832005-01-21 11:56:39 +000015779 ht = find_var_ht(name, &varname);
15780 if (htp != NULL)
15781 *htp = ht;
15782 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015784 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785}
15786
15787/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015788 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000015789 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015791 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015792find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000015793 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000015794 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015795 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000015796{
Bram Moolenaar33570922005-01-25 22:26:29 +000015797 hashitem_T *hi;
15798
15799 if (*varname == NUL)
15800 {
15801 /* Must be something like "s:", otherwise "ht" would be NULL. */
15802 switch (varname[-2])
15803 {
15804 case 's': return &SCRIPT_SV(current_SID).sv_var;
15805 case 'g': return &globvars_var;
15806 case 'v': return &vimvars_var;
15807 case 'b': return &curbuf->b_bufvar;
15808 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015809 case 'l': return current_funccal == NULL
15810 ? NULL : &current_funccal->l_vars_var;
15811 case 'a': return current_funccal == NULL
15812 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000015813 }
15814 return NULL;
15815 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015816
15817 hi = hash_find(ht, varname);
15818 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015819 {
15820 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015821 * worked find the variable again. Don't auto-load a script if it was
15822 * loaded already, otherwise it would be loaded every time when
15823 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015824 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015825 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015826 hi = hash_find(ht, varname);
15827 if (HASHITEM_EMPTY(hi))
15828 return NULL;
15829 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015830 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015831}
15832
15833/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015834 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015835 * Set "varname" to the start of name without ':'.
15836 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015837 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015838find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839 char_u *name;
15840 char_u **varname;
15841{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015842 hashitem_T *hi;
15843
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844 if (name[1] != ':')
15845 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015846 /* The name must not start with a colon or #. */
15847 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848 return NULL;
15849 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015850
15851 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015852 hi = hash_find(&compat_hashtab, name);
15853 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000015854 return &compat_hashtab;
15855
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015857 return &globvarht; /* global variable */
15858 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859 }
15860 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015861 if (*name == 'g') /* global variable */
15862 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015863 /* There must be no ':' or '#' in the rest of the name, unless g: is used
15864 */
15865 if (vim_strchr(name + 2, ':') != NULL
15866 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015867 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015869 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015871 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000015872 if (*name == 'v') /* v: variable */
15873 return &vimvarht;
15874 if (*name == 'a' && current_funccal != NULL) /* function argument */
15875 return &current_funccal->l_avars.dv_hashtab;
15876 if (*name == 'l' && current_funccal != NULL) /* local function variable */
15877 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878 if (*name == 's' /* script variable */
15879 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
15880 return &SCRIPT_VARS(current_SID);
15881 return NULL;
15882}
15883
15884/*
15885 * Get the string value of a (global/local) variable.
15886 * Returns NULL when it doesn't exist.
15887 */
15888 char_u *
15889get_var_value(name)
15890 char_u *name;
15891{
Bram Moolenaar33570922005-01-25 22:26:29 +000015892 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015893
Bram Moolenaara7043832005-01-21 11:56:39 +000015894 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895 if (v == NULL)
15896 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015897 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898}
15899
15900/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015901 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000015902 * sourcing this script and when executing functions defined in the script.
15903 */
15904 void
15905new_script_vars(id)
15906 scid_T id;
15907{
Bram Moolenaara7043832005-01-21 11:56:39 +000015908 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000015909 hashtab_T *ht;
15910 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000015911
Bram Moolenaar071d4272004-06-13 20:20:40 +000015912 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
15913 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015914 /* Re-allocating ga_data means that an ht_array pointing to
15915 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000015916 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000015917 for (i = 1; i <= ga_scripts.ga_len; ++i)
15918 {
15919 ht = &SCRIPT_VARS(i);
15920 if (ht->ht_mask == HT_INIT_SIZE - 1)
15921 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000015922 sv = &SCRIPT_SV(i);
15923 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000015924 }
15925
Bram Moolenaar071d4272004-06-13 20:20:40 +000015926 while (ga_scripts.ga_len < id)
15927 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015928 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
15929 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931 }
15932 }
15933}
15934
15935/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015936 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
15937 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938 */
15939 void
Bram Moolenaar33570922005-01-25 22:26:29 +000015940init_var_dict(dict, dict_var)
15941 dict_T *dict;
15942 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943{
Bram Moolenaar33570922005-01-25 22:26:29 +000015944 hash_init(&dict->dv_hashtab);
15945 dict->dv_refcount = 99999;
15946 dict_var->di_tv.vval.v_dict = dict;
15947 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015948 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015949 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15950 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951}
15952
15953/*
15954 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000015955 * Frees all allocated variables and the value they contain.
15956 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015957 */
15958 void
Bram Moolenaara7043832005-01-21 11:56:39 +000015959vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000015960 hashtab_T *ht;
15961{
15962 vars_clear_ext(ht, TRUE);
15963}
15964
15965/*
15966 * Like vars_clear(), but only free the value if "free_val" is TRUE.
15967 */
15968 static void
15969vars_clear_ext(ht, free_val)
15970 hashtab_T *ht;
15971 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972{
Bram Moolenaara7043832005-01-21 11:56:39 +000015973 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000015974 hashitem_T *hi;
15975 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015976
Bram Moolenaar33570922005-01-25 22:26:29 +000015977 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000015978 todo = ht->ht_used;
15979 for (hi = ht->ht_array; todo > 0; ++hi)
15980 {
15981 if (!HASHITEM_EMPTY(hi))
15982 {
15983 --todo;
15984
Bram Moolenaar33570922005-01-25 22:26:29 +000015985 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000015986 * ht_array might change then. hash_clear() takes care of it
15987 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015988 v = HI2DI(hi);
15989 if (free_val)
15990 clear_tv(&v->di_tv);
15991 if ((v->di_flags & DI_FLAGS_FIX) == 0)
15992 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000015993 }
15994 }
15995 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015996 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997}
15998
Bram Moolenaara7043832005-01-21 11:56:39 +000015999/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016000 * Delete a variable from hashtab "ht" at item "hi".
16001 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016004delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016005 hashtab_T *ht;
16006 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016007{
Bram Moolenaar33570922005-01-25 22:26:29 +000016008 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016009
16010 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016011 clear_tv(&di->di_tv);
16012 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013}
16014
16015/*
16016 * List the value of one internal variable.
16017 */
16018 static void
16019list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016020 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021 char_u *prefix;
16022{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016023 char_u *tofree;
16024 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016025 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016026
Bram Moolenaar33570922005-01-25 22:26:29 +000016027 s = echo_string(&v->di_tv, &tofree, numbuf);
16028 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016029 s == NULL ? (char_u *)"" : s);
16030 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031}
16032
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033 static void
16034list_one_var_a(prefix, name, type, string)
16035 char_u *prefix;
16036 char_u *name;
16037 int type;
16038 char_u *string;
16039{
16040 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16041 if (name != NULL) /* "a:" vars don't have a name stored */
16042 msg_puts(name);
16043 msg_putchar(' ');
16044 msg_advance(22);
16045 if (type == VAR_NUMBER)
16046 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016047 else if (type == VAR_FUNC)
16048 msg_putchar('*');
16049 else if (type == VAR_LIST)
16050 {
16051 msg_putchar('[');
16052 if (*string == '[')
16053 ++string;
16054 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016055 else if (type == VAR_DICT)
16056 {
16057 msg_putchar('{');
16058 if (*string == '{')
16059 ++string;
16060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061 else
16062 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016063
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016065
16066 if (type == VAR_FUNC)
16067 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068}
16069
16070/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016071 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072 * If the variable already exists, the value is updated.
16073 * Otherwise the variable is created.
16074 */
16075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016076set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016078 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016079 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016080{
Bram Moolenaar33570922005-01-25 22:26:29 +000016081 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016083 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016084 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016086 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016087 {
16088 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16089 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16090 ? name[2] : name[0]))
16091 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016092 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016093 return;
16094 }
16095 if (function_exists(name))
16096 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016097 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016098 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016099 return;
16100 }
16101 }
16102
Bram Moolenaara7043832005-01-21 11:56:39 +000016103 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016104 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016105 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016106 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016107 return;
16108 }
16109
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016110 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016111 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016113 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016114 if (var_check_ro(v->di_flags, name)
16115 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016116 return;
16117 if (v->di_tv.v_type != tv->v_type
16118 && !((v->di_tv.v_type == VAR_STRING
16119 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016120 && (tv->v_type == VAR_STRING
16121 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016122 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016123 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016124 return;
16125 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016126
16127 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016128 * Handle setting internal v: variables separately: we don't change
16129 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016130 */
16131 if (ht == &vimvarht)
16132 {
16133 if (v->di_tv.v_type == VAR_STRING)
16134 {
16135 vim_free(v->di_tv.vval.v_string);
16136 if (copy || tv->v_type != VAR_STRING)
16137 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16138 else
16139 {
16140 /* Take over the string to avoid an extra alloc/free. */
16141 v->di_tv.vval.v_string = tv->vval.v_string;
16142 tv->vval.v_string = NULL;
16143 }
16144 }
16145 else if (v->di_tv.v_type != VAR_NUMBER)
16146 EMSG2(_(e_intern2), "set_var()");
16147 else
16148 v->di_tv.vval.v_number = get_tv_number(tv);
16149 return;
16150 }
16151
16152 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153 }
16154 else /* add a new variable */
16155 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016156 /* Make sure the variable name is valid. */
16157 for (p = varname; *p != NUL; ++p)
16158 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16159 {
16160 EMSG2(_(e_illvar), varname);
16161 return;
16162 }
16163
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016164 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16165 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016166 if (v == NULL)
16167 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016168 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016169 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016171 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172 return;
16173 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016174 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016177 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016178 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016179 else
16180 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016181 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016182 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016183 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185}
16186
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016187/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016188 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16189 * Also give an error message.
16190 */
16191 static int
16192var_check_ro(flags, name)
16193 int flags;
16194 char_u *name;
16195{
16196 if (flags & DI_FLAGS_RO)
16197 {
16198 EMSG2(_(e_readonlyvar), name);
16199 return TRUE;
16200 }
16201 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16202 {
16203 EMSG2(_(e_readonlysbx), name);
16204 return TRUE;
16205 }
16206 return FALSE;
16207}
16208
16209/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016210 * Return TRUE if typeval "tv" is set to be locked (immutable).
16211 * Also give an error message, using "name".
16212 */
16213 static int
16214tv_check_lock(lock, name)
16215 int lock;
16216 char_u *name;
16217{
16218 if (lock & VAR_LOCKED)
16219 {
16220 EMSG2(_("E741: Value is locked: %s"),
16221 name == NULL ? (char_u *)_("Unknown") : name);
16222 return TRUE;
16223 }
16224 if (lock & VAR_FIXED)
16225 {
16226 EMSG2(_("E742: Cannot change value of %s"),
16227 name == NULL ? (char_u *)_("Unknown") : name);
16228 return TRUE;
16229 }
16230 return FALSE;
16231}
16232
16233/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016234 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016235 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016236 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016239copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016240 typval_T *from;
16241 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016243 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016244 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016245 switch (from->v_type)
16246 {
16247 case VAR_NUMBER:
16248 to->vval.v_number = from->vval.v_number;
16249 break;
16250 case VAR_STRING:
16251 case VAR_FUNC:
16252 if (from->vval.v_string == NULL)
16253 to->vval.v_string = NULL;
16254 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016255 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016256 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016257 if (from->v_type == VAR_FUNC)
16258 func_ref(to->vval.v_string);
16259 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016260 break;
16261 case VAR_LIST:
16262 if (from->vval.v_list == NULL)
16263 to->vval.v_list = NULL;
16264 else
16265 {
16266 to->vval.v_list = from->vval.v_list;
16267 ++to->vval.v_list->lv_refcount;
16268 }
16269 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016270 case VAR_DICT:
16271 if (from->vval.v_dict == NULL)
16272 to->vval.v_dict = NULL;
16273 else
16274 {
16275 to->vval.v_dict = from->vval.v_dict;
16276 ++to->vval.v_dict->dv_refcount;
16277 }
16278 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016279 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016280 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016281 break;
16282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016283}
16284
16285/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016286 * Make a copy of an item.
16287 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016288 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16289 * reference to an already copied list/dict can be used.
16290 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016291 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016292 static int
16293item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016294 typval_T *from;
16295 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016296 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016297 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016298{
16299 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016300 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016301
Bram Moolenaar33570922005-01-25 22:26:29 +000016302 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016303 {
16304 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016305 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016306 }
16307 ++recurse;
16308
16309 switch (from->v_type)
16310 {
16311 case VAR_NUMBER:
16312 case VAR_STRING:
16313 case VAR_FUNC:
16314 copy_tv(from, to);
16315 break;
16316 case VAR_LIST:
16317 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016318 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016319 if (from->vval.v_list == NULL)
16320 to->vval.v_list = NULL;
16321 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16322 {
16323 /* use the copy made earlier */
16324 to->vval.v_list = from->vval.v_list->lv_copylist;
16325 ++to->vval.v_list->lv_refcount;
16326 }
16327 else
16328 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16329 if (to->vval.v_list == NULL)
16330 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016331 break;
16332 case VAR_DICT:
16333 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016334 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016335 if (from->vval.v_dict == NULL)
16336 to->vval.v_dict = NULL;
16337 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16338 {
16339 /* use the copy made earlier */
16340 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16341 ++to->vval.v_dict->dv_refcount;
16342 }
16343 else
16344 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16345 if (to->vval.v_dict == NULL)
16346 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016347 break;
16348 default:
16349 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016350 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016351 }
16352 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016353 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016354}
16355
16356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357 * ":echo expr1 ..." print each argument separated with a space, add a
16358 * newline at the end.
16359 * ":echon expr1 ..." print each argument plain.
16360 */
16361 void
16362ex_echo(eap)
16363 exarg_T *eap;
16364{
16365 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016366 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016367 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368 char_u *p;
16369 int needclr = TRUE;
16370 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016371 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016372
16373 if (eap->skip)
16374 ++emsg_skip;
16375 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16376 {
16377 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016378 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379 {
16380 /*
16381 * Report the invalid expression unless the expression evaluation
16382 * has been cancelled due to an aborting error, an interrupt, or an
16383 * exception.
16384 */
16385 if (!aborting())
16386 EMSG2(_(e_invexpr2), p);
16387 break;
16388 }
16389 if (!eap->skip)
16390 {
16391 if (atstart)
16392 {
16393 atstart = FALSE;
16394 /* Call msg_start() after eval1(), evaluating the expression
16395 * may cause a message to appear. */
16396 if (eap->cmdidx == CMD_echo)
16397 msg_start();
16398 }
16399 else if (eap->cmdidx == CMD_echo)
16400 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016401 p = echo_string(&rettv, &tofree, numbuf);
16402 if (p != NULL)
16403 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016405 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016407 if (*p != TAB && needclr)
16408 {
16409 /* remove any text still there from the command */
16410 msg_clr_eos();
16411 needclr = FALSE;
16412 }
16413 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414 }
16415 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016416 {
16417#ifdef FEAT_MBYTE
16418 if (has_mbyte)
16419 {
16420 int i = (*mb_ptr2len_check)(p);
16421
16422 (void)msg_outtrans_len_attr(p, i, echo_attr);
16423 p += i - 1;
16424 }
16425 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016427 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016430 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016432 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016433 arg = skipwhite(arg);
16434 }
16435 eap->nextcmd = check_nextcmd(arg);
16436
16437 if (eap->skip)
16438 --emsg_skip;
16439 else
16440 {
16441 /* remove text that may still be there from the command */
16442 if (needclr)
16443 msg_clr_eos();
16444 if (eap->cmdidx == CMD_echo)
16445 msg_end();
16446 }
16447}
16448
16449/*
16450 * ":echohl {name}".
16451 */
16452 void
16453ex_echohl(eap)
16454 exarg_T *eap;
16455{
16456 int id;
16457
16458 id = syn_name2id(eap->arg);
16459 if (id == 0)
16460 echo_attr = 0;
16461 else
16462 echo_attr = syn_id2attr(id);
16463}
16464
16465/*
16466 * ":execute expr1 ..." execute the result of an expression.
16467 * ":echomsg expr1 ..." Print a message
16468 * ":echoerr expr1 ..." Print an error
16469 * Each gets spaces around each argument and a newline at the end for
16470 * echo commands
16471 */
16472 void
16473ex_execute(eap)
16474 exarg_T *eap;
16475{
16476 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016477 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016478 int ret = OK;
16479 char_u *p;
16480 garray_T ga;
16481 int len;
16482 int save_did_emsg;
16483
16484 ga_init2(&ga, 1, 80);
16485
16486 if (eap->skip)
16487 ++emsg_skip;
16488 while (*arg != NUL && *arg != '|' && *arg != '\n')
16489 {
16490 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016491 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016492 {
16493 /*
16494 * Report the invalid expression unless the expression evaluation
16495 * has been cancelled due to an aborting error, an interrupt, or an
16496 * exception.
16497 */
16498 if (!aborting())
16499 EMSG2(_(e_invexpr2), p);
16500 ret = FAIL;
16501 break;
16502 }
16503
16504 if (!eap->skip)
16505 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016506 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016507 len = (int)STRLEN(p);
16508 if (ga_grow(&ga, len + 2) == FAIL)
16509 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016510 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511 ret = FAIL;
16512 break;
16513 }
16514 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016515 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517 ga.ga_len += len;
16518 }
16519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016520 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521 arg = skipwhite(arg);
16522 }
16523
16524 if (ret != FAIL && ga.ga_data != NULL)
16525 {
16526 if (eap->cmdidx == CMD_echomsg)
16527 MSG_ATTR(ga.ga_data, echo_attr);
16528 else if (eap->cmdidx == CMD_echoerr)
16529 {
16530 /* We don't want to abort following commands, restore did_emsg. */
16531 save_did_emsg = did_emsg;
16532 EMSG((char_u *)ga.ga_data);
16533 if (!force_abort)
16534 did_emsg = save_did_emsg;
16535 }
16536 else if (eap->cmdidx == CMD_execute)
16537 do_cmdline((char_u *)ga.ga_data,
16538 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16539 }
16540
16541 ga_clear(&ga);
16542
16543 if (eap->skip)
16544 --emsg_skip;
16545
16546 eap->nextcmd = check_nextcmd(arg);
16547}
16548
16549/*
16550 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16551 * "arg" points to the "&" or '+' when called, to "option" when returning.
16552 * Returns NULL when no option name found. Otherwise pointer to the char
16553 * after the option name.
16554 */
16555 static char_u *
16556find_option_end(arg, opt_flags)
16557 char_u **arg;
16558 int *opt_flags;
16559{
16560 char_u *p = *arg;
16561
16562 ++p;
16563 if (*p == 'g' && p[1] == ':')
16564 {
16565 *opt_flags = OPT_GLOBAL;
16566 p += 2;
16567 }
16568 else if (*p == 'l' && p[1] == ':')
16569 {
16570 *opt_flags = OPT_LOCAL;
16571 p += 2;
16572 }
16573 else
16574 *opt_flags = 0;
16575
16576 if (!ASCII_ISALPHA(*p))
16577 return NULL;
16578 *arg = p;
16579
16580 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16581 p += 4; /* termcap option */
16582 else
16583 while (ASCII_ISALPHA(*p))
16584 ++p;
16585 return p;
16586}
16587
16588/*
16589 * ":function"
16590 */
16591 void
16592ex_function(eap)
16593 exarg_T *eap;
16594{
16595 char_u *theline;
16596 int j;
16597 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599 char_u *name = NULL;
16600 char_u *p;
16601 char_u *arg;
16602 garray_T newargs;
16603 garray_T newlines;
16604 int varargs = FALSE;
16605 int mustend = FALSE;
16606 int flags = 0;
16607 ufunc_T *fp;
16608 int indent;
16609 int nesting;
16610 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016611 dictitem_T *v;
16612 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016613 static int func_nr = 0; /* number for nameless function */
16614 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016615 hashtab_T *ht;
16616 int todo;
16617 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618
16619 /*
16620 * ":function" without argument: list functions.
16621 */
16622 if (ends_excmd(*eap->arg))
16623 {
16624 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016625 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016626 todo = func_hashtab.ht_used;
16627 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016628 {
16629 if (!HASHITEM_EMPTY(hi))
16630 {
16631 --todo;
16632 fp = HI2UF(hi);
16633 if (!isdigit(*fp->uf_name))
16634 list_func_head(fp, FALSE);
16635 }
16636 }
16637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638 eap->nextcmd = check_nextcmd(eap->arg);
16639 return;
16640 }
16641
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016642 /*
16643 * Get the function name. There are these situations:
16644 * func normal function name
16645 * "name" == func, "fudi.fd_dict" == NULL
16646 * dict.func new dictionary entry
16647 * "name" == NULL, "fudi.fd_dict" set,
16648 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16649 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016650 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016651 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16652 * dict.func existing dict entry that's not a Funcref
16653 * "name" == NULL, "fudi.fd_dict" set,
16654 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016657 name = trans_function_name(&p, eap->skip, 0, &fudi);
16658 paren = (vim_strchr(p, '(') != NULL);
16659 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660 {
16661 /*
16662 * Return on an invalid expression in braces, unless the expression
16663 * evaluation has been cancelled due to an aborting error, an
16664 * interrupt, or an exception.
16665 */
16666 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016667 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016668 if (!eap->skip && fudi.fd_newkey != NULL)
16669 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016670 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673 else
16674 eap->skip = TRUE;
16675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676 /* An error in a function call during evaluation of an expression in magic
16677 * braces should not cause the function not to be defined. */
16678 saved_did_emsg = did_emsg;
16679 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680
16681 /*
16682 * ":function func" with only function name: list function.
16683 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016684 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685 {
16686 if (!ends_excmd(*skipwhite(p)))
16687 {
16688 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016689 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016690 }
16691 eap->nextcmd = check_nextcmd(p);
16692 if (eap->nextcmd != NULL)
16693 *p = NUL;
16694 if (!eap->skip && !got_int)
16695 {
16696 fp = find_func(name);
16697 if (fp != NULL)
16698 {
16699 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016700 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016701 {
16702 msg_putchar('\n');
16703 msg_outnum((long)(j + 1));
16704 if (j < 9)
16705 msg_putchar(' ');
16706 if (j < 99)
16707 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016708 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709 out_flush(); /* show a line at a time */
16710 ui_breakcheck();
16711 }
16712 if (!got_int)
16713 {
16714 msg_putchar('\n');
16715 msg_puts((char_u *)" endfunction");
16716 }
16717 }
16718 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016719 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016721 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 }
16723
16724 /*
16725 * ":function name(arg1, arg2)" Define function.
16726 */
16727 p = skipwhite(p);
16728 if (*p != '(')
16729 {
16730 if (!eap->skip)
16731 {
16732 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016733 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734 }
16735 /* attempt to continue by skipping some text */
16736 if (vim_strchr(p, '(') != NULL)
16737 p = vim_strchr(p, '(');
16738 }
16739 p = skipwhite(p + 1);
16740
16741 ga_init2(&newargs, (int)sizeof(char_u *), 3);
16742 ga_init2(&newlines, (int)sizeof(char_u *), 3);
16743
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016744 if (!eap->skip)
16745 {
16746 /* Check the name of the function. */
16747 if (name != NULL)
16748 arg = name;
16749 else
16750 arg = fudi.fd_newkey;
16751 if (arg != NULL)
16752 {
16753 if (*arg == K_SPECIAL)
16754 j = 3;
16755 else
16756 j = 0;
16757 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
16758 : eval_isnamec(arg[j])))
16759 ++j;
16760 if (arg[j] != NUL)
16761 emsg_funcname(_(e_invarg2), arg);
16762 }
16763 }
16764
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765 /*
16766 * Isolate the arguments: "arg1, arg2, ...)"
16767 */
16768 while (*p != ')')
16769 {
16770 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
16771 {
16772 varargs = TRUE;
16773 p += 3;
16774 mustend = TRUE;
16775 }
16776 else
16777 {
16778 arg = p;
16779 while (ASCII_ISALNUM(*p) || *p == '_')
16780 ++p;
16781 if (arg == p || isdigit(*arg)
16782 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
16783 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
16784 {
16785 if (!eap->skip)
16786 EMSG2(_("E125: Illegal argument: %s"), arg);
16787 break;
16788 }
16789 if (ga_grow(&newargs, 1) == FAIL)
16790 goto erret;
16791 c = *p;
16792 *p = NUL;
16793 arg = vim_strsave(arg);
16794 if (arg == NULL)
16795 goto erret;
16796 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
16797 *p = c;
16798 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 if (*p == ',')
16800 ++p;
16801 else
16802 mustend = TRUE;
16803 }
16804 p = skipwhite(p);
16805 if (mustend && *p != ')')
16806 {
16807 if (!eap->skip)
16808 EMSG2(_(e_invarg2), eap->arg);
16809 break;
16810 }
16811 }
16812 ++p; /* skip the ')' */
16813
Bram Moolenaare9a41262005-01-15 22:18:47 +000016814 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 for (;;)
16816 {
16817 p = skipwhite(p);
16818 if (STRNCMP(p, "range", 5) == 0)
16819 {
16820 flags |= FC_RANGE;
16821 p += 5;
16822 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016823 else if (STRNCMP(p, "dict", 4) == 0)
16824 {
16825 flags |= FC_DICT;
16826 p += 4;
16827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828 else if (STRNCMP(p, "abort", 5) == 0)
16829 {
16830 flags |= FC_ABORT;
16831 p += 5;
16832 }
16833 else
16834 break;
16835 }
16836
16837 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
16838 EMSG(_(e_trailing));
16839
16840 /*
16841 * Read the body of the function, until ":endfunction" is found.
16842 */
16843 if (KeyTyped)
16844 {
16845 /* Check if the function already exists, don't let the user type the
16846 * whole function before telling him it doesn't work! For a script we
16847 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016848 if (!eap->skip && !eap->forceit)
16849 {
16850 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
16851 EMSG(_(e_funcdict));
16852 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016853 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016856 if (!eap->skip && did_emsg)
16857 goto erret;
16858
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859 msg_putchar('\n'); /* don't overwrite the function name */
16860 cmdline_row = msg_row;
16861 }
16862
16863 indent = 2;
16864 nesting = 0;
16865 for (;;)
16866 {
16867 msg_scroll = TRUE;
16868 need_wait_return = FALSE;
16869 if (eap->getline == NULL)
16870 theline = getcmdline(':', 0L, indent);
16871 else
16872 theline = eap->getline(':', eap->cookie, indent);
16873 if (KeyTyped)
16874 lines_left = Rows - 1;
16875 if (theline == NULL)
16876 {
16877 EMSG(_("E126: Missing :endfunction"));
16878 goto erret;
16879 }
16880
16881 if (skip_until != NULL)
16882 {
16883 /* between ":append" and "." and between ":python <<EOF" and "EOF"
16884 * don't check for ":endfunc". */
16885 if (STRCMP(theline, skip_until) == 0)
16886 {
16887 vim_free(skip_until);
16888 skip_until = NULL;
16889 }
16890 }
16891 else
16892 {
16893 /* skip ':' and blanks*/
16894 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
16895 ;
16896
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016897 /* Check for "endfunction". */
16898 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899 {
16900 vim_free(theline);
16901 break;
16902 }
16903
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016904 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905 * at "end". */
16906 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
16907 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016908 else if (STRNCMP(p, "if", 2) == 0
16909 || STRNCMP(p, "wh", 2) == 0
16910 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000016911 || STRNCMP(p, "try", 3) == 0)
16912 indent += 2;
16913
16914 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016915 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016917 if (*p == '!')
16918 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919 p += eval_fname_script(p);
16920 if (ASCII_ISALPHA(*p))
16921 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016922 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016923 if (*skipwhite(p) == '(')
16924 {
16925 ++nesting;
16926 indent += 2;
16927 }
16928 }
16929 }
16930
16931 /* Check for ":append" or ":insert". */
16932 p = skip_range(p, NULL);
16933 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
16934 || (p[0] == 'i'
16935 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
16936 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
16937 skip_until = vim_strsave((char_u *)".");
16938
16939 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
16940 arg = skipwhite(skiptowhite(p));
16941 if (arg[0] == '<' && arg[1] =='<'
16942 && ((p[0] == 'p' && p[1] == 'y'
16943 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
16944 || (p[0] == 'p' && p[1] == 'e'
16945 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
16946 || (p[0] == 't' && p[1] == 'c'
16947 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
16948 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
16949 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000016950 || (p[0] == 'm' && p[1] == 'z'
16951 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952 ))
16953 {
16954 /* ":python <<" continues until a dot, like ":append" */
16955 p = skipwhite(arg + 2);
16956 if (*p == NUL)
16957 skip_until = vim_strsave((char_u *)".");
16958 else
16959 skip_until = vim_strsave(p);
16960 }
16961 }
16962
16963 /* Add the line to the function. */
16964 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016965 {
16966 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016968 }
16969
16970 /* Copy the line to newly allocated memory. get_one_sourceline()
16971 * allocates 250 bytes per line, this saves 80% on average. The cost
16972 * is an extra alloc/free. */
16973 p = vim_strsave(theline);
16974 if (p != NULL)
16975 {
16976 vim_free(theline);
16977 theline = p;
16978 }
16979
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
16981 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982 }
16983
16984 /* Don't define the function when skipping commands or when an error was
16985 * detected. */
16986 if (eap->skip || did_emsg)
16987 goto erret;
16988
16989 /*
16990 * If there are no errors, add the function
16991 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016992 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016993 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016994 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000016995 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016996 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016997 emsg_funcname("E707: Function name conflicts with variable: %s",
16998 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016999 goto erret;
17000 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017001
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017002 fp = find_func(name);
17003 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017005 if (!eap->forceit)
17006 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017007 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017008 goto erret;
17009 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017010 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017011 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017012 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017013 name);
17014 goto erret;
17015 }
17016 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017017 ga_clear_strings(&(fp->uf_args));
17018 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017019 vim_free(name);
17020 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022 }
17023 else
17024 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017025 char numbuf[20];
17026
17027 fp = NULL;
17028 if (fudi.fd_newkey == NULL && !eap->forceit)
17029 {
17030 EMSG(_(e_funcdict));
17031 goto erret;
17032 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017033 if (fudi.fd_di == NULL)
17034 {
17035 /* Can't add a function to a locked dictionary */
17036 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17037 goto erret;
17038 }
17039 /* Can't change an existing function if it is locked */
17040 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17041 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017042
17043 /* Give the function a sequential number. Can only be used with a
17044 * Funcref! */
17045 vim_free(name);
17046 sprintf(numbuf, "%d", ++func_nr);
17047 name = vim_strsave((char_u *)numbuf);
17048 if (name == NULL)
17049 goto erret;
17050 }
17051
17052 if (fp == NULL)
17053 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017054 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017055 {
17056 int slen, plen;
17057 char_u *scriptname;
17058
17059 /* Check that the autoload name matches the script name. */
17060 j = FAIL;
17061 if (sourcing_name != NULL)
17062 {
17063 scriptname = autoload_name(name);
17064 if (scriptname != NULL)
17065 {
17066 p = vim_strchr(scriptname, '/');
17067 plen = STRLEN(p);
17068 slen = STRLEN(sourcing_name);
17069 if (slen > plen && fnamecmp(p,
17070 sourcing_name + slen - plen) == 0)
17071 j = OK;
17072 vim_free(scriptname);
17073 }
17074 }
17075 if (j == FAIL)
17076 {
17077 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17078 goto erret;
17079 }
17080 }
17081
17082 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083 if (fp == NULL)
17084 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017085
17086 if (fudi.fd_dict != NULL)
17087 {
17088 if (fudi.fd_di == NULL)
17089 {
17090 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017091 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017092 if (fudi.fd_di == NULL)
17093 {
17094 vim_free(fp);
17095 goto erret;
17096 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017097 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17098 {
17099 vim_free(fudi.fd_di);
17100 goto erret;
17101 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017102 }
17103 else
17104 /* overwrite existing dict entry */
17105 clear_tv(&fudi.fd_di->di_tv);
17106 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017107 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017108 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017109 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017110 }
17111
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017113 STRCPY(fp->uf_name, name);
17114 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017116 fp->uf_args = newargs;
17117 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017118#ifdef FEAT_PROFILE
17119 fp->uf_tml_count = NULL;
17120 fp->uf_tml_total = NULL;
17121 fp->uf_tml_self = NULL;
17122 fp->uf_profiling = FALSE;
17123 if (prof_def_func())
17124 func_do_profile(fp);
17125#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017126 fp->uf_varargs = varargs;
17127 fp->uf_flags = flags;
17128 fp->uf_calls = 0;
17129 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017130 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131
17132erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017133 ga_clear_strings(&newargs);
17134 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017135ret_free:
17136 vim_free(skip_until);
17137 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140}
17141
17142/*
17143 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017144 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017146 * flags:
17147 * TFN_INT: internal function name OK
17148 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149 * Advances "pp" to just after the function name (if no error).
17150 */
17151 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017152trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153 char_u **pp;
17154 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017155 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017156 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017158 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159 char_u *start;
17160 char_u *end;
17161 int lead;
17162 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017164 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017165
17166 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017167 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017168 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017169
17170 /* Check for hard coded <SNR>: already translated function ID (from a user
17171 * command). */
17172 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17173 && (*pp)[2] == (int)KE_SNR)
17174 {
17175 *pp += 3;
17176 len = get_id_len(pp) + 3;
17177 return vim_strnsave(start, len);
17178 }
17179
17180 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17181 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017183 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017185
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017186 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17187 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017188 if (end == start)
17189 {
17190 if (!skip)
17191 EMSG(_("E129: Function name required"));
17192 goto theend;
17193 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017194 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017195 {
17196 /*
17197 * Report an invalid expression in braces, unless the expression
17198 * evaluation has been cancelled due to an aborting error, an
17199 * interrupt, or an exception.
17200 */
17201 if (!aborting())
17202 {
17203 if (end != NULL)
17204 EMSG2(_(e_invarg2), start);
17205 }
17206 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017207 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017208 goto theend;
17209 }
17210
17211 if (lv.ll_tv != NULL)
17212 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017213 if (fdp != NULL)
17214 {
17215 fdp->fd_dict = lv.ll_dict;
17216 fdp->fd_newkey = lv.ll_newkey;
17217 lv.ll_newkey = NULL;
17218 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017219 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017220 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17221 {
17222 name = vim_strsave(lv.ll_tv->vval.v_string);
17223 *pp = end;
17224 }
17225 else
17226 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017227 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17228 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017229 EMSG(_(e_funcref));
17230 else
17231 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017232 name = NULL;
17233 }
17234 goto theend;
17235 }
17236
17237 if (lv.ll_name == NULL)
17238 {
17239 /* Error found, but continue after the function name. */
17240 *pp = end;
17241 goto theend;
17242 }
17243
17244 if (lv.ll_exp_name != NULL)
17245 len = STRLEN(lv.ll_exp_name);
17246 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017247 {
17248 if (lead == 2) /* skip over "s:" */
17249 lv.ll_name += 2;
17250 len = (int)(end - lv.ll_name);
17251 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017252
17253 /*
17254 * Copy the function name to allocated memory.
17255 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17256 * Accept <SNR>123_name() outside a script.
17257 */
17258 if (skip)
17259 lead = 0; /* do nothing */
17260 else if (lead > 0)
17261 {
17262 lead = 3;
17263 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17264 {
17265 if (current_SID <= 0)
17266 {
17267 EMSG(_(e_usingsid));
17268 goto theend;
17269 }
17270 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17271 lead += (int)STRLEN(sid_buf);
17272 }
17273 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017274 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017275 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017276 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017277 goto theend;
17278 }
17279 name = alloc((unsigned)(len + lead + 1));
17280 if (name != NULL)
17281 {
17282 if (lead > 0)
17283 {
17284 name[0] = K_SPECIAL;
17285 name[1] = KS_EXTRA;
17286 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017287 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017288 STRCPY(name + 3, sid_buf);
17289 }
17290 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17291 name[len + lead] = NUL;
17292 }
17293 *pp = end;
17294
17295theend:
17296 clear_lval(&lv);
17297 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298}
17299
17300/*
17301 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17302 * Return 2 if "p" starts with "s:".
17303 * Return 0 otherwise.
17304 */
17305 static int
17306eval_fname_script(p)
17307 char_u *p;
17308{
17309 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17310 || STRNICMP(p + 1, "SNR>", 4) == 0))
17311 return 5;
17312 if (p[0] == 's' && p[1] == ':')
17313 return 2;
17314 return 0;
17315}
17316
17317/*
17318 * Return TRUE if "p" starts with "<SID>" or "s:".
17319 * Only works if eval_fname_script() returned non-zero for "p"!
17320 */
17321 static int
17322eval_fname_sid(p)
17323 char_u *p;
17324{
17325 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17326}
17327
17328/*
17329 * List the head of the function: "name(arg1, arg2)".
17330 */
17331 static void
17332list_func_head(fp, indent)
17333 ufunc_T *fp;
17334 int indent;
17335{
17336 int j;
17337
17338 msg_start();
17339 if (indent)
17340 MSG_PUTS(" ");
17341 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017342 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343 {
17344 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017345 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346 }
17347 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017348 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017350 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 {
17352 if (j)
17353 MSG_PUTS(", ");
17354 msg_puts(FUNCARG(fp, j));
17355 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017356 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 {
17358 if (j)
17359 MSG_PUTS(", ");
17360 MSG_PUTS("...");
17361 }
17362 msg_putchar(')');
17363}
17364
17365/*
17366 * Find a function by name, return pointer to it in ufuncs.
17367 * Return NULL for unknown function.
17368 */
17369 static ufunc_T *
17370find_func(name)
17371 char_u *name;
17372{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017373 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017375 hi = hash_find(&func_hashtab, name);
17376 if (!HASHITEM_EMPTY(hi))
17377 return HI2UF(hi);
17378 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379}
17380
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017381#if defined(EXITFREE) || defined(PROTO)
17382 void
17383free_all_functions()
17384{
17385 hashitem_T *hi;
17386
17387 /* Need to start all over every time, because func_free() may change the
17388 * hash table. */
17389 while (func_hashtab.ht_used > 0)
17390 for (hi = func_hashtab.ht_array; ; ++hi)
17391 if (!HASHITEM_EMPTY(hi))
17392 {
17393 func_free(HI2UF(hi));
17394 break;
17395 }
17396}
17397#endif
17398
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017399/*
17400 * Return TRUE if a function "name" exists.
17401 */
17402 static int
17403function_exists(name)
17404 char_u *name;
17405{
17406 char_u *p = name;
17407 int n = FALSE;
17408
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017409 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017410 if (p != NULL)
17411 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017412 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017413 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017414 else
17415 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017416 vim_free(p);
17417 }
17418 return n;
17419}
17420
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017421/*
17422 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017423 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017424 */
17425 static int
17426builtin_function(name)
17427 char_u *name;
17428{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017429 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17430 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017431}
17432
Bram Moolenaar05159a02005-02-26 23:04:13 +000017433#if defined(FEAT_PROFILE) || defined(PROTO)
17434/*
17435 * Start profiling function "fp".
17436 */
17437 static void
17438func_do_profile(fp)
17439 ufunc_T *fp;
17440{
17441 fp->uf_tm_count = 0;
17442 profile_zero(&fp->uf_tm_self);
17443 profile_zero(&fp->uf_tm_total);
17444 if (fp->uf_tml_count == NULL)
17445 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17446 (sizeof(int) * fp->uf_lines.ga_len));
17447 if (fp->uf_tml_total == NULL)
17448 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17449 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17450 if (fp->uf_tml_self == NULL)
17451 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17452 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17453 fp->uf_tml_idx = -1;
17454 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17455 || fp->uf_tml_self == NULL)
17456 return; /* out of memory */
17457
17458 fp->uf_profiling = TRUE;
17459}
17460
17461/*
17462 * Dump the profiling results for all functions in file "fd".
17463 */
17464 void
17465func_dump_profile(fd)
17466 FILE *fd;
17467{
17468 hashitem_T *hi;
17469 int todo;
17470 ufunc_T *fp;
17471 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017472 ufunc_T **sorttab;
17473 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017474
17475 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017476 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17477
Bram Moolenaar05159a02005-02-26 23:04:13 +000017478 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17479 {
17480 if (!HASHITEM_EMPTY(hi))
17481 {
17482 --todo;
17483 fp = HI2UF(hi);
17484 if (fp->uf_profiling)
17485 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017486 if (sorttab != NULL)
17487 sorttab[st_len++] = fp;
17488
Bram Moolenaar05159a02005-02-26 23:04:13 +000017489 if (fp->uf_name[0] == K_SPECIAL)
17490 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17491 else
17492 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17493 if (fp->uf_tm_count == 1)
17494 fprintf(fd, "Called 1 time\n");
17495 else
17496 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17497 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17498 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17499 fprintf(fd, "\n");
17500 fprintf(fd, "count total (s) self (s)\n");
17501
17502 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17503 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017504 prof_func_line(fd, fp->uf_tml_count[i],
17505 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017506 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17507 }
17508 fprintf(fd, "\n");
17509 }
17510 }
17511 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017512
17513 if (sorttab != NULL && st_len > 0)
17514 {
17515 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17516 prof_total_cmp);
17517 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17518 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17519 prof_self_cmp);
17520 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17521 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017522}
Bram Moolenaar73830342005-02-28 22:48:19 +000017523
17524 static void
17525prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17526 FILE *fd;
17527 ufunc_T **sorttab;
17528 int st_len;
17529 char *title;
17530 int prefer_self; /* when equal print only self time */
17531{
17532 int i;
17533 ufunc_T *fp;
17534
17535 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17536 fprintf(fd, "count total (s) self (s) function\n");
17537 for (i = 0; i < 20 && i < st_len; ++i)
17538 {
17539 fp = sorttab[i];
17540 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17541 prefer_self);
17542 if (fp->uf_name[0] == K_SPECIAL)
17543 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17544 else
17545 fprintf(fd, " %s()\n", fp->uf_name);
17546 }
17547 fprintf(fd, "\n");
17548}
17549
17550/*
17551 * Print the count and times for one function or function line.
17552 */
17553 static void
17554prof_func_line(fd, count, total, self, prefer_self)
17555 FILE *fd;
17556 int count;
17557 proftime_T *total;
17558 proftime_T *self;
17559 int prefer_self; /* when equal print only self time */
17560{
17561 if (count > 0)
17562 {
17563 fprintf(fd, "%5d ", count);
17564 if (prefer_self && profile_equal(total, self))
17565 fprintf(fd, " ");
17566 else
17567 fprintf(fd, "%s ", profile_msg(total));
17568 if (!prefer_self && profile_equal(total, self))
17569 fprintf(fd, " ");
17570 else
17571 fprintf(fd, "%s ", profile_msg(self));
17572 }
17573 else
17574 fprintf(fd, " ");
17575}
17576
17577/*
17578 * Compare function for total time sorting.
17579 */
17580 static int
17581#ifdef __BORLANDC__
17582_RTLENTRYF
17583#endif
17584prof_total_cmp(s1, s2)
17585 const void *s1;
17586 const void *s2;
17587{
17588 ufunc_T *p1, *p2;
17589
17590 p1 = *(ufunc_T **)s1;
17591 p2 = *(ufunc_T **)s2;
17592 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17593}
17594
17595/*
17596 * Compare function for self time sorting.
17597 */
17598 static int
17599#ifdef __BORLANDC__
17600_RTLENTRYF
17601#endif
17602prof_self_cmp(s1, s2)
17603 const void *s1;
17604 const void *s2;
17605{
17606 ufunc_T *p1, *p2;
17607
17608 p1 = *(ufunc_T **)s1;
17609 p2 = *(ufunc_T **)s2;
17610 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17611}
17612
Bram Moolenaar05159a02005-02-26 23:04:13 +000017613#endif
17614
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017615/* The names of packages that once were loaded is remembered. */
17616static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
17617
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017618/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017619 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017620 * Return TRUE if a package was loaded.
17621 */
17622 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017623script_autoload(name, reload)
17624 char_u *name;
17625 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017626{
17627 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017628 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017629 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017630 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017631
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017632 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017633 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017634 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017635 return FALSE;
17636
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017637 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017638
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017639 /* Find the name in the list of previously loaded package names. Skip
17640 * "autoload/", it's always the same. */
17641 for (i = 0; i < ga_loaded.ga_len; ++i)
17642 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
17643 break;
17644 if (!reload && i < ga_loaded.ga_len)
17645 ret = FALSE; /* was loaded already */
17646 else
17647 {
17648 /* Remember the name if it wasn't loaded already. */
17649 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
17650 {
17651 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
17652 tofree = NULL;
17653 }
17654
17655 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
17656 if (cmd_runtime(scriptname, FALSE) == OK)
17657 ret = TRUE;
17658 }
17659
17660 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017661 return ret;
17662}
17663
17664/*
17665 * Return the autoload script name for a function or variable name.
17666 * Returns NULL when out of memory.
17667 */
17668 static char_u *
17669autoload_name(name)
17670 char_u *name;
17671{
17672 char_u *p;
17673 char_u *scriptname;
17674
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017675 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017676 scriptname = alloc((unsigned)(STRLEN(name) + 14));
17677 if (scriptname == NULL)
17678 return FALSE;
17679 STRCPY(scriptname, "autoload/");
17680 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017681 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017682 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017683 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017684 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017685 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017686}
17687
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
17689
17690/*
17691 * Function given to ExpandGeneric() to obtain the list of user defined
17692 * function names.
17693 */
17694 char_u *
17695get_user_func_name(xp, idx)
17696 expand_T *xp;
17697 int idx;
17698{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017699 static long_u done;
17700 static hashitem_T *hi;
17701 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702
17703 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017705 done = 0;
17706 hi = func_hashtab.ht_array;
17707 }
17708 if (done < func_hashtab.ht_used)
17709 {
17710 if (done++ > 0)
17711 ++hi;
17712 while (HASHITEM_EMPTY(hi))
17713 ++hi;
17714 fp = HI2UF(hi);
17715
17716 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
17717 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718
17719 cat_func_name(IObuff, fp);
17720 if (xp->xp_context != EXPAND_USER_FUNC)
17721 {
17722 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017723 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724 STRCAT(IObuff, ")");
17725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 return IObuff;
17727 }
17728 return NULL;
17729}
17730
17731#endif /* FEAT_CMDL_COMPL */
17732
17733/*
17734 * Copy the function name of "fp" to buffer "buf".
17735 * "buf" must be able to hold the function name plus three bytes.
17736 * Takes care of script-local function names.
17737 */
17738 static void
17739cat_func_name(buf, fp)
17740 char_u *buf;
17741 ufunc_T *fp;
17742{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017743 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 {
17745 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017746 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 }
17748 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017749 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750}
17751
17752/*
17753 * ":delfunction {name}"
17754 */
17755 void
17756ex_delfunction(eap)
17757 exarg_T *eap;
17758{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017759 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 char_u *p;
17761 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017762 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017763
17764 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017765 name = trans_function_name(&p, eap->skip, 0, &fudi);
17766 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017768 {
17769 if (fudi.fd_dict != NULL && !eap->skip)
17770 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 if (!ends_excmd(*skipwhite(p)))
17774 {
17775 vim_free(name);
17776 EMSG(_(e_trailing));
17777 return;
17778 }
17779 eap->nextcmd = check_nextcmd(p);
17780 if (eap->nextcmd != NULL)
17781 *p = NUL;
17782
17783 if (!eap->skip)
17784 fp = find_func(name);
17785 vim_free(name);
17786
17787 if (!eap->skip)
17788 {
17789 if (fp == NULL)
17790 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017791 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792 return;
17793 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017794 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795 {
17796 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
17797 return;
17798 }
17799
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017800 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017802 /* Delete the dict item that refers to the function, it will
17803 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017804 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017806 else
17807 func_free(fp);
17808 }
17809}
17810
17811/*
17812 * Free a function and remove it from the list of functions.
17813 */
17814 static void
17815func_free(fp)
17816 ufunc_T *fp;
17817{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017818 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017819
17820 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017821 ga_clear_strings(&(fp->uf_args));
17822 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000017823#ifdef FEAT_PROFILE
17824 vim_free(fp->uf_tml_count);
17825 vim_free(fp->uf_tml_total);
17826 vim_free(fp->uf_tml_self);
17827#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017828
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017829 /* remove the function from the function hashtable */
17830 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
17831 if (HASHITEM_EMPTY(hi))
17832 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017833 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017834 hash_remove(&func_hashtab, hi);
17835
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017836 vim_free(fp);
17837}
17838
17839/*
17840 * Unreference a Function: decrement the reference count and free it when it
17841 * becomes zero. Only for numbered functions.
17842 */
17843 static void
17844func_unref(name)
17845 char_u *name;
17846{
17847 ufunc_T *fp;
17848
17849 if (name != NULL && isdigit(*name))
17850 {
17851 fp = find_func(name);
17852 if (fp == NULL)
17853 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017854 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017855 {
17856 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017857 * when "uf_calls" becomes zero. */
17858 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017859 func_free(fp);
17860 }
17861 }
17862}
17863
17864/*
17865 * Count a reference to a Function.
17866 */
17867 static void
17868func_ref(name)
17869 char_u *name;
17870{
17871 ufunc_T *fp;
17872
17873 if (name != NULL && isdigit(*name))
17874 {
17875 fp = find_func(name);
17876 if (fp == NULL)
17877 EMSG2(_(e_intern2), "func_ref()");
17878 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017879 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880 }
17881}
17882
17883/*
17884 * Call a user function.
17885 */
17886 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000017887call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017888 ufunc_T *fp; /* pointer to function */
17889 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000017890 typval_T *argvars; /* arguments */
17891 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 linenr_T firstline; /* first line of range */
17893 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000017894 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895{
Bram Moolenaar33570922005-01-25 22:26:29 +000017896 char_u *save_sourcing_name;
17897 linenr_T save_sourcing_lnum;
17898 scid_T save_current_SID;
17899 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000017900 int save_did_emsg;
17901 static int depth = 0;
17902 dictitem_T *v;
17903 int fixvar_idx = 0; /* index in fixvar[] */
17904 int i;
17905 int ai;
17906 char_u numbuf[NUMBUFLEN];
17907 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017908#ifdef FEAT_PROFILE
17909 proftime_T wait_start;
17910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911
17912 /* If depth of calling is getting too high, don't execute the function */
17913 if (depth >= p_mfd)
17914 {
17915 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017916 rettv->v_type = VAR_NUMBER;
17917 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918 return;
17919 }
17920 ++depth;
17921
17922 line_breakcheck(); /* check for CTRL-C hit */
17923
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017924 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000017925 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017927 fc.rettv = rettv;
17928 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929 fc.linenr = 0;
17930 fc.returned = FALSE;
17931 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017933 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934 fc.dbg_tick = debug_tick;
17935
Bram Moolenaar33570922005-01-25 22:26:29 +000017936 /*
17937 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
17938 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
17939 * each argument variable and saves a lot of time.
17940 */
17941 /*
17942 * Init l: variables.
17943 */
17944 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000017945 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017946 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017947 /* Set l:self to "selfdict". */
17948 v = &fc.fixvar[fixvar_idx++].var;
17949 STRCPY(v->di_key, "self");
17950 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
17951 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
17952 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017953 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000017954 v->di_tv.vval.v_dict = selfdict;
17955 ++selfdict->dv_refcount;
17956 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017957
Bram Moolenaar33570922005-01-25 22:26:29 +000017958 /*
17959 * Init a: variables.
17960 * Set a:0 to "argcount".
17961 * Set a:000 to a list with room for the "..." arguments.
17962 */
17963 init_var_dict(&fc.l_avars, &fc.l_avars_var);
17964 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017965 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000017966 v = &fc.fixvar[fixvar_idx++].var;
17967 STRCPY(v->di_key, "000");
17968 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17969 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17970 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017971 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017972 v->di_tv.vval.v_list = &fc.l_varlist;
17973 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
17974 fc.l_varlist.lv_refcount = 99999;
17975
17976 /*
17977 * Set a:firstline to "firstline" and a:lastline to "lastline".
17978 * Set a:name to named arguments.
17979 * Set a:N to the "..." arguments.
17980 */
17981 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
17982 (varnumber_T)firstline);
17983 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
17984 (varnumber_T)lastline);
17985 for (i = 0; i < argcount; ++i)
17986 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017987 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017988 if (ai < 0)
17989 /* named argument a:name */
17990 name = FUNCARG(fp, i);
17991 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017992 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017993 /* "..." argument a:1, a:2, etc. */
17994 sprintf((char *)numbuf, "%d", ai + 1);
17995 name = numbuf;
17996 }
17997 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
17998 {
17999 v = &fc.fixvar[fixvar_idx++].var;
18000 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18001 }
18002 else
18003 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018004 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18005 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018006 if (v == NULL)
18007 break;
18008 v->di_flags = DI_FLAGS_RO;
18009 }
18010 STRCPY(v->di_key, name);
18011 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18012
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018013 /* Note: the values are copied directly to avoid alloc/free.
18014 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018015 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018016 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018017
18018 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18019 {
18020 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18021 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018022 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018023 }
18024 }
18025
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026 /* Don't redraw while executing the function. */
18027 ++RedrawingDisabled;
18028 save_sourcing_name = sourcing_name;
18029 save_sourcing_lnum = sourcing_lnum;
18030 sourcing_lnum = 1;
18031 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018032 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 if (sourcing_name != NULL)
18034 {
18035 if (save_sourcing_name != NULL
18036 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18037 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18038 else
18039 STRCPY(sourcing_name, "function ");
18040 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18041
18042 if (p_verbose >= 12)
18043 {
18044 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018045 verbose_enter_scroll();
18046
Bram Moolenaar555b2802005-05-19 21:08:39 +000018047 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 if (p_verbose >= 14)
18049 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018051 char_u numbuf[NUMBUFLEN];
18052 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053
18054 msg_puts((char_u *)"(");
18055 for (i = 0; i < argcount; ++i)
18056 {
18057 if (i > 0)
18058 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018059 if (argvars[i].v_type == VAR_NUMBER)
18060 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061 else
18062 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018063 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018064 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018066 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067 }
18068 }
18069 msg_puts((char_u *)")");
18070 }
18071 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018072
18073 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 --no_wait_return;
18075 }
18076 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018077#ifdef FEAT_PROFILE
18078 if (do_profiling)
18079 {
18080 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18081 func_do_profile(fp);
18082 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018083 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018084 {
18085 ++fp->uf_tm_count;
18086 profile_start(&fp->uf_tm_start);
18087 profile_zero(&fp->uf_tm_children);
18088 }
18089 script_prof_save(&wait_start);
18090 }
18091#endif
18092
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018094 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095 save_did_emsg = did_emsg;
18096 did_emsg = FALSE;
18097
18098 /* call do_cmdline() to execute the lines */
18099 do_cmdline(NULL, get_func_line, (void *)&fc,
18100 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18101
18102 --RedrawingDisabled;
18103
18104 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018105 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018107 clear_tv(rettv);
18108 rettv->v_type = VAR_NUMBER;
18109 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110 }
18111
Bram Moolenaar05159a02005-02-26 23:04:13 +000018112#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018113 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018114 {
18115 profile_end(&fp->uf_tm_start);
18116 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18117 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18118 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18119 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018120 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018121 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018122 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18123 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018124 }
18125 }
18126#endif
18127
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128 /* when being verbose, mention the return value */
18129 if (p_verbose >= 12)
18130 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018132 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018135 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018136 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018137 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18138 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018139 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018141 char_u buf[MSG_BUF_LEN];
18142 char_u numbuf[NUMBUFLEN];
18143 char_u *tofree;
18144
Bram Moolenaar555b2802005-05-19 21:08:39 +000018145 /* The value may be very long. Skip the middle part, so that we
18146 * have some idea how it starts and ends. smsg() would always
18147 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018148 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018149 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018150 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018151 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152 }
18153 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 vim_free(sourcing_name);
18160 sourcing_name = save_sourcing_name;
18161 sourcing_lnum = save_sourcing_lnum;
18162 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018163#ifdef FEAT_PROFILE
18164 if (do_profiling)
18165 script_prof_restore(&wait_start);
18166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167
18168 if (p_verbose >= 12 && sourcing_name != NULL)
18169 {
18170 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018171 verbose_enter_scroll();
18172
Bram Moolenaar555b2802005-05-19 21:08:39 +000018173 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018175
18176 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177 --no_wait_return;
18178 }
18179
18180 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018181 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182
Bram Moolenaar33570922005-01-25 22:26:29 +000018183 /* The a: variables typevals were not alloced, only free the allocated
18184 * variables. */
18185 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18186
18187 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188 --depth;
18189}
18190
18191/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018192 * Add a number variable "name" to dict "dp" with value "nr".
18193 */
18194 static void
18195add_nr_var(dp, v, name, nr)
18196 dict_T *dp;
18197 dictitem_T *v;
18198 char *name;
18199 varnumber_T nr;
18200{
18201 STRCPY(v->di_key, name);
18202 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18203 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18204 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018205 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018206 v->di_tv.vval.v_number = nr;
18207}
18208
18209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018210 * ":return [expr]"
18211 */
18212 void
18213ex_return(eap)
18214 exarg_T *eap;
18215{
18216 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018217 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218 int returning = FALSE;
18219
18220 if (current_funccal == NULL)
18221 {
18222 EMSG(_("E133: :return not inside a function"));
18223 return;
18224 }
18225
18226 if (eap->skip)
18227 ++emsg_skip;
18228
18229 eap->nextcmd = NULL;
18230 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018231 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232 {
18233 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018234 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018236 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237 }
18238 /* It's safer to return also on error. */
18239 else if (!eap->skip)
18240 {
18241 /*
18242 * Return unless the expression evaluation has been cancelled due to an
18243 * aborting error, an interrupt, or an exception.
18244 */
18245 if (!aborting())
18246 returning = do_return(eap, FALSE, TRUE, NULL);
18247 }
18248
18249 /* When skipping or the return gets pending, advance to the next command
18250 * in this line (!returning). Otherwise, ignore the rest of the line.
18251 * Following lines will be ignored by get_func_line(). */
18252 if (returning)
18253 eap->nextcmd = NULL;
18254 else if (eap->nextcmd == NULL) /* no argument */
18255 eap->nextcmd = check_nextcmd(arg);
18256
18257 if (eap->skip)
18258 --emsg_skip;
18259}
18260
18261/*
18262 * Return from a function. Possibly makes the return pending. Also called
18263 * for a pending return at the ":endtry" or after returning from an extra
18264 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018265 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018266 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267 * FALSE when the return gets pending.
18268 */
18269 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018270do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 exarg_T *eap;
18272 int reanimate;
18273 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018274 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275{
18276 int idx;
18277 struct condstack *cstack = eap->cstack;
18278
18279 if (reanimate)
18280 /* Undo the return. */
18281 current_funccal->returned = FALSE;
18282
18283 /*
18284 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18285 * not in its finally clause (which then is to be executed next) is found.
18286 * In this case, make the ":return" pending for execution at the ":endtry".
18287 * Otherwise, return normally.
18288 */
18289 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18290 if (idx >= 0)
18291 {
18292 cstack->cs_pending[idx] = CSTP_RETURN;
18293
18294 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018295 /* A pending return again gets pending. "rettv" points to an
18296 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018298 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299 else
18300 {
18301 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018302 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018304 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018306 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307 {
18308 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018309 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018310 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311 else
18312 EMSG(_(e_outofmem));
18313 }
18314 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018315 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316
18317 if (reanimate)
18318 {
18319 /* The pending return value could be overwritten by a ":return"
18320 * without argument in a finally clause; reset the default
18321 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018322 current_funccal->rettv->v_type = VAR_NUMBER;
18323 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324 }
18325 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018326 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 }
18328 else
18329 {
18330 current_funccal->returned = TRUE;
18331
18332 /* If the return is carried out now, store the return value. For
18333 * a return immediately after reanimation, the value is already
18334 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018335 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018337 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018338 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018340 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341 }
18342 }
18343
18344 return idx < 0;
18345}
18346
18347/*
18348 * Free the variable with a pending return value.
18349 */
18350 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018351discard_pending_return(rettv)
18352 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353{
Bram Moolenaar33570922005-01-25 22:26:29 +000018354 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355}
18356
18357/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018358 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359 * is an allocated string. Used by report_pending() for verbose messages.
18360 */
18361 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018362get_return_cmd(rettv)
18363 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018365 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018366 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018367 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018369 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018370 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018371 if (s == NULL)
18372 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018373
18374 STRCPY(IObuff, ":return ");
18375 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18376 if (STRLEN(s) + 8 >= IOSIZE)
18377 STRCPY(IObuff + IOSIZE - 4, "...");
18378 vim_free(tofree);
18379 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380}
18381
18382/*
18383 * Get next function line.
18384 * Called by do_cmdline() to get the next line.
18385 * Returns allocated string, or NULL for end of function.
18386 */
18387/* ARGSUSED */
18388 char_u *
18389get_func_line(c, cookie, indent)
18390 int c; /* not used */
18391 void *cookie;
18392 int indent; /* not used */
18393{
Bram Moolenaar33570922005-01-25 22:26:29 +000018394 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018395 ufunc_T *fp = fcp->func;
18396 char_u *retval;
18397 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398
18399 /* If breakpoints have been added/deleted need to check for it. */
18400 if (fcp->dbg_tick != debug_tick)
18401 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018402 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403 sourcing_lnum);
18404 fcp->dbg_tick = debug_tick;
18405 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018406#ifdef FEAT_PROFILE
18407 if (do_profiling)
18408 func_line_end(cookie);
18409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018410
Bram Moolenaar05159a02005-02-26 23:04:13 +000018411 gap = &fp->uf_lines;
18412 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413 retval = NULL;
18414 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18415 retval = NULL;
18416 else
18417 {
18418 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18419 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018420#ifdef FEAT_PROFILE
18421 if (do_profiling)
18422 func_line_start(cookie);
18423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424 }
18425
18426 /* Did we encounter a breakpoint? */
18427 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18428 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018429 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018430 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018431 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018432 sourcing_lnum);
18433 fcp->dbg_tick = debug_tick;
18434 }
18435
18436 return retval;
18437}
18438
Bram Moolenaar05159a02005-02-26 23:04:13 +000018439#if defined(FEAT_PROFILE) || defined(PROTO)
18440/*
18441 * Called when starting to read a function line.
18442 * "sourcing_lnum" must be correct!
18443 * When skipping lines it may not actually be executed, but we won't find out
18444 * until later and we need to store the time now.
18445 */
18446 void
18447func_line_start(cookie)
18448 void *cookie;
18449{
18450 funccall_T *fcp = (funccall_T *)cookie;
18451 ufunc_T *fp = fcp->func;
18452
18453 if (fp->uf_profiling && sourcing_lnum >= 1
18454 && sourcing_lnum <= fp->uf_lines.ga_len)
18455 {
18456 fp->uf_tml_idx = sourcing_lnum - 1;
18457 fp->uf_tml_execed = FALSE;
18458 profile_start(&fp->uf_tml_start);
18459 profile_zero(&fp->uf_tml_children);
18460 profile_get_wait(&fp->uf_tml_wait);
18461 }
18462}
18463
18464/*
18465 * Called when actually executing a function line.
18466 */
18467 void
18468func_line_exec(cookie)
18469 void *cookie;
18470{
18471 funccall_T *fcp = (funccall_T *)cookie;
18472 ufunc_T *fp = fcp->func;
18473
18474 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18475 fp->uf_tml_execed = TRUE;
18476}
18477
18478/*
18479 * Called when done with a function line.
18480 */
18481 void
18482func_line_end(cookie)
18483 void *cookie;
18484{
18485 funccall_T *fcp = (funccall_T *)cookie;
18486 ufunc_T *fp = fcp->func;
18487
18488 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18489 {
18490 if (fp->uf_tml_execed)
18491 {
18492 ++fp->uf_tml_count[fp->uf_tml_idx];
18493 profile_end(&fp->uf_tml_start);
18494 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18495 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18496 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18497 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18498 }
18499 fp->uf_tml_idx = -1;
18500 }
18501}
18502#endif
18503
Bram Moolenaar071d4272004-06-13 20:20:40 +000018504/*
18505 * Return TRUE if the currently active function should be ended, because a
18506 * return was encountered or an error occured. Used inside a ":while".
18507 */
18508 int
18509func_has_ended(cookie)
18510 void *cookie;
18511{
Bram Moolenaar33570922005-01-25 22:26:29 +000018512 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018513
18514 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18515 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018516 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517 || fcp->returned);
18518}
18519
18520/*
18521 * return TRUE if cookie indicates a function which "abort"s on errors.
18522 */
18523 int
18524func_has_abort(cookie)
18525 void *cookie;
18526{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018527 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528}
18529
18530#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18531typedef enum
18532{
18533 VAR_FLAVOUR_DEFAULT,
18534 VAR_FLAVOUR_SESSION,
18535 VAR_FLAVOUR_VIMINFO
18536} var_flavour_T;
18537
18538static var_flavour_T var_flavour __ARGS((char_u *varname));
18539
18540 static var_flavour_T
18541var_flavour(varname)
18542 char_u *varname;
18543{
18544 char_u *p = varname;
18545
18546 if (ASCII_ISUPPER(*p))
18547 {
18548 while (*(++p))
18549 if (ASCII_ISLOWER(*p))
18550 return VAR_FLAVOUR_SESSION;
18551 return VAR_FLAVOUR_VIMINFO;
18552 }
18553 else
18554 return VAR_FLAVOUR_DEFAULT;
18555}
18556#endif
18557
18558#if defined(FEAT_VIMINFO) || defined(PROTO)
18559/*
18560 * Restore global vars that start with a capital from the viminfo file
18561 */
18562 int
18563read_viminfo_varlist(virp, writing)
18564 vir_T *virp;
18565 int writing;
18566{
18567 char_u *tab;
18568 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018569 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570
18571 if (!writing && (find_viminfo_parameter('!') != NULL))
18572 {
18573 tab = vim_strchr(virp->vir_line + 1, '\t');
18574 if (tab != NULL)
18575 {
18576 *tab++ = '\0'; /* isolate the variable name */
18577 if (*tab == 'S') /* string var */
18578 is_string = TRUE;
18579
18580 tab = vim_strchr(tab, '\t');
18581 if (tab != NULL)
18582 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583 if (is_string)
18584 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018585 tv.v_type = VAR_STRING;
18586 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588 }
18589 else
18590 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018591 tv.v_type = VAR_NUMBER;
18592 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018594 set_var(virp->vir_line + 1, &tv, FALSE);
18595 if (is_string)
18596 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597 }
18598 }
18599 }
18600
18601 return viminfo_readline(virp);
18602}
18603
18604/*
18605 * Write global vars that start with a capital to the viminfo file
18606 */
18607 void
18608write_viminfo_varlist(fp)
18609 FILE *fp;
18610{
Bram Moolenaar33570922005-01-25 22:26:29 +000018611 hashitem_T *hi;
18612 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018613 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018614 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018615 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018616 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018617 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618
18619 if (find_viminfo_parameter('!') == NULL)
18620 return;
18621
18622 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018623
Bram Moolenaar33570922005-01-25 22:26:29 +000018624 todo = globvarht.ht_used;
18625 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018627 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018629 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018630 this_var = HI2DI(hi);
18631 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018632 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018633 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018634 {
18635 case VAR_STRING: s = "STR"; break;
18636 case VAR_NUMBER: s = "NUM"; break;
18637 default: continue;
18638 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018639 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018640 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18641 if (p != NULL)
18642 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018643 vim_free(tofree);
18644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645 }
18646 }
18647}
18648#endif
18649
18650#if defined(FEAT_SESSION) || defined(PROTO)
18651 int
18652store_session_globals(fd)
18653 FILE *fd;
18654{
Bram Moolenaar33570922005-01-25 22:26:29 +000018655 hashitem_T *hi;
18656 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018657 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 char_u *p, *t;
18659
Bram Moolenaar33570922005-01-25 22:26:29 +000018660 todo = globvarht.ht_used;
18661 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018663 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018665 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018666 this_var = HI2DI(hi);
18667 if ((this_var->di_tv.v_type == VAR_NUMBER
18668 || this_var->di_tv.v_type == VAR_STRING)
18669 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018670 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018671 /* Escape special characters with a backslash. Turn a LF and
18672 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018673 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018674 (char_u *)"\\\"\n\r");
18675 if (p == NULL) /* out of memory */
18676 break;
18677 for (t = p; *t != NUL; ++t)
18678 if (*t == '\n')
18679 *t = 'n';
18680 else if (*t == '\r')
18681 *t = 'r';
18682 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000018683 this_var->di_key,
18684 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18685 : ' ',
18686 p,
18687 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18688 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000018689 || put_eol(fd) == FAIL)
18690 {
18691 vim_free(p);
18692 return FAIL;
18693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694 vim_free(p);
18695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 }
18697 }
18698 return OK;
18699}
18700#endif
18701
Bram Moolenaar661b1822005-07-28 22:36:45 +000018702/*
18703 * Display script name where an item was last set.
18704 * Should only be invoked when 'verbose' is non-zero.
18705 */
18706 void
18707last_set_msg(scriptID)
18708 scid_T scriptID;
18709{
18710 if (scriptID != 0)
18711 {
18712 verbose_enter();
18713 MSG_PUTS(_("\n\tLast set from "));
18714 MSG_PUTS(get_scriptname(scriptID));
18715 verbose_leave();
18716 }
18717}
18718
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719#endif /* FEAT_EVAL */
18720
18721#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
18722
18723
18724#ifdef WIN3264
18725/*
18726 * Functions for ":8" filename modifier: get 8.3 version of a filename.
18727 */
18728static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18729static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
18730static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18731
18732/*
18733 * Get the short pathname of a file.
18734 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
18735 */
18736 static int
18737get_short_pathname(fnamep, bufp, fnamelen)
18738 char_u **fnamep;
18739 char_u **bufp;
18740 int *fnamelen;
18741{
18742 int l,len;
18743 char_u *newbuf;
18744
18745 len = *fnamelen;
18746
18747 l = GetShortPathName(*fnamep, *fnamep, len);
18748 if (l > len - 1)
18749 {
18750 /* If that doesn't work (not enough space), then save the string
18751 * and try again with a new buffer big enough
18752 */
18753 newbuf = vim_strnsave(*fnamep, l);
18754 if (newbuf == NULL)
18755 return 0;
18756
18757 vim_free(*bufp);
18758 *fnamep = *bufp = newbuf;
18759
18760 l = GetShortPathName(*fnamep,*fnamep,l+1);
18761
18762 /* Really should always succeed, as the buffer is big enough */
18763 }
18764
18765 *fnamelen = l;
18766 return 1;
18767}
18768
18769/*
18770 * Create a short path name. Returns the length of the buffer it needs.
18771 * Doesn't copy over the end of the buffer passed in.
18772 */
18773 static int
18774shortpath_for_invalid_fname(fname, bufp, fnamelen)
18775 char_u **fname;
18776 char_u **bufp;
18777 int *fnamelen;
18778{
18779 char_u *s, *p, *pbuf2, *pbuf3;
18780 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018781 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782
18783 /* Make a copy */
18784 len2 = *fnamelen;
18785 pbuf2 = vim_strnsave(*fname, len2);
18786 pbuf3 = NULL;
18787
18788 s = pbuf2 + len2 - 1; /* Find the end */
18789 slen = 1;
18790 plen = len2;
18791
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018792 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793 {
18794 --s;
18795 ++slen;
18796 --plen;
18797 }
18798
18799 do
18800 {
18801 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018802 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803 {
18804 --s;
18805 ++slen;
18806 --plen;
18807 }
18808 if (s <= pbuf2)
18809 break;
18810
18811 /* Remeber the character that is about to be blatted */
18812 ch = *s;
18813 *s = 0; /* get_short_pathname requires a null-terminated string */
18814
18815 /* Try it in situ */
18816 p = pbuf2;
18817 if (!get_short_pathname(&p, &pbuf3, &plen))
18818 {
18819 vim_free(pbuf2);
18820 return -1;
18821 }
18822 *s = ch; /* Preserve the string */
18823 } while (plen == 0);
18824
18825 if (plen > 0)
18826 {
18827 /* Remeber the length of the new string. */
18828 *fnamelen = len = plen + slen;
18829 vim_free(*bufp);
18830 if (len > len2)
18831 {
18832 /* If there's not enough space in the currently allocated string,
18833 * then copy it to a buffer big enough.
18834 */
18835 *fname= *bufp = vim_strnsave(p, len);
18836 if (*fname == NULL)
18837 return -1;
18838 }
18839 else
18840 {
18841 /* Transfer pbuf2 to being the main buffer (it's big enough) */
18842 *fname = *bufp = pbuf2;
18843 if (p != pbuf2)
18844 strncpy(*fname, p, plen);
18845 pbuf2 = NULL;
18846 }
18847 /* Concat the next bit */
18848 strncpy(*fname + plen, s, slen);
18849 (*fname)[len] = '\0';
18850 }
18851 vim_free(pbuf3);
18852 vim_free(pbuf2);
18853 return 0;
18854}
18855
18856/*
18857 * Get a pathname for a partial path.
18858 */
18859 static int
18860shortpath_for_partial(fnamep, bufp, fnamelen)
18861 char_u **fnamep;
18862 char_u **bufp;
18863 int *fnamelen;
18864{
18865 int sepcount, len, tflen;
18866 char_u *p;
18867 char_u *pbuf, *tfname;
18868 int hasTilde;
18869
18870 /* Count up the path seperators from the RHS.. so we know which part
18871 * of the path to return.
18872 */
18873 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018874 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875 if (vim_ispathsep(*p))
18876 ++sepcount;
18877
18878 /* Need full path first (use expand_env() to remove a "~/") */
18879 hasTilde = (**fnamep == '~');
18880 if (hasTilde)
18881 pbuf = tfname = expand_env_save(*fnamep);
18882 else
18883 pbuf = tfname = FullName_save(*fnamep, FALSE);
18884
18885 len = tflen = STRLEN(tfname);
18886
18887 if (!get_short_pathname(&tfname, &pbuf, &len))
18888 return -1;
18889
18890 if (len == 0)
18891 {
18892 /* Don't have a valid filename, so shorten the rest of the
18893 * path if we can. This CAN give us invalid 8.3 filenames, but
18894 * there's not a lot of point in guessing what it might be.
18895 */
18896 len = tflen;
18897 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
18898 return -1;
18899 }
18900
18901 /* Count the paths backward to find the beginning of the desired string. */
18902 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018903 {
18904#ifdef FEAT_MBYTE
18905 if (has_mbyte)
18906 p -= mb_head_off(tfname, p);
18907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 if (vim_ispathsep(*p))
18909 {
18910 if (sepcount == 0 || (hasTilde && sepcount == 1))
18911 break;
18912 else
18913 sepcount --;
18914 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 if (hasTilde)
18917 {
18918 --p;
18919 if (p >= tfname)
18920 *p = '~';
18921 else
18922 return -1;
18923 }
18924 else
18925 ++p;
18926
18927 /* Copy in the string - p indexes into tfname - allocated at pbuf */
18928 vim_free(*bufp);
18929 *fnamelen = (int)STRLEN(p);
18930 *bufp = pbuf;
18931 *fnamep = p;
18932
18933 return 0;
18934}
18935#endif /* WIN3264 */
18936
18937/*
18938 * Adjust a filename, according to a string of modifiers.
18939 * *fnamep must be NUL terminated when called. When returning, the length is
18940 * determined by *fnamelen.
18941 * Returns valid flags.
18942 * When there is an error, *fnamep is set to NULL.
18943 */
18944 int
18945modify_fname(src, usedlen, fnamep, bufp, fnamelen)
18946 char_u *src; /* string with modifiers */
18947 int *usedlen; /* characters after src that are used */
18948 char_u **fnamep; /* file name so far */
18949 char_u **bufp; /* buffer for allocated file name or NULL */
18950 int *fnamelen; /* length of fnamep */
18951{
18952 int valid = 0;
18953 char_u *tail;
18954 char_u *s, *p, *pbuf;
18955 char_u dirname[MAXPATHL];
18956 int c;
18957 int has_fullname = 0;
18958#ifdef WIN3264
18959 int has_shortname = 0;
18960#endif
18961
18962repeat:
18963 /* ":p" - full path/file_name */
18964 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
18965 {
18966 has_fullname = 1;
18967
18968 valid |= VALID_PATH;
18969 *usedlen += 2;
18970
18971 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
18972 if ((*fnamep)[0] == '~'
18973#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
18974 && ((*fnamep)[1] == '/'
18975# ifdef BACKSLASH_IN_FILENAME
18976 || (*fnamep)[1] == '\\'
18977# endif
18978 || (*fnamep)[1] == NUL)
18979
18980#endif
18981 )
18982 {
18983 *fnamep = expand_env_save(*fnamep);
18984 vim_free(*bufp); /* free any allocated file name */
18985 *bufp = *fnamep;
18986 if (*fnamep == NULL)
18987 return -1;
18988 }
18989
18990 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018991 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992 {
18993 if (vim_ispathsep(*p)
18994 && p[1] == '.'
18995 && (p[2] == NUL
18996 || vim_ispathsep(p[2])
18997 || (p[2] == '.'
18998 && (p[3] == NUL || vim_ispathsep(p[3])))))
18999 break;
19000 }
19001
19002 /* FullName_save() is slow, don't use it when not needed. */
19003 if (*p != NUL || !vim_isAbsName(*fnamep))
19004 {
19005 *fnamep = FullName_save(*fnamep, *p != NUL);
19006 vim_free(*bufp); /* free any allocated file name */
19007 *bufp = *fnamep;
19008 if (*fnamep == NULL)
19009 return -1;
19010 }
19011
19012 /* Append a path separator to a directory. */
19013 if (mch_isdir(*fnamep))
19014 {
19015 /* Make room for one or two extra characters. */
19016 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19017 vim_free(*bufp); /* free any allocated file name */
19018 *bufp = *fnamep;
19019 if (*fnamep == NULL)
19020 return -1;
19021 add_pathsep(*fnamep);
19022 }
19023 }
19024
19025 /* ":." - path relative to the current directory */
19026 /* ":~" - path relative to the home directory */
19027 /* ":8" - shortname path - postponed till after */
19028 while (src[*usedlen] == ':'
19029 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19030 {
19031 *usedlen += 2;
19032 if (c == '8')
19033 {
19034#ifdef WIN3264
19035 has_shortname = 1; /* Postpone this. */
19036#endif
19037 continue;
19038 }
19039 pbuf = NULL;
19040 /* Need full path first (use expand_env() to remove a "~/") */
19041 if (!has_fullname)
19042 {
19043 if (c == '.' && **fnamep == '~')
19044 p = pbuf = expand_env_save(*fnamep);
19045 else
19046 p = pbuf = FullName_save(*fnamep, FALSE);
19047 }
19048 else
19049 p = *fnamep;
19050
19051 has_fullname = 0;
19052
19053 if (p != NULL)
19054 {
19055 if (c == '.')
19056 {
19057 mch_dirname(dirname, MAXPATHL);
19058 s = shorten_fname(p, dirname);
19059 if (s != NULL)
19060 {
19061 *fnamep = s;
19062 if (pbuf != NULL)
19063 {
19064 vim_free(*bufp); /* free any allocated file name */
19065 *bufp = pbuf;
19066 pbuf = NULL;
19067 }
19068 }
19069 }
19070 else
19071 {
19072 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19073 /* Only replace it when it starts with '~' */
19074 if (*dirname == '~')
19075 {
19076 s = vim_strsave(dirname);
19077 if (s != NULL)
19078 {
19079 *fnamep = s;
19080 vim_free(*bufp);
19081 *bufp = s;
19082 }
19083 }
19084 }
19085 vim_free(pbuf);
19086 }
19087 }
19088
19089 tail = gettail(*fnamep);
19090 *fnamelen = (int)STRLEN(*fnamep);
19091
19092 /* ":h" - head, remove "/file_name", can be repeated */
19093 /* Don't remove the first "/" or "c:\" */
19094 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19095 {
19096 valid |= VALID_HEAD;
19097 *usedlen += 2;
19098 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019099 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100 --tail;
19101 *fnamelen = (int)(tail - *fnamep);
19102#ifdef VMS
19103 if (*fnamelen > 0)
19104 *fnamelen += 1; /* the path separator is part of the path */
19105#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019106 while (tail > s && !after_pathsep(s, tail))
19107 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108 }
19109
19110 /* ":8" - shortname */
19111 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19112 {
19113 *usedlen += 2;
19114#ifdef WIN3264
19115 has_shortname = 1;
19116#endif
19117 }
19118
19119#ifdef WIN3264
19120 /* Check shortname after we have done 'heads' and before we do 'tails'
19121 */
19122 if (has_shortname)
19123 {
19124 pbuf = NULL;
19125 /* Copy the string if it is shortened by :h */
19126 if (*fnamelen < (int)STRLEN(*fnamep))
19127 {
19128 p = vim_strnsave(*fnamep, *fnamelen);
19129 if (p == 0)
19130 return -1;
19131 vim_free(*bufp);
19132 *bufp = *fnamep = p;
19133 }
19134
19135 /* Split into two implementations - makes it easier. First is where
19136 * there isn't a full name already, second is where there is.
19137 */
19138 if (!has_fullname && !vim_isAbsName(*fnamep))
19139 {
19140 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19141 return -1;
19142 }
19143 else
19144 {
19145 int l;
19146
19147 /* Simple case, already have the full-name
19148 * Nearly always shorter, so try first time. */
19149 l = *fnamelen;
19150 if (!get_short_pathname(fnamep, bufp, &l))
19151 return -1;
19152
19153 if (l == 0)
19154 {
19155 /* Couldn't find the filename.. search the paths.
19156 */
19157 l = *fnamelen;
19158 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19159 return -1;
19160 }
19161 *fnamelen = l;
19162 }
19163 }
19164#endif /* WIN3264 */
19165
19166 /* ":t" - tail, just the basename */
19167 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19168 {
19169 *usedlen += 2;
19170 *fnamelen -= (int)(tail - *fnamep);
19171 *fnamep = tail;
19172 }
19173
19174 /* ":e" - extension, can be repeated */
19175 /* ":r" - root, without extension, can be repeated */
19176 while (src[*usedlen] == ':'
19177 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19178 {
19179 /* find a '.' in the tail:
19180 * - for second :e: before the current fname
19181 * - otherwise: The last '.'
19182 */
19183 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19184 s = *fnamep - 2;
19185 else
19186 s = *fnamep + *fnamelen - 1;
19187 for ( ; s > tail; --s)
19188 if (s[0] == '.')
19189 break;
19190 if (src[*usedlen + 1] == 'e') /* :e */
19191 {
19192 if (s > tail)
19193 {
19194 *fnamelen += (int)(*fnamep - (s + 1));
19195 *fnamep = s + 1;
19196#ifdef VMS
19197 /* cut version from the extension */
19198 s = *fnamep + *fnamelen - 1;
19199 for ( ; s > *fnamep; --s)
19200 if (s[0] == ';')
19201 break;
19202 if (s > *fnamep)
19203 *fnamelen = s - *fnamep;
19204#endif
19205 }
19206 else if (*fnamep <= tail)
19207 *fnamelen = 0;
19208 }
19209 else /* :r */
19210 {
19211 if (s > tail) /* remove one extension */
19212 *fnamelen = (int)(s - *fnamep);
19213 }
19214 *usedlen += 2;
19215 }
19216
19217 /* ":s?pat?foo?" - substitute */
19218 /* ":gs?pat?foo?" - global substitute */
19219 if (src[*usedlen] == ':'
19220 && (src[*usedlen + 1] == 's'
19221 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19222 {
19223 char_u *str;
19224 char_u *pat;
19225 char_u *sub;
19226 int sep;
19227 char_u *flags;
19228 int didit = FALSE;
19229
19230 flags = (char_u *)"";
19231 s = src + *usedlen + 2;
19232 if (src[*usedlen + 1] == 'g')
19233 {
19234 flags = (char_u *)"g";
19235 ++s;
19236 }
19237
19238 sep = *s++;
19239 if (sep)
19240 {
19241 /* find end of pattern */
19242 p = vim_strchr(s, sep);
19243 if (p != NULL)
19244 {
19245 pat = vim_strnsave(s, (int)(p - s));
19246 if (pat != NULL)
19247 {
19248 s = p + 1;
19249 /* find end of substitution */
19250 p = vim_strchr(s, sep);
19251 if (p != NULL)
19252 {
19253 sub = vim_strnsave(s, (int)(p - s));
19254 str = vim_strnsave(*fnamep, *fnamelen);
19255 if (sub != NULL && str != NULL)
19256 {
19257 *usedlen = (int)(p + 1 - src);
19258 s = do_string_sub(str, pat, sub, flags);
19259 if (s != NULL)
19260 {
19261 *fnamep = s;
19262 *fnamelen = (int)STRLEN(s);
19263 vim_free(*bufp);
19264 *bufp = s;
19265 didit = TRUE;
19266 }
19267 }
19268 vim_free(sub);
19269 vim_free(str);
19270 }
19271 vim_free(pat);
19272 }
19273 }
19274 /* after using ":s", repeat all the modifiers */
19275 if (didit)
19276 goto repeat;
19277 }
19278 }
19279
19280 return valid;
19281}
19282
19283/*
19284 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19285 * "flags" can be "g" to do a global substitute.
19286 * Returns an allocated string, NULL for error.
19287 */
19288 char_u *
19289do_string_sub(str, pat, sub, flags)
19290 char_u *str;
19291 char_u *pat;
19292 char_u *sub;
19293 char_u *flags;
19294{
19295 int sublen;
19296 regmatch_T regmatch;
19297 int i;
19298 int do_all;
19299 char_u *tail;
19300 garray_T ga;
19301 char_u *ret;
19302 char_u *save_cpo;
19303
19304 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19305 save_cpo = p_cpo;
19306 p_cpo = (char_u *)"";
19307
19308 ga_init2(&ga, 1, 200);
19309
19310 do_all = (flags[0] == 'g');
19311
19312 regmatch.rm_ic = p_ic;
19313 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19314 if (regmatch.regprog != NULL)
19315 {
19316 tail = str;
19317 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19318 {
19319 /*
19320 * Get some space for a temporary buffer to do the substitution
19321 * into. It will contain:
19322 * - The text up to where the match is.
19323 * - The substituted text.
19324 * - The text after the match.
19325 */
19326 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19327 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19328 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19329 {
19330 ga_clear(&ga);
19331 break;
19332 }
19333
19334 /* copy the text up to where the match is */
19335 i = (int)(regmatch.startp[0] - tail);
19336 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19337 /* add the substituted text */
19338 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19339 + ga.ga_len + i, TRUE, TRUE, FALSE);
19340 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341 /* avoid getting stuck on a match with an empty string */
19342 if (tail == regmatch.endp[0])
19343 {
19344 if (*tail == NUL)
19345 break;
19346 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19347 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019348 }
19349 else
19350 {
19351 tail = regmatch.endp[0];
19352 if (*tail == NUL)
19353 break;
19354 }
19355 if (!do_all)
19356 break;
19357 }
19358
19359 if (ga.ga_data != NULL)
19360 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19361
19362 vim_free(regmatch.regprog);
19363 }
19364
19365 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19366 ga_clear(&ga);
19367 p_cpo = save_cpo;
19368
19369 return ret;
19370}
19371
19372#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */