blob: 2b6703a98766b922ffcddaefa4362e8b88446cda [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
356static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
357static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
358static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
359static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
360static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
361static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
362static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
363static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000364static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000365static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
366static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
367static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
368static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
369static list_T *list_alloc __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000370static void list_free __ARGS((list_T *l));
371static listitem_T *listitem_alloc __ARGS((void));
372static void listitem_free __ARGS((listitem_T *item));
373static void listitem_remove __ARGS((list_T *l, listitem_T *item));
374static long list_len __ARGS((list_T *l));
375static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
376static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
377static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000378static listitem_T *list_find __ARGS((list_T *l, long n));
379static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000380static void list_append __ARGS((list_T *l, listitem_T *item));
381static int list_append_tv __ARGS((list_T *l, typval_T *tv));
382static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
383static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
384static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000385static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000386static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
387static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000388static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar33570922005-01-25 22:26:29 +0000389
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000390static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
391static void set_ref_in_list __ARGS((list_T *l, int copyID));
392static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaard9fba312005-06-26 22:34:35 +0000393
Bram Moolenaar33570922005-01-25 22:26:29 +0000394static void dict_unref __ARGS((dict_T *d));
395static void dict_free __ARGS((dict_T *d));
396static dictitem_T *dictitem_alloc __ARGS((char_u *key));
397static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
398static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
399static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000400static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000401static int dict_add __ARGS((dict_T *d, dictitem_T *item));
402static long dict_len __ARGS((dict_T *d));
403static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
404static char_u *dict2string __ARGS((typval_T *tv));
405static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406
407static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
408static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
409static char_u *string_quote __ARGS((char_u *str, int function));
410static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int find_internal_func __ARGS((char_u *name));
412static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
413static 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));
414static 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 +0000415static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416
417static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
418static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
419static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
420static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
421static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
422static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
423static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
424static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
425static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
426static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
427static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
428static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
429static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
430static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
431static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
432static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
433static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
434static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
435static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
436static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
437static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
438static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
439static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
440static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
441static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
442static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
443static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
444static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000467static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000481static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000509static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000526static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000527static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000530#ifdef vim_mkdir
531static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
532#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000533static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000538static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000556static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000561static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000562static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
565#ifdef HAVE_STRFTIME
566static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
567#endif
568static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000580static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000596static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000598static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
599static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static win_T *find_win_by_nr __ARGS((typval_T *vp));
601static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
602static int get_env_len __ARGS((char_u **arg));
603static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000604static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000605static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
606#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
607#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
608 valid character */
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000610static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000611static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
612static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static typval_T *alloc_tv __ARGS((void));
614static typval_T *alloc_string_tv __ARGS((char_u *string));
615static void free_tv __ARGS((typval_T *varp));
616static void clear_tv __ARGS((typval_T *varp));
617static void init_tv __ARGS((typval_T *varp));
618static long get_tv_number __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000619static long get_tv_number_chk __ARGS((typval_T *varp, int *denote));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
621static char_u *get_tv_string __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000622static char_u *get_tv_string_chk __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000624static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000625static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000626static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
628static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
629static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
630static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
631static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
632static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
633static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000634static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000636static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
638static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
639static int eval_fname_script __ARGS((char_u *p));
640static int eval_fname_sid __ARGS((char_u *p));
641static void list_func_head __ARGS((ufunc_T *fp, int indent));
642static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
643static ufunc_T *find_func __ARGS((char_u *name));
644static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000645static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000646#ifdef FEAT_PROFILE
647static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000648static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
649static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
650static int
651# ifdef __BORLANDC__
652 _RTLENTRYF
653# endif
654 prof_total_cmp __ARGS((const void *s1, const void *s2));
655static int
656# ifdef __BORLANDC__
657 _RTLENTRYF
658# endif
659 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000660#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000661static int script_autoload __ARGS((char_u *name));
662static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static void func_free __ARGS((ufunc_T *fp));
664static void func_unref __ARGS((char_u *name));
665static void func_ref __ARGS((char_u *name));
666static 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));
667static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
668
669static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
670
671static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
672static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
673static char_u *skip_var_one __ARGS((char_u *arg));
674static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
675static void list_glob_vars __ARGS((void));
676static void list_buf_vars __ARGS((void));
677static void list_win_vars __ARGS((void));
678static void list_vim_vars __ARGS((void));
679static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
680static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
681static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000682static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void clear_lval __ARGS((lval_T *lp));
684static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
685static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
686static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
687static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
688static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000689static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000691static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
692static void item_lock __ARGS((typval_T *tv, int deep, int lock));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000693static int tv_islocked __ARGS((typval_T *tv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694
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 Moolenaar24bbcfe2005-06-28 23:32:02 +00001210#if defined(FEAT_SYN_HL) || defined(PROTO)
1211/*
1212 * Evaluate an expression to a list with suggestions.
1213 * For the "expr:" part of 'spellsuggest'.
1214 */
1215 list_T *
1216eval_spell_expr(badword, expr)
1217 char_u *badword;
1218 char_u *expr;
1219{
1220 typval_T save_val;
1221 typval_T rettv;
1222 list_T *list = NULL;
1223 char_u *p = skipwhite(expr);
1224
1225 /* Set "v:val" to the bad word. */
1226 prepare_vimvar(VV_VAL, &save_val);
1227 vimvars[VV_VAL].vv_type = VAR_STRING;
1228 vimvars[VV_VAL].vv_str = badword;
1229 if (p_verbose == 0)
1230 ++emsg_off;
1231
1232 if (eval1(&p, &rettv, TRUE) == OK)
1233 {
1234 if (rettv.v_type != VAR_LIST)
1235 clear_tv(&rettv);
1236 else
1237 list = rettv.vval.v_list;
1238 }
1239
1240 if (p_verbose == 0)
1241 --emsg_off;
1242 vimvars[VV_VAL].vv_str = NULL;
1243 restore_vimvar(VV_VAL, &save_val);
1244
1245 return list;
1246}
1247
1248/*
1249 * "list" is supposed to contain two items: a word and a number. Return the
1250 * word in "pp" and the number as the return value.
1251 * Return -1 if anything isn't right.
1252 * Used to get the good word and score from the eval_spell_expr() result.
1253 */
1254 int
1255get_spellword(list, pp)
1256 list_T *list;
1257 char_u **pp;
1258{
1259 listitem_T *li;
1260
1261 li = list->lv_first;
1262 if (li == NULL)
1263 return -1;
1264 *pp = get_tv_string(&li->li_tv);
1265
1266 li = li->li_next;
1267 if (li == NULL)
1268 return -1;
1269 return get_tv_number(&li->li_tv);
1270}
1271#endif
1272
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1274/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001275 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001277 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001279 static int
1280call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 char_u *func;
1282 int argc;
1283 char_u **argv;
1284 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
Bram Moolenaar33570922005-01-25 22:26:29 +00001287 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 long n;
1289 int len;
1290 int i;
1291 int doesrange;
1292 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001293 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294
Bram Moolenaar33570922005-01-25 22:26:29 +00001295 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001297 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298
1299 for (i = 0; i < argc; i++)
1300 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001301 /* Pass a NULL or empty argument as an empty string */
1302 if (argv[i] == NULL || *argv[i] == NUL)
1303 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001304 argvars[i].v_type = VAR_STRING;
1305 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001306 continue;
1307 }
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 /* Recognize a number argument, the others must be strings. */
1310 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1311 if (len != 0 && len == (int)STRLEN(argv[i]))
1312 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001313 argvars[i].v_type = VAR_NUMBER;
1314 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
1316 else
1317 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001318 argvars[i].v_type = VAR_STRING;
1319 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321 }
1322
1323 if (safe)
1324 {
1325 save_funccalp = save_funccal();
1326 ++sandbox;
1327 }
1328
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001329 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1330 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001332 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (safe)
1334 {
1335 --sandbox;
1336 restore_funccal(save_funccalp);
1337 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001338 vim_free(argvars);
1339
1340 if (ret == FAIL)
1341 clear_tv(rettv);
1342
1343 return ret;
1344}
1345
1346/*
1347 * Call some vimL function and return the result as a string
1348 * Uses argv[argc] for the function arguments.
1349 */
1350 void *
1351call_func_retstr(func, argc, argv, safe)
1352 char_u *func;
1353 int argc;
1354 char_u **argv;
1355 int safe; /* use the sandbox */
1356{
1357 typval_T rettv;
1358 char_u *retval = NULL;
1359
1360 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1361 return NULL;
1362
1363 retval = vim_strsave(get_tv_string(&rettv));
1364
1365 clear_tv(&rettv);
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 return retval;
1368}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001369
1370/*
1371 * Call some vimL function and return the result as a list
1372 * Uses argv[argc] for the function arguments.
1373 */
1374 void *
1375call_func_retlist(func, argc, argv, safe)
1376 char_u *func;
1377 int argc;
1378 char_u **argv;
1379 int safe; /* use the sandbox */
1380{
1381 typval_T rettv;
1382
1383 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1384 return NULL;
1385
1386 if (rettv.v_type != VAR_LIST)
1387 {
1388 clear_tv(&rettv);
1389 return NULL;
1390 }
1391
1392 return rettv.vval.v_list;
1393}
1394
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395#endif
1396
1397/*
1398 * Save the current function call pointer, and set it to NULL.
1399 * Used when executing autocommands and for ":source".
1400 */
1401 void *
1402save_funccal()
1403{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001404 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 current_funccal = NULL;
1407 return (void *)fc;
1408}
1409
1410 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001411restore_funccal(vfc)
1412 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001414 funccall_T *fc = (funccall_T *)vfc;
1415
1416 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417}
1418
Bram Moolenaar05159a02005-02-26 23:04:13 +00001419#if defined(FEAT_PROFILE) || defined(PROTO)
1420/*
1421 * Prepare profiling for entering a child or something else that is not
1422 * counted for the script/function itself.
1423 * Should always be called in pair with prof_child_exit().
1424 */
1425 void
1426prof_child_enter(tm)
1427 proftime_T *tm; /* place to store waittime */
1428{
1429 funccall_T *fc = current_funccal;
1430
1431 if (fc != NULL && fc->func->uf_profiling)
1432 profile_start(&fc->prof_child);
1433 script_prof_save(tm);
1434}
1435
1436/*
1437 * Take care of time spent in a child.
1438 * Should always be called after prof_child_enter().
1439 */
1440 void
1441prof_child_exit(tm)
1442 proftime_T *tm; /* where waittime was stored */
1443{
1444 funccall_T *fc = current_funccal;
1445
1446 if (fc != NULL && fc->func->uf_profiling)
1447 {
1448 profile_end(&fc->prof_child);
1449 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1450 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1451 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1452 }
1453 script_prof_restore(tm);
1454}
1455#endif
1456
1457
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458#ifdef FEAT_FOLDING
1459/*
1460 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1461 * it in "*cp". Doesn't give error messages.
1462 */
1463 int
1464eval_foldexpr(arg, cp)
1465 char_u *arg;
1466 int *cp;
1467{
Bram Moolenaar33570922005-01-25 22:26:29 +00001468 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 int retval;
1470 char_u *s;
1471
1472 ++emsg_off;
1473 ++sandbox;
1474 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001475 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 retval = 0;
1477 else
1478 {
1479 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001480 if (tv.v_type == VAR_NUMBER)
1481 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001482 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 retval = 0;
1484 else
1485 {
1486 /* If the result is a string, check if there is a non-digit before
1487 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001488 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (!VIM_ISDIGIT(*s) && *s != '-')
1490 *cp = *s++;
1491 retval = atol((char *)s);
1492 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001493 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 }
1495 --emsg_off;
1496 --sandbox;
1497
1498 return retval;
1499}
1500#endif
1501
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001503 * ":let" list all variable values
1504 * ":let var1 var2" list variable values
1505 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001506 * ":let var += expr" assignment command.
1507 * ":let var -= expr" assignment command.
1508 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001509 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 */
1511 void
1512ex_let(eap)
1513 exarg_T *eap;
1514{
1515 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001517 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001519 int var_count = 0;
1520 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001521 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001523 expr = skip_var_list(arg, &var_count, &semicolon);
1524 if (expr == NULL)
1525 return;
1526 expr = vim_strchr(expr, '=');
1527 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001529 /*
1530 * ":let" without "=": list variables
1531 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001532 if (*arg == '[')
1533 EMSG(_(e_invarg));
1534 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001535 /* ":let var1 var2" */
1536 arg = list_arg_vars(eap, arg);
1537 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001539 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001540 list_glob_vars();
1541 list_buf_vars();
1542 list_win_vars();
1543 list_vim_vars();
1544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 eap->nextcmd = check_nextcmd(arg);
1546 }
1547 else
1548 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001549 op[0] = '=';
1550 op[1] = NUL;
1551 if (expr > arg)
1552 {
1553 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1554 op[0] = expr[-1]; /* +=, -= or .= */
1555 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001556 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001557
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 if (eap->skip)
1559 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001560 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (eap->skip)
1562 {
1563 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001564 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 --emsg_skip;
1566 }
1567 else if (i != FAIL)
1568 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001569 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001570 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001571 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 }
1573 }
1574}
1575
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001576/*
1577 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1578 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001579 * When "nextchars" is not NULL it points to a string with characters that
1580 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1581 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001582 * Returns OK or FAIL;
1583 */
1584 static int
1585ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1586 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001587 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001588 int copy; /* copy values from "tv", don't move */
1589 int semicolon; /* from skip_var_list() */
1590 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001591 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001592{
1593 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001594 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001595 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001596 listitem_T *item;
1597 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001598
1599 if (*arg != '[')
1600 {
1601 /*
1602 * ":let var = expr" or ":for var in list"
1603 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001604 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001605 return FAIL;
1606 return OK;
1607 }
1608
1609 /*
1610 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1611 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001612 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001613 {
1614 EMSG(_(e_listreq));
1615 return FAIL;
1616 }
1617
1618 i = list_len(l);
1619 if (semicolon == 0 && var_count < i)
1620 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001621 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001622 return FAIL;
1623 }
1624 if (var_count - semicolon > i)
1625 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001626 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001627 return FAIL;
1628 }
1629
1630 item = l->lv_first;
1631 while (*arg != ']')
1632 {
1633 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001634 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001635 item = item->li_next;
1636 if (arg == NULL)
1637 return FAIL;
1638
1639 arg = skipwhite(arg);
1640 if (*arg == ';')
1641 {
1642 /* Put the rest of the list (may be empty) in the var after ';'.
1643 * Create a new list for this. */
1644 l = list_alloc();
1645 if (l == NULL)
1646 return FAIL;
1647 while (item != NULL)
1648 {
1649 list_append_tv(l, &item->li_tv);
1650 item = item->li_next;
1651 }
1652
1653 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001654 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001655 ltv.vval.v_list = l;
1656 l->lv_refcount = 1;
1657
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001658 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1659 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660 clear_tv(&ltv);
1661 if (arg == NULL)
1662 return FAIL;
1663 break;
1664 }
1665 else if (*arg != ',' && *arg != ']')
1666 {
1667 EMSG2(_(e_intern2), "ex_let_vars()");
1668 return FAIL;
1669 }
1670 }
1671
1672 return OK;
1673}
1674
1675/*
1676 * Skip over assignable variable "var" or list of variables "[var, var]".
1677 * Used for ":let varvar = expr" and ":for varvar in expr".
1678 * For "[var, var]" increment "*var_count" for each variable.
1679 * for "[var, var; var]" set "semicolon".
1680 * Return NULL for an error.
1681 */
1682 static char_u *
1683skip_var_list(arg, var_count, semicolon)
1684 char_u *arg;
1685 int *var_count;
1686 int *semicolon;
1687{
1688 char_u *p, *s;
1689
1690 if (*arg == '[')
1691 {
1692 /* "[var, var]": find the matching ']'. */
1693 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001694 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695 {
1696 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1697 s = skip_var_one(p);
1698 if (s == p)
1699 {
1700 EMSG2(_(e_invarg2), p);
1701 return NULL;
1702 }
1703 ++*var_count;
1704
1705 p = skipwhite(s);
1706 if (*p == ']')
1707 break;
1708 else if (*p == ';')
1709 {
1710 if (*semicolon == 1)
1711 {
1712 EMSG(_("Double ; in list of variables"));
1713 return NULL;
1714 }
1715 *semicolon = 1;
1716 }
1717 else if (*p != ',')
1718 {
1719 EMSG2(_(e_invarg2), p);
1720 return NULL;
1721 }
1722 }
1723 return p + 1;
1724 }
1725 else
1726 return skip_var_one(arg);
1727}
1728
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001729/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001730 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1731 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001732 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001733 static char_u *
1734skip_var_one(arg)
1735 char_u *arg;
1736{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001737 if (*arg == '@' && arg[1] != NUL)
1738 return arg + 2;
1739 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1740 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001741}
1742
Bram Moolenaara7043832005-01-21 11:56:39 +00001743/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001744 * List variables for hashtab "ht" with prefix "prefix".
1745 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001746 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001747 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001748list_hashtable_vars(ht, prefix, empty)
1749 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001750 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001751 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001752{
Bram Moolenaar33570922005-01-25 22:26:29 +00001753 hashitem_T *hi;
1754 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001755 int todo;
1756
1757 todo = ht->ht_used;
1758 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1759 {
1760 if (!HASHITEM_EMPTY(hi))
1761 {
1762 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001763 di = HI2DI(hi);
1764 if (empty || di->di_tv.v_type != VAR_STRING
1765 || di->di_tv.vval.v_string != NULL)
1766 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001767 }
1768 }
1769}
1770
1771/*
1772 * List global variables.
1773 */
1774 static void
1775list_glob_vars()
1776{
Bram Moolenaar33570922005-01-25 22:26:29 +00001777 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001778}
1779
1780/*
1781 * List buffer variables.
1782 */
1783 static void
1784list_buf_vars()
1785{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001786 char_u numbuf[NUMBUFLEN];
1787
Bram Moolenaar33570922005-01-25 22:26:29 +00001788 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001789
1790 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1791 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001792}
1793
1794/*
1795 * List window variables.
1796 */
1797 static void
1798list_win_vars()
1799{
Bram Moolenaar33570922005-01-25 22:26:29 +00001800 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001801}
1802
1803/*
1804 * List Vim variables.
1805 */
1806 static void
1807list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808{
Bram Moolenaar33570922005-01-25 22:26:29 +00001809 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001810}
1811
1812/*
1813 * List variables in "arg".
1814 */
1815 static char_u *
1816list_arg_vars(eap, arg)
1817 exarg_T *eap;
1818 char_u *arg;
1819{
1820 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001821 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001823 char_u *name_start;
1824 char_u *arg_subsc;
1825 char_u *tofree;
1826 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827
1828 while (!ends_excmd(*arg) && !got_int)
1829 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001830 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001832 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001833 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1834 {
1835 emsg_severe = TRUE;
1836 EMSG(_(e_trailing));
1837 break;
1838 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001840 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001842 /* get_name_len() takes care of expanding curly braces */
1843 name_start = name = arg;
1844 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1845 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001847 /* This is mainly to keep test 49 working: when expanding
1848 * curly braces fails overrule the exception error message. */
1849 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001851 emsg_severe = TRUE;
1852 EMSG2(_(e_invarg2), arg);
1853 break;
1854 }
1855 error = TRUE;
1856 }
1857 else
1858 {
1859 if (tofree != NULL)
1860 name = tofree;
1861 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 else
1864 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001865 /* handle d.key, l[idx], f(expr) */
1866 arg_subsc = arg;
1867 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001868 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001870 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001871 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001872 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001873 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001874 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001875 case 'g': list_glob_vars(); break;
1876 case 'b': list_buf_vars(); break;
1877 case 'w': list_win_vars(); break;
1878 case 'v': list_vim_vars(); break;
1879 default:
1880 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001881 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001882 }
1883 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001884 {
1885 char_u numbuf[NUMBUFLEN];
1886 char_u *tf;
1887 int c;
1888 char_u *s;
1889
1890 s = echo_string(&tv, &tf, numbuf);
1891 c = *arg;
1892 *arg = NUL;
1893 list_one_var_a((char_u *)"",
1894 arg == arg_subsc ? name : name_start,
1895 tv.v_type, s == NULL ? (char_u *)"" : s);
1896 *arg = c;
1897 vim_free(tf);
1898 }
1899 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001900 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 }
1902 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001903
1904 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001906
1907 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 }
1909
1910 return arg;
1911}
1912
1913/*
1914 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1915 * Returns a pointer to the char just after the var name.
1916 * Returns NULL if there is an error.
1917 */
1918 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001919ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001921 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001924 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925{
1926 int c1;
1927 char_u *name;
1928 char_u *p;
1929 char_u *arg_end = NULL;
1930 int len;
1931 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933
1934 /*
1935 * ":let $VAR = expr": Set environment variable.
1936 */
1937 if (*arg == '$')
1938 {
1939 /* Find the end of the name. */
1940 ++arg;
1941 name = arg;
1942 len = get_env_len(&arg);
1943 if (len == 0)
1944 EMSG2(_(e_invarg2), name - 1);
1945 else
1946 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 if (op != NULL && (*op == '+' || *op == '-'))
1948 EMSG2(_(e_letwrong), op);
1949 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951 EMSG(_(e_letunexp));
1952 else
1953 {
1954 c1 = name[len];
1955 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001956 p = get_tv_string_chk(tv);
1957 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 {
1959 int mustfree = FALSE;
1960 char_u *s = vim_getenv(name, &mustfree);
1961
1962 if (s != NULL)
1963 {
1964 p = tofree = concat_str(s, p);
1965 if (mustfree)
1966 vim_free(s);
1967 }
1968 }
1969 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001970 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001972 if (STRICMP(name, "HOME") == 0)
1973 init_homedir();
1974 else if (didset_vim && STRICMP(name, "VIM") == 0)
1975 didset_vim = FALSE;
1976 else if (didset_vimruntime
1977 && STRICMP(name, "VIMRUNTIME") == 0)
1978 didset_vimruntime = FALSE;
1979 arg_end = arg;
1980 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001982 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001983 }
1984 }
1985 }
1986
1987 /*
1988 * ":let &option = expr": Set option value.
1989 * ":let &l:option = expr": Set local option value.
1990 * ":let &g:option = expr": Set global option value.
1991 */
1992 else if (*arg == '&')
1993 {
1994 /* Find the end of the name. */
1995 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 if (p == NULL || (endchars != NULL
1997 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001998 EMSG(_(e_letunexp));
1999 else
2000 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 long n;
2002 int opt_type;
2003 long numval;
2004 char_u *stringval = NULL;
2005 char_u *s;
2006
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002007 c1 = *p;
2008 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002009
2010 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002011 s = get_tv_string_chk(tv); /* != NULL if number or string */
2012 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002013 {
2014 opt_type = get_option_value(arg, &numval,
2015 &stringval, opt_flags);
2016 if ((opt_type == 1 && *op == '.')
2017 || (opt_type == 0 && *op != '.'))
2018 EMSG2(_(e_letwrong), op);
2019 else
2020 {
2021 if (opt_type == 1) /* number */
2022 {
2023 if (*op == '+')
2024 n = numval + n;
2025 else
2026 n = numval - n;
2027 }
2028 else if (opt_type == 0 && stringval != NULL) /* string */
2029 {
2030 s = concat_str(stringval, s);
2031 vim_free(stringval);
2032 stringval = s;
2033 }
2034 }
2035 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002036 if (s != NULL)
2037 {
2038 set_option_value(arg, n, s, opt_flags);
2039 arg_end = p;
2040 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002041 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002042 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002043 }
2044 }
2045
2046 /*
2047 * ":let @r = expr": Set register contents.
2048 */
2049 else if (*arg == '@')
2050 {
2051 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002052 if (op != NULL && (*op == '+' || *op == '-'))
2053 EMSG2(_(e_letwrong), op);
2054 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002055 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002056 EMSG(_(e_letunexp));
2057 else
2058 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002059 char_u *tofree = NULL;
2060 char_u *s;
2061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002062 p = get_tv_string_chk(tv);
2063 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002064 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002065 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002066 if (s != NULL)
2067 {
2068 p = tofree = concat_str(s, p);
2069 vim_free(s);
2070 }
2071 }
2072 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002073 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002074 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002075 arg_end = arg + 1;
2076 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002077 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 }
2079 }
2080
2081 /*
2082 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002083 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002084 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002085 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002086 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002087 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002088
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002089 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002090 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002091 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002092 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2093 EMSG(_(e_letunexp));
2094 else
2095 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002096 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002097 arg_end = p;
2098 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002099 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002100 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002101 }
2102
2103 else
2104 EMSG2(_(e_invarg2), arg);
2105
2106 return arg_end;
2107}
2108
2109/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002110 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2111 */
2112 static int
2113check_changedtick(arg)
2114 char_u *arg;
2115{
2116 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2117 {
2118 EMSG2(_(e_readonlyvar), arg);
2119 return TRUE;
2120 }
2121 return FALSE;
2122}
2123
2124/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002125 * Get an lval: variable, Dict item or List item that can be assigned a value
2126 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2127 * "name.key", "name.key[expr]" etc.
2128 * Indexing only works if "name" is an existing List or Dictionary.
2129 * "name" points to the start of the name.
2130 * If "rettv" is not NULL it points to the value to be assigned.
2131 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2132 * wrong; must end in space or cmd separator.
2133 *
2134 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002135 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002137 */
2138 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002139get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002141 typval_T *rettv;
2142 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002143 int unlet;
2144 int skip;
2145 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002146 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002148 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002149 char_u *expr_start, *expr_end;
2150 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002151 dictitem_T *v;
2152 typval_T var1;
2153 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002154 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002155 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002156 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002157 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002158 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002160 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002161 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162
2163 if (skip)
2164 {
2165 /* When skipping just find the end of the name. */
2166 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002167 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002168 }
2169
2170 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002171 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002172 if (expr_start != NULL)
2173 {
2174 /* Don't expand the name when we already know there is an error. */
2175 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2176 && *p != '[' && *p != '.')
2177 {
2178 EMSG(_(e_trailing));
2179 return NULL;
2180 }
2181
2182 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2183 if (lp->ll_exp_name == NULL)
2184 {
2185 /* Report an invalid expression in braces, unless the
2186 * expression evaluation has been cancelled due to an
2187 * aborting error, an interrupt, or an exception. */
2188 if (!aborting() && !quiet)
2189 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002190 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 EMSG2(_(e_invarg2), name);
2192 return NULL;
2193 }
2194 }
2195 lp->ll_name = lp->ll_exp_name;
2196 }
2197 else
2198 lp->ll_name = name;
2199
2200 /* Without [idx] or .key we are done. */
2201 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2202 return p;
2203
2204 cc = *p;
2205 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002206 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002207 if (v == NULL && !quiet)
2208 EMSG2(_(e_undefvar), lp->ll_name);
2209 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 if (v == NULL)
2211 return NULL;
2212
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002213 /*
2214 * Loop until no more [idx] or .key is following.
2215 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002216 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002217 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002219 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2220 && !(lp->ll_tv->v_type == VAR_DICT
2221 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002223 if (!quiet)
2224 EMSG(_("E689: Can only index a List or Dictionary"));
2225 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002227 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002229 if (!quiet)
2230 EMSG(_("E708: [:] must come last"));
2231 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002233
Bram Moolenaar8c711452005-01-14 21:53:12 +00002234 len = -1;
2235 if (*p == '.')
2236 {
2237 key = p + 1;
2238 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2239 ;
2240 if (len == 0)
2241 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002242 if (!quiet)
2243 EMSG(_(e_emptykey));
2244 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002245 }
2246 p = key + len;
2247 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002248 else
2249 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002250 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002251 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002252 if (*p == ':')
2253 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002254 else
2255 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002256 empty1 = FALSE;
2257 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002259 if (get_tv_string_chk(&var1) == NULL)
2260 {
2261 /* not a number or string */
2262 clear_tv(&var1);
2263 return NULL;
2264 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002265 }
2266
2267 /* Optionally get the second index [ :expr]. */
2268 if (*p == ':')
2269 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002270 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002271 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002272 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002273 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002274 if (!empty1)
2275 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002276 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002277 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002278 if (rettv != NULL && (rettv->v_type != VAR_LIST
2279 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002280 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 if (!quiet)
2282 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002283 if (!empty1)
2284 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002285 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002286 }
2287 p = skipwhite(p + 1);
2288 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002290 else
2291 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002292 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002293 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2294 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002295 if (!empty1)
2296 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002297 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002298 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002299 if (get_tv_string_chk(&var2) == NULL)
2300 {
2301 /* not a number or string */
2302 if (!empty1)
2303 clear_tv(&var1);
2304 clear_tv(&var2);
2305 return NULL;
2306 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002307 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002309 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002310 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002311 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002312
Bram Moolenaar8c711452005-01-14 21:53:12 +00002313 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002314 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002315 if (!quiet)
2316 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002317 if (!empty1)
2318 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002320 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002321 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002322 }
2323
2324 /* Skip to past ']'. */
2325 ++p;
2326 }
2327
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002328 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002329 {
2330 if (len == -1)
2331 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002333 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002334 if (*key == NUL)
2335 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002336 if (!quiet)
2337 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002338 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002339 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002340 }
2341 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002342 lp->ll_list = NULL;
2343 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002344 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002345 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002346 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002347 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002348 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002349 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002350 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002351 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002352 if (len == -1)
2353 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002355 }
2356 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002357 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002358 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002360 if (len == -1)
2361 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002362 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002363 p = NULL;
2364 break;
2365 }
2366 if (len == -1)
2367 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002368 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002369 }
2370 else
2371 {
2372 /*
2373 * Get the number and item for the only or first index of the List.
2374 */
2375 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002377 else
2378 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002379 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002380 clear_tv(&var1);
2381 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002382 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 lp->ll_list = lp->ll_tv->vval.v_list;
2384 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2385 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002386 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 if (!quiet)
2388 EMSGN(_(e_listidx), lp->ll_n1);
2389 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002390 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002391 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002392 }
2393
2394 /*
2395 * May need to find the item or absolute index for the second
2396 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 * When no index given: "lp->ll_empty2" is TRUE.
2398 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002399 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002400 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002401 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002402 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002403 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002404 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002405 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 if (ni == NULL)
2408 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002409 if (!quiet)
2410 EMSGN(_(e_listidx), lp->ll_n2);
2411 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002412 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002414 }
2415
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002416 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2417 if (lp->ll_n1 < 0)
2418 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2419 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002420 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 if (!quiet)
2422 EMSGN(_(e_listidx), lp->ll_n2);
2423 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002424 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002425 }
2426
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002427 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 }
2430
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002431 return p;
2432}
2433
2434/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002435 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 */
2437 static void
2438clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002439 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440{
2441 vim_free(lp->ll_exp_name);
2442 vim_free(lp->ll_newkey);
2443}
2444
2445/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002446 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 */
2450 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002452 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002454 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457{
2458 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002459 listitem_T *ni;
2460 listitem_T *ri;
2461 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462
2463 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 cc = *endp;
2468 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 if (op != NULL && *op != '=')
2470 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002471 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002473 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002474 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2475 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002476 {
2477 if (tv_op(&tv, rettv, op) == OK)
2478 set_var(lp->ll_name, &tv, FALSE);
2479 clear_tv(&tv);
2480 }
2481 }
2482 else
2483 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002487 else if (tv_check_lock(lp->ll_newkey == NULL
2488 ? lp->ll_tv->v_lock
2489 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2490 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 else if (lp->ll_range)
2492 {
2493 /*
2494 * Assign the List values to the list items.
2495 */
2496 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002497 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 if (op != NULL && *op != '=')
2499 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2500 else
2501 {
2502 clear_tv(&lp->ll_li->li_tv);
2503 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2504 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 ri = ri->li_next;
2506 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2507 break;
2508 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002509 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 /* Need to add an empty item. */
2511 ni = listitem_alloc();
2512 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002513 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 ri = NULL;
2515 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002516 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002518 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 ni->li_tv.vval.v_number = 0;
2520 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002521 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 lp->ll_li = lp->ll_li->li_next;
2523 ++lp->ll_n1;
2524 }
2525 if (ri != NULL)
2526 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002527 else if (lp->ll_empty2
2528 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 : lp->ll_n1 != lp->ll_n2)
2530 EMSG(_("E711: List value has not enough items"));
2531 }
2532 else
2533 {
2534 /*
2535 * Assign to a List or Dictionary item.
2536 */
2537 if (lp->ll_newkey != NULL)
2538 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002539 if (op != NULL && *op != '=')
2540 {
2541 EMSG2(_(e_letwrong), op);
2542 return;
2543 }
2544
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002546 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 if (di == NULL)
2548 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002549 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2550 {
2551 vim_free(di);
2552 return;
2553 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002555 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002556 else if (op != NULL && *op != '=')
2557 {
2558 tv_op(lp->ll_tv, rettv, op);
2559 return;
2560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002561 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002563
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 /*
2565 * Assign the value to the variable or list item.
2566 */
2567 if (copy)
2568 copy_tv(rettv, lp->ll_tv);
2569 else
2570 {
2571 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002572 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 }
2575 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576}
2577
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002578/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002579 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2580 * Returns OK or FAIL.
2581 */
2582 static int
2583tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 typval_T *tv1;
2585 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002586 char_u *op;
2587{
2588 long n;
2589 char_u numbuf[NUMBUFLEN];
2590 char_u *s;
2591
2592 /* Can't do anything with a Funcref or a Dict on the right. */
2593 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2594 {
2595 switch (tv1->v_type)
2596 {
2597 case VAR_DICT:
2598 case VAR_FUNC:
2599 break;
2600
2601 case VAR_LIST:
2602 if (*op != '+' || tv2->v_type != VAR_LIST)
2603 break;
2604 /* List += List */
2605 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2606 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2607 return OK;
2608
2609 case VAR_NUMBER:
2610 case VAR_STRING:
2611 if (tv2->v_type == VAR_LIST)
2612 break;
2613 if (*op == '+' || *op == '-')
2614 {
2615 /* nr += nr or nr -= nr*/
2616 n = get_tv_number(tv1);
2617 if (*op == '+')
2618 n += get_tv_number(tv2);
2619 else
2620 n -= get_tv_number(tv2);
2621 clear_tv(tv1);
2622 tv1->v_type = VAR_NUMBER;
2623 tv1->vval.v_number = n;
2624 }
2625 else
2626 {
2627 /* str .= str */
2628 s = get_tv_string(tv1);
2629 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2630 clear_tv(tv1);
2631 tv1->v_type = VAR_STRING;
2632 tv1->vval.v_string = s;
2633 }
2634 return OK;
2635 }
2636 }
2637
2638 EMSG2(_(e_letwrong), op);
2639 return FAIL;
2640}
2641
2642/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002643 * Add a watcher to a list.
2644 */
2645 static void
2646list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002647 list_T *l;
2648 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002649{
2650 lw->lw_next = l->lv_watch;
2651 l->lv_watch = lw;
2652}
2653
2654/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002655 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002656 * No warning when it isn't found...
2657 */
2658 static void
2659list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002660 list_T *l;
2661 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002662{
Bram Moolenaar33570922005-01-25 22:26:29 +00002663 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002664
2665 lwp = &l->lv_watch;
2666 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2667 {
2668 if (lw == lwrem)
2669 {
2670 *lwp = lw->lw_next;
2671 break;
2672 }
2673 lwp = &lw->lw_next;
2674 }
2675}
2676
2677/*
2678 * Just before removing an item from a list: advance watchers to the next
2679 * item.
2680 */
2681 static void
2682list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002683 list_T *l;
2684 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002685{
Bram Moolenaar33570922005-01-25 22:26:29 +00002686 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002687
2688 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2689 if (lw->lw_item == item)
2690 lw->lw_item = item->li_next;
2691}
2692
2693/*
2694 * Evaluate the expression used in a ":for var in expr" command.
2695 * "arg" points to "var".
2696 * Set "*errp" to TRUE for an error, FALSE otherwise;
2697 * Return a pointer that holds the info. Null when there is an error.
2698 */
2699 void *
2700eval_for_line(arg, errp, nextcmdp, skip)
2701 char_u *arg;
2702 int *errp;
2703 char_u **nextcmdp;
2704 int skip;
2705{
Bram Moolenaar33570922005-01-25 22:26:29 +00002706 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002707 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002708 typval_T tv;
2709 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002710
2711 *errp = TRUE; /* default: there is an error */
2712
Bram Moolenaar33570922005-01-25 22:26:29 +00002713 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002714 if (fi == NULL)
2715 return NULL;
2716
2717 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2718 if (expr == NULL)
2719 return fi;
2720
2721 expr = skipwhite(expr);
2722 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2723 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002724 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002725 return fi;
2726 }
2727
2728 if (skip)
2729 ++emsg_skip;
2730 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2731 {
2732 *errp = FALSE;
2733 if (!skip)
2734 {
2735 l = tv.vval.v_list;
2736 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002737 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002738 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002739 clear_tv(&tv);
2740 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002741 else
2742 {
2743 fi->fi_list = l;
2744 list_add_watch(l, &fi->fi_lw);
2745 fi->fi_lw.lw_item = l->lv_first;
2746 }
2747 }
2748 }
2749 if (skip)
2750 --emsg_skip;
2751
2752 return fi;
2753}
2754
2755/*
2756 * Use the first item in a ":for" list. Advance to the next.
2757 * Assign the values to the variable (list). "arg" points to the first one.
2758 * Return TRUE when a valid item was found, FALSE when at end of list or
2759 * something wrong.
2760 */
2761 int
2762next_for_item(fi_void, arg)
2763 void *fi_void;
2764 char_u *arg;
2765{
Bram Moolenaar33570922005-01-25 22:26:29 +00002766 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002767 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002768 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002769
2770 item = fi->fi_lw.lw_item;
2771 if (item == NULL)
2772 result = FALSE;
2773 else
2774 {
2775 fi->fi_lw.lw_item = item->li_next;
2776 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2777 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2778 }
2779 return result;
2780}
2781
2782/*
2783 * Free the structure used to store info used by ":for".
2784 */
2785 void
2786free_for_info(fi_void)
2787 void *fi_void;
2788{
Bram Moolenaar33570922005-01-25 22:26:29 +00002789 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002790
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002791 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002792 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002793 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002794 list_unref(fi->fi_list);
2795 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002796 vim_free(fi);
2797}
2798
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2800
2801 void
2802set_context_for_expression(xp, arg, cmdidx)
2803 expand_T *xp;
2804 char_u *arg;
2805 cmdidx_T cmdidx;
2806{
2807 int got_eq = FALSE;
2808 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002809 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002811 if (cmdidx == CMD_let)
2812 {
2813 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002814 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002815 {
2816 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002817 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002818 {
2819 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002820 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002821 if (vim_iswhite(*p))
2822 break;
2823 }
2824 return;
2825 }
2826 }
2827 else
2828 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2829 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 while ((xp->xp_pattern = vim_strpbrk(arg,
2831 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2832 {
2833 c = *xp->xp_pattern;
2834 if (c == '&')
2835 {
2836 c = xp->xp_pattern[1];
2837 if (c == '&')
2838 {
2839 ++xp->xp_pattern;
2840 xp->xp_context = cmdidx != CMD_let || got_eq
2841 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2842 }
2843 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002846 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2847 xp->xp_pattern += 2;
2848
2849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 }
2851 else if (c == '$')
2852 {
2853 /* environment variable */
2854 xp->xp_context = EXPAND_ENV_VARS;
2855 }
2856 else if (c == '=')
2857 {
2858 got_eq = TRUE;
2859 xp->xp_context = EXPAND_EXPRESSION;
2860 }
2861 else if (c == '<'
2862 && xp->xp_context == EXPAND_FUNCTIONS
2863 && vim_strchr(xp->xp_pattern, '(') == NULL)
2864 {
2865 /* Function name can start with "<SNR>" */
2866 break;
2867 }
2868 else if (cmdidx != CMD_let || got_eq)
2869 {
2870 if (c == '"') /* string */
2871 {
2872 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2873 if (c == '\\' && xp->xp_pattern[1] != NUL)
2874 ++xp->xp_pattern;
2875 xp->xp_context = EXPAND_NOTHING;
2876 }
2877 else if (c == '\'') /* literal string */
2878 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2881 /* skip */ ;
2882 xp->xp_context = EXPAND_NOTHING;
2883 }
2884 else if (c == '|')
2885 {
2886 if (xp->xp_pattern[1] == '|')
2887 {
2888 ++xp->xp_pattern;
2889 xp->xp_context = EXPAND_EXPRESSION;
2890 }
2891 else
2892 xp->xp_context = EXPAND_COMMANDS;
2893 }
2894 else
2895 xp->xp_context = EXPAND_EXPRESSION;
2896 }
2897 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002898 /* Doesn't look like something valid, expand as an expression
2899 * anyway. */
2900 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 arg = xp->xp_pattern;
2902 if (*arg != NUL)
2903 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2904 /* skip */ ;
2905 }
2906 xp->xp_pattern = arg;
2907}
2908
2909#endif /* FEAT_CMDL_COMPL */
2910
2911/*
2912 * ":1,25call func(arg1, arg2)" function call.
2913 */
2914 void
2915ex_call(eap)
2916 exarg_T *eap;
2917{
2918 char_u *arg = eap->arg;
2919 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002921 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002923 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 linenr_T lnum;
2925 int doesrange;
2926 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002927 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002929 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2930 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002931 if (tofree == NULL)
2932 return;
2933
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002934 /* Increase refcount on dictionary, it could get deleted when evaluating
2935 * the arguments. */
2936 if (fudi.fd_dict != NULL)
2937 ++fudi.fd_dict->dv_refcount;
2938
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002939 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2940 len = STRLEN(tofree);
2941 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942
Bram Moolenaar532c7802005-01-27 14:44:31 +00002943 /* Skip white space to allow ":call func ()". Not good, but required for
2944 * backward compatibility. */
2945 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002946 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947
2948 if (*startarg != '(')
2949 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00002950 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 goto end;
2952 }
2953
2954 /*
2955 * When skipping, evaluate the function once, to find the end of the
2956 * arguments.
2957 * When the function takes a range, this is discovered after the first
2958 * call, and the loop is broken.
2959 */
2960 if (eap->skip)
2961 {
2962 ++emsg_skip;
2963 lnum = eap->line2; /* do it once, also with an invalid range */
2964 }
2965 else
2966 lnum = eap->line1;
2967 for ( ; lnum <= eap->line2; ++lnum)
2968 {
2969 if (!eap->skip && eap->addr_count > 0)
2970 {
2971 curwin->w_cursor.lnum = lnum;
2972 curwin->w_cursor.col = 0;
2973 }
2974 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002975 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002976 eap->line1, eap->line2, &doesrange,
2977 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 {
2979 failed = TRUE;
2980 break;
2981 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002982 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 if (doesrange || eap->skip)
2984 break;
2985 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002986 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002987 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002988 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 if (aborting())
2990 break;
2991 }
2992 if (eap->skip)
2993 --emsg_skip;
2994
2995 if (!failed)
2996 {
2997 /* Check for trailing illegal characters and a following command. */
2998 if (!ends_excmd(*arg))
2999 {
3000 emsg_severe = TRUE;
3001 EMSG(_(e_trailing));
3002 }
3003 else
3004 eap->nextcmd = check_nextcmd(arg);
3005 }
3006
3007end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003008 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003009 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010}
3011
3012/*
3013 * ":unlet[!] var1 ... " command.
3014 */
3015 void
3016ex_unlet(eap)
3017 exarg_T *eap;
3018{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003019 ex_unletlock(eap, eap->arg, 0);
3020}
3021
3022/*
3023 * ":lockvar" and ":unlockvar" commands
3024 */
3025 void
3026ex_lockvar(eap)
3027 exarg_T *eap;
3028{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003030 int deep = 2;
3031
3032 if (eap->forceit)
3033 deep = -1;
3034 else if (vim_isdigit(*arg))
3035 {
3036 deep = getdigits(&arg);
3037 arg = skipwhite(arg);
3038 }
3039
3040 ex_unletlock(eap, arg, deep);
3041}
3042
3043/*
3044 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3045 */
3046 static void
3047ex_unletlock(eap, argstart, deep)
3048 exarg_T *eap;
3049 char_u *argstart;
3050 int deep;
3051{
3052 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003055 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056
3057 do
3058 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003060 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3061 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003062 if (lv.ll_name == NULL)
3063 error = TRUE; /* error but continue parsing */
3064 if (name_end == NULL || (!vim_iswhite(*name_end)
3065 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003067 if (name_end != NULL)
3068 {
3069 emsg_severe = TRUE;
3070 EMSG(_(e_trailing));
3071 }
3072 if (!(eap->skip || error))
3073 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 break;
3075 }
3076
3077 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003078 {
3079 if (eap->cmdidx == CMD_unlet)
3080 {
3081 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3082 error = TRUE;
3083 }
3084 else
3085 {
3086 if (do_lock_var(&lv, name_end, deep,
3087 eap->cmdidx == CMD_lockvar) == FAIL)
3088 error = TRUE;
3089 }
3090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003092 if (!eap->skip)
3093 clear_lval(&lv);
3094
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 arg = skipwhite(name_end);
3096 } while (!ends_excmd(*arg));
3097
3098 eap->nextcmd = check_nextcmd(arg);
3099}
3100
Bram Moolenaar8c711452005-01-14 21:53:12 +00003101 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003102do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003103 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003104 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003105 int forceit;
3106{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003107 int ret = OK;
3108 int cc;
3109
3110 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003111 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003112 cc = *name_end;
3113 *name_end = NUL;
3114
3115 /* Normal name or expanded name. */
3116 if (check_changedtick(lp->ll_name))
3117 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003118 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003119 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003120 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003121 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003122 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3123 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003124 else if (lp->ll_range)
3125 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003126 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003127
3128 /* Delete a range of List items. */
3129 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3130 {
3131 li = lp->ll_li->li_next;
3132 listitem_remove(lp->ll_list, lp->ll_li);
3133 lp->ll_li = li;
3134 ++lp->ll_n1;
3135 }
3136 }
3137 else
3138 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003139 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003140 /* unlet a List item. */
3141 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003142 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003143 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003144 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003145 }
3146
3147 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003148}
3149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150/*
3151 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003152 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 */
3154 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003155do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003157 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158{
Bram Moolenaar33570922005-01-25 22:26:29 +00003159 hashtab_T *ht;
3160 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003161 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
Bram Moolenaar33570922005-01-25 22:26:29 +00003163 ht = find_var_ht(name, &varname);
3164 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003166 hi = hash_find(ht, varname);
3167 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003168 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003169 if (var_check_ro(HI2DI(hi)->di_flags, name))
3170 return FAIL;
3171 delete_var(ht, hi);
3172 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003175 if (forceit)
3176 return OK;
3177 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 return FAIL;
3179}
3180
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003181/*
3182 * Lock or unlock variable indicated by "lp".
3183 * "deep" is the levels to go (-1 for unlimited);
3184 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3185 */
3186 static int
3187do_lock_var(lp, name_end, deep, lock)
3188 lval_T *lp;
3189 char_u *name_end;
3190 int deep;
3191 int lock;
3192{
3193 int ret = OK;
3194 int cc;
3195 dictitem_T *di;
3196
3197 if (deep == 0) /* nothing to do */
3198 return OK;
3199
3200 if (lp->ll_tv == NULL)
3201 {
3202 cc = *name_end;
3203 *name_end = NUL;
3204
3205 /* Normal name or expanded name. */
3206 if (check_changedtick(lp->ll_name))
3207 ret = FAIL;
3208 else
3209 {
3210 di = find_var(lp->ll_name, NULL);
3211 if (di == NULL)
3212 ret = FAIL;
3213 else
3214 {
3215 if (lock)
3216 di->di_flags |= DI_FLAGS_LOCK;
3217 else
3218 di->di_flags &= ~DI_FLAGS_LOCK;
3219 item_lock(&di->di_tv, deep, lock);
3220 }
3221 }
3222 *name_end = cc;
3223 }
3224 else if (lp->ll_range)
3225 {
3226 listitem_T *li = lp->ll_li;
3227
3228 /* (un)lock a range of List items. */
3229 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3230 {
3231 item_lock(&li->li_tv, deep, lock);
3232 li = li->li_next;
3233 ++lp->ll_n1;
3234 }
3235 }
3236 else if (lp->ll_list != NULL)
3237 /* (un)lock a List item. */
3238 item_lock(&lp->ll_li->li_tv, deep, lock);
3239 else
3240 /* un(lock) a Dictionary item. */
3241 item_lock(&lp->ll_di->di_tv, deep, lock);
3242
3243 return ret;
3244}
3245
3246/*
3247 * Lock or unlock an item. "deep" is nr of levels to go.
3248 */
3249 static void
3250item_lock(tv, deep, lock)
3251 typval_T *tv;
3252 int deep;
3253 int lock;
3254{
3255 static int recurse = 0;
3256 list_T *l;
3257 listitem_T *li;
3258 dict_T *d;
3259 hashitem_T *hi;
3260 int todo;
3261
3262 if (recurse >= DICT_MAXNEST)
3263 {
3264 EMSG(_("E743: variable nested too deep for (un)lock"));
3265 return;
3266 }
3267 if (deep == 0)
3268 return;
3269 ++recurse;
3270
3271 /* lock/unlock the item itself */
3272 if (lock)
3273 tv->v_lock |= VAR_LOCKED;
3274 else
3275 tv->v_lock &= ~VAR_LOCKED;
3276
3277 switch (tv->v_type)
3278 {
3279 case VAR_LIST:
3280 if ((l = tv->vval.v_list) != NULL)
3281 {
3282 if (lock)
3283 l->lv_lock |= VAR_LOCKED;
3284 else
3285 l->lv_lock &= ~VAR_LOCKED;
3286 if (deep < 0 || deep > 1)
3287 /* recursive: lock/unlock the items the List contains */
3288 for (li = l->lv_first; li != NULL; li = li->li_next)
3289 item_lock(&li->li_tv, deep - 1, lock);
3290 }
3291 break;
3292 case VAR_DICT:
3293 if ((d = tv->vval.v_dict) != NULL)
3294 {
3295 if (lock)
3296 d->dv_lock |= VAR_LOCKED;
3297 else
3298 d->dv_lock &= ~VAR_LOCKED;
3299 if (deep < 0 || deep > 1)
3300 {
3301 /* recursive: lock/unlock the items the List contains */
3302 todo = d->dv_hashtab.ht_used;
3303 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3304 {
3305 if (!HASHITEM_EMPTY(hi))
3306 {
3307 --todo;
3308 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3309 }
3310 }
3311 }
3312 }
3313 }
3314 --recurse;
3315}
3316
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3318/*
3319 * Delete all "menutrans_" variables.
3320 */
3321 void
3322del_menutrans_vars()
3323{
Bram Moolenaar33570922005-01-25 22:26:29 +00003324 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003325 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
Bram Moolenaar33570922005-01-25 22:26:29 +00003327 hash_lock(&globvarht);
3328 todo = globvarht.ht_used;
3329 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003330 {
3331 if (!HASHITEM_EMPTY(hi))
3332 {
3333 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003334 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3335 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003336 }
3337 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003338 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339}
3340#endif
3341
3342#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3343
3344/*
3345 * Local string buffer for the next two functions to store a variable name
3346 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3347 * get_user_var_name().
3348 */
3349
3350static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3351
3352static char_u *varnamebuf = NULL;
3353static int varnamebuflen = 0;
3354
3355/*
3356 * Function to concatenate a prefix and a variable name.
3357 */
3358 static char_u *
3359cat_prefix_varname(prefix, name)
3360 int prefix;
3361 char_u *name;
3362{
3363 int len;
3364
3365 len = (int)STRLEN(name) + 3;
3366 if (len > varnamebuflen)
3367 {
3368 vim_free(varnamebuf);
3369 len += 10; /* some additional space */
3370 varnamebuf = alloc(len);
3371 if (varnamebuf == NULL)
3372 {
3373 varnamebuflen = 0;
3374 return NULL;
3375 }
3376 varnamebuflen = len;
3377 }
3378 *varnamebuf = prefix;
3379 varnamebuf[1] = ':';
3380 STRCPY(varnamebuf + 2, name);
3381 return varnamebuf;
3382}
3383
3384/*
3385 * Function given to ExpandGeneric() to obtain the list of user defined
3386 * (global/buffer/window/built-in) variable names.
3387 */
3388/*ARGSUSED*/
3389 char_u *
3390get_user_var_name(xp, idx)
3391 expand_T *xp;
3392 int idx;
3393{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003394 static long_u gdone;
3395 static long_u bdone;
3396 static long_u wdone;
3397 static int vidx;
3398 static hashitem_T *hi;
3399 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
3401 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003402 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003403
3404 /* Global variables */
3405 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003407 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003408 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003409 else
3410 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003411 while (HASHITEM_EMPTY(hi))
3412 ++hi;
3413 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3414 return cat_prefix_varname('g', hi->hi_key);
3415 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003417
3418 /* b: variables */
3419 ht = &curbuf->b_vars.dv_hashtab;
3420 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003422 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003423 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003424 else
3425 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003426 while (HASHITEM_EMPTY(hi))
3427 ++hi;
3428 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003430 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003432 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 return (char_u *)"b:changedtick";
3434 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003435
3436 /* w: variables */
3437 ht = &curwin->w_vars.dv_hashtab;
3438 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003440 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003441 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003442 else
3443 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003444 while (HASHITEM_EMPTY(hi))
3445 ++hi;
3446 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003448
3449 /* v: variables */
3450 if (vidx < VV_LEN)
3451 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452
3453 vim_free(varnamebuf);
3454 varnamebuf = NULL;
3455 varnamebuflen = 0;
3456 return NULL;
3457}
3458
3459#endif /* FEAT_CMDL_COMPL */
3460
3461/*
3462 * types for expressions.
3463 */
3464typedef enum
3465{
3466 TYPE_UNKNOWN = 0
3467 , TYPE_EQUAL /* == */
3468 , TYPE_NEQUAL /* != */
3469 , TYPE_GREATER /* > */
3470 , TYPE_GEQUAL /* >= */
3471 , TYPE_SMALLER /* < */
3472 , TYPE_SEQUAL /* <= */
3473 , TYPE_MATCH /* =~ */
3474 , TYPE_NOMATCH /* !~ */
3475} exptype_T;
3476
3477/*
3478 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003479 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3481 */
3482
3483/*
3484 * Handle zero level expression.
3485 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003486 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 * Return OK or FAIL.
3488 */
3489 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003490eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 char_u **nextcmd;
3494 int evaluate;
3495{
3496 int ret;
3497 char_u *p;
3498
3499 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003500 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 if (ret == FAIL || !ends_excmd(*p))
3502 {
3503 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003504 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 /*
3506 * Report the invalid expression unless the expression evaluation has
3507 * been cancelled due to an aborting error, an interrupt, or an
3508 * exception.
3509 */
3510 if (!aborting())
3511 EMSG2(_(e_invexpr2), arg);
3512 ret = FAIL;
3513 }
3514 if (nextcmd != NULL)
3515 *nextcmd = check_nextcmd(p);
3516
3517 return ret;
3518}
3519
3520/*
3521 * Handle top level expression:
3522 * expr1 ? expr0 : expr0
3523 *
3524 * "arg" must point to the first non-white of the expression.
3525 * "arg" is advanced to the next non-white after the recognized expression.
3526 *
3527 * Return OK or FAIL.
3528 */
3529 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003530eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 int evaluate;
3534{
3535 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003536 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
3538 /*
3539 * Get the first variable.
3540 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003541 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 return FAIL;
3543
3544 if ((*arg)[0] == '?')
3545 {
3546 result = FALSE;
3547 if (evaluate)
3548 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003549 int error = FALSE;
3550
3551 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003553 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003554 if (error)
3555 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
3557
3558 /*
3559 * Get the second variable.
3560 */
3561 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003562 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 return FAIL;
3564
3565 /*
3566 * Check for the ":".
3567 */
3568 if ((*arg)[0] != ':')
3569 {
3570 EMSG(_("E109: Missing ':' after '?'"));
3571 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003572 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 return FAIL;
3574 }
3575
3576 /*
3577 * Get the third variable.
3578 */
3579 *arg = skipwhite(*arg + 1);
3580 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3581 {
3582 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003583 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 return FAIL;
3585 }
3586 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003587 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
3589
3590 return OK;
3591}
3592
3593/*
3594 * Handle first level expression:
3595 * expr2 || expr2 || expr2 logical OR
3596 *
3597 * "arg" must point to the first non-white of the expression.
3598 * "arg" is advanced to the next non-white after the recognized expression.
3599 *
3600 * Return OK or FAIL.
3601 */
3602 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003603eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 int evaluate;
3607{
Bram Moolenaar33570922005-01-25 22:26:29 +00003608 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 long result;
3610 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003611 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
3613 /*
3614 * Get the first variable.
3615 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003616 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 return FAIL;
3618
3619 /*
3620 * Repeat until there is no following "||".
3621 */
3622 first = TRUE;
3623 result = FALSE;
3624 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3625 {
3626 if (evaluate && first)
3627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003628 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003630 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003631 if (error)
3632 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 first = FALSE;
3634 }
3635
3636 /*
3637 * Get the second variable.
3638 */
3639 *arg = skipwhite(*arg + 2);
3640 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3641 return FAIL;
3642
3643 /*
3644 * Compute the result.
3645 */
3646 if (evaluate && !result)
3647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003648 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003650 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003651 if (error)
3652 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 }
3654 if (evaluate)
3655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003656 rettv->v_type = VAR_NUMBER;
3657 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
3659 }
3660
3661 return OK;
3662}
3663
3664/*
3665 * Handle second level expression:
3666 * expr3 && expr3 && expr3 logical AND
3667 *
3668 * "arg" must point to the first non-white of the expression.
3669 * "arg" is advanced to the next non-white after the recognized expression.
3670 *
3671 * Return OK or FAIL.
3672 */
3673 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003674eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 int evaluate;
3678{
Bram Moolenaar33570922005-01-25 22:26:29 +00003679 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 long result;
3681 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003682 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
3684 /*
3685 * Get the first variable.
3686 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003687 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 return FAIL;
3689
3690 /*
3691 * Repeat until there is no following "&&".
3692 */
3693 first = TRUE;
3694 result = TRUE;
3695 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3696 {
3697 if (evaluate && first)
3698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003699 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003701 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003702 if (error)
3703 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 first = FALSE;
3705 }
3706
3707 /*
3708 * Get the second variable.
3709 */
3710 *arg = skipwhite(*arg + 2);
3711 if (eval4(arg, &var2, evaluate && result) == FAIL)
3712 return FAIL;
3713
3714 /*
3715 * Compute the result.
3716 */
3717 if (evaluate && result)
3718 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003719 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003721 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003722 if (error)
3723 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
3725 if (evaluate)
3726 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003727 rettv->v_type = VAR_NUMBER;
3728 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 }
3730 }
3731
3732 return OK;
3733}
3734
3735/*
3736 * Handle third level expression:
3737 * var1 == var2
3738 * var1 =~ var2
3739 * var1 != var2
3740 * var1 !~ var2
3741 * var1 > var2
3742 * var1 >= var2
3743 * var1 < var2
3744 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003745 * var1 is var2
3746 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 *
3748 * "arg" must point to the first non-white of the expression.
3749 * "arg" is advanced to the next non-white after the recognized expression.
3750 *
3751 * Return OK or FAIL.
3752 */
3753 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003754eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 int evaluate;
3758{
Bram Moolenaar33570922005-01-25 22:26:29 +00003759 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 char_u *p;
3761 int i;
3762 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003763 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 int len = 2;
3765 long n1, n2;
3766 char_u *s1, *s2;
3767 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3768 regmatch_T regmatch;
3769 int ic;
3770 char_u *save_cpo;
3771
3772 /*
3773 * Get the first variable.
3774 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003775 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 return FAIL;
3777
3778 p = *arg;
3779 switch (p[0])
3780 {
3781 case '=': if (p[1] == '=')
3782 type = TYPE_EQUAL;
3783 else if (p[1] == '~')
3784 type = TYPE_MATCH;
3785 break;
3786 case '!': if (p[1] == '=')
3787 type = TYPE_NEQUAL;
3788 else if (p[1] == '~')
3789 type = TYPE_NOMATCH;
3790 break;
3791 case '>': if (p[1] != '=')
3792 {
3793 type = TYPE_GREATER;
3794 len = 1;
3795 }
3796 else
3797 type = TYPE_GEQUAL;
3798 break;
3799 case '<': if (p[1] != '=')
3800 {
3801 type = TYPE_SMALLER;
3802 len = 1;
3803 }
3804 else
3805 type = TYPE_SEQUAL;
3806 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003807 case 'i': if (p[1] == 's')
3808 {
3809 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3810 len = 5;
3811 if (!vim_isIDc(p[len]))
3812 {
3813 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3814 type_is = TRUE;
3815 }
3816 }
3817 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819
3820 /*
3821 * If there is a comparitive operator, use it.
3822 */
3823 if (type != TYPE_UNKNOWN)
3824 {
3825 /* extra question mark appended: ignore case */
3826 if (p[len] == '?')
3827 {
3828 ic = TRUE;
3829 ++len;
3830 }
3831 /* extra '#' appended: match case */
3832 else if (p[len] == '#')
3833 {
3834 ic = FALSE;
3835 ++len;
3836 }
3837 /* nothing appened: use 'ignorecase' */
3838 else
3839 ic = p_ic;
3840
3841 /*
3842 * Get the second variable.
3843 */
3844 *arg = skipwhite(p + len);
3845 if (eval5(arg, &var2, evaluate) == FAIL)
3846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003847 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 return FAIL;
3849 }
3850
3851 if (evaluate)
3852 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003853 if (type_is && rettv->v_type != var2.v_type)
3854 {
3855 /* For "is" a different type always means FALSE, for "notis"
3856 * it means TRUE. */
3857 n1 = (type == TYPE_NEQUAL);
3858 }
3859 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3860 {
3861 if (type_is)
3862 {
3863 n1 = (rettv->v_type == var2.v_type
3864 && rettv->vval.v_list == var2.vval.v_list);
3865 if (type == TYPE_NEQUAL)
3866 n1 = !n1;
3867 }
3868 else if (rettv->v_type != var2.v_type
3869 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3870 {
3871 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003872 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003873 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003874 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003875 clear_tv(rettv);
3876 clear_tv(&var2);
3877 return FAIL;
3878 }
3879 else
3880 {
3881 /* Compare two Lists for being equal or unequal. */
3882 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3883 if (type == TYPE_NEQUAL)
3884 n1 = !n1;
3885 }
3886 }
3887
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003888 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3889 {
3890 if (type_is)
3891 {
3892 n1 = (rettv->v_type == var2.v_type
3893 && rettv->vval.v_dict == var2.vval.v_dict);
3894 if (type == TYPE_NEQUAL)
3895 n1 = !n1;
3896 }
3897 else if (rettv->v_type != var2.v_type
3898 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3899 {
3900 if (rettv->v_type != var2.v_type)
3901 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3902 else
3903 EMSG(_("E736: Invalid operation for Dictionary"));
3904 clear_tv(rettv);
3905 clear_tv(&var2);
3906 return FAIL;
3907 }
3908 else
3909 {
3910 /* Compare two Dictionaries for being equal or unequal. */
3911 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3912 if (type == TYPE_NEQUAL)
3913 n1 = !n1;
3914 }
3915 }
3916
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003917 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3918 {
3919 if (rettv->v_type != var2.v_type
3920 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3921 {
3922 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003923 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003924 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003925 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003926 clear_tv(rettv);
3927 clear_tv(&var2);
3928 return FAIL;
3929 }
3930 else
3931 {
3932 /* Compare two Funcrefs for being equal or unequal. */
3933 if (rettv->vval.v_string == NULL
3934 || var2.vval.v_string == NULL)
3935 n1 = FALSE;
3936 else
3937 n1 = STRCMP(rettv->vval.v_string,
3938 var2.vval.v_string) == 0;
3939 if (type == TYPE_NEQUAL)
3940 n1 = !n1;
3941 }
3942 }
3943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 /*
3945 * If one of the two variables is a number, compare as a number.
3946 * When using "=~" or "!~", always compare as string.
3947 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003948 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3950 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003951 n1 = get_tv_number(rettv);
3952 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 switch (type)
3954 {
3955 case TYPE_EQUAL: n1 = (n1 == n2); break;
3956 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3957 case TYPE_GREATER: n1 = (n1 > n2); break;
3958 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3959 case TYPE_SMALLER: n1 = (n1 < n2); break;
3960 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3961 case TYPE_UNKNOWN:
3962 case TYPE_MATCH:
3963 case TYPE_NOMATCH: break; /* avoid gcc warning */
3964 }
3965 }
3966 else
3967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003968 s1 = get_tv_string_buf(rettv, buf1);
3969 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3971 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3972 else
3973 i = 0;
3974 n1 = FALSE;
3975 switch (type)
3976 {
3977 case TYPE_EQUAL: n1 = (i == 0); break;
3978 case TYPE_NEQUAL: n1 = (i != 0); break;
3979 case TYPE_GREATER: n1 = (i > 0); break;
3980 case TYPE_GEQUAL: n1 = (i >= 0); break;
3981 case TYPE_SMALLER: n1 = (i < 0); break;
3982 case TYPE_SEQUAL: n1 = (i <= 0); break;
3983
3984 case TYPE_MATCH:
3985 case TYPE_NOMATCH:
3986 /* avoid 'l' flag in 'cpoptions' */
3987 save_cpo = p_cpo;
3988 p_cpo = (char_u *)"";
3989 regmatch.regprog = vim_regcomp(s2,
3990 RE_MAGIC + RE_STRING);
3991 regmatch.rm_ic = ic;
3992 if (regmatch.regprog != NULL)
3993 {
3994 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3995 vim_free(regmatch.regprog);
3996 if (type == TYPE_NOMATCH)
3997 n1 = !n1;
3998 }
3999 p_cpo = save_cpo;
4000 break;
4001
4002 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4003 }
4004 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004005 clear_tv(rettv);
4006 clear_tv(&var2);
4007 rettv->v_type = VAR_NUMBER;
4008 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
4010 }
4011
4012 return OK;
4013}
4014
4015/*
4016 * Handle fourth level expression:
4017 * + number addition
4018 * - number subtraction
4019 * . string concatenation
4020 *
4021 * "arg" must point to the first non-white of the expression.
4022 * "arg" is advanced to the next non-white after the recognized expression.
4023 *
4024 * Return OK or FAIL.
4025 */
4026 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 int evaluate;
4031{
Bram Moolenaar33570922005-01-25 22:26:29 +00004032 typval_T var2;
4033 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 int op;
4035 long n1, n2;
4036 char_u *s1, *s2;
4037 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4038 char_u *p;
4039
4040 /*
4041 * Get the first variable.
4042 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 return FAIL;
4045
4046 /*
4047 * Repeat computing, until no '+', '-' or '.' is following.
4048 */
4049 for (;;)
4050 {
4051 op = **arg;
4052 if (op != '+' && op != '-' && op != '.')
4053 break;
4054
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004055 if (op != '+' || rettv->v_type != VAR_LIST)
4056 {
4057 /* For "list + ...", an illegal use of the first operand as
4058 * a number cannot be determined before evaluating the 2nd
4059 * operand: if this is also a list, all is ok.
4060 * For "something . ...", "something - ..." or "non-list + ...",
4061 * we know that the first operand needs to be a string or number
4062 * without evaluating the 2nd operand. So check before to avoid
4063 * side effects after an error. */
4064 if (evaluate && get_tv_string_chk(rettv) == NULL)
4065 {
4066 clear_tv(rettv);
4067 return FAIL;
4068 }
4069 }
4070
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 /*
4072 * Get the second variable.
4073 */
4074 *arg = skipwhite(*arg + 1);
4075 if (eval6(arg, &var2, evaluate) == FAIL)
4076 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004077 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 return FAIL;
4079 }
4080
4081 if (evaluate)
4082 {
4083 /*
4084 * Compute the result.
4085 */
4086 if (op == '.')
4087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004088 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4089 s2 = get_tv_string_buf_chk(&var2, buf2);
4090 if (s2 == NULL) /* type error ? */
4091 {
4092 clear_tv(rettv);
4093 clear_tv(&var2);
4094 return FAIL;
4095 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004096 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 clear_tv(rettv);
4098 rettv->v_type = VAR_STRING;
4099 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004101 else if (op == '+' && rettv->v_type == VAR_LIST
4102 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004103 {
4104 /* concatenate Lists */
4105 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4106 &var3) == FAIL)
4107 {
4108 clear_tv(rettv);
4109 clear_tv(&var2);
4110 return FAIL;
4111 }
4112 clear_tv(rettv);
4113 *rettv = var3;
4114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 else
4116 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004117 int error = FALSE;
4118
4119 n1 = get_tv_number_chk(rettv, &error);
4120 if (error)
4121 {
4122 /* This can only happen for "list + non-list".
4123 * For "non-list + ..." or "something - ...", we returned
4124 * before evaluating the 2nd operand. */
4125 clear_tv(rettv);
4126 return FAIL;
4127 }
4128 n2 = get_tv_number_chk(&var2, &error);
4129 if (error)
4130 {
4131 clear_tv(rettv);
4132 clear_tv(&var2);
4133 return FAIL;
4134 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 if (op == '+')
4137 n1 = n1 + n2;
4138 else
4139 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 rettv->v_type = VAR_NUMBER;
4141 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
4145 }
4146 return OK;
4147}
4148
4149/*
4150 * Handle fifth level expression:
4151 * * number multiplication
4152 * / number division
4153 * % number modulo
4154 *
4155 * "arg" must point to the first non-white of the expression.
4156 * "arg" is advanced to the next non-white after the recognized expression.
4157 *
4158 * Return OK or FAIL.
4159 */
4160 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004161eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 int evaluate;
4165{
Bram Moolenaar33570922005-01-25 22:26:29 +00004166 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 int op;
4168 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004169 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
4171 /*
4172 * Get the first variable.
4173 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004174 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 return FAIL;
4176
4177 /*
4178 * Repeat computing, until no '*', '/' or '%' is following.
4179 */
4180 for (;;)
4181 {
4182 op = **arg;
4183 if (op != '*' && op != '/' && op != '%')
4184 break;
4185
4186 if (evaluate)
4187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 if (error)
4191 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 else
4194 n1 = 0;
4195
4196 /*
4197 * Get the second variable.
4198 */
4199 *arg = skipwhite(*arg + 1);
4200 if (eval7(arg, &var2, evaluate) == FAIL)
4201 return FAIL;
4202
4203 if (evaluate)
4204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004205 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004207 if (error)
4208 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209
4210 /*
4211 * Compute the result.
4212 */
4213 if (op == '*')
4214 n1 = n1 * n2;
4215 else if (op == '/')
4216 {
4217 if (n2 == 0) /* give an error message? */
4218 n1 = 0x7fffffffL;
4219 else
4220 n1 = n1 / n2;
4221 }
4222 else
4223 {
4224 if (n2 == 0) /* give an error message? */
4225 n1 = 0;
4226 else
4227 n1 = n1 % n2;
4228 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 rettv->v_type = VAR_NUMBER;
4230 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
4232 }
4233
4234 return OK;
4235}
4236
4237/*
4238 * Handle sixth level expression:
4239 * number number constant
4240 * "string" string contstant
4241 * 'string' literal string contstant
4242 * &option-name option value
4243 * @r register contents
4244 * identifier variable value
4245 * function() function call
4246 * $VAR environment variable
4247 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004248 * [expr, expr] List
4249 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 *
4251 * Also handle:
4252 * ! in front logical NOT
4253 * - in front unary minus
4254 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004255 * trailing [] subscript in String or List
4256 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 *
4258 * "arg" must point to the first non-white of the expression.
4259 * "arg" is advanced to the next non-white after the recognized expression.
4260 *
4261 * Return OK or FAIL.
4262 */
4263 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 int evaluate;
4268{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 long n;
4270 int len;
4271 char_u *s;
4272 int val;
4273 char_u *start_leader, *end_leader;
4274 int ret = OK;
4275 char_u *alias;
4276
4277 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004279 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282
4283 /*
4284 * Skip '!' and '-' characters. They are handled later.
4285 */
4286 start_leader = *arg;
4287 while (**arg == '!' || **arg == '-' || **arg == '+')
4288 *arg = skipwhite(*arg + 1);
4289 end_leader = *arg;
4290
4291 switch (**arg)
4292 {
4293 /*
4294 * Number constant.
4295 */
4296 case '0':
4297 case '1':
4298 case '2':
4299 case '3':
4300 case '4':
4301 case '5':
4302 case '6':
4303 case '7':
4304 case '8':
4305 case '9':
4306 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4307 *arg += len;
4308 if (evaluate)
4309 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004310 rettv->v_type = VAR_NUMBER;
4311 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 }
4313 break;
4314
4315 /*
4316 * String constant: "string".
4317 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 break;
4320
4321 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004322 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004325 break;
4326
4327 /*
4328 * List: [expr, expr]
4329 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004330 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 break;
4332
4333 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004334 * Dictionary: {key: val, key: val}
4335 */
4336 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4337 break;
4338
4339 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004340 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004342 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 break;
4344
4345 /*
4346 * Environment variable: $VAR.
4347 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004348 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 break;
4350
4351 /*
4352 * Register contents: @r.
4353 */
4354 case '@': ++*arg;
4355 if (evaluate)
4356 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004357 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004358 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
4360 if (**arg != NUL)
4361 ++*arg;
4362 break;
4363
4364 /*
4365 * nested expression: (expression).
4366 */
4367 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004368 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 if (**arg == ')')
4370 ++*arg;
4371 else if (ret == OK)
4372 {
4373 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004374 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 ret = FAIL;
4376 }
4377 break;
4378
Bram Moolenaar8c711452005-01-14 21:53:12 +00004379 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 break;
4381 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004382
4383 if (ret == NOTDONE)
4384 {
4385 /*
4386 * Must be a variable or function name.
4387 * Can also be a curly-braces kind of name: {expr}.
4388 */
4389 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004390 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004391 if (alias != NULL)
4392 s = alias;
4393
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004394 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004395 ret = FAIL;
4396 else
4397 {
4398 if (**arg == '(') /* recursive! */
4399 {
4400 /* If "s" is the name of a variable of type VAR_FUNC
4401 * use its contents. */
4402 s = deref_func_name(s, &len);
4403
4404 /* Invoke the function. */
4405 ret = get_func_tv(s, len, rettv, arg,
4406 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004407 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004408 /* Stop the expression evaluation when immediately
4409 * aborting on error, or when an interrupt occurred or
4410 * an exception was thrown but not caught. */
4411 if (aborting())
4412 {
4413 if (ret == OK)
4414 clear_tv(rettv);
4415 ret = FAIL;
4416 }
4417 }
4418 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004419 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004420 else
4421 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004422 }
4423
4424 if (alias != NULL)
4425 vim_free(alias);
4426 }
4427
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 *arg = skipwhite(*arg);
4429
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004430 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4431 * expr(expr). */
4432 if (ret == OK)
4433 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434
4435 /*
4436 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4437 */
4438 if (ret == OK && evaluate && end_leader > start_leader)
4439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004440 int error = FALSE;
4441
4442 val = get_tv_number_chk(rettv, &error);
4443 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004445 clear_tv(rettv);
4446 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004448 else
4449 {
4450 while (end_leader > start_leader)
4451 {
4452 --end_leader;
4453 if (*end_leader == '!')
4454 val = !val;
4455 else if (*end_leader == '-')
4456 val = -val;
4457 }
4458 clear_tv(rettv);
4459 rettv->v_type = VAR_NUMBER;
4460 rettv->vval.v_number = val;
4461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 }
4463
4464 return ret;
4465}
4466
4467/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004468 * Evaluate an "[expr]" or "[expr:expr]" index.
4469 * "*arg" points to the '['.
4470 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4471 */
4472 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004473eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004474 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004475 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004476 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004477 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004478{
4479 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004480 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004481 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004482 long len = -1;
4483 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004484 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004485 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004486
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004487 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004488 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004489 if (verbose)
4490 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004491 return FAIL;
4492 }
4493
Bram Moolenaar8c711452005-01-14 21:53:12 +00004494 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004495 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004496 /*
4497 * dict.name
4498 */
4499 key = *arg + 1;
4500 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4501 ;
4502 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004503 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004505 }
4506 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004507 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004508 /*
4509 * something[idx]
4510 *
4511 * Get the (first) variable from inside the [].
4512 */
4513 *arg = skipwhite(*arg + 1);
4514 if (**arg == ':')
4515 empty1 = TRUE;
4516 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4517 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004518 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4519 {
4520 /* not a number or string */
4521 clear_tv(&var1);
4522 return FAIL;
4523 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004524
4525 /*
4526 * Get the second variable from inside the [:].
4527 */
4528 if (**arg == ':')
4529 {
4530 range = TRUE;
4531 *arg = skipwhite(*arg + 1);
4532 if (**arg == ']')
4533 empty2 = TRUE;
4534 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004536 if (!empty1)
4537 clear_tv(&var1);
4538 return FAIL;
4539 }
4540 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4541 {
4542 /* not a number or string */
4543 if (!empty1)
4544 clear_tv(&var1);
4545 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004546 return FAIL;
4547 }
4548 }
4549
4550 /* Check for the ']'. */
4551 if (**arg != ']')
4552 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004553 if (verbose)
4554 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004555 clear_tv(&var1);
4556 if (range)
4557 clear_tv(&var2);
4558 return FAIL;
4559 }
4560 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004561 }
4562
4563 if (evaluate)
4564 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004565 n1 = 0;
4566 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004568 n1 = get_tv_number(&var1);
4569 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004570 }
4571 if (range)
4572 {
4573 if (empty2)
4574 n2 = -1;
4575 else
4576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004577 n2 = get_tv_number(&var2);
4578 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004579 }
4580 }
4581
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004582 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 {
4584 case VAR_NUMBER:
4585 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004586 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004587 len = (long)STRLEN(s);
4588 if (range)
4589 {
4590 /* The resulting variable is a substring. If the indexes
4591 * are out of range the result is empty. */
4592 if (n1 < 0)
4593 {
4594 n1 = len + n1;
4595 if (n1 < 0)
4596 n1 = 0;
4597 }
4598 if (n2 < 0)
4599 n2 = len + n2;
4600 else if (n2 >= len)
4601 n2 = len;
4602 if (n1 >= len || n2 < 0 || n1 > n2)
4603 s = NULL;
4604 else
4605 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4606 }
4607 else
4608 {
4609 /* The resulting variable is a string of a single
4610 * character. If the index is too big or negative the
4611 * result is empty. */
4612 if (n1 >= len || n1 < 0)
4613 s = NULL;
4614 else
4615 s = vim_strnsave(s + n1, 1);
4616 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 clear_tv(rettv);
4618 rettv->v_type = VAR_STRING;
4619 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620 break;
4621
4622 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004623 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004624 if (n1 < 0)
4625 n1 = len + n1;
4626 if (!empty1 && (n1 < 0 || n1 >= len))
4627 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004628 if (verbose)
4629 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004630 return FAIL;
4631 }
4632 if (range)
4633 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004634 list_T *l;
4635 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004636
4637 if (n2 < 0)
4638 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004639 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004640 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004641 if (verbose)
4642 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004643 return FAIL;
4644 }
4645 l = list_alloc();
4646 if (l == NULL)
4647 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004648 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004649 n1 <= n2; ++n1)
4650 {
4651 if (list_append_tv(l, &item->li_tv) == FAIL)
4652 {
4653 list_free(l);
4654 return FAIL;
4655 }
4656 item = item->li_next;
4657 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004658 clear_tv(rettv);
4659 rettv->v_type = VAR_LIST;
4660 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004661 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004662 }
4663 else
4664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004665 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004666 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004667 clear_tv(rettv);
4668 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004669 }
4670 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671
4672 case VAR_DICT:
4673 if (range)
4674 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004675 if (verbose)
4676 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 if (len == -1)
4678 clear_tv(&var1);
4679 return FAIL;
4680 }
4681 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004682 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004683
4684 if (len == -1)
4685 {
4686 key = get_tv_string(&var1);
4687 if (*key == NUL)
4688 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004689 if (verbose)
4690 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004691 clear_tv(&var1);
4692 return FAIL;
4693 }
4694 }
4695
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004696 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004697
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004698 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004699 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004700 if (len == -1)
4701 clear_tv(&var1);
4702 if (item == NULL)
4703 return FAIL;
4704
4705 copy_tv(&item->di_tv, &var1);
4706 clear_tv(rettv);
4707 *rettv = var1;
4708 }
4709 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004710 }
4711 }
4712
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004713 return OK;
4714}
4715
4716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 * Get an option value.
4718 * "arg" points to the '&' or '+' before the option name.
4719 * "arg" is advanced to character after the option name.
4720 * Return OK or FAIL.
4721 */
4722 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004723get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004725 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 int evaluate;
4727{
4728 char_u *option_end;
4729 long numval;
4730 char_u *stringval;
4731 int opt_type;
4732 int c;
4733 int working = (**arg == '+'); /* has("+option") */
4734 int ret = OK;
4735 int opt_flags;
4736
4737 /*
4738 * Isolate the option name and find its value.
4739 */
4740 option_end = find_option_end(arg, &opt_flags);
4741 if (option_end == NULL)
4742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004743 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 EMSG2(_("E112: Option name missing: %s"), *arg);
4745 return FAIL;
4746 }
4747
4748 if (!evaluate)
4749 {
4750 *arg = option_end;
4751 return OK;
4752 }
4753
4754 c = *option_end;
4755 *option_end = NUL;
4756 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004757 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
4759 if (opt_type == -3) /* invalid name */
4760 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004761 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 EMSG2(_("E113: Unknown option: %s"), *arg);
4763 ret = FAIL;
4764 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004765 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
4767 if (opt_type == -2) /* hidden string option */
4768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004769 rettv->v_type = VAR_STRING;
4770 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 }
4772 else if (opt_type == -1) /* hidden number option */
4773 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004774 rettv->v_type = VAR_NUMBER;
4775 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 }
4777 else if (opt_type == 1) /* number option */
4778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 rettv->v_type = VAR_NUMBER;
4780 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
4782 else /* string option */
4783 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004784 rettv->v_type = VAR_STRING;
4785 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
4787 }
4788 else if (working && (opt_type == -2 || opt_type == -1))
4789 ret = FAIL;
4790
4791 *option_end = c; /* put back for error messages */
4792 *arg = option_end;
4793
4794 return ret;
4795}
4796
4797/*
4798 * Allocate a variable for a string constant.
4799 * Return OK or FAIL.
4800 */
4801 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004802get_string_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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 int evaluate;
4806{
4807 char_u *p;
4808 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 int extra = 0;
4810
4811 /*
4812 * Find the end of the string, skipping backslashed characters.
4813 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004814 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 {
4816 if (*p == '\\' && p[1] != NUL)
4817 {
4818 ++p;
4819 /* A "\<x>" form occupies at least 4 characters, and produces up
4820 * to 6 characters: reserve space for 2 extra */
4821 if (*p == '<')
4822 extra += 2;
4823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 }
4825
4826 if (*p != '"')
4827 {
4828 EMSG2(_("E114: Missing quote: %s"), *arg);
4829 return FAIL;
4830 }
4831
4832 /* If only parsing, set *arg and return here */
4833 if (!evaluate)
4834 {
4835 *arg = p + 1;
4836 return OK;
4837 }
4838
4839 /*
4840 * Copy the string into allocated memory, handling backslashed
4841 * characters.
4842 */
4843 name = alloc((unsigned)(p - *arg + extra));
4844 if (name == NULL)
4845 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004846 rettv->v_type = VAR_STRING;
4847 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848
Bram Moolenaar8c711452005-01-14 21:53:12 +00004849 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
4851 if (*p == '\\')
4852 {
4853 switch (*++p)
4854 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004855 case 'b': *name++ = BS; ++p; break;
4856 case 'e': *name++ = ESC; ++p; break;
4857 case 'f': *name++ = FF; ++p; break;
4858 case 'n': *name++ = NL; ++p; break;
4859 case 'r': *name++ = CAR; ++p; break;
4860 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861
4862 case 'X': /* hex: "\x1", "\x12" */
4863 case 'x':
4864 case 'u': /* Unicode: "\u0023" */
4865 case 'U':
4866 if (vim_isxdigit(p[1]))
4867 {
4868 int n, nr;
4869 int c = toupper(*p);
4870
4871 if (c == 'X')
4872 n = 2;
4873 else
4874 n = 4;
4875 nr = 0;
4876 while (--n >= 0 && vim_isxdigit(p[1]))
4877 {
4878 ++p;
4879 nr = (nr << 4) + hex2nr(*p);
4880 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004881 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882#ifdef FEAT_MBYTE
4883 /* For "\u" store the number according to
4884 * 'encoding'. */
4885 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004886 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 else
4888#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004889 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 break;
4892
4893 /* octal: "\1", "\12", "\123" */
4894 case '0':
4895 case '1':
4896 case '2':
4897 case '3':
4898 case '4':
4899 case '5':
4900 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004901 case '7': *name = *p++ - '0';
4902 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004904 *name = (*name << 3) + *p++ - '0';
4905 if (*p >= '0' && *p <= '7')
4906 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004908 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 break;
4910
4911 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004912 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 if (extra != 0)
4914 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004915 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 break;
4917 }
4918 /* FALLTHROUGH */
4919
Bram Moolenaar8c711452005-01-14 21:53:12 +00004920 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 break;
4922 }
4923 }
4924 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004925 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004928 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 *arg = p + 1;
4930
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 return OK;
4932}
4933
4934/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004935 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 * Return OK or FAIL.
4937 */
4938 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004939get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 int evaluate;
4943{
4944 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004945 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004946 int reduce = 0;
4947
4948 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004949 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004950 */
4951 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4952 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004953 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004954 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004956 break;
4957 ++reduce;
4958 ++p;
4959 }
4960 }
4961
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004963 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004964 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004965 return FAIL;
4966 }
4967
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004969 if (!evaluate)
4970 {
4971 *arg = p + 1;
4972 return OK;
4973 }
4974
4975 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004976 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004977 */
4978 str = alloc((unsigned)((p - *arg) - reduce));
4979 if (str == NULL)
4980 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004981 rettv->v_type = VAR_STRING;
4982 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004983
Bram Moolenaar8c711452005-01-14 21:53:12 +00004984 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004985 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004987 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004989 break;
4990 ++p;
4991 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004992 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004993 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004995 *arg = p + 1;
4996
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004997 return OK;
4998}
4999
5000/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005001 * Allocate a variable for a List and fill it from "*arg".
5002 * Return OK or FAIL.
5003 */
5004 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005005get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005006 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005007 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005008 int evaluate;
5009{
Bram Moolenaar33570922005-01-25 22:26:29 +00005010 list_T *l = NULL;
5011 typval_T tv;
5012 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005013
5014 if (evaluate)
5015 {
5016 l = list_alloc();
5017 if (l == NULL)
5018 return FAIL;
5019 }
5020
5021 *arg = skipwhite(*arg + 1);
5022 while (**arg != ']' && **arg != NUL)
5023 {
5024 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5025 goto failret;
5026 if (evaluate)
5027 {
5028 item = listitem_alloc();
5029 if (item != NULL)
5030 {
5031 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005032 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005033 list_append(l, item);
5034 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005035 else
5036 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005037 }
5038
5039 if (**arg == ']')
5040 break;
5041 if (**arg != ',')
5042 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005044 goto failret;
5045 }
5046 *arg = skipwhite(*arg + 1);
5047 }
5048
5049 if (**arg != ']')
5050 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005052failret:
5053 if (evaluate)
5054 list_free(l);
5055 return FAIL;
5056 }
5057
5058 *arg = skipwhite(*arg + 1);
5059 if (evaluate)
5060 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005061 rettv->v_type = VAR_LIST;
5062 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005063 ++l->lv_refcount;
5064 }
5065
5066 return OK;
5067}
5068
5069/*
5070 * Allocate an empty header for a list.
5071 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005072 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005073list_alloc()
5074{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005075 list_T *l;
5076
5077 l = (list_T *)alloc_clear(sizeof(list_T));
5078 if (l != NULL)
5079 {
5080 /* Prepend the list to the list of lists for garbage collection. */
5081 if (first_list != NULL)
5082 first_list->lv_used_prev = l;
5083 l->lv_used_prev = NULL;
5084 l->lv_used_next = first_list;
5085 first_list = l;
5086 }
5087 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005088}
5089
5090/*
5091 * Unreference a list: decrement the reference count and free it when it
5092 * becomes zero.
5093 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005094 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005095list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005096 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005097{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005098 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5099 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005100}
5101
5102/*
5103 * Free a list, including all items it points to.
5104 * Ignores the reference count.
5105 */
5106 static void
5107list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005108 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005109{
Bram Moolenaar33570922005-01-25 22:26:29 +00005110 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005111
Bram Moolenaard9fba312005-06-26 22:34:35 +00005112 /* Avoid that recursive reference to the list frees us again. */
5113 l->lv_refcount = DEL_REFCOUNT;
5114
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005115 /* Remove the list from the list of lists for garbage collection. */
5116 if (l->lv_used_prev == NULL)
5117 first_list = l->lv_used_next;
5118 else
5119 l->lv_used_prev->lv_used_next = l->lv_used_next;
5120 if (l->lv_used_next != NULL)
5121 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5122
Bram Moolenaard9fba312005-06-26 22:34:35 +00005123 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005124 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005125 /* Remove the item before deleting it. */
5126 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005127 listitem_free(item);
5128 }
5129 vim_free(l);
5130}
5131
5132/*
5133 * Allocate a list item.
5134 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005135 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005136listitem_alloc()
5137{
Bram Moolenaar33570922005-01-25 22:26:29 +00005138 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005139}
5140
5141/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005142 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143 */
5144 static void
5145listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005146 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005147{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005148 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149 vim_free(item);
5150}
5151
5152/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005153 * Remove a list item from a List and free it. Also clears the value.
5154 */
5155 static void
5156listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005157 list_T *l;
5158 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005159{
5160 list_remove(l, item, item);
5161 listitem_free(item);
5162}
5163
5164/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165 * Get the number of items in a list.
5166 */
5167 static long
5168list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005169 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005170{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005171 if (l == NULL)
5172 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005173 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174}
5175
5176/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005177 * Return TRUE when two lists have exactly the same values.
5178 */
5179 static int
5180list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005181 list_T *l1;
5182 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005183 int ic; /* ignore case for strings */
5184{
Bram Moolenaar33570922005-01-25 22:26:29 +00005185 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005186
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005187 if (list_len(l1) != list_len(l2))
5188 return FALSE;
5189
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005190 for (item1 = l1->lv_first, item2 = l2->lv_first;
5191 item1 != NULL && item2 != NULL;
5192 item1 = item1->li_next, item2 = item2->li_next)
5193 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5194 return FALSE;
5195 return item1 == NULL && item2 == NULL;
5196}
5197
5198/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005199 * Return TRUE when two dictionaries have exactly the same key/values.
5200 */
5201 static int
5202dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005203 dict_T *d1;
5204 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005205 int ic; /* ignore case for strings */
5206{
Bram Moolenaar33570922005-01-25 22:26:29 +00005207 hashitem_T *hi;
5208 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005209 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005210
5211 if (dict_len(d1) != dict_len(d2))
5212 return FALSE;
5213
Bram Moolenaar33570922005-01-25 22:26:29 +00005214 todo = d1->dv_hashtab.ht_used;
5215 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005216 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005217 if (!HASHITEM_EMPTY(hi))
5218 {
5219 item2 = dict_find(d2, hi->hi_key, -1);
5220 if (item2 == NULL)
5221 return FALSE;
5222 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5223 return FALSE;
5224 --todo;
5225 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005226 }
5227 return TRUE;
5228}
5229
5230/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005231 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005232 * Compares the items just like "==" would compare them, but strings and
5233 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005234 */
5235 static int
5236tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005237 typval_T *tv1;
5238 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005239 int ic; /* ignore case */
5240{
5241 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005242 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005243
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005244 if (tv1->v_type != tv2->v_type)
5245 return FALSE;
5246
5247 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005248 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005249 case VAR_LIST:
5250 /* recursive! */
5251 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5252
5253 case VAR_DICT:
5254 /* recursive! */
5255 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5256
5257 case VAR_FUNC:
5258 return (tv1->vval.v_string != NULL
5259 && tv2->vval.v_string != NULL
5260 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5261
5262 case VAR_NUMBER:
5263 return tv1->vval.v_number == tv2->vval.v_number;
5264
5265 case VAR_STRING:
5266 s1 = get_tv_string_buf(tv1, buf1);
5267 s2 = get_tv_string_buf(tv2, buf2);
5268 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005269 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005270
5271 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005272 return TRUE;
5273}
5274
5275/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 * Locate item with index "n" in list "l" and return it.
5277 * A negative index is counted from the end; -1 is the last item.
5278 * Returns NULL when "n" is out of range.
5279 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005280 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005282 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 long n;
5284{
Bram Moolenaar33570922005-01-25 22:26:29 +00005285 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005286 long idx;
5287
5288 if (l == NULL)
5289 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005290
5291 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005292 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005293 n = l->lv_len + n;
5294
5295 /* Check for index out of range. */
5296 if (n < 0 || n >= l->lv_len)
5297 return NULL;
5298
5299 /* When there is a cached index may start search from there. */
5300 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005302 if (n < l->lv_idx / 2)
5303 {
5304 /* closest to the start of the list */
5305 item = l->lv_first;
5306 idx = 0;
5307 }
5308 else if (n > (l->lv_idx + l->lv_len) / 2)
5309 {
5310 /* closest to the end of the list */
5311 item = l->lv_last;
5312 idx = l->lv_len - 1;
5313 }
5314 else
5315 {
5316 /* closest to the cached index */
5317 item = l->lv_idx_item;
5318 idx = l->lv_idx;
5319 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 }
5321 else
5322 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005323 if (n < l->lv_len / 2)
5324 {
5325 /* closest to the start of the list */
5326 item = l->lv_first;
5327 idx = 0;
5328 }
5329 else
5330 {
5331 /* closest to the end of the list */
5332 item = l->lv_last;
5333 idx = l->lv_len - 1;
5334 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005336
5337 while (n > idx)
5338 {
5339 /* search forward */
5340 item = item->li_next;
5341 ++idx;
5342 }
5343 while (n < idx)
5344 {
5345 /* search backward */
5346 item = item->li_prev;
5347 --idx;
5348 }
5349
5350 /* cache the used index */
5351 l->lv_idx = idx;
5352 l->lv_idx_item = item;
5353
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 return item;
5355}
5356
5357/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005358 * Locate "item" list "l" and return its index.
5359 * Returns -1 when "item" is not in the list.
5360 */
5361 static long
5362list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005363 list_T *l;
5364 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005365{
5366 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005367 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005368
5369 if (l == NULL)
5370 return -1;
5371 idx = 0;
5372 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5373 ++idx;
5374 if (li == NULL)
5375 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005376 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005377}
5378
5379/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 * Append item "item" to the end of list "l".
5381 */
5382 static void
5383list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005384 list_T *l;
5385 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386{
5387 if (l->lv_last == NULL)
5388 {
5389 /* empty list */
5390 l->lv_first = item;
5391 l->lv_last = item;
5392 item->li_prev = NULL;
5393 }
5394 else
5395 {
5396 l->lv_last->li_next = item;
5397 item->li_prev = l->lv_last;
5398 l->lv_last = item;
5399 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005400 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 item->li_next = NULL;
5402}
5403
5404/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005405 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005406 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 */
5408 static int
5409list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005410 list_T *l;
5411 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005413 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414
Bram Moolenaar05159a02005-02-26 23:04:13 +00005415 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005417 copy_tv(tv, &li->li_tv);
5418 list_append(l, li);
5419 return OK;
5420}
5421
5422/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005423 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005424 * Return FAIL when out of memory.
5425 */
5426 int
5427list_append_dict(list, dict)
5428 list_T *list;
5429 dict_T *dict;
5430{
5431 listitem_T *li = listitem_alloc();
5432
5433 if (li == NULL)
5434 return FAIL;
5435 li->li_tv.v_type = VAR_DICT;
5436 li->li_tv.v_lock = 0;
5437 li->li_tv.vval.v_dict = dict;
5438 list_append(list, li);
5439 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440 return OK;
5441}
5442
5443/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005444 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005445 * If "item" is NULL append at the end.
5446 * Return FAIL when out of memory.
5447 */
5448 static int
5449list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005450 list_T *l;
5451 typval_T *tv;
5452 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005453{
Bram Moolenaar33570922005-01-25 22:26:29 +00005454 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005455
5456 if (ni == NULL)
5457 return FAIL;
5458 copy_tv(tv, &ni->li_tv);
5459 if (item == NULL)
5460 /* Append new item at end of list. */
5461 list_append(l, ni);
5462 else
5463 {
5464 /* Insert new item before existing item. */
5465 ni->li_prev = item->li_prev;
5466 ni->li_next = item;
5467 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005468 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005469 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005470 ++l->lv_idx;
5471 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005472 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005473 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005474 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005475 l->lv_idx_item = NULL;
5476 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005477 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005478 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005479 }
5480 return OK;
5481}
5482
5483/*
5484 * Extend "l1" with "l2".
5485 * If "bef" is NULL append at the end, otherwise insert before this item.
5486 * Returns FAIL when out of memory.
5487 */
5488 static int
5489list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005490 list_T *l1;
5491 list_T *l2;
5492 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005493{
Bram Moolenaar33570922005-01-25 22:26:29 +00005494 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005495
5496 for (item = l2->lv_first; item != NULL; item = item->li_next)
5497 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5498 return FAIL;
5499 return OK;
5500}
5501
5502/*
5503 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5504 * Return FAIL when out of memory.
5505 */
5506 static int
5507list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005508 list_T *l1;
5509 list_T *l2;
5510 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005511{
Bram Moolenaar33570922005-01-25 22:26:29 +00005512 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005513
5514 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005515 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005516 if (l == NULL)
5517 return FAIL;
5518 tv->v_type = VAR_LIST;
5519 tv->vval.v_list = l;
5520
5521 /* append all items from the second list */
5522 return list_extend(l, l2, NULL);
5523}
5524
5525/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005526 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005528 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 * Returns NULL when out of memory.
5530 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005531 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005532list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005533 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005535 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536{
Bram Moolenaar33570922005-01-25 22:26:29 +00005537 list_T *copy;
5538 listitem_T *item;
5539 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005540
5541 if (orig == NULL)
5542 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543
5544 copy = list_alloc();
5545 if (copy != NULL)
5546 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005547 if (copyID != 0)
5548 {
5549 /* Do this before adding the items, because one of the items may
5550 * refer back to this list. */
5551 orig->lv_copyID = copyID;
5552 orig->lv_copylist = copy;
5553 }
5554 for (item = orig->lv_first; item != NULL && !got_int;
5555 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 {
5557 ni = listitem_alloc();
5558 if (ni == NULL)
5559 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005560 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005561 {
5562 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5563 {
5564 vim_free(ni);
5565 break;
5566 }
5567 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005570 list_append(copy, ni);
5571 }
5572 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005573 if (item != NULL)
5574 {
5575 list_unref(copy);
5576 copy = NULL;
5577 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005578 }
5579
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 return copy;
5581}
5582
5583/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005584 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005585 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005586 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005587 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005588list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005589 list_T *l;
5590 listitem_T *item;
5591 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005592{
Bram Moolenaar33570922005-01-25 22:26:29 +00005593 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005594
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005595 /* notify watchers */
5596 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005597 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005598 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005599 list_fix_watch(l, ip);
5600 if (ip == item2)
5601 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005603
5604 if (item2->li_next == NULL)
5605 l->lv_last = item->li_prev;
5606 else
5607 item2->li_next->li_prev = item->li_prev;
5608 if (item->li_prev == NULL)
5609 l->lv_first = item2->li_next;
5610 else
5611 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005612 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613}
5614
5615/*
5616 * Return an allocated string with the string representation of a list.
5617 * May return NULL.
5618 */
5619 static char_u *
5620list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005621 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005622{
5623 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005624
5625 if (tv->vval.v_list == NULL)
5626 return NULL;
5627 ga_init2(&ga, (int)sizeof(char), 80);
5628 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005629 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5630 {
5631 vim_free(ga.ga_data);
5632 return NULL;
5633 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 ga_append(&ga, ']');
5635 ga_append(&ga, NUL);
5636 return (char_u *)ga.ga_data;
5637}
5638
5639/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005640 * Join list "l" into a string in "*gap", using separator "sep".
5641 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005642 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005643 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005644 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005645list_join(gap, l, sep, echo)
5646 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005647 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005648 char_u *sep;
5649 int echo;
5650{
5651 int first = TRUE;
5652 char_u *tofree;
5653 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005654 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005655 char_u *s;
5656
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005657 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005658 {
5659 if (first)
5660 first = FALSE;
5661 else
5662 ga_concat(gap, sep);
5663
5664 if (echo)
5665 s = echo_string(&item->li_tv, &tofree, numbuf);
5666 else
5667 s = tv2string(&item->li_tv, &tofree, numbuf);
5668 if (s != NULL)
5669 ga_concat(gap, s);
5670 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005671 if (s == NULL)
5672 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005673 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005674 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005675}
5676
5677/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005678 * Garbage collection for lists and dictionaries.
5679 *
5680 * We use reference counts to be able to free most items right away when they
5681 * are no longer used. But for composite items it's possible that it becomes
5682 * unused while the reference count is > 0: When there is a recursive
5683 * reference. Example:
5684 * :let l = [1, 2, 3]
5685 * :let d = {9: l}
5686 * :let l[1] = d
5687 *
5688 * Since this is quite unusual we handle this with garbage collection: every
5689 * once in a while find out which lists and dicts are not referenced from any
5690 * variable.
5691 *
5692 * Here is a good reference text about garbage collection (refers to Python
5693 * but it applies to all reference-counting mechanisms):
5694 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005695 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005696
5697/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005698 * Do garbage collection for lists and dicts.
5699 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005700 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005701 int
5702garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005703{
5704 dict_T *dd;
5705 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005706 int copyID = ++current_copyID;
5707 buf_T *buf;
5708 win_T *wp;
5709 int i;
5710 funccall_T *fc;
5711 int did_free = FALSE;
5712
5713 /*
5714 * 1. Go through all accessible variables and mark all lists and dicts
5715 * with copyID.
5716 */
5717 /* script-local variables */
5718 for (i = 1; i <= ga_scripts.ga_len; ++i)
5719 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5720
5721 /* buffer-local variables */
5722 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5723 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5724
5725 /* window-local variables */
5726 FOR_ALL_WINDOWS(wp)
5727 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5728
5729 /* global variables */
5730 set_ref_in_ht(&globvarht, copyID);
5731
5732 /* function-local variables */
5733 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5734 {
5735 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5736 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5737 }
5738
5739 /*
5740 * 2. Go through the list of dicts and free items without the copyID.
5741 */
5742 for (dd = first_dict; dd != NULL; )
5743 if (dd->dv_copyID != copyID)
5744 {
5745 dict_free(dd);
5746 did_free = TRUE;
5747
5748 /* restart, next dict may also have been freed */
5749 dd = first_dict;
5750 }
5751 else
5752 dd = dd->dv_used_next;
5753
5754 /*
5755 * 3. Go through the list of lists and free items without the copyID.
5756 */
5757 for (ll = first_list; ll != NULL; )
5758 if (ll->lv_copyID != copyID)
5759 {
5760 list_free(ll);
5761 did_free = TRUE;
5762
5763 /* restart, next dict may also have been freed */
5764 ll = first_list;
5765 }
5766 else
5767 ll = ll->lv_used_next;
5768
5769 return did_free;
5770}
5771
5772/*
5773 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5774 */
5775 static void
5776set_ref_in_ht(ht, copyID)
5777 hashtab_T *ht;
5778 int copyID;
5779{
5780 int todo;
5781 hashitem_T *hi;
5782
5783 todo = ht->ht_used;
5784 for (hi = ht->ht_array; todo > 0; ++hi)
5785 if (!HASHITEM_EMPTY(hi))
5786 {
5787 --todo;
5788 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5789 }
5790}
5791
5792/*
5793 * Mark all lists and dicts referenced through list "l" with "copyID".
5794 */
5795 static void
5796set_ref_in_list(l, copyID)
5797 list_T *l;
5798 int copyID;
5799{
5800 listitem_T *li;
5801
5802 for (li = l->lv_first; li != NULL; li = li->li_next)
5803 set_ref_in_item(&li->li_tv, copyID);
5804}
5805
5806/*
5807 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5808 */
5809 static void
5810set_ref_in_item(tv, copyID)
5811 typval_T *tv;
5812 int copyID;
5813{
5814 dict_T *dd;
5815 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005816
5817 switch (tv->v_type)
5818 {
5819 case VAR_DICT:
5820 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005821 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005822 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005823 /* Didn't see this dict yet. */
5824 dd->dv_copyID = copyID;
5825 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005826 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005827 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005828
5829 case VAR_LIST:
5830 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005831 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005832 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005833 /* Didn't see this list yet. */
5834 ll->lv_copyID = copyID;
5835 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005836 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005837 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005838 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005839 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005840}
5841
5842/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 * Allocate an empty header for a dictionary.
5844 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005845 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846dict_alloc()
5847{
Bram Moolenaar33570922005-01-25 22:26:29 +00005848 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005849
Bram Moolenaar33570922005-01-25 22:26:29 +00005850 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005851 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005852 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005853 /* Add the list to the hashtable for garbage collection. */
5854 if (first_dict != NULL)
5855 first_dict->dv_used_prev = d;
5856 d->dv_used_next = first_dict;
5857 d->dv_used_prev = NULL;
5858
Bram Moolenaar33570922005-01-25 22:26:29 +00005859 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005860 d->dv_lock = 0;
5861 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005862 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005863 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005864 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005865}
5866
5867/*
5868 * Unreference a Dictionary: decrement the reference count and free it when it
5869 * becomes zero.
5870 */
5871 static void
5872dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005873 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005875 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
5876 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005877}
5878
5879/*
5880 * Free a Dictionary, including all items it contains.
5881 * Ignores the reference count.
5882 */
5883 static void
5884dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005885 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005887 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005888 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005889 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890
Bram Moolenaard9fba312005-06-26 22:34:35 +00005891 /* Avoid that recursive reference to the dict frees us again. */
5892 d->dv_refcount = DEL_REFCOUNT;
5893
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005894 /* Remove the dict from the list of dicts for garbage collection. */
5895 if (d->dv_used_prev == NULL)
5896 first_dict = d->dv_used_next;
5897 else
5898 d->dv_used_prev->dv_used_next = d->dv_used_next;
5899 if (d->dv_used_next != NULL)
5900 d->dv_used_next->dv_used_prev = d->dv_used_prev;
5901
5902 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005903 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 todo = d->dv_hashtab.ht_used;
5905 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005906 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005907 if (!HASHITEM_EMPTY(hi))
5908 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005909 /* Remove the item before deleting it, just in case there is
5910 * something recursive causing trouble. */
5911 di = HI2DI(hi);
5912 hash_remove(&d->dv_hashtab, hi);
5913 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005914 --todo;
5915 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005916 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005917 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005918 vim_free(d);
5919}
5920
5921/*
5922 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005923 * The "key" is copied to the new item.
5924 * Note that the value of the item "di_tv" still needs to be initialized!
5925 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005926 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005927 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005928dictitem_alloc(key)
5929 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005930{
Bram Moolenaar33570922005-01-25 22:26:29 +00005931 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005932
Bram Moolenaar33570922005-01-25 22:26:29 +00005933 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005934 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005935 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005936 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005937 di->di_flags = 0;
5938 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005939 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005940}
5941
5942/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005943 * Make a copy of a Dictionary item.
5944 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005945 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005946dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005947 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005948{
Bram Moolenaar33570922005-01-25 22:26:29 +00005949 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005950
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005952 if (di != NULL)
5953 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005954 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005955 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005956 copy_tv(&org->di_tv, &di->di_tv);
5957 }
5958 return di;
5959}
5960
5961/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005962 * Remove item "item" from Dictionary "dict" and free it.
5963 */
5964 static void
5965dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005966 dict_T *dict;
5967 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005968{
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005970
Bram Moolenaar33570922005-01-25 22:26:29 +00005971 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005972 if (HASHITEM_EMPTY(hi))
5973 EMSG2(_(e_intern2), "dictitem_remove()");
5974 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005975 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005976 dictitem_free(item);
5977}
5978
5979/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005980 * Free a dict item. Also clears the value.
5981 */
5982 static void
5983dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005984 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005985{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005986 clear_tv(&item->di_tv);
5987 vim_free(item);
5988}
5989
5990/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005991 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5992 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005993 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00005994 * Returns NULL when out of memory.
5995 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005996 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005997dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005998 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005999 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006000 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006001{
Bram Moolenaar33570922005-01-25 22:26:29 +00006002 dict_T *copy;
6003 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006004 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006006
6007 if (orig == NULL)
6008 return NULL;
6009
6010 copy = dict_alloc();
6011 if (copy != NULL)
6012 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006013 if (copyID != 0)
6014 {
6015 orig->dv_copyID = copyID;
6016 orig->dv_copydict = copy;
6017 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006018 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006019 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006020 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006021 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006022 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006023 --todo;
6024
6025 di = dictitem_alloc(hi->hi_key);
6026 if (di == NULL)
6027 break;
6028 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006029 {
6030 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6031 copyID) == FAIL)
6032 {
6033 vim_free(di);
6034 break;
6035 }
6036 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006037 else
6038 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6039 if (dict_add(copy, di) == FAIL)
6040 {
6041 dictitem_free(di);
6042 break;
6043 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006044 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006045 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006046
Bram Moolenaare9a41262005-01-15 22:18:47 +00006047 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006048 if (todo > 0)
6049 {
6050 dict_unref(copy);
6051 copy = NULL;
6052 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006053 }
6054
6055 return copy;
6056}
6057
6058/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006059 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006060 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006061 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006062 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006063dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 dict_T *d;
6065 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006066{
Bram Moolenaar33570922005-01-25 22:26:29 +00006067 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006068}
6069
Bram Moolenaar8c711452005-01-14 21:53:12 +00006070/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006071 * Add a number or string entry to dictionary "d".
6072 * When "str" is NULL use number "nr", otherwise use "str".
6073 * Returns FAIL when out of memory and when key already exists.
6074 */
6075 int
6076dict_add_nr_str(d, key, nr, str)
6077 dict_T *d;
6078 char *key;
6079 long nr;
6080 char_u *str;
6081{
6082 dictitem_T *item;
6083
6084 item = dictitem_alloc((char_u *)key);
6085 if (item == NULL)
6086 return FAIL;
6087 item->di_tv.v_lock = 0;
6088 if (str == NULL)
6089 {
6090 item->di_tv.v_type = VAR_NUMBER;
6091 item->di_tv.vval.v_number = nr;
6092 }
6093 else
6094 {
6095 item->di_tv.v_type = VAR_STRING;
6096 item->di_tv.vval.v_string = vim_strsave(str);
6097 }
6098 if (dict_add(d, item) == FAIL)
6099 {
6100 dictitem_free(item);
6101 return FAIL;
6102 }
6103 return OK;
6104}
6105
6106/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006107 * Get the number of items in a Dictionary.
6108 */
6109 static long
6110dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006111 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006112{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006113 if (d == NULL)
6114 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006115 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006116}
6117
6118/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006119 * Find item "key[len]" in Dictionary "d".
6120 * If "len" is negative use strlen(key).
6121 * Returns NULL when not found.
6122 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006123 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006124dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006125 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006126 char_u *key;
6127 int len;
6128{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006129#define AKEYLEN 200
6130 char_u buf[AKEYLEN];
6131 char_u *akey;
6132 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006134
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006135 if (len < 0)
6136 akey = key;
6137 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006138 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006139 tofree = akey = vim_strnsave(key, len);
6140 if (akey == NULL)
6141 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006142 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006143 else
6144 {
6145 /* Avoid a malloc/free by using buf[]. */
6146 STRNCPY(buf, key, len);
6147 buf[len] = NUL;
6148 akey = buf;
6149 }
6150
Bram Moolenaar33570922005-01-25 22:26:29 +00006151 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006152 vim_free(tofree);
6153 if (HASHITEM_EMPTY(hi))
6154 return NULL;
6155 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006156}
6157
6158/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006159 * Get a string item from a dictionary in allocated memory.
6160 * Returns NULL if the entry doesn't exist or out of memory.
6161 */
6162 char_u *
6163get_dict_string(d, key)
6164 dict_T *d;
6165 char_u *key;
6166{
6167 dictitem_T *di;
6168
6169 di = dict_find(d, key, -1);
6170 if (di == NULL)
6171 return NULL;
6172 return vim_strsave(get_tv_string(&di->di_tv));
6173}
6174
6175/*
6176 * Get a number item from a dictionary.
6177 * Returns 0 if the entry doesn't exist or out of memory.
6178 */
6179 long
6180get_dict_number(d, key)
6181 dict_T *d;
6182 char_u *key;
6183{
6184 dictitem_T *di;
6185
6186 di = dict_find(d, key, -1);
6187 if (di == NULL)
6188 return 0;
6189 return get_tv_number(&di->di_tv);
6190}
6191
6192/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006193 * Return an allocated string with the string representation of a Dictionary.
6194 * May return NULL.
6195 */
6196 static char_u *
6197dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006198 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006199{
6200 garray_T ga;
6201 int first = TRUE;
6202 char_u *tofree;
6203 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006204 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006205 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006206 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006207 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006208
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006209 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006210 return NULL;
6211 ga_init2(&ga, (int)sizeof(char), 80);
6212 ga_append(&ga, '{');
6213
Bram Moolenaar33570922005-01-25 22:26:29 +00006214 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006215 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006216 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006217 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006218 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006219 --todo;
6220
6221 if (first)
6222 first = FALSE;
6223 else
6224 ga_concat(&ga, (char_u *)", ");
6225
6226 tofree = string_quote(hi->hi_key, FALSE);
6227 if (tofree != NULL)
6228 {
6229 ga_concat(&ga, tofree);
6230 vim_free(tofree);
6231 }
6232 ga_concat(&ga, (char_u *)": ");
6233 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6234 if (s != NULL)
6235 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006236 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006237 if (s == NULL)
6238 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006239 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006240 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006241 if (todo > 0)
6242 {
6243 vim_free(ga.ga_data);
6244 return NULL;
6245 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006246
6247 ga_append(&ga, '}');
6248 ga_append(&ga, NUL);
6249 return (char_u *)ga.ga_data;
6250}
6251
6252/*
6253 * Allocate a variable for a Dictionary and fill it from "*arg".
6254 * Return OK or FAIL. Returns NOTDONE for {expr}.
6255 */
6256 static int
6257get_dict_tv(arg, rettv, evaluate)
6258 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006260 int evaluate;
6261{
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 dict_T *d = NULL;
6263 typval_T tvkey;
6264 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006265 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006266 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006267 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006268 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006269
6270 /*
6271 * First check if it's not a curly-braces thing: {expr}.
6272 * Must do this without evaluating, otherwise a function may be called
6273 * twice. Unfortunately this means we need to call eval1() twice for the
6274 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006275 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006276 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006277 if (*start != '}')
6278 {
6279 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6280 return FAIL;
6281 if (*start == '}')
6282 return NOTDONE;
6283 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006284
6285 if (evaluate)
6286 {
6287 d = dict_alloc();
6288 if (d == NULL)
6289 return FAIL;
6290 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006291 tvkey.v_type = VAR_UNKNOWN;
6292 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006293
6294 *arg = skipwhite(*arg + 1);
6295 while (**arg != '}' && **arg != NUL)
6296 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006297 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006298 goto failret;
6299 if (**arg != ':')
6300 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006301 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006302 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006303 goto failret;
6304 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006305 key = get_tv_string_buf_chk(&tvkey, buf);
6306 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006307 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006308 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6309 if (key != NULL)
6310 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006311 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006312 goto failret;
6313 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006314
6315 *arg = skipwhite(*arg + 1);
6316 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6317 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006318 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006319 goto failret;
6320 }
6321 if (evaluate)
6322 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006323 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006324 if (item != NULL)
6325 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006326 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006327 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006328 clear_tv(&tv);
6329 goto failret;
6330 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006331 item = dictitem_alloc(key);
6332 clear_tv(&tvkey);
6333 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006334 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006335 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006336 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006337 if (dict_add(d, item) == FAIL)
6338 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006339 }
6340 }
6341
6342 if (**arg == '}')
6343 break;
6344 if (**arg != ',')
6345 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006346 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006347 goto failret;
6348 }
6349 *arg = skipwhite(*arg + 1);
6350 }
6351
6352 if (**arg != '}')
6353 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006354 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006355failret:
6356 if (evaluate)
6357 dict_free(d);
6358 return FAIL;
6359 }
6360
6361 *arg = skipwhite(*arg + 1);
6362 if (evaluate)
6363 {
6364 rettv->v_type = VAR_DICT;
6365 rettv->vval.v_dict = d;
6366 ++d->dv_refcount;
6367 }
6368
6369 return OK;
6370}
6371
Bram Moolenaar8c711452005-01-14 21:53:12 +00006372/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006373 * Return a string with the string representation of a variable.
6374 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006375 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006376 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377 * May return NULL;
6378 */
6379 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006380echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006381 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006382 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006383 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006385 static int recurse = 0;
6386 char_u *r = NULL;
6387
Bram Moolenaar33570922005-01-25 22:26:29 +00006388 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006389 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006390 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006391 *tofree = NULL;
6392 return NULL;
6393 }
6394 ++recurse;
6395
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006396 switch (tv->v_type)
6397 {
6398 case VAR_FUNC:
6399 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006400 r = tv->vval.v_string;
6401 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006402 case VAR_LIST:
6403 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006404 r = *tofree;
6405 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006406 case VAR_DICT:
6407 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006408 r = *tofree;
6409 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006410 case VAR_STRING:
6411 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006412 *tofree = NULL;
6413 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006415 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006416 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006417 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006418 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006419
6420 --recurse;
6421 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006422}
6423
6424/*
6425 * Return a string with the string representation of a variable.
6426 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6427 * "numbuf" is used for a number.
6428 * Puts quotes around strings, so that they can be parsed back by eval().
6429 * May return NULL;
6430 */
6431 static char_u *
6432tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006433 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006434 char_u **tofree;
6435 char_u *numbuf;
6436{
6437 switch (tv->v_type)
6438 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006439 case VAR_FUNC:
6440 *tofree = string_quote(tv->vval.v_string, TRUE);
6441 return *tofree;
6442 case VAR_STRING:
6443 *tofree = string_quote(tv->vval.v_string, FALSE);
6444 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006445 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006446 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006447 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006448 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006449 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006450 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006452 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006453}
6454
6455/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006456 * Return string "str" in ' quotes, doubling ' characters.
6457 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006458 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006459 */
6460 static char_u *
6461string_quote(str, function)
6462 char_u *str;
6463 int function;
6464{
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006466 char_u *p, *r, *s;
6467
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 len = (function ? 13 : 3);
6469 if (str != NULL)
6470 {
6471 len += STRLEN(str);
6472 for (p = str; *p != NUL; mb_ptr_adv(p))
6473 if (*p == '\'')
6474 ++len;
6475 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006476 s = r = alloc(len);
6477 if (r != NULL)
6478 {
6479 if (function)
6480 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006481 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006482 r += 10;
6483 }
6484 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006485 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006486 if (str != NULL)
6487 for (p = str; *p != NUL; )
6488 {
6489 if (*p == '\'')
6490 *r++ = '\'';
6491 MB_COPY_CHAR(p, r);
6492 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006493 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006494 if (function)
6495 *r++ = ')';
6496 *r++ = NUL;
6497 }
6498 return s;
6499}
6500
6501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 * Get the value of an environment variable.
6503 * "arg" is pointing to the '$'. It is advanced to after the name.
6504 * If the environment variable was not set, silently assume it is empty.
6505 * Always return OK.
6506 */
6507 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006508get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 int evaluate;
6512{
6513 char_u *string = NULL;
6514 int len;
6515 int cc;
6516 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006517 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518
6519 ++*arg;
6520 name = *arg;
6521 len = get_env_len(arg);
6522 if (evaluate)
6523 {
6524 if (len != 0)
6525 {
6526 cc = name[len];
6527 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006528 /* first try vim_getenv(), fast for normal environment vars */
6529 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006531 {
6532 if (!mustfree)
6533 string = vim_strsave(string);
6534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 else
6536 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006537 if (mustfree)
6538 vim_free(string);
6539
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 /* next try expanding things like $VIM and ${HOME} */
6541 string = expand_env_save(name - 1);
6542 if (string != NULL && *string == '$')
6543 {
6544 vim_free(string);
6545 string = NULL;
6546 }
6547 }
6548 name[len] = cc;
6549 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006550 rettv->v_type = VAR_STRING;
6551 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 }
6553
6554 return OK;
6555}
6556
6557/*
6558 * Array with names and number of arguments of all internal functions
6559 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6560 */
6561static struct fst
6562{
6563 char *f_name; /* function name */
6564 char f_min_argc; /* minimal number of arguments */
6565 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006566 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568} functions[] =
6569{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006570 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 {"append", 2, 2, f_append},
6572 {"argc", 0, 0, f_argc},
6573 {"argidx", 0, 0, f_argidx},
6574 {"argv", 1, 1, f_argv},
6575 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006576 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 {"bufexists", 1, 1, f_bufexists},
6578 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6579 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6580 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6581 {"buflisted", 1, 1, f_buflisted},
6582 {"bufloaded", 1, 1, f_bufloaded},
6583 {"bufname", 1, 1, f_bufname},
6584 {"bufnr", 1, 1, f_bufnr},
6585 {"bufwinnr", 1, 1, f_bufwinnr},
6586 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006587 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006588 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 {"char2nr", 1, 1, f_char2nr},
6590 {"cindent", 1, 1, f_cindent},
6591 {"col", 1, 1, f_col},
6592 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006594 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 {"cscope_connection",0,3, f_cscope_connection},
6596 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006597 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 {"delete", 1, 1, f_delete},
6599 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006600 {"diff_filler", 1, 1, f_diff_filler},
6601 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006602 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006604 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 {"eventhandler", 0, 0, f_eventhandler},
6606 {"executable", 1, 1, f_executable},
6607 {"exists", 1, 1, f_exists},
6608 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006609 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6611 {"filereadable", 1, 1, f_filereadable},
6612 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006613 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006614 {"finddir", 1, 3, f_finddir},
6615 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 {"fnamemodify", 2, 2, f_fnamemodify},
6617 {"foldclosed", 1, 1, f_foldclosed},
6618 {"foldclosedend", 1, 1, f_foldclosedend},
6619 {"foldlevel", 1, 1, f_foldlevel},
6620 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006621 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006624 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006625 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 {"getbufvar", 2, 2, f_getbufvar},
6627 {"getchar", 0, 1, f_getchar},
6628 {"getcharmod", 0, 0, f_getcharmod},
6629 {"getcmdline", 0, 0, f_getcmdline},
6630 {"getcmdpos", 0, 0, f_getcmdpos},
6631 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006632 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006633 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 {"getfsize", 1, 1, f_getfsize},
6635 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006636 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006637 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006638 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006639 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 {"getregtype", 0, 1, f_getregtype},
6641 {"getwinposx", 0, 0, f_getwinposx},
6642 {"getwinposy", 0, 0, f_getwinposy},
6643 {"getwinvar", 2, 2, f_getwinvar},
6644 {"glob", 1, 1, f_glob},
6645 {"globpath", 2, 2, f_globpath},
6646 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006647 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 {"hasmapto", 1, 2, f_hasmapto},
6649 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6650 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6651 {"histadd", 2, 2, f_histadd},
6652 {"histdel", 1, 2, f_histdel},
6653 {"histget", 1, 2, f_histget},
6654 {"histnr", 1, 1, f_histnr},
6655 {"hlID", 1, 1, f_hlID},
6656 {"hlexists", 1, 1, f_hlexists},
6657 {"hostname", 0, 0, f_hostname},
6658 {"iconv", 3, 3, f_iconv},
6659 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006660 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 {"input", 1, 2, f_input},
6662 {"inputdialog", 1, 3, f_inputdialog},
6663 {"inputrestore", 0, 0, f_inputrestore},
6664 {"inputsave", 0, 0, f_inputsave},
6665 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006668 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006669 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006670 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006671 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006673 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 {"libcall", 3, 3, f_libcall},
6675 {"libcallnr", 3, 3, f_libcallnr},
6676 {"line", 1, 1, f_line},
6677 {"line2byte", 1, 1, f_line2byte},
6678 {"lispindent", 1, 1, f_lispindent},
6679 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006680 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 {"maparg", 1, 2, f_maparg},
6682 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006683 {"match", 2, 4, f_match},
6684 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006685 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006686 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006687 {"max", 1, 1, f_max},
6688 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006689#ifdef vim_mkdir
6690 {"mkdir", 1, 3, f_mkdir},
6691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 {"mode", 0, 0, f_mode},
6693 {"nextnonblank", 1, 1, f_nextnonblank},
6694 {"nr2char", 1, 1, f_nr2char},
6695 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006696 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006697 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 {"remote_expr", 2, 3, f_remote_expr},
6699 {"remote_foreground", 1, 1, f_remote_foreground},
6700 {"remote_peek", 1, 2, f_remote_peek},
6701 {"remote_read", 1, 1, f_remote_read},
6702 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006703 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006705 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006707 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 {"search", 1, 2, f_search},
6709 {"searchpair", 3, 5, f_searchpair},
6710 {"server2client", 2, 2, f_server2client},
6711 {"serverlist", 0, 0, f_serverlist},
6712 {"setbufvar", 3, 3, f_setbufvar},
6713 {"setcmdpos", 1, 1, f_setcmdpos},
6714 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006715 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 {"setreg", 2, 3, f_setreg},
6717 {"setwinvar", 3, 3, f_setwinvar},
6718 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006719 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006720 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006721 {"spellbadword", 0, 0, f_spellbadword},
6722 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006723 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724#ifdef HAVE_STRFTIME
6725 {"strftime", 1, 2, f_strftime},
6726#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006727 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006728 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 {"strlen", 1, 1, f_strlen},
6730 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006731 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 {"strtrans", 1, 1, f_strtrans},
6733 {"submatch", 1, 1, f_submatch},
6734 {"substitute", 4, 4, f_substitute},
6735 {"synID", 3, 3, f_synID},
6736 {"synIDattr", 2, 3, f_synIDattr},
6737 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006738 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006739 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 {"tempname", 0, 0, f_tempname},
6741 {"tolower", 1, 1, f_tolower},
6742 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006743 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006745 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 {"virtcol", 1, 1, f_virtcol},
6747 {"visualmode", 0, 1, f_visualmode},
6748 {"winbufnr", 1, 1, f_winbufnr},
6749 {"wincol", 0, 0, f_wincol},
6750 {"winheight", 1, 1, f_winheight},
6751 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006752 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 {"winrestcmd", 0, 0, f_winrestcmd},
6754 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006755 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756};
6757
6758#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6759
6760/*
6761 * Function given to ExpandGeneric() to obtain the list of internal
6762 * or user defined function names.
6763 */
6764 char_u *
6765get_function_name(xp, idx)
6766 expand_T *xp;
6767 int idx;
6768{
6769 static int intidx = -1;
6770 char_u *name;
6771
6772 if (idx == 0)
6773 intidx = -1;
6774 if (intidx < 0)
6775 {
6776 name = get_user_func_name(xp, idx);
6777 if (name != NULL)
6778 return name;
6779 }
6780 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6781 {
6782 STRCPY(IObuff, functions[intidx].f_name);
6783 STRCAT(IObuff, "(");
6784 if (functions[intidx].f_max_argc == 0)
6785 STRCAT(IObuff, ")");
6786 return IObuff;
6787 }
6788
6789 return NULL;
6790}
6791
6792/*
6793 * Function given to ExpandGeneric() to obtain the list of internal or
6794 * user defined variable or function names.
6795 */
6796/*ARGSUSED*/
6797 char_u *
6798get_expr_name(xp, idx)
6799 expand_T *xp;
6800 int idx;
6801{
6802 static int intidx = -1;
6803 char_u *name;
6804
6805 if (idx == 0)
6806 intidx = -1;
6807 if (intidx < 0)
6808 {
6809 name = get_function_name(xp, idx);
6810 if (name != NULL)
6811 return name;
6812 }
6813 return get_user_var_name(xp, ++intidx);
6814}
6815
6816#endif /* FEAT_CMDL_COMPL */
6817
6818/*
6819 * Find internal function in table above.
6820 * Return index, or -1 if not found
6821 */
6822 static int
6823find_internal_func(name)
6824 char_u *name; /* name of the function */
6825{
6826 int first = 0;
6827 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6828 int cmp;
6829 int x;
6830
6831 /*
6832 * Find the function name in the table. Binary search.
6833 */
6834 while (first <= last)
6835 {
6836 x = first + ((unsigned)(last - first) >> 1);
6837 cmp = STRCMP(name, functions[x].f_name);
6838 if (cmp < 0)
6839 last = x - 1;
6840 else if (cmp > 0)
6841 first = x + 1;
6842 else
6843 return x;
6844 }
6845 return -1;
6846}
6847
6848/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006849 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6850 * name it contains, otherwise return "name".
6851 */
6852 static char_u *
6853deref_func_name(name, lenp)
6854 char_u *name;
6855 int *lenp;
6856{
Bram Moolenaar33570922005-01-25 22:26:29 +00006857 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006858 int cc;
6859
6860 cc = name[*lenp];
6861 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006862 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006863 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006864 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006865 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006866 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006867 {
6868 *lenp = 0;
6869 return (char_u *)""; /* just in case */
6870 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006871 *lenp = STRLEN(v->di_tv.vval.v_string);
6872 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006873 }
6874
6875 return name;
6876}
6877
6878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 * Allocate a variable for the result of a function.
6880 * Return OK or FAIL.
6881 */
6882 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006883get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6884 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 char_u *name; /* name of the function */
6886 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 char_u **arg; /* argument, pointing to the '(' */
6889 linenr_T firstline; /* first line of range */
6890 linenr_T lastline; /* last line of range */
6891 int *doesrange; /* return: function handled range */
6892 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006893 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894{
6895 char_u *argp;
6896 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006897 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 int argcount = 0; /* number of arguments found */
6899
6900 /*
6901 * Get the arguments.
6902 */
6903 argp = *arg;
6904 while (argcount < MAX_FUNC_ARGS)
6905 {
6906 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6907 if (*argp == ')' || *argp == ',' || *argp == NUL)
6908 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6910 {
6911 ret = FAIL;
6912 break;
6913 }
6914 ++argcount;
6915 if (*argp != ',')
6916 break;
6917 }
6918 if (*argp == ')')
6919 ++argp;
6920 else
6921 ret = FAIL;
6922
6923 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006924 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006925 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006927 {
6928 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006929 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006930 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006931 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933
6934 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006935 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936
6937 *arg = skipwhite(argp);
6938 return ret;
6939}
6940
6941
6942/*
6943 * Call a function with its resolved parameters
6944 * Return OK or FAIL.
6945 */
6946 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006947call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006948 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 char_u *name; /* name of the function */
6950 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006951 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006953 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 linenr_T firstline; /* first line of range */
6955 linenr_T lastline; /* last line of range */
6956 int *doesrange; /* return: function handled range */
6957 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006958 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959{
6960 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961#define ERROR_UNKNOWN 0
6962#define ERROR_TOOMANY 1
6963#define ERROR_TOOFEW 2
6964#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006965#define ERROR_DICT 4
6966#define ERROR_NONE 5
6967#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 int error = ERROR_NONE;
6969 int i;
6970 int llen;
6971 ufunc_T *fp;
6972 int cc;
6973#define FLEN_FIXED 40
6974 char_u fname_buf[FLEN_FIXED + 1];
6975 char_u *fname;
6976
6977 /*
6978 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6979 * Change <SNR>123_name() to K_SNR 123_name().
6980 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6981 */
6982 cc = name[len];
6983 name[len] = NUL;
6984 llen = eval_fname_script(name);
6985 if (llen > 0)
6986 {
6987 fname_buf[0] = K_SPECIAL;
6988 fname_buf[1] = KS_EXTRA;
6989 fname_buf[2] = (int)KE_SNR;
6990 i = 3;
6991 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6992 {
6993 if (current_SID <= 0)
6994 error = ERROR_SCRIPT;
6995 else
6996 {
6997 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6998 i = (int)STRLEN(fname_buf);
6999 }
7000 }
7001 if (i + STRLEN(name + llen) < FLEN_FIXED)
7002 {
7003 STRCPY(fname_buf + i, name + llen);
7004 fname = fname_buf;
7005 }
7006 else
7007 {
7008 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7009 if (fname == NULL)
7010 error = ERROR_OTHER;
7011 else
7012 {
7013 mch_memmove(fname, fname_buf, (size_t)i);
7014 STRCPY(fname + i, name + llen);
7015 }
7016 }
7017 }
7018 else
7019 fname = name;
7020
7021 *doesrange = FALSE;
7022
7023
7024 /* execute the function if no errors detected and executing */
7025 if (evaluate && error == ERROR_NONE)
7026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007027 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 error = ERROR_UNKNOWN;
7029
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007030 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 {
7032 /*
7033 * User defined function.
7034 */
7035 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007036
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007038 /* Trigger FuncUndefined event, may load the function. */
7039 if (fp == NULL
7040 && apply_autocmds(EVENT_FUNCUNDEFINED,
7041 fname, fname, TRUE, NULL)
7042 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007044 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 fp = find_func(fname);
7046 }
7047#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007048 /* Try loading a package. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007049 if (fp == NULL && script_autoload(fname) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007050 {
7051 /* loaded a package, search for the function again */
7052 fp = find_func(fname);
7053 }
7054
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 if (fp != NULL)
7056 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007057 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007059 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007061 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007063 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007064 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 else
7066 {
7067 /*
7068 * Call the user function.
7069 * Save and restore search patterns, script variables and
7070 * redo buffer.
7071 */
7072 save_search_patterns();
7073 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007074 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007075 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007076 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007077 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7078 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7079 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007080 /* Function was unreferenced while being used, free it
7081 * now. */
7082 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 restoreRedobuff();
7084 restore_search_patterns();
7085 error = ERROR_NONE;
7086 }
7087 }
7088 }
7089 else
7090 {
7091 /*
7092 * Find the function name in the table, call its implementation.
7093 */
7094 i = find_internal_func(fname);
7095 if (i >= 0)
7096 {
7097 if (argcount < functions[i].f_min_argc)
7098 error = ERROR_TOOFEW;
7099 else if (argcount > functions[i].f_max_argc)
7100 error = ERROR_TOOMANY;
7101 else
7102 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007103 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007104 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 error = ERROR_NONE;
7106 }
7107 }
7108 }
7109 /*
7110 * The function call (or "FuncUndefined" autocommand sequence) might
7111 * have been aborted by an error, an interrupt, or an explicitly thrown
7112 * exception that has not been caught so far. This situation can be
7113 * tested for by calling aborting(). For an error in an internal
7114 * function or for the "E132" error in call_user_func(), however, the
7115 * throw point at which the "force_abort" flag (temporarily reset by
7116 * emsg()) is normally updated has not been reached yet. We need to
7117 * update that flag first to make aborting() reliable.
7118 */
7119 update_force_abort();
7120 }
7121 if (error == ERROR_NONE)
7122 ret = OK;
7123
7124 /*
7125 * Report an error unless the argument evaluation or function call has been
7126 * cancelled due to an aborting error, an interrupt, or an exception.
7127 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007128 if (!aborting())
7129 {
7130 switch (error)
7131 {
7132 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007133 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007134 break;
7135 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007136 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007137 break;
7138 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007139 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007140 name);
7141 break;
7142 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007143 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007144 name);
7145 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007146 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007147 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007148 name);
7149 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007150 }
7151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152
7153 name[len] = cc;
7154 if (fname != name && fname != fname_buf)
7155 vim_free(fname);
7156
7157 return ret;
7158}
7159
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007160/*
7161 * Give an error message with a function name. Handle <SNR> things.
7162 */
7163 static void
7164emsg_funcname(msg, name)
7165 char *msg;
7166 char_u *name;
7167{
7168 char_u *p;
7169
7170 if (*name == K_SPECIAL)
7171 p = concat_str((char_u *)"<SNR>", name + 3);
7172 else
7173 p = name;
7174 EMSG2(_(msg), p);
7175 if (p != name)
7176 vim_free(p);
7177}
7178
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179/*********************************************
7180 * Implementation of the built-in functions
7181 */
7182
7183/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007184 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 */
7186 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007187f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 typval_T *argvars;
7189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190{
Bram Moolenaar33570922005-01-25 22:26:29 +00007191 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007193 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007194 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007196 if ((l = argvars[0].vval.v_list) != NULL
7197 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7198 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007199 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007200 }
7201 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007202 EMSG(_(e_listreq));
7203}
7204
7205/*
7206 * "append(lnum, string/list)" function
7207 */
7208 static void
7209f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007210 typval_T *argvars;
7211 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007212{
7213 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007214 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007215 list_T *l = NULL;
7216 listitem_T *li = NULL;
7217 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007218 long added = 0;
7219
Bram Moolenaar0d660222005-01-07 21:51:51 +00007220 lnum = get_tv_lnum(argvars);
7221 if (lnum >= 0
7222 && lnum <= curbuf->b_ml.ml_line_count
7223 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007224 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007225 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007226 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007227 l = argvars[1].vval.v_list;
7228 if (l == NULL)
7229 return;
7230 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007231 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007232 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007233 for (;;)
7234 {
7235 if (l == NULL)
7236 tv = &argvars[1]; /* append a string */
7237 else if (li == NULL)
7238 break; /* end of list */
7239 else
7240 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007241 line = get_tv_string_chk(tv);
7242 if (line == NULL) /* type error */
7243 {
7244 rettv->vval.v_number = 1; /* Failed */
7245 break;
7246 }
7247 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007248 ++added;
7249 if (l == NULL)
7250 break;
7251 li = li->li_next;
7252 }
7253
7254 appended_lines_mark(lnum, added);
7255 if (curwin->w_cursor.lnum > lnum)
7256 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007258 else
7259 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260}
7261
7262/*
7263 * "argc()" function
7264 */
7265/* ARGSUSED */
7266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007267f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007268 typval_T *argvars;
7269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007271 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272}
7273
7274/*
7275 * "argidx()" function
7276 */
7277/* ARGSUSED */
7278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007279f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007280 typval_T *argvars;
7281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007283 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284}
7285
7286/*
7287 * "argv(nr)" function
7288 */
7289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007290f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007291 typval_T *argvars;
7292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293{
7294 int idx;
7295
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007296 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007298 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007300 rettv->vval.v_string = NULL;
7301 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302}
7303
7304/*
7305 * "browse(save, title, initdir, default)" function
7306 */
7307/* ARGSUSED */
7308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007309f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 typval_T *argvars;
7311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312{
7313#ifdef FEAT_BROWSE
7314 int save;
7315 char_u *title;
7316 char_u *initdir;
7317 char_u *defname;
7318 char_u buf[NUMBUFLEN];
7319 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007320 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007322 save = get_tv_number_chk(&argvars[0], &error);
7323 title = get_tv_string_chk(&argvars[1]);
7324 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7325 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007327 if (error || title == NULL || initdir == NULL || defname == NULL)
7328 rettv->vval.v_string = NULL;
7329 else
7330 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007331 do_browse(save ? BROWSE_SAVE : 0,
7332 title, defname, NULL, initdir, NULL, curbuf);
7333#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007334 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007335#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007336 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007337}
7338
7339/*
7340 * "browsedir(title, initdir)" function
7341 */
7342/* ARGSUSED */
7343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007344f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007345 typval_T *argvars;
7346 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007347{
7348#ifdef FEAT_BROWSE
7349 char_u *title;
7350 char_u *initdir;
7351 char_u buf[NUMBUFLEN];
7352
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007353 title = get_tv_string_chk(&argvars[0]);
7354 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007355
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007356 if (title == NULL || initdir == NULL)
7357 rettv->vval.v_string = NULL;
7358 else
7359 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007360 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007362 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007364 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365}
7366
Bram Moolenaar33570922005-01-25 22:26:29 +00007367static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007368
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369/*
7370 * Find a buffer by number or exact name.
7371 */
7372 static buf_T *
7373find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007374 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375{
7376 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007378 if (avar->v_type == VAR_NUMBER)
7379 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007380 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007382 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007383 if (buf == NULL)
7384 {
7385 /* No full path name match, try a match with a URL or a "nofile"
7386 * buffer, these don't use the full path. */
7387 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7388 if (buf->b_fname != NULL
7389 && (path_with_url(buf->b_fname)
7390#ifdef FEAT_QUICKFIX
7391 || bt_nofile(buf)
7392#endif
7393 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007394 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007395 break;
7396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 }
7398 return buf;
7399}
7400
7401/*
7402 * "bufexists(expr)" function
7403 */
7404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007405f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007406 typval_T *argvars;
7407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007409 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410}
7411
7412/*
7413 * "buflisted(expr)" function
7414 */
7415 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007416f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007417 typval_T *argvars;
7418 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419{
7420 buf_T *buf;
7421
7422 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007423 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424}
7425
7426/*
7427 * "bufloaded(expr)" function
7428 */
7429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007430f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007431 typval_T *argvars;
7432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433{
7434 buf_T *buf;
7435
7436 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007437 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438}
7439
Bram Moolenaar33570922005-01-25 22:26:29 +00007440static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007441
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442/*
7443 * Get buffer by number or pattern.
7444 */
7445 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007446get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007447 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007449 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 int save_magic;
7451 char_u *save_cpo;
7452 buf_T *buf;
7453
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007454 if (tv->v_type == VAR_NUMBER)
7455 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007456 if (tv->v_type != VAR_STRING)
7457 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 if (name == NULL || *name == NUL)
7459 return curbuf;
7460 if (name[0] == '$' && name[1] == NUL)
7461 return lastbuf;
7462
7463 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7464 save_magic = p_magic;
7465 p_magic = TRUE;
7466 save_cpo = p_cpo;
7467 p_cpo = (char_u *)"";
7468
7469 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7470 TRUE, FALSE));
7471
7472 p_magic = save_magic;
7473 p_cpo = save_cpo;
7474
7475 /* If not found, try expanding the name, like done for bufexists(). */
7476 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007477 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478
7479 return buf;
7480}
7481
7482/*
7483 * "bufname(expr)" function
7484 */
7485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007486f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007487 typval_T *argvars;
7488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489{
7490 buf_T *buf;
7491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007492 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007494 buf = get_buf_tv(&argvars[0]);
7495 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007497 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007499 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 --emsg_off;
7501}
7502
7503/*
7504 * "bufnr(expr)" function
7505 */
7506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007507f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007508 typval_T *argvars;
7509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510{
7511 buf_T *buf;
7512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007513 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007515 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007517 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007519 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 --emsg_off;
7521}
7522
7523/*
7524 * "bufwinnr(nr)" function
7525 */
7526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007527f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007528 typval_T *argvars;
7529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530{
7531#ifdef FEAT_WINDOWS
7532 win_T *wp;
7533 int winnr = 0;
7534#endif
7535 buf_T *buf;
7536
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007537 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007539 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540#ifdef FEAT_WINDOWS
7541 for (wp = firstwin; wp; wp = wp->w_next)
7542 {
7543 ++winnr;
7544 if (wp->w_buffer == buf)
7545 break;
7546 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007547 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007549 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550#endif
7551 --emsg_off;
7552}
7553
7554/*
7555 * "byte2line(byte)" function
7556 */
7557/*ARGSUSED*/
7558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007559f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007560 typval_T *argvars;
7561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562{
7563#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007564 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565#else
7566 long boff = 0;
7567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007568 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007570 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007572 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 (linenr_T)0, &boff);
7574#endif
7575}
7576
7577/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007578 * "byteidx()" function
7579 */
7580/*ARGSUSED*/
7581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007582f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 typval_T *argvars;
7584 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007585{
7586#ifdef FEAT_MBYTE
7587 char_u *t;
7588#endif
7589 char_u *str;
7590 long idx;
7591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007592 str = get_tv_string_chk(&argvars[0]);
7593 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007594 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007595 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007596 return;
7597
7598#ifdef FEAT_MBYTE
7599 t = str;
7600 for ( ; idx > 0; idx--)
7601 {
7602 if (*t == NUL) /* EOL reached */
7603 return;
7604 t += mb_ptr2len_check(t);
7605 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007606 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007607#else
7608 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007609 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007610#endif
7611}
7612
7613/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007614 * "call(func, arglist)" function
7615 */
7616 static void
7617f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 typval_T *argvars;
7619 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007620{
7621 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007622 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007623 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007624 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007625 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007627
7628 rettv->vval.v_number = 0;
7629 if (argvars[1].v_type != VAR_LIST)
7630 {
7631 EMSG(_(e_listreq));
7632 return;
7633 }
7634 if (argvars[1].vval.v_list == NULL)
7635 return;
7636
7637 if (argvars[0].v_type == VAR_FUNC)
7638 func = argvars[0].vval.v_string;
7639 else
7640 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007641 if (*func == NUL)
7642 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007643
Bram Moolenaare9a41262005-01-15 22:18:47 +00007644 if (argvars[2].v_type != VAR_UNKNOWN)
7645 {
7646 if (argvars[2].v_type != VAR_DICT)
7647 {
7648 EMSG(_(e_dictreq));
7649 return;
7650 }
7651 selfdict = argvars[2].vval.v_dict;
7652 }
7653
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007654 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7655 item = item->li_next)
7656 {
7657 if (argc == MAX_FUNC_ARGS)
7658 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007659 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007660 break;
7661 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007662 /* Make a copy of each argument. This is needed to be able to set
7663 * v_lock to VAR_FIXED in the copy without changing the original list.
7664 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007665 copy_tv(&item->li_tv, &argv[argc++]);
7666 }
7667
7668 if (item == NULL)
7669 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007670 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7671 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007672
7673 /* Free the arguments. */
7674 while (argc > 0)
7675 clear_tv(&argv[--argc]);
7676}
7677
7678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 * "char2nr(string)" function
7680 */
7681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007682f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007683 typval_T *argvars;
7684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685{
7686#ifdef FEAT_MBYTE
7687 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007688 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 else
7690#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007691 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692}
7693
7694/*
7695 * "cindent(lnum)" function
7696 */
7697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007698f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007699 typval_T *argvars;
7700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701{
7702#ifdef FEAT_CINDENT
7703 pos_T pos;
7704 linenr_T lnum;
7705
7706 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007707 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7709 {
7710 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007711 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 curwin->w_cursor = pos;
7713 }
7714 else
7715#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007716 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717}
7718
7719/*
7720 * "col(string)" function
7721 */
7722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007723f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007724 typval_T *argvars;
7725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726{
7727 colnr_T col = 0;
7728 pos_T *fp;
7729
7730 fp = var2fpos(&argvars[0], FALSE);
7731 if (fp != NULL)
7732 {
7733 if (fp->col == MAXCOL)
7734 {
7735 /* '> can be MAXCOL, get the length of the line then */
7736 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7737 col = STRLEN(ml_get(fp->lnum)) + 1;
7738 else
7739 col = MAXCOL;
7740 }
7741 else
7742 {
7743 col = fp->col + 1;
7744#ifdef FEAT_VIRTUALEDIT
7745 /* col(".") when the cursor is on the NUL at the end of the line
7746 * because of "coladd" can be seen as an extra column. */
7747 if (virtual_active() && fp == &curwin->w_cursor)
7748 {
7749 char_u *p = ml_get_cursor();
7750
7751 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7752 curwin->w_virtcol - curwin->w_cursor.coladd))
7753 {
7754# ifdef FEAT_MBYTE
7755 int l;
7756
7757 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7758 col += l;
7759# else
7760 if (*p != NUL && p[1] == NUL)
7761 ++col;
7762# endif
7763 }
7764 }
7765#endif
7766 }
7767 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007768 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769}
7770
7771/*
7772 * "confirm(message, buttons[, default [, type]])" function
7773 */
7774/*ARGSUSED*/
7775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007777 typval_T *argvars;
7778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
7780#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7781 char_u *message;
7782 char_u *buttons = NULL;
7783 char_u buf[NUMBUFLEN];
7784 char_u buf2[NUMBUFLEN];
7785 int def = 1;
7786 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007787 char_u *typestr;
7788 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007790 message = get_tv_string_chk(&argvars[0]);
7791 if (message == NULL)
7792 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007793 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007795 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7796 if (buttons == NULL)
7797 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007798 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007800 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007801 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007803 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7804 if (typestr == NULL)
7805 error = TRUE;
7806 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007808 switch (TOUPPER_ASC(*typestr))
7809 {
7810 case 'E': type = VIM_ERROR; break;
7811 case 'Q': type = VIM_QUESTION; break;
7812 case 'I': type = VIM_INFO; break;
7813 case 'W': type = VIM_WARNING; break;
7814 case 'G': type = VIM_GENERIC; break;
7815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 }
7817 }
7818 }
7819 }
7820
7821 if (buttons == NULL || *buttons == NUL)
7822 buttons = (char_u *)_("&Ok");
7823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007824 if (error)
7825 rettv->vval.v_number = 0;
7826 else
7827 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 def, NULL);
7829#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007830 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831#endif
7832}
7833
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007834/*
7835 * "copy()" function
7836 */
7837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007838f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007839 typval_T *argvars;
7840 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007841{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007842 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007843}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844
7845/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007846 * "count()" function
7847 */
7848 static void
7849f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007850 typval_T *argvars;
7851 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007852{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007853 long n = 0;
7854 int ic = FALSE;
7855
Bram Moolenaare9a41262005-01-15 22:18:47 +00007856 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007857 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007858 listitem_T *li;
7859 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007860 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007861
Bram Moolenaare9a41262005-01-15 22:18:47 +00007862 if ((l = argvars[0].vval.v_list) != NULL)
7863 {
7864 li = l->lv_first;
7865 if (argvars[2].v_type != VAR_UNKNOWN)
7866 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007867 int error = FALSE;
7868
7869 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007870 if (argvars[3].v_type != VAR_UNKNOWN)
7871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007872 idx = get_tv_number_chk(&argvars[3], &error);
7873 if (!error)
7874 {
7875 li = list_find(l, idx);
7876 if (li == NULL)
7877 EMSGN(_(e_listidx), idx);
7878 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007880 if (error)
7881 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007882 }
7883
7884 for ( ; li != NULL; li = li->li_next)
7885 if (tv_equal(&li->li_tv, &argvars[1], ic))
7886 ++n;
7887 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007888 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007889 else if (argvars[0].v_type == VAR_DICT)
7890 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007891 int todo;
7892 dict_T *d;
7893 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007894
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007895 if ((d = argvars[0].vval.v_dict) != NULL)
7896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007897 int error = FALSE;
7898
Bram Moolenaare9a41262005-01-15 22:18:47 +00007899 if (argvars[2].v_type != VAR_UNKNOWN)
7900 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007901 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007902 if (argvars[3].v_type != VAR_UNKNOWN)
7903 EMSG(_(e_invarg));
7904 }
7905
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007906 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007907 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007908 {
7909 if (!HASHITEM_EMPTY(hi))
7910 {
7911 --todo;
7912 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7913 ++n;
7914 }
7915 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007916 }
7917 }
7918 else
7919 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007920 rettv->vval.v_number = n;
7921}
7922
7923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7925 *
7926 * Checks the existence of a cscope connection.
7927 */
7928/*ARGSUSED*/
7929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007930f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007931 typval_T *argvars;
7932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
7934#ifdef FEAT_CSCOPE
7935 int num = 0;
7936 char_u *dbpath = NULL;
7937 char_u *prepend = NULL;
7938 char_u buf[NUMBUFLEN];
7939
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007940 if (argvars[0].v_type != VAR_UNKNOWN
7941 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007943 num = (int)get_tv_number(&argvars[0]);
7944 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007945 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007946 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 }
7948
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007949 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007951 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952#endif
7953}
7954
7955/*
7956 * "cursor(lnum, col)" function
7957 *
7958 * Moves the cursor to the specified line and column
7959 */
7960/*ARGSUSED*/
7961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007962f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007963 typval_T *argvars;
7964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965{
7966 long line, col;
7967
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007968 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007969 col = get_tv_number_chk(&argvars[1], NULL);
7970 if (line < 0 || col < 0)
7971 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 if (line > 0)
7973 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 if (col > 0)
7975 curwin->w_cursor.col = col - 1;
7976#ifdef FEAT_VIRTUALEDIT
7977 curwin->w_cursor.coladd = 0;
7978#endif
7979
7980 /* Make sure the cursor is in a valid position. */
7981 check_cursor();
7982#ifdef FEAT_MBYTE
7983 /* Correct cursor for multi-byte character. */
7984 if (has_mbyte)
7985 mb_adjust_cursor();
7986#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007987
7988 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989}
7990
7991/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007992 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 */
7994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007995f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007996 typval_T *argvars;
7997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007999 int noref = 0;
8000
8001 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008002 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008003 if (noref < 0 || noref > 1)
8004 EMSG(_(e_invarg));
8005 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008006 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007}
8008
8009/*
8010 * "delete()" function
8011 */
8012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008013f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008014 typval_T *argvars;
8015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
8017 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008018 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008020 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021}
8022
8023/*
8024 * "did_filetype()" function
8025 */
8026/*ARGSUSED*/
8027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008028f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008029 typval_T *argvars;
8030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031{
8032#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008033 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008035 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036#endif
8037}
8038
8039/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008040 * "diff_filler()" function
8041 */
8042/*ARGSUSED*/
8043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008044f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008045 typval_T *argvars;
8046 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008047{
8048#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008049 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008050#endif
8051}
8052
8053/*
8054 * "diff_hlID()" function
8055 */
8056/*ARGSUSED*/
8057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008058f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008059 typval_T *argvars;
8060 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008061{
8062#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008063 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008064 static linenr_T prev_lnum = 0;
8065 static int changedtick = 0;
8066 static int fnum = 0;
8067 static int change_start = 0;
8068 static int change_end = 0;
8069 static enum hlf_value hlID = 0;
8070 int filler_lines;
8071 int col;
8072
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008073 if (lnum < 0) /* ignore type error in {lnum} arg */
8074 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008075 if (lnum != prev_lnum
8076 || changedtick != curbuf->b_changedtick
8077 || fnum != curbuf->b_fnum)
8078 {
8079 /* New line, buffer, change: need to get the values. */
8080 filler_lines = diff_check(curwin, lnum);
8081 if (filler_lines < 0)
8082 {
8083 if (filler_lines == -1)
8084 {
8085 change_start = MAXCOL;
8086 change_end = -1;
8087 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8088 hlID = HLF_ADD; /* added line */
8089 else
8090 hlID = HLF_CHD; /* changed line */
8091 }
8092 else
8093 hlID = HLF_ADD; /* added line */
8094 }
8095 else
8096 hlID = (enum hlf_value)0;
8097 prev_lnum = lnum;
8098 changedtick = curbuf->b_changedtick;
8099 fnum = curbuf->b_fnum;
8100 }
8101
8102 if (hlID == HLF_CHD || hlID == HLF_TXD)
8103 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008104 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008105 if (col >= change_start && col <= change_end)
8106 hlID = HLF_TXD; /* changed text */
8107 else
8108 hlID = HLF_CHD; /* changed line */
8109 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008110 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008111#endif
8112}
8113
8114/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008115 * "empty({expr})" function
8116 */
8117 static void
8118f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008119 typval_T *argvars;
8120 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008121{
8122 int n;
8123
8124 switch (argvars[0].v_type)
8125 {
8126 case VAR_STRING:
8127 case VAR_FUNC:
8128 n = argvars[0].vval.v_string == NULL
8129 || *argvars[0].vval.v_string == NUL;
8130 break;
8131 case VAR_NUMBER:
8132 n = argvars[0].vval.v_number == 0;
8133 break;
8134 case VAR_LIST:
8135 n = argvars[0].vval.v_list == NULL
8136 || argvars[0].vval.v_list->lv_first == NULL;
8137 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008138 case VAR_DICT:
8139 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008140 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008141 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008142 default:
8143 EMSG2(_(e_intern2), "f_empty()");
8144 n = 0;
8145 }
8146
8147 rettv->vval.v_number = n;
8148}
8149
8150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 * "escape({string}, {chars})" function
8152 */
8153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008154f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008155 typval_T *argvars;
8156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157{
8158 char_u buf[NUMBUFLEN];
8159
Bram Moolenaar758711c2005-02-02 23:11:38 +00008160 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8161 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008162 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163}
8164
8165/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008166 * "eval()" function
8167 */
8168/*ARGSUSED*/
8169 static void
8170f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008171 typval_T *argvars;
8172 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008173{
8174 char_u *s;
8175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008176 s = get_tv_string_chk(&argvars[0]);
8177 if (s != NULL)
8178 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008180 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8181 {
8182 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008183 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008184 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008185 else if (*s != NUL)
8186 EMSG(_(e_trailing));
8187}
8188
8189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 * "eventhandler()" function
8191 */
8192/*ARGSUSED*/
8193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008194f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008195 typval_T *argvars;
8196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008198 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199}
8200
8201/*
8202 * "executable()" function
8203 */
8204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008205f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008206 typval_T *argvars;
8207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008209 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210}
8211
8212/*
8213 * "exists()" function
8214 */
8215 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008216f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008217 typval_T *argvars;
8218 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219{
8220 char_u *p;
8221 char_u *name;
8222 int n = FALSE;
8223 int len = 0;
8224
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008225 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 if (*p == '$') /* environment variable */
8227 {
8228 /* first try "normal" environment variables (fast) */
8229 if (mch_getenv(p + 1) != NULL)
8230 n = TRUE;
8231 else
8232 {
8233 /* try expanding things like $VIM and ${HOME} */
8234 p = expand_env_save(p);
8235 if (p != NULL && *p != '$')
8236 n = TRUE;
8237 vim_free(p);
8238 }
8239 }
8240 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008241 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 else if (*p == '*') /* internal or user defined function */
8243 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008244 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 }
8246 else if (*p == ':')
8247 {
8248 n = cmd_exists(p + 1);
8249 }
8250 else if (*p == '#')
8251 {
8252#ifdef FEAT_AUTOCMD
8253 name = p + 1;
8254 p = vim_strchr(name, '#');
8255 if (p != NULL)
8256 n = au_exists(name, p, p + 1);
8257 else
8258 n = au_exists(name, name + STRLEN(name), NULL);
8259#endif
8260 }
8261 else /* internal variable */
8262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008263 char_u *tofree;
8264 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008266 /* get_name_len() takes care of expanding curly braces */
8267 name = p;
8268 len = get_name_len(&p, &tofree, TRUE, FALSE);
8269 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008271 if (tofree != NULL)
8272 name = tofree;
8273 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8274 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008276 /* handle d.key, l[idx], f(expr) */
8277 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8278 if (n)
8279 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 }
8281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008283 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 }
8285
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008286 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287}
8288
8289/*
8290 * "expand()" function
8291 */
8292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008293f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008294 typval_T *argvars;
8295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296{
8297 char_u *s;
8298 int len;
8299 char_u *errormsg;
8300 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8301 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008302 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008304 rettv->v_type = VAR_STRING;
8305 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 if (*s == '%' || *s == '#' || *s == '<')
8307 {
8308 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008309 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 --emsg_off;
8311 }
8312 else
8313 {
8314 /* When the optional second argument is non-zero, don't remove matches
8315 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008316 if (argvars[1].v_type != VAR_UNKNOWN
8317 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008319 if (!error)
8320 {
8321 ExpandInit(&xpc);
8322 xpc.xp_context = EXPAND_FILES;
8323 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8324 ExpandCleanup(&xpc);
8325 }
8326 else
8327 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 }
8329}
8330
8331/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008332 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008333 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008334 */
8335 static void
8336f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008337 typval_T *argvars;
8338 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008339{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008340 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008341 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008342 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008343 list_T *l1, *l2;
8344 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008345 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008346 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008347
Bram Moolenaare9a41262005-01-15 22:18:47 +00008348 l1 = argvars[0].vval.v_list;
8349 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008350 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8351 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008352 {
8353 if (argvars[2].v_type != VAR_UNKNOWN)
8354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008355 before = get_tv_number_chk(&argvars[2], &error);
8356 if (error)
8357 return; /* type error; errmsg already given */
8358
Bram Moolenaar758711c2005-02-02 23:11:38 +00008359 if (before == l1->lv_len)
8360 item = NULL;
8361 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008362 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008363 item = list_find(l1, before);
8364 if (item == NULL)
8365 {
8366 EMSGN(_(e_listidx), before);
8367 return;
8368 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008369 }
8370 }
8371 else
8372 item = NULL;
8373 list_extend(l1, l2, item);
8374
Bram Moolenaare9a41262005-01-15 22:18:47 +00008375 copy_tv(&argvars[0], rettv);
8376 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008377 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008378 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8379 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008380 dict_T *d1, *d2;
8381 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008382 char_u *action;
8383 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008384 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008385 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008386
8387 d1 = argvars[0].vval.v_dict;
8388 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008389 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8390 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008391 {
8392 /* Check the third argument. */
8393 if (argvars[2].v_type != VAR_UNKNOWN)
8394 {
8395 static char *(av[]) = {"keep", "force", "error"};
8396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008397 action = get_tv_string_chk(&argvars[2]);
8398 if (action == NULL)
8399 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008400 for (i = 0; i < 3; ++i)
8401 if (STRCMP(action, av[i]) == 0)
8402 break;
8403 if (i == 3)
8404 {
8405 EMSGN(_(e_invarg2), action);
8406 return;
8407 }
8408 }
8409 else
8410 action = (char_u *)"force";
8411
8412 /* Go over all entries in the second dict and add them to the
8413 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008414 todo = d2->dv_hashtab.ht_used;
8415 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008416 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008417 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008418 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008419 --todo;
8420 di1 = dict_find(d1, hi2->hi_key, -1);
8421 if (di1 == NULL)
8422 {
8423 di1 = dictitem_copy(HI2DI(hi2));
8424 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8425 dictitem_free(di1);
8426 }
8427 else if (*action == 'e')
8428 {
8429 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8430 break;
8431 }
8432 else if (*action == 'f')
8433 {
8434 clear_tv(&di1->di_tv);
8435 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8436 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008437 }
8438 }
8439
Bram Moolenaare9a41262005-01-15 22:18:47 +00008440 copy_tv(&argvars[0], rettv);
8441 }
8442 }
8443 else
8444 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008445}
8446
8447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 * "filereadable()" function
8449 */
8450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008451f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008452 typval_T *argvars;
8453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 FILE *fd;
8456 char_u *p;
8457 int n;
8458
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008459 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8461 {
8462 n = TRUE;
8463 fclose(fd);
8464 }
8465 else
8466 n = FALSE;
8467
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008468 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469}
8470
8471/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008472 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 * rights to write into.
8474 */
8475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008477 typval_T *argvars;
8478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008480 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481}
8482
Bram Moolenaar33570922005-01-25 22:26:29 +00008483static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008484
8485 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008486findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008487 typval_T *argvars;
8488 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008489 int dir;
8490{
8491#ifdef FEAT_SEARCHPATH
8492 char_u *fname;
8493 char_u *fresult = NULL;
8494 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8495 char_u *p;
8496 char_u pathbuf[NUMBUFLEN];
8497 int count = 1;
8498 int first = TRUE;
8499
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008500 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008501
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008502 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008503 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008504 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8505 if (p == NULL)
8506 count = -1; /* error */
8507 else
8508 {
8509 if (*p != NUL)
8510 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008512 if (argvars[2].v_type != VAR_UNKNOWN)
8513 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8514 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008515 }
8516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008517 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008518 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008519 do
8520 {
8521 vim_free(fresult);
8522 fresult = find_file_in_path_option(first ? fname : NULL,
8523 first ? (int)STRLEN(fname) : 0,
8524 0, first, path, dir, NULL);
8525 first = FALSE;
8526 } while (--count > 0 && fresult != NULL);
8527 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008528
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008529 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008530#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008531 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008532#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008533 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008534}
8535
Bram Moolenaar33570922005-01-25 22:26:29 +00008536static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8537static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008538
8539/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008540 * Prepare v: variable "idx" to be used.
8541 * Save the current typeval in "save_tv".
8542 * When not used yet add the variable to the v: hashtable.
8543 */
8544 static void
8545prepare_vimvar(idx, save_tv)
8546 int idx;
8547 typval_T *save_tv;
8548{
8549 *save_tv = vimvars[idx].vv_tv;
8550 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8551 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
8552}
8553
8554/*
8555 * Restore v: variable "idx" to typeval "save_tv".
8556 * When no longer defined, remove the variable from the v: hashtable.
8557 */
8558 static void
8559restore_vimvar(idx, save_tv)
8560 int idx;
8561 typval_T *save_tv;
8562{
8563 hashitem_T *hi;
8564
8565 clear_tv(&vimvars[idx].vv_tv);
8566 vimvars[idx].vv_tv = *save_tv;
8567 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8568 {
8569 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
8570 if (HASHITEM_EMPTY(hi))
8571 EMSG2(_(e_intern2), "restore_vimvar()");
8572 else
8573 hash_remove(&vimvarht, hi);
8574 }
8575}
8576
8577/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008578 * Implementation of map() and filter().
8579 */
8580 static void
8581filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008582 typval_T *argvars;
8583 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008584 int map;
8585{
8586 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008587 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008588 listitem_T *li, *nli;
8589 list_T *l = NULL;
8590 dictitem_T *di;
8591 hashtab_T *ht;
8592 hashitem_T *hi;
8593 dict_T *d = NULL;
8594 typval_T save_val;
8595 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008596 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008597 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008598 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8599
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008600
8601 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008602 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008603 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008604 if ((l = argvars[0].vval.v_list) == NULL
8605 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008606 return;
8607 }
8608 else if (argvars[0].v_type == VAR_DICT)
8609 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008610 if ((d = argvars[0].vval.v_dict) == NULL
8611 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008612 return;
8613 }
8614 else
8615 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008616 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008617 return;
8618 }
8619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008620 expr = get_tv_string_buf_chk(&argvars[1], buf);
8621 /* On type errors, the preceding call has already displayed an error
8622 * message. Avoid a misleading error message for an empty string that
8623 * was not passed as argument. */
8624 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008626 prepare_vimvar(VV_VAL, &save_val);
8627 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008628
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008629 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008631 prepare_vimvar(VV_KEY, &save_key);
8632 vimvars[VV_KEY].vv_type = VAR_STRING;
8633
8634 ht = &d->dv_hashtab;
8635 hash_lock(ht);
8636 todo = ht->ht_used;
8637 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008638 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008639 if (!HASHITEM_EMPTY(hi))
8640 {
8641 --todo;
8642 di = HI2DI(hi);
8643 if (tv_check_lock(di->di_tv.v_lock, msg))
8644 break;
8645 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8646 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8647 break;
8648 if (!map && rem)
8649 dictitem_remove(d, di);
8650 clear_tv(&vimvars[VV_KEY].vv_tv);
8651 }
8652 }
8653 hash_unlock(ht);
8654
8655 restore_vimvar(VV_KEY, &save_key);
8656 }
8657 else
8658 {
8659 for (li = l->lv_first; li != NULL; li = nli)
8660 {
8661 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008662 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008663 nli = li->li_next;
8664 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008665 break;
8666 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008667 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008668 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008669 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008671 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008672 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008673
8674 copy_tv(&argvars[0], rettv);
8675}
8676
8677 static int
8678filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008679 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008680 char_u *expr;
8681 int map;
8682 int *remp;
8683{
Bram Moolenaar33570922005-01-25 22:26:29 +00008684 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008685 char_u *s;
8686
Bram Moolenaar33570922005-01-25 22:26:29 +00008687 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008688 s = expr;
8689 if (eval1(&s, &rettv, TRUE) == FAIL)
8690 return FAIL;
8691 if (*s != NUL) /* check for trailing chars after expr */
8692 {
8693 EMSG2(_(e_invexpr2), s);
8694 return FAIL;
8695 }
8696 if (map)
8697 {
8698 /* map(): replace the list item value */
8699 clear_tv(tv);
8700 *tv = rettv;
8701 }
8702 else
8703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008704 int error = FALSE;
8705
Bram Moolenaare9a41262005-01-15 22:18:47 +00008706 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008707 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008708 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008709 /* On type error, nothing has been removed; return FAIL to stop the
8710 * loop. The error message was given by get_tv_number_chk(). */
8711 if (error)
8712 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008713 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008714 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008715 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008716}
8717
8718/*
8719 * "filter()" function
8720 */
8721 static void
8722f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008723 typval_T *argvars;
8724 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008725{
8726 filter_map(argvars, rettv, FALSE);
8727}
8728
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008729/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008730 * "finddir({fname}[, {path}[, {count}]])" function
8731 */
8732 static void
8733f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008734 typval_T *argvars;
8735 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008736{
8737 findfilendir(argvars, rettv, TRUE);
8738}
8739
8740/*
8741 * "findfile({fname}[, {path}[, {count}]])" function
8742 */
8743 static void
8744f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008745 typval_T *argvars;
8746 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008747{
8748 findfilendir(argvars, rettv, FALSE);
8749}
8750
8751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 * "fnamemodify({fname}, {mods})" function
8753 */
8754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008755f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008756 typval_T *argvars;
8757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758{
8759 char_u *fname;
8760 char_u *mods;
8761 int usedlen = 0;
8762 int len;
8763 char_u *fbuf = NULL;
8764 char_u buf[NUMBUFLEN];
8765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008766 fname = get_tv_string_chk(&argvars[0]);
8767 mods = get_tv_string_buf_chk(&argvars[1], buf);
8768 if (fname == NULL || mods == NULL)
8769 fname = NULL;
8770 else
8771 {
8772 len = (int)STRLEN(fname);
8773 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008776 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008778 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 vim_free(fbuf);
8782}
8783
Bram Moolenaar33570922005-01-25 22:26:29 +00008784static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785
8786/*
8787 * "foldclosed()" function
8788 */
8789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008791 typval_T *argvars;
8792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 int end;
8794{
8795#ifdef FEAT_FOLDING
8796 linenr_T lnum;
8797 linenr_T first, last;
8798
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008799 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8801 {
8802 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8803 {
8804 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008805 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008807 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 return;
8809 }
8810 }
8811#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008812 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813}
8814
8815/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008816 * "foldclosed()" function
8817 */
8818 static void
8819f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008820 typval_T *argvars;
8821 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008822{
8823 foldclosed_both(argvars, rettv, FALSE);
8824}
8825
8826/*
8827 * "foldclosedend()" function
8828 */
8829 static void
8830f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008831 typval_T *argvars;
8832 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008833{
8834 foldclosed_both(argvars, rettv, TRUE);
8835}
8836
8837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 * "foldlevel()" function
8839 */
8840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008841f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008842 typval_T *argvars;
8843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844{
8845#ifdef FEAT_FOLDING
8846 linenr_T lnum;
8847
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008850 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 else
8852#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008853 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854}
8855
8856/*
8857 * "foldtext()" function
8858 */
8859/*ARGSUSED*/
8860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008861f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008862 typval_T *argvars;
8863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864{
8865#ifdef FEAT_FOLDING
8866 linenr_T lnum;
8867 char_u *s;
8868 char_u *r;
8869 int len;
8870 char *txt;
8871#endif
8872
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008873 rettv->v_type = VAR_STRING;
8874 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008876 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8877 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8878 <= curbuf->b_ml.ml_line_count
8879 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 {
8881 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008882 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8883 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 {
8885 if (!linewhite(lnum))
8886 break;
8887 ++lnum;
8888 }
8889
8890 /* Find interesting text in this line. */
8891 s = skipwhite(ml_get(lnum));
8892 /* skip C comment-start */
8893 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008896 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008897 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008898 {
8899 s = skipwhite(ml_get(lnum + 1));
8900 if (*s == '*')
8901 s = skipwhite(s + 1);
8902 }
8903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 txt = _("+-%s%3ld lines: ");
8905 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008906 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 + 20 /* for %3ld */
8908 + STRLEN(s))); /* concatenated */
8909 if (r != NULL)
8910 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008911 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8912 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8913 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 len = (int)STRLEN(r);
8915 STRCAT(r, s);
8916 /* remove 'foldmarker' and 'commentstring' */
8917 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008918 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 }
8920 }
8921#endif
8922}
8923
8924/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008925 * "foldtextresult(lnum)" function
8926 */
8927/*ARGSUSED*/
8928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008929f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008930 typval_T *argvars;
8931 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008932{
8933#ifdef FEAT_FOLDING
8934 linenr_T lnum;
8935 char_u *text;
8936 char_u buf[51];
8937 foldinfo_T foldinfo;
8938 int fold_count;
8939#endif
8940
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008941 rettv->v_type = VAR_STRING;
8942 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008943#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008945 /* treat illegal types and illegal string values for {lnum} the same */
8946 if (lnum < 0)
8947 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008948 fold_count = foldedCount(curwin, lnum, &foldinfo);
8949 if (fold_count > 0)
8950 {
8951 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8952 &foldinfo, buf);
8953 if (text == buf)
8954 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008955 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008956 }
8957#endif
8958}
8959
8960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 * "foreground()" function
8962 */
8963/*ARGSUSED*/
8964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008965f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008966 typval_T *argvars;
8967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970#ifdef FEAT_GUI
8971 if (gui.in_use)
8972 gui_mch_set_foreground();
8973#else
8974# ifdef WIN32
8975 win32_set_foreground();
8976# endif
8977#endif
8978}
8979
8980/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008981 * "function()" function
8982 */
8983/*ARGSUSED*/
8984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008985f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008986 typval_T *argvars;
8987 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008988{
8989 char_u *s;
8990
Bram Moolenaara7043832005-01-21 11:56:39 +00008991 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008992 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008993 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008994 EMSG2(_(e_invarg2), s);
8995 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008996 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008997 else
8998 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008999 rettv->vval.v_string = vim_strsave(s);
9000 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009001 }
9002}
9003
9004/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009005 * "garbagecollect()" function
9006 */
9007/*ARGSUSED*/
9008 static void
9009f_garbagecollect(argvars, rettv)
9010 typval_T *argvars;
9011 typval_T *rettv;
9012{
9013 garbage_collect();
9014}
9015
9016/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009017 * "get()" function
9018 */
9019 static void
9020f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009021 typval_T *argvars;
9022 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009023{
Bram Moolenaar33570922005-01-25 22:26:29 +00009024 listitem_T *li;
9025 list_T *l;
9026 dictitem_T *di;
9027 dict_T *d;
9028 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009029
Bram Moolenaare9a41262005-01-15 22:18:47 +00009030 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009031 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009032 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009033 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009034 int error = FALSE;
9035
9036 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9037 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009038 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009039 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009040 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009041 else if (argvars[0].v_type == VAR_DICT)
9042 {
9043 if ((d = argvars[0].vval.v_dict) != NULL)
9044 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009045 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009046 if (di != NULL)
9047 tv = &di->di_tv;
9048 }
9049 }
9050 else
9051 EMSG2(_(e_listdictarg), "get()");
9052
9053 if (tv == NULL)
9054 {
9055 if (argvars[2].v_type == VAR_UNKNOWN)
9056 rettv->vval.v_number = 0;
9057 else
9058 copy_tv(&argvars[2], rettv);
9059 }
9060 else
9061 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009062}
9063
9064/*
9065 * "getbufvar()" function
9066 */
9067 static void
9068f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009069 typval_T *argvars;
9070 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009071{
9072 buf_T *buf;
9073 buf_T *save_curbuf;
9074 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009075 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009076
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009077 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9078 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009079 ++emsg_off;
9080 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009081
9082 rettv->v_type = VAR_STRING;
9083 rettv->vval.v_string = NULL;
9084
9085 if (buf != NULL && varname != NULL)
9086 {
9087 if (*varname == '&') /* buffer-local-option */
9088 {
9089 /* set curbuf to be our buf, temporarily */
9090 save_curbuf = curbuf;
9091 curbuf = buf;
9092
9093 get_option_tv(&varname, rettv, TRUE);
9094
9095 /* restore previous notion of curbuf */
9096 curbuf = save_curbuf;
9097 }
9098 else
9099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009100 if (*varname == NUL)
9101 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9102 * scope prefix before the NUL byte is required by
9103 * find_var_in_ht(). */
9104 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009105 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009106 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009107 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009108 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009109 }
9110 }
9111
9112 --emsg_off;
9113}
9114
9115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 * "getchar()" function
9117 */
9118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009119f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009120 typval_T *argvars;
9121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122{
9123 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009124 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125
9126 ++no_mapping;
9127 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009128 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 /* getchar(): blocking wait. */
9130 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009131 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 /* getchar(1): only check if char avail */
9133 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009134 else if (error || vpeekc() == NUL)
9135 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 n = 0;
9137 else
9138 /* getchar(0) and char avail: return char */
9139 n = safe_vgetc();
9140 --no_mapping;
9141 --allow_keys;
9142
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009143 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 if (IS_SPECIAL(n) || mod_mask != 0)
9145 {
9146 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9147 int i = 0;
9148
9149 /* Turn a special key into three bytes, plus modifier. */
9150 if (mod_mask != 0)
9151 {
9152 temp[i++] = K_SPECIAL;
9153 temp[i++] = KS_MODIFIER;
9154 temp[i++] = mod_mask;
9155 }
9156 if (IS_SPECIAL(n))
9157 {
9158 temp[i++] = K_SPECIAL;
9159 temp[i++] = K_SECOND(n);
9160 temp[i++] = K_THIRD(n);
9161 }
9162#ifdef FEAT_MBYTE
9163 else if (has_mbyte)
9164 i += (*mb_char2bytes)(n, temp + i);
9165#endif
9166 else
9167 temp[i++] = n;
9168 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009169 rettv->v_type = VAR_STRING;
9170 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 }
9172}
9173
9174/*
9175 * "getcharmod()" function
9176 */
9177/*ARGSUSED*/
9178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009179f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009180 typval_T *argvars;
9181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184}
9185
9186/*
9187 * "getcmdline()" function
9188 */
9189/*ARGSUSED*/
9190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009191f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009192 typval_T *argvars;
9193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->v_type = VAR_STRING;
9196 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197}
9198
9199/*
9200 * "getcmdpos()" function
9201 */
9202/*ARGSUSED*/
9203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009204f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009205 typval_T *argvars;
9206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009208 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209}
9210
9211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 * "getcwd()" function
9213 */
9214/*ARGSUSED*/
9215 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009217 typval_T *argvars;
9218 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219{
9220 char_u cwd[MAXPATHL];
9221
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009222 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009224 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 else
9226 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009227 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230#endif
9231 }
9232}
9233
9234/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009235 * "getfontname()" function
9236 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009237/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009239f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009240 typval_T *argvars;
9241 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009242{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009243 rettv->v_type = VAR_STRING;
9244 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009245#ifdef FEAT_GUI
9246 if (gui.in_use)
9247 {
9248 GuiFont font;
9249 char_u *name = NULL;
9250
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009251 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009252 {
9253 /* Get the "Normal" font. Either the name saved by
9254 * hl_set_font_name() or from the font ID. */
9255 font = gui.norm_font;
9256 name = hl_get_font_name();
9257 }
9258 else
9259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009260 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009261 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9262 return;
9263 font = gui_mch_get_font(name, FALSE);
9264 if (font == NOFONT)
9265 return; /* Invalid font name, return empty string. */
9266 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009267 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009268 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009269 gui_mch_free_font(font);
9270 }
9271#endif
9272}
9273
9274/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009275 * "getfperm({fname})" function
9276 */
9277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009278f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009279 typval_T *argvars;
9280 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009281{
9282 char_u *fname;
9283 struct stat st;
9284 char_u *perm = NULL;
9285 char_u flags[] = "rwx";
9286 int i;
9287
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009288 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009289
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009290 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009291 if (mch_stat((char *)fname, &st) >= 0)
9292 {
9293 perm = vim_strsave((char_u *)"---------");
9294 if (perm != NULL)
9295 {
9296 for (i = 0; i < 9; i++)
9297 {
9298 if (st.st_mode & (1 << (8 - i)))
9299 perm[i] = flags[i % 3];
9300 }
9301 }
9302 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009304}
9305
9306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 * "getfsize({fname})" function
9308 */
9309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009310f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009311 typval_T *argvars;
9312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313{
9314 char_u *fname;
9315 struct stat st;
9316
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009317 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009319 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320
9321 if (mch_stat((char *)fname, &st) >= 0)
9322 {
9323 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009324 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 }
9328 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009329 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330}
9331
9332/*
9333 * "getftime({fname})" function
9334 */
9335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009336f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009337 typval_T *argvars;
9338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339{
9340 char_u *fname;
9341 struct stat st;
9342
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009343 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344
9345 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009346 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009348 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349}
9350
9351/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009352 * "getftype({fname})" function
9353 */
9354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009355f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009356 typval_T *argvars;
9357 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009358{
9359 char_u *fname;
9360 struct stat st;
9361 char_u *type = NULL;
9362 char *t;
9363
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009364 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009365
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009366 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009367 if (mch_lstat((char *)fname, &st) >= 0)
9368 {
9369#ifdef S_ISREG
9370 if (S_ISREG(st.st_mode))
9371 t = "file";
9372 else if (S_ISDIR(st.st_mode))
9373 t = "dir";
9374# ifdef S_ISLNK
9375 else if (S_ISLNK(st.st_mode))
9376 t = "link";
9377# endif
9378# ifdef S_ISBLK
9379 else if (S_ISBLK(st.st_mode))
9380 t = "bdev";
9381# endif
9382# ifdef S_ISCHR
9383 else if (S_ISCHR(st.st_mode))
9384 t = "cdev";
9385# endif
9386# ifdef S_ISFIFO
9387 else if (S_ISFIFO(st.st_mode))
9388 t = "fifo";
9389# endif
9390# ifdef S_ISSOCK
9391 else if (S_ISSOCK(st.st_mode))
9392 t = "fifo";
9393# endif
9394 else
9395 t = "other";
9396#else
9397# ifdef S_IFMT
9398 switch (st.st_mode & S_IFMT)
9399 {
9400 case S_IFREG: t = "file"; break;
9401 case S_IFDIR: t = "dir"; break;
9402# ifdef S_IFLNK
9403 case S_IFLNK: t = "link"; break;
9404# endif
9405# ifdef S_IFBLK
9406 case S_IFBLK: t = "bdev"; break;
9407# endif
9408# ifdef S_IFCHR
9409 case S_IFCHR: t = "cdev"; break;
9410# endif
9411# ifdef S_IFIFO
9412 case S_IFIFO: t = "fifo"; break;
9413# endif
9414# ifdef S_IFSOCK
9415 case S_IFSOCK: t = "socket"; break;
9416# endif
9417 default: t = "other";
9418 }
9419# else
9420 if (mch_isdir(fname))
9421 t = "dir";
9422 else
9423 t = "file";
9424# endif
9425#endif
9426 type = vim_strsave((char_u *)t);
9427 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009428 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009429}
9430
9431/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009432 * "getline(lnum)" function
9433 */
9434 static void
9435f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009436 typval_T *argvars;
9437 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009438{
9439 linenr_T lnum;
9440 linenr_T end;
9441 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00009442 list_T *l;
9443 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009444
9445 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009446 if (lnum < 0)
9447 rettv->vval.v_number = 0; /* failure; error message already given */
9448 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009449 {
9450 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9451 p = ml_get(lnum);
9452 else
9453 p = (char_u *)"";
9454
9455 rettv->v_type = VAR_STRING;
9456 rettv->vval.v_string = vim_strsave(p);
9457 }
9458 else
9459 {
9460 end = get_tv_lnum(&argvars[1]);
9461 if (end < lnum)
9462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009463 if (end >= 0) /* else: error message already given */
9464 EMSG(_(e_invrange));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009465 rettv->vval.v_number = 0;
9466 }
9467 else
9468 {
9469 l = list_alloc();
9470 if (l != NULL)
9471 {
9472 if (lnum < 1)
9473 lnum = 1;
9474 if (end > curbuf->b_ml.ml_line_count)
9475 end = curbuf->b_ml.ml_line_count;
9476 while (lnum <= end)
9477 {
9478 li = listitem_alloc();
9479 if (li == NULL)
9480 break;
9481 list_append(l, li);
9482 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009483 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009484 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
9485 }
9486 rettv->vval.v_list = l;
9487 rettv->v_type = VAR_LIST;
9488 ++l->lv_refcount;
9489 }
9490 }
9491 }
9492}
9493
9494/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009495 * "getqflist()" function
9496 */
9497/*ARGSUSED*/
9498 static void
9499f_getqflist(argvars, rettv)
9500 typval_T *argvars;
9501 typval_T *rettv;
9502{
9503#ifdef FEAT_QUICKFIX
9504 list_T *l;
9505#endif
9506
9507 rettv->vval.v_number = FALSE;
9508#ifdef FEAT_QUICKFIX
9509 l = list_alloc();
9510 if (l != NULL)
9511 {
9512 if (get_errorlist(l) != FAIL)
9513 {
9514 rettv->vval.v_list = l;
9515 rettv->v_type = VAR_LIST;
9516 ++l->lv_refcount;
9517 }
9518 else
9519 list_free(l);
9520 }
9521#endif
9522}
9523
9524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 * "getreg()" function
9526 */
9527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009528f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009529 typval_T *argvars;
9530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531{
9532 char_u *strregname;
9533 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009534 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009535 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009537 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009539 strregname = get_tv_string_chk(&argvars[0]);
9540 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009541 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009542 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009545 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 regname = (strregname == NULL ? '"' : *strregname);
9547 if (regname == 0)
9548 regname = '"';
9549
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009550 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009551 rettv->vval.v_string = error ? NULL :
9552 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553}
9554
9555/*
9556 * "getregtype()" function
9557 */
9558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009560 typval_T *argvars;
9561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562{
9563 char_u *strregname;
9564 int regname;
9565 char_u buf[NUMBUFLEN + 2];
9566 long reglen = 0;
9567
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009568 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009569 {
9570 strregname = get_tv_string_chk(&argvars[0]);
9571 if (strregname == NULL) /* type error; errmsg already given */
9572 {
9573 rettv->v_type = VAR_STRING;
9574 rettv->vval.v_string = NULL;
9575 return;
9576 }
9577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 else
9579 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009580 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581
9582 regname = (strregname == NULL ? '"' : *strregname);
9583 if (regname == 0)
9584 regname = '"';
9585
9586 buf[0] = NUL;
9587 buf[1] = NUL;
9588 switch (get_reg_type(regname, &reglen))
9589 {
9590 case MLINE: buf[0] = 'V'; break;
9591 case MCHAR: buf[0] = 'v'; break;
9592#ifdef FEAT_VISUAL
9593 case MBLOCK:
9594 buf[0] = Ctrl_V;
9595 sprintf((char *)buf + 1, "%ld", reglen + 1);
9596 break;
9597#endif
9598 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009599 rettv->v_type = VAR_STRING;
9600 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601}
9602
9603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 * "getwinposx()" function
9605 */
9606/*ARGSUSED*/
9607 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009608f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009609 typval_T *argvars;
9610 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009612 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613#ifdef FEAT_GUI
9614 if (gui.in_use)
9615 {
9616 int x, y;
9617
9618 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 }
9621#endif
9622}
9623
9624/*
9625 * "getwinposy()" function
9626 */
9627/*ARGSUSED*/
9628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629f_getwinposy(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009633 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634#ifdef FEAT_GUI
9635 if (gui.in_use)
9636 {
9637 int x, y;
9638
9639 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 }
9642#endif
9643}
9644
9645/*
9646 * "getwinvar()" function
9647 */
9648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009650 typval_T *argvars;
9651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652{
9653 win_T *win, *oldcurwin;
9654 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009655 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009658 varname = get_tv_string_chk(&argvars[1]);
9659 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 rettv->v_type = VAR_STRING;
9662 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663
9664 if (win != NULL && varname != NULL)
9665 {
9666 if (*varname == '&') /* window-local-option */
9667 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009668 /* Set curwin to be our win, temporarily. Also set curbuf, so
9669 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 oldcurwin = curwin;
9671 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009672 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675
9676 /* restore previous notion of curwin */
9677 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009678 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 }
9680 else
9681 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009682 if (*varname == NUL)
9683 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9684 * scope prefix before the NUL byte is required by
9685 * find_var_in_ht(). */
9686 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009688 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009690 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 }
9692 }
9693
9694 --emsg_off;
9695}
9696
9697/*
9698 * "glob()" function
9699 */
9700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009702 typval_T *argvars;
9703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704{
9705 expand_T xpc;
9706
9707 ExpandInit(&xpc);
9708 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709 rettv->v_type = VAR_STRING;
9710 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9712 ExpandCleanup(&xpc);
9713}
9714
9715/*
9716 * "globpath()" function
9717 */
9718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009720 typval_T *argvars;
9721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722{
9723 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009724 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009726 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009727 if (file == NULL)
9728 rettv->vval.v_string = NULL;
9729 else
9730 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731}
9732
9733/*
9734 * "has()" function
9735 */
9736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009738 typval_T *argvars;
9739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740{
9741 int i;
9742 char_u *name;
9743 int n = FALSE;
9744 static char *(has_list[]) =
9745 {
9746#ifdef AMIGA
9747 "amiga",
9748# ifdef FEAT_ARP
9749 "arp",
9750# endif
9751#endif
9752#ifdef __BEOS__
9753 "beos",
9754#endif
9755#ifdef MSDOS
9756# ifdef DJGPP
9757 "dos32",
9758# else
9759 "dos16",
9760# endif
9761#endif
9762#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9763 "mac",
9764#endif
9765#if defined(MACOS_X_UNIX)
9766 "macunix",
9767#endif
9768#ifdef OS2
9769 "os2",
9770#endif
9771#ifdef __QNX__
9772 "qnx",
9773#endif
9774#ifdef RISCOS
9775 "riscos",
9776#endif
9777#ifdef UNIX
9778 "unix",
9779#endif
9780#ifdef VMS
9781 "vms",
9782#endif
9783#ifdef WIN16
9784 "win16",
9785#endif
9786#ifdef WIN32
9787 "win32",
9788#endif
9789#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9790 "win32unix",
9791#endif
9792#ifdef WIN64
9793 "win64",
9794#endif
9795#ifdef EBCDIC
9796 "ebcdic",
9797#endif
9798#ifndef CASE_INSENSITIVE_FILENAME
9799 "fname_case",
9800#endif
9801#ifdef FEAT_ARABIC
9802 "arabic",
9803#endif
9804#ifdef FEAT_AUTOCMD
9805 "autocmd",
9806#endif
9807#ifdef FEAT_BEVAL
9808 "balloon_eval",
9809#endif
9810#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9811 "builtin_terms",
9812# ifdef ALL_BUILTIN_TCAPS
9813 "all_builtin_terms",
9814# endif
9815#endif
9816#ifdef FEAT_BYTEOFF
9817 "byte_offset",
9818#endif
9819#ifdef FEAT_CINDENT
9820 "cindent",
9821#endif
9822#ifdef FEAT_CLIENTSERVER
9823 "clientserver",
9824#endif
9825#ifdef FEAT_CLIPBOARD
9826 "clipboard",
9827#endif
9828#ifdef FEAT_CMDL_COMPL
9829 "cmdline_compl",
9830#endif
9831#ifdef FEAT_CMDHIST
9832 "cmdline_hist",
9833#endif
9834#ifdef FEAT_COMMENTS
9835 "comments",
9836#endif
9837#ifdef FEAT_CRYPT
9838 "cryptv",
9839#endif
9840#ifdef FEAT_CSCOPE
9841 "cscope",
9842#endif
9843#ifdef DEBUG
9844 "debug",
9845#endif
9846#ifdef FEAT_CON_DIALOG
9847 "dialog_con",
9848#endif
9849#ifdef FEAT_GUI_DIALOG
9850 "dialog_gui",
9851#endif
9852#ifdef FEAT_DIFF
9853 "diff",
9854#endif
9855#ifdef FEAT_DIGRAPHS
9856 "digraphs",
9857#endif
9858#ifdef FEAT_DND
9859 "dnd",
9860#endif
9861#ifdef FEAT_EMACS_TAGS
9862 "emacs_tags",
9863#endif
9864 "eval", /* always present, of course! */
9865#ifdef FEAT_EX_EXTRA
9866 "ex_extra",
9867#endif
9868#ifdef FEAT_SEARCH_EXTRA
9869 "extra_search",
9870#endif
9871#ifdef FEAT_FKMAP
9872 "farsi",
9873#endif
9874#ifdef FEAT_SEARCHPATH
9875 "file_in_path",
9876#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009877#if defined(UNIX) && !defined(USE_SYSTEM)
9878 "filterpipe",
9879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880#ifdef FEAT_FIND_ID
9881 "find_in_path",
9882#endif
9883#ifdef FEAT_FOLDING
9884 "folding",
9885#endif
9886#ifdef FEAT_FOOTER
9887 "footer",
9888#endif
9889#if !defined(USE_SYSTEM) && defined(UNIX)
9890 "fork",
9891#endif
9892#ifdef FEAT_GETTEXT
9893 "gettext",
9894#endif
9895#ifdef FEAT_GUI
9896 "gui",
9897#endif
9898#ifdef FEAT_GUI_ATHENA
9899# ifdef FEAT_GUI_NEXTAW
9900 "gui_neXtaw",
9901# else
9902 "gui_athena",
9903# endif
9904#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009905#ifdef FEAT_GUI_KDE
9906 "gui_kde",
9907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908#ifdef FEAT_GUI_GTK
9909 "gui_gtk",
9910# ifdef HAVE_GTK2
9911 "gui_gtk2",
9912# endif
9913#endif
9914#ifdef FEAT_GUI_MAC
9915 "gui_mac",
9916#endif
9917#ifdef FEAT_GUI_MOTIF
9918 "gui_motif",
9919#endif
9920#ifdef FEAT_GUI_PHOTON
9921 "gui_photon",
9922#endif
9923#ifdef FEAT_GUI_W16
9924 "gui_win16",
9925#endif
9926#ifdef FEAT_GUI_W32
9927 "gui_win32",
9928#endif
9929#ifdef FEAT_HANGULIN
9930 "hangul_input",
9931#endif
9932#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
9933 "iconv",
9934#endif
9935#ifdef FEAT_INS_EXPAND
9936 "insert_expand",
9937#endif
9938#ifdef FEAT_JUMPLIST
9939 "jumplist",
9940#endif
9941#ifdef FEAT_KEYMAP
9942 "keymap",
9943#endif
9944#ifdef FEAT_LANGMAP
9945 "langmap",
9946#endif
9947#ifdef FEAT_LIBCALL
9948 "libcall",
9949#endif
9950#ifdef FEAT_LINEBREAK
9951 "linebreak",
9952#endif
9953#ifdef FEAT_LISP
9954 "lispindent",
9955#endif
9956#ifdef FEAT_LISTCMDS
9957 "listcmds",
9958#endif
9959#ifdef FEAT_LOCALMAP
9960 "localmap",
9961#endif
9962#ifdef FEAT_MENU
9963 "menu",
9964#endif
9965#ifdef FEAT_SESSION
9966 "mksession",
9967#endif
9968#ifdef FEAT_MODIFY_FNAME
9969 "modify_fname",
9970#endif
9971#ifdef FEAT_MOUSE
9972 "mouse",
9973#endif
9974#ifdef FEAT_MOUSESHAPE
9975 "mouseshape",
9976#endif
9977#if defined(UNIX) || defined(VMS)
9978# ifdef FEAT_MOUSE_DEC
9979 "mouse_dec",
9980# endif
9981# ifdef FEAT_MOUSE_GPM
9982 "mouse_gpm",
9983# endif
9984# ifdef FEAT_MOUSE_JSB
9985 "mouse_jsbterm",
9986# endif
9987# ifdef FEAT_MOUSE_NET
9988 "mouse_netterm",
9989# endif
9990# ifdef FEAT_MOUSE_PTERM
9991 "mouse_pterm",
9992# endif
9993# ifdef FEAT_MOUSE_XTERM
9994 "mouse_xterm",
9995# endif
9996#endif
9997#ifdef FEAT_MBYTE
9998 "multi_byte",
9999#endif
10000#ifdef FEAT_MBYTE_IME
10001 "multi_byte_ime",
10002#endif
10003#ifdef FEAT_MULTI_LANG
10004 "multi_lang",
10005#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010006#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010007#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010008 "mzscheme",
10009#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011#ifdef FEAT_OLE
10012 "ole",
10013#endif
10014#ifdef FEAT_OSFILETYPE
10015 "osfiletype",
10016#endif
10017#ifdef FEAT_PATH_EXTRA
10018 "path_extra",
10019#endif
10020#ifdef FEAT_PERL
10021#ifndef DYNAMIC_PERL
10022 "perl",
10023#endif
10024#endif
10025#ifdef FEAT_PYTHON
10026#ifndef DYNAMIC_PYTHON
10027 "python",
10028#endif
10029#endif
10030#ifdef FEAT_POSTSCRIPT
10031 "postscript",
10032#endif
10033#ifdef FEAT_PRINTER
10034 "printer",
10035#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010036#ifdef FEAT_PROFILE
10037 "profile",
10038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039#ifdef FEAT_QUICKFIX
10040 "quickfix",
10041#endif
10042#ifdef FEAT_RIGHTLEFT
10043 "rightleft",
10044#endif
10045#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10046 "ruby",
10047#endif
10048#ifdef FEAT_SCROLLBIND
10049 "scrollbind",
10050#endif
10051#ifdef FEAT_CMDL_INFO
10052 "showcmd",
10053 "cmdline_info",
10054#endif
10055#ifdef FEAT_SIGNS
10056 "signs",
10057#endif
10058#ifdef FEAT_SMARTINDENT
10059 "smartindent",
10060#endif
10061#ifdef FEAT_SNIFF
10062 "sniff",
10063#endif
10064#ifdef FEAT_STL_OPT
10065 "statusline",
10066#endif
10067#ifdef FEAT_SUN_WORKSHOP
10068 "sun_workshop",
10069#endif
10070#ifdef FEAT_NETBEANS_INTG
10071 "netbeans_intg",
10072#endif
10073#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010074 "spell",
10075#endif
10076#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 "syntax",
10078#endif
10079#if defined(USE_SYSTEM) || !defined(UNIX)
10080 "system",
10081#endif
10082#ifdef FEAT_TAG_BINS
10083 "tag_binary",
10084#endif
10085#ifdef FEAT_TAG_OLDSTATIC
10086 "tag_old_static",
10087#endif
10088#ifdef FEAT_TAG_ANYWHITE
10089 "tag_any_white",
10090#endif
10091#ifdef FEAT_TCL
10092# ifndef DYNAMIC_TCL
10093 "tcl",
10094# endif
10095#endif
10096#ifdef TERMINFO
10097 "terminfo",
10098#endif
10099#ifdef FEAT_TERMRESPONSE
10100 "termresponse",
10101#endif
10102#ifdef FEAT_TEXTOBJ
10103 "textobjects",
10104#endif
10105#ifdef HAVE_TGETENT
10106 "tgetent",
10107#endif
10108#ifdef FEAT_TITLE
10109 "title",
10110#endif
10111#ifdef FEAT_TOOLBAR
10112 "toolbar",
10113#endif
10114#ifdef FEAT_USR_CMDS
10115 "user-commands", /* was accidentally included in 5.4 */
10116 "user_commands",
10117#endif
10118#ifdef FEAT_VIMINFO
10119 "viminfo",
10120#endif
10121#ifdef FEAT_VERTSPLIT
10122 "vertsplit",
10123#endif
10124#ifdef FEAT_VIRTUALEDIT
10125 "virtualedit",
10126#endif
10127#ifdef FEAT_VISUAL
10128 "visual",
10129#endif
10130#ifdef FEAT_VISUALEXTRA
10131 "visualextra",
10132#endif
10133#ifdef FEAT_VREPLACE
10134 "vreplace",
10135#endif
10136#ifdef FEAT_WILDIGN
10137 "wildignore",
10138#endif
10139#ifdef FEAT_WILDMENU
10140 "wildmenu",
10141#endif
10142#ifdef FEAT_WINDOWS
10143 "windows",
10144#endif
10145#ifdef FEAT_WAK
10146 "winaltkeys",
10147#endif
10148#ifdef FEAT_WRITEBACKUP
10149 "writebackup",
10150#endif
10151#ifdef FEAT_XIM
10152 "xim",
10153#endif
10154#ifdef FEAT_XFONTSET
10155 "xfontset",
10156#endif
10157#ifdef USE_XSMP
10158 "xsmp",
10159#endif
10160#ifdef USE_XSMP_INTERACT
10161 "xsmp_interact",
10162#endif
10163#ifdef FEAT_XCLIPBOARD
10164 "xterm_clipboard",
10165#endif
10166#ifdef FEAT_XTERM_SAVE
10167 "xterm_save",
10168#endif
10169#if defined(UNIX) && defined(FEAT_X11)
10170 "X11",
10171#endif
10172 NULL
10173 };
10174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010175 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 for (i = 0; has_list[i] != NULL; ++i)
10177 if (STRICMP(name, has_list[i]) == 0)
10178 {
10179 n = TRUE;
10180 break;
10181 }
10182
10183 if (n == FALSE)
10184 {
10185 if (STRNICMP(name, "patch", 5) == 0)
10186 n = has_patch(atoi((char *)name + 5));
10187 else if (STRICMP(name, "vim_starting") == 0)
10188 n = (starting != 0);
10189#ifdef DYNAMIC_TCL
10190 else if (STRICMP(name, "tcl") == 0)
10191 n = tcl_enabled(FALSE);
10192#endif
10193#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10194 else if (STRICMP(name, "iconv") == 0)
10195 n = iconv_enabled(FALSE);
10196#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010197#ifdef DYNAMIC_MZSCHEME
10198 else if (STRICMP(name, "mzscheme") == 0)
10199 n = mzscheme_enabled(FALSE);
10200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201#ifdef DYNAMIC_RUBY
10202 else if (STRICMP(name, "ruby") == 0)
10203 n = ruby_enabled(FALSE);
10204#endif
10205#ifdef DYNAMIC_PYTHON
10206 else if (STRICMP(name, "python") == 0)
10207 n = python_enabled(FALSE);
10208#endif
10209#ifdef DYNAMIC_PERL
10210 else if (STRICMP(name, "perl") == 0)
10211 n = perl_enabled(FALSE);
10212#endif
10213#ifdef FEAT_GUI
10214 else if (STRICMP(name, "gui_running") == 0)
10215 n = (gui.in_use || gui.starting);
10216# ifdef FEAT_GUI_W32
10217 else if (STRICMP(name, "gui_win32s") == 0)
10218 n = gui_is_win32s();
10219# endif
10220# ifdef FEAT_BROWSE
10221 else if (STRICMP(name, "browse") == 0)
10222 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10223# endif
10224#endif
10225#ifdef FEAT_SYN_HL
10226 else if (STRICMP(name, "syntax_items") == 0)
10227 n = syntax_present(curbuf);
10228#endif
10229#if defined(WIN3264)
10230 else if (STRICMP(name, "win95") == 0)
10231 n = mch_windows95();
10232#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010233#ifdef FEAT_NETBEANS_INTG
10234 else if (STRICMP(name, "netbeans_enabled") == 0)
10235 n = usingNetbeans;
10236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 }
10238
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010239 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240}
10241
10242/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010243 * "has_key()" function
10244 */
10245 static void
10246f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010247 typval_T *argvars;
10248 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010249{
10250 rettv->vval.v_number = 0;
10251 if (argvars[0].v_type != VAR_DICT)
10252 {
10253 EMSG(_(e_dictreq));
10254 return;
10255 }
10256 if (argvars[0].vval.v_dict == NULL)
10257 return;
10258
10259 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010260 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010261}
10262
10263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 * "hasmapto()" function
10265 */
10266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010267f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010268 typval_T *argvars;
10269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270{
10271 char_u *name;
10272 char_u *mode;
10273 char_u buf[NUMBUFLEN];
10274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010275 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010276 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 mode = (char_u *)"nvo";
10278 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010279 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280
10281 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010282 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010284 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285}
10286
10287/*
10288 * "histadd()" function
10289 */
10290/*ARGSUSED*/
10291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010292f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010293 typval_T *argvars;
10294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295{
10296#ifdef FEAT_CMDHIST
10297 int histype;
10298 char_u *str;
10299 char_u buf[NUMBUFLEN];
10300#endif
10301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010302 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 if (check_restricted() || check_secure())
10304 return;
10305#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010306 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10307 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 if (histype >= 0)
10309 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010310 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 if (*str != NUL)
10312 {
10313 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010314 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 return;
10316 }
10317 }
10318#endif
10319}
10320
10321/*
10322 * "histdel()" function
10323 */
10324/*ARGSUSED*/
10325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010326f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010327 typval_T *argvars;
10328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329{
10330#ifdef FEAT_CMDHIST
10331 int n;
10332 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010333 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010335 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10336 if (str == NULL)
10337 n = 0;
10338 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010340 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010341 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010343 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010344 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 else
10346 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010347 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010348 get_tv_string_buf(&argvars[1], buf));
10349 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010351 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352#endif
10353}
10354
10355/*
10356 * "histget()" function
10357 */
10358/*ARGSUSED*/
10359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010360f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010361 typval_T *argvars;
10362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363{
10364#ifdef FEAT_CMDHIST
10365 int type;
10366 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010367 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010369 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10370 if (str == NULL)
10371 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010373 {
10374 type = get_histtype(str);
10375 if (argvars[1].v_type == VAR_UNKNOWN)
10376 idx = get_history_idx(type);
10377 else
10378 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10379 /* -1 on type error */
10380 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010383 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010385 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386}
10387
10388/*
10389 * "histnr()" function
10390 */
10391/*ARGSUSED*/
10392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010393f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010394 typval_T *argvars;
10395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396{
10397 int i;
10398
10399#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010400 char_u *history = get_tv_string_chk(&argvars[0]);
10401
10402 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 if (i >= HIST_CMD && i < HIST_COUNT)
10404 i = get_history_idx(i);
10405 else
10406#endif
10407 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010408 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409}
10410
10411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412 * "highlightID(name)" function
10413 */
10414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010415f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010416 typval_T *argvars;
10417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010419 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420}
10421
10422/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010423 * "highlight_exists()" function
10424 */
10425 static void
10426f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010427 typval_T *argvars;
10428 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010429{
10430 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10431}
10432
10433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 * "hostname()" function
10435 */
10436/*ARGSUSED*/
10437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010438f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010439 typval_T *argvars;
10440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441{
10442 char_u hostname[256];
10443
10444 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010445 rettv->v_type = VAR_STRING;
10446 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447}
10448
10449/*
10450 * iconv() function
10451 */
10452/*ARGSUSED*/
10453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010454f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010455 typval_T *argvars;
10456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457{
10458#ifdef FEAT_MBYTE
10459 char_u buf1[NUMBUFLEN];
10460 char_u buf2[NUMBUFLEN];
10461 char_u *from, *to, *str;
10462 vimconv_T vimconv;
10463#endif
10464
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010465 rettv->v_type = VAR_STRING;
10466 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467
10468#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010469 str = get_tv_string(&argvars[0]);
10470 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10471 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 vimconv.vc_type = CONV_NONE;
10473 convert_setup(&vimconv, from, to);
10474
10475 /* If the encodings are equal, no conversion needed. */
10476 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010477 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010479 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480
10481 convert_setup(&vimconv, NULL, NULL);
10482 vim_free(from);
10483 vim_free(to);
10484#endif
10485}
10486
10487/*
10488 * "indent()" function
10489 */
10490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010491f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010492 typval_T *argvars;
10493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494{
10495 linenr_T lnum;
10496
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010497 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010499 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010501 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502}
10503
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010504/*
10505 * "index()" function
10506 */
10507 static void
10508f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010509 typval_T *argvars;
10510 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010511{
Bram Moolenaar33570922005-01-25 22:26:29 +000010512 list_T *l;
10513 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010514 long idx = 0;
10515 int ic = FALSE;
10516
10517 rettv->vval.v_number = -1;
10518 if (argvars[0].v_type != VAR_LIST)
10519 {
10520 EMSG(_(e_listreq));
10521 return;
10522 }
10523 l = argvars[0].vval.v_list;
10524 if (l != NULL)
10525 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010526 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010527 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010528 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010529 int error = FALSE;
10530
Bram Moolenaar758711c2005-02-02 23:11:38 +000010531 /* Start at specified item. Use the cached index that list_find()
10532 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010533 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010534 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010535 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 ic = get_tv_number_chk(&argvars[3], &error);
10537 if (error)
10538 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010539 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010540
Bram Moolenaar758711c2005-02-02 23:11:38 +000010541 for ( ; item != NULL; item = item->li_next, ++idx)
10542 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010543 {
10544 rettv->vval.v_number = idx;
10545 break;
10546 }
10547 }
10548}
10549
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550static int inputsecret_flag = 0;
10551
10552/*
10553 * "input()" function
10554 * Also handles inputsecret() when inputsecret is set.
10555 */
10556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010557f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010558 typval_T *argvars;
10559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010561 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 char_u *p = NULL;
10563 int c;
10564 char_u buf[NUMBUFLEN];
10565 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010566 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010568 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569
10570#ifdef NO_CONSOLE_INPUT
10571 /* While starting up, there is no place to enter text. */
10572 if (no_console_input())
10573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010574 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 return;
10576 }
10577#endif
10578
10579 cmd_silent = FALSE; /* Want to see the prompt. */
10580 if (prompt != NULL)
10581 {
10582 /* Only the part of the message after the last NL is considered as
10583 * prompt for the command line */
10584 p = vim_strrchr(prompt, '\n');
10585 if (p == NULL)
10586 p = prompt;
10587 else
10588 {
10589 ++p;
10590 c = *p;
10591 *p = NUL;
10592 msg_start();
10593 msg_clr_eos();
10594 msg_puts_attr(prompt, echo_attr);
10595 msg_didout = FALSE;
10596 msg_starthere();
10597 *p = c;
10598 }
10599 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010601 if (argvars[1].v_type != VAR_UNKNOWN)
10602 {
10603 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10604 if (defstr != NULL)
10605 stuffReadbuffSpec(defstr);
10606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010608 if (defstr != NULL)
10609 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 /* since the user typed this, no need to wait for return */
10613 need_wait_return = FALSE;
10614 msg_didout = FALSE;
10615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 cmd_silent = cmd_silent_save;
10617}
10618
10619/*
10620 * "inputdialog()" function
10621 */
10622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010623f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010624 typval_T *argvars;
10625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626{
10627#if defined(FEAT_GUI_TEXTDIALOG)
10628 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10629 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10630 {
10631 char_u *message;
10632 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010633 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010635 message = get_tv_string_chk(&argvars[0]);
10636 if (argvars[1].v_type != VAR_UNKNOWN
10637 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010639 STRNCPY(IObuff, defstr, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 IObuff[IOSIZE - 1] = NUL;
10641 }
10642 else
10643 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010644 if (message != NULL && defstr != NULL
10645 && do_dialog(VIM_QUESTION, NULL, message,
10646 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010647 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 else
10649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010650 if (message != NULL && defstr != NULL
10651 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010652 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010653 rettv->vval.v_string = vim_strsave(
10654 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010658 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 }
10660 else
10661#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010662 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663}
10664
10665static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10666
10667/*
10668 * "inputrestore()" function
10669 */
10670/*ARGSUSED*/
10671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010672f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010673 typval_T *argvars;
10674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675{
10676 if (ga_userinput.ga_len > 0)
10677 {
10678 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10680 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 }
10683 else if (p_verbose > 1)
10684 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010685 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010686 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 }
10688}
10689
10690/*
10691 * "inputsave()" function
10692 */
10693/*ARGSUSED*/
10694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695f_inputsave(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{
10699 /* Add an entry to the stack of typehead storage. */
10700 if (ga_grow(&ga_userinput, 1) == OK)
10701 {
10702 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10703 + ga_userinput.ga_len);
10704 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010705 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 }
10707 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010708 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709}
10710
10711/*
10712 * "inputsecret()" function
10713 */
10714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010715f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010716 typval_T *argvars;
10717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718{
10719 ++cmdline_star;
10720 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010721 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 --cmdline_star;
10723 --inputsecret_flag;
10724}
10725
10726/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010727 * "insert()" function
10728 */
10729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010730f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010731 typval_T *argvars;
10732 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010733{
10734 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010735 listitem_T *item;
10736 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010737 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010738
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010739 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010740 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010741 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010742 else if ((l = argvars[0].vval.v_list) != NULL
10743 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010744 {
10745 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010746 before = get_tv_number_chk(&argvars[2], &error);
10747 if (error)
10748 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010749
Bram Moolenaar758711c2005-02-02 23:11:38 +000010750 if (before == l->lv_len)
10751 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010752 else
10753 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010754 item = list_find(l, before);
10755 if (item == NULL)
10756 {
10757 EMSGN(_(e_listidx), before);
10758 l = NULL;
10759 }
10760 }
10761 if (l != NULL)
10762 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010763 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010764 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010765 }
10766 }
10767}
10768
10769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 * "isdirectory()" function
10771 */
10772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010773f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010774 typval_T *argvars;
10775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778}
10779
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010780/*
10781 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
10782 * it refers to a List or Dictionary that is locked.
10783 */
10784 static int
10785tv_islocked(tv)
10786 typval_T *tv;
10787{
10788 return (tv->v_lock & VAR_LOCKED)
10789 || (tv->v_type == VAR_LIST
10790 && tv->vval.v_list != NULL
10791 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
10792 || (tv->v_type == VAR_DICT
10793 && tv->vval.v_dict != NULL
10794 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
10795}
10796
10797/*
10798 * "islocked()" function
10799 */
10800 static void
10801f_islocked(argvars, rettv)
10802 typval_T *argvars;
10803 typval_T *rettv;
10804{
10805 lval_T lv;
10806 char_u *end;
10807 dictitem_T *di;
10808
10809 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010810 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10811 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010812 if (end != NULL && lv.ll_name != NULL)
10813 {
10814 if (*end != NUL)
10815 EMSG(_(e_trailing));
10816 else
10817 {
10818 if (lv.ll_tv == NULL)
10819 {
10820 if (check_changedtick(lv.ll_name))
10821 rettv->vval.v_number = 1; /* always locked */
10822 else
10823 {
10824 di = find_var(lv.ll_name, NULL);
10825 if (di != NULL)
10826 {
10827 /* Consider a variable locked when:
10828 * 1. the variable itself is locked
10829 * 2. the value of the variable is locked.
10830 * 3. the List or Dict value is locked.
10831 */
10832 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10833 || tv_islocked(&di->di_tv));
10834 }
10835 }
10836 }
10837 else if (lv.ll_range)
10838 EMSG(_("E745: Range not allowed"));
10839 else if (lv.ll_newkey != NULL)
10840 EMSG2(_(e_dictkey), lv.ll_newkey);
10841 else if (lv.ll_list != NULL)
10842 /* List item. */
10843 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10844 else
10845 /* Dictionary item. */
10846 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10847 }
10848 }
10849
10850 clear_lval(&lv);
10851}
10852
Bram Moolenaar33570922005-01-25 22:26:29 +000010853static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010854
10855/*
10856 * Turn a dict into a list:
10857 * "what" == 0: list of keys
10858 * "what" == 1: list of values
10859 * "what" == 2: list of items
10860 */
10861 static void
10862dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010863 typval_T *argvars;
10864 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010865 int what;
10866{
Bram Moolenaar33570922005-01-25 22:26:29 +000010867 list_T *l;
10868 list_T *l2;
10869 dictitem_T *di;
10870 hashitem_T *hi;
10871 listitem_T *li;
10872 listitem_T *li2;
10873 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010874 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010875
10876 rettv->vval.v_number = 0;
10877 if (argvars[0].v_type != VAR_DICT)
10878 {
10879 EMSG(_(e_dictreq));
10880 return;
10881 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010882 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010883 return;
10884
10885 l = list_alloc();
10886 if (l == NULL)
10887 return;
10888 rettv->v_type = VAR_LIST;
10889 rettv->vval.v_list = l;
10890 ++l->lv_refcount;
10891
Bram Moolenaar33570922005-01-25 22:26:29 +000010892 todo = d->dv_hashtab.ht_used;
10893 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010894 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010895 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010896 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010897 --todo;
10898 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010899
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010900 li = listitem_alloc();
10901 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010902 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010903 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010904
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010905 if (what == 0)
10906 {
10907 /* keys() */
10908 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010909 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010910 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10911 }
10912 else if (what == 1)
10913 {
10914 /* values() */
10915 copy_tv(&di->di_tv, &li->li_tv);
10916 }
10917 else
10918 {
10919 /* items() */
10920 l2 = list_alloc();
10921 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010922 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010923 li->li_tv.vval.v_list = l2;
10924 if (l2 == NULL)
10925 break;
10926 ++l2->lv_refcount;
10927
10928 li2 = listitem_alloc();
10929 if (li2 == NULL)
10930 break;
10931 list_append(l2, li2);
10932 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010933 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010934 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
10935
10936 li2 = listitem_alloc();
10937 if (li2 == NULL)
10938 break;
10939 list_append(l2, li2);
10940 copy_tv(&di->di_tv, &li2->li_tv);
10941 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010942 }
10943 }
10944}
10945
10946/*
10947 * "items(dict)" function
10948 */
10949 static void
10950f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010951 typval_T *argvars;
10952 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010953{
10954 dict_list(argvars, rettv, 2);
10955}
10956
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010958 * "join()" function
10959 */
10960 static void
10961f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010962 typval_T *argvars;
10963 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010964{
10965 garray_T ga;
10966 char_u *sep;
10967
10968 rettv->vval.v_number = 0;
10969 if (argvars[0].v_type != VAR_LIST)
10970 {
10971 EMSG(_(e_listreq));
10972 return;
10973 }
10974 if (argvars[0].vval.v_list == NULL)
10975 return;
10976 if (argvars[1].v_type == VAR_UNKNOWN)
10977 sep = (char_u *)" ";
10978 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010979 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010980
10981 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010982
10983 if (sep != NULL)
10984 {
10985 ga_init2(&ga, (int)sizeof(char), 80);
10986 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
10987 ga_append(&ga, NUL);
10988 rettv->vval.v_string = (char_u *)ga.ga_data;
10989 }
10990 else
10991 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010992}
10993
10994/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010995 * "keys()" function
10996 */
10997 static void
10998f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010999 typval_T *argvars;
11000 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011001{
11002 dict_list(argvars, rettv, 0);
11003}
11004
11005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 * "last_buffer_nr()" function.
11007 */
11008/*ARGSUSED*/
11009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011011 typval_T *argvars;
11012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013{
11014 int n = 0;
11015 buf_T *buf;
11016
11017 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11018 if (n < buf->b_fnum)
11019 n = buf->b_fnum;
11020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011021 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011022}
11023
11024/*
11025 * "len()" function
11026 */
11027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011028f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011029 typval_T *argvars;
11030 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011031{
11032 switch (argvars[0].v_type)
11033 {
11034 case VAR_STRING:
11035 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 rettv->vval.v_number = (varnumber_T)STRLEN(
11037 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011038 break;
11039 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011041 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011042 case VAR_DICT:
11043 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11044 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011045 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011046 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011047 break;
11048 }
11049}
11050
Bram Moolenaar33570922005-01-25 22:26:29 +000011051static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011052
11053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011055 typval_T *argvars;
11056 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011057 int type;
11058{
11059#ifdef FEAT_LIBCALL
11060 char_u *string_in;
11061 char_u **string_result;
11062 int nr_result;
11063#endif
11064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011066 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011067 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011068 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011069 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011070
11071 if (check_restricted() || check_secure())
11072 return;
11073
11074#ifdef FEAT_LIBCALL
11075 /* The first two args must be strings, otherwise its meaningless */
11076 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11077 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011078 string_in = NULL;
11079 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011080 string_in = argvars[2].vval.v_string;
11081 if (type == VAR_NUMBER)
11082 string_result = NULL;
11083 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011084 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011085 if (mch_libcall(argvars[0].vval.v_string,
11086 argvars[1].vval.v_string,
11087 string_in,
11088 argvars[2].vval.v_number,
11089 string_result,
11090 &nr_result) == OK
11091 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011092 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011093 }
11094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095}
11096
11097/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011098 * "libcall()" function
11099 */
11100 static void
11101f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011102 typval_T *argvars;
11103 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011104{
11105 libcall_common(argvars, rettv, VAR_STRING);
11106}
11107
11108/*
11109 * "libcallnr()" function
11110 */
11111 static void
11112f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011113 typval_T *argvars;
11114 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011115{
11116 libcall_common(argvars, rettv, VAR_NUMBER);
11117}
11118
11119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 * "line(string)" function
11121 */
11122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011123f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011124 typval_T *argvars;
11125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126{
11127 linenr_T lnum = 0;
11128 pos_T *fp;
11129
11130 fp = var2fpos(&argvars[0], TRUE);
11131 if (fp != NULL)
11132 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134}
11135
11136/*
11137 * "line2byte(lnum)" function
11138 */
11139/*ARGSUSED*/
11140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011141f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011142 typval_T *argvars;
11143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144{
11145#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011146 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147#else
11148 linenr_T lnum;
11149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011150 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011154 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11155 if (rettv->vval.v_number >= 0)
11156 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157#endif
11158}
11159
11160/*
11161 * "lispindent(lnum)" function
11162 */
11163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011165 typval_T *argvars;
11166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167{
11168#ifdef FEAT_LISP
11169 pos_T pos;
11170 linenr_T lnum;
11171
11172 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011173 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11175 {
11176 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011177 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 curwin->w_cursor = pos;
11179 }
11180 else
11181#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011182 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183}
11184
11185/*
11186 * "localtime()" function
11187 */
11188/*ARGSUSED*/
11189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011190f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011191 typval_T *argvars;
11192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011194 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195}
11196
Bram Moolenaar33570922005-01-25 22:26:29 +000011197static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198
11199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011200get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011201 typval_T *argvars;
11202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 int exact;
11204{
11205 char_u *keys;
11206 char_u *which;
11207 char_u buf[NUMBUFLEN];
11208 char_u *keys_buf = NULL;
11209 char_u *rhs;
11210 int mode;
11211 garray_T ga;
11212
11213 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011214 rettv->v_type = VAR_STRING;
11215 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011217 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218 if (*keys == NUL)
11219 return;
11220
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011221 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011222 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223 else
11224 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011225 if (which == NULL)
11226 return;
11227
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 mode = get_map_mode(&which, 0);
11229
11230 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011231 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232 vim_free(keys_buf);
11233 if (rhs != NULL)
11234 {
11235 ga_init(&ga);
11236 ga.ga_itemsize = 1;
11237 ga.ga_growsize = 40;
11238
11239 while (*rhs != NUL)
11240 ga_concat(&ga, str2special(&rhs, FALSE));
11241
11242 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011243 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 }
11245}
11246
11247/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011248 * "map()" function
11249 */
11250 static void
11251f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011252 typval_T *argvars;
11253 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011254{
11255 filter_map(argvars, rettv, TRUE);
11256}
11257
11258/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011259 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 */
11261 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011262f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011263 typval_T *argvars;
11264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011266 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267}
11268
11269/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011270 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 */
11272 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011273f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011274 typval_T *argvars;
11275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011277 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278}
11279
Bram Moolenaar33570922005-01-25 22:26:29 +000011280static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281
11282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011284 typval_T *argvars;
11285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 int type;
11287{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011288 char_u *str = NULL;
11289 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 char_u *pat;
11291 regmatch_T regmatch;
11292 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011293 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 char_u *save_cpo;
11295 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011296 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011297 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011298 list_T *l = NULL;
11299 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011300 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011301 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302
11303 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11304 save_cpo = p_cpo;
11305 p_cpo = (char_u *)"";
11306
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011307 rettv->vval.v_number = -1;
11308 if (type == 3)
11309 {
11310 /* return empty list when there are no matches */
11311 if ((rettv->vval.v_list = list_alloc()) == NULL)
11312 goto theend;
11313 rettv->v_type = VAR_LIST;
11314 ++rettv->vval.v_list->lv_refcount;
11315 }
11316 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318 rettv->v_type = VAR_STRING;
11319 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011322 if (argvars[0].v_type == VAR_LIST)
11323 {
11324 if ((l = argvars[0].vval.v_list) == NULL)
11325 goto theend;
11326 li = l->lv_first;
11327 }
11328 else
11329 expr = str = get_tv_string(&argvars[0]);
11330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011331 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11332 if (pat == NULL)
11333 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011334
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011335 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011337 int error = FALSE;
11338
11339 start = get_tv_number_chk(&argvars[2], &error);
11340 if (error)
11341 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011342 if (l != NULL)
11343 {
11344 li = list_find(l, start);
11345 if (li == NULL)
11346 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011347 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011348 }
11349 else
11350 {
11351 if (start < 0)
11352 start = 0;
11353 if (start > (long)STRLEN(str))
11354 goto theend;
11355 str += start;
11356 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011357
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011358 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359 nth = get_tv_number_chk(&argvars[3], &error);
11360 if (error)
11361 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362 }
11363
11364 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11365 if (regmatch.regprog != NULL)
11366 {
11367 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011368
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011369 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011370 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011371 if (l != NULL)
11372 {
11373 if (li == NULL)
11374 {
11375 match = FALSE;
11376 break;
11377 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011378 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011379 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011380 if (str == NULL)
11381 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011382 }
11383
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011384 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011385
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011386 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011387 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011388 if (l == NULL && !match)
11389 break;
11390
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011391 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011392 if (l != NULL)
11393 {
11394 li = li->li_next;
11395 ++idx;
11396 }
11397 else
11398 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011399#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011400 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011401#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011402 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011403#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011404 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011405 }
11406
11407 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011409 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011410 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011411 int i;
11412
11413 /* return list with matched string and submatches */
11414 for (i = 0; i < NSUBEXP; ++i)
11415 {
11416 if (regmatch.endp[i] == NULL)
11417 break;
11418 li = listitem_alloc();
11419 if (li == NULL)
11420 break;
11421 li->li_tv.v_type = VAR_STRING;
11422 li->li_tv.v_lock = 0;
11423 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11424 (int)(regmatch.endp[i] - regmatch.startp[i]));
11425 list_append(rettv->vval.v_list, li);
11426 }
11427 }
11428 else if (type == 2)
11429 {
11430 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011431 if (l != NULL)
11432 copy_tv(&li->li_tv, rettv);
11433 else
11434 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011436 }
11437 else if (l != NULL)
11438 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 else
11440 {
11441 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011442 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 (varnumber_T)(regmatch.startp[0] - str);
11444 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011447 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448 }
11449 }
11450 vim_free(regmatch.regprog);
11451 }
11452
11453theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011454 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455 p_cpo = save_cpo;
11456}
11457
11458/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011459 * "match()" function
11460 */
11461 static void
11462f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011463 typval_T *argvars;
11464 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011465{
11466 find_some_match(argvars, rettv, 1);
11467}
11468
11469/*
11470 * "matchend()" function
11471 */
11472 static void
11473f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011474 typval_T *argvars;
11475 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011476{
11477 find_some_match(argvars, rettv, 0);
11478}
11479
11480/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011481 * "matchlist()" function
11482 */
11483 static void
11484f_matchlist(argvars, rettv)
11485 typval_T *argvars;
11486 typval_T *rettv;
11487{
11488 find_some_match(argvars, rettv, 3);
11489}
11490
11491/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011492 * "matchstr()" function
11493 */
11494 static void
11495f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011496 typval_T *argvars;
11497 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011498{
11499 find_some_match(argvars, rettv, 2);
11500}
11501
Bram Moolenaar33570922005-01-25 22:26:29 +000011502static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011503
11504 static void
11505max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011506 typval_T *argvars;
11507 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011508 int domax;
11509{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011510 long n = 0;
11511 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011512 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011513
11514 if (argvars[0].v_type == VAR_LIST)
11515 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011516 list_T *l;
11517 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011518
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011519 l = argvars[0].vval.v_list;
11520 if (l != NULL)
11521 {
11522 li = l->lv_first;
11523 if (li != NULL)
11524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011525 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011526 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011527 {
11528 li = li->li_next;
11529 if (li == NULL)
11530 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011531 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011532 if (domax ? i > n : i < n)
11533 n = i;
11534 }
11535 }
11536 }
11537 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011538 else if (argvars[0].v_type == VAR_DICT)
11539 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011540 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011541 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011542 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011543 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011544
11545 d = argvars[0].vval.v_dict;
11546 if (d != NULL)
11547 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011548 todo = d->dv_hashtab.ht_used;
11549 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011550 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011551 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011552 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011553 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011554 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011555 if (first)
11556 {
11557 n = i;
11558 first = FALSE;
11559 }
11560 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011561 n = i;
11562 }
11563 }
11564 }
11565 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011566 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011567 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011568 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011569}
11570
11571/*
11572 * "max()" function
11573 */
11574 static void
11575f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011576 typval_T *argvars;
11577 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011578{
11579 max_min(argvars, rettv, TRUE);
11580}
11581
11582/*
11583 * "min()" function
11584 */
11585 static void
11586f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011587 typval_T *argvars;
11588 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011589{
11590 max_min(argvars, rettv, FALSE);
11591}
11592
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011593static int mkdir_recurse __ARGS((char_u *dir, int prot));
11594
11595/*
11596 * Create the directory in which "dir" is located, and higher levels when
11597 * needed.
11598 */
11599 static int
11600mkdir_recurse(dir, prot)
11601 char_u *dir;
11602 int prot;
11603{
11604 char_u *p;
11605 char_u *updir;
11606 int r = FAIL;
11607
11608 /* Get end of directory name in "dir".
11609 * We're done when it's "/" or "c:/". */
11610 p = gettail_sep(dir);
11611 if (p <= get_past_head(dir))
11612 return OK;
11613
11614 /* If the directory exists we're done. Otherwise: create it.*/
11615 updir = vim_strnsave(dir, (int)(p - dir));
11616 if (updir == NULL)
11617 return FAIL;
11618 if (mch_isdir(updir))
11619 r = OK;
11620 else if (mkdir_recurse(updir, prot) == OK)
11621 r = vim_mkdir_emsg(updir, prot);
11622 vim_free(updir);
11623 return r;
11624}
11625
11626#ifdef vim_mkdir
11627/*
11628 * "mkdir()" function
11629 */
11630 static void
11631f_mkdir(argvars, rettv)
11632 typval_T *argvars;
11633 typval_T *rettv;
11634{
11635 char_u *dir;
11636 char_u buf[NUMBUFLEN];
11637 int prot = 0755;
11638
11639 rettv->vval.v_number = FAIL;
11640 if (check_restricted() || check_secure())
11641 return;
11642
11643 dir = get_tv_string_buf(&argvars[0], buf);
11644 if (argvars[1].v_type != VAR_UNKNOWN)
11645 {
11646 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011647 prot = get_tv_number_chk(&argvars[2], NULL);
11648 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011649 mkdir_recurse(dir, prot);
11650 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011651 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011652}
11653#endif
11654
Bram Moolenaar0d660222005-01-07 21:51:51 +000011655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656 * "mode()" function
11657 */
11658/*ARGSUSED*/
11659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011660f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011661 typval_T *argvars;
11662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663{
11664 char_u buf[2];
11665
11666#ifdef FEAT_VISUAL
11667 if (VIsual_active)
11668 {
11669 if (VIsual_select)
11670 buf[0] = VIsual_mode + 's' - 'v';
11671 else
11672 buf[0] = VIsual_mode;
11673 }
11674 else
11675#endif
11676 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11677 buf[0] = 'r';
11678 else if (State & INSERT)
11679 {
11680 if (State & REPLACE_FLAG)
11681 buf[0] = 'R';
11682 else
11683 buf[0] = 'i';
11684 }
11685 else if (State & CMDLINE)
11686 buf[0] = 'c';
11687 else
11688 buf[0] = 'n';
11689
11690 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011691 rettv->vval.v_string = vim_strsave(buf);
11692 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693}
11694
11695/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011696 * "nextnonblank()" function
11697 */
11698 static void
11699f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011700 typval_T *argvars;
11701 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011702{
11703 linenr_T lnum;
11704
11705 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011707 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011708 {
11709 lnum = 0;
11710 break;
11711 }
11712 if (*skipwhite(ml_get(lnum)) != NUL)
11713 break;
11714 }
11715 rettv->vval.v_number = lnum;
11716}
11717
11718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 * "nr2char()" function
11720 */
11721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011722f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011723 typval_T *argvars;
11724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725{
11726 char_u buf[NUMBUFLEN];
11727
11728#ifdef FEAT_MBYTE
11729 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 else
11732#endif
11733 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011734 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 buf[1] = NUL;
11736 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011737 rettv->v_type = VAR_STRING;
11738 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011739}
11740
11741/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011742 * "prevnonblank()" function
11743 */
11744 static void
11745f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011746 typval_T *argvars;
11747 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011748{
11749 linenr_T lnum;
11750
11751 lnum = get_tv_lnum(argvars);
11752 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11753 lnum = 0;
11754 else
11755 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11756 --lnum;
11757 rettv->vval.v_number = lnum;
11758}
11759
Bram Moolenaar8c711452005-01-14 21:53:12 +000011760/*
11761 * "range()" function
11762 */
11763 static void
11764f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011765 typval_T *argvars;
11766 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011767{
11768 long start;
11769 long end;
11770 long stride = 1;
11771 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011772 list_T *l;
11773 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011774 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011776 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011777 if (argvars[1].v_type == VAR_UNKNOWN)
11778 {
11779 end = start - 1;
11780 start = 0;
11781 }
11782 else
11783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011784 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011785 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011786 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011787 }
11788
11789 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011790 if (error)
11791 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000011792 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011793 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000011794 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011795 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011796 else
11797 {
11798 l = list_alloc();
11799 if (l != NULL)
11800 {
11801 rettv->v_type = VAR_LIST;
11802 rettv->vval.v_list = l;
11803 ++l->lv_refcount;
11804
11805 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11806 {
11807 li = listitem_alloc();
11808 if (li == NULL)
11809 break;
11810 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011811 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011812 li->li_tv.vval.v_number = i;
11813 list_append(l, li);
11814 }
11815 }
11816 }
11817}
11818
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011819/*
11820 * "readfile()" function
11821 */
11822 static void
11823f_readfile(argvars, rettv)
11824 typval_T *argvars;
11825 typval_T *rettv;
11826{
11827 int binary = FALSE;
11828 char_u *fname;
11829 FILE *fd;
11830 list_T *l;
11831 listitem_T *li;
11832#define FREAD_SIZE 200 /* optimized for text lines */
11833 char_u buf[FREAD_SIZE];
11834 int readlen; /* size of last fread() */
11835 int buflen; /* nr of valid chars in buf[] */
11836 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11837 int tolist; /* first byte in buf[] still to be put in list */
11838 int chop; /* how many CR to chop off */
11839 char_u *prev = NULL; /* previously read bytes, if any */
11840 int prevlen = 0; /* length of "prev" if not NULL */
11841 char_u *s;
11842 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011843 long maxline = MAXLNUM;
11844 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011845
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011846 if (argvars[1].v_type != VAR_UNKNOWN)
11847 {
11848 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11849 binary = TRUE;
11850 if (argvars[2].v_type != VAR_UNKNOWN)
11851 maxline = get_tv_number(&argvars[2]);
11852 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011853
11854 l = list_alloc();
11855 if (l == NULL)
11856 return;
11857 rettv->v_type = VAR_LIST;
11858 rettv->vval.v_list = l;
11859 l->lv_refcount = 1;
11860
11861 /* Always open the file in binary mode, library functions have a mind of
11862 * their own about CR-LF conversion. */
11863 fname = get_tv_string(&argvars[0]);
11864 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11865 {
11866 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11867 return;
11868 }
11869
11870 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011871 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011872 {
11873 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11874 buflen = filtd + readlen;
11875 tolist = 0;
11876 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11877 {
11878 if (buf[filtd] == '\n' || readlen <= 0)
11879 {
11880 /* Only when in binary mode add an empty list item when the
11881 * last line ends in a '\n'. */
11882 if (!binary && readlen == 0 && filtd == 0)
11883 break;
11884
11885 /* Found end-of-line or end-of-file: add a text line to the
11886 * list. */
11887 chop = 0;
11888 if (!binary)
11889 while (filtd - chop - 1 >= tolist
11890 && buf[filtd - chop - 1] == '\r')
11891 ++chop;
11892 len = filtd - tolist - chop;
11893 if (prev == NULL)
11894 s = vim_strnsave(buf + tolist, len);
11895 else
11896 {
11897 s = alloc((unsigned)(prevlen + len + 1));
11898 if (s != NULL)
11899 {
11900 mch_memmove(s, prev, prevlen);
11901 vim_free(prev);
11902 prev = NULL;
11903 mch_memmove(s + prevlen, buf + tolist, len);
11904 s[prevlen + len] = NUL;
11905 }
11906 }
11907 tolist = filtd + 1;
11908
11909 li = listitem_alloc();
11910 if (li == NULL)
11911 {
11912 vim_free(s);
11913 break;
11914 }
11915 li->li_tv.v_type = VAR_STRING;
11916 li->li_tv.v_lock = 0;
11917 li->li_tv.vval.v_string = s;
11918 list_append(l, li);
11919
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011920 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011921 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011922 if (readlen <= 0)
11923 break;
11924 }
11925 else if (buf[filtd] == NUL)
11926 buf[filtd] = '\n';
11927 }
11928 if (readlen <= 0)
11929 break;
11930
11931 if (tolist == 0)
11932 {
11933 /* "buf" is full, need to move text to an allocated buffer */
11934 if (prev == NULL)
11935 {
11936 prev = vim_strnsave(buf, buflen);
11937 prevlen = buflen;
11938 }
11939 else
11940 {
11941 s = alloc((unsigned)(prevlen + buflen));
11942 if (s != NULL)
11943 {
11944 mch_memmove(s, prev, prevlen);
11945 mch_memmove(s + prevlen, buf, buflen);
11946 vim_free(prev);
11947 prev = s;
11948 prevlen += buflen;
11949 }
11950 }
11951 filtd = 0;
11952 }
11953 else
11954 {
11955 mch_memmove(buf, buf + tolist, buflen - tolist);
11956 filtd -= tolist;
11957 }
11958 }
11959
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011960 /*
11961 * For a negative line count use only the lines at the end of the file,
11962 * free the rest.
11963 */
11964 if (maxline < 0)
11965 while (cnt > -maxline)
11966 {
11967 listitem_remove(l, l->lv_first);
11968 --cnt;
11969 }
11970
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011971 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011972 fclose(fd);
11973}
11974
11975
Bram Moolenaar0d660222005-01-07 21:51:51 +000011976#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
11977static void make_connection __ARGS((void));
11978static int check_connection __ARGS((void));
11979
11980 static void
11981make_connection()
11982{
11983 if (X_DISPLAY == NULL
11984# ifdef FEAT_GUI
11985 && !gui.in_use
11986# endif
11987 )
11988 {
11989 x_force_connect = TRUE;
11990 setup_term_clip();
11991 x_force_connect = FALSE;
11992 }
11993}
11994
11995 static int
11996check_connection()
11997{
11998 make_connection();
11999 if (X_DISPLAY == NULL)
12000 {
12001 EMSG(_("E240: No connection to Vim server"));
12002 return FAIL;
12003 }
12004 return OK;
12005}
12006#endif
12007
12008#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012009static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012010
12011 static void
12012remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012013 typval_T *argvars;
12014 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012015 int expr;
12016{
12017 char_u *server_name;
12018 char_u *keys;
12019 char_u *r = NULL;
12020 char_u buf[NUMBUFLEN];
12021# ifdef WIN32
12022 HWND w;
12023# else
12024 Window w;
12025# endif
12026
12027 if (check_restricted() || check_secure())
12028 return;
12029
12030# ifdef FEAT_X11
12031 if (check_connection() == FAIL)
12032 return;
12033# endif
12034
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012035 server_name = get_tv_string_chk(&argvars[0]);
12036 if (server_name == NULL)
12037 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012038 keys = get_tv_string_buf(&argvars[1], buf);
12039# ifdef WIN32
12040 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12041# else
12042 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12043 < 0)
12044# endif
12045 {
12046 if (r != NULL)
12047 EMSG(r); /* sending worked but evaluation failed */
12048 else
12049 EMSG2(_("E241: Unable to send to %s"), server_name);
12050 return;
12051 }
12052
12053 rettv->vval.v_string = r;
12054
12055 if (argvars[2].v_type != VAR_UNKNOWN)
12056 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012057 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012058 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012059 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012060
12061 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012062 v.di_tv.v_type = VAR_STRING;
12063 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012064 idvar = get_tv_string_chk(&argvars[2]);
12065 if (idvar != NULL)
12066 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012067 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012068 }
12069}
12070#endif
12071
12072/*
12073 * "remote_expr()" function
12074 */
12075/*ARGSUSED*/
12076 static void
12077f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012078 typval_T *argvars;
12079 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012080{
12081 rettv->v_type = VAR_STRING;
12082 rettv->vval.v_string = NULL;
12083#ifdef FEAT_CLIENTSERVER
12084 remote_common(argvars, rettv, TRUE);
12085#endif
12086}
12087
12088/*
12089 * "remote_foreground()" function
12090 */
12091/*ARGSUSED*/
12092 static void
12093f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012094 typval_T *argvars;
12095 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012096{
12097 rettv->vval.v_number = 0;
12098#ifdef FEAT_CLIENTSERVER
12099# ifdef WIN32
12100 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012101 {
12102 char_u *server_name = get_tv_string_chk(&argvars[0]);
12103
12104 if (server_name != NULL)
12105 serverForeground(server_name);
12106 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012107# else
12108 /* Send a foreground() expression to the server. */
12109 argvars[1].v_type = VAR_STRING;
12110 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12111 argvars[2].v_type = VAR_UNKNOWN;
12112 remote_common(argvars, rettv, TRUE);
12113 vim_free(argvars[1].vval.v_string);
12114# endif
12115#endif
12116}
12117
12118/*ARGSUSED*/
12119 static void
12120f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012121 typval_T *argvars;
12122 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012123{
12124#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012125 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012126 char_u *s = NULL;
12127# ifdef WIN32
12128 int n = 0;
12129# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012130 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012131
12132 if (check_restricted() || check_secure())
12133 {
12134 rettv->vval.v_number = -1;
12135 return;
12136 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012137 serverid = get_tv_string_chk(&argvars[0]);
12138 if (serverid == NULL)
12139 {
12140 rettv->vval.v_number = -1;
12141 return; /* type error; errmsg already given */
12142 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012143# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012144 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012145 if (n == 0)
12146 rettv->vval.v_number = -1;
12147 else
12148 {
12149 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12150 rettv->vval.v_number = (s != NULL);
12151 }
12152# else
12153 rettv->vval.v_number = 0;
12154 if (check_connection() == FAIL)
12155 return;
12156
12157 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012158 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012159# endif
12160
12161 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012163 char_u *retvar;
12164
Bram Moolenaar33570922005-01-25 22:26:29 +000012165 v.di_tv.v_type = VAR_STRING;
12166 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012167 retvar = get_tv_string_chk(&argvars[1]);
12168 if (retvar != NULL)
12169 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012170 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012171 }
12172#else
12173 rettv->vval.v_number = -1;
12174#endif
12175}
12176
12177/*ARGSUSED*/
12178 static void
12179f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012180 typval_T *argvars;
12181 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012182{
12183 char_u *r = NULL;
12184
12185#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012186 char_u *serverid = get_tv_string_chk(&argvars[0]);
12187
12188 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012189 {
12190# ifdef WIN32
12191 /* The server's HWND is encoded in the 'id' parameter */
12192 int n = 0;
12193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012194 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012195 if (n != 0)
12196 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12197 if (r == NULL)
12198# else
12199 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012200 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012201# endif
12202 EMSG(_("E277: Unable to read a server reply"));
12203 }
12204#endif
12205 rettv->v_type = VAR_STRING;
12206 rettv->vval.v_string = r;
12207}
12208
12209/*
12210 * "remote_send()" function
12211 */
12212/*ARGSUSED*/
12213 static void
12214f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012215 typval_T *argvars;
12216 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012217{
12218 rettv->v_type = VAR_STRING;
12219 rettv->vval.v_string = NULL;
12220#ifdef FEAT_CLIENTSERVER
12221 remote_common(argvars, rettv, FALSE);
12222#endif
12223}
12224
12225/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012226 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012227 */
12228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012229f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012230 typval_T *argvars;
12231 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012232{
Bram Moolenaar33570922005-01-25 22:26:29 +000012233 list_T *l;
12234 listitem_T *item, *item2;
12235 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012236 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012237 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012238 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012239 dict_T *d;
12240 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012241
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012242 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012243 if (argvars[0].v_type == VAR_DICT)
12244 {
12245 if (argvars[2].v_type != VAR_UNKNOWN)
12246 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012247 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012248 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012250 key = get_tv_string_chk(&argvars[1]);
12251 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012253 di = dict_find(d, key, -1);
12254 if (di == NULL)
12255 EMSG2(_(e_dictkey), key);
12256 else
12257 {
12258 *rettv = di->di_tv;
12259 init_tv(&di->di_tv);
12260 dictitem_remove(d, di);
12261 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012262 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012263 }
12264 }
12265 else if (argvars[0].v_type != VAR_LIST)
12266 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012267 else if ((l = argvars[0].vval.v_list) != NULL
12268 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012270 int error = FALSE;
12271
12272 idx = get_tv_number_chk(&argvars[1], &error);
12273 if (error)
12274 ; /* type error: do nothing, errmsg already given */
12275 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012276 EMSGN(_(e_listidx), idx);
12277 else
12278 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012279 if (argvars[2].v_type == VAR_UNKNOWN)
12280 {
12281 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012282 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012283 *rettv = item->li_tv;
12284 vim_free(item);
12285 }
12286 else
12287 {
12288 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012289 end = get_tv_number_chk(&argvars[2], &error);
12290 if (error)
12291 ; /* type error: do nothing */
12292 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012293 EMSGN(_(e_listidx), end);
12294 else
12295 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012296 int cnt = 0;
12297
12298 for (li = item; li != NULL; li = li->li_next)
12299 {
12300 ++cnt;
12301 if (li == item2)
12302 break;
12303 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012304 if (li == NULL) /* didn't find "item2" after "item" */
12305 EMSG(_(e_invrange));
12306 else
12307 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012308 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012309 l = list_alloc();
12310 if (l != NULL)
12311 {
12312 rettv->v_type = VAR_LIST;
12313 rettv->vval.v_list = l;
12314 l->lv_first = item;
12315 l->lv_last = item2;
12316 l->lv_refcount = 1;
12317 item->li_prev = NULL;
12318 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012319 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012320 }
12321 }
12322 }
12323 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012324 }
12325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326}
12327
12328/*
12329 * "rename({from}, {to})" function
12330 */
12331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012332f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012333 typval_T *argvars;
12334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335{
12336 char_u buf[NUMBUFLEN];
12337
12338 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012339 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012341 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12342 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343}
12344
12345/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012346 * "repeat()" function
12347 */
12348/*ARGSUSED*/
12349 static void
12350f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012351 typval_T *argvars;
12352 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012353{
12354 char_u *p;
12355 int n;
12356 int slen;
12357 int len;
12358 char_u *r;
12359 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012360 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012361
12362 n = get_tv_number(&argvars[1]);
12363 if (argvars[0].v_type == VAR_LIST)
12364 {
12365 l = list_alloc();
12366 if (l != NULL && argvars[0].vval.v_list != NULL)
12367 {
12368 l->lv_refcount = 1;
12369 while (n-- > 0)
12370 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12371 break;
12372 }
12373 rettv->v_type = VAR_LIST;
12374 rettv->vval.v_list = l;
12375 }
12376 else
12377 {
12378 p = get_tv_string(&argvars[0]);
12379 rettv->v_type = VAR_STRING;
12380 rettv->vval.v_string = NULL;
12381
12382 slen = (int)STRLEN(p);
12383 len = slen * n;
12384 if (len <= 0)
12385 return;
12386
12387 r = alloc(len + 1);
12388 if (r != NULL)
12389 {
12390 for (i = 0; i < n; i++)
12391 mch_memmove(r + i * slen, p, (size_t)slen);
12392 r[len] = NUL;
12393 }
12394
12395 rettv->vval.v_string = r;
12396 }
12397}
12398
12399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400 * "resolve()" function
12401 */
12402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012403f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012404 typval_T *argvars;
12405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406{
12407 char_u *p;
12408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012409 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410#ifdef FEAT_SHORTCUT
12411 {
12412 char_u *v = NULL;
12413
12414 v = mch_resolve_shortcut(p);
12415 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012416 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012418 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419 }
12420#else
12421# ifdef HAVE_READLINK
12422 {
12423 char_u buf[MAXPATHL + 1];
12424 char_u *cpy;
12425 int len;
12426 char_u *remain = NULL;
12427 char_u *q;
12428 int is_relative_to_current = FALSE;
12429 int has_trailing_pathsep = FALSE;
12430 int limit = 100;
12431
12432 p = vim_strsave(p);
12433
12434 if (p[0] == '.' && (vim_ispathsep(p[1])
12435 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12436 is_relative_to_current = TRUE;
12437
12438 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012439 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440 has_trailing_pathsep = TRUE;
12441
12442 q = getnextcomp(p);
12443 if (*q != NUL)
12444 {
12445 /* Separate the first path component in "p", and keep the
12446 * remainder (beginning with the path separator). */
12447 remain = vim_strsave(q - 1);
12448 q[-1] = NUL;
12449 }
12450
12451 for (;;)
12452 {
12453 for (;;)
12454 {
12455 len = readlink((char *)p, (char *)buf, MAXPATHL);
12456 if (len <= 0)
12457 break;
12458 buf[len] = NUL;
12459
12460 if (limit-- == 0)
12461 {
12462 vim_free(p);
12463 vim_free(remain);
12464 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012465 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466 goto fail;
12467 }
12468
12469 /* Ensure that the result will have a trailing path separator
12470 * if the argument has one. */
12471 if (remain == NULL && has_trailing_pathsep)
12472 add_pathsep(buf);
12473
12474 /* Separate the first path component in the link value and
12475 * concatenate the remainders. */
12476 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12477 if (*q != NUL)
12478 {
12479 if (remain == NULL)
12480 remain = vim_strsave(q - 1);
12481 else
12482 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012483 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484 if (cpy != NULL)
12485 {
12486 STRCAT(cpy, remain);
12487 vim_free(remain);
12488 remain = cpy;
12489 }
12490 }
12491 q[-1] = NUL;
12492 }
12493
12494 q = gettail(p);
12495 if (q > p && *q == NUL)
12496 {
12497 /* Ignore trailing path separator. */
12498 q[-1] = NUL;
12499 q = gettail(p);
12500 }
12501 if (q > p && !mch_isFullName(buf))
12502 {
12503 /* symlink is relative to directory of argument */
12504 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12505 if (cpy != NULL)
12506 {
12507 STRCPY(cpy, p);
12508 STRCPY(gettail(cpy), buf);
12509 vim_free(p);
12510 p = cpy;
12511 }
12512 }
12513 else
12514 {
12515 vim_free(p);
12516 p = vim_strsave(buf);
12517 }
12518 }
12519
12520 if (remain == NULL)
12521 break;
12522
12523 /* Append the first path component of "remain" to "p". */
12524 q = getnextcomp(remain + 1);
12525 len = q - remain - (*q != NUL);
12526 cpy = vim_strnsave(p, STRLEN(p) + len);
12527 if (cpy != NULL)
12528 {
12529 STRNCAT(cpy, remain, len);
12530 vim_free(p);
12531 p = cpy;
12532 }
12533 /* Shorten "remain". */
12534 if (*q != NUL)
12535 STRCPY(remain, q - 1);
12536 else
12537 {
12538 vim_free(remain);
12539 remain = NULL;
12540 }
12541 }
12542
12543 /* If the result is a relative path name, make it explicitly relative to
12544 * the current directory if and only if the argument had this form. */
12545 if (!vim_ispathsep(*p))
12546 {
12547 if (is_relative_to_current
12548 && *p != NUL
12549 && !(p[0] == '.'
12550 && (p[1] == NUL
12551 || vim_ispathsep(p[1])
12552 || (p[1] == '.'
12553 && (p[2] == NUL
12554 || vim_ispathsep(p[2]))))))
12555 {
12556 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012557 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558 if (cpy != NULL)
12559 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560 vim_free(p);
12561 p = cpy;
12562 }
12563 }
12564 else if (!is_relative_to_current)
12565 {
12566 /* Strip leading "./". */
12567 q = p;
12568 while (q[0] == '.' && vim_ispathsep(q[1]))
12569 q += 2;
12570 if (q > p)
12571 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12572 }
12573 }
12574
12575 /* Ensure that the result will have no trailing path separator
12576 * if the argument had none. But keep "/" or "//". */
12577 if (!has_trailing_pathsep)
12578 {
12579 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012580 if (after_pathsep(p, q))
12581 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012582 }
12583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012584 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585 }
12586# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012587 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588# endif
12589#endif
12590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012591 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592
12593#ifdef HAVE_READLINK
12594fail:
12595#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012596 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597}
12598
12599/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012600 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012601 */
12602 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012603f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012604 typval_T *argvars;
12605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606{
Bram Moolenaar33570922005-01-25 22:26:29 +000012607 list_T *l;
12608 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012609
Bram Moolenaar0d660222005-01-07 21:51:51 +000012610 rettv->vval.v_number = 0;
12611 if (argvars[0].v_type != VAR_LIST)
12612 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012613 else if ((l = argvars[0].vval.v_list) != NULL
12614 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012615 {
12616 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012617 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012618 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012619 while (li != NULL)
12620 {
12621 ni = li->li_prev;
12622 list_append(l, li);
12623 li = ni;
12624 }
12625 rettv->vval.v_list = l;
12626 rettv->v_type = VAR_LIST;
12627 ++l->lv_refcount;
12628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629}
12630
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012631#define SP_NOMOVE 1 /* don't move cursor */
12632#define SP_REPEAT 2 /* repeat to find outer pair */
12633#define SP_RETCOUNT 4 /* return matchcount */
12634
Bram Moolenaar33570922005-01-25 22:26:29 +000012635static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012636
12637/*
12638 * Get flags for a search function.
12639 * Possibly sets "p_ws".
12640 * Returns BACKWARD, FORWARD or zero (for an error).
12641 */
12642 static int
12643get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012644 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012645 int *flagsp;
12646{
12647 int dir = FORWARD;
12648 char_u *flags;
12649 char_u nbuf[NUMBUFLEN];
12650 int mask;
12651
12652 if (varp->v_type != VAR_UNKNOWN)
12653 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012654 flags = get_tv_string_buf_chk(varp, nbuf);
12655 if (flags == NULL)
12656 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012657 while (*flags != NUL)
12658 {
12659 switch (*flags)
12660 {
12661 case 'b': dir = BACKWARD; break;
12662 case 'w': p_ws = TRUE; break;
12663 case 'W': p_ws = FALSE; break;
12664 default: mask = 0;
12665 if (flagsp != NULL)
12666 switch (*flags)
12667 {
12668 case 'n': mask = SP_NOMOVE; break;
12669 case 'r': mask = SP_REPEAT; break;
12670 case 'm': mask = SP_RETCOUNT; break;
12671 }
12672 if (mask == 0)
12673 {
12674 EMSG2(_(e_invarg2), flags);
12675 dir = 0;
12676 }
12677 else
12678 *flagsp |= mask;
12679 }
12680 if (dir == 0)
12681 break;
12682 ++flags;
12683 }
12684 }
12685 return dir;
12686}
12687
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688/*
12689 * "search()" function
12690 */
12691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012693 typval_T *argvars;
12694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695{
12696 char_u *pat;
12697 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012698 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699 int save_p_ws = p_ws;
12700 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012701 int flags = 0;
12702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012703 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012705 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012706 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12707 if (dir == 0)
12708 goto theend;
12709 if ((flags & ~SP_NOMOVE) != 0)
12710 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012711 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012712 goto theend;
12713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012715 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12717 SEARCH_KEEP, RE_SEARCH) != FAIL)
12718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012719 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720 curwin->w_cursor = pos;
12721 /* "/$" will put the cursor after the end of the line, may need to
12722 * correct that here */
12723 check_cursor();
12724 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012725
12726 /* If 'n' flag is used: restore cursor position. */
12727 if (flags & SP_NOMOVE)
12728 curwin->w_cursor = save_cursor;
12729theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730 p_ws = save_p_ws;
12731}
12732
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733/*
12734 * "searchpair()" function
12735 */
12736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012737f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012738 typval_T *argvars;
12739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740{
12741 char_u *spat, *mpat, *epat;
12742 char_u *skip;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012743 char_u *pat, *pat2 = NULL, *pat3 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744 pos_T pos;
12745 pos_T firstpos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012746 pos_T foundpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747 pos_T save_cursor;
12748 pos_T save_pos;
12749 int save_p_ws = p_ws;
12750 char_u *save_cpo;
12751 int dir;
12752 int flags = 0;
12753 char_u nbuf1[NUMBUFLEN];
12754 char_u nbuf2[NUMBUFLEN];
12755 char_u nbuf3[NUMBUFLEN];
12756 int n;
12757 int r;
12758 int nest = 1;
12759 int err;
12760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012761 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762
12763 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12764 save_cpo = p_cpo;
12765 p_cpo = (char_u *)"";
12766
12767 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012768 spat = get_tv_string_chk(&argvars[0]);
12769 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
12770 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
12771 if (spat == NULL || mpat == NULL || epat == NULL)
12772 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773
12774 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12775 * start/middle/end (pat3, for the top pair). */
12776 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12777 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12778 if (pat2 == NULL || pat3 == NULL)
12779 goto theend;
12780 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12781 if (*mpat == NUL)
12782 STRCPY(pat3, pat2);
12783 else
12784 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12785 spat, epat, mpat);
12786
12787 /* Handle the optional fourth argument: flags */
12788 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012789 if (dir == 0)
12790 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791
12792 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012793 if (argvars[3].v_type == VAR_UNKNOWN
12794 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795 skip = (char_u *)"";
12796 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012797 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
12798 if (skip == NULL)
12799 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800
12801 save_cursor = curwin->w_cursor;
12802 pos = curwin->w_cursor;
12803 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012804 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805 pat = pat3;
12806 for (;;)
12807 {
12808 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12809 SEARCH_KEEP, RE_SEARCH);
12810 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12811 /* didn't find it or found the first match again: FAIL */
12812 break;
12813
12814 if (firstpos.lnum == 0)
12815 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012816 if (equalpos(pos, foundpos))
12817 {
12818 /* Found the same position again. Can happen with a pattern that
12819 * has "\zs" at the end and searching backwards. Advance one
12820 * character and try again. */
12821 if (dir == BACKWARD)
12822 decl(&pos);
12823 else
12824 incl(&pos);
12825 }
12826 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827
12828 /* If the skip pattern matches, ignore this match. */
12829 if (*skip != NUL)
12830 {
12831 save_pos = curwin->w_cursor;
12832 curwin->w_cursor = pos;
12833 r = eval_to_bool(skip, &err, NULL, FALSE);
12834 curwin->w_cursor = save_pos;
12835 if (err)
12836 {
12837 /* Evaluating {skip} caused an error, break here. */
12838 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012839 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840 break;
12841 }
12842 if (r)
12843 continue;
12844 }
12845
12846 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12847 {
12848 /* Found end when searching backwards or start when searching
12849 * forward: nested pair. */
12850 ++nest;
12851 pat = pat2; /* nested, don't search for middle */
12852 }
12853 else
12854 {
12855 /* Found end when searching forward or start when searching
12856 * backward: end of (nested) pair; or found middle in outer pair. */
12857 if (--nest == 1)
12858 pat = pat3; /* outer level, search for middle */
12859 }
12860
12861 if (nest == 0)
12862 {
12863 /* Found the match: return matchcount or line number. */
12864 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012865 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868 curwin->w_cursor = pos;
12869 if (!(flags & SP_REPEAT))
12870 break;
12871 nest = 1; /* search for next unmatched */
12872 }
12873 }
12874
12875 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012876 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877 curwin->w_cursor = save_cursor;
12878
12879theend:
12880 vim_free(pat2);
12881 vim_free(pat3);
12882 p_ws = save_p_ws;
12883 p_cpo = save_cpo;
12884}
12885
Bram Moolenaar0d660222005-01-07 21:51:51 +000012886/*ARGSUSED*/
12887 static void
12888f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012889 typval_T *argvars;
12890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012892#ifdef FEAT_CLIENTSERVER
12893 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012894 char_u *server = get_tv_string_chk(&argvars[0]);
12895 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896
Bram Moolenaar0d660222005-01-07 21:51:51 +000012897 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012898 if (server == NULL || reply == NULL)
12899 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012900 if (check_restricted() || check_secure())
12901 return;
12902# ifdef FEAT_X11
12903 if (check_connection() == FAIL)
12904 return;
12905# endif
12906
12907 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012909 EMSG(_("E258: Unable to send to client"));
12910 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012912 rettv->vval.v_number = 0;
12913#else
12914 rettv->vval.v_number = -1;
12915#endif
12916}
12917
12918/*ARGSUSED*/
12919 static void
12920f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012921 typval_T *argvars;
12922 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012923{
12924 char_u *r = NULL;
12925
12926#ifdef FEAT_CLIENTSERVER
12927# ifdef WIN32
12928 r = serverGetVimNames();
12929# else
12930 make_connection();
12931 if (X_DISPLAY != NULL)
12932 r = serverGetVimNames(X_DISPLAY);
12933# endif
12934#endif
12935 rettv->v_type = VAR_STRING;
12936 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937}
12938
12939/*
12940 * "setbufvar()" function
12941 */
12942/*ARGSUSED*/
12943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012944f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012945 typval_T *argvars;
12946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947{
12948 buf_T *buf;
12949#ifdef FEAT_AUTOCMD
12950 aco_save_T aco;
12951#else
12952 buf_T *save_curbuf;
12953#endif
12954 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012955 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956 char_u nbuf[NUMBUFLEN];
12957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012958 rettv->vval.v_number = 0;
12959
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960 if (check_restricted() || check_secure())
12961 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012962 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12963 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012964 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965 varp = &argvars[2];
12966
12967 if (buf != NULL && varname != NULL && varp != NULL)
12968 {
12969 /* set curbuf to be our buf, temporarily */
12970#ifdef FEAT_AUTOCMD
12971 aucmd_prepbuf(&aco, buf);
12972#else
12973 save_curbuf = curbuf;
12974 curbuf = buf;
12975#endif
12976
12977 if (*varname == '&')
12978 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012979 long numval;
12980 char_u *strval;
12981 int error = FALSE;
12982
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012984 numval = get_tv_number_chk(varp, &error);
12985 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012986 if (!error && strval != NULL)
12987 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988 }
12989 else
12990 {
12991 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
12992 if (bufvarname != NULL)
12993 {
12994 STRCPY(bufvarname, "b:");
12995 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012996 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997 vim_free(bufvarname);
12998 }
12999 }
13000
13001 /* reset notion of buffer */
13002#ifdef FEAT_AUTOCMD
13003 aucmd_restbuf(&aco);
13004#else
13005 curbuf = save_curbuf;
13006#endif
13007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013008}
13009
13010/*
13011 * "setcmdpos()" function
13012 */
13013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013014f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013015 typval_T *argvars;
13016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013018 int pos = (int)get_tv_number(&argvars[0]) - 1;
13019
13020 if (pos >= 0)
13021 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022}
13023
13024/*
13025 * "setline()" function
13026 */
13027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013028f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013029 typval_T *argvars;
13030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031{
13032 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013033 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013034 list_T *l = NULL;
13035 listitem_T *li = NULL;
13036 long added = 0;
13037 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013039 lnum = get_tv_lnum(&argvars[0]);
13040 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013042 l = argvars[1].vval.v_list;
13043 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013045 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013046 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013047
13048 rettv->vval.v_number = 0; /* OK */
13049 for (;;)
13050 {
13051 if (l != NULL)
13052 {
13053 /* list argument, get next string */
13054 if (li == NULL)
13055 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013056 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013057 li = li->li_next;
13058 }
13059
13060 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013061 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013062 break;
13063 if (lnum <= curbuf->b_ml.ml_line_count)
13064 {
13065 /* existing line, replace it */
13066 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13067 {
13068 changed_bytes(lnum, 0);
13069 check_cursor_col();
13070 rettv->vval.v_number = 0; /* OK */
13071 }
13072 }
13073 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13074 {
13075 /* lnum is one past the last line, append the line */
13076 ++added;
13077 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13078 rettv->vval.v_number = 0; /* OK */
13079 }
13080
13081 if (l == NULL) /* only one string argument */
13082 break;
13083 ++lnum;
13084 }
13085
13086 if (added > 0)
13087 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013088}
13089
13090/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013091 * "setqflist()" function
13092 */
13093/*ARGSUSED*/
13094 static void
13095f_setqflist(argvars, rettv)
13096 typval_T *argvars;
13097 typval_T *rettv;
13098{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013099 char_u *act;
13100 int action = ' ';
13101
Bram Moolenaar2641f772005-03-25 21:58:17 +000013102 rettv->vval.v_number = -1;
13103
13104#ifdef FEAT_QUICKFIX
13105 if (argvars[0].v_type != VAR_LIST)
13106 EMSG(_(e_listreq));
13107 else
13108 {
13109 list_T *l = argvars[0].vval.v_list;
13110
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013111 if (argvars[1].v_type == VAR_STRING)
13112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013113 act = get_tv_string_chk(&argvars[1]);
13114 if (act == NULL)
13115 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013116 if (*act == 'a' || *act == 'r')
13117 action = *act;
13118 }
13119
13120 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013121 rettv->vval.v_number = 0;
13122 }
13123#endif
13124}
13125
13126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127 * "setreg()" function
13128 */
13129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013130f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013131 typval_T *argvars;
13132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133{
13134 int regname;
13135 char_u *strregname;
13136 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013137 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 int append;
13139 char_u yank_type;
13140 long block_len;
13141
13142 block_len = -1;
13143 yank_type = MAUTO;
13144 append = FALSE;
13145
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013146 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013147 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013149 if (strregname == NULL)
13150 return; /* type error; errmsg already given */
13151 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152 if (regname == 0 || regname == '@')
13153 regname = '"';
13154 else if (regname == '=')
13155 return;
13156
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013157 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013159 stropt = get_tv_string_chk(&argvars[2]);
13160 if (stropt == NULL)
13161 return; /* type error */
13162 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163 switch (*stropt)
13164 {
13165 case 'a': case 'A': /* append */
13166 append = TRUE;
13167 break;
13168 case 'v': case 'c': /* character-wise selection */
13169 yank_type = MCHAR;
13170 break;
13171 case 'V': case 'l': /* line-wise selection */
13172 yank_type = MLINE;
13173 break;
13174#ifdef FEAT_VISUAL
13175 case 'b': case Ctrl_V: /* block-wise selection */
13176 yank_type = MBLOCK;
13177 if (VIM_ISDIGIT(stropt[1]))
13178 {
13179 ++stropt;
13180 block_len = getdigits(&stropt) - 1;
13181 --stropt;
13182 }
13183 break;
13184#endif
13185 }
13186 }
13187
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013188 strval = get_tv_string_chk(&argvars[1]);
13189 if (strval != NULL)
13190 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013192 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193}
13194
13195
13196/*
13197 * "setwinvar(expr)" function
13198 */
13199/*ARGSUSED*/
13200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013201f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013202 typval_T *argvars;
13203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204{
13205 win_T *win;
13206#ifdef FEAT_WINDOWS
13207 win_T *save_curwin;
13208#endif
13209 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013210 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211 char_u nbuf[NUMBUFLEN];
13212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013213 rettv->vval.v_number = 0;
13214
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215 if (check_restricted() || check_secure())
13216 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013218 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 varp = &argvars[2];
13220
13221 if (win != NULL && varname != NULL && varp != NULL)
13222 {
13223#ifdef FEAT_WINDOWS
13224 /* set curwin to be our win, temporarily */
13225 save_curwin = curwin;
13226 curwin = win;
13227 curbuf = curwin->w_buffer;
13228#endif
13229
13230 if (*varname == '&')
13231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013232 long numval;
13233 char_u *strval;
13234 int error = FALSE;
13235
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013237 numval = get_tv_number_chk(varp, &error);
13238 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013239 if (!error && strval != NULL)
13240 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241 }
13242 else
13243 {
13244 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13245 if (winvarname != NULL)
13246 {
13247 STRCPY(winvarname, "w:");
13248 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013249 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250 vim_free(winvarname);
13251 }
13252 }
13253
13254#ifdef FEAT_WINDOWS
13255 /* Restore current window, if it's still valid (autocomands can make
13256 * it invalid). */
13257 if (win_valid(save_curwin))
13258 {
13259 curwin = save_curwin;
13260 curbuf = curwin->w_buffer;
13261 }
13262#endif
13263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264}
13265
13266/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013267 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268 */
13269 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013270f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013271 typval_T *argvars;
13272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013274 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275
Bram Moolenaar0d660222005-01-07 21:51:51 +000013276 p = get_tv_string(&argvars[0]);
13277 rettv->vval.v_string = vim_strsave(p);
13278 simplify_filename(rettv->vval.v_string); /* simplify in place */
13279 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280}
13281
Bram Moolenaar0d660222005-01-07 21:51:51 +000013282static int
13283#ifdef __BORLANDC__
13284 _RTLENTRYF
13285#endif
13286 item_compare __ARGS((const void *s1, const void *s2));
13287static int
13288#ifdef __BORLANDC__
13289 _RTLENTRYF
13290#endif
13291 item_compare2 __ARGS((const void *s1, const void *s2));
13292
13293static int item_compare_ic;
13294static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013295static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013296#define ITEM_COMPARE_FAIL 999
13297
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013299 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013301 static int
13302#ifdef __BORLANDC__
13303_RTLENTRYF
13304#endif
13305item_compare(s1, s2)
13306 const void *s1;
13307 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013309 char_u *p1, *p2;
13310 char_u *tofree1, *tofree2;
13311 int res;
13312 char_u numbuf1[NUMBUFLEN];
13313 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314
Bram Moolenaar33570922005-01-25 22:26:29 +000013315 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13316 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013317 if (item_compare_ic)
13318 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013320 res = STRCMP(p1, p2);
13321 vim_free(tofree1);
13322 vim_free(tofree2);
13323 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324}
13325
13326 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013327#ifdef __BORLANDC__
13328_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013330item_compare2(s1, s2)
13331 const void *s1;
13332 const void *s2;
13333{
13334 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013335 typval_T rettv;
13336 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013337 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013339 /* shortcut after failure in previous call; compare all items equal */
13340 if (item_compare_func_err)
13341 return 0;
13342
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013343 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13344 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013345 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13346 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013347
13348 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13349 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013350 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013351 clear_tv(&argv[0]);
13352 clear_tv(&argv[1]);
13353
13354 if (res == FAIL)
13355 res = ITEM_COMPARE_FAIL;
13356 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013357 /* return value has wrong type */
13358 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13359 if (item_compare_func_err)
13360 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013361 clear_tv(&rettv);
13362 return res;
13363}
13364
13365/*
13366 * "sort({list})" function
13367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013369f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013370 typval_T *argvars;
13371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372{
Bram Moolenaar33570922005-01-25 22:26:29 +000013373 list_T *l;
13374 listitem_T *li;
13375 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013376 long len;
13377 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378
Bram Moolenaar0d660222005-01-07 21:51:51 +000013379 rettv->vval.v_number = 0;
13380 if (argvars[0].v_type != VAR_LIST)
13381 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382 else
13383 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013384 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013385 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013386 return;
13387 rettv->vval.v_list = l;
13388 rettv->v_type = VAR_LIST;
13389 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390
Bram Moolenaar0d660222005-01-07 21:51:51 +000013391 len = list_len(l);
13392 if (len <= 1)
13393 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394
Bram Moolenaar0d660222005-01-07 21:51:51 +000013395 item_compare_ic = FALSE;
13396 item_compare_func = NULL;
13397 if (argvars[1].v_type != VAR_UNKNOWN)
13398 {
13399 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013400 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013401 else
13402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013403 int error = FALSE;
13404
13405 i = get_tv_number_chk(&argvars[1], &error);
13406 if (error)
13407 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013408 if (i == 1)
13409 item_compare_ic = TRUE;
13410 else
13411 item_compare_func = get_tv_string(&argvars[1]);
13412 }
13413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414
Bram Moolenaar0d660222005-01-07 21:51:51 +000013415 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013416 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013417 if (ptrs == NULL)
13418 return;
13419 i = 0;
13420 for (li = l->lv_first; li != NULL; li = li->li_next)
13421 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013423 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013424 /* test the compare function */
13425 if (item_compare_func != NULL
13426 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13427 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013428 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013430 {
13431 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013432 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013433 item_compare_func == NULL ? item_compare : item_compare2);
13434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013435 if (!item_compare_func_err)
13436 {
13437 /* Clear the List and append the items in the sorted order. */
13438 l->lv_first = l->lv_last = NULL;
13439 l->lv_len = 0;
13440 for (i = 0; i < len; ++i)
13441 list_append(l, ptrs[i]);
13442 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013443 }
13444
13445 vim_free(ptrs);
13446 }
13447}
13448
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013449/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013450 * "soundfold({word})" function
13451 */
13452 static void
13453f_soundfold(argvars, rettv)
13454 typval_T *argvars;
13455 typval_T *rettv;
13456{
13457 char_u *s;
13458
13459 rettv->v_type = VAR_STRING;
13460 s = get_tv_string(&argvars[0]);
13461#ifdef FEAT_SYN_HL
13462 rettv->vval.v_string = eval_soundfold(s);
13463#else
13464 rettv->vval.v_string = vim_strsave(s);
13465#endif
13466}
13467
13468/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013469 * "spellbadword()" function
13470 */
13471/* ARGSUSED */
13472 static void
13473f_spellbadword(argvars, rettv)
13474 typval_T *argvars;
13475 typval_T *rettv;
13476{
13477 int attr;
13478 char_u *ptr;
13479 int len;
13480
13481 rettv->vval.v_string = NULL;
13482 rettv->v_type = VAR_STRING;
13483
13484#ifdef FEAT_SYN_HL
13485 /* Find the start of the badly spelled word. */
13486 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
13487 return;
13488
13489 /* Get the length of the word and copy it. */
13490 ptr = ml_get_cursor();
Bram Moolenaar51ac12f2005-07-02 23:21:11 +000013491 len = spell_check(curwin, ptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013492 rettv->vval.v_string = vim_strnsave(ptr, len);
13493#endif
13494}
13495
13496/*
13497 * "spellsuggest()" function
13498 */
13499 static void
13500f_spellsuggest(argvars, rettv)
13501 typval_T *argvars;
13502 typval_T *rettv;
13503{
13504 char_u *str;
13505 int maxcount;
13506 garray_T ga;
13507 list_T *l;
13508 listitem_T *li;
13509 int i;
13510
13511 l = list_alloc();
13512 if (l == NULL)
13513 return;
13514 rettv->v_type = VAR_LIST;
13515 rettv->vval.v_list = l;
13516 ++l->lv_refcount;
13517
13518#ifdef FEAT_SYN_HL
13519 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13520 {
13521 str = get_tv_string(&argvars[0]);
13522 if (argvars[1].v_type != VAR_UNKNOWN)
13523 {
13524 maxcount = get_tv_number(&argvars[1]);
13525 if (maxcount <= 0)
13526 return;
13527 }
13528 else
13529 maxcount = 25;
13530
13531 spell_suggest_list(&ga, str, maxcount);
13532
13533 for (i = 0; i < ga.ga_len; ++i)
13534 {
13535 str = ((char_u **)ga.ga_data)[i];
13536
13537 li = listitem_alloc();
13538 if (li == NULL)
13539 vim_free(str);
13540 else
13541 {
13542 li->li_tv.v_type = VAR_STRING;
13543 li->li_tv.v_lock = 0;
13544 li->li_tv.vval.v_string = str;
13545 list_append(l, li);
13546 }
13547 }
13548 ga_clear(&ga);
13549 }
13550#endif
13551}
13552
Bram Moolenaar0d660222005-01-07 21:51:51 +000013553 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013554f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013555 typval_T *argvars;
13556 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013557{
13558 char_u *str;
13559 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013560 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013561 regmatch_T regmatch;
13562 char_u patbuf[NUMBUFLEN];
13563 char_u *save_cpo;
13564 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013565 listitem_T *ni;
13566 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013567 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013568 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013569 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013570
13571 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13572 save_cpo = p_cpo;
13573 p_cpo = (char_u *)"";
13574
13575 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013576 if (argvars[1].v_type != VAR_UNKNOWN)
13577 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013578 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13579 if (pat == NULL)
13580 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013581 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013582 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013583 }
13584 if (pat == NULL || *pat == NUL)
13585 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013586
13587 l = list_alloc();
13588 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013590 rettv->v_type = VAR_LIST;
13591 rettv->vval.v_list = l;
13592 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013593 if (typeerr)
13594 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595
Bram Moolenaar0d660222005-01-07 21:51:51 +000013596 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13597 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013599 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013600 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013601 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013602 if (*str == NUL)
13603 match = FALSE; /* empty item at the end */
13604 else
13605 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013606 if (match)
13607 end = regmatch.startp[0];
13608 else
13609 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013610 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13611 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013612 {
13613 ni = listitem_alloc();
13614 if (ni == NULL)
13615 break;
13616 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013617 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013618 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13619 list_append(l, ni);
13620 }
13621 if (!match)
13622 break;
13623 /* Advance to just after the match. */
13624 if (regmatch.endp[0] > str)
13625 col = 0;
13626 else
13627 {
13628 /* Don't get stuck at the same match. */
13629#ifdef FEAT_MBYTE
13630 col = mb_ptr2len_check(regmatch.endp[0]);
13631#else
13632 col = 1;
13633#endif
13634 }
13635 str = regmatch.endp[0];
13636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637
Bram Moolenaar0d660222005-01-07 21:51:51 +000013638 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640
Bram Moolenaar0d660222005-01-07 21:51:51 +000013641 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642}
13643
13644#ifdef HAVE_STRFTIME
13645/*
13646 * "strftime({format}[, {time}])" function
13647 */
13648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013650 typval_T *argvars;
13651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652{
13653 char_u result_buf[256];
13654 struct tm *curtime;
13655 time_t seconds;
13656 char_u *p;
13657
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013658 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013660 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013661 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662 seconds = time(NULL);
13663 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013664 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665 curtime = localtime(&seconds);
13666 /* MSVC returns NULL for an invalid value of seconds. */
13667 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669 else
13670 {
13671# ifdef FEAT_MBYTE
13672 vimconv_T conv;
13673 char_u *enc;
13674
13675 conv.vc_type = CONV_NONE;
13676 enc = enc_locale();
13677 convert_setup(&conv, p_enc, enc);
13678 if (conv.vc_type != CONV_NONE)
13679 p = string_convert(&conv, p, NULL);
13680# endif
13681 if (p != NULL)
13682 (void)strftime((char *)result_buf, sizeof(result_buf),
13683 (char *)p, curtime);
13684 else
13685 result_buf[0] = NUL;
13686
13687# ifdef FEAT_MBYTE
13688 if (conv.vc_type != CONV_NONE)
13689 vim_free(p);
13690 convert_setup(&conv, enc, p_enc);
13691 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013692 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 else
13694# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013695 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696
13697# ifdef FEAT_MBYTE
13698 /* Release conversion descriptors */
13699 convert_setup(&conv, NULL, NULL);
13700 vim_free(enc);
13701# endif
13702 }
13703}
13704#endif
13705
13706/*
13707 * "stridx()" function
13708 */
13709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013710f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013711 typval_T *argvars;
13712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713{
13714 char_u buf[NUMBUFLEN];
13715 char_u *needle;
13716 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000013717 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000013719 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013721 needle = get_tv_string_chk(&argvars[1]);
13722 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000013723 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013724 if (needle == NULL || haystack == NULL)
13725 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726
Bram Moolenaar33570922005-01-25 22:26:29 +000013727 if (argvars[2].v_type != VAR_UNKNOWN)
13728 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013729 int error = FALSE;
13730
13731 start_idx = get_tv_number_chk(&argvars[2], &error);
13732 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000013733 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013734 if (start_idx >= 0)
13735 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000013736 }
13737
13738 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13739 if (pos != NULL)
13740 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741}
13742
13743/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013744 * "string()" function
13745 */
13746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013747f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013748 typval_T *argvars;
13749 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013750{
13751 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013752 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013754 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013755 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013756 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013757 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758}
13759
13760/*
13761 * "strlen()" function
13762 */
13763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013764f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013765 typval_T *argvars;
13766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013768 rettv->vval.v_number = (varnumber_T)(STRLEN(
13769 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770}
13771
13772/*
13773 * "strpart()" function
13774 */
13775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013776f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013777 typval_T *argvars;
13778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779{
13780 char_u *p;
13781 int n;
13782 int len;
13783 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013784 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013786 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787 slen = (int)STRLEN(p);
13788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013789 n = get_tv_number_chk(&argvars[1], &error);
13790 if (error)
13791 len = 0;
13792 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013793 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794 else
13795 len = slen - n; /* default len: all bytes that are available. */
13796
13797 /*
13798 * Only return the overlap between the specified part and the actual
13799 * string.
13800 */
13801 if (n < 0)
13802 {
13803 len += n;
13804 n = 0;
13805 }
13806 else if (n > slen)
13807 n = slen;
13808 if (len < 0)
13809 len = 0;
13810 else if (n + len > slen)
13811 len = slen - n;
13812
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013813 rettv->v_type = VAR_STRING;
13814 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815}
13816
13817/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013818 * "strridx()" function
13819 */
13820 static void
13821f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013822 typval_T *argvars;
13823 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013824{
13825 char_u buf[NUMBUFLEN];
13826 char_u *needle;
13827 char_u *haystack;
13828 char_u *rest;
13829 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013830 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013832 needle = get_tv_string_chk(&argvars[1]);
13833 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013834 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013835
13836 rettv->vval.v_number = -1;
13837 if (needle == NULL || haystack == NULL)
13838 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013839 if (argvars[2].v_type != VAR_UNKNOWN)
13840 {
13841 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013842 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000013843 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013844 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013845 }
13846 else
13847 end_idx = haystack_len;
13848
Bram Moolenaar0d660222005-01-07 21:51:51 +000013849 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000013850 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013851 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013852 lastmatch = haystack + end_idx;
13853 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013854 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000013855 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013856 for (rest = haystack; *rest != '\0'; ++rest)
13857 {
13858 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013859 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013860 break;
13861 lastmatch = rest;
13862 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000013863 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013864
13865 if (lastmatch == NULL)
13866 rettv->vval.v_number = -1;
13867 else
13868 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13869}
13870
13871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 * "strtrans()" function
13873 */
13874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013875f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013876 typval_T *argvars;
13877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013879 rettv->v_type = VAR_STRING;
13880 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881}
13882
13883/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013884 * "submatch()" function
13885 */
13886 static void
13887f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013888 typval_T *argvars;
13889 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013890{
13891 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013892 rettv->vval.v_string =
13893 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013894}
13895
13896/*
13897 * "substitute()" function
13898 */
13899 static void
13900f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013901 typval_T *argvars;
13902 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013903{
13904 char_u patbuf[NUMBUFLEN];
13905 char_u subbuf[NUMBUFLEN];
13906 char_u flagsbuf[NUMBUFLEN];
13907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013908 char_u *str = get_tv_string_chk(&argvars[0]);
13909 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13910 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
13911 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
13912
Bram Moolenaar0d660222005-01-07 21:51:51 +000013913 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013914 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
13915 rettv->vval.v_string = NULL;
13916 else
13917 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013918}
13919
13920/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013921 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922 */
13923/*ARGSUSED*/
13924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013925f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013926 typval_T *argvars;
13927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928{
13929 int id = 0;
13930#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013931 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932 long col;
13933 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000013934 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013936 lnum = get_tv_lnum(argvars); /* -1 on type error */
13937 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
13938 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013940 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013941 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13942 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943#endif
13944
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013945 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946}
13947
13948/*
13949 * "synIDattr(id, what [, mode])" function
13950 */
13951/*ARGSUSED*/
13952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013953f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013954 typval_T *argvars;
13955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956{
13957 char_u *p = NULL;
13958#ifdef FEAT_SYN_HL
13959 int id;
13960 char_u *what;
13961 char_u *mode;
13962 char_u modebuf[NUMBUFLEN];
13963 int modec;
13964
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013965 id = get_tv_number(&argvars[0]);
13966 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013967 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013969 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970 modec = TOLOWER_ASC(mode[0]);
13971 if (modec != 't' && modec != 'c'
13972#ifdef FEAT_GUI
13973 && modec != 'g'
13974#endif
13975 )
13976 modec = 0; /* replace invalid with current */
13977 }
13978 else
13979 {
13980#ifdef FEAT_GUI
13981 if (gui.in_use)
13982 modec = 'g';
13983 else
13984#endif
13985 if (t_colors > 1)
13986 modec = 'c';
13987 else
13988 modec = 't';
13989 }
13990
13991
13992 switch (TOLOWER_ASC(what[0]))
13993 {
13994 case 'b':
13995 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13996 p = highlight_color(id, what, modec);
13997 else /* bold */
13998 p = highlight_has_attr(id, HL_BOLD, modec);
13999 break;
14000
14001 case 'f': /* fg[#] */
14002 p = highlight_color(id, what, modec);
14003 break;
14004
14005 case 'i':
14006 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14007 p = highlight_has_attr(id, HL_INVERSE, modec);
14008 else /* italic */
14009 p = highlight_has_attr(id, HL_ITALIC, modec);
14010 break;
14011
14012 case 'n': /* name */
14013 p = get_highlight_name(NULL, id - 1);
14014 break;
14015
14016 case 'r': /* reverse */
14017 p = highlight_has_attr(id, HL_INVERSE, modec);
14018 break;
14019
14020 case 's': /* standout */
14021 p = highlight_has_attr(id, HL_STANDOUT, modec);
14022 break;
14023
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014024 case 'u':
14025 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14026 /* underline */
14027 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14028 else
14029 /* undercurl */
14030 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 break;
14032 }
14033
14034 if (p != NULL)
14035 p = vim_strsave(p);
14036#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014037 rettv->v_type = VAR_STRING;
14038 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039}
14040
14041/*
14042 * "synIDtrans(id)" function
14043 */
14044/*ARGSUSED*/
14045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014046f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014047 typval_T *argvars;
14048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049{
14050 int id;
14051
14052#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014053 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054
14055 if (id > 0)
14056 id = syn_get_final_id(id);
14057 else
14058#endif
14059 id = 0;
14060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014061 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062}
14063
14064/*
14065 * "system()" function
14066 */
14067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014068f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014069 typval_T *argvars;
14070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014072 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014074 char_u *infile = NULL;
14075 char_u buf[NUMBUFLEN];
14076 int err = FALSE;
14077 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014079 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014080 {
14081 /*
14082 * Write the string to a temp file, to be used for input of the shell
14083 * command.
14084 */
14085 if ((infile = vim_tempname('i')) == NULL)
14086 {
14087 EMSG(_(e_notmp));
14088 return;
14089 }
14090
14091 fd = mch_fopen((char *)infile, WRITEBIN);
14092 if (fd == NULL)
14093 {
14094 EMSG2(_(e_notopen), infile);
14095 goto done;
14096 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014097 p = get_tv_string_buf_chk(&argvars[1], buf);
14098 if (p == NULL)
14099 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014100 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14101 err = TRUE;
14102 if (fclose(fd) != 0)
14103 err = TRUE;
14104 if (err)
14105 {
14106 EMSG(_("E677: Error writing temp file"));
14107 goto done;
14108 }
14109 }
14110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014111 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014112
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113#ifdef USE_CR
14114 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014115 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 {
14117 char_u *s;
14118
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014119 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120 {
14121 if (*s == CAR)
14122 *s = NL;
14123 }
14124 }
14125#else
14126# ifdef USE_CRNL
14127 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014128 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129 {
14130 char_u *s, *d;
14131
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014132 d = res;
14133 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134 {
14135 if (s[0] == CAR && s[1] == NL)
14136 ++s;
14137 *d++ = *s;
14138 }
14139 *d = NUL;
14140 }
14141# endif
14142#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014143
14144done:
14145 if (infile != NULL)
14146 {
14147 mch_remove(infile);
14148 vim_free(infile);
14149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014150 rettv->v_type = VAR_STRING;
14151 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152}
14153
14154/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014155 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014156 */
14157 static void
14158f_taglist(argvars, rettv)
14159 typval_T *argvars;
14160 typval_T *rettv;
14161{
14162 char_u *tag_pattern;
14163 list_T *l;
14164
14165 tag_pattern = get_tv_string(&argvars[0]);
14166
14167 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014168 if (*tag_pattern == NUL)
14169 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014170
14171 l = list_alloc();
14172 if (l != NULL)
14173 {
14174 if (get_tags(l, tag_pattern) != FAIL)
14175 {
14176 rettv->vval.v_list = l;
14177 rettv->v_type = VAR_LIST;
14178 ++l->lv_refcount;
14179 }
14180 else
14181 list_free(l);
14182 }
14183}
14184
14185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186 * "tempname()" function
14187 */
14188/*ARGSUSED*/
14189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014190f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014191 typval_T *argvars;
14192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193{
14194 static int x = 'A';
14195
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014196 rettv->v_type = VAR_STRING;
14197 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198
14199 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14200 * names. Skip 'I' and 'O', they are used for shell redirection. */
14201 do
14202 {
14203 if (x == 'Z')
14204 x = '0';
14205 else if (x == '9')
14206 x = 'A';
14207 else
14208 {
14209#ifdef EBCDIC
14210 if (x == 'I')
14211 x = 'J';
14212 else if (x == 'R')
14213 x = 'S';
14214 else
14215#endif
14216 ++x;
14217 }
14218 } while (x == 'I' || x == 'O');
14219}
14220
14221/*
14222 * "tolower(string)" function
14223 */
14224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014225f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014226 typval_T *argvars;
14227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228{
14229 char_u *p;
14230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014231 p = vim_strsave(get_tv_string(&argvars[0]));
14232 rettv->v_type = VAR_STRING;
14233 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234
14235 if (p != NULL)
14236 while (*p != NUL)
14237 {
14238#ifdef FEAT_MBYTE
14239 int l;
14240
14241 if (enc_utf8)
14242 {
14243 int c, lc;
14244
14245 c = utf_ptr2char(p);
14246 lc = utf_tolower(c);
14247 l = utf_ptr2len_check(p);
14248 /* TODO: reallocate string when byte count changes. */
14249 if (utf_char2len(lc) == l)
14250 utf_char2bytes(lc, p);
14251 p += l;
14252 }
14253 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
14254 p += l; /* skip multi-byte character */
14255 else
14256#endif
14257 {
14258 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14259 ++p;
14260 }
14261 }
14262}
14263
14264/*
14265 * "toupper(string)" function
14266 */
14267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014268f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014269 typval_T *argvars;
14270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014272 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014273 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274}
14275
14276/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014277 * "tr(string, fromstr, tostr)" function
14278 */
14279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014280f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014281 typval_T *argvars;
14282 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014283{
14284 char_u *instr;
14285 char_u *fromstr;
14286 char_u *tostr;
14287 char_u *p;
14288#ifdef FEAT_MBYTE
14289 int inlen;
14290 int fromlen;
14291 int tolen;
14292 int idx;
14293 char_u *cpstr;
14294 int cplen;
14295 int first = TRUE;
14296#endif
14297 char_u buf[NUMBUFLEN];
14298 char_u buf2[NUMBUFLEN];
14299 garray_T ga;
14300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014301 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014302 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14303 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014304
14305 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014306 rettv->v_type = VAR_STRING;
14307 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014308 if (fromstr == NULL || tostr == NULL)
14309 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014310 ga_init2(&ga, (int)sizeof(char), 80);
14311
14312#ifdef FEAT_MBYTE
14313 if (!has_mbyte)
14314#endif
14315 /* not multi-byte: fromstr and tostr must be the same length */
14316 if (STRLEN(fromstr) != STRLEN(tostr))
14317 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014318#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014319error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014320#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014321 EMSG2(_(e_invarg2), fromstr);
14322 ga_clear(&ga);
14323 return;
14324 }
14325
14326 /* fromstr and tostr have to contain the same number of chars */
14327 while (*instr != NUL)
14328 {
14329#ifdef FEAT_MBYTE
14330 if (has_mbyte)
14331 {
14332 inlen = mb_ptr2len_check(instr);
14333 cpstr = instr;
14334 cplen = inlen;
14335 idx = 0;
14336 for (p = fromstr; *p != NUL; p += fromlen)
14337 {
14338 fromlen = mb_ptr2len_check(p);
14339 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14340 {
14341 for (p = tostr; *p != NUL; p += tolen)
14342 {
14343 tolen = mb_ptr2len_check(p);
14344 if (idx-- == 0)
14345 {
14346 cplen = tolen;
14347 cpstr = p;
14348 break;
14349 }
14350 }
14351 if (*p == NUL) /* tostr is shorter than fromstr */
14352 goto error;
14353 break;
14354 }
14355 ++idx;
14356 }
14357
14358 if (first && cpstr == instr)
14359 {
14360 /* Check that fromstr and tostr have the same number of
14361 * (multi-byte) characters. Done only once when a character
14362 * of instr doesn't appear in fromstr. */
14363 first = FALSE;
14364 for (p = tostr; *p != NUL; p += tolen)
14365 {
14366 tolen = mb_ptr2len_check(p);
14367 --idx;
14368 }
14369 if (idx != 0)
14370 goto error;
14371 }
14372
14373 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014374 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014375 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014376
14377 instr += inlen;
14378 }
14379 else
14380#endif
14381 {
14382 /* When not using multi-byte chars we can do it faster. */
14383 p = vim_strchr(fromstr, *instr);
14384 if (p != NULL)
14385 ga_append(&ga, tostr[p - fromstr]);
14386 else
14387 ga_append(&ga, *instr);
14388 ++instr;
14389 }
14390 }
14391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014392 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014393}
14394
14395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396 * "type(expr)" function
14397 */
14398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014399f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014400 typval_T *argvars;
14401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014403 int n;
14404
14405 switch (argvars[0].v_type)
14406 {
14407 case VAR_NUMBER: n = 0; break;
14408 case VAR_STRING: n = 1; break;
14409 case VAR_FUNC: n = 2; break;
14410 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014411 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014412 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14413 }
14414 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415}
14416
14417/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014418 * "values(dict)" function
14419 */
14420 static void
14421f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014422 typval_T *argvars;
14423 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014424{
14425 dict_list(argvars, rettv, 1);
14426}
14427
14428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 * "virtcol(string)" function
14430 */
14431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014432f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014433 typval_T *argvars;
14434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435{
14436 colnr_T vcol = 0;
14437 pos_T *fp;
14438
14439 fp = var2fpos(&argvars[0], FALSE);
14440 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14441 {
14442 getvvcol(curwin, fp, NULL, NULL, &vcol);
14443 ++vcol;
14444 }
14445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014446 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447}
14448
14449/*
14450 * "visualmode()" function
14451 */
14452/*ARGSUSED*/
14453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014454f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014455 typval_T *argvars;
14456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457{
14458#ifdef FEAT_VISUAL
14459 char_u str[2];
14460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462 str[0] = curbuf->b_visual_mode_eval;
14463 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014464 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465
14466 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014467 if ((argvars[0].v_type == VAR_NUMBER
14468 && argvars[0].vval.v_number != 0)
14469 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014470 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 curbuf->b_visual_mode_eval = NUL;
14472#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014473 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474#endif
14475}
14476
14477/*
14478 * "winbufnr(nr)" function
14479 */
14480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014481f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014482 typval_T *argvars;
14483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484{
14485 win_T *wp;
14486
14487 wp = find_win_by_nr(&argvars[0]);
14488 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014489 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014491 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492}
14493
14494/*
14495 * "wincol()" function
14496 */
14497/*ARGSUSED*/
14498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014499f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014500 typval_T *argvars;
14501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502{
14503 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014504 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505}
14506
14507/*
14508 * "winheight(nr)" function
14509 */
14510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014511f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014512 typval_T *argvars;
14513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514{
14515 win_T *wp;
14516
14517 wp = find_win_by_nr(&argvars[0]);
14518 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014519 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014521 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522}
14523
14524/*
14525 * "winline()" function
14526 */
14527/*ARGSUSED*/
14528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014529f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014530 typval_T *argvars;
14531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532{
14533 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014534 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535}
14536
14537/*
14538 * "winnr()" function
14539 */
14540/* ARGSUSED */
14541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014542f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014543 typval_T *argvars;
14544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545{
14546 int nr = 1;
14547#ifdef FEAT_WINDOWS
14548 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014549 win_T *twin = curwin;
14550 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014552 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014553 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014554 arg = get_tv_string_chk(&argvars[0]);
14555 if (arg == NULL)
14556 nr = 0; /* type error; errmsg already given */
14557 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014558 twin = lastwin;
14559 else if (STRCMP(arg, "#") == 0)
14560 {
14561 twin = prevwin;
14562 if (prevwin == NULL)
14563 nr = 0;
14564 }
14565 else
14566 {
14567 EMSG2(_(e_invexpr2), arg);
14568 nr = 0;
14569 }
14570 }
14571
14572 if (nr > 0)
14573 for (wp = firstwin; wp != twin; wp = wp->w_next)
14574 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014576 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577}
14578
14579/*
14580 * "winrestcmd()" function
14581 */
14582/* ARGSUSED */
14583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014584f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014585 typval_T *argvars;
14586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587{
14588#ifdef FEAT_WINDOWS
14589 win_T *wp;
14590 int winnr = 1;
14591 garray_T ga;
14592 char_u buf[50];
14593
14594 ga_init2(&ga, (int)sizeof(char), 70);
14595 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14596 {
14597 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14598 ga_concat(&ga, buf);
14599# ifdef FEAT_VERTSPLIT
14600 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14601 ga_concat(&ga, buf);
14602# endif
14603 ++winnr;
14604 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014605 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014607 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014609 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014611 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612}
14613
14614/*
14615 * "winwidth(nr)" function
14616 */
14617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014618f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014619 typval_T *argvars;
14620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621{
14622 win_T *wp;
14623
14624 wp = find_win_by_nr(&argvars[0]);
14625 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014626 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627 else
14628#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014629 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014631 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632#endif
14633}
14634
14635 static win_T *
14636find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014637 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014638{
14639#ifdef FEAT_WINDOWS
14640 win_T *wp;
14641#endif
14642 int nr;
14643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014644 nr = get_tv_number_chk(vp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014645
14646#ifdef FEAT_WINDOWS
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014647 if (nr < 0)
14648 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649 if (nr == 0)
14650 return curwin;
14651
14652 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14653 if (--nr <= 0)
14654 break;
14655 return wp;
14656#else
14657 if (nr == 0 || nr == 1)
14658 return curwin;
14659 return NULL;
14660#endif
14661}
14662
14663/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014664 * "writefile()" function
14665 */
14666 static void
14667f_writefile(argvars, rettv)
14668 typval_T *argvars;
14669 typval_T *rettv;
14670{
14671 int binary = FALSE;
14672 char_u *fname;
14673 FILE *fd;
14674 listitem_T *li;
14675 char_u *s;
14676 int ret = 0;
14677 int c;
14678
14679 if (argvars[0].v_type != VAR_LIST)
14680 {
14681 EMSG2(_(e_listarg), "writefile()");
14682 return;
14683 }
14684 if (argvars[0].vval.v_list == NULL)
14685 return;
14686
14687 if (argvars[2].v_type != VAR_UNKNOWN
14688 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
14689 binary = TRUE;
14690
14691 /* Always open the file in binary mode, library functions have a mind of
14692 * their own about CR-LF conversion. */
14693 fname = get_tv_string(&argvars[1]);
14694 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
14695 {
14696 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14697 ret = -1;
14698 }
14699 else
14700 {
14701 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
14702 li = li->li_next)
14703 {
14704 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
14705 {
14706 if (*s == '\n')
14707 c = putc(NUL, fd);
14708 else
14709 c = putc(*s, fd);
14710 if (c == EOF)
14711 {
14712 ret = -1;
14713 break;
14714 }
14715 }
14716 if (!binary || li->li_next != NULL)
14717 if (putc('\n', fd) == EOF)
14718 {
14719 ret = -1;
14720 break;
14721 }
14722 if (ret < 0)
14723 {
14724 EMSG(_(e_write));
14725 break;
14726 }
14727 }
14728 fclose(fd);
14729 }
14730
14731 rettv->vval.v_number = ret;
14732}
14733
14734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735 * Translate a String variable into a position.
14736 */
14737 static pos_T *
14738var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000014739 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740 int lnum; /* TRUE when $ is last line */
14741{
14742 char_u *name;
14743 static pos_T pos;
14744 pos_T *pp;
14745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014746 name = get_tv_string_chk(varp);
14747 if (name == NULL)
14748 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 if (name[0] == '.') /* cursor */
14750 return &curwin->w_cursor;
14751 if (name[0] == '\'') /* mark */
14752 {
14753 pp = getmark(name[1], FALSE);
14754 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
14755 return NULL;
14756 return pp;
14757 }
14758 if (name[0] == '$') /* last column or line */
14759 {
14760 if (lnum)
14761 {
14762 pos.lnum = curbuf->b_ml.ml_line_count;
14763 pos.col = 0;
14764 }
14765 else
14766 {
14767 pos.lnum = curwin->w_cursor.lnum;
14768 pos.col = (colnr_T)STRLEN(ml_get_curline());
14769 }
14770 return &pos;
14771 }
14772 return NULL;
14773}
14774
14775/*
14776 * Get the length of an environment variable name.
14777 * Advance "arg" to the first character after the name.
14778 * Return 0 for error.
14779 */
14780 static int
14781get_env_len(arg)
14782 char_u **arg;
14783{
14784 char_u *p;
14785 int len;
14786
14787 for (p = *arg; vim_isIDc(*p); ++p)
14788 ;
14789 if (p == *arg) /* no name found */
14790 return 0;
14791
14792 len = (int)(p - *arg);
14793 *arg = p;
14794 return len;
14795}
14796
14797/*
14798 * Get the length of the name of a function or internal variable.
14799 * "arg" is advanced to the first non-white character after the name.
14800 * Return 0 if something is wrong.
14801 */
14802 static int
14803get_id_len(arg)
14804 char_u **arg;
14805{
14806 char_u *p;
14807 int len;
14808
14809 /* Find the end of the name. */
14810 for (p = *arg; eval_isnamec(*p); ++p)
14811 ;
14812 if (p == *arg) /* no name found */
14813 return 0;
14814
14815 len = (int)(p - *arg);
14816 *arg = skipwhite(p);
14817
14818 return len;
14819}
14820
14821/*
Bram Moolenaara7043832005-01-21 11:56:39 +000014822 * Get the length of the name of a variable or function.
14823 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014825 * Return -1 if curly braces expansion failed.
14826 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827 * If the name contains 'magic' {}'s, expand them and return the
14828 * expanded name in an allocated string via 'alias' - caller must free.
14829 */
14830 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014831get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832 char_u **arg;
14833 char_u **alias;
14834 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014835 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836{
14837 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838 char_u *p;
14839 char_u *expr_start;
14840 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841
14842 *alias = NULL; /* default to no alias */
14843
14844 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
14845 && (*arg)[2] == (int)KE_SNR)
14846 {
14847 /* hard coded <SNR>, already translated */
14848 *arg += 3;
14849 return get_id_len(arg) + 3;
14850 }
14851 len = eval_fname_script(*arg);
14852 if (len > 0)
14853 {
14854 /* literal "<SID>", "s:" or "<SNR>" */
14855 *arg += len;
14856 }
14857
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014859 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014861 p = find_name_end(*arg, &expr_start, &expr_end,
14862 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863 if (expr_start != NULL)
14864 {
14865 char_u *temp_string;
14866
14867 if (!evaluate)
14868 {
14869 len += (int)(p - *arg);
14870 *arg = skipwhite(p);
14871 return len;
14872 }
14873
14874 /*
14875 * Include any <SID> etc in the expanded string:
14876 * Thus the -len here.
14877 */
14878 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
14879 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014880 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881 *alias = temp_string;
14882 *arg = skipwhite(p);
14883 return (int)STRLEN(temp_string);
14884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885
14886 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014887 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888 EMSG2(_(e_invexpr2), *arg);
14889
14890 return len;
14891}
14892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014893/*
14894 * Find the end of a variable or function name, taking care of magic braces.
14895 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
14896 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014897 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014898 * Return a pointer to just after the name. Equal to "arg" if there is no
14899 * valid name.
14900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014902find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014903 char_u *arg;
14904 char_u **expr_start;
14905 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014906 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014908 int mb_nest = 0;
14909 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910 char_u *p;
14911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014912 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014913 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014914 *expr_start = NULL;
14915 *expr_end = NULL;
14916 }
14917
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014918 /* Quick check for valid starting character. */
14919 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
14920 return arg;
14921
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014922 for (p = arg; *p != NUL
14923 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014924 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014925 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014926 || mb_nest != 0
14927 || br_nest != 0); ++p)
14928 {
14929 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014931 if (*p == '[')
14932 ++br_nest;
14933 else if (*p == ']')
14934 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014936 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014938 if (*p == '{')
14939 {
14940 mb_nest++;
14941 if (expr_start != NULL && *expr_start == NULL)
14942 *expr_start = p;
14943 }
14944 else if (*p == '}')
14945 {
14946 mb_nest--;
14947 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
14948 *expr_end = p;
14949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951 }
14952
14953 return p;
14954}
14955
14956/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014957 * Expands out the 'magic' {}'s in a variable/function name.
14958 * Note that this can call itself recursively, to deal with
14959 * constructs like foo{bar}{baz}{bam}
14960 * The four pointer arguments point to "foo{expre}ss{ion}bar"
14961 * "in_start" ^
14962 * "expr_start" ^
14963 * "expr_end" ^
14964 * "in_end" ^
14965 *
14966 * Returns a new allocated string, which the caller must free.
14967 * Returns NULL for failure.
14968 */
14969 static char_u *
14970make_expanded_name(in_start, expr_start, expr_end, in_end)
14971 char_u *in_start;
14972 char_u *expr_start;
14973 char_u *expr_end;
14974 char_u *in_end;
14975{
14976 char_u c1;
14977 char_u *retval = NULL;
14978 char_u *temp_result;
14979 char_u *nextcmd = NULL;
14980
14981 if (expr_end == NULL || in_end == NULL)
14982 return NULL;
14983 *expr_start = NUL;
14984 *expr_end = NUL;
14985 c1 = *in_end;
14986 *in_end = NUL;
14987
14988 temp_result = eval_to_string(expr_start + 1, &nextcmd);
14989 if (temp_result != NULL && nextcmd == NULL)
14990 {
14991 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
14992 + (in_end - expr_end) + 1));
14993 if (retval != NULL)
14994 {
14995 STRCPY(retval, in_start);
14996 STRCAT(retval, temp_result);
14997 STRCAT(retval, expr_end + 1);
14998 }
14999 }
15000 vim_free(temp_result);
15001
15002 *in_end = c1; /* put char back for error messages */
15003 *expr_start = '{';
15004 *expr_end = '}';
15005
15006 if (retval != NULL)
15007 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015008 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015009 if (expr_start != NULL)
15010 {
15011 /* Further expansion! */
15012 temp_result = make_expanded_name(retval, expr_start,
15013 expr_end, temp_result);
15014 vim_free(retval);
15015 retval = temp_result;
15016 }
15017 }
15018
15019 return retval;
15020}
15021
15022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015024 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025 */
15026 static int
15027eval_isnamec(c)
15028 int c;
15029{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015030 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15031}
15032
15033/*
15034 * Return TRUE if character "c" can be used as the first character in a
15035 * variable or function name (excluding '{' and '}').
15036 */
15037 static int
15038eval_isnamec1(c)
15039 int c;
15040{
15041 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042}
15043
15044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045 * Set number v: variable to "val".
15046 */
15047 void
15048set_vim_var_nr(idx, val)
15049 int idx;
15050 long val;
15051{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015052 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015053}
15054
15055/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015056 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057 */
15058 long
15059get_vim_var_nr(idx)
15060 int idx;
15061{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015062 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015063}
15064
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015065#if defined(FEAT_AUTOCMD) || defined(PROTO)
15066/*
15067 * Get string v: variable value. Uses a static buffer, can only be used once.
15068 */
15069 char_u *
15070get_vim_var_str(idx)
15071 int idx;
15072{
15073 return get_tv_string(&vimvars[idx].vv_tv);
15074}
15075#endif
15076
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077/*
15078 * Set v:count, v:count1 and v:prevcount.
15079 */
15080 void
15081set_vcount(count, count1)
15082 long count;
15083 long count1;
15084{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015085 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15086 vimvars[VV_COUNT].vv_nr = count;
15087 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088}
15089
15090/*
15091 * Set string v: variable to a copy of "val".
15092 */
15093 void
15094set_vim_var_string(idx, val, len)
15095 int idx;
15096 char_u *val;
15097 int len; /* length of "val" to use or -1 (whole string) */
15098{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015099 /* Need to do this (at least) once, since we can't initialize a union.
15100 * Will always be invoked when "v:progname" is set. */
15101 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15102
Bram Moolenaare9a41262005-01-15 22:18:47 +000015103 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015104 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015105 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015107 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015109 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110}
15111
15112/*
15113 * Set v:register if needed.
15114 */
15115 void
15116set_reg_var(c)
15117 int c;
15118{
15119 char_u regname;
15120
15121 if (c == 0 || c == ' ')
15122 regname = '"';
15123 else
15124 regname = c;
15125 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015126 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127 set_vim_var_string(VV_REG, &regname, 1);
15128}
15129
15130/*
15131 * Get or set v:exception. If "oldval" == NULL, return the current value.
15132 * Otherwise, restore the value to "oldval" and return NULL.
15133 * Must always be called in pairs to save and restore v:exception! Does not
15134 * take care of memory allocations.
15135 */
15136 char_u *
15137v_exception(oldval)
15138 char_u *oldval;
15139{
15140 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015141 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142
Bram Moolenaare9a41262005-01-15 22:18:47 +000015143 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144 return NULL;
15145}
15146
15147/*
15148 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15149 * Otherwise, restore the value to "oldval" and return NULL.
15150 * Must always be called in pairs to save and restore v:throwpoint! Does not
15151 * take care of memory allocations.
15152 */
15153 char_u *
15154v_throwpoint(oldval)
15155 char_u *oldval;
15156{
15157 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015158 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159
Bram Moolenaare9a41262005-01-15 22:18:47 +000015160 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161 return NULL;
15162}
15163
15164#if defined(FEAT_AUTOCMD) || defined(PROTO)
15165/*
15166 * Set v:cmdarg.
15167 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15168 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15169 * Must always be called in pairs!
15170 */
15171 char_u *
15172set_cmdarg(eap, oldarg)
15173 exarg_T *eap;
15174 char_u *oldarg;
15175{
15176 char_u *oldval;
15177 char_u *newval;
15178 unsigned len;
15179
Bram Moolenaare9a41262005-01-15 22:18:47 +000015180 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015181 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015183 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015184 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015185 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186 }
15187
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015188 if (eap->force_bin == FORCE_BIN)
15189 len = 6;
15190 else if (eap->force_bin == FORCE_NOBIN)
15191 len = 8;
15192 else
15193 len = 0;
15194 if (eap->force_ff != 0)
15195 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15196# ifdef FEAT_MBYTE
15197 if (eap->force_enc != 0)
15198 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15199# endif
15200
15201 newval = alloc(len + 1);
15202 if (newval == NULL)
15203 return NULL;
15204
15205 if (eap->force_bin == FORCE_BIN)
15206 sprintf((char *)newval, " ++bin");
15207 else if (eap->force_bin == FORCE_NOBIN)
15208 sprintf((char *)newval, " ++nobin");
15209 else
15210 *newval = NUL;
15211 if (eap->force_ff != 0)
15212 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15213 eap->cmd + eap->force_ff);
15214# ifdef FEAT_MBYTE
15215 if (eap->force_enc != 0)
15216 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15217 eap->cmd + eap->force_enc);
15218# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015219 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015220 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221}
15222#endif
15223
15224/*
15225 * Get the value of internal variable "name".
15226 * Return OK or FAIL.
15227 */
15228 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015229get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230 char_u *name;
15231 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015232 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015233 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015234{
15235 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015236 typval_T *tv = NULL;
15237 typval_T atv;
15238 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015239 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240
15241 /* truncate the name, so that we can use strcmp() */
15242 cc = name[len];
15243 name[len] = NUL;
15244
15245 /*
15246 * Check for "b:changedtick".
15247 */
15248 if (STRCMP(name, "b:changedtick") == 0)
15249 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015250 atv.v_type = VAR_NUMBER;
15251 atv.vval.v_number = curbuf->b_changedtick;
15252 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015253 }
15254
15255 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256 * Check for user-defined variables.
15257 */
15258 else
15259 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015260 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015262 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263 }
15264
Bram Moolenaare9a41262005-01-15 22:18:47 +000015265 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015266 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015267 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015268 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269 ret = FAIL;
15270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015271 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015272 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273
15274 name[len] = cc;
15275
15276 return ret;
15277}
15278
15279/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015280 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15281 * Also handle function call with Funcref variable: func(expr)
15282 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15283 */
15284 static int
15285handle_subscript(arg, rettv, evaluate, verbose)
15286 char_u **arg;
15287 typval_T *rettv;
15288 int evaluate; /* do more than finding the end */
15289 int verbose; /* give error messages */
15290{
15291 int ret = OK;
15292 dict_T *selfdict = NULL;
15293 char_u *s;
15294 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015295 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015296
15297 while (ret == OK
15298 && (**arg == '['
15299 || (**arg == '.' && rettv->v_type == VAR_DICT)
15300 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15301 && !vim_iswhite(*(*arg - 1)))
15302 {
15303 if (**arg == '(')
15304 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015305 /* need to copy the funcref so that we can clear rettv */
15306 functv = *rettv;
15307 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015308
15309 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015310 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015311 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015312 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15313 &len, evaluate, selfdict);
15314
15315 /* Clear the funcref afterwards, so that deleting it while
15316 * evaluating the arguments is possible (see test55). */
15317 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015318
15319 /* Stop the expression evaluation when immediately aborting on
15320 * error, or when an interrupt occurred or an exception was thrown
15321 * but not caught. */
15322 if (aborting())
15323 {
15324 if (ret == OK)
15325 clear_tv(rettv);
15326 ret = FAIL;
15327 }
15328 dict_unref(selfdict);
15329 selfdict = NULL;
15330 }
15331 else /* **arg == '[' || **arg == '.' */
15332 {
15333 dict_unref(selfdict);
15334 if (rettv->v_type == VAR_DICT)
15335 {
15336 selfdict = rettv->vval.v_dict;
15337 if (selfdict != NULL)
15338 ++selfdict->dv_refcount;
15339 }
15340 else
15341 selfdict = NULL;
15342 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15343 {
15344 clear_tv(rettv);
15345 ret = FAIL;
15346 }
15347 }
15348 }
15349 dict_unref(selfdict);
15350 return ret;
15351}
15352
15353/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015354 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15355 * value).
15356 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015357 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015358alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015359{
Bram Moolenaar33570922005-01-25 22:26:29 +000015360 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015361}
15362
15363/*
15364 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 * The string "s" must have been allocated, it is consumed.
15366 * Return NULL for out of memory, the variable otherwise.
15367 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015368 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015369alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370 char_u *s;
15371{
Bram Moolenaar33570922005-01-25 22:26:29 +000015372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015374 rettv = alloc_tv();
15375 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015377 rettv->v_type = VAR_STRING;
15378 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379 }
15380 else
15381 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015382 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383}
15384
15385/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015386 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 */
15388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015389free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015390 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391{
15392 if (varp != NULL)
15393 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015394 switch (varp->v_type)
15395 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015396 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015397 func_unref(varp->vval.v_string);
15398 /*FALLTHROUGH*/
15399 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015400 vim_free(varp->vval.v_string);
15401 break;
15402 case VAR_LIST:
15403 list_unref(varp->vval.v_list);
15404 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015405 case VAR_DICT:
15406 dict_unref(varp->vval.v_dict);
15407 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015408 case VAR_NUMBER:
15409 case VAR_UNKNOWN:
15410 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015411 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015412 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015413 break;
15414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415 vim_free(varp);
15416 }
15417}
15418
15419/*
15420 * Free the memory for a variable value and set the value to NULL or 0.
15421 */
15422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015423clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015424 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425{
15426 if (varp != NULL)
15427 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015428 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015430 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015431 func_unref(varp->vval.v_string);
15432 /*FALLTHROUGH*/
15433 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015434 vim_free(varp->vval.v_string);
15435 varp->vval.v_string = NULL;
15436 break;
15437 case VAR_LIST:
15438 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015439 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015440 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015441 case VAR_DICT:
15442 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015443 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015444 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015445 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015446 varp->vval.v_number = 0;
15447 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015448 case VAR_UNKNOWN:
15449 break;
15450 default:
15451 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015453 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454 }
15455}
15456
15457/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015458 * Set the value of a variable to NULL without freeing items.
15459 */
15460 static void
15461init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015462 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015463{
15464 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015465 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015466}
15467
15468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015469 * Get the number value of a variable.
15470 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015471 * For incompatible types, return 0.
15472 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15473 * caller of incompatible types: it sets *denote to TRUE if "denote"
15474 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015475 */
15476 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015477get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015478 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015480 int error = FALSE;
15481
15482 return get_tv_number_chk(varp, &error); /* return 0L on error */
15483}
15484
15485 static long
15486get_tv_number_chk(varp, denote)
15487 typval_T *varp;
15488 int *denote;
15489{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015490 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015492 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015493 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015494 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015495 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015496 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015497 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015498 break;
15499 case VAR_STRING:
15500 if (varp->vval.v_string != NULL)
15501 vim_str2nr(varp->vval.v_string, NULL, NULL,
15502 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015503 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015504 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015505 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015506 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015507 case VAR_DICT:
15508 EMSG(_("E728: Using a Dictionary as a number"));
15509 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015510 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015511 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015512 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015514 if (denote == NULL) /* useful for values that must be unsigned */
15515 n = -1;
15516 else
15517 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015518 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519}
15520
15521/*
15522 * Get the lnum from the first argument. Also accepts ".", "$", etc.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015523 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524 */
15525 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015526get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015527 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528{
Bram Moolenaar33570922005-01-25 22:26:29 +000015529 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530 linenr_T lnum;
15531
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015532 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533 if (lnum == 0) /* no valid number, try using line() */
15534 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015535 rettv.v_type = VAR_NUMBER;
15536 f_line(argvars, &rettv);
15537 lnum = rettv.vval.v_number;
15538 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539 }
15540 return lnum;
15541}
15542
15543/*
15544 * Get the string value of a variable.
15545 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015546 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15547 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548 * If the String variable has never been set, return an empty string.
15549 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015550 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15551 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552 */
15553 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015554get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015555 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556{
15557 static char_u mybuf[NUMBUFLEN];
15558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015559 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560}
15561
15562 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015563get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015564 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015565 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015567 char_u *res = get_tv_string_buf_chk(varp, buf);
15568
15569 return res != NULL ? res : (char_u *)"";
15570}
15571
15572 static char_u *
15573get_tv_string_chk(varp)
15574 typval_T *varp;
15575{
15576 static char_u mybuf[NUMBUFLEN];
15577
15578 return get_tv_string_buf_chk(varp, mybuf);
15579}
15580
15581 static char_u *
15582get_tv_string_buf_chk(varp, buf)
15583 typval_T *varp;
15584 char_u *buf;
15585{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015586 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015588 case VAR_NUMBER:
15589 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15590 return buf;
15591 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015592 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015593 break;
15594 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015595 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015596 break;
15597 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015598 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015599 break;
15600 case VAR_STRING:
15601 if (varp->vval.v_string != NULL)
15602 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015603 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015604 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015605 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015606 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015608 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609}
15610
15611/*
15612 * Find variable "name" in the list of variables.
15613 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015614 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015615 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015616 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015618 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015619find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015621 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015624 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625
Bram Moolenaara7043832005-01-21 11:56:39 +000015626 ht = find_var_ht(name, &varname);
15627 if (htp != NULL)
15628 *htp = ht;
15629 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015631 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632}
15633
15634/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015635 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000015636 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015638 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015639find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000015640 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000015641 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015642 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000015643{
Bram Moolenaar33570922005-01-25 22:26:29 +000015644 hashitem_T *hi;
15645
15646 if (*varname == NUL)
15647 {
15648 /* Must be something like "s:", otherwise "ht" would be NULL. */
15649 switch (varname[-2])
15650 {
15651 case 's': return &SCRIPT_SV(current_SID).sv_var;
15652 case 'g': return &globvars_var;
15653 case 'v': return &vimvars_var;
15654 case 'b': return &curbuf->b_bufvar;
15655 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015656 case 'l': return current_funccal == NULL
15657 ? NULL : &current_funccal->l_vars_var;
15658 case 'a': return current_funccal == NULL
15659 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000015660 }
15661 return NULL;
15662 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015663
15664 hi = hash_find(ht, varname);
15665 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015666 {
15667 /* For global variables we may try auto-loading the script. If it
15668 * worked find the variable again. */
15669 if (ht == &globvarht && !writing
15670 && script_autoload(varname) && !aborting())
15671 hi = hash_find(ht, varname);
15672 if (HASHITEM_EMPTY(hi))
15673 return NULL;
15674 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015675 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015676}
15677
15678/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015679 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015680 * Set "varname" to the start of name without ':'.
15681 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015682 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015683find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684 char_u *name;
15685 char_u **varname;
15686{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015687 hashitem_T *hi;
15688
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689 if (name[1] != ':')
15690 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015691 /* The name must not start with a colon or #. */
15692 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693 return NULL;
15694 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015695
15696 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015697 hi = hash_find(&compat_hashtab, name);
15698 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000015699 return &compat_hashtab;
15700
Bram Moolenaar071d4272004-06-13 20:20:40 +000015701 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015702 return &globvarht; /* global variable */
15703 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704 }
15705 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015706 if (*name == 'g') /* global variable */
15707 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015708 /* There must be no ':' or '#' in the rest of the name, unless g: is used
15709 */
15710 if (vim_strchr(name + 2, ':') != NULL
15711 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015712 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015714 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015716 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000015717 if (*name == 'v') /* v: variable */
15718 return &vimvarht;
15719 if (*name == 'a' && current_funccal != NULL) /* function argument */
15720 return &current_funccal->l_avars.dv_hashtab;
15721 if (*name == 'l' && current_funccal != NULL) /* local function variable */
15722 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723 if (*name == 's' /* script variable */
15724 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
15725 return &SCRIPT_VARS(current_SID);
15726 return NULL;
15727}
15728
15729/*
15730 * Get the string value of a (global/local) variable.
15731 * Returns NULL when it doesn't exist.
15732 */
15733 char_u *
15734get_var_value(name)
15735 char_u *name;
15736{
Bram Moolenaar33570922005-01-25 22:26:29 +000015737 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738
Bram Moolenaara7043832005-01-21 11:56:39 +000015739 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740 if (v == NULL)
15741 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015742 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743}
15744
15745/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015746 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 * sourcing this script and when executing functions defined in the script.
15748 */
15749 void
15750new_script_vars(id)
15751 scid_T id;
15752{
Bram Moolenaara7043832005-01-21 11:56:39 +000015753 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000015754 hashtab_T *ht;
15755 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000015756
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
15758 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015759 /* Re-allocating ga_data means that an ht_array pointing to
15760 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000015761 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000015762 for (i = 1; i <= ga_scripts.ga_len; ++i)
15763 {
15764 ht = &SCRIPT_VARS(i);
15765 if (ht->ht_mask == HT_INIT_SIZE - 1)
15766 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000015767 sv = &SCRIPT_SV(i);
15768 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000015769 }
15770
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771 while (ga_scripts.ga_len < id)
15772 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015773 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
15774 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776 }
15777 }
15778}
15779
15780/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015781 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
15782 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783 */
15784 void
Bram Moolenaar33570922005-01-25 22:26:29 +000015785init_var_dict(dict, dict_var)
15786 dict_T *dict;
15787 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788{
Bram Moolenaar33570922005-01-25 22:26:29 +000015789 hash_init(&dict->dv_hashtab);
15790 dict->dv_refcount = 99999;
15791 dict_var->di_tv.vval.v_dict = dict;
15792 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015793 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015794 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15795 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796}
15797
15798/*
15799 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000015800 * Frees all allocated variables and the value they contain.
15801 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802 */
15803 void
Bram Moolenaara7043832005-01-21 11:56:39 +000015804vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000015805 hashtab_T *ht;
15806{
15807 vars_clear_ext(ht, TRUE);
15808}
15809
15810/*
15811 * Like vars_clear(), but only free the value if "free_val" is TRUE.
15812 */
15813 static void
15814vars_clear_ext(ht, free_val)
15815 hashtab_T *ht;
15816 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817{
Bram Moolenaara7043832005-01-21 11:56:39 +000015818 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000015819 hashitem_T *hi;
15820 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821
Bram Moolenaar33570922005-01-25 22:26:29 +000015822 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000015823 todo = ht->ht_used;
15824 for (hi = ht->ht_array; todo > 0; ++hi)
15825 {
15826 if (!HASHITEM_EMPTY(hi))
15827 {
15828 --todo;
15829
Bram Moolenaar33570922005-01-25 22:26:29 +000015830 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000015831 * ht_array might change then. hash_clear() takes care of it
15832 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015833 v = HI2DI(hi);
15834 if (free_val)
15835 clear_tv(&v->di_tv);
15836 if ((v->di_flags & DI_FLAGS_FIX) == 0)
15837 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000015838 }
15839 }
15840 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015841 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842}
15843
Bram Moolenaara7043832005-01-21 11:56:39 +000015844/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015845 * Delete a variable from hashtab "ht" at item "hi".
15846 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000015847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000015849delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000015850 hashtab_T *ht;
15851 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852{
Bram Moolenaar33570922005-01-25 22:26:29 +000015853 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015854
15855 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000015856 clear_tv(&di->di_tv);
15857 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858}
15859
15860/*
15861 * List the value of one internal variable.
15862 */
15863 static void
15864list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000015865 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866 char_u *prefix;
15867{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015868 char_u *tofree;
15869 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015870 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015871
Bram Moolenaar33570922005-01-25 22:26:29 +000015872 s = echo_string(&v->di_tv, &tofree, numbuf);
15873 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015874 s == NULL ? (char_u *)"" : s);
15875 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876}
15877
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878 static void
15879list_one_var_a(prefix, name, type, string)
15880 char_u *prefix;
15881 char_u *name;
15882 int type;
15883 char_u *string;
15884{
15885 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
15886 if (name != NULL) /* "a:" vars don't have a name stored */
15887 msg_puts(name);
15888 msg_putchar(' ');
15889 msg_advance(22);
15890 if (type == VAR_NUMBER)
15891 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015892 else if (type == VAR_FUNC)
15893 msg_putchar('*');
15894 else if (type == VAR_LIST)
15895 {
15896 msg_putchar('[');
15897 if (*string == '[')
15898 ++string;
15899 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015900 else if (type == VAR_DICT)
15901 {
15902 msg_putchar('{');
15903 if (*string == '{')
15904 ++string;
15905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906 else
15907 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015908
Bram Moolenaar071d4272004-06-13 20:20:40 +000015909 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015910
15911 if (type == VAR_FUNC)
15912 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015913}
15914
15915/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015916 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015917 * If the variable already exists, the value is updated.
15918 * Otherwise the variable is created.
15919 */
15920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015921set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015923 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015924 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015925{
Bram Moolenaar33570922005-01-25 22:26:29 +000015926 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015927 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015928 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015929 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015931 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015932 {
15933 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
15934 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
15935 ? name[2] : name[0]))
15936 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015937 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015938 return;
15939 }
15940 if (function_exists(name))
15941 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015942 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015943 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015944 return;
15945 }
15946 }
15947
Bram Moolenaara7043832005-01-21 11:56:39 +000015948 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015949 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000015950 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000015951 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000015952 return;
15953 }
15954
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015955 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015956 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015957 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015958 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015959 if (var_check_ro(v->di_flags, name)
15960 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000015961 return;
15962 if (v->di_tv.v_type != tv->v_type
15963 && !((v->di_tv.v_type == VAR_STRING
15964 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015965 && (tv->v_type == VAR_STRING
15966 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015967 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015968 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015969 return;
15970 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015971
15972 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000015973 * Handle setting internal v: variables separately: we don't change
15974 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000015975 */
15976 if (ht == &vimvarht)
15977 {
15978 if (v->di_tv.v_type == VAR_STRING)
15979 {
15980 vim_free(v->di_tv.vval.v_string);
15981 if (copy || tv->v_type != VAR_STRING)
15982 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
15983 else
15984 {
15985 /* Take over the string to avoid an extra alloc/free. */
15986 v->di_tv.vval.v_string = tv->vval.v_string;
15987 tv->vval.v_string = NULL;
15988 }
15989 }
15990 else if (v->di_tv.v_type != VAR_NUMBER)
15991 EMSG2(_(e_intern2), "set_var()");
15992 else
15993 v->di_tv.vval.v_number = get_tv_number(tv);
15994 return;
15995 }
15996
15997 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998 }
15999 else /* add a new variable */
16000 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016001 /* Make sure the variable name is valid. */
16002 for (p = varname; *p != NUL; ++p)
16003 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16004 {
16005 EMSG2(_(e_illvar), varname);
16006 return;
16007 }
16008
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016009 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16010 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016011 if (v == NULL)
16012 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016013 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016014 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016016 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 return;
16018 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016019 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016022 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016023 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016024 else
16025 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016026 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016027 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016028 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030}
16031
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016032/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016033 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16034 * Also give an error message.
16035 */
16036 static int
16037var_check_ro(flags, name)
16038 int flags;
16039 char_u *name;
16040{
16041 if (flags & DI_FLAGS_RO)
16042 {
16043 EMSG2(_(e_readonlyvar), name);
16044 return TRUE;
16045 }
16046 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16047 {
16048 EMSG2(_(e_readonlysbx), name);
16049 return TRUE;
16050 }
16051 return FALSE;
16052}
16053
16054/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016055 * Return TRUE if typeval "tv" is set to be locked (immutable).
16056 * Also give an error message, using "name".
16057 */
16058 static int
16059tv_check_lock(lock, name)
16060 int lock;
16061 char_u *name;
16062{
16063 if (lock & VAR_LOCKED)
16064 {
16065 EMSG2(_("E741: Value is locked: %s"),
16066 name == NULL ? (char_u *)_("Unknown") : name);
16067 return TRUE;
16068 }
16069 if (lock & VAR_FIXED)
16070 {
16071 EMSG2(_("E742: Cannot change value of %s"),
16072 name == NULL ? (char_u *)_("Unknown") : name);
16073 return TRUE;
16074 }
16075 return FALSE;
16076}
16077
16078/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016079 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016080 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016081 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016084copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016085 typval_T *from;
16086 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016087{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016088 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016089 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016090 switch (from->v_type)
16091 {
16092 case VAR_NUMBER:
16093 to->vval.v_number = from->vval.v_number;
16094 break;
16095 case VAR_STRING:
16096 case VAR_FUNC:
16097 if (from->vval.v_string == NULL)
16098 to->vval.v_string = NULL;
16099 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016100 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016101 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016102 if (from->v_type == VAR_FUNC)
16103 func_ref(to->vval.v_string);
16104 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016105 break;
16106 case VAR_LIST:
16107 if (from->vval.v_list == NULL)
16108 to->vval.v_list = NULL;
16109 else
16110 {
16111 to->vval.v_list = from->vval.v_list;
16112 ++to->vval.v_list->lv_refcount;
16113 }
16114 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016115 case VAR_DICT:
16116 if (from->vval.v_dict == NULL)
16117 to->vval.v_dict = NULL;
16118 else
16119 {
16120 to->vval.v_dict = from->vval.v_dict;
16121 ++to->vval.v_dict->dv_refcount;
16122 }
16123 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016124 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016125 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016126 break;
16127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128}
16129
16130/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016131 * Make a copy of an item.
16132 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016133 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16134 * reference to an already copied list/dict can be used.
16135 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016136 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016137 static int
16138item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016139 typval_T *from;
16140 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016141 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016142 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016143{
16144 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016145 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016146
Bram Moolenaar33570922005-01-25 22:26:29 +000016147 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016148 {
16149 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016150 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016151 }
16152 ++recurse;
16153
16154 switch (from->v_type)
16155 {
16156 case VAR_NUMBER:
16157 case VAR_STRING:
16158 case VAR_FUNC:
16159 copy_tv(from, to);
16160 break;
16161 case VAR_LIST:
16162 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016163 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016164 if (from->vval.v_list == NULL)
16165 to->vval.v_list = NULL;
16166 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16167 {
16168 /* use the copy made earlier */
16169 to->vval.v_list = from->vval.v_list->lv_copylist;
16170 ++to->vval.v_list->lv_refcount;
16171 }
16172 else
16173 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16174 if (to->vval.v_list == NULL)
16175 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016176 break;
16177 case VAR_DICT:
16178 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016179 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016180 if (from->vval.v_dict == NULL)
16181 to->vval.v_dict = NULL;
16182 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16183 {
16184 /* use the copy made earlier */
16185 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16186 ++to->vval.v_dict->dv_refcount;
16187 }
16188 else
16189 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16190 if (to->vval.v_dict == NULL)
16191 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016192 break;
16193 default:
16194 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016195 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016196 }
16197 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016198 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016199}
16200
16201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202 * ":echo expr1 ..." print each argument separated with a space, add a
16203 * newline at the end.
16204 * ":echon expr1 ..." print each argument plain.
16205 */
16206 void
16207ex_echo(eap)
16208 exarg_T *eap;
16209{
16210 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016211 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016212 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213 char_u *p;
16214 int needclr = TRUE;
16215 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016216 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217
16218 if (eap->skip)
16219 ++emsg_skip;
16220 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16221 {
16222 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016223 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 {
16225 /*
16226 * Report the invalid expression unless the expression evaluation
16227 * has been cancelled due to an aborting error, an interrupt, or an
16228 * exception.
16229 */
16230 if (!aborting())
16231 EMSG2(_(e_invexpr2), p);
16232 break;
16233 }
16234 if (!eap->skip)
16235 {
16236 if (atstart)
16237 {
16238 atstart = FALSE;
16239 /* Call msg_start() after eval1(), evaluating the expression
16240 * may cause a message to appear. */
16241 if (eap->cmdidx == CMD_echo)
16242 msg_start();
16243 }
16244 else if (eap->cmdidx == CMD_echo)
16245 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016246 p = echo_string(&rettv, &tofree, numbuf);
16247 if (p != NULL)
16248 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016250 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016252 if (*p != TAB && needclr)
16253 {
16254 /* remove any text still there from the command */
16255 msg_clr_eos();
16256 needclr = FALSE;
16257 }
16258 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259 }
16260 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016261 {
16262#ifdef FEAT_MBYTE
16263 if (has_mbyte)
16264 {
16265 int i = (*mb_ptr2len_check)(p);
16266
16267 (void)msg_outtrans_len_attr(p, i, echo_attr);
16268 p += i - 1;
16269 }
16270 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016272 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016275 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016277 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278 arg = skipwhite(arg);
16279 }
16280 eap->nextcmd = check_nextcmd(arg);
16281
16282 if (eap->skip)
16283 --emsg_skip;
16284 else
16285 {
16286 /* remove text that may still be there from the command */
16287 if (needclr)
16288 msg_clr_eos();
16289 if (eap->cmdidx == CMD_echo)
16290 msg_end();
16291 }
16292}
16293
16294/*
16295 * ":echohl {name}".
16296 */
16297 void
16298ex_echohl(eap)
16299 exarg_T *eap;
16300{
16301 int id;
16302
16303 id = syn_name2id(eap->arg);
16304 if (id == 0)
16305 echo_attr = 0;
16306 else
16307 echo_attr = syn_id2attr(id);
16308}
16309
16310/*
16311 * ":execute expr1 ..." execute the result of an expression.
16312 * ":echomsg expr1 ..." Print a message
16313 * ":echoerr expr1 ..." Print an error
16314 * Each gets spaces around each argument and a newline at the end for
16315 * echo commands
16316 */
16317 void
16318ex_execute(eap)
16319 exarg_T *eap;
16320{
16321 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016322 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323 int ret = OK;
16324 char_u *p;
16325 garray_T ga;
16326 int len;
16327 int save_did_emsg;
16328
16329 ga_init2(&ga, 1, 80);
16330
16331 if (eap->skip)
16332 ++emsg_skip;
16333 while (*arg != NUL && *arg != '|' && *arg != '\n')
16334 {
16335 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016336 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 {
16338 /*
16339 * Report the invalid expression unless the expression evaluation
16340 * has been cancelled due to an aborting error, an interrupt, or an
16341 * exception.
16342 */
16343 if (!aborting())
16344 EMSG2(_(e_invexpr2), p);
16345 ret = FAIL;
16346 break;
16347 }
16348
16349 if (!eap->skip)
16350 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016351 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352 len = (int)STRLEN(p);
16353 if (ga_grow(&ga, len + 2) == FAIL)
16354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016355 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016356 ret = FAIL;
16357 break;
16358 }
16359 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362 ga.ga_len += len;
16363 }
16364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016365 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366 arg = skipwhite(arg);
16367 }
16368
16369 if (ret != FAIL && ga.ga_data != NULL)
16370 {
16371 if (eap->cmdidx == CMD_echomsg)
16372 MSG_ATTR(ga.ga_data, echo_attr);
16373 else if (eap->cmdidx == CMD_echoerr)
16374 {
16375 /* We don't want to abort following commands, restore did_emsg. */
16376 save_did_emsg = did_emsg;
16377 EMSG((char_u *)ga.ga_data);
16378 if (!force_abort)
16379 did_emsg = save_did_emsg;
16380 }
16381 else if (eap->cmdidx == CMD_execute)
16382 do_cmdline((char_u *)ga.ga_data,
16383 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16384 }
16385
16386 ga_clear(&ga);
16387
16388 if (eap->skip)
16389 --emsg_skip;
16390
16391 eap->nextcmd = check_nextcmd(arg);
16392}
16393
16394/*
16395 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16396 * "arg" points to the "&" or '+' when called, to "option" when returning.
16397 * Returns NULL when no option name found. Otherwise pointer to the char
16398 * after the option name.
16399 */
16400 static char_u *
16401find_option_end(arg, opt_flags)
16402 char_u **arg;
16403 int *opt_flags;
16404{
16405 char_u *p = *arg;
16406
16407 ++p;
16408 if (*p == 'g' && p[1] == ':')
16409 {
16410 *opt_flags = OPT_GLOBAL;
16411 p += 2;
16412 }
16413 else if (*p == 'l' && p[1] == ':')
16414 {
16415 *opt_flags = OPT_LOCAL;
16416 p += 2;
16417 }
16418 else
16419 *opt_flags = 0;
16420
16421 if (!ASCII_ISALPHA(*p))
16422 return NULL;
16423 *arg = p;
16424
16425 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16426 p += 4; /* termcap option */
16427 else
16428 while (ASCII_ISALPHA(*p))
16429 ++p;
16430 return p;
16431}
16432
16433/*
16434 * ":function"
16435 */
16436 void
16437ex_function(eap)
16438 exarg_T *eap;
16439{
16440 char_u *theline;
16441 int j;
16442 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 char_u *name = NULL;
16445 char_u *p;
16446 char_u *arg;
16447 garray_T newargs;
16448 garray_T newlines;
16449 int varargs = FALSE;
16450 int mustend = FALSE;
16451 int flags = 0;
16452 ufunc_T *fp;
16453 int indent;
16454 int nesting;
16455 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016456 dictitem_T *v;
16457 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016458 static int func_nr = 0; /* number for nameless function */
16459 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016460 hashtab_T *ht;
16461 int todo;
16462 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463
16464 /*
16465 * ":function" without argument: list functions.
16466 */
16467 if (ends_excmd(*eap->arg))
16468 {
16469 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016470 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016471 todo = func_hashtab.ht_used;
16472 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016473 {
16474 if (!HASHITEM_EMPTY(hi))
16475 {
16476 --todo;
16477 fp = HI2UF(hi);
16478 if (!isdigit(*fp->uf_name))
16479 list_func_head(fp, FALSE);
16480 }
16481 }
16482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483 eap->nextcmd = check_nextcmd(eap->arg);
16484 return;
16485 }
16486
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016487 /*
16488 * Get the function name. There are these situations:
16489 * func normal function name
16490 * "name" == func, "fudi.fd_dict" == NULL
16491 * dict.func new dictionary entry
16492 * "name" == NULL, "fudi.fd_dict" set,
16493 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16494 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016495 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016496 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16497 * dict.func existing dict entry that's not a Funcref
16498 * "name" == NULL, "fudi.fd_dict" set,
16499 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016502 name = trans_function_name(&p, eap->skip, 0, &fudi);
16503 paren = (vim_strchr(p, '(') != NULL);
16504 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505 {
16506 /*
16507 * Return on an invalid expression in braces, unless the expression
16508 * evaluation has been cancelled due to an aborting error, an
16509 * interrupt, or an exception.
16510 */
16511 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016512 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016513 if (!eap->skip && fudi.fd_newkey != NULL)
16514 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016515 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518 else
16519 eap->skip = TRUE;
16520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521 /* An error in a function call during evaluation of an expression in magic
16522 * braces should not cause the function not to be defined. */
16523 saved_did_emsg = did_emsg;
16524 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525
16526 /*
16527 * ":function func" with only function name: list function.
16528 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016529 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016530 {
16531 if (!ends_excmd(*skipwhite(p)))
16532 {
16533 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016534 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 }
16536 eap->nextcmd = check_nextcmd(p);
16537 if (eap->nextcmd != NULL)
16538 *p = NUL;
16539 if (!eap->skip && !got_int)
16540 {
16541 fp = find_func(name);
16542 if (fp != NULL)
16543 {
16544 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016545 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 {
16547 msg_putchar('\n');
16548 msg_outnum((long)(j + 1));
16549 if (j < 9)
16550 msg_putchar(' ');
16551 if (j < 99)
16552 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016553 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554 out_flush(); /* show a line at a time */
16555 ui_breakcheck();
16556 }
16557 if (!got_int)
16558 {
16559 msg_putchar('\n');
16560 msg_puts((char_u *)" endfunction");
16561 }
16562 }
16563 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016564 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016566 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 }
16568
16569 /*
16570 * ":function name(arg1, arg2)" Define function.
16571 */
16572 p = skipwhite(p);
16573 if (*p != '(')
16574 {
16575 if (!eap->skip)
16576 {
16577 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016578 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016579 }
16580 /* attempt to continue by skipping some text */
16581 if (vim_strchr(p, '(') != NULL)
16582 p = vim_strchr(p, '(');
16583 }
16584 p = skipwhite(p + 1);
16585
16586 ga_init2(&newargs, (int)sizeof(char_u *), 3);
16587 ga_init2(&newlines, (int)sizeof(char_u *), 3);
16588
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016589 if (!eap->skip)
16590 {
16591 /* Check the name of the function. */
16592 if (name != NULL)
16593 arg = name;
16594 else
16595 arg = fudi.fd_newkey;
16596 if (arg != NULL)
16597 {
16598 if (*arg == K_SPECIAL)
16599 j = 3;
16600 else
16601 j = 0;
16602 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
16603 : eval_isnamec(arg[j])))
16604 ++j;
16605 if (arg[j] != NUL)
16606 emsg_funcname(_(e_invarg2), arg);
16607 }
16608 }
16609
Bram Moolenaar071d4272004-06-13 20:20:40 +000016610 /*
16611 * Isolate the arguments: "arg1, arg2, ...)"
16612 */
16613 while (*p != ')')
16614 {
16615 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
16616 {
16617 varargs = TRUE;
16618 p += 3;
16619 mustend = TRUE;
16620 }
16621 else
16622 {
16623 arg = p;
16624 while (ASCII_ISALNUM(*p) || *p == '_')
16625 ++p;
16626 if (arg == p || isdigit(*arg)
16627 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
16628 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
16629 {
16630 if (!eap->skip)
16631 EMSG2(_("E125: Illegal argument: %s"), arg);
16632 break;
16633 }
16634 if (ga_grow(&newargs, 1) == FAIL)
16635 goto erret;
16636 c = *p;
16637 *p = NUL;
16638 arg = vim_strsave(arg);
16639 if (arg == NULL)
16640 goto erret;
16641 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
16642 *p = c;
16643 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 if (*p == ',')
16645 ++p;
16646 else
16647 mustend = TRUE;
16648 }
16649 p = skipwhite(p);
16650 if (mustend && *p != ')')
16651 {
16652 if (!eap->skip)
16653 EMSG2(_(e_invarg2), eap->arg);
16654 break;
16655 }
16656 }
16657 ++p; /* skip the ')' */
16658
Bram Moolenaare9a41262005-01-15 22:18:47 +000016659 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660 for (;;)
16661 {
16662 p = skipwhite(p);
16663 if (STRNCMP(p, "range", 5) == 0)
16664 {
16665 flags |= FC_RANGE;
16666 p += 5;
16667 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016668 else if (STRNCMP(p, "dict", 4) == 0)
16669 {
16670 flags |= FC_DICT;
16671 p += 4;
16672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673 else if (STRNCMP(p, "abort", 5) == 0)
16674 {
16675 flags |= FC_ABORT;
16676 p += 5;
16677 }
16678 else
16679 break;
16680 }
16681
16682 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
16683 EMSG(_(e_trailing));
16684
16685 /*
16686 * Read the body of the function, until ":endfunction" is found.
16687 */
16688 if (KeyTyped)
16689 {
16690 /* Check if the function already exists, don't let the user type the
16691 * whole function before telling him it doesn't work! For a script we
16692 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016693 if (!eap->skip && !eap->forceit)
16694 {
16695 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
16696 EMSG(_(e_funcdict));
16697 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016698 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016700
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016701 if (!eap->skip && did_emsg)
16702 goto erret;
16703
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704 msg_putchar('\n'); /* don't overwrite the function name */
16705 cmdline_row = msg_row;
16706 }
16707
16708 indent = 2;
16709 nesting = 0;
16710 for (;;)
16711 {
16712 msg_scroll = TRUE;
16713 need_wait_return = FALSE;
16714 if (eap->getline == NULL)
16715 theline = getcmdline(':', 0L, indent);
16716 else
16717 theline = eap->getline(':', eap->cookie, indent);
16718 if (KeyTyped)
16719 lines_left = Rows - 1;
16720 if (theline == NULL)
16721 {
16722 EMSG(_("E126: Missing :endfunction"));
16723 goto erret;
16724 }
16725
16726 if (skip_until != NULL)
16727 {
16728 /* between ":append" and "." and between ":python <<EOF" and "EOF"
16729 * don't check for ":endfunc". */
16730 if (STRCMP(theline, skip_until) == 0)
16731 {
16732 vim_free(skip_until);
16733 skip_until = NULL;
16734 }
16735 }
16736 else
16737 {
16738 /* skip ':' and blanks*/
16739 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
16740 ;
16741
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016742 /* Check for "endfunction". */
16743 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744 {
16745 vim_free(theline);
16746 break;
16747 }
16748
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016749 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750 * at "end". */
16751 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
16752 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016753 else if (STRNCMP(p, "if", 2) == 0
16754 || STRNCMP(p, "wh", 2) == 0
16755 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 || STRNCMP(p, "try", 3) == 0)
16757 indent += 2;
16758
16759 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016760 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016762 if (*p == '!')
16763 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016764 p += eval_fname_script(p);
16765 if (ASCII_ISALPHA(*p))
16766 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016767 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 if (*skipwhite(p) == '(')
16769 {
16770 ++nesting;
16771 indent += 2;
16772 }
16773 }
16774 }
16775
16776 /* Check for ":append" or ":insert". */
16777 p = skip_range(p, NULL);
16778 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
16779 || (p[0] == 'i'
16780 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
16781 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
16782 skip_until = vim_strsave((char_u *)".");
16783
16784 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
16785 arg = skipwhite(skiptowhite(p));
16786 if (arg[0] == '<' && arg[1] =='<'
16787 && ((p[0] == 'p' && p[1] == 'y'
16788 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
16789 || (p[0] == 'p' && p[1] == 'e'
16790 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
16791 || (p[0] == 't' && p[1] == 'c'
16792 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
16793 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
16794 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000016795 || (p[0] == 'm' && p[1] == 'z'
16796 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797 ))
16798 {
16799 /* ":python <<" continues until a dot, like ":append" */
16800 p = skipwhite(arg + 2);
16801 if (*p == NUL)
16802 skip_until = vim_strsave((char_u *)".");
16803 else
16804 skip_until = vim_strsave(p);
16805 }
16806 }
16807
16808 /* Add the line to the function. */
16809 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016810 {
16811 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016813 }
16814
16815 /* Copy the line to newly allocated memory. get_one_sourceline()
16816 * allocates 250 bytes per line, this saves 80% on average. The cost
16817 * is an extra alloc/free. */
16818 p = vim_strsave(theline);
16819 if (p != NULL)
16820 {
16821 vim_free(theline);
16822 theline = p;
16823 }
16824
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
16826 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 }
16828
16829 /* Don't define the function when skipping commands or when an error was
16830 * detected. */
16831 if (eap->skip || did_emsg)
16832 goto erret;
16833
16834 /*
16835 * If there are no errors, add the function
16836 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016837 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016838 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016839 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000016840 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016841 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016842 emsg_funcname("E707: Function name conflicts with variable: %s",
16843 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016844 goto erret;
16845 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016846
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016847 fp = find_func(name);
16848 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016850 if (!eap->forceit)
16851 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016852 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016853 goto erret;
16854 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016855 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016856 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016857 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016858 name);
16859 goto erret;
16860 }
16861 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016862 ga_clear_strings(&(fp->uf_args));
16863 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016864 vim_free(name);
16865 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867 }
16868 else
16869 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016870 char numbuf[20];
16871
16872 fp = NULL;
16873 if (fudi.fd_newkey == NULL && !eap->forceit)
16874 {
16875 EMSG(_(e_funcdict));
16876 goto erret;
16877 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000016878 if (fudi.fd_di == NULL)
16879 {
16880 /* Can't add a function to a locked dictionary */
16881 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
16882 goto erret;
16883 }
16884 /* Can't change an existing function if it is locked */
16885 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
16886 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016887
16888 /* Give the function a sequential number. Can only be used with a
16889 * Funcref! */
16890 vim_free(name);
16891 sprintf(numbuf, "%d", ++func_nr);
16892 name = vim_strsave((char_u *)numbuf);
16893 if (name == NULL)
16894 goto erret;
16895 }
16896
16897 if (fp == NULL)
16898 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016899 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016900 {
16901 int slen, plen;
16902 char_u *scriptname;
16903
16904 /* Check that the autoload name matches the script name. */
16905 j = FAIL;
16906 if (sourcing_name != NULL)
16907 {
16908 scriptname = autoload_name(name);
16909 if (scriptname != NULL)
16910 {
16911 p = vim_strchr(scriptname, '/');
16912 plen = STRLEN(p);
16913 slen = STRLEN(sourcing_name);
16914 if (slen > plen && fnamecmp(p,
16915 sourcing_name + slen - plen) == 0)
16916 j = OK;
16917 vim_free(scriptname);
16918 }
16919 }
16920 if (j == FAIL)
16921 {
16922 EMSG2(_("E746: Function name does not match script file name: %s"), name);
16923 goto erret;
16924 }
16925 }
16926
16927 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 if (fp == NULL)
16929 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016930
16931 if (fudi.fd_dict != NULL)
16932 {
16933 if (fudi.fd_di == NULL)
16934 {
16935 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016936 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016937 if (fudi.fd_di == NULL)
16938 {
16939 vim_free(fp);
16940 goto erret;
16941 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016942 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
16943 {
16944 vim_free(fudi.fd_di);
16945 goto erret;
16946 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016947 }
16948 else
16949 /* overwrite existing dict entry */
16950 clear_tv(&fudi.fd_di->di_tv);
16951 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016952 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016953 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016954 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016955 }
16956
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016958 STRCPY(fp->uf_name, name);
16959 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016961 fp->uf_args = newargs;
16962 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016963#ifdef FEAT_PROFILE
16964 fp->uf_tml_count = NULL;
16965 fp->uf_tml_total = NULL;
16966 fp->uf_tml_self = NULL;
16967 fp->uf_profiling = FALSE;
16968 if (prof_def_func())
16969 func_do_profile(fp);
16970#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016971 fp->uf_varargs = varargs;
16972 fp->uf_flags = flags;
16973 fp->uf_calls = 0;
16974 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016975 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976
16977erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978 ga_clear_strings(&newargs);
16979 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016980ret_free:
16981 vim_free(skip_until);
16982 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985}
16986
16987/*
16988 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000016989 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016991 * flags:
16992 * TFN_INT: internal function name OK
16993 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994 * Advances "pp" to just after the function name (if no error).
16995 */
16996 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016997trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 char_u **pp;
16999 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017000 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017001 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017003 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 char_u *start;
17005 char_u *end;
17006 int lead;
17007 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017009 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017010
17011 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017012 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017014
17015 /* Check for hard coded <SNR>: already translated function ID (from a user
17016 * command). */
17017 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17018 && (*pp)[2] == (int)KE_SNR)
17019 {
17020 *pp += 3;
17021 len = get_id_len(pp) + 3;
17022 return vim_strnsave(start, len);
17023 }
17024
17025 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17026 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017028 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017030
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017031 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17032 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017033 if (end == start)
17034 {
17035 if (!skip)
17036 EMSG(_("E129: Function name required"));
17037 goto theend;
17038 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017039 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017040 {
17041 /*
17042 * Report an invalid expression in braces, unless the expression
17043 * evaluation has been cancelled due to an aborting error, an
17044 * interrupt, or an exception.
17045 */
17046 if (!aborting())
17047 {
17048 if (end != NULL)
17049 EMSG2(_(e_invarg2), start);
17050 }
17051 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017052 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017053 goto theend;
17054 }
17055
17056 if (lv.ll_tv != NULL)
17057 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017058 if (fdp != NULL)
17059 {
17060 fdp->fd_dict = lv.ll_dict;
17061 fdp->fd_newkey = lv.ll_newkey;
17062 lv.ll_newkey = NULL;
17063 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017064 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017065 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17066 {
17067 name = vim_strsave(lv.ll_tv->vval.v_string);
17068 *pp = end;
17069 }
17070 else
17071 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017072 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17073 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017074 EMSG(_(e_funcref));
17075 else
17076 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017077 name = NULL;
17078 }
17079 goto theend;
17080 }
17081
17082 if (lv.ll_name == NULL)
17083 {
17084 /* Error found, but continue after the function name. */
17085 *pp = end;
17086 goto theend;
17087 }
17088
17089 if (lv.ll_exp_name != NULL)
17090 len = STRLEN(lv.ll_exp_name);
17091 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017092 {
17093 if (lead == 2) /* skip over "s:" */
17094 lv.ll_name += 2;
17095 len = (int)(end - lv.ll_name);
17096 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017097
17098 /*
17099 * Copy the function name to allocated memory.
17100 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17101 * Accept <SNR>123_name() outside a script.
17102 */
17103 if (skip)
17104 lead = 0; /* do nothing */
17105 else if (lead > 0)
17106 {
17107 lead = 3;
17108 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17109 {
17110 if (current_SID <= 0)
17111 {
17112 EMSG(_(e_usingsid));
17113 goto theend;
17114 }
17115 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17116 lead += (int)STRLEN(sid_buf);
17117 }
17118 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017119 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017120 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017121 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017122 goto theend;
17123 }
17124 name = alloc((unsigned)(len + lead + 1));
17125 if (name != NULL)
17126 {
17127 if (lead > 0)
17128 {
17129 name[0] = K_SPECIAL;
17130 name[1] = KS_EXTRA;
17131 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017132 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017133 STRCPY(name + 3, sid_buf);
17134 }
17135 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17136 name[len + lead] = NUL;
17137 }
17138 *pp = end;
17139
17140theend:
17141 clear_lval(&lv);
17142 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143}
17144
17145/*
17146 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17147 * Return 2 if "p" starts with "s:".
17148 * Return 0 otherwise.
17149 */
17150 static int
17151eval_fname_script(p)
17152 char_u *p;
17153{
17154 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17155 || STRNICMP(p + 1, "SNR>", 4) == 0))
17156 return 5;
17157 if (p[0] == 's' && p[1] == ':')
17158 return 2;
17159 return 0;
17160}
17161
17162/*
17163 * Return TRUE if "p" starts with "<SID>" or "s:".
17164 * Only works if eval_fname_script() returned non-zero for "p"!
17165 */
17166 static int
17167eval_fname_sid(p)
17168 char_u *p;
17169{
17170 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17171}
17172
17173/*
17174 * List the head of the function: "name(arg1, arg2)".
17175 */
17176 static void
17177list_func_head(fp, indent)
17178 ufunc_T *fp;
17179 int indent;
17180{
17181 int j;
17182
17183 msg_start();
17184 if (indent)
17185 MSG_PUTS(" ");
17186 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017187 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188 {
17189 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017190 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191 }
17192 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017193 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017195 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196 {
17197 if (j)
17198 MSG_PUTS(", ");
17199 msg_puts(FUNCARG(fp, j));
17200 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017201 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202 {
17203 if (j)
17204 MSG_PUTS(", ");
17205 MSG_PUTS("...");
17206 }
17207 msg_putchar(')');
17208}
17209
17210/*
17211 * Find a function by name, return pointer to it in ufuncs.
17212 * Return NULL for unknown function.
17213 */
17214 static ufunc_T *
17215find_func(name)
17216 char_u *name;
17217{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017218 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017220 hi = hash_find(&func_hashtab, name);
17221 if (!HASHITEM_EMPTY(hi))
17222 return HI2UF(hi);
17223 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224}
17225
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017226#if defined(EXITFREE) || defined(PROTO)
17227 void
17228free_all_functions()
17229{
17230 hashitem_T *hi;
17231
17232 /* Need to start all over every time, because func_free() may change the
17233 * hash table. */
17234 while (func_hashtab.ht_used > 0)
17235 for (hi = func_hashtab.ht_array; ; ++hi)
17236 if (!HASHITEM_EMPTY(hi))
17237 {
17238 func_free(HI2UF(hi));
17239 break;
17240 }
17241}
17242#endif
17243
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017244/*
17245 * Return TRUE if a function "name" exists.
17246 */
17247 static int
17248function_exists(name)
17249 char_u *name;
17250{
17251 char_u *p = name;
17252 int n = FALSE;
17253
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017254 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017255 if (p != NULL)
17256 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017257 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017258 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017259 else
17260 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017261 vim_free(p);
17262 }
17263 return n;
17264}
17265
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017266/*
17267 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017268 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017269 */
17270 static int
17271builtin_function(name)
17272 char_u *name;
17273{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017274 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17275 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017276}
17277
Bram Moolenaar05159a02005-02-26 23:04:13 +000017278#if defined(FEAT_PROFILE) || defined(PROTO)
17279/*
17280 * Start profiling function "fp".
17281 */
17282 static void
17283func_do_profile(fp)
17284 ufunc_T *fp;
17285{
17286 fp->uf_tm_count = 0;
17287 profile_zero(&fp->uf_tm_self);
17288 profile_zero(&fp->uf_tm_total);
17289 if (fp->uf_tml_count == NULL)
17290 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17291 (sizeof(int) * fp->uf_lines.ga_len));
17292 if (fp->uf_tml_total == NULL)
17293 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17294 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17295 if (fp->uf_tml_self == NULL)
17296 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17297 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17298 fp->uf_tml_idx = -1;
17299 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17300 || fp->uf_tml_self == NULL)
17301 return; /* out of memory */
17302
17303 fp->uf_profiling = TRUE;
17304}
17305
17306/*
17307 * Dump the profiling results for all functions in file "fd".
17308 */
17309 void
17310func_dump_profile(fd)
17311 FILE *fd;
17312{
17313 hashitem_T *hi;
17314 int todo;
17315 ufunc_T *fp;
17316 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017317 ufunc_T **sorttab;
17318 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017319
17320 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017321 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17322
Bram Moolenaar05159a02005-02-26 23:04:13 +000017323 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17324 {
17325 if (!HASHITEM_EMPTY(hi))
17326 {
17327 --todo;
17328 fp = HI2UF(hi);
17329 if (fp->uf_profiling)
17330 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017331 if (sorttab != NULL)
17332 sorttab[st_len++] = fp;
17333
Bram Moolenaar05159a02005-02-26 23:04:13 +000017334 if (fp->uf_name[0] == K_SPECIAL)
17335 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17336 else
17337 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17338 if (fp->uf_tm_count == 1)
17339 fprintf(fd, "Called 1 time\n");
17340 else
17341 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17342 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17343 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17344 fprintf(fd, "\n");
17345 fprintf(fd, "count total (s) self (s)\n");
17346
17347 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17348 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017349 prof_func_line(fd, fp->uf_tml_count[i],
17350 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017351 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17352 }
17353 fprintf(fd, "\n");
17354 }
17355 }
17356 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017357
17358 if (sorttab != NULL && st_len > 0)
17359 {
17360 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17361 prof_total_cmp);
17362 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17363 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17364 prof_self_cmp);
17365 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17366 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017367}
Bram Moolenaar73830342005-02-28 22:48:19 +000017368
17369 static void
17370prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17371 FILE *fd;
17372 ufunc_T **sorttab;
17373 int st_len;
17374 char *title;
17375 int prefer_self; /* when equal print only self time */
17376{
17377 int i;
17378 ufunc_T *fp;
17379
17380 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17381 fprintf(fd, "count total (s) self (s) function\n");
17382 for (i = 0; i < 20 && i < st_len; ++i)
17383 {
17384 fp = sorttab[i];
17385 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17386 prefer_self);
17387 if (fp->uf_name[0] == K_SPECIAL)
17388 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17389 else
17390 fprintf(fd, " %s()\n", fp->uf_name);
17391 }
17392 fprintf(fd, "\n");
17393}
17394
17395/*
17396 * Print the count and times for one function or function line.
17397 */
17398 static void
17399prof_func_line(fd, count, total, self, prefer_self)
17400 FILE *fd;
17401 int count;
17402 proftime_T *total;
17403 proftime_T *self;
17404 int prefer_self; /* when equal print only self time */
17405{
17406 if (count > 0)
17407 {
17408 fprintf(fd, "%5d ", count);
17409 if (prefer_self && profile_equal(total, self))
17410 fprintf(fd, " ");
17411 else
17412 fprintf(fd, "%s ", profile_msg(total));
17413 if (!prefer_self && profile_equal(total, self))
17414 fprintf(fd, " ");
17415 else
17416 fprintf(fd, "%s ", profile_msg(self));
17417 }
17418 else
17419 fprintf(fd, " ");
17420}
17421
17422/*
17423 * Compare function for total time sorting.
17424 */
17425 static int
17426#ifdef __BORLANDC__
17427_RTLENTRYF
17428#endif
17429prof_total_cmp(s1, s2)
17430 const void *s1;
17431 const void *s2;
17432{
17433 ufunc_T *p1, *p2;
17434
17435 p1 = *(ufunc_T **)s1;
17436 p2 = *(ufunc_T **)s2;
17437 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17438}
17439
17440/*
17441 * Compare function for self time sorting.
17442 */
17443 static int
17444#ifdef __BORLANDC__
17445_RTLENTRYF
17446#endif
17447prof_self_cmp(s1, s2)
17448 const void *s1;
17449 const void *s2;
17450{
17451 ufunc_T *p1, *p2;
17452
17453 p1 = *(ufunc_T **)s1;
17454 p2 = *(ufunc_T **)s2;
17455 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17456}
17457
Bram Moolenaar05159a02005-02-26 23:04:13 +000017458#endif
17459
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017460/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017461 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017462 * Return TRUE if a package was loaded.
17463 */
17464 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017465script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017466 char_u *name;
17467{
17468 char_u *p;
17469 char_u *scriptname;
17470 int ret = FALSE;
17471
17472 /* If there is no colon after name[1] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017473 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017474 if (p == NULL || p <= name + 2)
17475 return FALSE;
17476
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017477 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
17478 scriptname = autoload_name(name);
17479 if (cmd_runtime(scriptname, FALSE) == OK)
17480 ret = TRUE;
17481
17482 vim_free(scriptname);
17483 return ret;
17484}
17485
17486/*
17487 * Return the autoload script name for a function or variable name.
17488 * Returns NULL when out of memory.
17489 */
17490 static char_u *
17491autoload_name(name)
17492 char_u *name;
17493{
17494 char_u *p;
17495 char_u *scriptname;
17496
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017497 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017498 scriptname = alloc((unsigned)(STRLEN(name) + 14));
17499 if (scriptname == NULL)
17500 return FALSE;
17501 STRCPY(scriptname, "autoload/");
17502 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017503 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017504 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017505 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017506 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017507 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017508}
17509
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
17511
17512/*
17513 * Function given to ExpandGeneric() to obtain the list of user defined
17514 * function names.
17515 */
17516 char_u *
17517get_user_func_name(xp, idx)
17518 expand_T *xp;
17519 int idx;
17520{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017521 static long_u done;
17522 static hashitem_T *hi;
17523 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524
17525 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017527 done = 0;
17528 hi = func_hashtab.ht_array;
17529 }
17530 if (done < func_hashtab.ht_used)
17531 {
17532 if (done++ > 0)
17533 ++hi;
17534 while (HASHITEM_EMPTY(hi))
17535 ++hi;
17536 fp = HI2UF(hi);
17537
17538 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
17539 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540
17541 cat_func_name(IObuff, fp);
17542 if (xp->xp_context != EXPAND_USER_FUNC)
17543 {
17544 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017545 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546 STRCAT(IObuff, ")");
17547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548 return IObuff;
17549 }
17550 return NULL;
17551}
17552
17553#endif /* FEAT_CMDL_COMPL */
17554
17555/*
17556 * Copy the function name of "fp" to buffer "buf".
17557 * "buf" must be able to hold the function name plus three bytes.
17558 * Takes care of script-local function names.
17559 */
17560 static void
17561cat_func_name(buf, fp)
17562 char_u *buf;
17563 ufunc_T *fp;
17564{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017565 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 {
17567 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017568 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 }
17570 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017571 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572}
17573
17574/*
17575 * ":delfunction {name}"
17576 */
17577 void
17578ex_delfunction(eap)
17579 exarg_T *eap;
17580{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017581 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582 char_u *p;
17583 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017584 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585
17586 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017587 name = trans_function_name(&p, eap->skip, 0, &fudi);
17588 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017590 {
17591 if (fudi.fd_dict != NULL && !eap->skip)
17592 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017595 if (!ends_excmd(*skipwhite(p)))
17596 {
17597 vim_free(name);
17598 EMSG(_(e_trailing));
17599 return;
17600 }
17601 eap->nextcmd = check_nextcmd(p);
17602 if (eap->nextcmd != NULL)
17603 *p = NUL;
17604
17605 if (!eap->skip)
17606 fp = find_func(name);
17607 vim_free(name);
17608
17609 if (!eap->skip)
17610 {
17611 if (fp == NULL)
17612 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017613 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614 return;
17615 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017616 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617 {
17618 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
17619 return;
17620 }
17621
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017622 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017624 /* Delete the dict item that refers to the function, it will
17625 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017626 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017628 else
17629 func_free(fp);
17630 }
17631}
17632
17633/*
17634 * Free a function and remove it from the list of functions.
17635 */
17636 static void
17637func_free(fp)
17638 ufunc_T *fp;
17639{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017640 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017641
17642 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017643 ga_clear_strings(&(fp->uf_args));
17644 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000017645#ifdef FEAT_PROFILE
17646 vim_free(fp->uf_tml_count);
17647 vim_free(fp->uf_tml_total);
17648 vim_free(fp->uf_tml_self);
17649#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017650
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017651 /* remove the function from the function hashtable */
17652 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
17653 if (HASHITEM_EMPTY(hi))
17654 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017655 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017656 hash_remove(&func_hashtab, hi);
17657
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017658 vim_free(fp);
17659}
17660
17661/*
17662 * Unreference a Function: decrement the reference count and free it when it
17663 * becomes zero. Only for numbered functions.
17664 */
17665 static void
17666func_unref(name)
17667 char_u *name;
17668{
17669 ufunc_T *fp;
17670
17671 if (name != NULL && isdigit(*name))
17672 {
17673 fp = find_func(name);
17674 if (fp == NULL)
17675 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017676 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017677 {
17678 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017679 * when "uf_calls" becomes zero. */
17680 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017681 func_free(fp);
17682 }
17683 }
17684}
17685
17686/*
17687 * Count a reference to a Function.
17688 */
17689 static void
17690func_ref(name)
17691 char_u *name;
17692{
17693 ufunc_T *fp;
17694
17695 if (name != NULL && isdigit(*name))
17696 {
17697 fp = find_func(name);
17698 if (fp == NULL)
17699 EMSG2(_(e_intern2), "func_ref()");
17700 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017701 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702 }
17703}
17704
17705/*
17706 * Call a user function.
17707 */
17708 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000017709call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 ufunc_T *fp; /* pointer to function */
17711 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000017712 typval_T *argvars; /* arguments */
17713 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714 linenr_T firstline; /* first line of range */
17715 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000017716 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717{
Bram Moolenaar33570922005-01-25 22:26:29 +000017718 char_u *save_sourcing_name;
17719 linenr_T save_sourcing_lnum;
17720 scid_T save_current_SID;
17721 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000017722 int save_did_emsg;
17723 static int depth = 0;
17724 dictitem_T *v;
17725 int fixvar_idx = 0; /* index in fixvar[] */
17726 int i;
17727 int ai;
17728 char_u numbuf[NUMBUFLEN];
17729 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017730#ifdef FEAT_PROFILE
17731 proftime_T wait_start;
17732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733
17734 /* If depth of calling is getting too high, don't execute the function */
17735 if (depth >= p_mfd)
17736 {
17737 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017738 rettv->v_type = VAR_NUMBER;
17739 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740 return;
17741 }
17742 ++depth;
17743
17744 line_breakcheck(); /* check for CTRL-C hit */
17745
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017746 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000017747 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017749 fc.rettv = rettv;
17750 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751 fc.linenr = 0;
17752 fc.returned = FALSE;
17753 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017755 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756 fc.dbg_tick = debug_tick;
17757
Bram Moolenaar33570922005-01-25 22:26:29 +000017758 /*
17759 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
17760 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
17761 * each argument variable and saves a lot of time.
17762 */
17763 /*
17764 * Init l: variables.
17765 */
17766 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000017767 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017768 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017769 /* Set l:self to "selfdict". */
17770 v = &fc.fixvar[fixvar_idx++].var;
17771 STRCPY(v->di_key, "self");
17772 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
17773 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
17774 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017775 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000017776 v->di_tv.vval.v_dict = selfdict;
17777 ++selfdict->dv_refcount;
17778 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017779
Bram Moolenaar33570922005-01-25 22:26:29 +000017780 /*
17781 * Init a: variables.
17782 * Set a:0 to "argcount".
17783 * Set a:000 to a list with room for the "..." arguments.
17784 */
17785 init_var_dict(&fc.l_avars, &fc.l_avars_var);
17786 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017787 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000017788 v = &fc.fixvar[fixvar_idx++].var;
17789 STRCPY(v->di_key, "000");
17790 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17791 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17792 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017793 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017794 v->di_tv.vval.v_list = &fc.l_varlist;
17795 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
17796 fc.l_varlist.lv_refcount = 99999;
17797
17798 /*
17799 * Set a:firstline to "firstline" and a:lastline to "lastline".
17800 * Set a:name to named arguments.
17801 * Set a:N to the "..." arguments.
17802 */
17803 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
17804 (varnumber_T)firstline);
17805 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
17806 (varnumber_T)lastline);
17807 for (i = 0; i < argcount; ++i)
17808 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017809 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017810 if (ai < 0)
17811 /* named argument a:name */
17812 name = FUNCARG(fp, i);
17813 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017814 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017815 /* "..." argument a:1, a:2, etc. */
17816 sprintf((char *)numbuf, "%d", ai + 1);
17817 name = numbuf;
17818 }
17819 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
17820 {
17821 v = &fc.fixvar[fixvar_idx++].var;
17822 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17823 }
17824 else
17825 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017826 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17827 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000017828 if (v == NULL)
17829 break;
17830 v->di_flags = DI_FLAGS_RO;
17831 }
17832 STRCPY(v->di_key, name);
17833 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17834
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017835 /* Note: the values are copied directly to avoid alloc/free.
17836 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017837 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017838 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017839
17840 if (ai >= 0 && ai < MAX_FUNC_ARGS)
17841 {
17842 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
17843 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017844 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017845 }
17846 }
17847
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848 /* Don't redraw while executing the function. */
17849 ++RedrawingDisabled;
17850 save_sourcing_name = sourcing_name;
17851 save_sourcing_lnum = sourcing_lnum;
17852 sourcing_lnum = 1;
17853 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017854 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 if (sourcing_name != NULL)
17856 {
17857 if (save_sourcing_name != NULL
17858 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
17859 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
17860 else
17861 STRCPY(sourcing_name, "function ");
17862 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
17863
17864 if (p_verbose >= 12)
17865 {
17866 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017867 verbose_enter_scroll();
17868
Bram Moolenaar555b2802005-05-19 21:08:39 +000017869 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870 if (p_verbose >= 14)
17871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000017873 char_u numbuf[NUMBUFLEN];
17874 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875
17876 msg_puts((char_u *)"(");
17877 for (i = 0; i < argcount; ++i)
17878 {
17879 if (i > 0)
17880 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017881 if (argvars[i].v_type == VAR_NUMBER)
17882 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883 else
17884 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017885 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017886 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017887 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017888 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889 }
17890 }
17891 msg_puts((char_u *)")");
17892 }
17893 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017894
17895 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896 --no_wait_return;
17897 }
17898 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017899#ifdef FEAT_PROFILE
17900 if (do_profiling)
17901 {
17902 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
17903 func_do_profile(fp);
17904 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017905 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000017906 {
17907 ++fp->uf_tm_count;
17908 profile_start(&fp->uf_tm_start);
17909 profile_zero(&fp->uf_tm_children);
17910 }
17911 script_prof_save(&wait_start);
17912 }
17913#endif
17914
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017916 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 save_did_emsg = did_emsg;
17918 did_emsg = FALSE;
17919
17920 /* call do_cmdline() to execute the lines */
17921 do_cmdline(NULL, get_func_line, (void *)&fc,
17922 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
17923
17924 --RedrawingDisabled;
17925
17926 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017927 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017929 clear_tv(rettv);
17930 rettv->v_type = VAR_NUMBER;
17931 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932 }
17933
Bram Moolenaar05159a02005-02-26 23:04:13 +000017934#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017935 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000017936 {
17937 profile_end(&fp->uf_tm_start);
17938 profile_sub_wait(&wait_start, &fp->uf_tm_start);
17939 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
17940 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
17941 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017942 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017943 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017944 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
17945 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017946 }
17947 }
17948#endif
17949
Bram Moolenaar071d4272004-06-13 20:20:40 +000017950 /* when being verbose, mention the return value */
17951 if (p_verbose >= 12)
17952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017954 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000017957 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017958 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000017959 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
17960 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017961 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017963 char_u buf[MSG_BUF_LEN];
17964 char_u numbuf[NUMBUFLEN];
17965 char_u *tofree;
17966
Bram Moolenaar555b2802005-05-19 21:08:39 +000017967 /* The value may be very long. Skip the middle part, so that we
17968 * have some idea how it starts and ends. smsg() would always
17969 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000017970 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017971 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000017972 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017973 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 }
17975 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017976
17977 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978 --no_wait_return;
17979 }
17980
17981 vim_free(sourcing_name);
17982 sourcing_name = save_sourcing_name;
17983 sourcing_lnum = save_sourcing_lnum;
17984 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017985#ifdef FEAT_PROFILE
17986 if (do_profiling)
17987 script_prof_restore(&wait_start);
17988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989
17990 if (p_verbose >= 12 && sourcing_name != NULL)
17991 {
17992 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017993 verbose_enter_scroll();
17994
Bram Moolenaar555b2802005-05-19 21:08:39 +000017995 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017997
17998 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017999 --no_wait_return;
18000 }
18001
18002 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018003 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004
Bram Moolenaar33570922005-01-25 22:26:29 +000018005 /* The a: variables typevals were not alloced, only free the allocated
18006 * variables. */
18007 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18008
18009 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010 --depth;
18011}
18012
18013/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018014 * Add a number variable "name" to dict "dp" with value "nr".
18015 */
18016 static void
18017add_nr_var(dp, v, name, nr)
18018 dict_T *dp;
18019 dictitem_T *v;
18020 char *name;
18021 varnumber_T nr;
18022{
18023 STRCPY(v->di_key, name);
18024 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18025 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18026 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018027 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018028 v->di_tv.vval.v_number = nr;
18029}
18030
18031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 * ":return [expr]"
18033 */
18034 void
18035ex_return(eap)
18036 exarg_T *eap;
18037{
18038 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018039 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 int returning = FALSE;
18041
18042 if (current_funccal == NULL)
18043 {
18044 EMSG(_("E133: :return not inside a function"));
18045 return;
18046 }
18047
18048 if (eap->skip)
18049 ++emsg_skip;
18050
18051 eap->nextcmd = NULL;
18052 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018053 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054 {
18055 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018056 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018057 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018058 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059 }
18060 /* It's safer to return also on error. */
18061 else if (!eap->skip)
18062 {
18063 /*
18064 * Return unless the expression evaluation has been cancelled due to an
18065 * aborting error, an interrupt, or an exception.
18066 */
18067 if (!aborting())
18068 returning = do_return(eap, FALSE, TRUE, NULL);
18069 }
18070
18071 /* When skipping or the return gets pending, advance to the next command
18072 * in this line (!returning). Otherwise, ignore the rest of the line.
18073 * Following lines will be ignored by get_func_line(). */
18074 if (returning)
18075 eap->nextcmd = NULL;
18076 else if (eap->nextcmd == NULL) /* no argument */
18077 eap->nextcmd = check_nextcmd(arg);
18078
18079 if (eap->skip)
18080 --emsg_skip;
18081}
18082
18083/*
18084 * Return from a function. Possibly makes the return pending. Also called
18085 * for a pending return at the ":endtry" or after returning from an extra
18086 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018087 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018088 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089 * FALSE when the return gets pending.
18090 */
18091 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018092do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093 exarg_T *eap;
18094 int reanimate;
18095 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018096 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097{
18098 int idx;
18099 struct condstack *cstack = eap->cstack;
18100
18101 if (reanimate)
18102 /* Undo the return. */
18103 current_funccal->returned = FALSE;
18104
18105 /*
18106 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18107 * not in its finally clause (which then is to be executed next) is found.
18108 * In this case, make the ":return" pending for execution at the ":endtry".
18109 * Otherwise, return normally.
18110 */
18111 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18112 if (idx >= 0)
18113 {
18114 cstack->cs_pending[idx] = CSTP_RETURN;
18115
18116 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018117 /* A pending return again gets pending. "rettv" points to an
18118 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018120 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121 else
18122 {
18123 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018124 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018126 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018128 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 {
18130 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018131 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018132 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133 else
18134 EMSG(_(e_outofmem));
18135 }
18136 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018137 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138
18139 if (reanimate)
18140 {
18141 /* The pending return value could be overwritten by a ":return"
18142 * without argument in a finally clause; reset the default
18143 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018144 current_funccal->rettv->v_type = VAR_NUMBER;
18145 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146 }
18147 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018148 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 }
18150 else
18151 {
18152 current_funccal->returned = TRUE;
18153
18154 /* If the return is carried out now, store the return value. For
18155 * a return immediately after reanimation, the value is already
18156 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018157 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018159 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018160 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018162 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163 }
18164 }
18165
18166 return idx < 0;
18167}
18168
18169/*
18170 * Free the variable with a pending return value.
18171 */
18172 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018173discard_pending_return(rettv)
18174 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175{
Bram Moolenaar33570922005-01-25 22:26:29 +000018176 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177}
18178
18179/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018180 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 * is an allocated string. Used by report_pending() for verbose messages.
18182 */
18183 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018184get_return_cmd(rettv)
18185 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018187 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018188 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018189 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018190
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018191 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018192 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018193 if (s == NULL)
18194 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018195
18196 STRCPY(IObuff, ":return ");
18197 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18198 if (STRLEN(s) + 8 >= IOSIZE)
18199 STRCPY(IObuff + IOSIZE - 4, "...");
18200 vim_free(tofree);
18201 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202}
18203
18204/*
18205 * Get next function line.
18206 * Called by do_cmdline() to get the next line.
18207 * Returns allocated string, or NULL for end of function.
18208 */
18209/* ARGSUSED */
18210 char_u *
18211get_func_line(c, cookie, indent)
18212 int c; /* not used */
18213 void *cookie;
18214 int indent; /* not used */
18215{
Bram Moolenaar33570922005-01-25 22:26:29 +000018216 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018217 ufunc_T *fp = fcp->func;
18218 char_u *retval;
18219 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220
18221 /* If breakpoints have been added/deleted need to check for it. */
18222 if (fcp->dbg_tick != debug_tick)
18223 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018224 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225 sourcing_lnum);
18226 fcp->dbg_tick = debug_tick;
18227 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018228#ifdef FEAT_PROFILE
18229 if (do_profiling)
18230 func_line_end(cookie);
18231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232
Bram Moolenaar05159a02005-02-26 23:04:13 +000018233 gap = &fp->uf_lines;
18234 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235 retval = NULL;
18236 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18237 retval = NULL;
18238 else
18239 {
18240 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18241 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018242#ifdef FEAT_PROFILE
18243 if (do_profiling)
18244 func_line_start(cookie);
18245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246 }
18247
18248 /* Did we encounter a breakpoint? */
18249 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18250 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018251 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018253 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254 sourcing_lnum);
18255 fcp->dbg_tick = debug_tick;
18256 }
18257
18258 return retval;
18259}
18260
Bram Moolenaar05159a02005-02-26 23:04:13 +000018261#if defined(FEAT_PROFILE) || defined(PROTO)
18262/*
18263 * Called when starting to read a function line.
18264 * "sourcing_lnum" must be correct!
18265 * When skipping lines it may not actually be executed, but we won't find out
18266 * until later and we need to store the time now.
18267 */
18268 void
18269func_line_start(cookie)
18270 void *cookie;
18271{
18272 funccall_T *fcp = (funccall_T *)cookie;
18273 ufunc_T *fp = fcp->func;
18274
18275 if (fp->uf_profiling && sourcing_lnum >= 1
18276 && sourcing_lnum <= fp->uf_lines.ga_len)
18277 {
18278 fp->uf_tml_idx = sourcing_lnum - 1;
18279 fp->uf_tml_execed = FALSE;
18280 profile_start(&fp->uf_tml_start);
18281 profile_zero(&fp->uf_tml_children);
18282 profile_get_wait(&fp->uf_tml_wait);
18283 }
18284}
18285
18286/*
18287 * Called when actually executing a function line.
18288 */
18289 void
18290func_line_exec(cookie)
18291 void *cookie;
18292{
18293 funccall_T *fcp = (funccall_T *)cookie;
18294 ufunc_T *fp = fcp->func;
18295
18296 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18297 fp->uf_tml_execed = TRUE;
18298}
18299
18300/*
18301 * Called when done with a function line.
18302 */
18303 void
18304func_line_end(cookie)
18305 void *cookie;
18306{
18307 funccall_T *fcp = (funccall_T *)cookie;
18308 ufunc_T *fp = fcp->func;
18309
18310 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18311 {
18312 if (fp->uf_tml_execed)
18313 {
18314 ++fp->uf_tml_count[fp->uf_tml_idx];
18315 profile_end(&fp->uf_tml_start);
18316 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18317 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18318 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18319 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18320 }
18321 fp->uf_tml_idx = -1;
18322 }
18323}
18324#endif
18325
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326/*
18327 * Return TRUE if the currently active function should be ended, because a
18328 * return was encountered or an error occured. Used inside a ":while".
18329 */
18330 int
18331func_has_ended(cookie)
18332 void *cookie;
18333{
Bram Moolenaar33570922005-01-25 22:26:29 +000018334 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335
18336 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18337 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018338 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339 || fcp->returned);
18340}
18341
18342/*
18343 * return TRUE if cookie indicates a function which "abort"s on errors.
18344 */
18345 int
18346func_has_abort(cookie)
18347 void *cookie;
18348{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018349 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350}
18351
18352#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18353typedef enum
18354{
18355 VAR_FLAVOUR_DEFAULT,
18356 VAR_FLAVOUR_SESSION,
18357 VAR_FLAVOUR_VIMINFO
18358} var_flavour_T;
18359
18360static var_flavour_T var_flavour __ARGS((char_u *varname));
18361
18362 static var_flavour_T
18363var_flavour(varname)
18364 char_u *varname;
18365{
18366 char_u *p = varname;
18367
18368 if (ASCII_ISUPPER(*p))
18369 {
18370 while (*(++p))
18371 if (ASCII_ISLOWER(*p))
18372 return VAR_FLAVOUR_SESSION;
18373 return VAR_FLAVOUR_VIMINFO;
18374 }
18375 else
18376 return VAR_FLAVOUR_DEFAULT;
18377}
18378#endif
18379
18380#if defined(FEAT_VIMINFO) || defined(PROTO)
18381/*
18382 * Restore global vars that start with a capital from the viminfo file
18383 */
18384 int
18385read_viminfo_varlist(virp, writing)
18386 vir_T *virp;
18387 int writing;
18388{
18389 char_u *tab;
18390 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018391 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392
18393 if (!writing && (find_viminfo_parameter('!') != NULL))
18394 {
18395 tab = vim_strchr(virp->vir_line + 1, '\t');
18396 if (tab != NULL)
18397 {
18398 *tab++ = '\0'; /* isolate the variable name */
18399 if (*tab == 'S') /* string var */
18400 is_string = TRUE;
18401
18402 tab = vim_strchr(tab, '\t');
18403 if (tab != NULL)
18404 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018405 if (is_string)
18406 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018407 tv.v_type = VAR_STRING;
18408 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018410 }
18411 else
18412 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018413 tv.v_type = VAR_NUMBER;
18414 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018416 set_var(virp->vir_line + 1, &tv, FALSE);
18417 if (is_string)
18418 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 }
18420 }
18421 }
18422
18423 return viminfo_readline(virp);
18424}
18425
18426/*
18427 * Write global vars that start with a capital to the viminfo file
18428 */
18429 void
18430write_viminfo_varlist(fp)
18431 FILE *fp;
18432{
Bram Moolenaar33570922005-01-25 22:26:29 +000018433 hashitem_T *hi;
18434 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018435 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018436 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018437 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018438 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018439 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440
18441 if (find_viminfo_parameter('!') == NULL)
18442 return;
18443
18444 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018445
Bram Moolenaar33570922005-01-25 22:26:29 +000018446 todo = globvarht.ht_used;
18447 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018449 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018451 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018452 this_var = HI2DI(hi);
18453 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018454 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018455 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018456 {
18457 case VAR_STRING: s = "STR"; break;
18458 case VAR_NUMBER: s = "NUM"; break;
18459 default: continue;
18460 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018461 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018462 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18463 if (p != NULL)
18464 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018465 vim_free(tofree);
18466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018467 }
18468 }
18469}
18470#endif
18471
18472#if defined(FEAT_SESSION) || defined(PROTO)
18473 int
18474store_session_globals(fd)
18475 FILE *fd;
18476{
Bram Moolenaar33570922005-01-25 22:26:29 +000018477 hashitem_T *hi;
18478 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018479 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480 char_u *p, *t;
18481
Bram Moolenaar33570922005-01-25 22:26:29 +000018482 todo = globvarht.ht_used;
18483 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018485 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018487 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018488 this_var = HI2DI(hi);
18489 if ((this_var->di_tv.v_type == VAR_NUMBER
18490 || this_var->di_tv.v_type == VAR_STRING)
18491 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018492 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018493 /* Escape special characters with a backslash. Turn a LF and
18494 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018495 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018496 (char_u *)"\\\"\n\r");
18497 if (p == NULL) /* out of memory */
18498 break;
18499 for (t = p; *t != NUL; ++t)
18500 if (*t == '\n')
18501 *t = 'n';
18502 else if (*t == '\r')
18503 *t = 'r';
18504 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000018505 this_var->di_key,
18506 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18507 : ' ',
18508 p,
18509 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18510 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000018511 || put_eol(fd) == FAIL)
18512 {
18513 vim_free(p);
18514 return FAIL;
18515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516 vim_free(p);
18517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 }
18519 }
18520 return OK;
18521}
18522#endif
18523
18524#endif /* FEAT_EVAL */
18525
18526#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
18527
18528
18529#ifdef WIN3264
18530/*
18531 * Functions for ":8" filename modifier: get 8.3 version of a filename.
18532 */
18533static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18534static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
18535static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18536
18537/*
18538 * Get the short pathname of a file.
18539 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
18540 */
18541 static int
18542get_short_pathname(fnamep, bufp, fnamelen)
18543 char_u **fnamep;
18544 char_u **bufp;
18545 int *fnamelen;
18546{
18547 int l,len;
18548 char_u *newbuf;
18549
18550 len = *fnamelen;
18551
18552 l = GetShortPathName(*fnamep, *fnamep, len);
18553 if (l > len - 1)
18554 {
18555 /* If that doesn't work (not enough space), then save the string
18556 * and try again with a new buffer big enough
18557 */
18558 newbuf = vim_strnsave(*fnamep, l);
18559 if (newbuf == NULL)
18560 return 0;
18561
18562 vim_free(*bufp);
18563 *fnamep = *bufp = newbuf;
18564
18565 l = GetShortPathName(*fnamep,*fnamep,l+1);
18566
18567 /* Really should always succeed, as the buffer is big enough */
18568 }
18569
18570 *fnamelen = l;
18571 return 1;
18572}
18573
18574/*
18575 * Create a short path name. Returns the length of the buffer it needs.
18576 * Doesn't copy over the end of the buffer passed in.
18577 */
18578 static int
18579shortpath_for_invalid_fname(fname, bufp, fnamelen)
18580 char_u **fname;
18581 char_u **bufp;
18582 int *fnamelen;
18583{
18584 char_u *s, *p, *pbuf2, *pbuf3;
18585 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018586 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587
18588 /* Make a copy */
18589 len2 = *fnamelen;
18590 pbuf2 = vim_strnsave(*fname, len2);
18591 pbuf3 = NULL;
18592
18593 s = pbuf2 + len2 - 1; /* Find the end */
18594 slen = 1;
18595 plen = len2;
18596
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018597 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 {
18599 --s;
18600 ++slen;
18601 --plen;
18602 }
18603
18604 do
18605 {
18606 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018607 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608 {
18609 --s;
18610 ++slen;
18611 --plen;
18612 }
18613 if (s <= pbuf2)
18614 break;
18615
18616 /* Remeber the character that is about to be blatted */
18617 ch = *s;
18618 *s = 0; /* get_short_pathname requires a null-terminated string */
18619
18620 /* Try it in situ */
18621 p = pbuf2;
18622 if (!get_short_pathname(&p, &pbuf3, &plen))
18623 {
18624 vim_free(pbuf2);
18625 return -1;
18626 }
18627 *s = ch; /* Preserve the string */
18628 } while (plen == 0);
18629
18630 if (plen > 0)
18631 {
18632 /* Remeber the length of the new string. */
18633 *fnamelen = len = plen + slen;
18634 vim_free(*bufp);
18635 if (len > len2)
18636 {
18637 /* If there's not enough space in the currently allocated string,
18638 * then copy it to a buffer big enough.
18639 */
18640 *fname= *bufp = vim_strnsave(p, len);
18641 if (*fname == NULL)
18642 return -1;
18643 }
18644 else
18645 {
18646 /* Transfer pbuf2 to being the main buffer (it's big enough) */
18647 *fname = *bufp = pbuf2;
18648 if (p != pbuf2)
18649 strncpy(*fname, p, plen);
18650 pbuf2 = NULL;
18651 }
18652 /* Concat the next bit */
18653 strncpy(*fname + plen, s, slen);
18654 (*fname)[len] = '\0';
18655 }
18656 vim_free(pbuf3);
18657 vim_free(pbuf2);
18658 return 0;
18659}
18660
18661/*
18662 * Get a pathname for a partial path.
18663 */
18664 static int
18665shortpath_for_partial(fnamep, bufp, fnamelen)
18666 char_u **fnamep;
18667 char_u **bufp;
18668 int *fnamelen;
18669{
18670 int sepcount, len, tflen;
18671 char_u *p;
18672 char_u *pbuf, *tfname;
18673 int hasTilde;
18674
18675 /* Count up the path seperators from the RHS.. so we know which part
18676 * of the path to return.
18677 */
18678 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018679 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680 if (vim_ispathsep(*p))
18681 ++sepcount;
18682
18683 /* Need full path first (use expand_env() to remove a "~/") */
18684 hasTilde = (**fnamep == '~');
18685 if (hasTilde)
18686 pbuf = tfname = expand_env_save(*fnamep);
18687 else
18688 pbuf = tfname = FullName_save(*fnamep, FALSE);
18689
18690 len = tflen = STRLEN(tfname);
18691
18692 if (!get_short_pathname(&tfname, &pbuf, &len))
18693 return -1;
18694
18695 if (len == 0)
18696 {
18697 /* Don't have a valid filename, so shorten the rest of the
18698 * path if we can. This CAN give us invalid 8.3 filenames, but
18699 * there's not a lot of point in guessing what it might be.
18700 */
18701 len = tflen;
18702 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
18703 return -1;
18704 }
18705
18706 /* Count the paths backward to find the beginning of the desired string. */
18707 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018708 {
18709#ifdef FEAT_MBYTE
18710 if (has_mbyte)
18711 p -= mb_head_off(tfname, p);
18712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713 if (vim_ispathsep(*p))
18714 {
18715 if (sepcount == 0 || (hasTilde && sepcount == 1))
18716 break;
18717 else
18718 sepcount --;
18719 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 if (hasTilde)
18722 {
18723 --p;
18724 if (p >= tfname)
18725 *p = '~';
18726 else
18727 return -1;
18728 }
18729 else
18730 ++p;
18731
18732 /* Copy in the string - p indexes into tfname - allocated at pbuf */
18733 vim_free(*bufp);
18734 *fnamelen = (int)STRLEN(p);
18735 *bufp = pbuf;
18736 *fnamep = p;
18737
18738 return 0;
18739}
18740#endif /* WIN3264 */
18741
18742/*
18743 * Adjust a filename, according to a string of modifiers.
18744 * *fnamep must be NUL terminated when called. When returning, the length is
18745 * determined by *fnamelen.
18746 * Returns valid flags.
18747 * When there is an error, *fnamep is set to NULL.
18748 */
18749 int
18750modify_fname(src, usedlen, fnamep, bufp, fnamelen)
18751 char_u *src; /* string with modifiers */
18752 int *usedlen; /* characters after src that are used */
18753 char_u **fnamep; /* file name so far */
18754 char_u **bufp; /* buffer for allocated file name or NULL */
18755 int *fnamelen; /* length of fnamep */
18756{
18757 int valid = 0;
18758 char_u *tail;
18759 char_u *s, *p, *pbuf;
18760 char_u dirname[MAXPATHL];
18761 int c;
18762 int has_fullname = 0;
18763#ifdef WIN3264
18764 int has_shortname = 0;
18765#endif
18766
18767repeat:
18768 /* ":p" - full path/file_name */
18769 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
18770 {
18771 has_fullname = 1;
18772
18773 valid |= VALID_PATH;
18774 *usedlen += 2;
18775
18776 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
18777 if ((*fnamep)[0] == '~'
18778#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
18779 && ((*fnamep)[1] == '/'
18780# ifdef BACKSLASH_IN_FILENAME
18781 || (*fnamep)[1] == '\\'
18782# endif
18783 || (*fnamep)[1] == NUL)
18784
18785#endif
18786 )
18787 {
18788 *fnamep = expand_env_save(*fnamep);
18789 vim_free(*bufp); /* free any allocated file name */
18790 *bufp = *fnamep;
18791 if (*fnamep == NULL)
18792 return -1;
18793 }
18794
18795 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018796 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 {
18798 if (vim_ispathsep(*p)
18799 && p[1] == '.'
18800 && (p[2] == NUL
18801 || vim_ispathsep(p[2])
18802 || (p[2] == '.'
18803 && (p[3] == NUL || vim_ispathsep(p[3])))))
18804 break;
18805 }
18806
18807 /* FullName_save() is slow, don't use it when not needed. */
18808 if (*p != NUL || !vim_isAbsName(*fnamep))
18809 {
18810 *fnamep = FullName_save(*fnamep, *p != NUL);
18811 vim_free(*bufp); /* free any allocated file name */
18812 *bufp = *fnamep;
18813 if (*fnamep == NULL)
18814 return -1;
18815 }
18816
18817 /* Append a path separator to a directory. */
18818 if (mch_isdir(*fnamep))
18819 {
18820 /* Make room for one or two extra characters. */
18821 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
18822 vim_free(*bufp); /* free any allocated file name */
18823 *bufp = *fnamep;
18824 if (*fnamep == NULL)
18825 return -1;
18826 add_pathsep(*fnamep);
18827 }
18828 }
18829
18830 /* ":." - path relative to the current directory */
18831 /* ":~" - path relative to the home directory */
18832 /* ":8" - shortname path - postponed till after */
18833 while (src[*usedlen] == ':'
18834 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
18835 {
18836 *usedlen += 2;
18837 if (c == '8')
18838 {
18839#ifdef WIN3264
18840 has_shortname = 1; /* Postpone this. */
18841#endif
18842 continue;
18843 }
18844 pbuf = NULL;
18845 /* Need full path first (use expand_env() to remove a "~/") */
18846 if (!has_fullname)
18847 {
18848 if (c == '.' && **fnamep == '~')
18849 p = pbuf = expand_env_save(*fnamep);
18850 else
18851 p = pbuf = FullName_save(*fnamep, FALSE);
18852 }
18853 else
18854 p = *fnamep;
18855
18856 has_fullname = 0;
18857
18858 if (p != NULL)
18859 {
18860 if (c == '.')
18861 {
18862 mch_dirname(dirname, MAXPATHL);
18863 s = shorten_fname(p, dirname);
18864 if (s != NULL)
18865 {
18866 *fnamep = s;
18867 if (pbuf != NULL)
18868 {
18869 vim_free(*bufp); /* free any allocated file name */
18870 *bufp = pbuf;
18871 pbuf = NULL;
18872 }
18873 }
18874 }
18875 else
18876 {
18877 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
18878 /* Only replace it when it starts with '~' */
18879 if (*dirname == '~')
18880 {
18881 s = vim_strsave(dirname);
18882 if (s != NULL)
18883 {
18884 *fnamep = s;
18885 vim_free(*bufp);
18886 *bufp = s;
18887 }
18888 }
18889 }
18890 vim_free(pbuf);
18891 }
18892 }
18893
18894 tail = gettail(*fnamep);
18895 *fnamelen = (int)STRLEN(*fnamep);
18896
18897 /* ":h" - head, remove "/file_name", can be repeated */
18898 /* Don't remove the first "/" or "c:\" */
18899 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
18900 {
18901 valid |= VALID_HEAD;
18902 *usedlen += 2;
18903 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018904 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905 --tail;
18906 *fnamelen = (int)(tail - *fnamep);
18907#ifdef VMS
18908 if (*fnamelen > 0)
18909 *fnamelen += 1; /* the path separator is part of the path */
18910#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018911 while (tail > s && !after_pathsep(s, tail))
18912 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913 }
18914
18915 /* ":8" - shortname */
18916 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
18917 {
18918 *usedlen += 2;
18919#ifdef WIN3264
18920 has_shortname = 1;
18921#endif
18922 }
18923
18924#ifdef WIN3264
18925 /* Check shortname after we have done 'heads' and before we do 'tails'
18926 */
18927 if (has_shortname)
18928 {
18929 pbuf = NULL;
18930 /* Copy the string if it is shortened by :h */
18931 if (*fnamelen < (int)STRLEN(*fnamep))
18932 {
18933 p = vim_strnsave(*fnamep, *fnamelen);
18934 if (p == 0)
18935 return -1;
18936 vim_free(*bufp);
18937 *bufp = *fnamep = p;
18938 }
18939
18940 /* Split into two implementations - makes it easier. First is where
18941 * there isn't a full name already, second is where there is.
18942 */
18943 if (!has_fullname && !vim_isAbsName(*fnamep))
18944 {
18945 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
18946 return -1;
18947 }
18948 else
18949 {
18950 int l;
18951
18952 /* Simple case, already have the full-name
18953 * Nearly always shorter, so try first time. */
18954 l = *fnamelen;
18955 if (!get_short_pathname(fnamep, bufp, &l))
18956 return -1;
18957
18958 if (l == 0)
18959 {
18960 /* Couldn't find the filename.. search the paths.
18961 */
18962 l = *fnamelen;
18963 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
18964 return -1;
18965 }
18966 *fnamelen = l;
18967 }
18968 }
18969#endif /* WIN3264 */
18970
18971 /* ":t" - tail, just the basename */
18972 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
18973 {
18974 *usedlen += 2;
18975 *fnamelen -= (int)(tail - *fnamep);
18976 *fnamep = tail;
18977 }
18978
18979 /* ":e" - extension, can be repeated */
18980 /* ":r" - root, without extension, can be repeated */
18981 while (src[*usedlen] == ':'
18982 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
18983 {
18984 /* find a '.' in the tail:
18985 * - for second :e: before the current fname
18986 * - otherwise: The last '.'
18987 */
18988 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
18989 s = *fnamep - 2;
18990 else
18991 s = *fnamep + *fnamelen - 1;
18992 for ( ; s > tail; --s)
18993 if (s[0] == '.')
18994 break;
18995 if (src[*usedlen + 1] == 'e') /* :e */
18996 {
18997 if (s > tail)
18998 {
18999 *fnamelen += (int)(*fnamep - (s + 1));
19000 *fnamep = s + 1;
19001#ifdef VMS
19002 /* cut version from the extension */
19003 s = *fnamep + *fnamelen - 1;
19004 for ( ; s > *fnamep; --s)
19005 if (s[0] == ';')
19006 break;
19007 if (s > *fnamep)
19008 *fnamelen = s - *fnamep;
19009#endif
19010 }
19011 else if (*fnamep <= tail)
19012 *fnamelen = 0;
19013 }
19014 else /* :r */
19015 {
19016 if (s > tail) /* remove one extension */
19017 *fnamelen = (int)(s - *fnamep);
19018 }
19019 *usedlen += 2;
19020 }
19021
19022 /* ":s?pat?foo?" - substitute */
19023 /* ":gs?pat?foo?" - global substitute */
19024 if (src[*usedlen] == ':'
19025 && (src[*usedlen + 1] == 's'
19026 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19027 {
19028 char_u *str;
19029 char_u *pat;
19030 char_u *sub;
19031 int sep;
19032 char_u *flags;
19033 int didit = FALSE;
19034
19035 flags = (char_u *)"";
19036 s = src + *usedlen + 2;
19037 if (src[*usedlen + 1] == 'g')
19038 {
19039 flags = (char_u *)"g";
19040 ++s;
19041 }
19042
19043 sep = *s++;
19044 if (sep)
19045 {
19046 /* find end of pattern */
19047 p = vim_strchr(s, sep);
19048 if (p != NULL)
19049 {
19050 pat = vim_strnsave(s, (int)(p - s));
19051 if (pat != NULL)
19052 {
19053 s = p + 1;
19054 /* find end of substitution */
19055 p = vim_strchr(s, sep);
19056 if (p != NULL)
19057 {
19058 sub = vim_strnsave(s, (int)(p - s));
19059 str = vim_strnsave(*fnamep, *fnamelen);
19060 if (sub != NULL && str != NULL)
19061 {
19062 *usedlen = (int)(p + 1 - src);
19063 s = do_string_sub(str, pat, sub, flags);
19064 if (s != NULL)
19065 {
19066 *fnamep = s;
19067 *fnamelen = (int)STRLEN(s);
19068 vim_free(*bufp);
19069 *bufp = s;
19070 didit = TRUE;
19071 }
19072 }
19073 vim_free(sub);
19074 vim_free(str);
19075 }
19076 vim_free(pat);
19077 }
19078 }
19079 /* after using ":s", repeat all the modifiers */
19080 if (didit)
19081 goto repeat;
19082 }
19083 }
19084
19085 return valid;
19086}
19087
19088/*
19089 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19090 * "flags" can be "g" to do a global substitute.
19091 * Returns an allocated string, NULL for error.
19092 */
19093 char_u *
19094do_string_sub(str, pat, sub, flags)
19095 char_u *str;
19096 char_u *pat;
19097 char_u *sub;
19098 char_u *flags;
19099{
19100 int sublen;
19101 regmatch_T regmatch;
19102 int i;
19103 int do_all;
19104 char_u *tail;
19105 garray_T ga;
19106 char_u *ret;
19107 char_u *save_cpo;
19108
19109 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19110 save_cpo = p_cpo;
19111 p_cpo = (char_u *)"";
19112
19113 ga_init2(&ga, 1, 200);
19114
19115 do_all = (flags[0] == 'g');
19116
19117 regmatch.rm_ic = p_ic;
19118 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19119 if (regmatch.regprog != NULL)
19120 {
19121 tail = str;
19122 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19123 {
19124 /*
19125 * Get some space for a temporary buffer to do the substitution
19126 * into. It will contain:
19127 * - The text up to where the match is.
19128 * - The substituted text.
19129 * - The text after the match.
19130 */
19131 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19132 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19133 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19134 {
19135 ga_clear(&ga);
19136 break;
19137 }
19138
19139 /* copy the text up to where the match is */
19140 i = (int)(regmatch.startp[0] - tail);
19141 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19142 /* add the substituted text */
19143 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19144 + ga.ga_len + i, TRUE, TRUE, FALSE);
19145 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019146 /* avoid getting stuck on a match with an empty string */
19147 if (tail == regmatch.endp[0])
19148 {
19149 if (*tail == NUL)
19150 break;
19151 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19152 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 }
19154 else
19155 {
19156 tail = regmatch.endp[0];
19157 if (*tail == NUL)
19158 break;
19159 }
19160 if (!do_all)
19161 break;
19162 }
19163
19164 if (ga.ga_data != NULL)
19165 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19166
19167 vim_free(regmatch.regprog);
19168 }
19169
19170 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19171 ga_clear(&ga);
19172 p_cpo = save_cpo;
19173
19174 return ret;
19175}
19176
19177#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */