blob: 56e534e6dca68458f8acc764de24485b836abd95 [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)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#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 Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000344};
345
346/* shorthand */
347#define vv_type vv_di.di_tv.v_type
348#define vv_nr vv_di.di_tv.vval.v_number
349#define vv_str vv_di.di_tv.vval.v_string
350#define vv_tv vv_di.di_tv
351
352/*
353 * The v: variables are stored in dictionary "vimvardict".
354 * "vimvars_var" is the variable that is used for the "l:" scope.
355 */
356static dict_T vimvardict;
357static dictitem_T vimvars_var;
358#define vimvarht vimvardict.dv_hashtab
359
Bram Moolenaara40058a2005-07-11 22:42:07 +0000360static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
361static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
362#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
363static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
364#endif
365static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
366static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
367static char_u *skip_var_one __ARGS((char_u *arg));
368static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
369static void list_glob_vars __ARGS((void));
370static void list_buf_vars __ARGS((void));
371static void list_win_vars __ARGS((void));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000372#ifdef FEAT_WINDOWS
373static void list_tab_vars __ARGS((void));
374#endif
Bram Moolenaara40058a2005-07-11 22:42:07 +0000375static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000376static void list_script_vars __ARGS((void));
377static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000378static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
379static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
380static int check_changedtick __ARGS((char_u *arg));
381static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
382static void clear_lval __ARGS((lval_T *lp));
383static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
384static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
385static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
386static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
387static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
388static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
389static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
390static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
391static void item_lock __ARGS((typval_T *tv, int deep, int lock));
392static int tv_islocked __ARGS((typval_T *tv));
393
Bram Moolenaar33570922005-01-25 22:26:29 +0000394static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
395static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
397static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
399static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000403static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000404static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000408static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000409static listitem_T *listitem_alloc __ARGS((void));
410static void listitem_free __ARGS((listitem_T *item));
411static void listitem_remove __ARGS((list_T *l, listitem_T *item));
412static long list_len __ARGS((list_T *l));
413static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
414static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
415static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000417static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000419static void list_append __ARGS((list_T *l, listitem_T *item));
420static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000421static int list_append_string __ARGS((list_T *l, char_u *str, int len));
422static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
424static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
425static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000426static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000428static char_u *list2string __ARGS((typval_T *tv, int copyID));
429static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000430static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
431static void set_ref_in_list __ARGS((list_T *l, int copyID));
432static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static void dict_unref __ARGS((dict_T *d));
434static void dict_free __ARGS((dict_T *d));
435static dictitem_T *dictitem_alloc __ARGS((char_u *key));
436static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
437static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
438static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static int dict_add __ARGS((dict_T *d, dictitem_T *item));
441static long dict_len __ARGS((dict_T *d));
442static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000445static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
446static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static char_u *string_quote __ARGS((char_u *str, int function));
448static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
449static int find_internal_func __ARGS((char_u *name));
450static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
451static 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));
452static 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 +0000453static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454
455static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000471static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000475#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000476static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000477static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
479#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
485static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000498static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000499static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000512static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000513static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000514static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000520static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000528static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000529static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000530static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000532static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000533static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000553static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000554static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000559static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000575static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000576static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000577static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000581#ifdef vim_mkdir
582static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
583#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000584static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000587static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000589static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000590static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000592static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000593static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000606static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000607static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000608static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000610static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000615static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000616static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000617static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000618static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000619static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000623static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000624static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000627static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628#ifdef HAVE_STRFTIME
629static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
630#endif
631static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000643static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000644static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000645static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000646static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000647static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000649static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000663static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000665static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000666static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000668static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
669static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static int get_env_len __ARGS((char_u **arg));
671static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000672static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000673static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
674#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
675#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
676 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000677static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000679static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000680static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
681static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static typval_T *alloc_tv __ARGS((void));
683static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static void init_tv __ARGS((typval_T *varp));
685static long get_tv_number __ARGS((typval_T *varp));
686static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000687static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static char_u *get_tv_string __ARGS((typval_T *varp));
689static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000690static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000692static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
694static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
695static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
696static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
697static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
698static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
699static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000700static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000702static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
704static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
705static int eval_fname_script __ARGS((char_u *p));
706static int eval_fname_sid __ARGS((char_u *p));
707static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static ufunc_T *find_func __ARGS((char_u *name));
709static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000710static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000711#ifdef FEAT_PROFILE
712static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000713static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
714static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
715static int
716# ifdef __BORLANDC__
717 _RTLENTRYF
718# endif
719 prof_total_cmp __ARGS((const void *s1, const void *s2));
720static int
721# ifdef __BORLANDC__
722 _RTLENTRYF
723# endif
724 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000725#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000726static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000727static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000728static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000729static void func_free __ARGS((ufunc_T *fp));
730static void func_unref __ARGS((char_u *name));
731static void func_ref __ARGS((char_u *name));
732static 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));
733static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000734static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
735static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000736static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000737static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000738static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000739
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000740/* Character used as separated in autoload function/variable names. */
741#define AUTOLOAD_CHAR '#'
742
Bram Moolenaar33570922005-01-25 22:26:29 +0000743/*
744 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000745 */
746 void
747eval_init()
748{
Bram Moolenaar33570922005-01-25 22:26:29 +0000749 int i;
750 struct vimvar *p;
751
752 init_var_dict(&globvardict, &globvars_var);
753 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000754 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000755 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000756
757 for (i = 0; i < VV_LEN; ++i)
758 {
759 p = &vimvars[i];
760 STRCPY(p->vv_di.di_key, p->vv_name);
761 if (p->vv_flags & VV_RO)
762 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
763 else if (p->vv_flags & VV_RO_SBX)
764 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
765 else
766 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000767
768 /* add to v: scope dict, unless the value is not always available */
769 if (p->vv_type != VAR_UNKNOWN)
770 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000771 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000772 /* add to compat scope dict */
773 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000774 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000775}
776
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000777#if defined(EXITFREE) || defined(PROTO)
778 void
779eval_clear()
780{
781 int i;
782 struct vimvar *p;
783
784 for (i = 0; i < VV_LEN; ++i)
785 {
786 p = &vimvars[i];
787 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000788 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000789 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000790 p->vv_di.di_tv.vval.v_string = NULL;
791 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000792 }
793 hash_clear(&vimvarht);
794 hash_clear(&compat_hashtab);
795
796 /* script-local variables */
797 for (i = 1; i <= ga_scripts.ga_len; ++i)
798 vars_clear(&SCRIPT_VARS(i));
799 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000800 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000801
802 /* global variables */
803 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000804
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000805 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000806 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000807 hash_clear(&func_hashtab);
808
809 /* unreferenced lists and dicts */
810 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000811}
812#endif
813
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 * Return the name of the executed function.
816 */
817 char_u *
818func_name(cookie)
819 void *cookie;
820{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000821 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822}
823
824/*
825 * Return the address holding the next breakpoint line for a funccall cookie.
826 */
827 linenr_T *
828func_breakpoint(cookie)
829 void *cookie;
830{
Bram Moolenaar33570922005-01-25 22:26:29 +0000831 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832}
833
834/*
835 * Return the address holding the debug tick for a funccall cookie.
836 */
837 int *
838func_dbg_tick(cookie)
839 void *cookie;
840{
Bram Moolenaar33570922005-01-25 22:26:29 +0000841 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843
844/*
845 * Return the nesting level for a funccall cookie.
846 */
847 int
848func_level(cookie)
849 void *cookie;
850{
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852}
853
854/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000855funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
857/*
858 * Return TRUE when a function was ended by a ":return" command.
859 */
860 int
861current_func_returned()
862{
863 return current_funccal->returned;
864}
865
866
867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 * Set an internal variable to a string value. Creates the variable if it does
869 * not already exist.
870 */
871 void
872set_internal_string_var(name, value)
873 char_u *name;
874 char_u *value;
875{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000876 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000877 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
879 val = vim_strsave(value);
880 if (val != NULL)
881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000882 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000883 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000885 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000886 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 }
888 }
889}
890
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000891static lval_T *redir_lval = NULL;
892static char_u *redir_endp = NULL;
893static char_u *redir_varname = NULL;
894
895/*
896 * Start recording command output to a variable
897 * Returns OK if successfully completed the setup. FAIL otherwise.
898 */
899 int
900var_redir_start(name, append)
901 char_u *name;
902 int append; /* append to an existing variable */
903{
904 int save_emsg;
905 int err;
906 typval_T tv;
907
908 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000909 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000910 {
911 EMSG(_(e_invarg));
912 return FAIL;
913 }
914
915 redir_varname = vim_strsave(name);
916 if (redir_varname == NULL)
917 return FAIL;
918
919 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
920 if (redir_lval == NULL)
921 {
922 var_redir_stop();
923 return FAIL;
924 }
925
926 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000927 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
928 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000929 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
930 {
931 if (redir_endp != NULL && *redir_endp != NUL)
932 /* Trailing characters are present after the variable name */
933 EMSG(_(e_trailing));
934 else
935 EMSG(_(e_invarg));
936 var_redir_stop();
937 return FAIL;
938 }
939
940 /* check if we can write to the variable: set it to or append an empty
941 * string */
942 save_emsg = did_emsg;
943 did_emsg = FALSE;
944 tv.v_type = VAR_STRING;
945 tv.vval.v_string = (char_u *)"";
946 if (append)
947 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
948 else
949 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
950 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000951 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000952 if (err)
953 {
954 var_redir_stop();
955 return FAIL;
956 }
957 if (redir_lval->ll_newkey != NULL)
958 {
959 /* Dictionary item was created, don't do it again. */
960 vim_free(redir_lval->ll_newkey);
961 redir_lval->ll_newkey = NULL;
962 }
963
964 return OK;
965}
966
967/*
968 * Append "value[len]" to the variable set by var_redir_start().
969 */
970 void
971var_redir_str(value, len)
972 char_u *value;
973 int len;
974{
975 char_u *val;
976 typval_T tv;
977 int save_emsg;
978 int err;
979
980 if (redir_lval == NULL)
981 return;
982
983 if (len == -1)
984 /* Append the entire string */
985 val = vim_strsave(value);
986 else
987 /* Append only the specified number of characters */
988 val = vim_strnsave(value, len);
989 if (val == NULL)
990 return;
991
992 tv.v_type = VAR_STRING;
993 tv.vval.v_string = val;
994
995 save_emsg = did_emsg;
996 did_emsg = FALSE;
997 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
998 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000999 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001000 if (err)
1001 var_redir_stop();
1002
1003 vim_free(tv.vval.v_string);
1004}
1005
1006/*
1007 * Stop redirecting command output to a variable.
1008 */
1009 void
1010var_redir_stop()
1011{
1012 if (redir_lval != NULL)
1013 {
1014 clear_lval(redir_lval);
1015 vim_free(redir_lval);
1016 redir_lval = NULL;
1017 }
1018 vim_free(redir_varname);
1019 redir_varname = NULL;
1020}
1021
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022# if defined(FEAT_MBYTE) || defined(PROTO)
1023 int
1024eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1025 char_u *enc_from;
1026 char_u *enc_to;
1027 char_u *fname_from;
1028 char_u *fname_to;
1029{
1030 int err = FALSE;
1031
1032 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1033 set_vim_var_string(VV_CC_TO, enc_to, -1);
1034 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1035 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1036 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1037 err = TRUE;
1038 set_vim_var_string(VV_CC_FROM, NULL, -1);
1039 set_vim_var_string(VV_CC_TO, NULL, -1);
1040 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1041 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1042
1043 if (err)
1044 return FAIL;
1045 return OK;
1046}
1047# endif
1048
1049# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1050 int
1051eval_printexpr(fname, args)
1052 char_u *fname;
1053 char_u *args;
1054{
1055 int err = FALSE;
1056
1057 set_vim_var_string(VV_FNAME_IN, fname, -1);
1058 set_vim_var_string(VV_CMDARG, args, -1);
1059 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1060 err = TRUE;
1061 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1062 set_vim_var_string(VV_CMDARG, NULL, -1);
1063
1064 if (err)
1065 {
1066 mch_remove(fname);
1067 return FAIL;
1068 }
1069 return OK;
1070}
1071# endif
1072
1073# if defined(FEAT_DIFF) || defined(PROTO)
1074 void
1075eval_diff(origfile, newfile, outfile)
1076 char_u *origfile;
1077 char_u *newfile;
1078 char_u *outfile;
1079{
1080 int err = FALSE;
1081
1082 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1083 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1084 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1085 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1086 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1087 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1088 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1089}
1090
1091 void
1092eval_patch(origfile, difffile, outfile)
1093 char_u *origfile;
1094 char_u *difffile;
1095 char_u *outfile;
1096{
1097 int err;
1098
1099 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1100 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1101 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1102 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1103 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1104 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1105 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1106}
1107# endif
1108
1109/*
1110 * Top level evaluation function, returning a boolean.
1111 * Sets "error" to TRUE if there was an error.
1112 * Return TRUE or FALSE.
1113 */
1114 int
1115eval_to_bool(arg, error, nextcmd, skip)
1116 char_u *arg;
1117 int *error;
1118 char_u **nextcmd;
1119 int skip; /* only parse, don't execute */
1120{
Bram Moolenaar33570922005-01-25 22:26:29 +00001121 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 int retval = FALSE;
1123
1124 if (skip)
1125 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001126 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 else
1129 {
1130 *error = FALSE;
1131 if (!skip)
1132 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001133 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001134 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 }
1136 }
1137 if (skip)
1138 --emsg_skip;
1139
1140 return retval;
1141}
1142
1143/*
1144 * Top level evaluation function, returning a string. If "skip" is TRUE,
1145 * only parsing to "nextcmd" is done, without reporting errors. Return
1146 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1147 */
1148 char_u *
1149eval_to_string_skip(arg, nextcmd, skip)
1150 char_u *arg;
1151 char_u **nextcmd;
1152 int skip; /* only parse, don't execute */
1153{
Bram Moolenaar33570922005-01-25 22:26:29 +00001154 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 char_u *retval;
1156
1157 if (skip)
1158 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001159 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 retval = NULL;
1161 else
1162 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001163 retval = vim_strsave(get_tv_string(&tv));
1164 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 }
1166 if (skip)
1167 --emsg_skip;
1168
1169 return retval;
1170}
1171
1172/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001173 * Skip over an expression at "*pp".
1174 * Return FAIL for an error, OK otherwise.
1175 */
1176 int
1177skip_expr(pp)
1178 char_u **pp;
1179{
Bram Moolenaar33570922005-01-25 22:26:29 +00001180 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001181
1182 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001184}
1185
1186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 * Top level evaluation function, returning a string.
1188 * Return pointer to allocated memory, or NULL for failure.
1189 */
1190 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001191eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 char_u *arg;
1193 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001194 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195{
Bram Moolenaar33570922005-01-25 22:26:29 +00001196 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001198 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001200 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 retval = NULL;
1202 else
1203 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001204 if (dolist && tv.v_type == VAR_LIST)
1205 {
1206 ga_init2(&ga, (int)sizeof(char), 80);
1207 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1208 ga_append(&ga, NUL);
1209 retval = (char_u *)ga.ga_data;
1210 }
1211 else
1212 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001213 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 }
1215
1216 return retval;
1217}
1218
1219/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001220 * Call eval_to_string() without using current local variables and using
1221 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 */
1223 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001224eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 char_u *arg;
1226 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001227 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228{
1229 char_u *retval;
1230 void *save_funccalp;
1231
1232 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001233 if (use_sandbox)
1234 ++sandbox;
1235 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001236 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001237 if (use_sandbox)
1238 --sandbox;
1239 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 restore_funccal(save_funccalp);
1241 return retval;
1242}
1243
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244/*
1245 * Top level evaluation function, returning a number.
1246 * Evaluates "expr" silently.
1247 * Returns -1 for an error.
1248 */
1249 int
1250eval_to_number(expr)
1251 char_u *expr;
1252{
Bram Moolenaar33570922005-01-25 22:26:29 +00001253 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001255 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256
1257 ++emsg_off;
1258
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001259 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 retval = -1;
1261 else
1262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001263 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001264 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 }
1266 --emsg_off;
1267
1268 return retval;
1269}
1270
Bram Moolenaara40058a2005-07-11 22:42:07 +00001271/*
1272 * Prepare v: variable "idx" to be used.
1273 * Save the current typeval in "save_tv".
1274 * When not used yet add the variable to the v: hashtable.
1275 */
1276 static void
1277prepare_vimvar(idx, save_tv)
1278 int idx;
1279 typval_T *save_tv;
1280{
1281 *save_tv = vimvars[idx].vv_tv;
1282 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1283 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1284}
1285
1286/*
1287 * Restore v: variable "idx" to typeval "save_tv".
1288 * When no longer defined, remove the variable from the v: hashtable.
1289 */
1290 static void
1291restore_vimvar(idx, save_tv)
1292 int idx;
1293 typval_T *save_tv;
1294{
1295 hashitem_T *hi;
1296
1297 clear_tv(&vimvars[idx].vv_tv);
1298 vimvars[idx].vv_tv = *save_tv;
1299 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1300 {
1301 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1302 if (HASHITEM_EMPTY(hi))
1303 EMSG2(_(e_intern2), "restore_vimvar()");
1304 else
1305 hash_remove(&vimvarht, hi);
1306 }
1307}
1308
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001309#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001310/*
1311 * Evaluate an expression to a list with suggestions.
1312 * For the "expr:" part of 'spellsuggest'.
1313 */
1314 list_T *
1315eval_spell_expr(badword, expr)
1316 char_u *badword;
1317 char_u *expr;
1318{
1319 typval_T save_val;
1320 typval_T rettv;
1321 list_T *list = NULL;
1322 char_u *p = skipwhite(expr);
1323
1324 /* Set "v:val" to the bad word. */
1325 prepare_vimvar(VV_VAL, &save_val);
1326 vimvars[VV_VAL].vv_type = VAR_STRING;
1327 vimvars[VV_VAL].vv_str = badword;
1328 if (p_verbose == 0)
1329 ++emsg_off;
1330
1331 if (eval1(&p, &rettv, TRUE) == OK)
1332 {
1333 if (rettv.v_type != VAR_LIST)
1334 clear_tv(&rettv);
1335 else
1336 list = rettv.vval.v_list;
1337 }
1338
1339 if (p_verbose == 0)
1340 --emsg_off;
1341 vimvars[VV_VAL].vv_str = NULL;
1342 restore_vimvar(VV_VAL, &save_val);
1343
1344 return list;
1345}
1346
1347/*
1348 * "list" is supposed to contain two items: a word and a number. Return the
1349 * word in "pp" and the number as the return value.
1350 * Return -1 if anything isn't right.
1351 * Used to get the good word and score from the eval_spell_expr() result.
1352 */
1353 int
1354get_spellword(list, pp)
1355 list_T *list;
1356 char_u **pp;
1357{
1358 listitem_T *li;
1359
1360 li = list->lv_first;
1361 if (li == NULL)
1362 return -1;
1363 *pp = get_tv_string(&li->li_tv);
1364
1365 li = li->li_next;
1366 if (li == NULL)
1367 return -1;
1368 return get_tv_number(&li->li_tv);
1369}
1370#endif
1371
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001372/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001373 * Top level evaluation function.
1374 * Returns an allocated typval_T with the result.
1375 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001376 */
1377 typval_T *
1378eval_expr(arg, nextcmd)
1379 char_u *arg;
1380 char_u **nextcmd;
1381{
1382 typval_T *tv;
1383
1384 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001385 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001386 {
1387 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001388 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001389 }
1390
1391 return tv;
1392}
1393
1394
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1396/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001397 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001399 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001401 static int
1402call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 char_u *func;
1404 int argc;
1405 char_u **argv;
1406 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
Bram Moolenaar33570922005-01-25 22:26:29 +00001409 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 long n;
1411 int len;
1412 int i;
1413 int doesrange;
1414 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001415 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001417 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001419 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
1421 for (i = 0; i < argc; i++)
1422 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001423 /* Pass a NULL or empty argument as an empty string */
1424 if (argv[i] == NULL || *argv[i] == NUL)
1425 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001426 argvars[i].v_type = VAR_STRING;
1427 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001428 continue;
1429 }
1430
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 /* Recognize a number argument, the others must be strings. */
1432 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1433 if (len != 0 && len == (int)STRLEN(argv[i]))
1434 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001435 argvars[i].v_type = VAR_NUMBER;
1436 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 }
1438 else
1439 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001440 argvars[i].v_type = VAR_STRING;
1441 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 }
1443 }
1444
1445 if (safe)
1446 {
1447 save_funccalp = save_funccal();
1448 ++sandbox;
1449 }
1450
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001451 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1452 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001454 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 if (safe)
1456 {
1457 --sandbox;
1458 restore_funccal(save_funccalp);
1459 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001460 vim_free(argvars);
1461
1462 if (ret == FAIL)
1463 clear_tv(rettv);
1464
1465 return ret;
1466}
1467
1468/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001469 * Call vimL function "func" and return the result as a string.
1470 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001471 * Uses argv[argc] for the function arguments.
1472 */
1473 void *
1474call_func_retstr(func, argc, argv, safe)
1475 char_u *func;
1476 int argc;
1477 char_u **argv;
1478 int safe; /* use the sandbox */
1479{
1480 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001481 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001482
1483 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1484 return NULL;
1485
1486 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001487 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 return retval;
1489}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001490
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001491#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001492/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001493 * Call vimL function "func" and return the result as a number.
1494 * Returns -1 when calling the function fails.
1495 * Uses argv[argc] for the function arguments.
1496 */
1497 long
1498call_func_retnr(func, argc, argv, safe)
1499 char_u *func;
1500 int argc;
1501 char_u **argv;
1502 int safe; /* use the sandbox */
1503{
1504 typval_T rettv;
1505 long retval;
1506
1507 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1508 return -1;
1509
1510 retval = get_tv_number_chk(&rettv, NULL);
1511 clear_tv(&rettv);
1512 return retval;
1513}
1514#endif
1515
1516/*
1517 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001518 * Uses argv[argc] for the function arguments.
1519 */
1520 void *
1521call_func_retlist(func, argc, argv, safe)
1522 char_u *func;
1523 int argc;
1524 char_u **argv;
1525 int safe; /* use the sandbox */
1526{
1527 typval_T rettv;
1528
1529 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1530 return NULL;
1531
1532 if (rettv.v_type != VAR_LIST)
1533 {
1534 clear_tv(&rettv);
1535 return NULL;
1536 }
1537
1538 return rettv.vval.v_list;
1539}
1540
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541#endif
1542
1543/*
1544 * Save the current function call pointer, and set it to NULL.
1545 * Used when executing autocommands and for ":source".
1546 */
1547 void *
1548save_funccal()
1549{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001550 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 current_funccal = NULL;
1553 return (void *)fc;
1554}
1555
1556 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001557restore_funccal(vfc)
1558 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001560 funccall_T *fc = (funccall_T *)vfc;
1561
1562 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563}
1564
Bram Moolenaar05159a02005-02-26 23:04:13 +00001565#if defined(FEAT_PROFILE) || defined(PROTO)
1566/*
1567 * Prepare profiling for entering a child or something else that is not
1568 * counted for the script/function itself.
1569 * Should always be called in pair with prof_child_exit().
1570 */
1571 void
1572prof_child_enter(tm)
1573 proftime_T *tm; /* place to store waittime */
1574{
1575 funccall_T *fc = current_funccal;
1576
1577 if (fc != NULL && fc->func->uf_profiling)
1578 profile_start(&fc->prof_child);
1579 script_prof_save(tm);
1580}
1581
1582/*
1583 * Take care of time spent in a child.
1584 * Should always be called after prof_child_enter().
1585 */
1586 void
1587prof_child_exit(tm)
1588 proftime_T *tm; /* where waittime was stored */
1589{
1590 funccall_T *fc = current_funccal;
1591
1592 if (fc != NULL && fc->func->uf_profiling)
1593 {
1594 profile_end(&fc->prof_child);
1595 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1596 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1597 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1598 }
1599 script_prof_restore(tm);
1600}
1601#endif
1602
1603
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604#ifdef FEAT_FOLDING
1605/*
1606 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1607 * it in "*cp". Doesn't give error messages.
1608 */
1609 int
1610eval_foldexpr(arg, cp)
1611 char_u *arg;
1612 int *cp;
1613{
Bram Moolenaar33570922005-01-25 22:26:29 +00001614 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 int retval;
1616 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001617 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1618 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
1620 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001621 if (use_sandbox)
1622 ++sandbox;
1623 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001625 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 retval = 0;
1627 else
1628 {
1629 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001630 if (tv.v_type == VAR_NUMBER)
1631 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001632 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 retval = 0;
1634 else
1635 {
1636 /* If the result is a string, check if there is a non-digit before
1637 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001638 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 if (!VIM_ISDIGIT(*s) && *s != '-')
1640 *cp = *s++;
1641 retval = atol((char *)s);
1642 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001643 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 }
1645 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001646 if (use_sandbox)
1647 --sandbox;
1648 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649
1650 return retval;
1651}
1652#endif
1653
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001655 * ":let" list all variable values
1656 * ":let var1 var2" list variable values
1657 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001658 * ":let var += expr" assignment command.
1659 * ":let var -= expr" assignment command.
1660 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001661 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 */
1663 void
1664ex_let(eap)
1665 exarg_T *eap;
1666{
1667 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001668 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001669 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001671 int var_count = 0;
1672 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001673 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001674 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaardb552d602006-03-23 22:59:57 +00001676 argend = skip_var_list(arg, &var_count, &semicolon);
1677 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001678 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001679 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1680 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001681 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001684 /*
1685 * ":let" without "=": list variables
1686 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001687 if (*arg == '[')
1688 EMSG(_(e_invarg));
1689 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001690 /* ":let var1 var2" */
1691 arg = list_arg_vars(eap, arg);
1692 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001694 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001695 list_glob_vars();
1696 list_buf_vars();
1697 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001698#ifdef FEAT_WINDOWS
1699 list_tab_vars();
1700#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001701 list_script_vars();
1702 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001703 list_vim_vars();
1704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 eap->nextcmd = check_nextcmd(arg);
1706 }
1707 else
1708 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001709 op[0] = '=';
1710 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001711 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001712 {
1713 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1714 op[0] = expr[-1]; /* +=, -= or .= */
1715 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001717
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 if (eap->skip)
1719 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001720 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (eap->skip)
1722 {
1723 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001724 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 --emsg_skip;
1726 }
1727 else if (i != FAIL)
1728 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001730 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001731 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 }
1733 }
1734}
1735
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736/*
1737 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1738 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739 * When "nextchars" is not NULL it points to a string with characters that
1740 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1741 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001742 * Returns OK or FAIL;
1743 */
1744 static int
1745ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1746 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001747 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 int copy; /* copy values from "tv", don't move */
1749 int semicolon; /* from skip_var_list() */
1750 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001751 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001752{
1753 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001754 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001756 listitem_T *item;
1757 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001758
1759 if (*arg != '[')
1760 {
1761 /*
1762 * ":let var = expr" or ":for var in list"
1763 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001764 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765 return FAIL;
1766 return OK;
1767 }
1768
1769 /*
1770 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1771 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001772 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001773 {
1774 EMSG(_(e_listreq));
1775 return FAIL;
1776 }
1777
1778 i = list_len(l);
1779 if (semicolon == 0 && var_count < i)
1780 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001781 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782 return FAIL;
1783 }
1784 if (var_count - semicolon > i)
1785 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001786 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787 return FAIL;
1788 }
1789
1790 item = l->lv_first;
1791 while (*arg != ']')
1792 {
1793 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001794 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 item = item->li_next;
1796 if (arg == NULL)
1797 return FAIL;
1798
1799 arg = skipwhite(arg);
1800 if (*arg == ';')
1801 {
1802 /* Put the rest of the list (may be empty) in the var after ';'.
1803 * Create a new list for this. */
1804 l = list_alloc();
1805 if (l == NULL)
1806 return FAIL;
1807 while (item != NULL)
1808 {
1809 list_append_tv(l, &item->li_tv);
1810 item = item->li_next;
1811 }
1812
1813 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001814 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815 ltv.vval.v_list = l;
1816 l->lv_refcount = 1;
1817
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001818 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1819 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820 clear_tv(&ltv);
1821 if (arg == NULL)
1822 return FAIL;
1823 break;
1824 }
1825 else if (*arg != ',' && *arg != ']')
1826 {
1827 EMSG2(_(e_intern2), "ex_let_vars()");
1828 return FAIL;
1829 }
1830 }
1831
1832 return OK;
1833}
1834
1835/*
1836 * Skip over assignable variable "var" or list of variables "[var, var]".
1837 * Used for ":let varvar = expr" and ":for varvar in expr".
1838 * For "[var, var]" increment "*var_count" for each variable.
1839 * for "[var, var; var]" set "semicolon".
1840 * Return NULL for an error.
1841 */
1842 static char_u *
1843skip_var_list(arg, var_count, semicolon)
1844 char_u *arg;
1845 int *var_count;
1846 int *semicolon;
1847{
1848 char_u *p, *s;
1849
1850 if (*arg == '[')
1851 {
1852 /* "[var, var]": find the matching ']'. */
1853 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001854 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 {
1856 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1857 s = skip_var_one(p);
1858 if (s == p)
1859 {
1860 EMSG2(_(e_invarg2), p);
1861 return NULL;
1862 }
1863 ++*var_count;
1864
1865 p = skipwhite(s);
1866 if (*p == ']')
1867 break;
1868 else if (*p == ';')
1869 {
1870 if (*semicolon == 1)
1871 {
1872 EMSG(_("Double ; in list of variables"));
1873 return NULL;
1874 }
1875 *semicolon = 1;
1876 }
1877 else if (*p != ',')
1878 {
1879 EMSG2(_(e_invarg2), p);
1880 return NULL;
1881 }
1882 }
1883 return p + 1;
1884 }
1885 else
1886 return skip_var_one(arg);
1887}
1888
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001889/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001890 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1891 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001892 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 static char_u *
1894skip_var_one(arg)
1895 char_u *arg;
1896{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001897 if (*arg == '@' && arg[1] != NUL)
1898 return arg + 2;
1899 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1900 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901}
1902
Bram Moolenaara7043832005-01-21 11:56:39 +00001903/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001904 * List variables for hashtab "ht" with prefix "prefix".
1905 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001906 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001908list_hashtable_vars(ht, prefix, empty)
1909 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001910 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001911 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001912{
Bram Moolenaar33570922005-01-25 22:26:29 +00001913 hashitem_T *hi;
1914 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001915 int todo;
1916
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001917 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001918 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1919 {
1920 if (!HASHITEM_EMPTY(hi))
1921 {
1922 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 di = HI2DI(hi);
1924 if (empty || di->di_tv.v_type != VAR_STRING
1925 || di->di_tv.vval.v_string != NULL)
1926 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001927 }
1928 }
1929}
1930
1931/*
1932 * List global variables.
1933 */
1934 static void
1935list_glob_vars()
1936{
Bram Moolenaar33570922005-01-25 22:26:29 +00001937 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001938}
1939
1940/*
1941 * List buffer variables.
1942 */
1943 static void
1944list_buf_vars()
1945{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001946 char_u numbuf[NUMBUFLEN];
1947
Bram Moolenaar33570922005-01-25 22:26:29 +00001948 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001949
1950 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1951 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001952}
1953
1954/*
1955 * List window variables.
1956 */
1957 static void
1958list_win_vars()
1959{
Bram Moolenaar33570922005-01-25 22:26:29 +00001960 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001961}
1962
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001963#ifdef FEAT_WINDOWS
1964/*
1965 * List tab page variables.
1966 */
1967 static void
1968list_tab_vars()
1969{
1970 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1971}
1972#endif
1973
Bram Moolenaara7043832005-01-21 11:56:39 +00001974/*
1975 * List Vim variables.
1976 */
1977 static void
1978list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001979{
Bram Moolenaar33570922005-01-25 22:26:29 +00001980 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981}
1982
1983/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001984 * List script-local variables, if there is a script.
1985 */
1986 static void
1987list_script_vars()
1988{
1989 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1990 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
1991}
1992
1993/*
1994 * List function variables, if there is a function.
1995 */
1996 static void
1997list_func_vars()
1998{
1999 if (current_funccal != NULL)
2000 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
2001 (char_u *)"l:", FALSE);
2002}
2003
2004/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002005 * List variables in "arg".
2006 */
2007 static char_u *
2008list_arg_vars(eap, arg)
2009 exarg_T *eap;
2010 char_u *arg;
2011{
2012 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002013 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002015 char_u *name_start;
2016 char_u *arg_subsc;
2017 char_u *tofree;
2018 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002019
2020 while (!ends_excmd(*arg) && !got_int)
2021 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002022 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002023 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002024 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002025 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2026 {
2027 emsg_severe = TRUE;
2028 EMSG(_(e_trailing));
2029 break;
2030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002032 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002033 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002034 /* get_name_len() takes care of expanding curly braces */
2035 name_start = name = arg;
2036 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2037 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002038 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002039 /* This is mainly to keep test 49 working: when expanding
2040 * curly braces fails overrule the exception error message. */
2041 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002043 emsg_severe = TRUE;
2044 EMSG2(_(e_invarg2), arg);
2045 break;
2046 }
2047 error = TRUE;
2048 }
2049 else
2050 {
2051 if (tofree != NULL)
2052 name = tofree;
2053 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002054 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002055 else
2056 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002057 /* handle d.key, l[idx], f(expr) */
2058 arg_subsc = arg;
2059 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002060 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002061 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002062 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002063 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002064 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002065 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002066 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002067 case 'g': list_glob_vars(); break;
2068 case 'b': list_buf_vars(); break;
2069 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002070#ifdef FEAT_WINDOWS
2071 case 't': list_tab_vars(); break;
2072#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002073 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002074 case 's': list_script_vars(); break;
2075 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002076 default:
2077 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002078 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 }
2080 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002081 {
2082 char_u numbuf[NUMBUFLEN];
2083 char_u *tf;
2084 int c;
2085 char_u *s;
2086
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002087 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002088 c = *arg;
2089 *arg = NUL;
2090 list_one_var_a((char_u *)"",
2091 arg == arg_subsc ? name : name_start,
2092 tv.v_type, s == NULL ? (char_u *)"" : s);
2093 *arg = c;
2094 vim_free(tf);
2095 }
2096 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002097 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098 }
2099 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002100
2101 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002102 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002103
2104 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002105 }
2106
2107 return arg;
2108}
2109
2110/*
2111 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2112 * Returns a pointer to the char just after the var name.
2113 * Returns NULL if there is an error.
2114 */
2115 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002116ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002117 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002118 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002119 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002120 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002121 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002122{
2123 int c1;
2124 char_u *name;
2125 char_u *p;
2126 char_u *arg_end = NULL;
2127 int len;
2128 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002129 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002130
2131 /*
2132 * ":let $VAR = expr": Set environment variable.
2133 */
2134 if (*arg == '$')
2135 {
2136 /* Find the end of the name. */
2137 ++arg;
2138 name = arg;
2139 len = get_env_len(&arg);
2140 if (len == 0)
2141 EMSG2(_(e_invarg2), name - 1);
2142 else
2143 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002144 if (op != NULL && (*op == '+' || *op == '-'))
2145 EMSG2(_(e_letwrong), op);
2146 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002147 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002148 EMSG(_(e_letunexp));
2149 else
2150 {
2151 c1 = name[len];
2152 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002153 p = get_tv_string_chk(tv);
2154 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002155 {
2156 int mustfree = FALSE;
2157 char_u *s = vim_getenv(name, &mustfree);
2158
2159 if (s != NULL)
2160 {
2161 p = tofree = concat_str(s, p);
2162 if (mustfree)
2163 vim_free(s);
2164 }
2165 }
2166 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002167 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002168 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002169 if (STRICMP(name, "HOME") == 0)
2170 init_homedir();
2171 else if (didset_vim && STRICMP(name, "VIM") == 0)
2172 didset_vim = FALSE;
2173 else if (didset_vimruntime
2174 && STRICMP(name, "VIMRUNTIME") == 0)
2175 didset_vimruntime = FALSE;
2176 arg_end = arg;
2177 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002179 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002180 }
2181 }
2182 }
2183
2184 /*
2185 * ":let &option = expr": Set option value.
2186 * ":let &l:option = expr": Set local option value.
2187 * ":let &g:option = expr": Set global option value.
2188 */
2189 else if (*arg == '&')
2190 {
2191 /* Find the end of the name. */
2192 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002193 if (p == NULL || (endchars != NULL
2194 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 EMSG(_(e_letunexp));
2196 else
2197 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002198 long n;
2199 int opt_type;
2200 long numval;
2201 char_u *stringval = NULL;
2202 char_u *s;
2203
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 c1 = *p;
2205 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002206
2207 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002208 s = get_tv_string_chk(tv); /* != NULL if number or string */
2209 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002210 {
2211 opt_type = get_option_value(arg, &numval,
2212 &stringval, opt_flags);
2213 if ((opt_type == 1 && *op == '.')
2214 || (opt_type == 0 && *op != '.'))
2215 EMSG2(_(e_letwrong), op);
2216 else
2217 {
2218 if (opt_type == 1) /* number */
2219 {
2220 if (*op == '+')
2221 n = numval + n;
2222 else
2223 n = numval - n;
2224 }
2225 else if (opt_type == 0 && stringval != NULL) /* string */
2226 {
2227 s = concat_str(stringval, s);
2228 vim_free(stringval);
2229 stringval = s;
2230 }
2231 }
2232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002233 if (s != NULL)
2234 {
2235 set_option_value(arg, n, s, opt_flags);
2236 arg_end = p;
2237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002239 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 }
2241 }
2242
2243 /*
2244 * ":let @r = expr": Set register contents.
2245 */
2246 else if (*arg == '@')
2247 {
2248 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002249 if (op != NULL && (*op == '+' || *op == '-'))
2250 EMSG2(_(e_letwrong), op);
2251 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002252 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 EMSG(_(e_letunexp));
2254 else
2255 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002256 char_u *tofree = NULL;
2257 char_u *s;
2258
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002259 p = get_tv_string_chk(tv);
2260 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002261 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002262 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002263 if (s != NULL)
2264 {
2265 p = tofree = concat_str(s, p);
2266 vim_free(s);
2267 }
2268 }
2269 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002270 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002271 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002272 arg_end = arg + 1;
2273 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002274 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 }
2276 }
2277
2278 /*
2279 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002280 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002282 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002284 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002286 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002287 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2290 EMSG(_(e_letunexp));
2291 else
2292 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002293 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002294 arg_end = p;
2295 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002297 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 }
2299
2300 else
2301 EMSG2(_(e_invarg2), arg);
2302
2303 return arg_end;
2304}
2305
2306/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002307 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2308 */
2309 static int
2310check_changedtick(arg)
2311 char_u *arg;
2312{
2313 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2314 {
2315 EMSG2(_(e_readonlyvar), arg);
2316 return TRUE;
2317 }
2318 return FALSE;
2319}
2320
2321/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 * Get an lval: variable, Dict item or List item that can be assigned a value
2323 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2324 * "name.key", "name.key[expr]" etc.
2325 * Indexing only works if "name" is an existing List or Dictionary.
2326 * "name" points to the start of the name.
2327 * If "rettv" is not NULL it points to the value to be assigned.
2328 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2329 * wrong; must end in space or cmd separator.
2330 *
2331 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002332 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002333 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 */
2335 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002336get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002338 typval_T *rettv;
2339 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 int unlet;
2341 int skip;
2342 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002343 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002346 char_u *expr_start, *expr_end;
2347 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002348 dictitem_T *v;
2349 typval_T var1;
2350 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002351 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002352 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002353 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002354 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002355 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002356
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002357 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002358 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359
2360 if (skip)
2361 {
2362 /* When skipping just find the end of the name. */
2363 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002364 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 }
2366
2367 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002368 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 if (expr_start != NULL)
2370 {
2371 /* Don't expand the name when we already know there is an error. */
2372 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2373 && *p != '[' && *p != '.')
2374 {
2375 EMSG(_(e_trailing));
2376 return NULL;
2377 }
2378
2379 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2380 if (lp->ll_exp_name == NULL)
2381 {
2382 /* Report an invalid expression in braces, unless the
2383 * expression evaluation has been cancelled due to an
2384 * aborting error, an interrupt, or an exception. */
2385 if (!aborting() && !quiet)
2386 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002387 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002388 EMSG2(_(e_invarg2), name);
2389 return NULL;
2390 }
2391 }
2392 lp->ll_name = lp->ll_exp_name;
2393 }
2394 else
2395 lp->ll_name = name;
2396
2397 /* Without [idx] or .key we are done. */
2398 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2399 return p;
2400
2401 cc = *p;
2402 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002403 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002404 if (v == NULL && !quiet)
2405 EMSG2(_(e_undefvar), lp->ll_name);
2406 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002407 if (v == NULL)
2408 return NULL;
2409
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002410 /*
2411 * Loop until no more [idx] or .key is following.
2412 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002413 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002414 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002415 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002416 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2417 && !(lp->ll_tv->v_type == VAR_DICT
2418 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002420 if (!quiet)
2421 EMSG(_("E689: Can only index a List or Dictionary"));
2422 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002426 if (!quiet)
2427 EMSG(_("E708: [:] must come last"));
2428 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002430
Bram Moolenaar8c711452005-01-14 21:53:12 +00002431 len = -1;
2432 if (*p == '.')
2433 {
2434 key = p + 1;
2435 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2436 ;
2437 if (len == 0)
2438 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 if (!quiet)
2440 EMSG(_(e_emptykey));
2441 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002442 }
2443 p = key + len;
2444 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002445 else
2446 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002447 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002448 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 if (*p == ':')
2450 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002451 else
2452 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002453 empty1 = FALSE;
2454 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 if (get_tv_string_chk(&var1) == NULL)
2457 {
2458 /* not a number or string */
2459 clear_tv(&var1);
2460 return NULL;
2461 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002462 }
2463
2464 /* Optionally get the second index [ :expr]. */
2465 if (*p == ':')
2466 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002468 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002471 if (!empty1)
2472 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002474 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 if (rettv != NULL && (rettv->v_type != VAR_LIST
2476 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 if (!quiet)
2479 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002480 if (!empty1)
2481 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002483 }
2484 p = skipwhite(p + 1);
2485 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 else
2488 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2491 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 if (!empty1)
2493 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002496 if (get_tv_string_chk(&var2) == NULL)
2497 {
2498 /* not a number or string */
2499 if (!empty1)
2500 clear_tv(&var1);
2501 clear_tv(&var2);
2502 return NULL;
2503 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002506 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002507 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002509
Bram Moolenaar8c711452005-01-14 21:53:12 +00002510 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002511 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 if (!quiet)
2513 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 if (!empty1)
2515 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002517 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 }
2520
2521 /* Skip to past ']'. */
2522 ++p;
2523 }
2524
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 {
2527 if (len == -1)
2528 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002530 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002531 if (*key == NUL)
2532 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 if (!quiet)
2534 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 }
2538 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002539 lp->ll_list = NULL;
2540 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002541 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002543 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002544 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002546 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002548 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002549 if (len == -1)
2550 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 }
2553 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002555 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002557 if (len == -1)
2558 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002560 p = NULL;
2561 break;
2562 }
2563 if (len == -1)
2564 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 }
2567 else
2568 {
2569 /*
2570 * Get the number and item for the only or first index of the List.
2571 */
2572 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002574 else
2575 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002576 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002577 clear_tv(&var1);
2578 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002579 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 lp->ll_list = lp->ll_tv->vval.v_list;
2581 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2582 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002584 if (lp->ll_n1 < 0)
2585 {
2586 lp->ll_n1 = 0;
2587 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2588 }
2589 }
2590 if (lp->ll_li == NULL)
2591 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595 }
2596
2597 /*
2598 * May need to find the item or absolute index for the second
2599 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 * When no index given: "lp->ll_empty2" is TRUE.
2601 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002605 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002606 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002610 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 }
2614
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2616 if (lp->ll_n1 < 0)
2617 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2618 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002620 }
2621
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002623 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002624 }
2625
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 return p;
2627}
2628
2629/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002630 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 */
2632 static void
2633clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002634 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635{
2636 vim_free(lp->ll_exp_name);
2637 vim_free(lp->ll_newkey);
2638}
2639
2640/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002641 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002643 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 */
2645 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002646set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002647 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002649 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002651 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652{
2653 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002654 listitem_T *ri;
2655 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656
2657 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 cc = *endp;
2662 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002663 if (op != NULL && *op != '=')
2664 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002665 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002666
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002667 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002668 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002669 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002670 {
2671 if (tv_op(&tv, rettv, op) == OK)
2672 set_var(lp->ll_name, &tv, FALSE);
2673 clear_tv(&tv);
2674 }
2675 }
2676 else
2677 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002679 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002681 else if (tv_check_lock(lp->ll_newkey == NULL
2682 ? lp->ll_tv->v_lock
2683 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2684 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 else if (lp->ll_range)
2686 {
2687 /*
2688 * Assign the List values to the list items.
2689 */
2690 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002692 if (op != NULL && *op != '=')
2693 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2694 else
2695 {
2696 clear_tv(&lp->ll_li->li_tv);
2697 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2698 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 ri = ri->li_next;
2700 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2701 break;
2702 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002705 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 ri = NULL;
2708 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 lp->ll_li = lp->ll_li->li_next;
2712 ++lp->ll_n1;
2713 }
2714 if (ri != NULL)
2715 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002716 else if (lp->ll_empty2
2717 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 : lp->ll_n1 != lp->ll_n2)
2719 EMSG(_("E711: List value has not enough items"));
2720 }
2721 else
2722 {
2723 /*
2724 * Assign to a List or Dictionary item.
2725 */
2726 if (lp->ll_newkey != NULL)
2727 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002728 if (op != NULL && *op != '=')
2729 {
2730 EMSG2(_(e_letwrong), op);
2731 return;
2732 }
2733
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002735 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 if (di == NULL)
2737 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002738 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2739 {
2740 vim_free(di);
2741 return;
2742 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002744 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002745 else if (op != NULL && *op != '=')
2746 {
2747 tv_op(lp->ll_tv, rettv, op);
2748 return;
2749 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002750 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 /*
2754 * Assign the value to the variable or list item.
2755 */
2756 if (copy)
2757 copy_tv(rettv, lp->ll_tv);
2758 else
2759 {
2760 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002761 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002763 }
2764 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002765}
2766
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002767/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002768 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2769 * Returns OK or FAIL.
2770 */
2771 static int
2772tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002773 typval_T *tv1;
2774 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002775 char_u *op;
2776{
2777 long n;
2778 char_u numbuf[NUMBUFLEN];
2779 char_u *s;
2780
2781 /* Can't do anything with a Funcref or a Dict on the right. */
2782 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2783 {
2784 switch (tv1->v_type)
2785 {
2786 case VAR_DICT:
2787 case VAR_FUNC:
2788 break;
2789
2790 case VAR_LIST:
2791 if (*op != '+' || tv2->v_type != VAR_LIST)
2792 break;
2793 /* List += List */
2794 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2795 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2796 return OK;
2797
2798 case VAR_NUMBER:
2799 case VAR_STRING:
2800 if (tv2->v_type == VAR_LIST)
2801 break;
2802 if (*op == '+' || *op == '-')
2803 {
2804 /* nr += nr or nr -= nr*/
2805 n = get_tv_number(tv1);
2806 if (*op == '+')
2807 n += get_tv_number(tv2);
2808 else
2809 n -= get_tv_number(tv2);
2810 clear_tv(tv1);
2811 tv1->v_type = VAR_NUMBER;
2812 tv1->vval.v_number = n;
2813 }
2814 else
2815 {
2816 /* str .= str */
2817 s = get_tv_string(tv1);
2818 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2819 clear_tv(tv1);
2820 tv1->v_type = VAR_STRING;
2821 tv1->vval.v_string = s;
2822 }
2823 return OK;
2824 }
2825 }
2826
2827 EMSG2(_(e_letwrong), op);
2828 return FAIL;
2829}
2830
2831/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002832 * Add a watcher to a list.
2833 */
2834 static void
2835list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002836 list_T *l;
2837 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002838{
2839 lw->lw_next = l->lv_watch;
2840 l->lv_watch = lw;
2841}
2842
2843/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002844 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002845 * No warning when it isn't found...
2846 */
2847 static void
2848list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002849 list_T *l;
2850 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002851{
Bram Moolenaar33570922005-01-25 22:26:29 +00002852 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002853
2854 lwp = &l->lv_watch;
2855 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2856 {
2857 if (lw == lwrem)
2858 {
2859 *lwp = lw->lw_next;
2860 break;
2861 }
2862 lwp = &lw->lw_next;
2863 }
2864}
2865
2866/*
2867 * Just before removing an item from a list: advance watchers to the next
2868 * item.
2869 */
2870 static void
2871list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002872 list_T *l;
2873 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002874{
Bram Moolenaar33570922005-01-25 22:26:29 +00002875 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002876
2877 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2878 if (lw->lw_item == item)
2879 lw->lw_item = item->li_next;
2880}
2881
2882/*
2883 * Evaluate the expression used in a ":for var in expr" command.
2884 * "arg" points to "var".
2885 * Set "*errp" to TRUE for an error, FALSE otherwise;
2886 * Return a pointer that holds the info. Null when there is an error.
2887 */
2888 void *
2889eval_for_line(arg, errp, nextcmdp, skip)
2890 char_u *arg;
2891 int *errp;
2892 char_u **nextcmdp;
2893 int skip;
2894{
Bram Moolenaar33570922005-01-25 22:26:29 +00002895 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002896 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002897 typval_T tv;
2898 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002899
2900 *errp = TRUE; /* default: there is an error */
2901
Bram Moolenaar33570922005-01-25 22:26:29 +00002902 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002903 if (fi == NULL)
2904 return NULL;
2905
2906 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2907 if (expr == NULL)
2908 return fi;
2909
2910 expr = skipwhite(expr);
2911 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2912 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002913 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002914 return fi;
2915 }
2916
2917 if (skip)
2918 ++emsg_skip;
2919 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2920 {
2921 *errp = FALSE;
2922 if (!skip)
2923 {
2924 l = tv.vval.v_list;
2925 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002926 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002927 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002928 clear_tv(&tv);
2929 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002930 else
2931 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002932 /* No need to increment the refcount, it's already set for the
2933 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002934 fi->fi_list = l;
2935 list_add_watch(l, &fi->fi_lw);
2936 fi->fi_lw.lw_item = l->lv_first;
2937 }
2938 }
2939 }
2940 if (skip)
2941 --emsg_skip;
2942
2943 return fi;
2944}
2945
2946/*
2947 * Use the first item in a ":for" list. Advance to the next.
2948 * Assign the values to the variable (list). "arg" points to the first one.
2949 * Return TRUE when a valid item was found, FALSE when at end of list or
2950 * something wrong.
2951 */
2952 int
2953next_for_item(fi_void, arg)
2954 void *fi_void;
2955 char_u *arg;
2956{
Bram Moolenaar33570922005-01-25 22:26:29 +00002957 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002958 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002959 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002960
2961 item = fi->fi_lw.lw_item;
2962 if (item == NULL)
2963 result = FALSE;
2964 else
2965 {
2966 fi->fi_lw.lw_item = item->li_next;
2967 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2968 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2969 }
2970 return result;
2971}
2972
2973/*
2974 * Free the structure used to store info used by ":for".
2975 */
2976 void
2977free_for_info(fi_void)
2978 void *fi_void;
2979{
Bram Moolenaar33570922005-01-25 22:26:29 +00002980 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002981
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002982 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002983 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002984 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002985 list_unref(fi->fi_list);
2986 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002987 vim_free(fi);
2988}
2989
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2991
2992 void
2993set_context_for_expression(xp, arg, cmdidx)
2994 expand_T *xp;
2995 char_u *arg;
2996 cmdidx_T cmdidx;
2997{
2998 int got_eq = FALSE;
2999 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003000 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003002 if (cmdidx == CMD_let)
3003 {
3004 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003005 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003006 {
3007 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003008 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003009 {
3010 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003011 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003012 if (vim_iswhite(*p))
3013 break;
3014 }
3015 return;
3016 }
3017 }
3018 else
3019 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3020 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 while ((xp->xp_pattern = vim_strpbrk(arg,
3022 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3023 {
3024 c = *xp->xp_pattern;
3025 if (c == '&')
3026 {
3027 c = xp->xp_pattern[1];
3028 if (c == '&')
3029 {
3030 ++xp->xp_pattern;
3031 xp->xp_context = cmdidx != CMD_let || got_eq
3032 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3033 }
3034 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003037 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3038 xp->xp_pattern += 2;
3039
3040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 }
3042 else if (c == '$')
3043 {
3044 /* environment variable */
3045 xp->xp_context = EXPAND_ENV_VARS;
3046 }
3047 else if (c == '=')
3048 {
3049 got_eq = TRUE;
3050 xp->xp_context = EXPAND_EXPRESSION;
3051 }
3052 else if (c == '<'
3053 && xp->xp_context == EXPAND_FUNCTIONS
3054 && vim_strchr(xp->xp_pattern, '(') == NULL)
3055 {
3056 /* Function name can start with "<SNR>" */
3057 break;
3058 }
3059 else if (cmdidx != CMD_let || got_eq)
3060 {
3061 if (c == '"') /* string */
3062 {
3063 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3064 if (c == '\\' && xp->xp_pattern[1] != NUL)
3065 ++xp->xp_pattern;
3066 xp->xp_context = EXPAND_NOTHING;
3067 }
3068 else if (c == '\'') /* literal string */
3069 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003070 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3072 /* skip */ ;
3073 xp->xp_context = EXPAND_NOTHING;
3074 }
3075 else if (c == '|')
3076 {
3077 if (xp->xp_pattern[1] == '|')
3078 {
3079 ++xp->xp_pattern;
3080 xp->xp_context = EXPAND_EXPRESSION;
3081 }
3082 else
3083 xp->xp_context = EXPAND_COMMANDS;
3084 }
3085 else
3086 xp->xp_context = EXPAND_EXPRESSION;
3087 }
3088 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003089 /* Doesn't look like something valid, expand as an expression
3090 * anyway. */
3091 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 arg = xp->xp_pattern;
3093 if (*arg != NUL)
3094 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3095 /* skip */ ;
3096 }
3097 xp->xp_pattern = arg;
3098}
3099
3100#endif /* FEAT_CMDL_COMPL */
3101
3102/*
3103 * ":1,25call func(arg1, arg2)" function call.
3104 */
3105 void
3106ex_call(eap)
3107 exarg_T *eap;
3108{
3109 char_u *arg = eap->arg;
3110 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003112 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003114 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 linenr_T lnum;
3116 int doesrange;
3117 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003118 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003120 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3121 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003122 if (tofree == NULL)
3123 return;
3124
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003125 /* Increase refcount on dictionary, it could get deleted when evaluating
3126 * the arguments. */
3127 if (fudi.fd_dict != NULL)
3128 ++fudi.fd_dict->dv_refcount;
3129
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003130 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003131 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003132 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133
Bram Moolenaar532c7802005-01-27 14:44:31 +00003134 /* Skip white space to allow ":call func ()". Not good, but required for
3135 * backward compatibility. */
3136 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003137 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
3139 if (*startarg != '(')
3140 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003141 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 goto end;
3143 }
3144
3145 /*
3146 * When skipping, evaluate the function once, to find the end of the
3147 * arguments.
3148 * When the function takes a range, this is discovered after the first
3149 * call, and the loop is broken.
3150 */
3151 if (eap->skip)
3152 {
3153 ++emsg_skip;
3154 lnum = eap->line2; /* do it once, also with an invalid range */
3155 }
3156 else
3157 lnum = eap->line1;
3158 for ( ; lnum <= eap->line2; ++lnum)
3159 {
3160 if (!eap->skip && eap->addr_count > 0)
3161 {
3162 curwin->w_cursor.lnum = lnum;
3163 curwin->w_cursor.col = 0;
3164 }
3165 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003166 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003167 eap->line1, eap->line2, &doesrange,
3168 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 {
3170 failed = TRUE;
3171 break;
3172 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003173 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 if (doesrange || eap->skip)
3175 break;
3176 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003177 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003178 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003179 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 if (aborting())
3181 break;
3182 }
3183 if (eap->skip)
3184 --emsg_skip;
3185
3186 if (!failed)
3187 {
3188 /* Check for trailing illegal characters and a following command. */
3189 if (!ends_excmd(*arg))
3190 {
3191 emsg_severe = TRUE;
3192 EMSG(_(e_trailing));
3193 }
3194 else
3195 eap->nextcmd = check_nextcmd(arg);
3196 }
3197
3198end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003199 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003200 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201}
3202
3203/*
3204 * ":unlet[!] var1 ... " command.
3205 */
3206 void
3207ex_unlet(eap)
3208 exarg_T *eap;
3209{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003210 ex_unletlock(eap, eap->arg, 0);
3211}
3212
3213/*
3214 * ":lockvar" and ":unlockvar" commands
3215 */
3216 void
3217ex_lockvar(eap)
3218 exarg_T *eap;
3219{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003221 int deep = 2;
3222
3223 if (eap->forceit)
3224 deep = -1;
3225 else if (vim_isdigit(*arg))
3226 {
3227 deep = getdigits(&arg);
3228 arg = skipwhite(arg);
3229 }
3230
3231 ex_unletlock(eap, arg, deep);
3232}
3233
3234/*
3235 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3236 */
3237 static void
3238ex_unletlock(eap, argstart, deep)
3239 exarg_T *eap;
3240 char_u *argstart;
3241 int deep;
3242{
3243 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003246 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247
3248 do
3249 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003250 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003251 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3252 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003253 if (lv.ll_name == NULL)
3254 error = TRUE; /* error but continue parsing */
3255 if (name_end == NULL || (!vim_iswhite(*name_end)
3256 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003258 if (name_end != NULL)
3259 {
3260 emsg_severe = TRUE;
3261 EMSG(_(e_trailing));
3262 }
3263 if (!(eap->skip || error))
3264 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 break;
3266 }
3267
3268 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003269 {
3270 if (eap->cmdidx == CMD_unlet)
3271 {
3272 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3273 error = TRUE;
3274 }
3275 else
3276 {
3277 if (do_lock_var(&lv, name_end, deep,
3278 eap->cmdidx == CMD_lockvar) == FAIL)
3279 error = TRUE;
3280 }
3281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003283 if (!eap->skip)
3284 clear_lval(&lv);
3285
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 arg = skipwhite(name_end);
3287 } while (!ends_excmd(*arg));
3288
3289 eap->nextcmd = check_nextcmd(arg);
3290}
3291
Bram Moolenaar8c711452005-01-14 21:53:12 +00003292 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003293do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003294 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003295 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003296 int forceit;
3297{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003298 int ret = OK;
3299 int cc;
3300
3301 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003302 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003303 cc = *name_end;
3304 *name_end = NUL;
3305
3306 /* Normal name or expanded name. */
3307 if (check_changedtick(lp->ll_name))
3308 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003309 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003310 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003311 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003312 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003313 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3314 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003315 else if (lp->ll_range)
3316 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003317 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003318
3319 /* Delete a range of List items. */
3320 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3321 {
3322 li = lp->ll_li->li_next;
3323 listitem_remove(lp->ll_list, lp->ll_li);
3324 lp->ll_li = li;
3325 ++lp->ll_n1;
3326 }
3327 }
3328 else
3329 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003330 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003331 /* unlet a List item. */
3332 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003333 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003334 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003335 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003336 }
3337
3338 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003339}
3340
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341/*
3342 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003343 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 */
3345 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003346do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003348 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
Bram Moolenaar33570922005-01-25 22:26:29 +00003350 hashtab_T *ht;
3351 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003352 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
Bram Moolenaar33570922005-01-25 22:26:29 +00003354 ht = find_var_ht(name, &varname);
3355 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003357 hi = hash_find(ht, varname);
3358 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003359 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003360 if (var_check_ro(HI2DI(hi)->di_flags, name))
3361 return FAIL;
3362 delete_var(ht, hi);
3363 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003366 if (forceit)
3367 return OK;
3368 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 return FAIL;
3370}
3371
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003372/*
3373 * Lock or unlock variable indicated by "lp".
3374 * "deep" is the levels to go (-1 for unlimited);
3375 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3376 */
3377 static int
3378do_lock_var(lp, name_end, deep, lock)
3379 lval_T *lp;
3380 char_u *name_end;
3381 int deep;
3382 int lock;
3383{
3384 int ret = OK;
3385 int cc;
3386 dictitem_T *di;
3387
3388 if (deep == 0) /* nothing to do */
3389 return OK;
3390
3391 if (lp->ll_tv == NULL)
3392 {
3393 cc = *name_end;
3394 *name_end = NUL;
3395
3396 /* Normal name or expanded name. */
3397 if (check_changedtick(lp->ll_name))
3398 ret = FAIL;
3399 else
3400 {
3401 di = find_var(lp->ll_name, NULL);
3402 if (di == NULL)
3403 ret = FAIL;
3404 else
3405 {
3406 if (lock)
3407 di->di_flags |= DI_FLAGS_LOCK;
3408 else
3409 di->di_flags &= ~DI_FLAGS_LOCK;
3410 item_lock(&di->di_tv, deep, lock);
3411 }
3412 }
3413 *name_end = cc;
3414 }
3415 else if (lp->ll_range)
3416 {
3417 listitem_T *li = lp->ll_li;
3418
3419 /* (un)lock a range of List items. */
3420 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3421 {
3422 item_lock(&li->li_tv, deep, lock);
3423 li = li->li_next;
3424 ++lp->ll_n1;
3425 }
3426 }
3427 else if (lp->ll_list != NULL)
3428 /* (un)lock a List item. */
3429 item_lock(&lp->ll_li->li_tv, deep, lock);
3430 else
3431 /* un(lock) a Dictionary item. */
3432 item_lock(&lp->ll_di->di_tv, deep, lock);
3433
3434 return ret;
3435}
3436
3437/*
3438 * Lock or unlock an item. "deep" is nr of levels to go.
3439 */
3440 static void
3441item_lock(tv, deep, lock)
3442 typval_T *tv;
3443 int deep;
3444 int lock;
3445{
3446 static int recurse = 0;
3447 list_T *l;
3448 listitem_T *li;
3449 dict_T *d;
3450 hashitem_T *hi;
3451 int todo;
3452
3453 if (recurse >= DICT_MAXNEST)
3454 {
3455 EMSG(_("E743: variable nested too deep for (un)lock"));
3456 return;
3457 }
3458 if (deep == 0)
3459 return;
3460 ++recurse;
3461
3462 /* lock/unlock the item itself */
3463 if (lock)
3464 tv->v_lock |= VAR_LOCKED;
3465 else
3466 tv->v_lock &= ~VAR_LOCKED;
3467
3468 switch (tv->v_type)
3469 {
3470 case VAR_LIST:
3471 if ((l = tv->vval.v_list) != NULL)
3472 {
3473 if (lock)
3474 l->lv_lock |= VAR_LOCKED;
3475 else
3476 l->lv_lock &= ~VAR_LOCKED;
3477 if (deep < 0 || deep > 1)
3478 /* recursive: lock/unlock the items the List contains */
3479 for (li = l->lv_first; li != NULL; li = li->li_next)
3480 item_lock(&li->li_tv, deep - 1, lock);
3481 }
3482 break;
3483 case VAR_DICT:
3484 if ((d = tv->vval.v_dict) != NULL)
3485 {
3486 if (lock)
3487 d->dv_lock |= VAR_LOCKED;
3488 else
3489 d->dv_lock &= ~VAR_LOCKED;
3490 if (deep < 0 || deep > 1)
3491 {
3492 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003493 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003494 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3495 {
3496 if (!HASHITEM_EMPTY(hi))
3497 {
3498 --todo;
3499 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3500 }
3501 }
3502 }
3503 }
3504 }
3505 --recurse;
3506}
3507
Bram Moolenaara40058a2005-07-11 22:42:07 +00003508/*
3509 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3510 * it refers to a List or Dictionary that is locked.
3511 */
3512 static int
3513tv_islocked(tv)
3514 typval_T *tv;
3515{
3516 return (tv->v_lock & VAR_LOCKED)
3517 || (tv->v_type == VAR_LIST
3518 && tv->vval.v_list != NULL
3519 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3520 || (tv->v_type == VAR_DICT
3521 && tv->vval.v_dict != NULL
3522 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3523}
3524
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3526/*
3527 * Delete all "menutrans_" variables.
3528 */
3529 void
3530del_menutrans_vars()
3531{
Bram Moolenaar33570922005-01-25 22:26:29 +00003532 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003533 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
Bram Moolenaar33570922005-01-25 22:26:29 +00003535 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003536 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003537 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003538 {
3539 if (!HASHITEM_EMPTY(hi))
3540 {
3541 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003542 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3543 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003544 }
3545 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003546 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547}
3548#endif
3549
3550#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3551
3552/*
3553 * Local string buffer for the next two functions to store a variable name
3554 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3555 * get_user_var_name().
3556 */
3557
3558static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3559
3560static char_u *varnamebuf = NULL;
3561static int varnamebuflen = 0;
3562
3563/*
3564 * Function to concatenate a prefix and a variable name.
3565 */
3566 static char_u *
3567cat_prefix_varname(prefix, name)
3568 int prefix;
3569 char_u *name;
3570{
3571 int len;
3572
3573 len = (int)STRLEN(name) + 3;
3574 if (len > varnamebuflen)
3575 {
3576 vim_free(varnamebuf);
3577 len += 10; /* some additional space */
3578 varnamebuf = alloc(len);
3579 if (varnamebuf == NULL)
3580 {
3581 varnamebuflen = 0;
3582 return NULL;
3583 }
3584 varnamebuflen = len;
3585 }
3586 *varnamebuf = prefix;
3587 varnamebuf[1] = ':';
3588 STRCPY(varnamebuf + 2, name);
3589 return varnamebuf;
3590}
3591
3592/*
3593 * Function given to ExpandGeneric() to obtain the list of user defined
3594 * (global/buffer/window/built-in) variable names.
3595 */
3596/*ARGSUSED*/
3597 char_u *
3598get_user_var_name(xp, idx)
3599 expand_T *xp;
3600 int idx;
3601{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003602 static long_u gdone;
3603 static long_u bdone;
3604 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003605#ifdef FEAT_WINDOWS
3606 static long_u tdone;
3607#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003608 static int vidx;
3609 static hashitem_T *hi;
3610 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611
3612 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003613 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003614 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003615#ifdef FEAT_WINDOWS
3616 tdone = 0;
3617#endif
3618 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003619
3620 /* Global variables */
3621 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003623 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003624 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003625 else
3626 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003627 while (HASHITEM_EMPTY(hi))
3628 ++hi;
3629 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3630 return cat_prefix_varname('g', hi->hi_key);
3631 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003633
3634 /* b: variables */
3635 ht = &curbuf->b_vars.dv_hashtab;
3636 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003638 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003639 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003640 else
3641 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003642 while (HASHITEM_EMPTY(hi))
3643 ++hi;
3644 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003646 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003648 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 return (char_u *)"b:changedtick";
3650 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003651
3652 /* w: variables */
3653 ht = &curwin->w_vars.dv_hashtab;
3654 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003656 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003657 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003658 else
3659 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003660 while (HASHITEM_EMPTY(hi))
3661 ++hi;
3662 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003664
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003665#ifdef FEAT_WINDOWS
3666 /* t: variables */
3667 ht = &curtab->tp_vars.dv_hashtab;
3668 if (tdone < ht->ht_used)
3669 {
3670 if (tdone++ == 0)
3671 hi = ht->ht_array;
3672 else
3673 ++hi;
3674 while (HASHITEM_EMPTY(hi))
3675 ++hi;
3676 return cat_prefix_varname('t', hi->hi_key);
3677 }
3678#endif
3679
Bram Moolenaar33570922005-01-25 22:26:29 +00003680 /* v: variables */
3681 if (vidx < VV_LEN)
3682 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
3684 vim_free(varnamebuf);
3685 varnamebuf = NULL;
3686 varnamebuflen = 0;
3687 return NULL;
3688}
3689
3690#endif /* FEAT_CMDL_COMPL */
3691
3692/*
3693 * types for expressions.
3694 */
3695typedef enum
3696{
3697 TYPE_UNKNOWN = 0
3698 , TYPE_EQUAL /* == */
3699 , TYPE_NEQUAL /* != */
3700 , TYPE_GREATER /* > */
3701 , TYPE_GEQUAL /* >= */
3702 , TYPE_SMALLER /* < */
3703 , TYPE_SEQUAL /* <= */
3704 , TYPE_MATCH /* =~ */
3705 , TYPE_NOMATCH /* !~ */
3706} exptype_T;
3707
3708/*
3709 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003710 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3712 */
3713
3714/*
3715 * Handle zero level expression.
3716 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003717 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003718 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 * Return OK or FAIL.
3720 */
3721 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003722eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 char_u **nextcmd;
3726 int evaluate;
3727{
3728 int ret;
3729 char_u *p;
3730
3731 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003732 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 if (ret == FAIL || !ends_excmd(*p))
3734 {
3735 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003736 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 /*
3738 * Report the invalid expression unless the expression evaluation has
3739 * been cancelled due to an aborting error, an interrupt, or an
3740 * exception.
3741 */
3742 if (!aborting())
3743 EMSG2(_(e_invexpr2), arg);
3744 ret = FAIL;
3745 }
3746 if (nextcmd != NULL)
3747 *nextcmd = check_nextcmd(p);
3748
3749 return ret;
3750}
3751
3752/*
3753 * Handle top level expression:
3754 * expr1 ? expr0 : expr0
3755 *
3756 * "arg" must point to the first non-white of the expression.
3757 * "arg" is advanced to the next non-white after the recognized expression.
3758 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003759 * Note: "rettv.v_lock" is not set.
3760 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 * Return OK or FAIL.
3762 */
3763 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003764eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 int evaluate;
3768{
3769 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003770 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
3772 /*
3773 * Get the first variable.
3774 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003775 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 return FAIL;
3777
3778 if ((*arg)[0] == '?')
3779 {
3780 result = FALSE;
3781 if (evaluate)
3782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003783 int error = FALSE;
3784
3785 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003787 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003788 if (error)
3789 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
3791
3792 /*
3793 * Get the second variable.
3794 */
3795 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003796 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 return FAIL;
3798
3799 /*
3800 * Check for the ":".
3801 */
3802 if ((*arg)[0] != ':')
3803 {
3804 EMSG(_("E109: Missing ':' after '?'"));
3805 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003806 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 return FAIL;
3808 }
3809
3810 /*
3811 * Get the third variable.
3812 */
3813 *arg = skipwhite(*arg + 1);
3814 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3815 {
3816 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003817 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 return FAIL;
3819 }
3820 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003821 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
3823
3824 return OK;
3825}
3826
3827/*
3828 * Handle first level expression:
3829 * expr2 || expr2 || expr2 logical OR
3830 *
3831 * "arg" must point to the first non-white of the expression.
3832 * "arg" is advanced to the next non-white after the recognized expression.
3833 *
3834 * Return OK or FAIL.
3835 */
3836 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003837eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 int evaluate;
3841{
Bram Moolenaar33570922005-01-25 22:26:29 +00003842 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 long result;
3844 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003845 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846
3847 /*
3848 * Get the first variable.
3849 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003850 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 return FAIL;
3852
3853 /*
3854 * Repeat until there is no following "||".
3855 */
3856 first = TRUE;
3857 result = FALSE;
3858 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3859 {
3860 if (evaluate && first)
3861 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003862 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003864 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003865 if (error)
3866 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 first = FALSE;
3868 }
3869
3870 /*
3871 * Get the second variable.
3872 */
3873 *arg = skipwhite(*arg + 2);
3874 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3875 return FAIL;
3876
3877 /*
3878 * Compute the result.
3879 */
3880 if (evaluate && !result)
3881 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003882 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003884 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003885 if (error)
3886 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 }
3888 if (evaluate)
3889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003890 rettv->v_type = VAR_NUMBER;
3891 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
3893 }
3894
3895 return OK;
3896}
3897
3898/*
3899 * Handle second level expression:
3900 * expr3 && expr3 && expr3 logical AND
3901 *
3902 * "arg" must point to the first non-white of the expression.
3903 * "arg" is advanced to the next non-white after the recognized expression.
3904 *
3905 * Return OK or FAIL.
3906 */
3907 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003908eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 int evaluate;
3912{
Bram Moolenaar33570922005-01-25 22:26:29 +00003913 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 long result;
3915 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003916 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917
3918 /*
3919 * Get the first variable.
3920 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003921 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 return FAIL;
3923
3924 /*
3925 * Repeat until there is no following "&&".
3926 */
3927 first = TRUE;
3928 result = TRUE;
3929 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3930 {
3931 if (evaluate && first)
3932 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003933 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003935 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003936 if (error)
3937 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 first = FALSE;
3939 }
3940
3941 /*
3942 * Get the second variable.
3943 */
3944 *arg = skipwhite(*arg + 2);
3945 if (eval4(arg, &var2, evaluate && result) == FAIL)
3946 return FAIL;
3947
3948 /*
3949 * Compute the result.
3950 */
3951 if (evaluate && result)
3952 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003953 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003955 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003956 if (error)
3957 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
3959 if (evaluate)
3960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003961 rettv->v_type = VAR_NUMBER;
3962 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
3964 }
3965
3966 return OK;
3967}
3968
3969/*
3970 * Handle third level expression:
3971 * var1 == var2
3972 * var1 =~ var2
3973 * var1 != var2
3974 * var1 !~ var2
3975 * var1 > var2
3976 * var1 >= var2
3977 * var1 < var2
3978 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003979 * var1 is var2
3980 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 *
3982 * "arg" must point to the first non-white of the expression.
3983 * "arg" is advanced to the next non-white after the recognized expression.
3984 *
3985 * Return OK or FAIL.
3986 */
3987 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003988eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 int evaluate;
3992{
Bram Moolenaar33570922005-01-25 22:26:29 +00003993 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 char_u *p;
3995 int i;
3996 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003997 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 int len = 2;
3999 long n1, n2;
4000 char_u *s1, *s2;
4001 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4002 regmatch_T regmatch;
4003 int ic;
4004 char_u *save_cpo;
4005
4006 /*
4007 * Get the first variable.
4008 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004009 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 return FAIL;
4011
4012 p = *arg;
4013 switch (p[0])
4014 {
4015 case '=': if (p[1] == '=')
4016 type = TYPE_EQUAL;
4017 else if (p[1] == '~')
4018 type = TYPE_MATCH;
4019 break;
4020 case '!': if (p[1] == '=')
4021 type = TYPE_NEQUAL;
4022 else if (p[1] == '~')
4023 type = TYPE_NOMATCH;
4024 break;
4025 case '>': if (p[1] != '=')
4026 {
4027 type = TYPE_GREATER;
4028 len = 1;
4029 }
4030 else
4031 type = TYPE_GEQUAL;
4032 break;
4033 case '<': if (p[1] != '=')
4034 {
4035 type = TYPE_SMALLER;
4036 len = 1;
4037 }
4038 else
4039 type = TYPE_SEQUAL;
4040 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004041 case 'i': if (p[1] == 's')
4042 {
4043 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4044 len = 5;
4045 if (!vim_isIDc(p[len]))
4046 {
4047 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4048 type_is = TRUE;
4049 }
4050 }
4051 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
4053
4054 /*
4055 * If there is a comparitive operator, use it.
4056 */
4057 if (type != TYPE_UNKNOWN)
4058 {
4059 /* extra question mark appended: ignore case */
4060 if (p[len] == '?')
4061 {
4062 ic = TRUE;
4063 ++len;
4064 }
4065 /* extra '#' appended: match case */
4066 else if (p[len] == '#')
4067 {
4068 ic = FALSE;
4069 ++len;
4070 }
4071 /* nothing appened: use 'ignorecase' */
4072 else
4073 ic = p_ic;
4074
4075 /*
4076 * Get the second variable.
4077 */
4078 *arg = skipwhite(p + len);
4079 if (eval5(arg, &var2, evaluate) == FAIL)
4080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 return FAIL;
4083 }
4084
4085 if (evaluate)
4086 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004087 if (type_is && rettv->v_type != var2.v_type)
4088 {
4089 /* For "is" a different type always means FALSE, for "notis"
4090 * it means TRUE. */
4091 n1 = (type == TYPE_NEQUAL);
4092 }
4093 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4094 {
4095 if (type_is)
4096 {
4097 n1 = (rettv->v_type == var2.v_type
4098 && rettv->vval.v_list == var2.vval.v_list);
4099 if (type == TYPE_NEQUAL)
4100 n1 = !n1;
4101 }
4102 else if (rettv->v_type != var2.v_type
4103 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4104 {
4105 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004106 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004107 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004108 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004109 clear_tv(rettv);
4110 clear_tv(&var2);
4111 return FAIL;
4112 }
4113 else
4114 {
4115 /* Compare two Lists for being equal or unequal. */
4116 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4117 if (type == TYPE_NEQUAL)
4118 n1 = !n1;
4119 }
4120 }
4121
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004122 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4123 {
4124 if (type_is)
4125 {
4126 n1 = (rettv->v_type == var2.v_type
4127 && rettv->vval.v_dict == var2.vval.v_dict);
4128 if (type == TYPE_NEQUAL)
4129 n1 = !n1;
4130 }
4131 else if (rettv->v_type != var2.v_type
4132 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4133 {
4134 if (rettv->v_type != var2.v_type)
4135 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4136 else
4137 EMSG(_("E736: Invalid operation for Dictionary"));
4138 clear_tv(rettv);
4139 clear_tv(&var2);
4140 return FAIL;
4141 }
4142 else
4143 {
4144 /* Compare two Dictionaries for being equal or unequal. */
4145 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4146 if (type == TYPE_NEQUAL)
4147 n1 = !n1;
4148 }
4149 }
4150
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004151 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4152 {
4153 if (rettv->v_type != var2.v_type
4154 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4155 {
4156 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004157 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004158 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004159 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004160 clear_tv(rettv);
4161 clear_tv(&var2);
4162 return FAIL;
4163 }
4164 else
4165 {
4166 /* Compare two Funcrefs for being equal or unequal. */
4167 if (rettv->vval.v_string == NULL
4168 || var2.vval.v_string == NULL)
4169 n1 = FALSE;
4170 else
4171 n1 = STRCMP(rettv->vval.v_string,
4172 var2.vval.v_string) == 0;
4173 if (type == TYPE_NEQUAL)
4174 n1 = !n1;
4175 }
4176 }
4177
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 /*
4179 * If one of the two variables is a number, compare as a number.
4180 * When using "=~" or "!~", always compare as string.
4181 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004182 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 n1 = get_tv_number(rettv);
4186 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 switch (type)
4188 {
4189 case TYPE_EQUAL: n1 = (n1 == n2); break;
4190 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4191 case TYPE_GREATER: n1 = (n1 > n2); break;
4192 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4193 case TYPE_SMALLER: n1 = (n1 < n2); break;
4194 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4195 case TYPE_UNKNOWN:
4196 case TYPE_MATCH:
4197 case TYPE_NOMATCH: break; /* avoid gcc warning */
4198 }
4199 }
4200 else
4201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 s1 = get_tv_string_buf(rettv, buf1);
4203 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4205 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4206 else
4207 i = 0;
4208 n1 = FALSE;
4209 switch (type)
4210 {
4211 case TYPE_EQUAL: n1 = (i == 0); break;
4212 case TYPE_NEQUAL: n1 = (i != 0); break;
4213 case TYPE_GREATER: n1 = (i > 0); break;
4214 case TYPE_GEQUAL: n1 = (i >= 0); break;
4215 case TYPE_SMALLER: n1 = (i < 0); break;
4216 case TYPE_SEQUAL: n1 = (i <= 0); break;
4217
4218 case TYPE_MATCH:
4219 case TYPE_NOMATCH:
4220 /* avoid 'l' flag in 'cpoptions' */
4221 save_cpo = p_cpo;
4222 p_cpo = (char_u *)"";
4223 regmatch.regprog = vim_regcomp(s2,
4224 RE_MAGIC + RE_STRING);
4225 regmatch.rm_ic = ic;
4226 if (regmatch.regprog != NULL)
4227 {
4228 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4229 vim_free(regmatch.regprog);
4230 if (type == TYPE_NOMATCH)
4231 n1 = !n1;
4232 }
4233 p_cpo = save_cpo;
4234 break;
4235
4236 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4237 }
4238 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239 clear_tv(rettv);
4240 clear_tv(&var2);
4241 rettv->v_type = VAR_NUMBER;
4242 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 }
4244 }
4245
4246 return OK;
4247}
4248
4249/*
4250 * Handle fourth level expression:
4251 * + number addition
4252 * - number subtraction
4253 * . string concatenation
4254 *
4255 * "arg" must point to the first non-white of the expression.
4256 * "arg" is advanced to the next non-white after the recognized expression.
4257 *
4258 * Return OK or FAIL.
4259 */
4260 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 int evaluate;
4265{
Bram Moolenaar33570922005-01-25 22:26:29 +00004266 typval_T var2;
4267 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 int op;
4269 long n1, n2;
4270 char_u *s1, *s2;
4271 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4272 char_u *p;
4273
4274 /*
4275 * Get the first variable.
4276 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 return FAIL;
4279
4280 /*
4281 * Repeat computing, until no '+', '-' or '.' is following.
4282 */
4283 for (;;)
4284 {
4285 op = **arg;
4286 if (op != '+' && op != '-' && op != '.')
4287 break;
4288
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004289 if (op != '+' || rettv->v_type != VAR_LIST)
4290 {
4291 /* For "list + ...", an illegal use of the first operand as
4292 * a number cannot be determined before evaluating the 2nd
4293 * operand: if this is also a list, all is ok.
4294 * For "something . ...", "something - ..." or "non-list + ...",
4295 * we know that the first operand needs to be a string or number
4296 * without evaluating the 2nd operand. So check before to avoid
4297 * side effects after an error. */
4298 if (evaluate && get_tv_string_chk(rettv) == NULL)
4299 {
4300 clear_tv(rettv);
4301 return FAIL;
4302 }
4303 }
4304
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 /*
4306 * Get the second variable.
4307 */
4308 *arg = skipwhite(*arg + 1);
4309 if (eval6(arg, &var2, evaluate) == FAIL)
4310 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 return FAIL;
4313 }
4314
4315 if (evaluate)
4316 {
4317 /*
4318 * Compute the result.
4319 */
4320 if (op == '.')
4321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004322 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4323 s2 = get_tv_string_buf_chk(&var2, buf2);
4324 if (s2 == NULL) /* type error ? */
4325 {
4326 clear_tv(rettv);
4327 clear_tv(&var2);
4328 return FAIL;
4329 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004330 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004331 clear_tv(rettv);
4332 rettv->v_type = VAR_STRING;
4333 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004335 else if (op == '+' && rettv->v_type == VAR_LIST
4336 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004337 {
4338 /* concatenate Lists */
4339 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4340 &var3) == FAIL)
4341 {
4342 clear_tv(rettv);
4343 clear_tv(&var2);
4344 return FAIL;
4345 }
4346 clear_tv(rettv);
4347 *rettv = var3;
4348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 else
4350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004351 int error = FALSE;
4352
4353 n1 = get_tv_number_chk(rettv, &error);
4354 if (error)
4355 {
4356 /* This can only happen for "list + non-list".
4357 * For "non-list + ..." or "something - ...", we returned
4358 * before evaluating the 2nd operand. */
4359 clear_tv(rettv);
4360 return FAIL;
4361 }
4362 n2 = get_tv_number_chk(&var2, &error);
4363 if (error)
4364 {
4365 clear_tv(rettv);
4366 clear_tv(&var2);
4367 return FAIL;
4368 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004369 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 if (op == '+')
4371 n1 = n1 + n2;
4372 else
4373 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004374 rettv->v_type = VAR_NUMBER;
4375 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004377 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379 }
4380 return OK;
4381}
4382
4383/*
4384 * Handle fifth level expression:
4385 * * number multiplication
4386 * / number division
4387 * % number modulo
4388 *
4389 * "arg" must point to the first non-white of the expression.
4390 * "arg" is advanced to the next non-white after the recognized expression.
4391 *
4392 * Return OK or FAIL.
4393 */
4394 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004395eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 int evaluate;
4399{
Bram Moolenaar33570922005-01-25 22:26:29 +00004400 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 int op;
4402 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004403 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
4405 /*
4406 * Get the first variable.
4407 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004408 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 return FAIL;
4410
4411 /*
4412 * Repeat computing, until no '*', '/' or '%' is following.
4413 */
4414 for (;;)
4415 {
4416 op = **arg;
4417 if (op != '*' && op != '/' && op != '%')
4418 break;
4419
4420 if (evaluate)
4421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004422 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004423 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004424 if (error)
4425 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 }
4427 else
4428 n1 = 0;
4429
4430 /*
4431 * Get the second variable.
4432 */
4433 *arg = skipwhite(*arg + 1);
4434 if (eval7(arg, &var2, evaluate) == FAIL)
4435 return FAIL;
4436
4437 if (evaluate)
4438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004439 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004440 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004441 if (error)
4442 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
4444 /*
4445 * Compute the result.
4446 */
4447 if (op == '*')
4448 n1 = n1 * n2;
4449 else if (op == '/')
4450 {
4451 if (n2 == 0) /* give an error message? */
4452 n1 = 0x7fffffffL;
4453 else
4454 n1 = n1 / n2;
4455 }
4456 else
4457 {
4458 if (n2 == 0) /* give an error message? */
4459 n1 = 0;
4460 else
4461 n1 = n1 % n2;
4462 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004463 rettv->v_type = VAR_NUMBER;
4464 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 }
4466 }
4467
4468 return OK;
4469}
4470
4471/*
4472 * Handle sixth level expression:
4473 * number number constant
4474 * "string" string contstant
4475 * 'string' literal string contstant
4476 * &option-name option value
4477 * @r register contents
4478 * identifier variable value
4479 * function() function call
4480 * $VAR environment variable
4481 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004482 * [expr, expr] List
4483 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 *
4485 * Also handle:
4486 * ! in front logical NOT
4487 * - in front unary minus
4488 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004489 * trailing [] subscript in String or List
4490 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 *
4492 * "arg" must point to the first non-white of the expression.
4493 * "arg" is advanced to the next non-white after the recognized expression.
4494 *
4495 * Return OK or FAIL.
4496 */
4497 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004498eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 int evaluate;
4502{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 long n;
4504 int len;
4505 char_u *s;
4506 int val;
4507 char_u *start_leader, *end_leader;
4508 int ret = OK;
4509 char_u *alias;
4510
4511 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004512 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004513 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004515 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516
4517 /*
4518 * Skip '!' and '-' characters. They are handled later.
4519 */
4520 start_leader = *arg;
4521 while (**arg == '!' || **arg == '-' || **arg == '+')
4522 *arg = skipwhite(*arg + 1);
4523 end_leader = *arg;
4524
4525 switch (**arg)
4526 {
4527 /*
4528 * Number constant.
4529 */
4530 case '0':
4531 case '1':
4532 case '2':
4533 case '3':
4534 case '4':
4535 case '5':
4536 case '6':
4537 case '7':
4538 case '8':
4539 case '9':
4540 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4541 *arg += len;
4542 if (evaluate)
4543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004544 rettv->v_type = VAR_NUMBER;
4545 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 }
4547 break;
4548
4549 /*
4550 * String constant: "string".
4551 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004552 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 break;
4554
4555 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004556 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004558 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004559 break;
4560
4561 /*
4562 * List: [expr, expr]
4563 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004564 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 break;
4566
4567 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004568 * Dictionary: {key: val, key: val}
4569 */
4570 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4571 break;
4572
4573 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004574 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004576 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 break;
4578
4579 /*
4580 * Environment variable: $VAR.
4581 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004582 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 break;
4584
4585 /*
4586 * Register contents: @r.
4587 */
4588 case '@': ++*arg;
4589 if (evaluate)
4590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004591 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004592 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 }
4594 if (**arg != NUL)
4595 ++*arg;
4596 break;
4597
4598 /*
4599 * nested expression: (expression).
4600 */
4601 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004602 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 if (**arg == ')')
4604 ++*arg;
4605 else if (ret == OK)
4606 {
4607 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004608 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 ret = FAIL;
4610 }
4611 break;
4612
Bram Moolenaar8c711452005-01-14 21:53:12 +00004613 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 break;
4615 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004616
4617 if (ret == NOTDONE)
4618 {
4619 /*
4620 * Must be a variable or function name.
4621 * Can also be a curly-braces kind of name: {expr}.
4622 */
4623 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004624 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004625 if (alias != NULL)
4626 s = alias;
4627
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004628 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004629 ret = FAIL;
4630 else
4631 {
4632 if (**arg == '(') /* recursive! */
4633 {
4634 /* If "s" is the name of a variable of type VAR_FUNC
4635 * use its contents. */
4636 s = deref_func_name(s, &len);
4637
4638 /* Invoke the function. */
4639 ret = get_func_tv(s, len, rettv, arg,
4640 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004641 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004642 /* Stop the expression evaluation when immediately
4643 * aborting on error, or when an interrupt occurred or
4644 * an exception was thrown but not caught. */
4645 if (aborting())
4646 {
4647 if (ret == OK)
4648 clear_tv(rettv);
4649 ret = FAIL;
4650 }
4651 }
4652 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004653 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004654 else
4655 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004656 }
4657
4658 if (alias != NULL)
4659 vim_free(alias);
4660 }
4661
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 *arg = skipwhite(*arg);
4663
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004664 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4665 * expr(expr). */
4666 if (ret == OK)
4667 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668
4669 /*
4670 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4671 */
4672 if (ret == OK && evaluate && end_leader > start_leader)
4673 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004674 int error = FALSE;
4675
4676 val = get_tv_number_chk(rettv, &error);
4677 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004679 clear_tv(rettv);
4680 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004682 else
4683 {
4684 while (end_leader > start_leader)
4685 {
4686 --end_leader;
4687 if (*end_leader == '!')
4688 val = !val;
4689 else if (*end_leader == '-')
4690 val = -val;
4691 }
4692 clear_tv(rettv);
4693 rettv->v_type = VAR_NUMBER;
4694 rettv->vval.v_number = val;
4695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 }
4697
4698 return ret;
4699}
4700
4701/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004702 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4703 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004704 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4705 */
4706 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004707eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004708 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004709 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004710 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004711 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004712{
4713 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004714 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004715 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004716 long len = -1;
4717 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004719 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004720
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004721 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004723 if (verbose)
4724 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004725 return FAIL;
4726 }
4727
Bram Moolenaar8c711452005-01-14 21:53:12 +00004728 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004729 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004730 /*
4731 * dict.name
4732 */
4733 key = *arg + 1;
4734 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4735 ;
4736 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004737 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004738 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004739 }
4740 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004741 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004742 /*
4743 * something[idx]
4744 *
4745 * Get the (first) variable from inside the [].
4746 */
4747 *arg = skipwhite(*arg + 1);
4748 if (**arg == ':')
4749 empty1 = TRUE;
4750 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4751 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004752 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4753 {
4754 /* not a number or string */
4755 clear_tv(&var1);
4756 return FAIL;
4757 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004758
4759 /*
4760 * Get the second variable from inside the [:].
4761 */
4762 if (**arg == ':')
4763 {
4764 range = TRUE;
4765 *arg = skipwhite(*arg + 1);
4766 if (**arg == ']')
4767 empty2 = TRUE;
4768 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770 if (!empty1)
4771 clear_tv(&var1);
4772 return FAIL;
4773 }
4774 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4775 {
4776 /* not a number or string */
4777 if (!empty1)
4778 clear_tv(&var1);
4779 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004780 return FAIL;
4781 }
4782 }
4783
4784 /* Check for the ']'. */
4785 if (**arg != ']')
4786 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004787 if (verbose)
4788 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004789 clear_tv(&var1);
4790 if (range)
4791 clear_tv(&var2);
4792 return FAIL;
4793 }
4794 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004795 }
4796
4797 if (evaluate)
4798 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004799 n1 = 0;
4800 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004802 n1 = get_tv_number(&var1);
4803 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004804 }
4805 if (range)
4806 {
4807 if (empty2)
4808 n2 = -1;
4809 else
4810 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004811 n2 = get_tv_number(&var2);
4812 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004813 }
4814 }
4815
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004816 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004817 {
4818 case VAR_NUMBER:
4819 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004820 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004821 len = (long)STRLEN(s);
4822 if (range)
4823 {
4824 /* The resulting variable is a substring. If the indexes
4825 * are out of range the result is empty. */
4826 if (n1 < 0)
4827 {
4828 n1 = len + n1;
4829 if (n1 < 0)
4830 n1 = 0;
4831 }
4832 if (n2 < 0)
4833 n2 = len + n2;
4834 else if (n2 >= len)
4835 n2 = len;
4836 if (n1 >= len || n2 < 0 || n1 > n2)
4837 s = NULL;
4838 else
4839 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4840 }
4841 else
4842 {
4843 /* The resulting variable is a string of a single
4844 * character. If the index is too big or negative the
4845 * result is empty. */
4846 if (n1 >= len || n1 < 0)
4847 s = NULL;
4848 else
4849 s = vim_strnsave(s + n1, 1);
4850 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 clear_tv(rettv);
4852 rettv->v_type = VAR_STRING;
4853 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004854 break;
4855
4856 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004857 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004858 if (n1 < 0)
4859 n1 = len + n1;
4860 if (!empty1 && (n1 < 0 || n1 >= len))
4861 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004862 /* For a range we allow invalid values and return an empty
4863 * list. A list index out of range is an error. */
4864 if (!range)
4865 {
4866 if (verbose)
4867 EMSGN(_(e_listidx), n1);
4868 return FAIL;
4869 }
4870 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004871 }
4872 if (range)
4873 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004874 list_T *l;
4875 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004876
4877 if (n2 < 0)
4878 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004879 else if (n2 >= len)
4880 n2 = len - 1;
4881 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004882 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004883 l = list_alloc();
4884 if (l == NULL)
4885 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004886 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004887 n1 <= n2; ++n1)
4888 {
4889 if (list_append_tv(l, &item->li_tv) == FAIL)
4890 {
4891 list_free(l);
4892 return FAIL;
4893 }
4894 item = item->li_next;
4895 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004896 clear_tv(rettv);
4897 rettv->v_type = VAR_LIST;
4898 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004899 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004900 }
4901 else
4902 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004903 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904 clear_tv(rettv);
4905 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004906 }
4907 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004908
4909 case VAR_DICT:
4910 if (range)
4911 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004912 if (verbose)
4913 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004914 if (len == -1)
4915 clear_tv(&var1);
4916 return FAIL;
4917 }
4918 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004919 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004920
4921 if (len == -1)
4922 {
4923 key = get_tv_string(&var1);
4924 if (*key == NUL)
4925 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004926 if (verbose)
4927 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004928 clear_tv(&var1);
4929 return FAIL;
4930 }
4931 }
4932
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004933 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004934
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004935 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004936 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004937 if (len == -1)
4938 clear_tv(&var1);
4939 if (item == NULL)
4940 return FAIL;
4941
4942 copy_tv(&item->di_tv, &var1);
4943 clear_tv(rettv);
4944 *rettv = var1;
4945 }
4946 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004947 }
4948 }
4949
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004950 return OK;
4951}
4952
4953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 * Get an option value.
4955 * "arg" points to the '&' or '+' before the option name.
4956 * "arg" is advanced to character after the option name.
4957 * Return OK or FAIL.
4958 */
4959 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004960get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004962 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 int evaluate;
4964{
4965 char_u *option_end;
4966 long numval;
4967 char_u *stringval;
4968 int opt_type;
4969 int c;
4970 int working = (**arg == '+'); /* has("+option") */
4971 int ret = OK;
4972 int opt_flags;
4973
4974 /*
4975 * Isolate the option name and find its value.
4976 */
4977 option_end = find_option_end(arg, &opt_flags);
4978 if (option_end == NULL)
4979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004980 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 EMSG2(_("E112: Option name missing: %s"), *arg);
4982 return FAIL;
4983 }
4984
4985 if (!evaluate)
4986 {
4987 *arg = option_end;
4988 return OK;
4989 }
4990
4991 c = *option_end;
4992 *option_end = NUL;
4993 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004994 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995
4996 if (opt_type == -3) /* invalid name */
4997 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004998 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 EMSG2(_("E113: Unknown option: %s"), *arg);
5000 ret = FAIL;
5001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005002 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
5004 if (opt_type == -2) /* hidden string option */
5005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005006 rettv->v_type = VAR_STRING;
5007 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
5009 else if (opt_type == -1) /* hidden number option */
5010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005011 rettv->v_type = VAR_NUMBER;
5012 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
5014 else if (opt_type == 1) /* number option */
5015 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005016 rettv->v_type = VAR_NUMBER;
5017 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 else /* string option */
5020 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005021 rettv->v_type = VAR_STRING;
5022 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024 }
5025 else if (working && (opt_type == -2 || opt_type == -1))
5026 ret = FAIL;
5027
5028 *option_end = c; /* put back for error messages */
5029 *arg = option_end;
5030
5031 return ret;
5032}
5033
5034/*
5035 * Allocate a variable for a string constant.
5036 * Return OK or FAIL.
5037 */
5038 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005039get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 int evaluate;
5043{
5044 char_u *p;
5045 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 int extra = 0;
5047
5048 /*
5049 * Find the end of the string, skipping backslashed characters.
5050 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005051 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
5053 if (*p == '\\' && p[1] != NUL)
5054 {
5055 ++p;
5056 /* A "\<x>" form occupies at least 4 characters, and produces up
5057 * to 6 characters: reserve space for 2 extra */
5058 if (*p == '<')
5059 extra += 2;
5060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 }
5062
5063 if (*p != '"')
5064 {
5065 EMSG2(_("E114: Missing quote: %s"), *arg);
5066 return FAIL;
5067 }
5068
5069 /* If only parsing, set *arg and return here */
5070 if (!evaluate)
5071 {
5072 *arg = p + 1;
5073 return OK;
5074 }
5075
5076 /*
5077 * Copy the string into allocated memory, handling backslashed
5078 * characters.
5079 */
5080 name = alloc((unsigned)(p - *arg + extra));
5081 if (name == NULL)
5082 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005083 rettv->v_type = VAR_STRING;
5084 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 {
5088 if (*p == '\\')
5089 {
5090 switch (*++p)
5091 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092 case 'b': *name++ = BS; ++p; break;
5093 case 'e': *name++ = ESC; ++p; break;
5094 case 'f': *name++ = FF; ++p; break;
5095 case 'n': *name++ = NL; ++p; break;
5096 case 'r': *name++ = CAR; ++p; break;
5097 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098
5099 case 'X': /* hex: "\x1", "\x12" */
5100 case 'x':
5101 case 'u': /* Unicode: "\u0023" */
5102 case 'U':
5103 if (vim_isxdigit(p[1]))
5104 {
5105 int n, nr;
5106 int c = toupper(*p);
5107
5108 if (c == 'X')
5109 n = 2;
5110 else
5111 n = 4;
5112 nr = 0;
5113 while (--n >= 0 && vim_isxdigit(p[1]))
5114 {
5115 ++p;
5116 nr = (nr << 4) + hex2nr(*p);
5117 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005118 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119#ifdef FEAT_MBYTE
5120 /* For "\u" store the number according to
5121 * 'encoding'. */
5122 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005123 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 else
5125#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005126 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 break;
5129
5130 /* octal: "\1", "\12", "\123" */
5131 case '0':
5132 case '1':
5133 case '2':
5134 case '3':
5135 case '4':
5136 case '5':
5137 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 case '7': *name = *p++ - '0';
5139 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141 *name = (*name << 3) + *p++ - '0';
5142 if (*p >= '0' && *p <= '7')
5143 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 break;
5147
5148 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 if (extra != 0)
5151 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 break;
5154 }
5155 /* FALLTHROUGH */
5156
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 break;
5159 }
5160 }
5161 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 *arg = p + 1;
5167
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 return OK;
5169}
5170
5171/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 * Return OK or FAIL.
5174 */
5175 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 int evaluate;
5180{
5181 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005182 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005183 int reduce = 0;
5184
5185 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005187 */
5188 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5189 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005190 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005191 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005193 break;
5194 ++reduce;
5195 ++p;
5196 }
5197 }
5198
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005200 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005201 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005202 return FAIL;
5203 }
5204
Bram Moolenaar8c711452005-01-14 21:53:12 +00005205 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005206 if (!evaluate)
5207 {
5208 *arg = p + 1;
5209 return OK;
5210 }
5211
5212 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005214 */
5215 str = alloc((unsigned)((p - *arg) - reduce));
5216 if (str == NULL)
5217 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005218 rettv->v_type = VAR_STRING;
5219 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005220
Bram Moolenaar8c711452005-01-14 21:53:12 +00005221 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005222 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005224 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005226 break;
5227 ++p;
5228 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005230 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005232 *arg = p + 1;
5233
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005234 return OK;
5235}
5236
5237/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238 * Allocate a variable for a List and fill it from "*arg".
5239 * Return OK or FAIL.
5240 */
5241 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005242get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005244 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005245 int evaluate;
5246{
Bram Moolenaar33570922005-01-25 22:26:29 +00005247 list_T *l = NULL;
5248 typval_T tv;
5249 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250
5251 if (evaluate)
5252 {
5253 l = list_alloc();
5254 if (l == NULL)
5255 return FAIL;
5256 }
5257
5258 *arg = skipwhite(*arg + 1);
5259 while (**arg != ']' && **arg != NUL)
5260 {
5261 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5262 goto failret;
5263 if (evaluate)
5264 {
5265 item = listitem_alloc();
5266 if (item != NULL)
5267 {
5268 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005269 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270 list_append(l, item);
5271 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005272 else
5273 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 }
5275
5276 if (**arg == ']')
5277 break;
5278 if (**arg != ',')
5279 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 goto failret;
5282 }
5283 *arg = skipwhite(*arg + 1);
5284 }
5285
5286 if (**arg != ']')
5287 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005288 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289failret:
5290 if (evaluate)
5291 list_free(l);
5292 return FAIL;
5293 }
5294
5295 *arg = skipwhite(*arg + 1);
5296 if (evaluate)
5297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005298 rettv->v_type = VAR_LIST;
5299 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 ++l->lv_refcount;
5301 }
5302
5303 return OK;
5304}
5305
5306/*
5307 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005308 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005310 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311list_alloc()
5312{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005313 list_T *l;
5314
5315 l = (list_T *)alloc_clear(sizeof(list_T));
5316 if (l != NULL)
5317 {
5318 /* Prepend the list to the list of lists for garbage collection. */
5319 if (first_list != NULL)
5320 first_list->lv_used_prev = l;
5321 l->lv_used_prev = NULL;
5322 l->lv_used_next = first_list;
5323 first_list = l;
5324 }
5325 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326}
5327
5328/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005329 * Allocate an empty list for a return value.
5330 * Returns OK or FAIL.
5331 */
5332 static int
5333rettv_list_alloc(rettv)
5334 typval_T *rettv;
5335{
5336 list_T *l = list_alloc();
5337
5338 if (l == NULL)
5339 return FAIL;
5340
5341 rettv->vval.v_list = l;
5342 rettv->v_type = VAR_LIST;
5343 ++l->lv_refcount;
5344 return OK;
5345}
5346
5347/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348 * Unreference a list: decrement the reference count and free it when it
5349 * becomes zero.
5350 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005351 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005353 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005355 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5356 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357}
5358
5359/*
5360 * Free a list, including all items it points to.
5361 * Ignores the reference count.
5362 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005363 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005365 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366{
Bram Moolenaar33570922005-01-25 22:26:29 +00005367 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368
Bram Moolenaard9fba312005-06-26 22:34:35 +00005369 /* Avoid that recursive reference to the list frees us again. */
5370 l->lv_refcount = DEL_REFCOUNT;
5371
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005372 /* Remove the list from the list of lists for garbage collection. */
5373 if (l->lv_used_prev == NULL)
5374 first_list = l->lv_used_next;
5375 else
5376 l->lv_used_prev->lv_used_next = l->lv_used_next;
5377 if (l->lv_used_next != NULL)
5378 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5379
Bram Moolenaard9fba312005-06-26 22:34:35 +00005380 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005382 /* Remove the item before deleting it. */
5383 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 listitem_free(item);
5385 }
5386 vim_free(l);
5387}
5388
5389/*
5390 * Allocate a list item.
5391 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005392 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393listitem_alloc()
5394{
Bram Moolenaar33570922005-01-25 22:26:29 +00005395 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396}
5397
5398/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005399 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 */
5401 static void
5402listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005403 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005405 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 vim_free(item);
5407}
5408
5409/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005410 * Remove a list item from a List and free it. Also clears the value.
5411 */
5412 static void
5413listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005414 list_T *l;
5415 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005416{
5417 list_remove(l, item, item);
5418 listitem_free(item);
5419}
5420
5421/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 * Get the number of items in a list.
5423 */
5424 static long
5425list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005426 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428 if (l == NULL)
5429 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005430 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431}
5432
5433/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005434 * Return TRUE when two lists have exactly the same values.
5435 */
5436 static int
5437list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005438 list_T *l1;
5439 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005440 int ic; /* ignore case for strings */
5441{
Bram Moolenaar33570922005-01-25 22:26:29 +00005442 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005443
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005444 if (list_len(l1) != list_len(l2))
5445 return FALSE;
5446
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005447 for (item1 = l1->lv_first, item2 = l2->lv_first;
5448 item1 != NULL && item2 != NULL;
5449 item1 = item1->li_next, item2 = item2->li_next)
5450 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5451 return FALSE;
5452 return item1 == NULL && item2 == NULL;
5453}
5454
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005455#if defined(FEAT_PYTHON) || defined(PROTO)
5456/*
5457 * Return the dictitem that an entry in a hashtable points to.
5458 */
5459 dictitem_T *
5460dict_lookup(hi)
5461 hashitem_T *hi;
5462{
5463 return HI2DI(hi);
5464}
5465#endif
5466
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005467/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005468 * Return TRUE when two dictionaries have exactly the same key/values.
5469 */
5470 static int
5471dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005472 dict_T *d1;
5473 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005474 int ic; /* ignore case for strings */
5475{
Bram Moolenaar33570922005-01-25 22:26:29 +00005476 hashitem_T *hi;
5477 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005478 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005479
5480 if (dict_len(d1) != dict_len(d2))
5481 return FALSE;
5482
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005483 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005484 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005485 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005486 if (!HASHITEM_EMPTY(hi))
5487 {
5488 item2 = dict_find(d2, hi->hi_key, -1);
5489 if (item2 == NULL)
5490 return FALSE;
5491 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5492 return FALSE;
5493 --todo;
5494 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005495 }
5496 return TRUE;
5497}
5498
5499/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005500 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005501 * Compares the items just like "==" would compare them, but strings and
5502 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005503 */
5504 static int
5505tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005506 typval_T *tv1;
5507 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005508 int ic; /* ignore case */
5509{
5510 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005511 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005512
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005513 if (tv1->v_type != tv2->v_type)
5514 return FALSE;
5515
5516 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005517 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005518 case VAR_LIST:
5519 /* recursive! */
5520 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5521
5522 case VAR_DICT:
5523 /* recursive! */
5524 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5525
5526 case VAR_FUNC:
5527 return (tv1->vval.v_string != NULL
5528 && tv2->vval.v_string != NULL
5529 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5530
5531 case VAR_NUMBER:
5532 return tv1->vval.v_number == tv2->vval.v_number;
5533
5534 case VAR_STRING:
5535 s1 = get_tv_string_buf(tv1, buf1);
5536 s2 = get_tv_string_buf(tv2, buf2);
5537 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005538 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005539
5540 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005541 return TRUE;
5542}
5543
5544/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 * Locate item with index "n" in list "l" and return it.
5546 * A negative index is counted from the end; -1 is the last item.
5547 * Returns NULL when "n" is out of range.
5548 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005549 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005551 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 long n;
5553{
Bram Moolenaar33570922005-01-25 22:26:29 +00005554 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 long idx;
5556
5557 if (l == NULL)
5558 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005559
5560 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005562 n = l->lv_len + n;
5563
5564 /* Check for index out of range. */
5565 if (n < 0 || n >= l->lv_len)
5566 return NULL;
5567
5568 /* When there is a cached index may start search from there. */
5569 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005570 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005571 if (n < l->lv_idx / 2)
5572 {
5573 /* closest to the start of the list */
5574 item = l->lv_first;
5575 idx = 0;
5576 }
5577 else if (n > (l->lv_idx + l->lv_len) / 2)
5578 {
5579 /* closest to the end of the list */
5580 item = l->lv_last;
5581 idx = l->lv_len - 1;
5582 }
5583 else
5584 {
5585 /* closest to the cached index */
5586 item = l->lv_idx_item;
5587 idx = l->lv_idx;
5588 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005589 }
5590 else
5591 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005592 if (n < l->lv_len / 2)
5593 {
5594 /* closest to the start of the list */
5595 item = l->lv_first;
5596 idx = 0;
5597 }
5598 else
5599 {
5600 /* closest to the end of the list */
5601 item = l->lv_last;
5602 idx = l->lv_len - 1;
5603 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005605
5606 while (n > idx)
5607 {
5608 /* search forward */
5609 item = item->li_next;
5610 ++idx;
5611 }
5612 while (n < idx)
5613 {
5614 /* search backward */
5615 item = item->li_prev;
5616 --idx;
5617 }
5618
5619 /* cache the used index */
5620 l->lv_idx = idx;
5621 l->lv_idx_item = item;
5622
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 return item;
5624}
5625
5626/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005627 * Get list item "l[idx]" as a number.
5628 */
5629 static long
5630list_find_nr(l, idx, errorp)
5631 list_T *l;
5632 long idx;
5633 int *errorp; /* set to TRUE when something wrong */
5634{
5635 listitem_T *li;
5636
5637 li = list_find(l, idx);
5638 if (li == NULL)
5639 {
5640 if (errorp != NULL)
5641 *errorp = TRUE;
5642 return -1L;
5643 }
5644 return get_tv_number_chk(&li->li_tv, errorp);
5645}
5646
5647/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005648 * Locate "item" list "l" and return its index.
5649 * Returns -1 when "item" is not in the list.
5650 */
5651 static long
5652list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005653 list_T *l;
5654 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005655{
5656 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005657 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005658
5659 if (l == NULL)
5660 return -1;
5661 idx = 0;
5662 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5663 ++idx;
5664 if (li == NULL)
5665 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005666 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005667}
5668
5669/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005670 * Append item "item" to the end of list "l".
5671 */
5672 static void
5673list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005674 list_T *l;
5675 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005676{
5677 if (l->lv_last == NULL)
5678 {
5679 /* empty list */
5680 l->lv_first = item;
5681 l->lv_last = item;
5682 item->li_prev = NULL;
5683 }
5684 else
5685 {
5686 l->lv_last->li_next = item;
5687 item->li_prev = l->lv_last;
5688 l->lv_last = item;
5689 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005690 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005691 item->li_next = NULL;
5692}
5693
5694/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005695 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005696 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005697 */
5698 static int
5699list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005700 list_T *l;
5701 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005703 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005704
Bram Moolenaar05159a02005-02-26 23:04:13 +00005705 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005706 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005707 copy_tv(tv, &li->li_tv);
5708 list_append(l, li);
5709 return OK;
5710}
5711
5712/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005713 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005714 * Return FAIL when out of memory.
5715 */
5716 int
5717list_append_dict(list, dict)
5718 list_T *list;
5719 dict_T *dict;
5720{
5721 listitem_T *li = listitem_alloc();
5722
5723 if (li == NULL)
5724 return FAIL;
5725 li->li_tv.v_type = VAR_DICT;
5726 li->li_tv.v_lock = 0;
5727 li->li_tv.vval.v_dict = dict;
5728 list_append(list, li);
5729 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005730 return OK;
5731}
5732
5733/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005734 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005735 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005736 * Returns FAIL when out of memory.
5737 */
5738 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005739list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005740 list_T *l;
5741 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005742 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005743{
5744 listitem_T *li = listitem_alloc();
5745
5746 if (li == NULL)
5747 return FAIL;
5748 list_append(l, li);
5749 li->li_tv.v_type = VAR_STRING;
5750 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005751 if (str == NULL)
5752 li->li_tv.vval.v_string = NULL;
5753 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005754 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005755 return FAIL;
5756 return OK;
5757}
5758
5759/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005760 * Append "n" to list "l".
5761 * Returns FAIL when out of memory.
5762 */
5763 static int
5764list_append_number(l, n)
5765 list_T *l;
5766 varnumber_T n;
5767{
5768 listitem_T *li;
5769
5770 li = listitem_alloc();
5771 if (li == NULL)
5772 return FAIL;
5773 li->li_tv.v_type = VAR_NUMBER;
5774 li->li_tv.v_lock = 0;
5775 li->li_tv.vval.v_number = n;
5776 list_append(l, li);
5777 return OK;
5778}
5779
5780/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005781 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005782 * If "item" is NULL append at the end.
5783 * Return FAIL when out of memory.
5784 */
5785 static int
5786list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005787 list_T *l;
5788 typval_T *tv;
5789 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005790{
Bram Moolenaar33570922005-01-25 22:26:29 +00005791 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005792
5793 if (ni == NULL)
5794 return FAIL;
5795 copy_tv(tv, &ni->li_tv);
5796 if (item == NULL)
5797 /* Append new item at end of list. */
5798 list_append(l, ni);
5799 else
5800 {
5801 /* Insert new item before existing item. */
5802 ni->li_prev = item->li_prev;
5803 ni->li_next = item;
5804 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005805 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005806 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005807 ++l->lv_idx;
5808 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005809 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005810 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005811 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005812 l->lv_idx_item = NULL;
5813 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005814 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005815 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005816 }
5817 return OK;
5818}
5819
5820/*
5821 * Extend "l1" with "l2".
5822 * If "bef" is NULL append at the end, otherwise insert before this item.
5823 * Returns FAIL when out of memory.
5824 */
5825 static int
5826list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005827 list_T *l1;
5828 list_T *l2;
5829 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005830{
Bram Moolenaar33570922005-01-25 22:26:29 +00005831 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005832
5833 for (item = l2->lv_first; item != NULL; item = item->li_next)
5834 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5835 return FAIL;
5836 return OK;
5837}
5838
5839/*
5840 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5841 * Return FAIL when out of memory.
5842 */
5843 static int
5844list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005845 list_T *l1;
5846 list_T *l2;
5847 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005848{
Bram Moolenaar33570922005-01-25 22:26:29 +00005849 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005850
5851 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005852 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005853 if (l == NULL)
5854 return FAIL;
5855 tv->v_type = VAR_LIST;
5856 tv->vval.v_list = l;
5857
5858 /* append all items from the second list */
5859 return list_extend(l, l2, NULL);
5860}
5861
5862/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005863 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005865 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005866 * Returns NULL when out of memory.
5867 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005868 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005869list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005870 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005872 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873{
Bram Moolenaar33570922005-01-25 22:26:29 +00005874 list_T *copy;
5875 listitem_T *item;
5876 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005877
5878 if (orig == NULL)
5879 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880
5881 copy = list_alloc();
5882 if (copy != NULL)
5883 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005884 if (copyID != 0)
5885 {
5886 /* Do this before adding the items, because one of the items may
5887 * refer back to this list. */
5888 orig->lv_copyID = copyID;
5889 orig->lv_copylist = copy;
5890 }
5891 for (item = orig->lv_first; item != NULL && !got_int;
5892 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005893 {
5894 ni = listitem_alloc();
5895 if (ni == NULL)
5896 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005897 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005898 {
5899 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5900 {
5901 vim_free(ni);
5902 break;
5903 }
5904 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005906 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 list_append(copy, ni);
5908 }
5909 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005910 if (item != NULL)
5911 {
5912 list_unref(copy);
5913 copy = NULL;
5914 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915 }
5916
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917 return copy;
5918}
5919
5920/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005921 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005922 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005924 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005925list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 list_T *l;
5927 listitem_T *item;
5928 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005929{
Bram Moolenaar33570922005-01-25 22:26:29 +00005930 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005932 /* notify watchers */
5933 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005935 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005936 list_fix_watch(l, ip);
5937 if (ip == item2)
5938 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005940
5941 if (item2->li_next == NULL)
5942 l->lv_last = item->li_prev;
5943 else
5944 item2->li_next->li_prev = item->li_prev;
5945 if (item->li_prev == NULL)
5946 l->lv_first = item2->li_next;
5947 else
5948 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005949 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950}
5951
5952/*
5953 * Return an allocated string with the string representation of a list.
5954 * May return NULL.
5955 */
5956 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005957list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005958 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005959 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960{
5961 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962
5963 if (tv->vval.v_list == NULL)
5964 return NULL;
5965 ga_init2(&ga, (int)sizeof(char), 80);
5966 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005967 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005968 {
5969 vim_free(ga.ga_data);
5970 return NULL;
5971 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972 ga_append(&ga, ']');
5973 ga_append(&ga, NUL);
5974 return (char_u *)ga.ga_data;
5975}
5976
5977/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005978 * Join list "l" into a string in "*gap", using separator "sep".
5979 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005980 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005981 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005982 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005983list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005984 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005985 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005986 char_u *sep;
5987 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005988 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005989{
5990 int first = TRUE;
5991 char_u *tofree;
5992 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005993 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005994 char_u *s;
5995
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005996 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005997 {
5998 if (first)
5999 first = FALSE;
6000 else
6001 ga_concat(gap, sep);
6002
6003 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006004 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006005 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006006 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006007 if (s != NULL)
6008 ga_concat(gap, s);
6009 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006010 if (s == NULL)
6011 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006012 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006013 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006014}
6015
6016/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006017 * Garbage collection for lists and dictionaries.
6018 *
6019 * We use reference counts to be able to free most items right away when they
6020 * are no longer used. But for composite items it's possible that it becomes
6021 * unused while the reference count is > 0: When there is a recursive
6022 * reference. Example:
6023 * :let l = [1, 2, 3]
6024 * :let d = {9: l}
6025 * :let l[1] = d
6026 *
6027 * Since this is quite unusual we handle this with garbage collection: every
6028 * once in a while find out which lists and dicts are not referenced from any
6029 * variable.
6030 *
6031 * Here is a good reference text about garbage collection (refers to Python
6032 * but it applies to all reference-counting mechanisms):
6033 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006034 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006035
6036/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006037 * Do garbage collection for lists and dicts.
6038 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006039 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006040 int
6041garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006042{
6043 dict_T *dd;
6044 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006045 int copyID = ++current_copyID;
6046 buf_T *buf;
6047 win_T *wp;
6048 int i;
6049 funccall_T *fc;
6050 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006051#ifdef FEAT_WINDOWS
6052 tabpage_T *tp;
6053#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006054
6055 /*
6056 * 1. Go through all accessible variables and mark all lists and dicts
6057 * with copyID.
6058 */
6059 /* script-local variables */
6060 for (i = 1; i <= ga_scripts.ga_len; ++i)
6061 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6062
6063 /* buffer-local variables */
6064 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6065 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6066
6067 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006068 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006069 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6070
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006071#ifdef FEAT_WINDOWS
6072 /* tabpage-local variables */
6073 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6074 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6075#endif
6076
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006077 /* global variables */
6078 set_ref_in_ht(&globvarht, copyID);
6079
6080 /* function-local variables */
6081 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6082 {
6083 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6084 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6085 }
6086
6087 /*
6088 * 2. Go through the list of dicts and free items without the copyID.
6089 */
6090 for (dd = first_dict; dd != NULL; )
6091 if (dd->dv_copyID != copyID)
6092 {
6093 dict_free(dd);
6094 did_free = TRUE;
6095
6096 /* restart, next dict may also have been freed */
6097 dd = first_dict;
6098 }
6099 else
6100 dd = dd->dv_used_next;
6101
6102 /*
6103 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006104 * But don't free a list that has a watcher (used in a for loop), these
6105 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006106 */
6107 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006108 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006109 {
6110 list_free(ll);
6111 did_free = TRUE;
6112
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006113 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006114 ll = first_list;
6115 }
6116 else
6117 ll = ll->lv_used_next;
6118
6119 return did_free;
6120}
6121
6122/*
6123 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6124 */
6125 static void
6126set_ref_in_ht(ht, copyID)
6127 hashtab_T *ht;
6128 int copyID;
6129{
6130 int todo;
6131 hashitem_T *hi;
6132
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006133 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006134 for (hi = ht->ht_array; todo > 0; ++hi)
6135 if (!HASHITEM_EMPTY(hi))
6136 {
6137 --todo;
6138 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6139 }
6140}
6141
6142/*
6143 * Mark all lists and dicts referenced through list "l" with "copyID".
6144 */
6145 static void
6146set_ref_in_list(l, copyID)
6147 list_T *l;
6148 int copyID;
6149{
6150 listitem_T *li;
6151
6152 for (li = l->lv_first; li != NULL; li = li->li_next)
6153 set_ref_in_item(&li->li_tv, copyID);
6154}
6155
6156/*
6157 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6158 */
6159 static void
6160set_ref_in_item(tv, copyID)
6161 typval_T *tv;
6162 int copyID;
6163{
6164 dict_T *dd;
6165 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006166
6167 switch (tv->v_type)
6168 {
6169 case VAR_DICT:
6170 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006171 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006172 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006173 /* Didn't see this dict yet. */
6174 dd->dv_copyID = copyID;
6175 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006176 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006177 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006178
6179 case VAR_LIST:
6180 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006181 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006182 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006183 /* Didn't see this list yet. */
6184 ll->lv_copyID = copyID;
6185 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006186 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006187 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006188 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006189 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006190}
6191
6192/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006193 * Allocate an empty header for a dictionary.
6194 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006195 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006196dict_alloc()
6197{
Bram Moolenaar33570922005-01-25 22:26:29 +00006198 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006199
Bram Moolenaar33570922005-01-25 22:26:29 +00006200 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006201 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006202 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006203 /* Add the list to the hashtable for garbage collection. */
6204 if (first_dict != NULL)
6205 first_dict->dv_used_prev = d;
6206 d->dv_used_next = first_dict;
6207 d->dv_used_prev = NULL;
6208
Bram Moolenaar33570922005-01-25 22:26:29 +00006209 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006210 d->dv_lock = 0;
6211 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006212 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006213 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006214 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006215}
6216
6217/*
6218 * Unreference a Dictionary: decrement the reference count and free it when it
6219 * becomes zero.
6220 */
6221 static void
6222dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006223 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006224{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006225 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6226 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006227}
6228
6229/*
6230 * Free a Dictionary, including all items it contains.
6231 * Ignores the reference count.
6232 */
6233 static void
6234dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006235 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006236{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006237 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006238 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006239 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006240
Bram Moolenaard9fba312005-06-26 22:34:35 +00006241 /* Avoid that recursive reference to the dict frees us again. */
6242 d->dv_refcount = DEL_REFCOUNT;
6243
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006244 /* Remove the dict from the list of dicts for garbage collection. */
6245 if (d->dv_used_prev == NULL)
6246 first_dict = d->dv_used_next;
6247 else
6248 d->dv_used_prev->dv_used_next = d->dv_used_next;
6249 if (d->dv_used_next != NULL)
6250 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6251
6252 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006253 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006254 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006255 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006256 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006257 if (!HASHITEM_EMPTY(hi))
6258 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006259 /* Remove the item before deleting it, just in case there is
6260 * something recursive causing trouble. */
6261 di = HI2DI(hi);
6262 hash_remove(&d->dv_hashtab, hi);
6263 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006264 --todo;
6265 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006266 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006267 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006268 vim_free(d);
6269}
6270
6271/*
6272 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006273 * The "key" is copied to the new item.
6274 * Note that the value of the item "di_tv" still needs to be initialized!
6275 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006276 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006277 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006278dictitem_alloc(key)
6279 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006280{
Bram Moolenaar33570922005-01-25 22:26:29 +00006281 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006282
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006283 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006284 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006285 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006286 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006287 di->di_flags = 0;
6288 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006289 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006290}
6291
6292/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006293 * Make a copy of a Dictionary item.
6294 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006296dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006297 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006298{
Bram Moolenaar33570922005-01-25 22:26:29 +00006299 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006300
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006301 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6302 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006303 if (di != NULL)
6304 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006305 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006306 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006307 copy_tv(&org->di_tv, &di->di_tv);
6308 }
6309 return di;
6310}
6311
6312/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006313 * Remove item "item" from Dictionary "dict" and free it.
6314 */
6315 static void
6316dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006317 dict_T *dict;
6318 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006319{
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006321
Bram Moolenaar33570922005-01-25 22:26:29 +00006322 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006323 if (HASHITEM_EMPTY(hi))
6324 EMSG2(_(e_intern2), "dictitem_remove()");
6325 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006326 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006327 dictitem_free(item);
6328}
6329
6330/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006331 * Free a dict item. Also clears the value.
6332 */
6333 static void
6334dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006335 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006336{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006337 clear_tv(&item->di_tv);
6338 vim_free(item);
6339}
6340
6341/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006342 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6343 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006344 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006345 * Returns NULL when out of memory.
6346 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006347 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006348dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006350 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006351 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006352{
Bram Moolenaar33570922005-01-25 22:26:29 +00006353 dict_T *copy;
6354 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006355 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006356 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006357
6358 if (orig == NULL)
6359 return NULL;
6360
6361 copy = dict_alloc();
6362 if (copy != NULL)
6363 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006364 if (copyID != 0)
6365 {
6366 orig->dv_copyID = copyID;
6367 orig->dv_copydict = copy;
6368 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006369 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006370 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006371 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006372 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006373 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006374 --todo;
6375
6376 di = dictitem_alloc(hi->hi_key);
6377 if (di == NULL)
6378 break;
6379 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006380 {
6381 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6382 copyID) == FAIL)
6383 {
6384 vim_free(di);
6385 break;
6386 }
6387 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006388 else
6389 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6390 if (dict_add(copy, di) == FAIL)
6391 {
6392 dictitem_free(di);
6393 break;
6394 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006395 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006396 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006397
Bram Moolenaare9a41262005-01-15 22:18:47 +00006398 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006399 if (todo > 0)
6400 {
6401 dict_unref(copy);
6402 copy = NULL;
6403 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006404 }
6405
6406 return copy;
6407}
6408
6409/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006410 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006411 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006412 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006413 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006414dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 dict_T *d;
6416 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006417{
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006419}
6420
Bram Moolenaar8c711452005-01-14 21:53:12 +00006421/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006422 * Add a number or string entry to dictionary "d".
6423 * When "str" is NULL use number "nr", otherwise use "str".
6424 * Returns FAIL when out of memory and when key already exists.
6425 */
6426 int
6427dict_add_nr_str(d, key, nr, str)
6428 dict_T *d;
6429 char *key;
6430 long nr;
6431 char_u *str;
6432{
6433 dictitem_T *item;
6434
6435 item = dictitem_alloc((char_u *)key);
6436 if (item == NULL)
6437 return FAIL;
6438 item->di_tv.v_lock = 0;
6439 if (str == NULL)
6440 {
6441 item->di_tv.v_type = VAR_NUMBER;
6442 item->di_tv.vval.v_number = nr;
6443 }
6444 else
6445 {
6446 item->di_tv.v_type = VAR_STRING;
6447 item->di_tv.vval.v_string = vim_strsave(str);
6448 }
6449 if (dict_add(d, item) == FAIL)
6450 {
6451 dictitem_free(item);
6452 return FAIL;
6453 }
6454 return OK;
6455}
6456
6457/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006458 * Get the number of items in a Dictionary.
6459 */
6460 static long
6461dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006462 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006463{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006464 if (d == NULL)
6465 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006466 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006467}
6468
6469/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006470 * Find item "key[len]" in Dictionary "d".
6471 * If "len" is negative use strlen(key).
6472 * Returns NULL when not found.
6473 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006474 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006475dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006476 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006477 char_u *key;
6478 int len;
6479{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006480#define AKEYLEN 200
6481 char_u buf[AKEYLEN];
6482 char_u *akey;
6483 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006484 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006485
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006486 if (len < 0)
6487 akey = key;
6488 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006489 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006490 tofree = akey = vim_strnsave(key, len);
6491 if (akey == NULL)
6492 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006493 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006494 else
6495 {
6496 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006497 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006498 akey = buf;
6499 }
6500
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006502 vim_free(tofree);
6503 if (HASHITEM_EMPTY(hi))
6504 return NULL;
6505 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506}
6507
6508/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006509 * Get a string item from a dictionary.
6510 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006511 * Returns NULL if the entry doesn't exist or out of memory.
6512 */
6513 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006514get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006515 dict_T *d;
6516 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006517 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006518{
6519 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006520 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006521
6522 di = dict_find(d, key, -1);
6523 if (di == NULL)
6524 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006525 s = get_tv_string(&di->di_tv);
6526 if (save && s != NULL)
6527 s = vim_strsave(s);
6528 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006529}
6530
6531/*
6532 * Get a number item from a dictionary.
6533 * Returns 0 if the entry doesn't exist or out of memory.
6534 */
6535 long
6536get_dict_number(d, key)
6537 dict_T *d;
6538 char_u *key;
6539{
6540 dictitem_T *di;
6541
6542 di = dict_find(d, key, -1);
6543 if (di == NULL)
6544 return 0;
6545 return get_tv_number(&di->di_tv);
6546}
6547
6548/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006549 * Return an allocated string with the string representation of a Dictionary.
6550 * May return NULL.
6551 */
6552 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006553dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006554 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006555 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006556{
6557 garray_T ga;
6558 int first = TRUE;
6559 char_u *tofree;
6560 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006562 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006564 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006565
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006566 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006567 return NULL;
6568 ga_init2(&ga, (int)sizeof(char), 80);
6569 ga_append(&ga, '{');
6570
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006571 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006572 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006573 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006574 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006575 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006576 --todo;
6577
6578 if (first)
6579 first = FALSE;
6580 else
6581 ga_concat(&ga, (char_u *)", ");
6582
6583 tofree = string_quote(hi->hi_key, FALSE);
6584 if (tofree != NULL)
6585 {
6586 ga_concat(&ga, tofree);
6587 vim_free(tofree);
6588 }
6589 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006590 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006591 if (s != NULL)
6592 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006593 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006594 if (s == NULL)
6595 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006596 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006597 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006598 if (todo > 0)
6599 {
6600 vim_free(ga.ga_data);
6601 return NULL;
6602 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006603
6604 ga_append(&ga, '}');
6605 ga_append(&ga, NUL);
6606 return (char_u *)ga.ga_data;
6607}
6608
6609/*
6610 * Allocate a variable for a Dictionary and fill it from "*arg".
6611 * Return OK or FAIL. Returns NOTDONE for {expr}.
6612 */
6613 static int
6614get_dict_tv(arg, rettv, evaluate)
6615 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006617 int evaluate;
6618{
Bram Moolenaar33570922005-01-25 22:26:29 +00006619 dict_T *d = NULL;
6620 typval_T tvkey;
6621 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006622 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006623 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006624 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006625 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006626
6627 /*
6628 * First check if it's not a curly-braces thing: {expr}.
6629 * Must do this without evaluating, otherwise a function may be called
6630 * twice. Unfortunately this means we need to call eval1() twice for the
6631 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006632 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006633 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006634 if (*start != '}')
6635 {
6636 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6637 return FAIL;
6638 if (*start == '}')
6639 return NOTDONE;
6640 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006641
6642 if (evaluate)
6643 {
6644 d = dict_alloc();
6645 if (d == NULL)
6646 return FAIL;
6647 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006648 tvkey.v_type = VAR_UNKNOWN;
6649 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006650
6651 *arg = skipwhite(*arg + 1);
6652 while (**arg != '}' && **arg != NUL)
6653 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006654 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006655 goto failret;
6656 if (**arg != ':')
6657 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006658 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006659 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006660 goto failret;
6661 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006662 key = get_tv_string_buf_chk(&tvkey, buf);
6663 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006665 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6666 if (key != NULL)
6667 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006668 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006669 goto failret;
6670 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006671
6672 *arg = skipwhite(*arg + 1);
6673 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6674 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006675 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006676 goto failret;
6677 }
6678 if (evaluate)
6679 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006680 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006681 if (item != NULL)
6682 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006683 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006684 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006685 clear_tv(&tv);
6686 goto failret;
6687 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006688 item = dictitem_alloc(key);
6689 clear_tv(&tvkey);
6690 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006691 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006692 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006693 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006694 if (dict_add(d, item) == FAIL)
6695 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006696 }
6697 }
6698
6699 if (**arg == '}')
6700 break;
6701 if (**arg != ',')
6702 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006703 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006704 goto failret;
6705 }
6706 *arg = skipwhite(*arg + 1);
6707 }
6708
6709 if (**arg != '}')
6710 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006711 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006712failret:
6713 if (evaluate)
6714 dict_free(d);
6715 return FAIL;
6716 }
6717
6718 *arg = skipwhite(*arg + 1);
6719 if (evaluate)
6720 {
6721 rettv->v_type = VAR_DICT;
6722 rettv->vval.v_dict = d;
6723 ++d->dv_refcount;
6724 }
6725
6726 return OK;
6727}
6728
Bram Moolenaar8c711452005-01-14 21:53:12 +00006729/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006730 * Return a string with the string representation of a variable.
6731 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006732 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006733 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006734 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006735 * May return NULL;
6736 */
6737 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006738echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006739 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006740 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006741 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006742 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006743{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006744 static int recurse = 0;
6745 char_u *r = NULL;
6746
Bram Moolenaar33570922005-01-25 22:26:29 +00006747 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006748 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006749 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006750 *tofree = NULL;
6751 return NULL;
6752 }
6753 ++recurse;
6754
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006755 switch (tv->v_type)
6756 {
6757 case VAR_FUNC:
6758 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006759 r = tv->vval.v_string;
6760 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006761
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006762 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006763 if (tv->vval.v_list == NULL)
6764 {
6765 *tofree = NULL;
6766 r = NULL;
6767 }
6768 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6769 {
6770 *tofree = NULL;
6771 r = (char_u *)"[...]";
6772 }
6773 else
6774 {
6775 tv->vval.v_list->lv_copyID = copyID;
6776 *tofree = list2string(tv, copyID);
6777 r = *tofree;
6778 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006779 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006780
Bram Moolenaar8c711452005-01-14 21:53:12 +00006781 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006782 if (tv->vval.v_dict == NULL)
6783 {
6784 *tofree = NULL;
6785 r = NULL;
6786 }
6787 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6788 {
6789 *tofree = NULL;
6790 r = (char_u *)"{...}";
6791 }
6792 else
6793 {
6794 tv->vval.v_dict->dv_copyID = copyID;
6795 *tofree = dict2string(tv, copyID);
6796 r = *tofree;
6797 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006798 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006799
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006800 case VAR_STRING:
6801 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006802 *tofree = NULL;
6803 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006804 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006805
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006806 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006807 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006808 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006809 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006810
6811 --recurse;
6812 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006813}
6814
6815/*
6816 * Return a string with the string representation of a variable.
6817 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6818 * "numbuf" is used for a number.
6819 * Puts quotes around strings, so that they can be parsed back by eval().
6820 * May return NULL;
6821 */
6822 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006823tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006824 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006825 char_u **tofree;
6826 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006827 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006828{
6829 switch (tv->v_type)
6830 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006831 case VAR_FUNC:
6832 *tofree = string_quote(tv->vval.v_string, TRUE);
6833 return *tofree;
6834 case VAR_STRING:
6835 *tofree = string_quote(tv->vval.v_string, FALSE);
6836 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006837 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006838 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006839 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006840 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006841 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006842 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006843 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006844 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006845}
6846
6847/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006848 * Return string "str" in ' quotes, doubling ' characters.
6849 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006850 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006851 */
6852 static char_u *
6853string_quote(str, function)
6854 char_u *str;
6855 int function;
6856{
Bram Moolenaar33570922005-01-25 22:26:29 +00006857 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006858 char_u *p, *r, *s;
6859
Bram Moolenaar33570922005-01-25 22:26:29 +00006860 len = (function ? 13 : 3);
6861 if (str != NULL)
6862 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006863 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006864 for (p = str; *p != NUL; mb_ptr_adv(p))
6865 if (*p == '\'')
6866 ++len;
6867 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006868 s = r = alloc(len);
6869 if (r != NULL)
6870 {
6871 if (function)
6872 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006873 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006874 r += 10;
6875 }
6876 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006877 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006878 if (str != NULL)
6879 for (p = str; *p != NUL; )
6880 {
6881 if (*p == '\'')
6882 *r++ = '\'';
6883 MB_COPY_CHAR(p, r);
6884 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006885 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006886 if (function)
6887 *r++ = ')';
6888 *r++ = NUL;
6889 }
6890 return s;
6891}
6892
6893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 * Get the value of an environment variable.
6895 * "arg" is pointing to the '$'. It is advanced to after the name.
6896 * If the environment variable was not set, silently assume it is empty.
6897 * Always return OK.
6898 */
6899 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006900get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 int evaluate;
6904{
6905 char_u *string = NULL;
6906 int len;
6907 int cc;
6908 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006909 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910
6911 ++*arg;
6912 name = *arg;
6913 len = get_env_len(arg);
6914 if (evaluate)
6915 {
6916 if (len != 0)
6917 {
6918 cc = name[len];
6919 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006920 /* first try vim_getenv(), fast for normal environment vars */
6921 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006923 {
6924 if (!mustfree)
6925 string = vim_strsave(string);
6926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 else
6928 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006929 if (mustfree)
6930 vim_free(string);
6931
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 /* next try expanding things like $VIM and ${HOME} */
6933 string = expand_env_save(name - 1);
6934 if (string != NULL && *string == '$')
6935 {
6936 vim_free(string);
6937 string = NULL;
6938 }
6939 }
6940 name[len] = cc;
6941 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006942 rettv->v_type = VAR_STRING;
6943 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 }
6945
6946 return OK;
6947}
6948
6949/*
6950 * Array with names and number of arguments of all internal functions
6951 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6952 */
6953static struct fst
6954{
6955 char *f_name; /* function name */
6956 char f_min_argc; /* minimal number of arguments */
6957 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006958 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006959 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960} functions[] =
6961{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006962 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 {"append", 2, 2, f_append},
6964 {"argc", 0, 0, f_argc},
6965 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00006966 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006968 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 {"bufexists", 1, 1, f_bufexists},
6970 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6971 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6972 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6973 {"buflisted", 1, 1, f_buflisted},
6974 {"bufloaded", 1, 1, f_bufloaded},
6975 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006976 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 {"bufwinnr", 1, 1, f_bufwinnr},
6978 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006979 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006980 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00006981 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 {"char2nr", 1, 1, f_char2nr},
6983 {"cindent", 1, 1, f_cindent},
6984 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006985#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00006986 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006987 {"complete_add", 1, 1, f_complete_add},
6988 {"complete_check", 0, 0, f_complete_check},
6989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006991 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006992 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00006994 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006995 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 {"delete", 1, 1, f_delete},
6997 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006998 {"diff_filler", 1, 1, f_diff_filler},
6999 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007000 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007002 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 {"eventhandler", 0, 0, f_eventhandler},
7004 {"executable", 1, 1, f_executable},
7005 {"exists", 1, 1, f_exists},
7006 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007007 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007008 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7010 {"filereadable", 1, 1, f_filereadable},
7011 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007012 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007013 {"finddir", 1, 3, f_finddir},
7014 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 {"fnamemodify", 2, 2, f_fnamemodify},
7016 {"foldclosed", 1, 1, f_foldclosed},
7017 {"foldclosedend", 1, 1, f_foldclosedend},
7018 {"foldlevel", 1, 1, f_foldlevel},
7019 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007020 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007022 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007024 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007025 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 {"getbufvar", 2, 2, f_getbufvar},
7027 {"getchar", 0, 1, f_getchar},
7028 {"getcharmod", 0, 0, f_getcharmod},
7029 {"getcmdline", 0, 0, f_getcmdline},
7030 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007031 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007033 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007034 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 {"getfsize", 1, 1, f_getfsize},
7036 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007037 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007038 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007039 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007040 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007041 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007042 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007044 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 {"getwinposx", 0, 0, f_getwinposx},
7046 {"getwinposy", 0, 0, f_getwinposy},
7047 {"getwinvar", 2, 2, f_getwinvar},
7048 {"glob", 1, 1, f_glob},
7049 {"globpath", 2, 2, f_globpath},
7050 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007051 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007052 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7054 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7055 {"histadd", 2, 2, f_histadd},
7056 {"histdel", 1, 2, f_histdel},
7057 {"histget", 1, 2, f_histget},
7058 {"histnr", 1, 1, f_histnr},
7059 {"hlID", 1, 1, f_hlID},
7060 {"hlexists", 1, 1, f_hlexists},
7061 {"hostname", 0, 0, f_hostname},
7062 {"iconv", 3, 3, f_iconv},
7063 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007064 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007065 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007067 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 {"inputrestore", 0, 0, f_inputrestore},
7069 {"inputsave", 0, 0, f_inputsave},
7070 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007071 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007073 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007074 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007075 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007078 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 {"libcall", 3, 3, f_libcall},
7080 {"libcallnr", 3, 3, f_libcallnr},
7081 {"line", 1, 1, f_line},
7082 {"line2byte", 1, 1, f_line2byte},
7083 {"lispindent", 1, 1, f_lispindent},
7084 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007085 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007086 {"maparg", 1, 3, f_maparg},
7087 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007088 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007089 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007090 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007091 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007092 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007093 {"max", 1, 1, f_max},
7094 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007095#ifdef vim_mkdir
7096 {"mkdir", 1, 3, f_mkdir},
7097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 {"mode", 0, 0, f_mode},
7099 {"nextnonblank", 1, 1, f_nextnonblank},
7100 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007101 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007103 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007104 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007105 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007106 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007107 {"reltime", 0, 2, f_reltime},
7108 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {"remote_expr", 2, 3, f_remote_expr},
7110 {"remote_foreground", 1, 1, f_remote_foreground},
7111 {"remote_peek", 1, 2, f_remote_peek},
7112 {"remote_read", 1, 1, f_remote_read},
7113 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007114 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007116 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007118 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007119 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007120 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007121 {"searchpair", 3, 6, f_searchpair},
7122 {"searchpairpos", 3, 6, f_searchpairpos},
7123 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 {"server2client", 2, 2, f_server2client},
7125 {"serverlist", 0, 0, f_serverlist},
7126 {"setbufvar", 3, 3, f_setbufvar},
7127 {"setcmdpos", 1, 1, f_setcmdpos},
7128 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007129 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007130 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007131 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007133 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {"setwinvar", 3, 3, f_setwinvar},
7135 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007136 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007137 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007138 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007139 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007140 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007141 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142#ifdef HAVE_STRFTIME
7143 {"strftime", 1, 2, f_strftime},
7144#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007146 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 {"strlen", 1, 1, f_strlen},
7148 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007149 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 {"strtrans", 1, 1, f_strtrans},
7151 {"submatch", 1, 1, f_submatch},
7152 {"substitute", 4, 4, f_substitute},
7153 {"synID", 3, 3, f_synID},
7154 {"synIDattr", 2, 3, f_synIDattr},
7155 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007156 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007157 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007158 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007159 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007160 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007161 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007163 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 {"tolower", 1, 1, f_tolower},
7165 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007166 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007168 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 {"virtcol", 1, 1, f_virtcol},
7170 {"visualmode", 0, 1, f_visualmode},
7171 {"winbufnr", 1, 1, f_winbufnr},
7172 {"wincol", 0, 0, f_wincol},
7173 {"winheight", 1, 1, f_winheight},
7174 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007175 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007177 {"winrestview", 1, 1, f_winrestview},
7178 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007180 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181};
7182
7183#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7184
7185/*
7186 * Function given to ExpandGeneric() to obtain the list of internal
7187 * or user defined function names.
7188 */
7189 char_u *
7190get_function_name(xp, idx)
7191 expand_T *xp;
7192 int idx;
7193{
7194 static int intidx = -1;
7195 char_u *name;
7196
7197 if (idx == 0)
7198 intidx = -1;
7199 if (intidx < 0)
7200 {
7201 name = get_user_func_name(xp, idx);
7202 if (name != NULL)
7203 return name;
7204 }
7205 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7206 {
7207 STRCPY(IObuff, functions[intidx].f_name);
7208 STRCAT(IObuff, "(");
7209 if (functions[intidx].f_max_argc == 0)
7210 STRCAT(IObuff, ")");
7211 return IObuff;
7212 }
7213
7214 return NULL;
7215}
7216
7217/*
7218 * Function given to ExpandGeneric() to obtain the list of internal or
7219 * user defined variable or function names.
7220 */
7221/*ARGSUSED*/
7222 char_u *
7223get_expr_name(xp, idx)
7224 expand_T *xp;
7225 int idx;
7226{
7227 static int intidx = -1;
7228 char_u *name;
7229
7230 if (idx == 0)
7231 intidx = -1;
7232 if (intidx < 0)
7233 {
7234 name = get_function_name(xp, idx);
7235 if (name != NULL)
7236 return name;
7237 }
7238 return get_user_var_name(xp, ++intidx);
7239}
7240
7241#endif /* FEAT_CMDL_COMPL */
7242
7243/*
7244 * Find internal function in table above.
7245 * Return index, or -1 if not found
7246 */
7247 static int
7248find_internal_func(name)
7249 char_u *name; /* name of the function */
7250{
7251 int first = 0;
7252 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7253 int cmp;
7254 int x;
7255
7256 /*
7257 * Find the function name in the table. Binary search.
7258 */
7259 while (first <= last)
7260 {
7261 x = first + ((unsigned)(last - first) >> 1);
7262 cmp = STRCMP(name, functions[x].f_name);
7263 if (cmp < 0)
7264 last = x - 1;
7265 else if (cmp > 0)
7266 first = x + 1;
7267 else
7268 return x;
7269 }
7270 return -1;
7271}
7272
7273/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007274 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7275 * name it contains, otherwise return "name".
7276 */
7277 static char_u *
7278deref_func_name(name, lenp)
7279 char_u *name;
7280 int *lenp;
7281{
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007283 int cc;
7284
7285 cc = name[*lenp];
7286 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007287 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007288 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007290 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007291 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007292 {
7293 *lenp = 0;
7294 return (char_u *)""; /* just in case */
7295 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007296 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007297 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007298 }
7299
7300 return name;
7301}
7302
7303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 * Allocate a variable for the result of a function.
7305 * Return OK or FAIL.
7306 */
7307 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007308get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7309 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 char_u *name; /* name of the function */
7311 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 char_u **arg; /* argument, pointing to the '(' */
7314 linenr_T firstline; /* first line of range */
7315 linenr_T lastline; /* last line of range */
7316 int *doesrange; /* return: function handled range */
7317 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007318 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319{
7320 char_u *argp;
7321 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007322 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 int argcount = 0; /* number of arguments found */
7324
7325 /*
7326 * Get the arguments.
7327 */
7328 argp = *arg;
7329 while (argcount < MAX_FUNC_ARGS)
7330 {
7331 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7332 if (*argp == ')' || *argp == ',' || *argp == NUL)
7333 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7335 {
7336 ret = FAIL;
7337 break;
7338 }
7339 ++argcount;
7340 if (*argp != ',')
7341 break;
7342 }
7343 if (*argp == ')')
7344 ++argp;
7345 else
7346 ret = FAIL;
7347
7348 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007349 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007350 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 {
7353 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007354 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007355 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007356 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358
7359 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007360 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361
7362 *arg = skipwhite(argp);
7363 return ret;
7364}
7365
7366
7367/*
7368 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007369 * Return OK when the function can't be called, FAIL otherwise.
7370 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 */
7372 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007373call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 char_u *name; /* name of the function */
7376 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007379 typval_T *argvars; /* vars for arguments, must have "argcount"
7380 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 linenr_T firstline; /* first line of range */
7382 linenr_T lastline; /* last line of range */
7383 int *doesrange; /* return: function handled range */
7384 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007385 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386{
7387 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388#define ERROR_UNKNOWN 0
7389#define ERROR_TOOMANY 1
7390#define ERROR_TOOFEW 2
7391#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392#define ERROR_DICT 4
7393#define ERROR_NONE 5
7394#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 int error = ERROR_NONE;
7396 int i;
7397 int llen;
7398 ufunc_T *fp;
7399 int cc;
7400#define FLEN_FIXED 40
7401 char_u fname_buf[FLEN_FIXED + 1];
7402 char_u *fname;
7403
7404 /*
7405 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7406 * Change <SNR>123_name() to K_SNR 123_name().
7407 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7408 */
7409 cc = name[len];
7410 name[len] = NUL;
7411 llen = eval_fname_script(name);
7412 if (llen > 0)
7413 {
7414 fname_buf[0] = K_SPECIAL;
7415 fname_buf[1] = KS_EXTRA;
7416 fname_buf[2] = (int)KE_SNR;
7417 i = 3;
7418 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7419 {
7420 if (current_SID <= 0)
7421 error = ERROR_SCRIPT;
7422 else
7423 {
7424 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7425 i = (int)STRLEN(fname_buf);
7426 }
7427 }
7428 if (i + STRLEN(name + llen) < FLEN_FIXED)
7429 {
7430 STRCPY(fname_buf + i, name + llen);
7431 fname = fname_buf;
7432 }
7433 else
7434 {
7435 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7436 if (fname == NULL)
7437 error = ERROR_OTHER;
7438 else
7439 {
7440 mch_memmove(fname, fname_buf, (size_t)i);
7441 STRCPY(fname + i, name + llen);
7442 }
7443 }
7444 }
7445 else
7446 fname = name;
7447
7448 *doesrange = FALSE;
7449
7450
7451 /* execute the function if no errors detected and executing */
7452 if (evaluate && error == ERROR_NONE)
7453 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007454 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 error = ERROR_UNKNOWN;
7456
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007457 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 {
7459 /*
7460 * User defined function.
7461 */
7462 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007463
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007465 /* Trigger FuncUndefined event, may load the function. */
7466 if (fp == NULL
7467 && apply_autocmds(EVENT_FUNCUNDEFINED,
7468 fname, fname, TRUE, NULL)
7469 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007471 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 fp = find_func(fname);
7473 }
7474#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007475 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007476 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007477 {
7478 /* loaded a package, search for the function again */
7479 fp = find_func(fname);
7480 }
7481
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 if (fp != NULL)
7483 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007484 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007486 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007488 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007490 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007491 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 else
7493 {
7494 /*
7495 * Call the user function.
7496 * Save and restore search patterns, script variables and
7497 * redo buffer.
7498 */
7499 save_search_patterns();
7500 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007501 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007502 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007503 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007504 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7505 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7506 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007507 /* Function was unreferenced while being used, free it
7508 * now. */
7509 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 restoreRedobuff();
7511 restore_search_patterns();
7512 error = ERROR_NONE;
7513 }
7514 }
7515 }
7516 else
7517 {
7518 /*
7519 * Find the function name in the table, call its implementation.
7520 */
7521 i = find_internal_func(fname);
7522 if (i >= 0)
7523 {
7524 if (argcount < functions[i].f_min_argc)
7525 error = ERROR_TOOFEW;
7526 else if (argcount > functions[i].f_max_argc)
7527 error = ERROR_TOOMANY;
7528 else
7529 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007530 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007531 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 error = ERROR_NONE;
7533 }
7534 }
7535 }
7536 /*
7537 * The function call (or "FuncUndefined" autocommand sequence) might
7538 * have been aborted by an error, an interrupt, or an explicitly thrown
7539 * exception that has not been caught so far. This situation can be
7540 * tested for by calling aborting(). For an error in an internal
7541 * function or for the "E132" error in call_user_func(), however, the
7542 * throw point at which the "force_abort" flag (temporarily reset by
7543 * emsg()) is normally updated has not been reached yet. We need to
7544 * update that flag first to make aborting() reliable.
7545 */
7546 update_force_abort();
7547 }
7548 if (error == ERROR_NONE)
7549 ret = OK;
7550
7551 /*
7552 * Report an error unless the argument evaluation or function call has been
7553 * cancelled due to an aborting error, an interrupt, or an exception.
7554 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007555 if (!aborting())
7556 {
7557 switch (error)
7558 {
7559 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007560 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561 break;
7562 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007563 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564 break;
7565 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007566 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567 name);
7568 break;
7569 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007570 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571 name);
7572 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007573 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007574 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007575 name);
7576 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577 }
7578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579
7580 name[len] = cc;
7581 if (fname != name && fname != fname_buf)
7582 vim_free(fname);
7583
7584 return ret;
7585}
7586
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007587/*
7588 * Give an error message with a function name. Handle <SNR> things.
7589 */
7590 static void
7591emsg_funcname(msg, name)
7592 char *msg;
7593 char_u *name;
7594{
7595 char_u *p;
7596
7597 if (*name == K_SPECIAL)
7598 p = concat_str((char_u *)"<SNR>", name + 3);
7599 else
7600 p = name;
7601 EMSG2(_(msg), p);
7602 if (p != name)
7603 vim_free(p);
7604}
7605
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606/*********************************************
7607 * Implementation of the built-in functions
7608 */
7609
7610/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007611 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 */
7613 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007614f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007615 typval_T *argvars;
7616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617{
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007620 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007621 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007623 if ((l = argvars[0].vval.v_list) != NULL
7624 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7625 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007626 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007627 }
7628 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007629 EMSG(_(e_listreq));
7630}
7631
7632/*
7633 * "append(lnum, string/list)" function
7634 */
7635 static void
7636f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007637 typval_T *argvars;
7638 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007639{
7640 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007641 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007642 list_T *l = NULL;
7643 listitem_T *li = NULL;
7644 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007645 long added = 0;
7646
Bram Moolenaar0d660222005-01-07 21:51:51 +00007647 lnum = get_tv_lnum(argvars);
7648 if (lnum >= 0
7649 && lnum <= curbuf->b_ml.ml_line_count
7650 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007651 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007652 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007653 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007654 l = argvars[1].vval.v_list;
7655 if (l == NULL)
7656 return;
7657 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007658 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007659 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007660 for (;;)
7661 {
7662 if (l == NULL)
7663 tv = &argvars[1]; /* append a string */
7664 else if (li == NULL)
7665 break; /* end of list */
7666 else
7667 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007668 line = get_tv_string_chk(tv);
7669 if (line == NULL) /* type error */
7670 {
7671 rettv->vval.v_number = 1; /* Failed */
7672 break;
7673 }
7674 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007675 ++added;
7676 if (l == NULL)
7677 break;
7678 li = li->li_next;
7679 }
7680
7681 appended_lines_mark(lnum, added);
7682 if (curwin->w_cursor.lnum > lnum)
7683 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007685 else
7686 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687}
7688
7689/*
7690 * "argc()" function
7691 */
7692/* ARGSUSED */
7693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007694f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007695 typval_T *argvars;
7696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007698 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699}
7700
7701/*
7702 * "argidx()" function
7703 */
7704/* ARGSUSED */
7705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007706f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007707 typval_T *argvars;
7708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007710 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711}
7712
7713/*
7714 * "argv(nr)" function
7715 */
7716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007717f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007718 typval_T *argvars;
7719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720{
7721 int idx;
7722
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007723 if (argvars[0].v_type != VAR_UNKNOWN)
7724 {
7725 idx = get_tv_number_chk(&argvars[0], NULL);
7726 if (idx >= 0 && idx < ARGCOUNT)
7727 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7728 else
7729 rettv->vval.v_string = NULL;
7730 rettv->v_type = VAR_STRING;
7731 }
7732 else if (rettv_list_alloc(rettv) == OK)
7733 for (idx = 0; idx < ARGCOUNT; ++idx)
7734 list_append_string(rettv->vval.v_list,
7735 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736}
7737
7738/*
7739 * "browse(save, title, initdir, default)" function
7740 */
7741/* ARGSUSED */
7742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007743f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007744 typval_T *argvars;
7745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746{
7747#ifdef FEAT_BROWSE
7748 int save;
7749 char_u *title;
7750 char_u *initdir;
7751 char_u *defname;
7752 char_u buf[NUMBUFLEN];
7753 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007754 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007756 save = get_tv_number_chk(&argvars[0], &error);
7757 title = get_tv_string_chk(&argvars[1]);
7758 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7759 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007761 if (error || title == NULL || initdir == NULL || defname == NULL)
7762 rettv->vval.v_string = NULL;
7763 else
7764 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007765 do_browse(save ? BROWSE_SAVE : 0,
7766 title, defname, NULL, initdir, NULL, curbuf);
7767#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007768 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007769#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007770 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007771}
7772
7773/*
7774 * "browsedir(title, initdir)" function
7775 */
7776/* ARGSUSED */
7777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007778f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007779 typval_T *argvars;
7780 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007781{
7782#ifdef FEAT_BROWSE
7783 char_u *title;
7784 char_u *initdir;
7785 char_u buf[NUMBUFLEN];
7786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007787 title = get_tv_string_chk(&argvars[0]);
7788 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007790 if (title == NULL || initdir == NULL)
7791 rettv->vval.v_string = NULL;
7792 else
7793 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007794 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007796 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007798 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799}
7800
Bram Moolenaar33570922005-01-25 22:26:29 +00007801static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007802
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803/*
7804 * Find a buffer by number or exact name.
7805 */
7806 static buf_T *
7807find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007808 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809{
7810 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007812 if (avar->v_type == VAR_NUMBER)
7813 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007814 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007816 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007817 if (buf == NULL)
7818 {
7819 /* No full path name match, try a match with a URL or a "nofile"
7820 * buffer, these don't use the full path. */
7821 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7822 if (buf->b_fname != NULL
7823 && (path_with_url(buf->b_fname)
7824#ifdef FEAT_QUICKFIX
7825 || bt_nofile(buf)
7826#endif
7827 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007828 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007829 break;
7830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 }
7832 return buf;
7833}
7834
7835/*
7836 * "bufexists(expr)" function
7837 */
7838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007839f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007840 typval_T *argvars;
7841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007843 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844}
7845
7846/*
7847 * "buflisted(expr)" function
7848 */
7849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007850f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007851 typval_T *argvars;
7852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853{
7854 buf_T *buf;
7855
7856 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007857 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858}
7859
7860/*
7861 * "bufloaded(expr)" function
7862 */
7863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007864f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007865 typval_T *argvars;
7866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867{
7868 buf_T *buf;
7869
7870 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872}
7873
Bram Moolenaar33570922005-01-25 22:26:29 +00007874static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007875
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876/*
7877 * Get buffer by number or pattern.
7878 */
7879 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007880get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007881 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007883 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 int save_magic;
7885 char_u *save_cpo;
7886 buf_T *buf;
7887
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007888 if (tv->v_type == VAR_NUMBER)
7889 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007890 if (tv->v_type != VAR_STRING)
7891 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 if (name == NULL || *name == NUL)
7893 return curbuf;
7894 if (name[0] == '$' && name[1] == NUL)
7895 return lastbuf;
7896
7897 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7898 save_magic = p_magic;
7899 p_magic = TRUE;
7900 save_cpo = p_cpo;
7901 p_cpo = (char_u *)"";
7902
7903 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7904 TRUE, FALSE));
7905
7906 p_magic = save_magic;
7907 p_cpo = save_cpo;
7908
7909 /* If not found, try expanding the name, like done for bufexists(). */
7910 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007911 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912
7913 return buf;
7914}
7915
7916/*
7917 * "bufname(expr)" function
7918 */
7919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007920f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007921 typval_T *argvars;
7922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923{
7924 buf_T *buf;
7925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007926 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007928 buf = get_buf_tv(&argvars[0]);
7929 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007931 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007933 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 --emsg_off;
7935}
7936
7937/*
7938 * "bufnr(expr)" function
7939 */
7940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007941f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007942 typval_T *argvars;
7943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944{
7945 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007946 int error = FALSE;
7947 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007949 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007951 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007952 --emsg_off;
7953
7954 /* If the buffer isn't found and the second argument is not zero create a
7955 * new buffer. */
7956 if (buf == NULL
7957 && argvars[1].v_type != VAR_UNKNOWN
7958 && get_tv_number_chk(&argvars[1], &error) != 0
7959 && !error
7960 && (name = get_tv_string_chk(&argvars[0])) != NULL
7961 && !error)
7962 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7963
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007967 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968}
7969
7970/*
7971 * "bufwinnr(nr)" function
7972 */
7973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007974f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007975 typval_T *argvars;
7976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977{
7978#ifdef FEAT_WINDOWS
7979 win_T *wp;
7980 int winnr = 0;
7981#endif
7982 buf_T *buf;
7983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007984 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007986 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987#ifdef FEAT_WINDOWS
7988 for (wp = firstwin; wp; wp = wp->w_next)
7989 {
7990 ++winnr;
7991 if (wp->w_buffer == buf)
7992 break;
7993 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007994 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007996 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997#endif
7998 --emsg_off;
7999}
8000
8001/*
8002 * "byte2line(byte)" function
8003 */
8004/*ARGSUSED*/
8005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008006f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008007 typval_T *argvars;
8008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009{
8010#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008011 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012#else
8013 long boff = 0;
8014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008015 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008017 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008019 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 (linenr_T)0, &boff);
8021#endif
8022}
8023
8024/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008025 * "byteidx()" function
8026 */
8027/*ARGSUSED*/
8028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008029f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008030 typval_T *argvars;
8031 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008032{
8033#ifdef FEAT_MBYTE
8034 char_u *t;
8035#endif
8036 char_u *str;
8037 long idx;
8038
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008039 str = get_tv_string_chk(&argvars[0]);
8040 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008041 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008042 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008043 return;
8044
8045#ifdef FEAT_MBYTE
8046 t = str;
8047 for ( ; idx > 0; idx--)
8048 {
8049 if (*t == NUL) /* EOL reached */
8050 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008051 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008052 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008053 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008054#else
8055 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008056 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008057#endif
8058}
8059
8060/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008061 * "call(func, arglist)" function
8062 */
8063 static void
8064f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008065 typval_T *argvars;
8066 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008067{
8068 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008069 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008070 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008071 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008072 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008073 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008074
8075 rettv->vval.v_number = 0;
8076 if (argvars[1].v_type != VAR_LIST)
8077 {
8078 EMSG(_(e_listreq));
8079 return;
8080 }
8081 if (argvars[1].vval.v_list == NULL)
8082 return;
8083
8084 if (argvars[0].v_type == VAR_FUNC)
8085 func = argvars[0].vval.v_string;
8086 else
8087 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008088 if (*func == NUL)
8089 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008090
Bram Moolenaare9a41262005-01-15 22:18:47 +00008091 if (argvars[2].v_type != VAR_UNKNOWN)
8092 {
8093 if (argvars[2].v_type != VAR_DICT)
8094 {
8095 EMSG(_(e_dictreq));
8096 return;
8097 }
8098 selfdict = argvars[2].vval.v_dict;
8099 }
8100
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008101 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8102 item = item->li_next)
8103 {
8104 if (argc == MAX_FUNC_ARGS)
8105 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008106 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008107 break;
8108 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008109 /* Make a copy of each argument. This is needed to be able to set
8110 * v_lock to VAR_FIXED in the copy without changing the original list.
8111 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008112 copy_tv(&item->li_tv, &argv[argc++]);
8113 }
8114
8115 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008116 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008117 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8118 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008119
8120 /* Free the arguments. */
8121 while (argc > 0)
8122 clear_tv(&argv[--argc]);
8123}
8124
8125/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008126 * "changenr()" function
8127 */
8128/*ARGSUSED*/
8129 static void
8130f_changenr(argvars, rettv)
8131 typval_T *argvars;
8132 typval_T *rettv;
8133{
8134 rettv->vval.v_number = curbuf->b_u_seq_cur;
8135}
8136
8137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 * "char2nr(string)" function
8139 */
8140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008141f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008142 typval_T *argvars;
8143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144{
8145#ifdef FEAT_MBYTE
8146 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008147 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 else
8149#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008150 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151}
8152
8153/*
8154 * "cindent(lnum)" function
8155 */
8156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008157f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008158 typval_T *argvars;
8159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160{
8161#ifdef FEAT_CINDENT
8162 pos_T pos;
8163 linenr_T lnum;
8164
8165 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008166 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8168 {
8169 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008170 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 curwin->w_cursor = pos;
8172 }
8173 else
8174#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008175 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176}
8177
8178/*
8179 * "col(string)" function
8180 */
8181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008182f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008183 typval_T *argvars;
8184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185{
8186 colnr_T col = 0;
8187 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008188 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008190 fp = var2fpos(&argvars[0], FALSE, &fnum);
8191 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {
8193 if (fp->col == MAXCOL)
8194 {
8195 /* '> can be MAXCOL, get the length of the line then */
8196 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008197 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 else
8199 col = MAXCOL;
8200 }
8201 else
8202 {
8203 col = fp->col + 1;
8204#ifdef FEAT_VIRTUALEDIT
8205 /* col(".") when the cursor is on the NUL at the end of the line
8206 * because of "coladd" can be seen as an extra column. */
8207 if (virtual_active() && fp == &curwin->w_cursor)
8208 {
8209 char_u *p = ml_get_cursor();
8210
8211 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8212 curwin->w_virtcol - curwin->w_cursor.coladd))
8213 {
8214# ifdef FEAT_MBYTE
8215 int l;
8216
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008217 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 col += l;
8219# else
8220 if (*p != NUL && p[1] == NUL)
8221 ++col;
8222# endif
8223 }
8224 }
8225#endif
8226 }
8227 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008228 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229}
8230
Bram Moolenaar572cb562005-08-05 21:35:02 +00008231#if defined(FEAT_INS_EXPAND)
8232/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008233 * "complete()" function
8234 */
8235/*ARGSUSED*/
8236 static void
8237f_complete(argvars, rettv)
8238 typval_T *argvars;
8239 typval_T *rettv;
8240{
8241 int startcol;
8242
8243 if ((State & INSERT) == 0)
8244 {
8245 EMSG(_("E785: complete() can only be used in Insert mode"));
8246 return;
8247 }
8248 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8249 {
8250 EMSG(_(e_invarg));
8251 return;
8252 }
8253
8254 startcol = get_tv_number_chk(&argvars[0], NULL);
8255 if (startcol <= 0)
8256 return;
8257
8258 set_completion(startcol - 1, argvars[1].vval.v_list);
8259}
8260
8261/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008262 * "complete_add()" function
8263 */
8264/*ARGSUSED*/
8265 static void
8266f_complete_add(argvars, rettv)
8267 typval_T *argvars;
8268 typval_T *rettv;
8269{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008270 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008271}
8272
8273/*
8274 * "complete_check()" function
8275 */
8276/*ARGSUSED*/
8277 static void
8278f_complete_check(argvars, rettv)
8279 typval_T *argvars;
8280 typval_T *rettv;
8281{
8282 int saved = RedrawingDisabled;
8283
8284 RedrawingDisabled = 0;
8285 ins_compl_check_keys(0);
8286 rettv->vval.v_number = compl_interrupted;
8287 RedrawingDisabled = saved;
8288}
8289#endif
8290
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291/*
8292 * "confirm(message, buttons[, default [, type]])" function
8293 */
8294/*ARGSUSED*/
8295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008296f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008297 typval_T *argvars;
8298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299{
8300#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8301 char_u *message;
8302 char_u *buttons = NULL;
8303 char_u buf[NUMBUFLEN];
8304 char_u buf2[NUMBUFLEN];
8305 int def = 1;
8306 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008307 char_u *typestr;
8308 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008310 message = get_tv_string_chk(&argvars[0]);
8311 if (message == NULL)
8312 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008313 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008315 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8316 if (buttons == NULL)
8317 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008318 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008320 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008321 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008323 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8324 if (typestr == NULL)
8325 error = TRUE;
8326 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008328 switch (TOUPPER_ASC(*typestr))
8329 {
8330 case 'E': type = VIM_ERROR; break;
8331 case 'Q': type = VIM_QUESTION; break;
8332 case 'I': type = VIM_INFO; break;
8333 case 'W': type = VIM_WARNING; break;
8334 case 'G': type = VIM_GENERIC; break;
8335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 }
8337 }
8338 }
8339 }
8340
8341 if (buttons == NULL || *buttons == NUL)
8342 buttons = (char_u *)_("&Ok");
8343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008344 if (error)
8345 rettv->vval.v_number = 0;
8346 else
8347 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 def, NULL);
8349#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008350 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351#endif
8352}
8353
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008354/*
8355 * "copy()" function
8356 */
8357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008358f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008359 typval_T *argvars;
8360 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008361{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008362 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008363}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364
8365/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008366 * "count()" function
8367 */
8368 static void
8369f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008370 typval_T *argvars;
8371 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008372{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008373 long n = 0;
8374 int ic = FALSE;
8375
Bram Moolenaare9a41262005-01-15 22:18:47 +00008376 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008377 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008378 listitem_T *li;
8379 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008380 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008381
Bram Moolenaare9a41262005-01-15 22:18:47 +00008382 if ((l = argvars[0].vval.v_list) != NULL)
8383 {
8384 li = l->lv_first;
8385 if (argvars[2].v_type != VAR_UNKNOWN)
8386 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008387 int error = FALSE;
8388
8389 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008390 if (argvars[3].v_type != VAR_UNKNOWN)
8391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008392 idx = get_tv_number_chk(&argvars[3], &error);
8393 if (!error)
8394 {
8395 li = list_find(l, idx);
8396 if (li == NULL)
8397 EMSGN(_(e_listidx), idx);
8398 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008399 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008400 if (error)
8401 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008402 }
8403
8404 for ( ; li != NULL; li = li->li_next)
8405 if (tv_equal(&li->li_tv, &argvars[1], ic))
8406 ++n;
8407 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008408 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008409 else if (argvars[0].v_type == VAR_DICT)
8410 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008411 int todo;
8412 dict_T *d;
8413 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008414
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008415 if ((d = argvars[0].vval.v_dict) != NULL)
8416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008417 int error = FALSE;
8418
Bram Moolenaare9a41262005-01-15 22:18:47 +00008419 if (argvars[2].v_type != VAR_UNKNOWN)
8420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008421 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008422 if (argvars[3].v_type != VAR_UNKNOWN)
8423 EMSG(_(e_invarg));
8424 }
8425
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008426 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008427 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008428 {
8429 if (!HASHITEM_EMPTY(hi))
8430 {
8431 --todo;
8432 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8433 ++n;
8434 }
8435 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008436 }
8437 }
8438 else
8439 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008440 rettv->vval.v_number = n;
8441}
8442
8443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8445 *
8446 * Checks the existence of a cscope connection.
8447 */
8448/*ARGSUSED*/
8449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008450f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008451 typval_T *argvars;
8452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453{
8454#ifdef FEAT_CSCOPE
8455 int num = 0;
8456 char_u *dbpath = NULL;
8457 char_u *prepend = NULL;
8458 char_u buf[NUMBUFLEN];
8459
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008460 if (argvars[0].v_type != VAR_UNKNOWN
8461 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008463 num = (int)get_tv_number(&argvars[0]);
8464 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008465 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008466 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 }
8468
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008469 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008471 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472#endif
8473}
8474
8475/*
8476 * "cursor(lnum, col)" function
8477 *
8478 * Moves the cursor to the specified line and column
8479 */
8480/*ARGSUSED*/
8481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008482f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008483 typval_T *argvars;
8484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485{
8486 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008487#ifdef FEAT_VIRTUALEDIT
8488 long coladd = 0;
8489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490
Bram Moolenaara5525202006-03-02 22:52:09 +00008491 if (argvars[1].v_type == VAR_UNKNOWN)
8492 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008493 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008494
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008495 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008496 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008497 line = pos.lnum;
8498 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008499#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008500 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008501#endif
8502 }
8503 else
8504 {
8505 line = get_tv_lnum(argvars);
8506 col = get_tv_number_chk(&argvars[1], NULL);
8507#ifdef FEAT_VIRTUALEDIT
8508 if (argvars[2].v_type != VAR_UNKNOWN)
8509 coladd = get_tv_number_chk(&argvars[2], NULL);
8510#endif
8511 }
8512 if (line < 0 || col < 0
8513#ifdef FEAT_VIRTUALEDIT
8514 || coladd < 0
8515#endif
8516 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008517 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 if (line > 0)
8519 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 if (col > 0)
8521 curwin->w_cursor.col = col - 1;
8522#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008523 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524#endif
8525
8526 /* Make sure the cursor is in a valid position. */
8527 check_cursor();
8528#ifdef FEAT_MBYTE
8529 /* Correct cursor for multi-byte character. */
8530 if (has_mbyte)
8531 mb_adjust_cursor();
8532#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008533
8534 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535}
8536
8537/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008538 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 */
8540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008541f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 typval_T *argvars;
8543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008545 int noref = 0;
8546
8547 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008548 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008549 if (noref < 0 || noref > 1)
8550 EMSG(_(e_invarg));
8551 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008552 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553}
8554
8555/*
8556 * "delete()" function
8557 */
8558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008559f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008560 typval_T *argvars;
8561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562{
8563 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008564 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008566 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567}
8568
8569/*
8570 * "did_filetype()" function
8571 */
8572/*ARGSUSED*/
8573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008574f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008575 typval_T *argvars;
8576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577{
8578#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008581 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582#endif
8583}
8584
8585/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008586 * "diff_filler()" function
8587 */
8588/*ARGSUSED*/
8589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008591 typval_T *argvars;
8592 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008593{
8594#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008595 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008596#endif
8597}
8598
8599/*
8600 * "diff_hlID()" function
8601 */
8602/*ARGSUSED*/
8603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008604f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008605 typval_T *argvars;
8606 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008607{
8608#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008609 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008610 static linenr_T prev_lnum = 0;
8611 static int changedtick = 0;
8612 static int fnum = 0;
8613 static int change_start = 0;
8614 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008615 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008616 int filler_lines;
8617 int col;
8618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008619 if (lnum < 0) /* ignore type error in {lnum} arg */
8620 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008621 if (lnum != prev_lnum
8622 || changedtick != curbuf->b_changedtick
8623 || fnum != curbuf->b_fnum)
8624 {
8625 /* New line, buffer, change: need to get the values. */
8626 filler_lines = diff_check(curwin, lnum);
8627 if (filler_lines < 0)
8628 {
8629 if (filler_lines == -1)
8630 {
8631 change_start = MAXCOL;
8632 change_end = -1;
8633 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8634 hlID = HLF_ADD; /* added line */
8635 else
8636 hlID = HLF_CHD; /* changed line */
8637 }
8638 else
8639 hlID = HLF_ADD; /* added line */
8640 }
8641 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008642 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008643 prev_lnum = lnum;
8644 changedtick = curbuf->b_changedtick;
8645 fnum = curbuf->b_fnum;
8646 }
8647
8648 if (hlID == HLF_CHD || hlID == HLF_TXD)
8649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008650 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008651 if (col >= change_start && col <= change_end)
8652 hlID = HLF_TXD; /* changed text */
8653 else
8654 hlID = HLF_CHD; /* changed line */
8655 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008656 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008657#endif
8658}
8659
8660/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008661 * "empty({expr})" function
8662 */
8663 static void
8664f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008665 typval_T *argvars;
8666 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008667{
8668 int n;
8669
8670 switch (argvars[0].v_type)
8671 {
8672 case VAR_STRING:
8673 case VAR_FUNC:
8674 n = argvars[0].vval.v_string == NULL
8675 || *argvars[0].vval.v_string == NUL;
8676 break;
8677 case VAR_NUMBER:
8678 n = argvars[0].vval.v_number == 0;
8679 break;
8680 case VAR_LIST:
8681 n = argvars[0].vval.v_list == NULL
8682 || argvars[0].vval.v_list->lv_first == NULL;
8683 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008684 case VAR_DICT:
8685 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008686 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008687 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008688 default:
8689 EMSG2(_(e_intern2), "f_empty()");
8690 n = 0;
8691 }
8692
8693 rettv->vval.v_number = n;
8694}
8695
8696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 * "escape({string}, {chars})" function
8698 */
8699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008700f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008701 typval_T *argvars;
8702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703{
8704 char_u buf[NUMBUFLEN];
8705
Bram Moolenaar758711c2005-02-02 23:11:38 +00008706 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8707 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008708 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709}
8710
8711/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008712 * "eval()" function
8713 */
8714/*ARGSUSED*/
8715 static void
8716f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008717 typval_T *argvars;
8718 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008719{
8720 char_u *s;
8721
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008722 s = get_tv_string_chk(&argvars[0]);
8723 if (s != NULL)
8724 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008725
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008726 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8727 {
8728 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008729 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008730 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008731 else if (*s != NUL)
8732 EMSG(_(e_trailing));
8733}
8734
8735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 * "eventhandler()" function
8737 */
8738/*ARGSUSED*/
8739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008740f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008741 typval_T *argvars;
8742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008744 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745}
8746
8747/*
8748 * "executable()" function
8749 */
8750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008752 typval_T *argvars;
8753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008755 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756}
8757
8758/*
8759 * "exists()" function
8760 */
8761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008762f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008763 typval_T *argvars;
8764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765{
8766 char_u *p;
8767 char_u *name;
8768 int n = FALSE;
8769 int len = 0;
8770
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008771 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 if (*p == '$') /* environment variable */
8773 {
8774 /* first try "normal" environment variables (fast) */
8775 if (mch_getenv(p + 1) != NULL)
8776 n = TRUE;
8777 else
8778 {
8779 /* try expanding things like $VIM and ${HOME} */
8780 p = expand_env_save(p);
8781 if (p != NULL && *p != '$')
8782 n = TRUE;
8783 vim_free(p);
8784 }
8785 }
8786 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008787 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 else if (*p == '*') /* internal or user defined function */
8789 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008790 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 }
8792 else if (*p == ':')
8793 {
8794 n = cmd_exists(p + 1);
8795 }
8796 else if (*p == '#')
8797 {
8798#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008799 if (p[1] == '#')
8800 n = autocmd_supported(p + 2);
8801 else
8802 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803#endif
8804 }
8805 else /* internal variable */
8806 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008807 char_u *tofree;
8808 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008810 /* get_name_len() takes care of expanding curly braces */
8811 name = p;
8812 len = get_name_len(&p, &tofree, TRUE, FALSE);
8813 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008815 if (tofree != NULL)
8816 name = tofree;
8817 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8818 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008820 /* handle d.key, l[idx], f(expr) */
8821 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8822 if (n)
8823 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 }
8825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008827 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 }
8829
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008830 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831}
8832
8833/*
8834 * "expand()" function
8835 */
8836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008837f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008838 typval_T *argvars;
8839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840{
8841 char_u *s;
8842 int len;
8843 char_u *errormsg;
8844 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8845 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008846 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848 rettv->v_type = VAR_STRING;
8849 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 if (*s == '%' || *s == '#' || *s == '<')
8851 {
8852 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008853 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 --emsg_off;
8855 }
8856 else
8857 {
8858 /* When the optional second argument is non-zero, don't remove matches
8859 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008860 if (argvars[1].v_type != VAR_UNKNOWN
8861 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008863 if (!error)
8864 {
8865 ExpandInit(&xpc);
8866 xpc.xp_context = EXPAND_FILES;
8867 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008868 }
8869 else
8870 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 }
8872}
8873
8874/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008875 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008876 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008877 */
8878 static void
8879f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008880 typval_T *argvars;
8881 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008882{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008883 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008884 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008885 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008886 list_T *l1, *l2;
8887 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008888 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008889 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008890
Bram Moolenaare9a41262005-01-15 22:18:47 +00008891 l1 = argvars[0].vval.v_list;
8892 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008893 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8894 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008895 {
8896 if (argvars[2].v_type != VAR_UNKNOWN)
8897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008898 before = get_tv_number_chk(&argvars[2], &error);
8899 if (error)
8900 return; /* type error; errmsg already given */
8901
Bram Moolenaar758711c2005-02-02 23:11:38 +00008902 if (before == l1->lv_len)
8903 item = NULL;
8904 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008905 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008906 item = list_find(l1, before);
8907 if (item == NULL)
8908 {
8909 EMSGN(_(e_listidx), before);
8910 return;
8911 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008912 }
8913 }
8914 else
8915 item = NULL;
8916 list_extend(l1, l2, item);
8917
Bram Moolenaare9a41262005-01-15 22:18:47 +00008918 copy_tv(&argvars[0], rettv);
8919 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008920 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008921 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8922 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008923 dict_T *d1, *d2;
8924 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008925 char_u *action;
8926 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008927 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008928 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008929
8930 d1 = argvars[0].vval.v_dict;
8931 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008932 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8933 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008934 {
8935 /* Check the third argument. */
8936 if (argvars[2].v_type != VAR_UNKNOWN)
8937 {
8938 static char *(av[]) = {"keep", "force", "error"};
8939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008940 action = get_tv_string_chk(&argvars[2]);
8941 if (action == NULL)
8942 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008943 for (i = 0; i < 3; ++i)
8944 if (STRCMP(action, av[i]) == 0)
8945 break;
8946 if (i == 3)
8947 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008948 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008949 return;
8950 }
8951 }
8952 else
8953 action = (char_u *)"force";
8954
8955 /* Go over all entries in the second dict and add them to the
8956 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008957 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008958 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008959 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008960 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008961 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008962 --todo;
8963 di1 = dict_find(d1, hi2->hi_key, -1);
8964 if (di1 == NULL)
8965 {
8966 di1 = dictitem_copy(HI2DI(hi2));
8967 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8968 dictitem_free(di1);
8969 }
8970 else if (*action == 'e')
8971 {
8972 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8973 break;
8974 }
8975 else if (*action == 'f')
8976 {
8977 clear_tv(&di1->di_tv);
8978 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8979 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008980 }
8981 }
8982
Bram Moolenaare9a41262005-01-15 22:18:47 +00008983 copy_tv(&argvars[0], rettv);
8984 }
8985 }
8986 else
8987 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008988}
8989
8990/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008991 * "feedkeys()" function
8992 */
8993/*ARGSUSED*/
8994 static void
8995f_feedkeys(argvars, rettv)
8996 typval_T *argvars;
8997 typval_T *rettv;
8998{
8999 int remap = TRUE;
9000 char_u *keys, *flags;
9001 char_u nbuf[NUMBUFLEN];
9002 int typed = FALSE;
9003
9004 rettv->vval.v_number = 0;
9005 keys = get_tv_string(&argvars[0]);
9006 if (*keys != NUL)
9007 {
9008 if (argvars[1].v_type != VAR_UNKNOWN)
9009 {
9010 flags = get_tv_string_buf(&argvars[1], nbuf);
9011 for ( ; *flags != NUL; ++flags)
9012 {
9013 switch (*flags)
9014 {
9015 case 'n': remap = FALSE; break;
9016 case 'm': remap = TRUE; break;
9017 case 't': typed = TRUE; break;
9018 }
9019 }
9020 }
9021
9022 ins_typebuf(keys, (remap ? REMAP_YES : REMAP_NONE),
9023 typebuf.tb_len, !typed, FALSE);
9024 typebuf_was_filled = TRUE;
9025 }
9026}
9027
9028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 * "filereadable()" function
9030 */
9031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009032f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009033 typval_T *argvars;
9034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035{
9036 FILE *fd;
9037 char_u *p;
9038 int n;
9039
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009040 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9042 {
9043 n = TRUE;
9044 fclose(fd);
9045 }
9046 else
9047 n = FALSE;
9048
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050}
9051
9052/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009053 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 * rights to write into.
9055 */
9056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009058 typval_T *argvars;
9059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009061 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062}
9063
Bram Moolenaar33570922005-01-25 22:26:29 +00009064static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009065
9066 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009067findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009068 typval_T *argvars;
9069 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009070 int dir;
9071{
9072#ifdef FEAT_SEARCHPATH
9073 char_u *fname;
9074 char_u *fresult = NULL;
9075 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9076 char_u *p;
9077 char_u pathbuf[NUMBUFLEN];
9078 int count = 1;
9079 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009080 int error = FALSE;
9081#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009082
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009083 rettv->vval.v_string = NULL;
9084 rettv->v_type = VAR_STRING;
9085
9086#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009088
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009089 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009091 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9092 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009093 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009094 else
9095 {
9096 if (*p != NUL)
9097 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009098
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009099 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009100 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009101 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009102 }
9103
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009104 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9105 error = TRUE;
9106
9107 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009109 do
9110 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009111 if (rettv->v_type == VAR_STRING)
9112 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009113 fresult = find_file_in_path_option(first ? fname : NULL,
9114 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009115 0, first, path, dir, NULL,
9116 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009117 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009118
9119 if (fresult != NULL && rettv->v_type == VAR_LIST)
9120 list_append_string(rettv->vval.v_list, fresult, -1);
9121
9122 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009123 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009124
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009125 if (rettv->v_type == VAR_STRING)
9126 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009127#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009128}
9129
Bram Moolenaar33570922005-01-25 22:26:29 +00009130static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9131static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009132
9133/*
9134 * Implementation of map() and filter().
9135 */
9136 static void
9137filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009138 typval_T *argvars;
9139 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009140 int map;
9141{
9142 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009143 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009144 listitem_T *li, *nli;
9145 list_T *l = NULL;
9146 dictitem_T *di;
9147 hashtab_T *ht;
9148 hashitem_T *hi;
9149 dict_T *d = NULL;
9150 typval_T save_val;
9151 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009152 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009153 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009154 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009155 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009156
9157 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009158 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009159 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009160 if ((l = argvars[0].vval.v_list) == NULL
9161 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009162 return;
9163 }
9164 else if (argvars[0].v_type == VAR_DICT)
9165 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009166 if ((d = argvars[0].vval.v_dict) == NULL
9167 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009168 return;
9169 }
9170 else
9171 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009172 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009173 return;
9174 }
9175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009176 expr = get_tv_string_buf_chk(&argvars[1], buf);
9177 /* On type errors, the preceding call has already displayed an error
9178 * message. Avoid a misleading error message for an empty string that
9179 * was not passed as argument. */
9180 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009181 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009182 prepare_vimvar(VV_VAL, &save_val);
9183 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009184
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009185 /* We reset "did_emsg" to be able to detect whether an error
9186 * occurred during evaluation of the expression. */
9187 save_did_emsg = did_emsg;
9188 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009189
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009190 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009192 prepare_vimvar(VV_KEY, &save_key);
9193 vimvars[VV_KEY].vv_type = VAR_STRING;
9194
9195 ht = &d->dv_hashtab;
9196 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009197 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009198 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009199 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009200 if (!HASHITEM_EMPTY(hi))
9201 {
9202 --todo;
9203 di = HI2DI(hi);
9204 if (tv_check_lock(di->di_tv.v_lock, msg))
9205 break;
9206 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009207 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009208 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009209 break;
9210 if (!map && rem)
9211 dictitem_remove(d, di);
9212 clear_tv(&vimvars[VV_KEY].vv_tv);
9213 }
9214 }
9215 hash_unlock(ht);
9216
9217 restore_vimvar(VV_KEY, &save_key);
9218 }
9219 else
9220 {
9221 for (li = l->lv_first; li != NULL; li = nli)
9222 {
9223 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009224 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009225 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009226 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009227 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009228 break;
9229 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009230 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009231 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009232 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009233
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009234 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009235
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009236 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009237 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009238
9239 copy_tv(&argvars[0], rettv);
9240}
9241
9242 static int
9243filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009244 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009245 char_u *expr;
9246 int map;
9247 int *remp;
9248{
Bram Moolenaar33570922005-01-25 22:26:29 +00009249 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009250 char_u *s;
9251
Bram Moolenaar33570922005-01-25 22:26:29 +00009252 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009253 s = expr;
9254 if (eval1(&s, &rettv, TRUE) == FAIL)
9255 return FAIL;
9256 if (*s != NUL) /* check for trailing chars after expr */
9257 {
9258 EMSG2(_(e_invexpr2), s);
9259 return FAIL;
9260 }
9261 if (map)
9262 {
9263 /* map(): replace the list item value */
9264 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009265 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009266 *tv = rettv;
9267 }
9268 else
9269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009270 int error = FALSE;
9271
Bram Moolenaare9a41262005-01-15 22:18:47 +00009272 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009273 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009274 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009275 /* On type error, nothing has been removed; return FAIL to stop the
9276 * loop. The error message was given by get_tv_number_chk(). */
9277 if (error)
9278 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009279 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009280 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009281 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009282}
9283
9284/*
9285 * "filter()" function
9286 */
9287 static void
9288f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009289 typval_T *argvars;
9290 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009291{
9292 filter_map(argvars, rettv, FALSE);
9293}
9294
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009295/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009296 * "finddir({fname}[, {path}[, {count}]])" function
9297 */
9298 static void
9299f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009300 typval_T *argvars;
9301 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009302{
9303 findfilendir(argvars, rettv, TRUE);
9304}
9305
9306/*
9307 * "findfile({fname}[, {path}[, {count}]])" function
9308 */
9309 static void
9310f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009311 typval_T *argvars;
9312 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009313{
9314 findfilendir(argvars, rettv, FALSE);
9315}
9316
9317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 * "fnamemodify({fname}, {mods})" function
9319 */
9320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009321f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009322 typval_T *argvars;
9323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324{
9325 char_u *fname;
9326 char_u *mods;
9327 int usedlen = 0;
9328 int len;
9329 char_u *fbuf = NULL;
9330 char_u buf[NUMBUFLEN];
9331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009332 fname = get_tv_string_chk(&argvars[0]);
9333 mods = get_tv_string_buf_chk(&argvars[1], buf);
9334 if (fname == NULL || mods == NULL)
9335 fname = NULL;
9336 else
9337 {
9338 len = (int)STRLEN(fname);
9339 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009342 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009344 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009346 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 vim_free(fbuf);
9348}
9349
Bram Moolenaar33570922005-01-25 22:26:29 +00009350static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351
9352/*
9353 * "foldclosed()" function
9354 */
9355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009356foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009357 typval_T *argvars;
9358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 int end;
9360{
9361#ifdef FEAT_FOLDING
9362 linenr_T lnum;
9363 linenr_T first, last;
9364
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009365 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9367 {
9368 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9369 {
9370 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009373 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 return;
9375 }
9376 }
9377#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009378 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379}
9380
9381/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009382 * "foldclosed()" function
9383 */
9384 static void
9385f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009386 typval_T *argvars;
9387 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009388{
9389 foldclosed_both(argvars, rettv, FALSE);
9390}
9391
9392/*
9393 * "foldclosedend()" function
9394 */
9395 static void
9396f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009397 typval_T *argvars;
9398 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009399{
9400 foldclosed_both(argvars, rettv, TRUE);
9401}
9402
9403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 * "foldlevel()" function
9405 */
9406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009407f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009408 typval_T *argvars;
9409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410{
9411#ifdef FEAT_FOLDING
9412 linenr_T lnum;
9413
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009414 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 else
9418#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009419 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420}
9421
9422/*
9423 * "foldtext()" function
9424 */
9425/*ARGSUSED*/
9426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009428 typval_T *argvars;
9429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430{
9431#ifdef FEAT_FOLDING
9432 linenr_T lnum;
9433 char_u *s;
9434 char_u *r;
9435 int len;
9436 char *txt;
9437#endif
9438
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009439 rettv->v_type = VAR_STRING;
9440 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009442 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9443 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9444 <= curbuf->b_ml.ml_line_count
9445 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 {
9447 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009448 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9449 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 {
9451 if (!linewhite(lnum))
9452 break;
9453 ++lnum;
9454 }
9455
9456 /* Find interesting text in this line. */
9457 s = skipwhite(ml_get(lnum));
9458 /* skip C comment-start */
9459 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009460 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009462 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009463 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009464 {
9465 s = skipwhite(ml_get(lnum + 1));
9466 if (*s == '*')
9467 s = skipwhite(s + 1);
9468 }
9469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 txt = _("+-%s%3ld lines: ");
9471 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009472 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 + 20 /* for %3ld */
9474 + STRLEN(s))); /* concatenated */
9475 if (r != NULL)
9476 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009477 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9478 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9479 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 len = (int)STRLEN(r);
9481 STRCAT(r, s);
9482 /* remove 'foldmarker' and 'commentstring' */
9483 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 }
9486 }
9487#endif
9488}
9489
9490/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009491 * "foldtextresult(lnum)" function
9492 */
9493/*ARGSUSED*/
9494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009496 typval_T *argvars;
9497 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009498{
9499#ifdef FEAT_FOLDING
9500 linenr_T lnum;
9501 char_u *text;
9502 char_u buf[51];
9503 foldinfo_T foldinfo;
9504 int fold_count;
9505#endif
9506
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009507 rettv->v_type = VAR_STRING;
9508 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009509#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009510 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009511 /* treat illegal types and illegal string values for {lnum} the same */
9512 if (lnum < 0)
9513 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009514 fold_count = foldedCount(curwin, lnum, &foldinfo);
9515 if (fold_count > 0)
9516 {
9517 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9518 &foldinfo, buf);
9519 if (text == buf)
9520 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009522 }
9523#endif
9524}
9525
9526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 * "foreground()" function
9528 */
9529/*ARGSUSED*/
9530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009531f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009532 typval_T *argvars;
9533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536#ifdef FEAT_GUI
9537 if (gui.in_use)
9538 gui_mch_set_foreground();
9539#else
9540# ifdef WIN32
9541 win32_set_foreground();
9542# endif
9543#endif
9544}
9545
9546/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009547 * "function()" function
9548 */
9549/*ARGSUSED*/
9550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009551f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009552 typval_T *argvars;
9553 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009554{
9555 char_u *s;
9556
Bram Moolenaara7043832005-01-21 11:56:39 +00009557 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009559 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009560 EMSG2(_(e_invarg2), s);
9561 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009562 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009563 else
9564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565 rettv->vval.v_string = vim_strsave(s);
9566 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009567 }
9568}
9569
9570/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009571 * "garbagecollect()" function
9572 */
9573/*ARGSUSED*/
9574 static void
9575f_garbagecollect(argvars, rettv)
9576 typval_T *argvars;
9577 typval_T *rettv;
9578{
9579 garbage_collect();
9580}
9581
9582/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009583 * "get()" function
9584 */
9585 static void
9586f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009587 typval_T *argvars;
9588 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009589{
Bram Moolenaar33570922005-01-25 22:26:29 +00009590 listitem_T *li;
9591 list_T *l;
9592 dictitem_T *di;
9593 dict_T *d;
9594 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009595
Bram Moolenaare9a41262005-01-15 22:18:47 +00009596 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009597 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009598 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009600 int error = FALSE;
9601
9602 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9603 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009604 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009605 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009606 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009607 else if (argvars[0].v_type == VAR_DICT)
9608 {
9609 if ((d = argvars[0].vval.v_dict) != NULL)
9610 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009611 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009612 if (di != NULL)
9613 tv = &di->di_tv;
9614 }
9615 }
9616 else
9617 EMSG2(_(e_listdictarg), "get()");
9618
9619 if (tv == NULL)
9620 {
9621 if (argvars[2].v_type == VAR_UNKNOWN)
9622 rettv->vval.v_number = 0;
9623 else
9624 copy_tv(&argvars[2], rettv);
9625 }
9626 else
9627 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009628}
9629
Bram Moolenaar342337a2005-07-21 21:11:17 +00009630static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009631
9632/*
9633 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009634 * Return a range (from start to end) of lines in rettv from the specified
9635 * buffer.
9636 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009637 */
9638 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009639get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009640 buf_T *buf;
9641 linenr_T start;
9642 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009643 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009644 typval_T *rettv;
9645{
9646 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009647
Bram Moolenaar342337a2005-07-21 21:11:17 +00009648 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009649 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009650 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009651 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009652 }
9653 else
9654 rettv->vval.v_number = 0;
9655
9656 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9657 return;
9658
9659 if (!retlist)
9660 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009661 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9662 p = ml_get_buf(buf, start, FALSE);
9663 else
9664 p = (char_u *)"";
9665
9666 rettv->v_type = VAR_STRING;
9667 rettv->vval.v_string = vim_strsave(p);
9668 }
9669 else
9670 {
9671 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009672 return;
9673
9674 if (start < 1)
9675 start = 1;
9676 if (end > buf->b_ml.ml_line_count)
9677 end = buf->b_ml.ml_line_count;
9678 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009679 if (list_append_string(rettv->vval.v_list,
9680 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009681 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009682 }
9683}
9684
9685/*
9686 * "getbufline()" function
9687 */
9688 static void
9689f_getbufline(argvars, rettv)
9690 typval_T *argvars;
9691 typval_T *rettv;
9692{
9693 linenr_T lnum;
9694 linenr_T end;
9695 buf_T *buf;
9696
9697 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9698 ++emsg_off;
9699 buf = get_buf_tv(&argvars[0]);
9700 --emsg_off;
9701
Bram Moolenaar661b1822005-07-28 22:36:45 +00009702 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009703 if (argvars[2].v_type == VAR_UNKNOWN)
9704 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009705 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009706 end = get_tv_lnum_buf(&argvars[2], buf);
9707
Bram Moolenaar342337a2005-07-21 21:11:17 +00009708 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009709}
9710
Bram Moolenaar0d660222005-01-07 21:51:51 +00009711/*
9712 * "getbufvar()" function
9713 */
9714 static void
9715f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009716 typval_T *argvars;
9717 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009718{
9719 buf_T *buf;
9720 buf_T *save_curbuf;
9721 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009722 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009724 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9725 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009726 ++emsg_off;
9727 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009728
9729 rettv->v_type = VAR_STRING;
9730 rettv->vval.v_string = NULL;
9731
9732 if (buf != NULL && varname != NULL)
9733 {
9734 if (*varname == '&') /* buffer-local-option */
9735 {
9736 /* set curbuf to be our buf, temporarily */
9737 save_curbuf = curbuf;
9738 curbuf = buf;
9739
9740 get_option_tv(&varname, rettv, TRUE);
9741
9742 /* restore previous notion of curbuf */
9743 curbuf = save_curbuf;
9744 }
9745 else
9746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009747 if (*varname == NUL)
9748 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9749 * scope prefix before the NUL byte is required by
9750 * find_var_in_ht(). */
9751 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009752 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009753 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009754 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009755 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009756 }
9757 }
9758
9759 --emsg_off;
9760}
9761
9762/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 * "getchar()" function
9764 */
9765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009766f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009767 typval_T *argvars;
9768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769{
9770 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009771 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772
9773 ++no_mapping;
9774 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009775 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 /* getchar(): blocking wait. */
9777 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009778 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 /* getchar(1): only check if char avail */
9780 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009781 else if (error || vpeekc() == NUL)
9782 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 n = 0;
9784 else
9785 /* getchar(0) and char avail: return char */
9786 n = safe_vgetc();
9787 --no_mapping;
9788 --allow_keys;
9789
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009790 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 if (IS_SPECIAL(n) || mod_mask != 0)
9792 {
9793 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9794 int i = 0;
9795
9796 /* Turn a special key into three bytes, plus modifier. */
9797 if (mod_mask != 0)
9798 {
9799 temp[i++] = K_SPECIAL;
9800 temp[i++] = KS_MODIFIER;
9801 temp[i++] = mod_mask;
9802 }
9803 if (IS_SPECIAL(n))
9804 {
9805 temp[i++] = K_SPECIAL;
9806 temp[i++] = K_SECOND(n);
9807 temp[i++] = K_THIRD(n);
9808 }
9809#ifdef FEAT_MBYTE
9810 else if (has_mbyte)
9811 i += (*mb_char2bytes)(n, temp + i);
9812#endif
9813 else
9814 temp[i++] = n;
9815 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009816 rettv->v_type = VAR_STRING;
9817 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 }
9819}
9820
9821/*
9822 * "getcharmod()" function
9823 */
9824/*ARGSUSED*/
9825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009826f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009827 typval_T *argvars;
9828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831}
9832
9833/*
9834 * "getcmdline()" function
9835 */
9836/*ARGSUSED*/
9837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009838f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009839 typval_T *argvars;
9840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842 rettv->v_type = VAR_STRING;
9843 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844}
9845
9846/*
9847 * "getcmdpos()" function
9848 */
9849/*ARGSUSED*/
9850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009851f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009852 typval_T *argvars;
9853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009855 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856}
9857
9858/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009859 * "getcmdtype()" function
9860 */
9861/*ARGSUSED*/
9862 static void
9863f_getcmdtype(argvars, rettv)
9864 typval_T *argvars;
9865 typval_T *rettv;
9866{
9867 rettv->v_type = VAR_STRING;
9868 rettv->vval.v_string = alloc(2);
9869 if (rettv->vval.v_string != NULL)
9870 {
9871 rettv->vval.v_string[0] = get_cmdline_type();
9872 rettv->vval.v_string[1] = NUL;
9873 }
9874}
9875
9876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 * "getcwd()" function
9878 */
9879/*ARGSUSED*/
9880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009881f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009882 typval_T *argvars;
9883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884{
9885 char_u cwd[MAXPATHL];
9886
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009887 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 else
9891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009892 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009894 if (rettv->vval.v_string != NULL)
9895 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896#endif
9897 }
9898}
9899
9900/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009901 * "getfontname()" function
9902 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009903/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009905f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009906 typval_T *argvars;
9907 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009908{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009909 rettv->v_type = VAR_STRING;
9910 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009911#ifdef FEAT_GUI
9912 if (gui.in_use)
9913 {
9914 GuiFont font;
9915 char_u *name = NULL;
9916
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009917 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009918 {
9919 /* Get the "Normal" font. Either the name saved by
9920 * hl_set_font_name() or from the font ID. */
9921 font = gui.norm_font;
9922 name = hl_get_font_name();
9923 }
9924 else
9925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009926 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009927 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9928 return;
9929 font = gui_mch_get_font(name, FALSE);
9930 if (font == NOFONT)
9931 return; /* Invalid font name, return empty string. */
9932 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009933 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009934 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009935 gui_mch_free_font(font);
9936 }
9937#endif
9938}
9939
9940/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009941 * "getfperm({fname})" function
9942 */
9943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009945 typval_T *argvars;
9946 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009947{
9948 char_u *fname;
9949 struct stat st;
9950 char_u *perm = NULL;
9951 char_u flags[] = "rwx";
9952 int i;
9953
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009954 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009955
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009957 if (mch_stat((char *)fname, &st) >= 0)
9958 {
9959 perm = vim_strsave((char_u *)"---------");
9960 if (perm != NULL)
9961 {
9962 for (i = 0; i < 9; i++)
9963 {
9964 if (st.st_mode & (1 << (8 - i)))
9965 perm[i] = flags[i % 3];
9966 }
9967 }
9968 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009969 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009970}
9971
9972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 * "getfsize({fname})" function
9974 */
9975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009976f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009977 typval_T *argvars;
9978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979{
9980 char_u *fname;
9981 struct stat st;
9982
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009983 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986
9987 if (mch_stat((char *)fname, &st) >= 0)
9988 {
9989 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009992 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 }
9994 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009995 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996}
9997
9998/*
9999 * "getftime({fname})" function
10000 */
10001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010002f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010003 typval_T *argvars;
10004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005{
10006 char_u *fname;
10007 struct stat st;
10008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010009 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010
10011 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010012 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010014 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015}
10016
10017/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010018 * "getftype({fname})" function
10019 */
10020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010021f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010022 typval_T *argvars;
10023 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010024{
10025 char_u *fname;
10026 struct stat st;
10027 char_u *type = NULL;
10028 char *t;
10029
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010030 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010033 if (mch_lstat((char *)fname, &st) >= 0)
10034 {
10035#ifdef S_ISREG
10036 if (S_ISREG(st.st_mode))
10037 t = "file";
10038 else if (S_ISDIR(st.st_mode))
10039 t = "dir";
10040# ifdef S_ISLNK
10041 else if (S_ISLNK(st.st_mode))
10042 t = "link";
10043# endif
10044# ifdef S_ISBLK
10045 else if (S_ISBLK(st.st_mode))
10046 t = "bdev";
10047# endif
10048# ifdef S_ISCHR
10049 else if (S_ISCHR(st.st_mode))
10050 t = "cdev";
10051# endif
10052# ifdef S_ISFIFO
10053 else if (S_ISFIFO(st.st_mode))
10054 t = "fifo";
10055# endif
10056# ifdef S_ISSOCK
10057 else if (S_ISSOCK(st.st_mode))
10058 t = "fifo";
10059# endif
10060 else
10061 t = "other";
10062#else
10063# ifdef S_IFMT
10064 switch (st.st_mode & S_IFMT)
10065 {
10066 case S_IFREG: t = "file"; break;
10067 case S_IFDIR: t = "dir"; break;
10068# ifdef S_IFLNK
10069 case S_IFLNK: t = "link"; break;
10070# endif
10071# ifdef S_IFBLK
10072 case S_IFBLK: t = "bdev"; break;
10073# endif
10074# ifdef S_IFCHR
10075 case S_IFCHR: t = "cdev"; break;
10076# endif
10077# ifdef S_IFIFO
10078 case S_IFIFO: t = "fifo"; break;
10079# endif
10080# ifdef S_IFSOCK
10081 case S_IFSOCK: t = "socket"; break;
10082# endif
10083 default: t = "other";
10084 }
10085# else
10086 if (mch_isdir(fname))
10087 t = "dir";
10088 else
10089 t = "file";
10090# endif
10091#endif
10092 type = vim_strsave((char_u *)t);
10093 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010095}
10096
10097/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010098 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010099 */
10100 static void
10101f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010102 typval_T *argvars;
10103 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010104{
10105 linenr_T lnum;
10106 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010107 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010108
10109 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010110 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010111 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010112 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010113 retlist = FALSE;
10114 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010115 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010116 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010117 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010118 retlist = TRUE;
10119 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010120
Bram Moolenaar342337a2005-07-21 21:11:17 +000010121 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010122}
10123
10124/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010125 * "getpos(string)" function
10126 */
10127 static void
10128f_getpos(argvars, rettv)
10129 typval_T *argvars;
10130 typval_T *rettv;
10131{
10132 pos_T *fp;
10133 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010134 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010135
10136 if (rettv_list_alloc(rettv) == OK)
10137 {
10138 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010139 fp = var2fpos(&argvars[0], TRUE, &fnum);
10140 if (fnum != -1)
10141 list_append_number(l, (varnumber_T)fnum);
10142 else
10143 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010144 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10145 : (varnumber_T)0);
10146 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10147 : (varnumber_T)0);
10148 list_append_number(l,
10149#ifdef FEAT_VIRTUALEDIT
10150 (fp != NULL) ? (varnumber_T)fp->coladd :
10151#endif
10152 (varnumber_T)0);
10153 }
10154 else
10155 rettv->vval.v_number = FALSE;
10156}
10157
10158/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010159 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010160 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010161/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010162 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010163f_getqflist(argvars, rettv)
10164 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010165 typval_T *rettv;
10166{
10167#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010168 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010169#endif
10170
10171 rettv->vval.v_number = FALSE;
10172#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010173 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010174 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010175 wp = NULL;
10176 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10177 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010178 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010179 if (wp == NULL)
10180 return;
10181 }
10182
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010183 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010184 }
10185#endif
10186}
10187
10188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 * "getreg()" function
10190 */
10191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010192f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010193 typval_T *argvars;
10194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195{
10196 char_u *strregname;
10197 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010198 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010199 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010201 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010203 strregname = get_tv_string_chk(&argvars[0]);
10204 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010205 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010206 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010209 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 regname = (strregname == NULL ? '"' : *strregname);
10211 if (regname == 0)
10212 regname = '"';
10213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010214 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 rettv->vval.v_string = error ? NULL :
10216 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217}
10218
10219/*
10220 * "getregtype()" function
10221 */
10222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010223f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010224 typval_T *argvars;
10225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226{
10227 char_u *strregname;
10228 int regname;
10229 char_u buf[NUMBUFLEN + 2];
10230 long reglen = 0;
10231
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010232 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010233 {
10234 strregname = get_tv_string_chk(&argvars[0]);
10235 if (strregname == NULL) /* type error; errmsg already given */
10236 {
10237 rettv->v_type = VAR_STRING;
10238 rettv->vval.v_string = NULL;
10239 return;
10240 }
10241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 else
10243 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010244 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245
10246 regname = (strregname == NULL ? '"' : *strregname);
10247 if (regname == 0)
10248 regname = '"';
10249
10250 buf[0] = NUL;
10251 buf[1] = NUL;
10252 switch (get_reg_type(regname, &reglen))
10253 {
10254 case MLINE: buf[0] = 'V'; break;
10255 case MCHAR: buf[0] = 'v'; break;
10256#ifdef FEAT_VISUAL
10257 case MBLOCK:
10258 buf[0] = Ctrl_V;
10259 sprintf((char *)buf + 1, "%ld", reglen + 1);
10260 break;
10261#endif
10262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010263 rettv->v_type = VAR_STRING;
10264 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265}
10266
10267/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010268 * "gettabwinvar()" function
10269 */
10270 static void
10271f_gettabwinvar(argvars, rettv)
10272 typval_T *argvars;
10273 typval_T *rettv;
10274{
10275 getwinvar(argvars, rettv, 1);
10276}
10277
10278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 * "getwinposx()" function
10280 */
10281/*ARGSUSED*/
10282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010283f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010284 typval_T *argvars;
10285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010287 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288#ifdef FEAT_GUI
10289 if (gui.in_use)
10290 {
10291 int x, y;
10292
10293 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010294 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 }
10296#endif
10297}
10298
10299/*
10300 * "getwinposy()" function
10301 */
10302/*ARGSUSED*/
10303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010304f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010305 typval_T *argvars;
10306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010308 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309#ifdef FEAT_GUI
10310 if (gui.in_use)
10311 {
10312 int x, y;
10313
10314 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010315 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 }
10317#endif
10318}
10319
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010320/*
10321 * Find window specifed by "vp" in tabpage "tp".
10322 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010323 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010324find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010325 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010326 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010327{
10328#ifdef FEAT_WINDOWS
10329 win_T *wp;
10330#endif
10331 int nr;
10332
10333 nr = get_tv_number_chk(vp, NULL);
10334
10335#ifdef FEAT_WINDOWS
10336 if (nr < 0)
10337 return NULL;
10338 if (nr == 0)
10339 return curwin;
10340
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010341 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10342 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010343 if (--nr <= 0)
10344 break;
10345 return wp;
10346#else
10347 if (nr == 0 || nr == 1)
10348 return curwin;
10349 return NULL;
10350#endif
10351}
10352
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353/*
10354 * "getwinvar()" function
10355 */
10356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010357f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010358 typval_T *argvars;
10359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010361 getwinvar(argvars, rettv, 0);
10362}
10363
10364/*
10365 * getwinvar() and gettabwinvar()
10366 */
10367 static void
10368getwinvar(argvars, rettv, off)
10369 typval_T *argvars;
10370 typval_T *rettv;
10371 int off; /* 1 for gettabwinvar() */
10372{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 win_T *win, *oldcurwin;
10374 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010375 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010376 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010378#ifdef FEAT_WINDOWS
10379 if (off == 1)
10380 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10381 else
10382 tp = curtab;
10383#endif
10384 win = find_win_by_nr(&argvars[off], tp);
10385 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010386 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010388 rettv->v_type = VAR_STRING;
10389 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390
10391 if (win != NULL && varname != NULL)
10392 {
10393 if (*varname == '&') /* window-local-option */
10394 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010395 /* Set curwin to be our win, temporarily. Also set curbuf, so
10396 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 oldcurwin = curwin;
10398 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010399 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010401 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402
10403 /* restore previous notion of curwin */
10404 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010405 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 }
10407 else
10408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010409 if (*varname == NUL)
10410 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10411 * scope prefix before the NUL byte is required by
10412 * find_var_in_ht(). */
10413 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010415 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010417 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 }
10419 }
10420
10421 --emsg_off;
10422}
10423
10424/*
10425 * "glob()" function
10426 */
10427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010428f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010429 typval_T *argvars;
10430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431{
10432 expand_T xpc;
10433
10434 ExpandInit(&xpc);
10435 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010436 rettv->v_type = VAR_STRING;
10437 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439}
10440
10441/*
10442 * "globpath()" function
10443 */
10444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010445f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010446 typval_T *argvars;
10447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448{
10449 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010450 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010452 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010453 if (file == NULL)
10454 rettv->vval.v_string = NULL;
10455 else
10456 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457}
10458
10459/*
10460 * "has()" function
10461 */
10462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010463f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010464 typval_T *argvars;
10465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466{
10467 int i;
10468 char_u *name;
10469 int n = FALSE;
10470 static char *(has_list[]) =
10471 {
10472#ifdef AMIGA
10473 "amiga",
10474# ifdef FEAT_ARP
10475 "arp",
10476# endif
10477#endif
10478#ifdef __BEOS__
10479 "beos",
10480#endif
10481#ifdef MSDOS
10482# ifdef DJGPP
10483 "dos32",
10484# else
10485 "dos16",
10486# endif
10487#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010488#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 "mac",
10490#endif
10491#if defined(MACOS_X_UNIX)
10492 "macunix",
10493#endif
10494#ifdef OS2
10495 "os2",
10496#endif
10497#ifdef __QNX__
10498 "qnx",
10499#endif
10500#ifdef RISCOS
10501 "riscos",
10502#endif
10503#ifdef UNIX
10504 "unix",
10505#endif
10506#ifdef VMS
10507 "vms",
10508#endif
10509#ifdef WIN16
10510 "win16",
10511#endif
10512#ifdef WIN32
10513 "win32",
10514#endif
10515#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10516 "win32unix",
10517#endif
10518#ifdef WIN64
10519 "win64",
10520#endif
10521#ifdef EBCDIC
10522 "ebcdic",
10523#endif
10524#ifndef CASE_INSENSITIVE_FILENAME
10525 "fname_case",
10526#endif
10527#ifdef FEAT_ARABIC
10528 "arabic",
10529#endif
10530#ifdef FEAT_AUTOCMD
10531 "autocmd",
10532#endif
10533#ifdef FEAT_BEVAL
10534 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010535# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10536 "balloon_multiline",
10537# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538#endif
10539#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10540 "builtin_terms",
10541# ifdef ALL_BUILTIN_TCAPS
10542 "all_builtin_terms",
10543# endif
10544#endif
10545#ifdef FEAT_BYTEOFF
10546 "byte_offset",
10547#endif
10548#ifdef FEAT_CINDENT
10549 "cindent",
10550#endif
10551#ifdef FEAT_CLIENTSERVER
10552 "clientserver",
10553#endif
10554#ifdef FEAT_CLIPBOARD
10555 "clipboard",
10556#endif
10557#ifdef FEAT_CMDL_COMPL
10558 "cmdline_compl",
10559#endif
10560#ifdef FEAT_CMDHIST
10561 "cmdline_hist",
10562#endif
10563#ifdef FEAT_COMMENTS
10564 "comments",
10565#endif
10566#ifdef FEAT_CRYPT
10567 "cryptv",
10568#endif
10569#ifdef FEAT_CSCOPE
10570 "cscope",
10571#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010572#ifdef CURSOR_SHAPE
10573 "cursorshape",
10574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575#ifdef DEBUG
10576 "debug",
10577#endif
10578#ifdef FEAT_CON_DIALOG
10579 "dialog_con",
10580#endif
10581#ifdef FEAT_GUI_DIALOG
10582 "dialog_gui",
10583#endif
10584#ifdef FEAT_DIFF
10585 "diff",
10586#endif
10587#ifdef FEAT_DIGRAPHS
10588 "digraphs",
10589#endif
10590#ifdef FEAT_DND
10591 "dnd",
10592#endif
10593#ifdef FEAT_EMACS_TAGS
10594 "emacs_tags",
10595#endif
10596 "eval", /* always present, of course! */
10597#ifdef FEAT_EX_EXTRA
10598 "ex_extra",
10599#endif
10600#ifdef FEAT_SEARCH_EXTRA
10601 "extra_search",
10602#endif
10603#ifdef FEAT_FKMAP
10604 "farsi",
10605#endif
10606#ifdef FEAT_SEARCHPATH
10607 "file_in_path",
10608#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010609#if defined(UNIX) && !defined(USE_SYSTEM)
10610 "filterpipe",
10611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612#ifdef FEAT_FIND_ID
10613 "find_in_path",
10614#endif
10615#ifdef FEAT_FOLDING
10616 "folding",
10617#endif
10618#ifdef FEAT_FOOTER
10619 "footer",
10620#endif
10621#if !defined(USE_SYSTEM) && defined(UNIX)
10622 "fork",
10623#endif
10624#ifdef FEAT_GETTEXT
10625 "gettext",
10626#endif
10627#ifdef FEAT_GUI
10628 "gui",
10629#endif
10630#ifdef FEAT_GUI_ATHENA
10631# ifdef FEAT_GUI_NEXTAW
10632 "gui_neXtaw",
10633# else
10634 "gui_athena",
10635# endif
10636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637#ifdef FEAT_GUI_GTK
10638 "gui_gtk",
10639# ifdef HAVE_GTK2
10640 "gui_gtk2",
10641# endif
10642#endif
10643#ifdef FEAT_GUI_MAC
10644 "gui_mac",
10645#endif
10646#ifdef FEAT_GUI_MOTIF
10647 "gui_motif",
10648#endif
10649#ifdef FEAT_GUI_PHOTON
10650 "gui_photon",
10651#endif
10652#ifdef FEAT_GUI_W16
10653 "gui_win16",
10654#endif
10655#ifdef FEAT_GUI_W32
10656 "gui_win32",
10657#endif
10658#ifdef FEAT_HANGULIN
10659 "hangul_input",
10660#endif
10661#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10662 "iconv",
10663#endif
10664#ifdef FEAT_INS_EXPAND
10665 "insert_expand",
10666#endif
10667#ifdef FEAT_JUMPLIST
10668 "jumplist",
10669#endif
10670#ifdef FEAT_KEYMAP
10671 "keymap",
10672#endif
10673#ifdef FEAT_LANGMAP
10674 "langmap",
10675#endif
10676#ifdef FEAT_LIBCALL
10677 "libcall",
10678#endif
10679#ifdef FEAT_LINEBREAK
10680 "linebreak",
10681#endif
10682#ifdef FEAT_LISP
10683 "lispindent",
10684#endif
10685#ifdef FEAT_LISTCMDS
10686 "listcmds",
10687#endif
10688#ifdef FEAT_LOCALMAP
10689 "localmap",
10690#endif
10691#ifdef FEAT_MENU
10692 "menu",
10693#endif
10694#ifdef FEAT_SESSION
10695 "mksession",
10696#endif
10697#ifdef FEAT_MODIFY_FNAME
10698 "modify_fname",
10699#endif
10700#ifdef FEAT_MOUSE
10701 "mouse",
10702#endif
10703#ifdef FEAT_MOUSESHAPE
10704 "mouseshape",
10705#endif
10706#if defined(UNIX) || defined(VMS)
10707# ifdef FEAT_MOUSE_DEC
10708 "mouse_dec",
10709# endif
10710# ifdef FEAT_MOUSE_GPM
10711 "mouse_gpm",
10712# endif
10713# ifdef FEAT_MOUSE_JSB
10714 "mouse_jsbterm",
10715# endif
10716# ifdef FEAT_MOUSE_NET
10717 "mouse_netterm",
10718# endif
10719# ifdef FEAT_MOUSE_PTERM
10720 "mouse_pterm",
10721# endif
10722# ifdef FEAT_MOUSE_XTERM
10723 "mouse_xterm",
10724# endif
10725#endif
10726#ifdef FEAT_MBYTE
10727 "multi_byte",
10728#endif
10729#ifdef FEAT_MBYTE_IME
10730 "multi_byte_ime",
10731#endif
10732#ifdef FEAT_MULTI_LANG
10733 "multi_lang",
10734#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010735#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010736#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010737 "mzscheme",
10738#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740#ifdef FEAT_OLE
10741 "ole",
10742#endif
10743#ifdef FEAT_OSFILETYPE
10744 "osfiletype",
10745#endif
10746#ifdef FEAT_PATH_EXTRA
10747 "path_extra",
10748#endif
10749#ifdef FEAT_PERL
10750#ifndef DYNAMIC_PERL
10751 "perl",
10752#endif
10753#endif
10754#ifdef FEAT_PYTHON
10755#ifndef DYNAMIC_PYTHON
10756 "python",
10757#endif
10758#endif
10759#ifdef FEAT_POSTSCRIPT
10760 "postscript",
10761#endif
10762#ifdef FEAT_PRINTER
10763 "printer",
10764#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010765#ifdef FEAT_PROFILE
10766 "profile",
10767#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010768#ifdef FEAT_RELTIME
10769 "reltime",
10770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771#ifdef FEAT_QUICKFIX
10772 "quickfix",
10773#endif
10774#ifdef FEAT_RIGHTLEFT
10775 "rightleft",
10776#endif
10777#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10778 "ruby",
10779#endif
10780#ifdef FEAT_SCROLLBIND
10781 "scrollbind",
10782#endif
10783#ifdef FEAT_CMDL_INFO
10784 "showcmd",
10785 "cmdline_info",
10786#endif
10787#ifdef FEAT_SIGNS
10788 "signs",
10789#endif
10790#ifdef FEAT_SMARTINDENT
10791 "smartindent",
10792#endif
10793#ifdef FEAT_SNIFF
10794 "sniff",
10795#endif
10796#ifdef FEAT_STL_OPT
10797 "statusline",
10798#endif
10799#ifdef FEAT_SUN_WORKSHOP
10800 "sun_workshop",
10801#endif
10802#ifdef FEAT_NETBEANS_INTG
10803 "netbeans_intg",
10804#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010805#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010806 "spell",
10807#endif
10808#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 "syntax",
10810#endif
10811#if defined(USE_SYSTEM) || !defined(UNIX)
10812 "system",
10813#endif
10814#ifdef FEAT_TAG_BINS
10815 "tag_binary",
10816#endif
10817#ifdef FEAT_TAG_OLDSTATIC
10818 "tag_old_static",
10819#endif
10820#ifdef FEAT_TAG_ANYWHITE
10821 "tag_any_white",
10822#endif
10823#ifdef FEAT_TCL
10824# ifndef DYNAMIC_TCL
10825 "tcl",
10826# endif
10827#endif
10828#ifdef TERMINFO
10829 "terminfo",
10830#endif
10831#ifdef FEAT_TERMRESPONSE
10832 "termresponse",
10833#endif
10834#ifdef FEAT_TEXTOBJ
10835 "textobjects",
10836#endif
10837#ifdef HAVE_TGETENT
10838 "tgetent",
10839#endif
10840#ifdef FEAT_TITLE
10841 "title",
10842#endif
10843#ifdef FEAT_TOOLBAR
10844 "toolbar",
10845#endif
10846#ifdef FEAT_USR_CMDS
10847 "user-commands", /* was accidentally included in 5.4 */
10848 "user_commands",
10849#endif
10850#ifdef FEAT_VIMINFO
10851 "viminfo",
10852#endif
10853#ifdef FEAT_VERTSPLIT
10854 "vertsplit",
10855#endif
10856#ifdef FEAT_VIRTUALEDIT
10857 "virtualedit",
10858#endif
10859#ifdef FEAT_VISUAL
10860 "visual",
10861#endif
10862#ifdef FEAT_VISUALEXTRA
10863 "visualextra",
10864#endif
10865#ifdef FEAT_VREPLACE
10866 "vreplace",
10867#endif
10868#ifdef FEAT_WILDIGN
10869 "wildignore",
10870#endif
10871#ifdef FEAT_WILDMENU
10872 "wildmenu",
10873#endif
10874#ifdef FEAT_WINDOWS
10875 "windows",
10876#endif
10877#ifdef FEAT_WAK
10878 "winaltkeys",
10879#endif
10880#ifdef FEAT_WRITEBACKUP
10881 "writebackup",
10882#endif
10883#ifdef FEAT_XIM
10884 "xim",
10885#endif
10886#ifdef FEAT_XFONTSET
10887 "xfontset",
10888#endif
10889#ifdef USE_XSMP
10890 "xsmp",
10891#endif
10892#ifdef USE_XSMP_INTERACT
10893 "xsmp_interact",
10894#endif
10895#ifdef FEAT_XCLIPBOARD
10896 "xterm_clipboard",
10897#endif
10898#ifdef FEAT_XTERM_SAVE
10899 "xterm_save",
10900#endif
10901#if defined(UNIX) && defined(FEAT_X11)
10902 "X11",
10903#endif
10904 NULL
10905 };
10906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010907 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 for (i = 0; has_list[i] != NULL; ++i)
10909 if (STRICMP(name, has_list[i]) == 0)
10910 {
10911 n = TRUE;
10912 break;
10913 }
10914
10915 if (n == FALSE)
10916 {
10917 if (STRNICMP(name, "patch", 5) == 0)
10918 n = has_patch(atoi((char *)name + 5));
10919 else if (STRICMP(name, "vim_starting") == 0)
10920 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010921#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10922 else if (STRICMP(name, "balloon_multiline") == 0)
10923 n = multiline_balloon_available();
10924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925#ifdef DYNAMIC_TCL
10926 else if (STRICMP(name, "tcl") == 0)
10927 n = tcl_enabled(FALSE);
10928#endif
10929#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10930 else if (STRICMP(name, "iconv") == 0)
10931 n = iconv_enabled(FALSE);
10932#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010933#ifdef DYNAMIC_MZSCHEME
10934 else if (STRICMP(name, "mzscheme") == 0)
10935 n = mzscheme_enabled(FALSE);
10936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937#ifdef DYNAMIC_RUBY
10938 else if (STRICMP(name, "ruby") == 0)
10939 n = ruby_enabled(FALSE);
10940#endif
10941#ifdef DYNAMIC_PYTHON
10942 else if (STRICMP(name, "python") == 0)
10943 n = python_enabled(FALSE);
10944#endif
10945#ifdef DYNAMIC_PERL
10946 else if (STRICMP(name, "perl") == 0)
10947 n = perl_enabled(FALSE);
10948#endif
10949#ifdef FEAT_GUI
10950 else if (STRICMP(name, "gui_running") == 0)
10951 n = (gui.in_use || gui.starting);
10952# ifdef FEAT_GUI_W32
10953 else if (STRICMP(name, "gui_win32s") == 0)
10954 n = gui_is_win32s();
10955# endif
10956# ifdef FEAT_BROWSE
10957 else if (STRICMP(name, "browse") == 0)
10958 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10959# endif
10960#endif
10961#ifdef FEAT_SYN_HL
10962 else if (STRICMP(name, "syntax_items") == 0)
10963 n = syntax_present(curbuf);
10964#endif
10965#if defined(WIN3264)
10966 else if (STRICMP(name, "win95") == 0)
10967 n = mch_windows95();
10968#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010969#ifdef FEAT_NETBEANS_INTG
10970 else if (STRICMP(name, "netbeans_enabled") == 0)
10971 n = usingNetbeans;
10972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 }
10974
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976}
10977
10978/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010979 * "has_key()" function
10980 */
10981 static void
10982f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010983 typval_T *argvars;
10984 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010985{
10986 rettv->vval.v_number = 0;
10987 if (argvars[0].v_type != VAR_DICT)
10988 {
10989 EMSG(_(e_dictreq));
10990 return;
10991 }
10992 if (argvars[0].vval.v_dict == NULL)
10993 return;
10994
10995 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010996 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010997}
10998
10999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 * "hasmapto()" function
11001 */
11002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011003f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011004 typval_T *argvars;
11005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006{
11007 char_u *name;
11008 char_u *mode;
11009 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011010 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011012 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011013 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 mode = (char_u *)"nvo";
11015 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011017 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011018 if (argvars[2].v_type != VAR_UNKNOWN)
11019 abbr = get_tv_number(&argvars[2]);
11020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021
Bram Moolenaar2c932302006-03-18 21:42:09 +000011022 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011023 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026}
11027
11028/*
11029 * "histadd()" function
11030 */
11031/*ARGSUSED*/
11032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011033f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011034 typval_T *argvars;
11035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036{
11037#ifdef FEAT_CMDHIST
11038 int histype;
11039 char_u *str;
11040 char_u buf[NUMBUFLEN];
11041#endif
11042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011043 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 if (check_restricted() || check_secure())
11045 return;
11046#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011047 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11048 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 if (histype >= 0)
11050 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 if (*str != NUL)
11053 {
11054 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011055 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 return;
11057 }
11058 }
11059#endif
11060}
11061
11062/*
11063 * "histdel()" function
11064 */
11065/*ARGSUSED*/
11066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011067f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011068 typval_T *argvars;
11069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070{
11071#ifdef FEAT_CMDHIST
11072 int n;
11073 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011074 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11077 if (str == NULL)
11078 n = 0;
11079 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011082 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011084 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 else
11087 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011088 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011089 get_tv_string_buf(&argvars[1], buf));
11090 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011092 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093#endif
11094}
11095
11096/*
11097 * "histget()" function
11098 */
11099/*ARGSUSED*/
11100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011101f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011102 typval_T *argvars;
11103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104{
11105#ifdef FEAT_CMDHIST
11106 int type;
11107 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011108 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011110 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11111 if (str == NULL)
11112 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011114 {
11115 type = get_histtype(str);
11116 if (argvars[1].v_type == VAR_UNKNOWN)
11117 idx = get_history_idx(type);
11118 else
11119 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11120 /* -1 on type error */
11121 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011124 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011126 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127}
11128
11129/*
11130 * "histnr()" function
11131 */
11132/*ARGSUSED*/
11133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011134f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011135 typval_T *argvars;
11136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137{
11138 int i;
11139
11140#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011141 char_u *history = get_tv_string_chk(&argvars[0]);
11142
11143 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 if (i >= HIST_CMD && i < HIST_COUNT)
11145 i = get_history_idx(i);
11146 else
11147#endif
11148 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011149 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150}
11151
11152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 * "highlightID(name)" function
11154 */
11155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011156f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011157 typval_T *argvars;
11158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011160 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161}
11162
11163/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011164 * "highlight_exists()" function
11165 */
11166 static void
11167f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011168 typval_T *argvars;
11169 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011170{
11171 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11172}
11173
11174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 * "hostname()" function
11176 */
11177/*ARGSUSED*/
11178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011179f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011180 typval_T *argvars;
11181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182{
11183 char_u hostname[256];
11184
11185 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011186 rettv->v_type = VAR_STRING;
11187 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188}
11189
11190/*
11191 * iconv() function
11192 */
11193/*ARGSUSED*/
11194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011195f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011196 typval_T *argvars;
11197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198{
11199#ifdef FEAT_MBYTE
11200 char_u buf1[NUMBUFLEN];
11201 char_u buf2[NUMBUFLEN];
11202 char_u *from, *to, *str;
11203 vimconv_T vimconv;
11204#endif
11205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011206 rettv->v_type = VAR_STRING;
11207 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208
11209#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210 str = get_tv_string(&argvars[0]);
11211 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11212 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213 vimconv.vc_type = CONV_NONE;
11214 convert_setup(&vimconv, from, to);
11215
11216 /* If the encodings are equal, no conversion needed. */
11217 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011220 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221
11222 convert_setup(&vimconv, NULL, NULL);
11223 vim_free(from);
11224 vim_free(to);
11225#endif
11226}
11227
11228/*
11229 * "indent()" function
11230 */
11231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011232f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011233 typval_T *argvars;
11234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235{
11236 linenr_T lnum;
11237
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011238 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011240 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011242 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243}
11244
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011245/*
11246 * "index()" function
11247 */
11248 static void
11249f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011250 typval_T *argvars;
11251 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011252{
Bram Moolenaar33570922005-01-25 22:26:29 +000011253 list_T *l;
11254 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011255 long idx = 0;
11256 int ic = FALSE;
11257
11258 rettv->vval.v_number = -1;
11259 if (argvars[0].v_type != VAR_LIST)
11260 {
11261 EMSG(_(e_listreq));
11262 return;
11263 }
11264 l = argvars[0].vval.v_list;
11265 if (l != NULL)
11266 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011267 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011268 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011270 int error = FALSE;
11271
Bram Moolenaar758711c2005-02-02 23:11:38 +000011272 /* Start at specified item. Use the cached index that list_find()
11273 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011274 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011275 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011276 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011277 ic = get_tv_number_chk(&argvars[3], &error);
11278 if (error)
11279 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011280 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011281
Bram Moolenaar758711c2005-02-02 23:11:38 +000011282 for ( ; item != NULL; item = item->li_next, ++idx)
11283 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011284 {
11285 rettv->vval.v_number = idx;
11286 break;
11287 }
11288 }
11289}
11290
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291static int inputsecret_flag = 0;
11292
11293/*
11294 * "input()" function
11295 * Also handles inputsecret() when inputsecret is set.
11296 */
11297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011299 typval_T *argvars;
11300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011302 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303 char_u *p = NULL;
11304 int c;
11305 char_u buf[NUMBUFLEN];
11306 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011307 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011308 int xp_type = EXPAND_NOTHING;
11309 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011311 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312
11313#ifdef NO_CONSOLE_INPUT
11314 /* While starting up, there is no place to enter text. */
11315 if (no_console_input())
11316 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011317 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 return;
11319 }
11320#endif
11321
11322 cmd_silent = FALSE; /* Want to see the prompt. */
11323 if (prompt != NULL)
11324 {
11325 /* Only the part of the message after the last NL is considered as
11326 * prompt for the command line */
11327 p = vim_strrchr(prompt, '\n');
11328 if (p == NULL)
11329 p = prompt;
11330 else
11331 {
11332 ++p;
11333 c = *p;
11334 *p = NUL;
11335 msg_start();
11336 msg_clr_eos();
11337 msg_puts_attr(prompt, echo_attr);
11338 msg_didout = FALSE;
11339 msg_starthere();
11340 *p = c;
11341 }
11342 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011344 if (argvars[1].v_type != VAR_UNKNOWN)
11345 {
11346 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11347 if (defstr != NULL)
11348 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349
Bram Moolenaar4463f292005-09-25 22:20:24 +000011350 if (argvars[2].v_type != VAR_UNKNOWN)
11351 {
11352 char_u *xp_name;
11353 int xp_namelen;
11354 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011355
Bram Moolenaar4463f292005-09-25 22:20:24 +000011356 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011357
Bram Moolenaar4463f292005-09-25 22:20:24 +000011358 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11359 if (xp_name == NULL)
11360 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011361
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011362 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011363
Bram Moolenaar4463f292005-09-25 22:20:24 +000011364 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11365 &xp_arg) == FAIL)
11366 return;
11367 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011368 }
11369
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011370 if (defstr != NULL)
11371 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011372 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11373 xp_type, xp_arg);
11374
11375 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011377 /* since the user typed this, no need to wait for return */
11378 need_wait_return = FALSE;
11379 msg_didout = FALSE;
11380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 cmd_silent = cmd_silent_save;
11382}
11383
11384/*
11385 * "inputdialog()" function
11386 */
11387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011389 typval_T *argvars;
11390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391{
11392#if defined(FEAT_GUI_TEXTDIALOG)
11393 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11394 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11395 {
11396 char_u *message;
11397 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011398 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011400 message = get_tv_string_chk(&argvars[0]);
11401 if (argvars[1].v_type != VAR_UNKNOWN
11402 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011403 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 else
11405 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011406 if (message != NULL && defstr != NULL
11407 && do_dialog(VIM_QUESTION, NULL, message,
11408 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011409 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 else
11411 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011412 if (message != NULL && defstr != NULL
11413 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011414 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011415 rettv->vval.v_string = vim_strsave(
11416 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011420 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 }
11422 else
11423#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011424 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425}
11426
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011427/*
11428 * "inputlist()" function
11429 */
11430 static void
11431f_inputlist(argvars, rettv)
11432 typval_T *argvars;
11433 typval_T *rettv;
11434{
11435 listitem_T *li;
11436 int selected;
11437 int mouse_used;
11438
11439 rettv->vval.v_number = 0;
11440#ifdef NO_CONSOLE_INPUT
11441 /* While starting up, there is no place to enter text. */
11442 if (no_console_input())
11443 return;
11444#endif
11445 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11446 {
11447 EMSG2(_(e_listarg), "inputlist()");
11448 return;
11449 }
11450
11451 msg_start();
11452 lines_left = Rows; /* avoid more prompt */
11453 msg_scroll = TRUE;
11454 msg_clr_eos();
11455
11456 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11457 {
11458 msg_puts(get_tv_string(&li->li_tv));
11459 msg_putchar('\n');
11460 }
11461
11462 /* Ask for choice. */
11463 selected = prompt_for_number(&mouse_used);
11464 if (mouse_used)
11465 selected -= lines_left;
11466
11467 rettv->vval.v_number = selected;
11468}
11469
11470
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11472
11473/*
11474 * "inputrestore()" function
11475 */
11476/*ARGSUSED*/
11477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011479 typval_T *argvars;
11480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481{
11482 if (ga_userinput.ga_len > 0)
11483 {
11484 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11486 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488 }
11489 else if (p_verbose > 1)
11490 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011491 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 }
11494}
11495
11496/*
11497 * "inputsave()" function
11498 */
11499/*ARGSUSED*/
11500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 typval_T *argvars;
11503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504{
11505 /* Add an entry to the stack of typehead storage. */
11506 if (ga_grow(&ga_userinput, 1) == OK)
11507 {
11508 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11509 + ga_userinput.ga_len);
11510 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512 }
11513 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011514 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515}
11516
11517/*
11518 * "inputsecret()" function
11519 */
11520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011521f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011522 typval_T *argvars;
11523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524{
11525 ++cmdline_star;
11526 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011527 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 --cmdline_star;
11529 --inputsecret_flag;
11530}
11531
11532/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011533 * "insert()" function
11534 */
11535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011536f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011537 typval_T *argvars;
11538 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011539{
11540 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011541 listitem_T *item;
11542 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011543 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011544
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011545 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011546 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011547 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011548 else if ((l = argvars[0].vval.v_list) != NULL
11549 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011550 {
11551 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011552 before = get_tv_number_chk(&argvars[2], &error);
11553 if (error)
11554 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011555
Bram Moolenaar758711c2005-02-02 23:11:38 +000011556 if (before == l->lv_len)
11557 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011558 else
11559 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011560 item = list_find(l, before);
11561 if (item == NULL)
11562 {
11563 EMSGN(_(e_listidx), before);
11564 l = NULL;
11565 }
11566 }
11567 if (l != NULL)
11568 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011569 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011570 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011571 }
11572 }
11573}
11574
11575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 * "isdirectory()" function
11577 */
11578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011579f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011580 typval_T *argvars;
11581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011583 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584}
11585
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011586/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011587 * "islocked()" function
11588 */
11589 static void
11590f_islocked(argvars, rettv)
11591 typval_T *argvars;
11592 typval_T *rettv;
11593{
11594 lval_T lv;
11595 char_u *end;
11596 dictitem_T *di;
11597
11598 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011599 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11600 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011601 if (end != NULL && lv.ll_name != NULL)
11602 {
11603 if (*end != NUL)
11604 EMSG(_(e_trailing));
11605 else
11606 {
11607 if (lv.ll_tv == NULL)
11608 {
11609 if (check_changedtick(lv.ll_name))
11610 rettv->vval.v_number = 1; /* always locked */
11611 else
11612 {
11613 di = find_var(lv.ll_name, NULL);
11614 if (di != NULL)
11615 {
11616 /* Consider a variable locked when:
11617 * 1. the variable itself is locked
11618 * 2. the value of the variable is locked.
11619 * 3. the List or Dict value is locked.
11620 */
11621 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11622 || tv_islocked(&di->di_tv));
11623 }
11624 }
11625 }
11626 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011627 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011628 else if (lv.ll_newkey != NULL)
11629 EMSG2(_(e_dictkey), lv.ll_newkey);
11630 else if (lv.ll_list != NULL)
11631 /* List item. */
11632 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11633 else
11634 /* Dictionary item. */
11635 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11636 }
11637 }
11638
11639 clear_lval(&lv);
11640}
11641
Bram Moolenaar33570922005-01-25 22:26:29 +000011642static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011643
11644/*
11645 * Turn a dict into a list:
11646 * "what" == 0: list of keys
11647 * "what" == 1: list of values
11648 * "what" == 2: list of items
11649 */
11650 static void
11651dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011652 typval_T *argvars;
11653 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011654 int what;
11655{
Bram Moolenaar33570922005-01-25 22:26:29 +000011656 list_T *l2;
11657 dictitem_T *di;
11658 hashitem_T *hi;
11659 listitem_T *li;
11660 listitem_T *li2;
11661 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011662 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011663
11664 rettv->vval.v_number = 0;
11665 if (argvars[0].v_type != VAR_DICT)
11666 {
11667 EMSG(_(e_dictreq));
11668 return;
11669 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011670 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011671 return;
11672
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011673 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011674 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011675
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011676 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011677 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011678 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011679 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011680 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011681 --todo;
11682 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011683
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011684 li = listitem_alloc();
11685 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011686 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011687 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011688
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011689 if (what == 0)
11690 {
11691 /* keys() */
11692 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011693 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011694 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11695 }
11696 else if (what == 1)
11697 {
11698 /* values() */
11699 copy_tv(&di->di_tv, &li->li_tv);
11700 }
11701 else
11702 {
11703 /* items() */
11704 l2 = list_alloc();
11705 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011706 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011707 li->li_tv.vval.v_list = l2;
11708 if (l2 == NULL)
11709 break;
11710 ++l2->lv_refcount;
11711
11712 li2 = listitem_alloc();
11713 if (li2 == NULL)
11714 break;
11715 list_append(l2, li2);
11716 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011717 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011718 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11719
11720 li2 = listitem_alloc();
11721 if (li2 == NULL)
11722 break;
11723 list_append(l2, li2);
11724 copy_tv(&di->di_tv, &li2->li_tv);
11725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011726 }
11727 }
11728}
11729
11730/*
11731 * "items(dict)" function
11732 */
11733 static void
11734f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011735 typval_T *argvars;
11736 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011737{
11738 dict_list(argvars, rettv, 2);
11739}
11740
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011742 * "join()" function
11743 */
11744 static void
11745f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011746 typval_T *argvars;
11747 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011748{
11749 garray_T ga;
11750 char_u *sep;
11751
11752 rettv->vval.v_number = 0;
11753 if (argvars[0].v_type != VAR_LIST)
11754 {
11755 EMSG(_(e_listreq));
11756 return;
11757 }
11758 if (argvars[0].vval.v_list == NULL)
11759 return;
11760 if (argvars[1].v_type == VAR_UNKNOWN)
11761 sep = (char_u *)" ";
11762 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011763 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011764
11765 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011766
11767 if (sep != NULL)
11768 {
11769 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011770 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011771 ga_append(&ga, NUL);
11772 rettv->vval.v_string = (char_u *)ga.ga_data;
11773 }
11774 else
11775 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011776}
11777
11778/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011779 * "keys()" function
11780 */
11781 static void
11782f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011783 typval_T *argvars;
11784 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011785{
11786 dict_list(argvars, rettv, 0);
11787}
11788
11789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790 * "last_buffer_nr()" function.
11791 */
11792/*ARGSUSED*/
11793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011794f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011795 typval_T *argvars;
11796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797{
11798 int n = 0;
11799 buf_T *buf;
11800
11801 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11802 if (n < buf->b_fnum)
11803 n = buf->b_fnum;
11804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011805 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011806}
11807
11808/*
11809 * "len()" function
11810 */
11811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011812f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011813 typval_T *argvars;
11814 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011815{
11816 switch (argvars[0].v_type)
11817 {
11818 case VAR_STRING:
11819 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011820 rettv->vval.v_number = (varnumber_T)STRLEN(
11821 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011822 break;
11823 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011824 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011825 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011826 case VAR_DICT:
11827 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11828 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011829 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011830 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011831 break;
11832 }
11833}
11834
Bram Moolenaar33570922005-01-25 22:26:29 +000011835static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011836
11837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011838libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011839 typval_T *argvars;
11840 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011841 int type;
11842{
11843#ifdef FEAT_LIBCALL
11844 char_u *string_in;
11845 char_u **string_result;
11846 int nr_result;
11847#endif
11848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011849 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011850 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011851 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011852 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011854
11855 if (check_restricted() || check_secure())
11856 return;
11857
11858#ifdef FEAT_LIBCALL
11859 /* The first two args must be strings, otherwise its meaningless */
11860 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11861 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011862 string_in = NULL;
11863 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011864 string_in = argvars[2].vval.v_string;
11865 if (type == VAR_NUMBER)
11866 string_result = NULL;
11867 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011868 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011869 if (mch_libcall(argvars[0].vval.v_string,
11870 argvars[1].vval.v_string,
11871 string_in,
11872 argvars[2].vval.v_number,
11873 string_result,
11874 &nr_result) == OK
11875 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011876 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011877 }
11878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879}
11880
11881/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011882 * "libcall()" function
11883 */
11884 static void
11885f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011886 typval_T *argvars;
11887 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011888{
11889 libcall_common(argvars, rettv, VAR_STRING);
11890}
11891
11892/*
11893 * "libcallnr()" function
11894 */
11895 static void
11896f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011897 typval_T *argvars;
11898 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011899{
11900 libcall_common(argvars, rettv, VAR_NUMBER);
11901}
11902
11903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904 * "line(string)" function
11905 */
11906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011907f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011908 typval_T *argvars;
11909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910{
11911 linenr_T lnum = 0;
11912 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011913 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011915 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916 if (fp != NULL)
11917 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011918 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919}
11920
11921/*
11922 * "line2byte(lnum)" function
11923 */
11924/*ARGSUSED*/
11925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011926f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011927 typval_T *argvars;
11928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929{
11930#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011931 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932#else
11933 linenr_T lnum;
11934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011935 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011937 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011939 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11940 if (rettv->vval.v_number >= 0)
11941 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942#endif
11943}
11944
11945/*
11946 * "lispindent(lnum)" function
11947 */
11948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011949f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011950 typval_T *argvars;
11951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952{
11953#ifdef FEAT_LISP
11954 pos_T pos;
11955 linenr_T lnum;
11956
11957 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011958 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11960 {
11961 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011962 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963 curwin->w_cursor = pos;
11964 }
11965 else
11966#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011967 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968}
11969
11970/*
11971 * "localtime()" function
11972 */
11973/*ARGSUSED*/
11974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011975f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011976 typval_T *argvars;
11977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011979 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980}
11981
Bram Moolenaar33570922005-01-25 22:26:29 +000011982static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983
11984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011985get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011986 typval_T *argvars;
11987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 int exact;
11989{
11990 char_u *keys;
11991 char_u *which;
11992 char_u buf[NUMBUFLEN];
11993 char_u *keys_buf = NULL;
11994 char_u *rhs;
11995 int mode;
11996 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000011997 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998
11999 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012000 rettv->v_type = VAR_STRING;
12001 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012003 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004 if (*keys == NUL)
12005 return;
12006
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012007 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012009 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012010 if (argvars[2].v_type != VAR_UNKNOWN)
12011 abbr = get_tv_number(&argvars[2]);
12012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013 else
12014 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012015 if (which == NULL)
12016 return;
12017
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018 mode = get_map_mode(&which, 0);
12019
12020 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012021 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022 vim_free(keys_buf);
12023 if (rhs != NULL)
12024 {
12025 ga_init(&ga);
12026 ga.ga_itemsize = 1;
12027 ga.ga_growsize = 40;
12028
12029 while (*rhs != NUL)
12030 ga_concat(&ga, str2special(&rhs, FALSE));
12031
12032 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 }
12035}
12036
12037/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012038 * "map()" function
12039 */
12040 static void
12041f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012042 typval_T *argvars;
12043 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012044{
12045 filter_map(argvars, rettv, TRUE);
12046}
12047
12048/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012049 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 */
12051 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012052f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012053 typval_T *argvars;
12054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012056 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057}
12058
12059/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012060 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 */
12062 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012063f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012064 typval_T *argvars;
12065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012067 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068}
12069
Bram Moolenaar33570922005-01-25 22:26:29 +000012070static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071
12072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012073find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012074 typval_T *argvars;
12075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 int type;
12077{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012078 char_u *str = NULL;
12079 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 char_u *pat;
12081 regmatch_T regmatch;
12082 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012083 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084 char_u *save_cpo;
12085 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012086 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012087 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012088 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012089 list_T *l = NULL;
12090 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012091 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012092 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093
12094 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12095 save_cpo = p_cpo;
12096 p_cpo = (char_u *)"";
12097
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012098 rettv->vval.v_number = -1;
12099 if (type == 3)
12100 {
12101 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012102 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012103 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012104 }
12105 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012107 rettv->v_type = VAR_STRING;
12108 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012111 if (argvars[0].v_type == VAR_LIST)
12112 {
12113 if ((l = argvars[0].vval.v_list) == NULL)
12114 goto theend;
12115 li = l->lv_first;
12116 }
12117 else
12118 expr = str = get_tv_string(&argvars[0]);
12119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012120 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12121 if (pat == NULL)
12122 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012123
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012124 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012126 int error = FALSE;
12127
12128 start = get_tv_number_chk(&argvars[2], &error);
12129 if (error)
12130 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012131 if (l != NULL)
12132 {
12133 li = list_find(l, start);
12134 if (li == NULL)
12135 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012136 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012137 }
12138 else
12139 {
12140 if (start < 0)
12141 start = 0;
12142 if (start > (long)STRLEN(str))
12143 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012144 /* When "count" argument is there ignore matches before "start",
12145 * otherwise skip part of the string. Differs when pattern is "^"
12146 * or "\<". */
12147 if (argvars[3].v_type != VAR_UNKNOWN)
12148 startcol = start;
12149 else
12150 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012151 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012152
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012153 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012154 nth = get_tv_number_chk(&argvars[3], &error);
12155 if (error)
12156 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 }
12158
12159 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12160 if (regmatch.regprog != NULL)
12161 {
12162 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012163
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012164 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012165 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012166 if (l != NULL)
12167 {
12168 if (li == NULL)
12169 {
12170 match = FALSE;
12171 break;
12172 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012173 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012174 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012175 if (str == NULL)
12176 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012177 }
12178
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012179 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012180
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012181 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012182 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012183 if (l == NULL && !match)
12184 break;
12185
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012186 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012187 if (l != NULL)
12188 {
12189 li = li->li_next;
12190 ++idx;
12191 }
12192 else
12193 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012194#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012195 startcol = (colnr_T)(regmatch.startp[0]
12196 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012197#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012198 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012199#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012200 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012201 }
12202
12203 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012205 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012206 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012207 int i;
12208
12209 /* return list with matched string and submatches */
12210 for (i = 0; i < NSUBEXP; ++i)
12211 {
12212 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012213 {
12214 if (list_append_string(rettv->vval.v_list,
12215 (char_u *)"", 0) == FAIL)
12216 break;
12217 }
12218 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012219 regmatch.startp[i],
12220 (int)(regmatch.endp[i] - regmatch.startp[i]))
12221 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012222 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012223 }
12224 }
12225 else if (type == 2)
12226 {
12227 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012228 if (l != NULL)
12229 copy_tv(&li->li_tv, rettv);
12230 else
12231 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012233 }
12234 else if (l != NULL)
12235 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236 else
12237 {
12238 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012239 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 (varnumber_T)(regmatch.startp[0] - str);
12241 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012242 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012244 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245 }
12246 }
12247 vim_free(regmatch.regprog);
12248 }
12249
12250theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012251 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252 p_cpo = save_cpo;
12253}
12254
12255/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012256 * "match()" function
12257 */
12258 static void
12259f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012260 typval_T *argvars;
12261 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012262{
12263 find_some_match(argvars, rettv, 1);
12264}
12265
12266/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012267 * "matcharg()" function
12268 */
12269 static void
12270f_matcharg(argvars, rettv)
12271 typval_T *argvars;
12272 typval_T *rettv;
12273{
12274 if (rettv_list_alloc(rettv) == OK)
12275 {
12276#ifdef FEAT_SEARCH_EXTRA
12277 int mi = get_tv_number(&argvars[0]);
12278
12279 if (mi >= 1 && mi <= 3)
12280 {
12281 list_append_string(rettv->vval.v_list,
12282 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12283 list_append_string(rettv->vval.v_list,
12284 curwin->w_match_pat[mi - 1], -1);
12285 }
12286#endif
12287 }
12288}
12289
12290/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012291 * "matchend()" function
12292 */
12293 static void
12294f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012295 typval_T *argvars;
12296 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012297{
12298 find_some_match(argvars, rettv, 0);
12299}
12300
12301/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012302 * "matchlist()" function
12303 */
12304 static void
12305f_matchlist(argvars, rettv)
12306 typval_T *argvars;
12307 typval_T *rettv;
12308{
12309 find_some_match(argvars, rettv, 3);
12310}
12311
12312/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012313 * "matchstr()" function
12314 */
12315 static void
12316f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012317 typval_T *argvars;
12318 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012319{
12320 find_some_match(argvars, rettv, 2);
12321}
12322
Bram Moolenaar33570922005-01-25 22:26:29 +000012323static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012324
12325 static void
12326max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012327 typval_T *argvars;
12328 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012329 int domax;
12330{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012331 long n = 0;
12332 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012333 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012334
12335 if (argvars[0].v_type == VAR_LIST)
12336 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012337 list_T *l;
12338 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012339
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012340 l = argvars[0].vval.v_list;
12341 if (l != NULL)
12342 {
12343 li = l->lv_first;
12344 if (li != NULL)
12345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012346 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012347 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012348 {
12349 li = li->li_next;
12350 if (li == NULL)
12351 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012352 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012353 if (domax ? i > n : i < n)
12354 n = i;
12355 }
12356 }
12357 }
12358 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012359 else if (argvars[0].v_type == VAR_DICT)
12360 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012361 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012362 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012363 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012364 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012365
12366 d = argvars[0].vval.v_dict;
12367 if (d != NULL)
12368 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012369 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012370 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012371 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012372 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012373 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012374 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012375 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012376 if (first)
12377 {
12378 n = i;
12379 first = FALSE;
12380 }
12381 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012382 n = i;
12383 }
12384 }
12385 }
12386 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012387 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012388 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012389 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012390}
12391
12392/*
12393 * "max()" function
12394 */
12395 static void
12396f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012397 typval_T *argvars;
12398 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012399{
12400 max_min(argvars, rettv, TRUE);
12401}
12402
12403/*
12404 * "min()" function
12405 */
12406 static void
12407f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012408 typval_T *argvars;
12409 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012410{
12411 max_min(argvars, rettv, FALSE);
12412}
12413
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012414static int mkdir_recurse __ARGS((char_u *dir, int prot));
12415
12416/*
12417 * Create the directory in which "dir" is located, and higher levels when
12418 * needed.
12419 */
12420 static int
12421mkdir_recurse(dir, prot)
12422 char_u *dir;
12423 int prot;
12424{
12425 char_u *p;
12426 char_u *updir;
12427 int r = FAIL;
12428
12429 /* Get end of directory name in "dir".
12430 * We're done when it's "/" or "c:/". */
12431 p = gettail_sep(dir);
12432 if (p <= get_past_head(dir))
12433 return OK;
12434
12435 /* If the directory exists we're done. Otherwise: create it.*/
12436 updir = vim_strnsave(dir, (int)(p - dir));
12437 if (updir == NULL)
12438 return FAIL;
12439 if (mch_isdir(updir))
12440 r = OK;
12441 else if (mkdir_recurse(updir, prot) == OK)
12442 r = vim_mkdir_emsg(updir, prot);
12443 vim_free(updir);
12444 return r;
12445}
12446
12447#ifdef vim_mkdir
12448/*
12449 * "mkdir()" function
12450 */
12451 static void
12452f_mkdir(argvars, rettv)
12453 typval_T *argvars;
12454 typval_T *rettv;
12455{
12456 char_u *dir;
12457 char_u buf[NUMBUFLEN];
12458 int prot = 0755;
12459
12460 rettv->vval.v_number = FAIL;
12461 if (check_restricted() || check_secure())
12462 return;
12463
12464 dir = get_tv_string_buf(&argvars[0], buf);
12465 if (argvars[1].v_type != VAR_UNKNOWN)
12466 {
12467 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012468 prot = get_tv_number_chk(&argvars[2], NULL);
12469 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012470 mkdir_recurse(dir, prot);
12471 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012472 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012473}
12474#endif
12475
Bram Moolenaar0d660222005-01-07 21:51:51 +000012476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477 * "mode()" function
12478 */
12479/*ARGSUSED*/
12480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012481f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012482 typval_T *argvars;
12483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484{
12485 char_u buf[2];
12486
12487#ifdef FEAT_VISUAL
12488 if (VIsual_active)
12489 {
12490 if (VIsual_select)
12491 buf[0] = VIsual_mode + 's' - 'v';
12492 else
12493 buf[0] = VIsual_mode;
12494 }
12495 else
12496#endif
12497 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12498 buf[0] = 'r';
12499 else if (State & INSERT)
12500 {
12501 if (State & REPLACE_FLAG)
12502 buf[0] = 'R';
12503 else
12504 buf[0] = 'i';
12505 }
12506 else if (State & CMDLINE)
12507 buf[0] = 'c';
12508 else
12509 buf[0] = 'n';
12510
12511 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012512 rettv->vval.v_string = vim_strsave(buf);
12513 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514}
12515
12516/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012517 * "nextnonblank()" function
12518 */
12519 static void
12520f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012521 typval_T *argvars;
12522 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012523{
12524 linenr_T lnum;
12525
12526 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12527 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012528 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012529 {
12530 lnum = 0;
12531 break;
12532 }
12533 if (*skipwhite(ml_get(lnum)) != NUL)
12534 break;
12535 }
12536 rettv->vval.v_number = lnum;
12537}
12538
12539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540 * "nr2char()" function
12541 */
12542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012543f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012544 typval_T *argvars;
12545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546{
12547 char_u buf[NUMBUFLEN];
12548
12549#ifdef FEAT_MBYTE
12550 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012551 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552 else
12553#endif
12554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012555 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 buf[1] = NUL;
12557 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012558 rettv->v_type = VAR_STRING;
12559 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012560}
12561
12562/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012563 * "pathshorten()" function
12564 */
12565 static void
12566f_pathshorten(argvars, rettv)
12567 typval_T *argvars;
12568 typval_T *rettv;
12569{
12570 char_u *p;
12571
12572 rettv->v_type = VAR_STRING;
12573 p = get_tv_string_chk(&argvars[0]);
12574 if (p == NULL)
12575 rettv->vval.v_string = NULL;
12576 else
12577 {
12578 p = vim_strsave(p);
12579 rettv->vval.v_string = p;
12580 if (p != NULL)
12581 shorten_dir(p);
12582 }
12583}
12584
12585/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012586 * "prevnonblank()" function
12587 */
12588 static void
12589f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012590 typval_T *argvars;
12591 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012592{
12593 linenr_T lnum;
12594
12595 lnum = get_tv_lnum(argvars);
12596 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12597 lnum = 0;
12598 else
12599 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12600 --lnum;
12601 rettv->vval.v_number = lnum;
12602}
12603
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012604#ifdef HAVE_STDARG_H
12605/* This dummy va_list is here because:
12606 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12607 * - locally in the function results in a "used before set" warning
12608 * - using va_start() to initialize it gives "function with fixed args" error */
12609static va_list ap;
12610#endif
12611
Bram Moolenaar8c711452005-01-14 21:53:12 +000012612/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012613 * "printf()" function
12614 */
12615 static void
12616f_printf(argvars, rettv)
12617 typval_T *argvars;
12618 typval_T *rettv;
12619{
12620 rettv->v_type = VAR_STRING;
12621 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012622#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012623 {
12624 char_u buf[NUMBUFLEN];
12625 int len;
12626 char_u *s;
12627 int saved_did_emsg = did_emsg;
12628 char *fmt;
12629
12630 /* Get the required length, allocate the buffer and do it for real. */
12631 did_emsg = FALSE;
12632 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012633 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012634 if (!did_emsg)
12635 {
12636 s = alloc(len + 1);
12637 if (s != NULL)
12638 {
12639 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012640 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012641 }
12642 }
12643 did_emsg |= saved_did_emsg;
12644 }
12645#endif
12646}
12647
12648/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012649 * "pumvisible()" function
12650 */
12651/*ARGSUSED*/
12652 static void
12653f_pumvisible(argvars, rettv)
12654 typval_T *argvars;
12655 typval_T *rettv;
12656{
12657 rettv->vval.v_number = 0;
12658#ifdef FEAT_INS_EXPAND
12659 if (pum_visible())
12660 rettv->vval.v_number = 1;
12661#endif
12662}
12663
12664/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012665 * "range()" function
12666 */
12667 static void
12668f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012669 typval_T *argvars;
12670 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012671{
12672 long start;
12673 long end;
12674 long stride = 1;
12675 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012676 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012678 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012679 if (argvars[1].v_type == VAR_UNKNOWN)
12680 {
12681 end = start - 1;
12682 start = 0;
12683 }
12684 else
12685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012686 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012687 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012688 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012689 }
12690
12691 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012692 if (error)
12693 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012694 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012695 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012696 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012697 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012698 else
12699 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012700 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012701 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012702 if (list_append_number(rettv->vval.v_list,
12703 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012704 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012705 }
12706}
12707
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012708/*
12709 * "readfile()" function
12710 */
12711 static void
12712f_readfile(argvars, rettv)
12713 typval_T *argvars;
12714 typval_T *rettv;
12715{
12716 int binary = FALSE;
12717 char_u *fname;
12718 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012719 listitem_T *li;
12720#define FREAD_SIZE 200 /* optimized for text lines */
12721 char_u buf[FREAD_SIZE];
12722 int readlen; /* size of last fread() */
12723 int buflen; /* nr of valid chars in buf[] */
12724 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12725 int tolist; /* first byte in buf[] still to be put in list */
12726 int chop; /* how many CR to chop off */
12727 char_u *prev = NULL; /* previously read bytes, if any */
12728 int prevlen = 0; /* length of "prev" if not NULL */
12729 char_u *s;
12730 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012731 long maxline = MAXLNUM;
12732 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012733
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012734 if (argvars[1].v_type != VAR_UNKNOWN)
12735 {
12736 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12737 binary = TRUE;
12738 if (argvars[2].v_type != VAR_UNKNOWN)
12739 maxline = get_tv_number(&argvars[2]);
12740 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012741
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012742 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012743 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012744
12745 /* Always open the file in binary mode, library functions have a mind of
12746 * their own about CR-LF conversion. */
12747 fname = get_tv_string(&argvars[0]);
12748 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12749 {
12750 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12751 return;
12752 }
12753
12754 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012755 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012756 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012757 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012758 buflen = filtd + readlen;
12759 tolist = 0;
12760 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12761 {
12762 if (buf[filtd] == '\n' || readlen <= 0)
12763 {
12764 /* Only when in binary mode add an empty list item when the
12765 * last line ends in a '\n'. */
12766 if (!binary && readlen == 0 && filtd == 0)
12767 break;
12768
12769 /* Found end-of-line or end-of-file: add a text line to the
12770 * list. */
12771 chop = 0;
12772 if (!binary)
12773 while (filtd - chop - 1 >= tolist
12774 && buf[filtd - chop - 1] == '\r')
12775 ++chop;
12776 len = filtd - tolist - chop;
12777 if (prev == NULL)
12778 s = vim_strnsave(buf + tolist, len);
12779 else
12780 {
12781 s = alloc((unsigned)(prevlen + len + 1));
12782 if (s != NULL)
12783 {
12784 mch_memmove(s, prev, prevlen);
12785 vim_free(prev);
12786 prev = NULL;
12787 mch_memmove(s + prevlen, buf + tolist, len);
12788 s[prevlen + len] = NUL;
12789 }
12790 }
12791 tolist = filtd + 1;
12792
12793 li = listitem_alloc();
12794 if (li == NULL)
12795 {
12796 vim_free(s);
12797 break;
12798 }
12799 li->li_tv.v_type = VAR_STRING;
12800 li->li_tv.v_lock = 0;
12801 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012802 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012803
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012804 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012805 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012806 if (readlen <= 0)
12807 break;
12808 }
12809 else if (buf[filtd] == NUL)
12810 buf[filtd] = '\n';
12811 }
12812 if (readlen <= 0)
12813 break;
12814
12815 if (tolist == 0)
12816 {
12817 /* "buf" is full, need to move text to an allocated buffer */
12818 if (prev == NULL)
12819 {
12820 prev = vim_strnsave(buf, buflen);
12821 prevlen = buflen;
12822 }
12823 else
12824 {
12825 s = alloc((unsigned)(prevlen + buflen));
12826 if (s != NULL)
12827 {
12828 mch_memmove(s, prev, prevlen);
12829 mch_memmove(s + prevlen, buf, buflen);
12830 vim_free(prev);
12831 prev = s;
12832 prevlen += buflen;
12833 }
12834 }
12835 filtd = 0;
12836 }
12837 else
12838 {
12839 mch_memmove(buf, buf + tolist, buflen - tolist);
12840 filtd -= tolist;
12841 }
12842 }
12843
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012844 /*
12845 * For a negative line count use only the lines at the end of the file,
12846 * free the rest.
12847 */
12848 if (maxline < 0)
12849 while (cnt > -maxline)
12850 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012851 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012852 --cnt;
12853 }
12854
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012855 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012856 fclose(fd);
12857}
12858
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012859#if defined(FEAT_RELTIME)
12860static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12861
12862/*
12863 * Convert a List to proftime_T.
12864 * Return FAIL when there is something wrong.
12865 */
12866 static int
12867list2proftime(arg, tm)
12868 typval_T *arg;
12869 proftime_T *tm;
12870{
12871 long n1, n2;
12872 int error = FALSE;
12873
12874 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12875 || arg->vval.v_list->lv_len != 2)
12876 return FAIL;
12877 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12878 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12879# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012880 tm->HighPart = n1;
12881 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012882# else
12883 tm->tv_sec = n1;
12884 tm->tv_usec = n2;
12885# endif
12886 return error ? FAIL : OK;
12887}
12888#endif /* FEAT_RELTIME */
12889
12890/*
12891 * "reltime()" function
12892 */
12893 static void
12894f_reltime(argvars, rettv)
12895 typval_T *argvars;
12896 typval_T *rettv;
12897{
12898#ifdef FEAT_RELTIME
12899 proftime_T res;
12900 proftime_T start;
12901
12902 if (argvars[0].v_type == VAR_UNKNOWN)
12903 {
12904 /* No arguments: get current time. */
12905 profile_start(&res);
12906 }
12907 else if (argvars[1].v_type == VAR_UNKNOWN)
12908 {
12909 if (list2proftime(&argvars[0], &res) == FAIL)
12910 return;
12911 profile_end(&res);
12912 }
12913 else
12914 {
12915 /* Two arguments: compute the difference. */
12916 if (list2proftime(&argvars[0], &start) == FAIL
12917 || list2proftime(&argvars[1], &res) == FAIL)
12918 return;
12919 profile_sub(&res, &start);
12920 }
12921
12922 if (rettv_list_alloc(rettv) == OK)
12923 {
12924 long n1, n2;
12925
12926# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012927 n1 = res.HighPart;
12928 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012929# else
12930 n1 = res.tv_sec;
12931 n2 = res.tv_usec;
12932# endif
12933 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
12934 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
12935 }
12936#endif
12937}
12938
12939/*
12940 * "reltimestr()" function
12941 */
12942 static void
12943f_reltimestr(argvars, rettv)
12944 typval_T *argvars;
12945 typval_T *rettv;
12946{
12947#ifdef FEAT_RELTIME
12948 proftime_T tm;
12949#endif
12950
12951 rettv->v_type = VAR_STRING;
12952 rettv->vval.v_string = NULL;
12953#ifdef FEAT_RELTIME
12954 if (list2proftime(&argvars[0], &tm) == OK)
12955 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
12956#endif
12957}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012958
Bram Moolenaar0d660222005-01-07 21:51:51 +000012959#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12960static void make_connection __ARGS((void));
12961static int check_connection __ARGS((void));
12962
12963 static void
12964make_connection()
12965{
12966 if (X_DISPLAY == NULL
12967# ifdef FEAT_GUI
12968 && !gui.in_use
12969# endif
12970 )
12971 {
12972 x_force_connect = TRUE;
12973 setup_term_clip();
12974 x_force_connect = FALSE;
12975 }
12976}
12977
12978 static int
12979check_connection()
12980{
12981 make_connection();
12982 if (X_DISPLAY == NULL)
12983 {
12984 EMSG(_("E240: No connection to Vim server"));
12985 return FAIL;
12986 }
12987 return OK;
12988}
12989#endif
12990
12991#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012992static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012993
12994 static void
12995remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012996 typval_T *argvars;
12997 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012998 int expr;
12999{
13000 char_u *server_name;
13001 char_u *keys;
13002 char_u *r = NULL;
13003 char_u buf[NUMBUFLEN];
13004# ifdef WIN32
13005 HWND w;
13006# else
13007 Window w;
13008# endif
13009
13010 if (check_restricted() || check_secure())
13011 return;
13012
13013# ifdef FEAT_X11
13014 if (check_connection() == FAIL)
13015 return;
13016# endif
13017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013018 server_name = get_tv_string_chk(&argvars[0]);
13019 if (server_name == NULL)
13020 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013021 keys = get_tv_string_buf(&argvars[1], buf);
13022# ifdef WIN32
13023 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13024# else
13025 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13026 < 0)
13027# endif
13028 {
13029 if (r != NULL)
13030 EMSG(r); /* sending worked but evaluation failed */
13031 else
13032 EMSG2(_("E241: Unable to send to %s"), server_name);
13033 return;
13034 }
13035
13036 rettv->vval.v_string = r;
13037
13038 if (argvars[2].v_type != VAR_UNKNOWN)
13039 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013040 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013041 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013042 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013043
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013044 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013045 v.di_tv.v_type = VAR_STRING;
13046 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013047 idvar = get_tv_string_chk(&argvars[2]);
13048 if (idvar != NULL)
13049 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013050 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013051 }
13052}
13053#endif
13054
13055/*
13056 * "remote_expr()" function
13057 */
13058/*ARGSUSED*/
13059 static void
13060f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013061 typval_T *argvars;
13062 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013063{
13064 rettv->v_type = VAR_STRING;
13065 rettv->vval.v_string = NULL;
13066#ifdef FEAT_CLIENTSERVER
13067 remote_common(argvars, rettv, TRUE);
13068#endif
13069}
13070
13071/*
13072 * "remote_foreground()" function
13073 */
13074/*ARGSUSED*/
13075 static void
13076f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013077 typval_T *argvars;
13078 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013079{
13080 rettv->vval.v_number = 0;
13081#ifdef FEAT_CLIENTSERVER
13082# ifdef WIN32
13083 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013084 {
13085 char_u *server_name = get_tv_string_chk(&argvars[0]);
13086
13087 if (server_name != NULL)
13088 serverForeground(server_name);
13089 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013090# else
13091 /* Send a foreground() expression to the server. */
13092 argvars[1].v_type = VAR_STRING;
13093 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13094 argvars[2].v_type = VAR_UNKNOWN;
13095 remote_common(argvars, rettv, TRUE);
13096 vim_free(argvars[1].vval.v_string);
13097# endif
13098#endif
13099}
13100
13101/*ARGSUSED*/
13102 static void
13103f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013104 typval_T *argvars;
13105 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013106{
13107#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013108 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013109 char_u *s = NULL;
13110# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013111 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013112# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013113 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013114
13115 if (check_restricted() || check_secure())
13116 {
13117 rettv->vval.v_number = -1;
13118 return;
13119 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013120 serverid = get_tv_string_chk(&argvars[0]);
13121 if (serverid == NULL)
13122 {
13123 rettv->vval.v_number = -1;
13124 return; /* type error; errmsg already given */
13125 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013126# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013127 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013128 if (n == 0)
13129 rettv->vval.v_number = -1;
13130 else
13131 {
13132 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13133 rettv->vval.v_number = (s != NULL);
13134 }
13135# else
13136 rettv->vval.v_number = 0;
13137 if (check_connection() == FAIL)
13138 return;
13139
13140 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013141 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013142# endif
13143
13144 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13145 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013146 char_u *retvar;
13147
Bram Moolenaar33570922005-01-25 22:26:29 +000013148 v.di_tv.v_type = VAR_STRING;
13149 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013150 retvar = get_tv_string_chk(&argvars[1]);
13151 if (retvar != NULL)
13152 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013153 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013154 }
13155#else
13156 rettv->vval.v_number = -1;
13157#endif
13158}
13159
13160/*ARGSUSED*/
13161 static void
13162f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013163 typval_T *argvars;
13164 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013165{
13166 char_u *r = NULL;
13167
13168#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013169 char_u *serverid = get_tv_string_chk(&argvars[0]);
13170
13171 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013172 {
13173# ifdef WIN32
13174 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013175 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013176
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013177 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013178 if (n != 0)
13179 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13180 if (r == NULL)
13181# else
13182 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013183 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013184# endif
13185 EMSG(_("E277: Unable to read a server reply"));
13186 }
13187#endif
13188 rettv->v_type = VAR_STRING;
13189 rettv->vval.v_string = r;
13190}
13191
13192/*
13193 * "remote_send()" function
13194 */
13195/*ARGSUSED*/
13196 static void
13197f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013198 typval_T *argvars;
13199 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013200{
13201 rettv->v_type = VAR_STRING;
13202 rettv->vval.v_string = NULL;
13203#ifdef FEAT_CLIENTSERVER
13204 remote_common(argvars, rettv, FALSE);
13205#endif
13206}
13207
13208/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013209 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013210 */
13211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013212f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013213 typval_T *argvars;
13214 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013215{
Bram Moolenaar33570922005-01-25 22:26:29 +000013216 list_T *l;
13217 listitem_T *item, *item2;
13218 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013219 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013220 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013221 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013222 dict_T *d;
13223 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013224
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013225 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013226 if (argvars[0].v_type == VAR_DICT)
13227 {
13228 if (argvars[2].v_type != VAR_UNKNOWN)
13229 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013230 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000013231 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013233 key = get_tv_string_chk(&argvars[1]);
13234 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013236 di = dict_find(d, key, -1);
13237 if (di == NULL)
13238 EMSG2(_(e_dictkey), key);
13239 else
13240 {
13241 *rettv = di->di_tv;
13242 init_tv(&di->di_tv);
13243 dictitem_remove(d, di);
13244 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013245 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013246 }
13247 }
13248 else if (argvars[0].v_type != VAR_LIST)
13249 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013250 else if ((l = argvars[0].vval.v_list) != NULL
13251 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013253 int error = FALSE;
13254
13255 idx = get_tv_number_chk(&argvars[1], &error);
13256 if (error)
13257 ; /* type error: do nothing, errmsg already given */
13258 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013259 EMSGN(_(e_listidx), idx);
13260 else
13261 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013262 if (argvars[2].v_type == VAR_UNKNOWN)
13263 {
13264 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013265 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013266 *rettv = item->li_tv;
13267 vim_free(item);
13268 }
13269 else
13270 {
13271 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013272 end = get_tv_number_chk(&argvars[2], &error);
13273 if (error)
13274 ; /* type error: do nothing */
13275 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013276 EMSGN(_(e_listidx), end);
13277 else
13278 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013279 int cnt = 0;
13280
13281 for (li = item; li != NULL; li = li->li_next)
13282 {
13283 ++cnt;
13284 if (li == item2)
13285 break;
13286 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013287 if (li == NULL) /* didn't find "item2" after "item" */
13288 EMSG(_(e_invrange));
13289 else
13290 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013291 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013292 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013293 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013294 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013295 l->lv_first = item;
13296 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013297 item->li_prev = NULL;
13298 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013299 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013300 }
13301 }
13302 }
13303 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013304 }
13305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306}
13307
13308/*
13309 * "rename({from}, {to})" function
13310 */
13311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013312f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013313 typval_T *argvars;
13314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315{
13316 char_u buf[NUMBUFLEN];
13317
13318 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013319 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013321 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13322 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323}
13324
13325/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013326 * "repeat()" function
13327 */
13328/*ARGSUSED*/
13329 static void
13330f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013331 typval_T *argvars;
13332 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013333{
13334 char_u *p;
13335 int n;
13336 int slen;
13337 int len;
13338 char_u *r;
13339 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013340
13341 n = get_tv_number(&argvars[1]);
13342 if (argvars[0].v_type == VAR_LIST)
13343 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013344 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013345 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013346 if (list_extend(rettv->vval.v_list,
13347 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013348 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013349 }
13350 else
13351 {
13352 p = get_tv_string(&argvars[0]);
13353 rettv->v_type = VAR_STRING;
13354 rettv->vval.v_string = NULL;
13355
13356 slen = (int)STRLEN(p);
13357 len = slen * n;
13358 if (len <= 0)
13359 return;
13360
13361 r = alloc(len + 1);
13362 if (r != NULL)
13363 {
13364 for (i = 0; i < n; i++)
13365 mch_memmove(r + i * slen, p, (size_t)slen);
13366 r[len] = NUL;
13367 }
13368
13369 rettv->vval.v_string = r;
13370 }
13371}
13372
13373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374 * "resolve()" function
13375 */
13376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013377f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013378 typval_T *argvars;
13379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380{
13381 char_u *p;
13382
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013383 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384#ifdef FEAT_SHORTCUT
13385 {
13386 char_u *v = NULL;
13387
13388 v = mch_resolve_shortcut(p);
13389 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013390 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013392 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393 }
13394#else
13395# ifdef HAVE_READLINK
13396 {
13397 char_u buf[MAXPATHL + 1];
13398 char_u *cpy;
13399 int len;
13400 char_u *remain = NULL;
13401 char_u *q;
13402 int is_relative_to_current = FALSE;
13403 int has_trailing_pathsep = FALSE;
13404 int limit = 100;
13405
13406 p = vim_strsave(p);
13407
13408 if (p[0] == '.' && (vim_ispathsep(p[1])
13409 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13410 is_relative_to_current = TRUE;
13411
13412 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013413 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414 has_trailing_pathsep = TRUE;
13415
13416 q = getnextcomp(p);
13417 if (*q != NUL)
13418 {
13419 /* Separate the first path component in "p", and keep the
13420 * remainder (beginning with the path separator). */
13421 remain = vim_strsave(q - 1);
13422 q[-1] = NUL;
13423 }
13424
13425 for (;;)
13426 {
13427 for (;;)
13428 {
13429 len = readlink((char *)p, (char *)buf, MAXPATHL);
13430 if (len <= 0)
13431 break;
13432 buf[len] = NUL;
13433
13434 if (limit-- == 0)
13435 {
13436 vim_free(p);
13437 vim_free(remain);
13438 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013439 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 goto fail;
13441 }
13442
13443 /* Ensure that the result will have a trailing path separator
13444 * if the argument has one. */
13445 if (remain == NULL && has_trailing_pathsep)
13446 add_pathsep(buf);
13447
13448 /* Separate the first path component in the link value and
13449 * concatenate the remainders. */
13450 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13451 if (*q != NUL)
13452 {
13453 if (remain == NULL)
13454 remain = vim_strsave(q - 1);
13455 else
13456 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013457 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 if (cpy != NULL)
13459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 vim_free(remain);
13461 remain = cpy;
13462 }
13463 }
13464 q[-1] = NUL;
13465 }
13466
13467 q = gettail(p);
13468 if (q > p && *q == NUL)
13469 {
13470 /* Ignore trailing path separator. */
13471 q[-1] = NUL;
13472 q = gettail(p);
13473 }
13474 if (q > p && !mch_isFullName(buf))
13475 {
13476 /* symlink is relative to directory of argument */
13477 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13478 if (cpy != NULL)
13479 {
13480 STRCPY(cpy, p);
13481 STRCPY(gettail(cpy), buf);
13482 vim_free(p);
13483 p = cpy;
13484 }
13485 }
13486 else
13487 {
13488 vim_free(p);
13489 p = vim_strsave(buf);
13490 }
13491 }
13492
13493 if (remain == NULL)
13494 break;
13495
13496 /* Append the first path component of "remain" to "p". */
13497 q = getnextcomp(remain + 1);
13498 len = q - remain - (*q != NUL);
13499 cpy = vim_strnsave(p, STRLEN(p) + len);
13500 if (cpy != NULL)
13501 {
13502 STRNCAT(cpy, remain, len);
13503 vim_free(p);
13504 p = cpy;
13505 }
13506 /* Shorten "remain". */
13507 if (*q != NUL)
13508 STRCPY(remain, q - 1);
13509 else
13510 {
13511 vim_free(remain);
13512 remain = NULL;
13513 }
13514 }
13515
13516 /* If the result is a relative path name, make it explicitly relative to
13517 * the current directory if and only if the argument had this form. */
13518 if (!vim_ispathsep(*p))
13519 {
13520 if (is_relative_to_current
13521 && *p != NUL
13522 && !(p[0] == '.'
13523 && (p[1] == NUL
13524 || vim_ispathsep(p[1])
13525 || (p[1] == '.'
13526 && (p[2] == NUL
13527 || vim_ispathsep(p[2]))))))
13528 {
13529 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013530 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 if (cpy != NULL)
13532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533 vim_free(p);
13534 p = cpy;
13535 }
13536 }
13537 else if (!is_relative_to_current)
13538 {
13539 /* Strip leading "./". */
13540 q = p;
13541 while (q[0] == '.' && vim_ispathsep(q[1]))
13542 q += 2;
13543 if (q > p)
13544 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13545 }
13546 }
13547
13548 /* Ensure that the result will have no trailing path separator
13549 * if the argument had none. But keep "/" or "//". */
13550 if (!has_trailing_pathsep)
13551 {
13552 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013553 if (after_pathsep(p, q))
13554 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555 }
13556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013557 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558 }
13559# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013560 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561# endif
13562#endif
13563
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013564 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565
13566#ifdef HAVE_READLINK
13567fail:
13568#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013569 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570}
13571
13572/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013573 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574 */
13575 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013576f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013577 typval_T *argvars;
13578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579{
Bram Moolenaar33570922005-01-25 22:26:29 +000013580 list_T *l;
13581 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582
Bram Moolenaar0d660222005-01-07 21:51:51 +000013583 rettv->vval.v_number = 0;
13584 if (argvars[0].v_type != VAR_LIST)
13585 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013586 else if ((l = argvars[0].vval.v_list) != NULL
13587 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013588 {
13589 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013590 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013591 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013592 while (li != NULL)
13593 {
13594 ni = li->li_prev;
13595 list_append(l, li);
13596 li = ni;
13597 }
13598 rettv->vval.v_list = l;
13599 rettv->v_type = VAR_LIST;
13600 ++l->lv_refcount;
13601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602}
13603
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013604#define SP_NOMOVE 0x01 /* don't move cursor */
13605#define SP_REPEAT 0x02 /* repeat to find outer pair */
13606#define SP_RETCOUNT 0x04 /* return matchcount */
13607#define SP_SETPCMARK 0x08 /* set previous context mark */
13608#define SP_START 0x10 /* accept match at start position */
13609#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13610#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013611
Bram Moolenaar33570922005-01-25 22:26:29 +000013612static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013613
13614/*
13615 * Get flags for a search function.
13616 * Possibly sets "p_ws".
13617 * Returns BACKWARD, FORWARD or zero (for an error).
13618 */
13619 static int
13620get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013621 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013622 int *flagsp;
13623{
13624 int dir = FORWARD;
13625 char_u *flags;
13626 char_u nbuf[NUMBUFLEN];
13627 int mask;
13628
13629 if (varp->v_type != VAR_UNKNOWN)
13630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013631 flags = get_tv_string_buf_chk(varp, nbuf);
13632 if (flags == NULL)
13633 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013634 while (*flags != NUL)
13635 {
13636 switch (*flags)
13637 {
13638 case 'b': dir = BACKWARD; break;
13639 case 'w': p_ws = TRUE; break;
13640 case 'W': p_ws = FALSE; break;
13641 default: mask = 0;
13642 if (flagsp != NULL)
13643 switch (*flags)
13644 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013645 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013646 case 'e': mask = SP_END; break;
13647 case 'm': mask = SP_RETCOUNT; break;
13648 case 'n': mask = SP_NOMOVE; break;
13649 case 'p': mask = SP_SUBPAT; break;
13650 case 'r': mask = SP_REPEAT; break;
13651 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013652 }
13653 if (mask == 0)
13654 {
13655 EMSG2(_(e_invarg2), flags);
13656 dir = 0;
13657 }
13658 else
13659 *flagsp |= mask;
13660 }
13661 if (dir == 0)
13662 break;
13663 ++flags;
13664 }
13665 }
13666 return dir;
13667}
13668
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013670 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013672 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013673search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013674 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013675 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013676 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013678 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679 char_u *pat;
13680 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013681 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682 int save_p_ws = p_ws;
13683 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013684 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013685 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013686 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013687 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013689 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013690 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013691 if (dir == 0)
13692 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013693 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013694 if (flags & SP_START)
13695 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013696 if (flags & SP_END)
13697 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013698
13699 /* Optional extra argument: line number to stop searching. */
13700 if (argvars[1].v_type != VAR_UNKNOWN
13701 && argvars[2].v_type != VAR_UNKNOWN)
13702 {
13703 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13704 if (lnum_stop < 0)
13705 goto theend;
13706 }
13707
Bram Moolenaar231334e2005-07-25 20:46:57 +000013708 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013709 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013710 * Check to make sure only those flags are set.
13711 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13712 * flags cannot be set. Check for that condition also.
13713 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013714 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013715 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013716 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013717 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013718 goto theend;
13719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013721 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013722 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13723 options, RE_SEARCH, (linenr_T)lnum_stop);
13724 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013726 if (flags & SP_SUBPAT)
13727 retval = subpatnum;
13728 else
13729 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013730 if (flags & SP_SETPCMARK)
13731 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013733 if (match_pos != NULL)
13734 {
13735 /* Store the match cursor position */
13736 match_pos->lnum = pos.lnum;
13737 match_pos->col = pos.col + 1;
13738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739 /* "/$" will put the cursor after the end of the line, may need to
13740 * correct that here */
13741 check_cursor();
13742 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013743
13744 /* If 'n' flag is used: restore cursor position. */
13745 if (flags & SP_NOMOVE)
13746 curwin->w_cursor = save_cursor;
13747theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013749
13750 return retval;
13751}
13752
13753/*
13754 * "search()" function
13755 */
13756 static void
13757f_search(argvars, rettv)
13758 typval_T *argvars;
13759 typval_T *rettv;
13760{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013761 int flags = 0;
13762
13763 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764}
13765
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013767 * "searchdecl()" function
13768 */
13769 static void
13770f_searchdecl(argvars, rettv)
13771 typval_T *argvars;
13772 typval_T *rettv;
13773{
13774 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013775 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013776 int error = FALSE;
13777 char_u *name;
13778
13779 rettv->vval.v_number = 1; /* default: FAIL */
13780
13781 name = get_tv_string_chk(&argvars[0]);
13782 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013783 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013784 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013785 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13786 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13787 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013788 if (!error && name != NULL)
13789 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013790 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013791}
13792
13793/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013794 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013796 static int
13797searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013798 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013799 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800{
13801 char_u *spat, *mpat, *epat;
13802 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804 int dir;
13805 int flags = 0;
13806 char_u nbuf1[NUMBUFLEN];
13807 char_u nbuf2[NUMBUFLEN];
13808 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013809 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013810 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013813 spat = get_tv_string_chk(&argvars[0]);
13814 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13815 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13816 if (spat == NULL || mpat == NULL || epat == NULL)
13817 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 /* Handle the optional fourth argument: flags */
13820 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013821 if (dir == 0)
13822 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013823
13824 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013825 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13826 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013827 if ((flags & (SP_END | SP_SUBPAT)) != 0
13828 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013829 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013830 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013831 goto theend;
13832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013834 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013835 if (argvars[3].v_type == VAR_UNKNOWN
13836 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 skip = (char_u *)"";
13838 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013840 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013841 if (argvars[5].v_type != VAR_UNKNOWN)
13842 {
13843 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13844 if (lnum_stop < 0)
13845 goto theend;
13846 }
13847 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013848 if (skip == NULL)
13849 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013851 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13852 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013853
13854theend:
13855 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013856
13857 return retval;
13858}
13859
13860/*
13861 * "searchpair()" function
13862 */
13863 static void
13864f_searchpair(argvars, rettv)
13865 typval_T *argvars;
13866 typval_T *rettv;
13867{
13868 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13869}
13870
13871/*
13872 * "searchpairpos()" function
13873 */
13874 static void
13875f_searchpairpos(argvars, rettv)
13876 typval_T *argvars;
13877 typval_T *rettv;
13878{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013879 pos_T match_pos;
13880 int lnum = 0;
13881 int col = 0;
13882
13883 rettv->vval.v_number = 0;
13884
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013885 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013886 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013887
13888 if (searchpair_cmn(argvars, &match_pos) > 0)
13889 {
13890 lnum = match_pos.lnum;
13891 col = match_pos.col;
13892 }
13893
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013894 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13895 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013896}
13897
13898/*
13899 * Search for a start/middle/end thing.
13900 * Used by searchpair(), see its documentation for the details.
13901 * Returns 0 or -1 for no match,
13902 */
13903 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013904do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013905 char_u *spat; /* start pattern */
13906 char_u *mpat; /* middle pattern */
13907 char_u *epat; /* end pattern */
13908 int dir; /* BACKWARD or FORWARD */
13909 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013910 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013911 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013912 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013913{
13914 char_u *save_cpo;
13915 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13916 long retval = 0;
13917 pos_T pos;
13918 pos_T firstpos;
13919 pos_T foundpos;
13920 pos_T save_cursor;
13921 pos_T save_pos;
13922 int n;
13923 int r;
13924 int nest = 1;
13925 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013926 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013927
13928 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13929 save_cpo = p_cpo;
13930 p_cpo = (char_u *)"";
13931
13932 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13933 * start/middle/end (pat3, for the top pair). */
13934 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13935 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13936 if (pat2 == NULL || pat3 == NULL)
13937 goto theend;
13938 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13939 if (*mpat == NUL)
13940 STRCPY(pat3, pat2);
13941 else
13942 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13943 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013944 if (flags & SP_START)
13945 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013946
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 save_cursor = curwin->w_cursor;
13948 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013949 clearpos(&firstpos);
13950 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 pat = pat3;
13952 for (;;)
13953 {
13954 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013955 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13957 /* didn't find it or found the first match again: FAIL */
13958 break;
13959
13960 if (firstpos.lnum == 0)
13961 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013962 if (equalpos(pos, foundpos))
13963 {
13964 /* Found the same position again. Can happen with a pattern that
13965 * has "\zs" at the end and searching backwards. Advance one
13966 * character and try again. */
13967 if (dir == BACKWARD)
13968 decl(&pos);
13969 else
13970 incl(&pos);
13971 }
13972 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973
13974 /* If the skip pattern matches, ignore this match. */
13975 if (*skip != NUL)
13976 {
13977 save_pos = curwin->w_cursor;
13978 curwin->w_cursor = pos;
13979 r = eval_to_bool(skip, &err, NULL, FALSE);
13980 curwin->w_cursor = save_pos;
13981 if (err)
13982 {
13983 /* Evaluating {skip} caused an error, break here. */
13984 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013985 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986 break;
13987 }
13988 if (r)
13989 continue;
13990 }
13991
13992 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13993 {
13994 /* Found end when searching backwards or start when searching
13995 * forward: nested pair. */
13996 ++nest;
13997 pat = pat2; /* nested, don't search for middle */
13998 }
13999 else
14000 {
14001 /* Found end when searching forward or start when searching
14002 * backward: end of (nested) pair; or found middle in outer pair. */
14003 if (--nest == 1)
14004 pat = pat3; /* outer level, search for middle */
14005 }
14006
14007 if (nest == 0)
14008 {
14009 /* Found the match: return matchcount or line number. */
14010 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014011 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014013 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014014 if (flags & SP_SETPCMARK)
14015 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 curwin->w_cursor = pos;
14017 if (!(flags & SP_REPEAT))
14018 break;
14019 nest = 1; /* search for next unmatched */
14020 }
14021 }
14022
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014023 if (match_pos != NULL)
14024 {
14025 /* Store the match cursor position */
14026 match_pos->lnum = curwin->w_cursor.lnum;
14027 match_pos->col = curwin->w_cursor.col + 1;
14028 }
14029
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014031 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032 curwin->w_cursor = save_cursor;
14033
14034theend:
14035 vim_free(pat2);
14036 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014038
14039 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040}
14041
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014042/*
14043 * "searchpos()" function
14044 */
14045 static void
14046f_searchpos(argvars, rettv)
14047 typval_T *argvars;
14048 typval_T *rettv;
14049{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014050 pos_T match_pos;
14051 int lnum = 0;
14052 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014053 int n;
14054 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014055
14056 rettv->vval.v_number = 0;
14057
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014058 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014059 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014060
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014061 n = search_cmn(argvars, &match_pos, &flags);
14062 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014063 {
14064 lnum = match_pos.lnum;
14065 col = match_pos.col;
14066 }
14067
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014068 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14069 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014070 if (flags & SP_SUBPAT)
14071 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014072}
14073
14074
Bram Moolenaar0d660222005-01-07 21:51:51 +000014075/*ARGSUSED*/
14076 static void
14077f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014078 typval_T *argvars;
14079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014081#ifdef FEAT_CLIENTSERVER
14082 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014083 char_u *server = get_tv_string_chk(&argvars[0]);
14084 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085
Bram Moolenaar0d660222005-01-07 21:51:51 +000014086 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014087 if (server == NULL || reply == NULL)
14088 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014089 if (check_restricted() || check_secure())
14090 return;
14091# ifdef FEAT_X11
14092 if (check_connection() == FAIL)
14093 return;
14094# endif
14095
14096 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014098 EMSG(_("E258: Unable to send to client"));
14099 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014101 rettv->vval.v_number = 0;
14102#else
14103 rettv->vval.v_number = -1;
14104#endif
14105}
14106
14107/*ARGSUSED*/
14108 static void
14109f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014110 typval_T *argvars;
14111 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014112{
14113 char_u *r = NULL;
14114
14115#ifdef FEAT_CLIENTSERVER
14116# ifdef WIN32
14117 r = serverGetVimNames();
14118# else
14119 make_connection();
14120 if (X_DISPLAY != NULL)
14121 r = serverGetVimNames(X_DISPLAY);
14122# endif
14123#endif
14124 rettv->v_type = VAR_STRING;
14125 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126}
14127
14128/*
14129 * "setbufvar()" function
14130 */
14131/*ARGSUSED*/
14132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014133f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014134 typval_T *argvars;
14135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136{
14137 buf_T *buf;
14138#ifdef FEAT_AUTOCMD
14139 aco_save_T aco;
14140#else
14141 buf_T *save_curbuf;
14142#endif
14143 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014144 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145 char_u nbuf[NUMBUFLEN];
14146
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014147 rettv->vval.v_number = 0;
14148
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 if (check_restricted() || check_secure())
14150 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014151 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14152 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014153 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154 varp = &argvars[2];
14155
14156 if (buf != NULL && varname != NULL && varp != NULL)
14157 {
14158 /* set curbuf to be our buf, temporarily */
14159#ifdef FEAT_AUTOCMD
14160 aucmd_prepbuf(&aco, buf);
14161#else
14162 save_curbuf = curbuf;
14163 curbuf = buf;
14164#endif
14165
14166 if (*varname == '&')
14167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014168 long numval;
14169 char_u *strval;
14170 int error = FALSE;
14171
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014173 numval = get_tv_number_chk(varp, &error);
14174 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014175 if (!error && strval != NULL)
14176 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 }
14178 else
14179 {
14180 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14181 if (bufvarname != NULL)
14182 {
14183 STRCPY(bufvarname, "b:");
14184 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014185 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186 vim_free(bufvarname);
14187 }
14188 }
14189
14190 /* reset notion of buffer */
14191#ifdef FEAT_AUTOCMD
14192 aucmd_restbuf(&aco);
14193#else
14194 curbuf = save_curbuf;
14195#endif
14196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197}
14198
14199/*
14200 * "setcmdpos()" function
14201 */
14202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014203f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014204 typval_T *argvars;
14205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014207 int pos = (int)get_tv_number(&argvars[0]) - 1;
14208
14209 if (pos >= 0)
14210 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211}
14212
14213/*
14214 * "setline()" function
14215 */
14216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014217f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014218 typval_T *argvars;
14219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220{
14221 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014222 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014223 list_T *l = NULL;
14224 listitem_T *li = NULL;
14225 long added = 0;
14226 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014228 lnum = get_tv_lnum(&argvars[0]);
14229 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014231 l = argvars[1].vval.v_list;
14232 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014234 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014236
14237 rettv->vval.v_number = 0; /* OK */
14238 for (;;)
14239 {
14240 if (l != NULL)
14241 {
14242 /* list argument, get next string */
14243 if (li == NULL)
14244 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014245 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014246 li = li->li_next;
14247 }
14248
14249 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014250 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014251 break;
14252 if (lnum <= curbuf->b_ml.ml_line_count)
14253 {
14254 /* existing line, replace it */
14255 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14256 {
14257 changed_bytes(lnum, 0);
14258 check_cursor_col();
14259 rettv->vval.v_number = 0; /* OK */
14260 }
14261 }
14262 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14263 {
14264 /* lnum is one past the last line, append the line */
14265 ++added;
14266 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14267 rettv->vval.v_number = 0; /* OK */
14268 }
14269
14270 if (l == NULL) /* only one string argument */
14271 break;
14272 ++lnum;
14273 }
14274
14275 if (added > 0)
14276 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277}
14278
14279/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014280 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014281 */
14282/*ARGSUSED*/
14283 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014284set_qf_ll_list(wp, list_arg, action_arg, rettv)
14285 win_T *wp;
14286 typval_T *list_arg;
14287 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014288 typval_T *rettv;
14289{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014290#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014291 char_u *act;
14292 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014293#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014294
Bram Moolenaar2641f772005-03-25 21:58:17 +000014295 rettv->vval.v_number = -1;
14296
14297#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014298 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014299 EMSG(_(e_listreq));
14300 else
14301 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014302 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014303
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014304 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014305 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014306 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014307 if (act == NULL)
14308 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014309 if (*act == 'a' || *act == 'r')
14310 action = *act;
14311 }
14312
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014313 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014314 rettv->vval.v_number = 0;
14315 }
14316#endif
14317}
14318
14319/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014320 * "setloclist()" function
14321 */
14322/*ARGSUSED*/
14323 static void
14324f_setloclist(argvars, rettv)
14325 typval_T *argvars;
14326 typval_T *rettv;
14327{
14328 win_T *win;
14329
14330 rettv->vval.v_number = -1;
14331
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014332 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014333 if (win != NULL)
14334 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14335}
14336
14337/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014338 * "setpos()" function
14339 */
14340/*ARGSUSED*/
14341 static void
14342f_setpos(argvars, rettv)
14343 typval_T *argvars;
14344 typval_T *rettv;
14345{
14346 pos_T pos;
14347 int fnum;
14348 char_u *name;
14349
14350 name = get_tv_string_chk(argvars);
14351 if (name != NULL)
14352 {
14353 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14354 {
14355 --pos.col;
14356 if (name[0] == '.') /* cursor */
14357 {
14358 if (fnum == curbuf->b_fnum)
14359 {
14360 curwin->w_cursor = pos;
14361 check_cursor();
14362 }
14363 else
14364 EMSG(_(e_invarg));
14365 }
14366 else if (name[0] == '\'') /* mark */
14367 (void)setmark_pos(name[1], &pos, fnum);
14368 else
14369 EMSG(_(e_invarg));
14370 }
14371 }
14372}
14373
14374/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014375 * "setqflist()" function
14376 */
14377/*ARGSUSED*/
14378 static void
14379f_setqflist(argvars, rettv)
14380 typval_T *argvars;
14381 typval_T *rettv;
14382{
14383 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14384}
14385
14386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 * "setreg()" function
14388 */
14389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014390f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014391 typval_T *argvars;
14392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393{
14394 int regname;
14395 char_u *strregname;
14396 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014397 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398 int append;
14399 char_u yank_type;
14400 long block_len;
14401
14402 block_len = -1;
14403 yank_type = MAUTO;
14404 append = FALSE;
14405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014406 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014407 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014409 if (strregname == NULL)
14410 return; /* type error; errmsg already given */
14411 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412 if (regname == 0 || regname == '@')
14413 regname = '"';
14414 else if (regname == '=')
14415 return;
14416
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014417 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014419 stropt = get_tv_string_chk(&argvars[2]);
14420 if (stropt == NULL)
14421 return; /* type error */
14422 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423 switch (*stropt)
14424 {
14425 case 'a': case 'A': /* append */
14426 append = TRUE;
14427 break;
14428 case 'v': case 'c': /* character-wise selection */
14429 yank_type = MCHAR;
14430 break;
14431 case 'V': case 'l': /* line-wise selection */
14432 yank_type = MLINE;
14433 break;
14434#ifdef FEAT_VISUAL
14435 case 'b': case Ctrl_V: /* block-wise selection */
14436 yank_type = MBLOCK;
14437 if (VIM_ISDIGIT(stropt[1]))
14438 {
14439 ++stropt;
14440 block_len = getdigits(&stropt) - 1;
14441 --stropt;
14442 }
14443 break;
14444#endif
14445 }
14446 }
14447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014448 strval = get_tv_string_chk(&argvars[1]);
14449 if (strval != NULL)
14450 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014452 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453}
14454
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014455/*
14456 * "settabwinvar()" function
14457 */
14458 static void
14459f_settabwinvar(argvars, rettv)
14460 typval_T *argvars;
14461 typval_T *rettv;
14462{
14463 setwinvar(argvars, rettv, 1);
14464}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465
14466/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014467 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014470f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014471 typval_T *argvars;
14472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014474 setwinvar(argvars, rettv, 0);
14475}
14476
14477/*
14478 * "setwinvar()" and "settabwinvar()" functions
14479 */
14480 static void
14481setwinvar(argvars, rettv, off)
14482 typval_T *argvars;
14483 typval_T *rettv;
14484 int off;
14485{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486 win_T *win;
14487#ifdef FEAT_WINDOWS
14488 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014489 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490#endif
14491 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014492 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014494 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014496 rettv->vval.v_number = 0;
14497
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498 if (check_restricted() || check_secure())
14499 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014500
14501#ifdef FEAT_WINDOWS
14502 if (off == 1)
14503 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14504 else
14505 tp = curtab;
14506#endif
14507 win = find_win_by_nr(&argvars[off], tp);
14508 varname = get_tv_string_chk(&argvars[off + 1]);
14509 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510
14511 if (win != NULL && varname != NULL && varp != NULL)
14512 {
14513#ifdef FEAT_WINDOWS
14514 /* set curwin to be our win, temporarily */
14515 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014516 save_curtab = curtab;
14517 goto_tabpage_tp(tp);
14518 if (!win_valid(win))
14519 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520 curwin = win;
14521 curbuf = curwin->w_buffer;
14522#endif
14523
14524 if (*varname == '&')
14525 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014526 long numval;
14527 char_u *strval;
14528 int error = FALSE;
14529
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014531 numval = get_tv_number_chk(varp, &error);
14532 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014533 if (!error && strval != NULL)
14534 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535 }
14536 else
14537 {
14538 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14539 if (winvarname != NULL)
14540 {
14541 STRCPY(winvarname, "w:");
14542 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014543 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544 vim_free(winvarname);
14545 }
14546 }
14547
14548#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014549 /* Restore current tabpage and window, if still valid (autocomands can
14550 * make them invalid). */
14551 if (valid_tabpage(save_curtab))
14552 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 if (win_valid(save_curwin))
14554 {
14555 curwin = save_curwin;
14556 curbuf = curwin->w_buffer;
14557 }
14558#endif
14559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560}
14561
14562/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014563 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 */
14565 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014566f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014567 typval_T *argvars;
14568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014570 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014571
Bram Moolenaar0d660222005-01-07 21:51:51 +000014572 p = get_tv_string(&argvars[0]);
14573 rettv->vval.v_string = vim_strsave(p);
14574 simplify_filename(rettv->vval.v_string); /* simplify in place */
14575 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576}
14577
Bram Moolenaar0d660222005-01-07 21:51:51 +000014578static int
14579#ifdef __BORLANDC__
14580 _RTLENTRYF
14581#endif
14582 item_compare __ARGS((const void *s1, const void *s2));
14583static int
14584#ifdef __BORLANDC__
14585 _RTLENTRYF
14586#endif
14587 item_compare2 __ARGS((const void *s1, const void *s2));
14588
14589static int item_compare_ic;
14590static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014591static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014592#define ITEM_COMPARE_FAIL 999
14593
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014595 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014597 static int
14598#ifdef __BORLANDC__
14599_RTLENTRYF
14600#endif
14601item_compare(s1, s2)
14602 const void *s1;
14603 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014605 char_u *p1, *p2;
14606 char_u *tofree1, *tofree2;
14607 int res;
14608 char_u numbuf1[NUMBUFLEN];
14609 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014611 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14612 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014613 if (item_compare_ic)
14614 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014616 res = STRCMP(p1, p2);
14617 vim_free(tofree1);
14618 vim_free(tofree2);
14619 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620}
14621
14622 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014623#ifdef __BORLANDC__
14624_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014626item_compare2(s1, s2)
14627 const void *s1;
14628 const void *s2;
14629{
14630 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014631 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014632 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014633 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014635 /* shortcut after failure in previous call; compare all items equal */
14636 if (item_compare_func_err)
14637 return 0;
14638
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014639 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14640 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014641 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14642 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014643
14644 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014645 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014646 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014647 clear_tv(&argv[0]);
14648 clear_tv(&argv[1]);
14649
14650 if (res == FAIL)
14651 res = ITEM_COMPARE_FAIL;
14652 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014653 /* return value has wrong type */
14654 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14655 if (item_compare_func_err)
14656 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014657 clear_tv(&rettv);
14658 return res;
14659}
14660
14661/*
14662 * "sort({list})" function
14663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014665f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014666 typval_T *argvars;
14667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668{
Bram Moolenaar33570922005-01-25 22:26:29 +000014669 list_T *l;
14670 listitem_T *li;
14671 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014672 long len;
14673 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674
Bram Moolenaar0d660222005-01-07 21:51:51 +000014675 rettv->vval.v_number = 0;
14676 if (argvars[0].v_type != VAR_LIST)
14677 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678 else
14679 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014680 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014681 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014682 return;
14683 rettv->vval.v_list = l;
14684 rettv->v_type = VAR_LIST;
14685 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686
Bram Moolenaar0d660222005-01-07 21:51:51 +000014687 len = list_len(l);
14688 if (len <= 1)
14689 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690
Bram Moolenaar0d660222005-01-07 21:51:51 +000014691 item_compare_ic = FALSE;
14692 item_compare_func = NULL;
14693 if (argvars[1].v_type != VAR_UNKNOWN)
14694 {
14695 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014696 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014697 else
14698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014699 int error = FALSE;
14700
14701 i = get_tv_number_chk(&argvars[1], &error);
14702 if (error)
14703 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014704 if (i == 1)
14705 item_compare_ic = TRUE;
14706 else
14707 item_compare_func = get_tv_string(&argvars[1]);
14708 }
14709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710
Bram Moolenaar0d660222005-01-07 21:51:51 +000014711 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014712 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014713 if (ptrs == NULL)
14714 return;
14715 i = 0;
14716 for (li = l->lv_first; li != NULL; li = li->li_next)
14717 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014718
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014719 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014720 /* test the compare function */
14721 if (item_compare_func != NULL
14722 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14723 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014724 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014726 {
14727 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014728 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014729 item_compare_func == NULL ? item_compare : item_compare2);
14730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014731 if (!item_compare_func_err)
14732 {
14733 /* Clear the List and append the items in the sorted order. */
14734 l->lv_first = l->lv_last = NULL;
14735 l->lv_len = 0;
14736 for (i = 0; i < len; ++i)
14737 list_append(l, ptrs[i]);
14738 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014739 }
14740
14741 vim_free(ptrs);
14742 }
14743}
14744
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014745/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014746 * "soundfold({word})" function
14747 */
14748 static void
14749f_soundfold(argvars, rettv)
14750 typval_T *argvars;
14751 typval_T *rettv;
14752{
14753 char_u *s;
14754
14755 rettv->v_type = VAR_STRING;
14756 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014757#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014758 rettv->vval.v_string = eval_soundfold(s);
14759#else
14760 rettv->vval.v_string = vim_strsave(s);
14761#endif
14762}
14763
14764/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014765 * "spellbadword()" function
14766 */
14767/* ARGSUSED */
14768 static void
14769f_spellbadword(argvars, rettv)
14770 typval_T *argvars;
14771 typval_T *rettv;
14772{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014773 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014774 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014775 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014776
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014777 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014778 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014779
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014780#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014781 if (argvars[0].v_type == VAR_UNKNOWN)
14782 {
14783 /* Find the start and length of the badly spelled word. */
14784 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14785 if (len != 0)
14786 word = ml_get_cursor();
14787 }
14788 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14789 {
14790 char_u *str = get_tv_string_chk(&argvars[0]);
14791 int capcol = -1;
14792
14793 if (str != NULL)
14794 {
14795 /* Check the argument for spelling. */
14796 while (*str != NUL)
14797 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014798 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014799 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014800 {
14801 word = str;
14802 break;
14803 }
14804 str += len;
14805 }
14806 }
14807 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014808#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014809
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014810 list_append_string(rettv->vval.v_list, word, len);
14811 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014812 attr == HLF_SPB ? "bad" :
14813 attr == HLF_SPR ? "rare" :
14814 attr == HLF_SPL ? "local" :
14815 attr == HLF_SPC ? "caps" :
14816 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014817}
14818
14819/*
14820 * "spellsuggest()" function
14821 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014822/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014823 static void
14824f_spellsuggest(argvars, rettv)
14825 typval_T *argvars;
14826 typval_T *rettv;
14827{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014828#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014829 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014830 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014831 int maxcount;
14832 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014833 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014834 listitem_T *li;
14835 int need_capital = FALSE;
14836#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014837
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014838 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014839 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014840
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014841#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014842 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14843 {
14844 str = get_tv_string(&argvars[0]);
14845 if (argvars[1].v_type != VAR_UNKNOWN)
14846 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014847 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014848 if (maxcount <= 0)
14849 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014850 if (argvars[2].v_type != VAR_UNKNOWN)
14851 {
14852 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14853 if (typeerr)
14854 return;
14855 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014856 }
14857 else
14858 maxcount = 25;
14859
Bram Moolenaar4770d092006-01-12 23:22:24 +000014860 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014861
14862 for (i = 0; i < ga.ga_len; ++i)
14863 {
14864 str = ((char_u **)ga.ga_data)[i];
14865
14866 li = listitem_alloc();
14867 if (li == NULL)
14868 vim_free(str);
14869 else
14870 {
14871 li->li_tv.v_type = VAR_STRING;
14872 li->li_tv.v_lock = 0;
14873 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014874 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014875 }
14876 }
14877 ga_clear(&ga);
14878 }
14879#endif
14880}
14881
Bram Moolenaar0d660222005-01-07 21:51:51 +000014882 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014883f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014884 typval_T *argvars;
14885 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014886{
14887 char_u *str;
14888 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014889 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014890 regmatch_T regmatch;
14891 char_u patbuf[NUMBUFLEN];
14892 char_u *save_cpo;
14893 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014894 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014895 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014896 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014897
14898 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14899 save_cpo = p_cpo;
14900 p_cpo = (char_u *)"";
14901
14902 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014903 if (argvars[1].v_type != VAR_UNKNOWN)
14904 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014905 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14906 if (pat == NULL)
14907 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014908 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014909 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014910 }
14911 if (pat == NULL || *pat == NUL)
14912 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014913
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014914 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014916 if (typeerr)
14917 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014918
Bram Moolenaar0d660222005-01-07 21:51:51 +000014919 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14920 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014922 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014923 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014924 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014925 if (*str == NUL)
14926 match = FALSE; /* empty item at the end */
14927 else
14928 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014929 if (match)
14930 end = regmatch.startp[0];
14931 else
14932 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014933 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14934 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014935 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014936 if (list_append_string(rettv->vval.v_list, str,
14937 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014938 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014939 }
14940 if (!match)
14941 break;
14942 /* Advance to just after the match. */
14943 if (regmatch.endp[0] > str)
14944 col = 0;
14945 else
14946 {
14947 /* Don't get stuck at the same match. */
14948#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014949 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014950#else
14951 col = 1;
14952#endif
14953 }
14954 str = regmatch.endp[0];
14955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956
Bram Moolenaar0d660222005-01-07 21:51:51 +000014957 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959
Bram Moolenaar0d660222005-01-07 21:51:51 +000014960 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961}
14962
Bram Moolenaar2c932302006-03-18 21:42:09 +000014963/*
14964 * "str2nr()" function
14965 */
14966 static void
14967f_str2nr(argvars, rettv)
14968 typval_T *argvars;
14969 typval_T *rettv;
14970{
14971 int base = 10;
14972 char_u *p;
14973 long n;
14974
14975 if (argvars[1].v_type != VAR_UNKNOWN)
14976 {
14977 base = get_tv_number(&argvars[1]);
14978 if (base != 8 && base != 10 && base != 16)
14979 {
14980 EMSG(_(e_invarg));
14981 return;
14982 }
14983 }
14984
14985 p = skipwhite(get_tv_string(&argvars[0]));
14986 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
14987 rettv->vval.v_number = n;
14988}
14989
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990#ifdef HAVE_STRFTIME
14991/*
14992 * "strftime({format}[, {time}])" function
14993 */
14994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014995f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014996 typval_T *argvars;
14997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014998{
14999 char_u result_buf[256];
15000 struct tm *curtime;
15001 time_t seconds;
15002 char_u *p;
15003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015004 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015006 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015007 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008 seconds = time(NULL);
15009 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015010 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011 curtime = localtime(&seconds);
15012 /* MSVC returns NULL for an invalid value of seconds. */
15013 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015014 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015 else
15016 {
15017# ifdef FEAT_MBYTE
15018 vimconv_T conv;
15019 char_u *enc;
15020
15021 conv.vc_type = CONV_NONE;
15022 enc = enc_locale();
15023 convert_setup(&conv, p_enc, enc);
15024 if (conv.vc_type != CONV_NONE)
15025 p = string_convert(&conv, p, NULL);
15026# endif
15027 if (p != NULL)
15028 (void)strftime((char *)result_buf, sizeof(result_buf),
15029 (char *)p, curtime);
15030 else
15031 result_buf[0] = NUL;
15032
15033# ifdef FEAT_MBYTE
15034 if (conv.vc_type != CONV_NONE)
15035 vim_free(p);
15036 convert_setup(&conv, enc, p_enc);
15037 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015038 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039 else
15040# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015041 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042
15043# ifdef FEAT_MBYTE
15044 /* Release conversion descriptors */
15045 convert_setup(&conv, NULL, NULL);
15046 vim_free(enc);
15047# endif
15048 }
15049}
15050#endif
15051
15052/*
15053 * "stridx()" function
15054 */
15055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015056f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015057 typval_T *argvars;
15058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059{
15060 char_u buf[NUMBUFLEN];
15061 char_u *needle;
15062 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015063 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015065 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015067 needle = get_tv_string_chk(&argvars[1]);
15068 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015069 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015070 if (needle == NULL || haystack == NULL)
15071 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072
Bram Moolenaar33570922005-01-25 22:26:29 +000015073 if (argvars[2].v_type != VAR_UNKNOWN)
15074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015075 int error = FALSE;
15076
15077 start_idx = get_tv_number_chk(&argvars[2], &error);
15078 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015079 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015080 if (start_idx >= 0)
15081 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015082 }
15083
15084 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15085 if (pos != NULL)
15086 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087}
15088
15089/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015090 * "string()" function
15091 */
15092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015093f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015094 typval_T *argvars;
15095 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015096{
15097 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015098 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015100 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015101 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015102 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015103 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015104}
15105
15106/*
15107 * "strlen()" function
15108 */
15109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015110f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015111 typval_T *argvars;
15112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015114 rettv->vval.v_number = (varnumber_T)(STRLEN(
15115 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116}
15117
15118/*
15119 * "strpart()" function
15120 */
15121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015122f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015123 typval_T *argvars;
15124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125{
15126 char_u *p;
15127 int n;
15128 int len;
15129 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015130 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015132 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133 slen = (int)STRLEN(p);
15134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015135 n = get_tv_number_chk(&argvars[1], &error);
15136 if (error)
15137 len = 0;
15138 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015139 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140 else
15141 len = slen - n; /* default len: all bytes that are available. */
15142
15143 /*
15144 * Only return the overlap between the specified part and the actual
15145 * string.
15146 */
15147 if (n < 0)
15148 {
15149 len += n;
15150 n = 0;
15151 }
15152 else if (n > slen)
15153 n = slen;
15154 if (len < 0)
15155 len = 0;
15156 else if (n + len > slen)
15157 len = slen - n;
15158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015159 rettv->v_type = VAR_STRING;
15160 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161}
15162
15163/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015164 * "strridx()" function
15165 */
15166 static void
15167f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015168 typval_T *argvars;
15169 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015170{
15171 char_u buf[NUMBUFLEN];
15172 char_u *needle;
15173 char_u *haystack;
15174 char_u *rest;
15175 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015176 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015177
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015178 needle = get_tv_string_chk(&argvars[1]);
15179 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015180
15181 rettv->vval.v_number = -1;
15182 if (needle == NULL || haystack == NULL)
15183 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015184
15185 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015186 if (argvars[2].v_type != VAR_UNKNOWN)
15187 {
15188 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015189 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015190 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015191 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015192 }
15193 else
15194 end_idx = haystack_len;
15195
Bram Moolenaar0d660222005-01-07 21:51:51 +000015196 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015197 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015198 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015199 lastmatch = haystack + end_idx;
15200 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015201 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015202 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015203 for (rest = haystack; *rest != '\0'; ++rest)
15204 {
15205 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015206 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015207 break;
15208 lastmatch = rest;
15209 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015210 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015211
15212 if (lastmatch == NULL)
15213 rettv->vval.v_number = -1;
15214 else
15215 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15216}
15217
15218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015219 * "strtrans()" function
15220 */
15221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015222f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015223 typval_T *argvars;
15224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015225{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015226 rettv->v_type = VAR_STRING;
15227 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228}
15229
15230/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015231 * "submatch()" function
15232 */
15233 static void
15234f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015235 typval_T *argvars;
15236 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015237{
15238 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015239 rettv->vval.v_string =
15240 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015241}
15242
15243/*
15244 * "substitute()" function
15245 */
15246 static void
15247f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015248 typval_T *argvars;
15249 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015250{
15251 char_u patbuf[NUMBUFLEN];
15252 char_u subbuf[NUMBUFLEN];
15253 char_u flagsbuf[NUMBUFLEN];
15254
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015255 char_u *str = get_tv_string_chk(&argvars[0]);
15256 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15257 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15258 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15259
Bram Moolenaar0d660222005-01-07 21:51:51 +000015260 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015261 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15262 rettv->vval.v_string = NULL;
15263 else
15264 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015265}
15266
15267/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015268 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269 */
15270/*ARGSUSED*/
15271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015272f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015273 typval_T *argvars;
15274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015275{
15276 int id = 0;
15277#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015278 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279 long col;
15280 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015281 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015283 lnum = get_tv_lnum(argvars); /* -1 on type error */
15284 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15285 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015286
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015287 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015288 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015289 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015290#endif
15291
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015292 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015293}
15294
15295/*
15296 * "synIDattr(id, what [, mode])" function
15297 */
15298/*ARGSUSED*/
15299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015300f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015301 typval_T *argvars;
15302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303{
15304 char_u *p = NULL;
15305#ifdef FEAT_SYN_HL
15306 int id;
15307 char_u *what;
15308 char_u *mode;
15309 char_u modebuf[NUMBUFLEN];
15310 int modec;
15311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015312 id = get_tv_number(&argvars[0]);
15313 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015314 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015316 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317 modec = TOLOWER_ASC(mode[0]);
15318 if (modec != 't' && modec != 'c'
15319#ifdef FEAT_GUI
15320 && modec != 'g'
15321#endif
15322 )
15323 modec = 0; /* replace invalid with current */
15324 }
15325 else
15326 {
15327#ifdef FEAT_GUI
15328 if (gui.in_use)
15329 modec = 'g';
15330 else
15331#endif
15332 if (t_colors > 1)
15333 modec = 'c';
15334 else
15335 modec = 't';
15336 }
15337
15338
15339 switch (TOLOWER_ASC(what[0]))
15340 {
15341 case 'b':
15342 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15343 p = highlight_color(id, what, modec);
15344 else /* bold */
15345 p = highlight_has_attr(id, HL_BOLD, modec);
15346 break;
15347
15348 case 'f': /* fg[#] */
15349 p = highlight_color(id, what, modec);
15350 break;
15351
15352 case 'i':
15353 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15354 p = highlight_has_attr(id, HL_INVERSE, modec);
15355 else /* italic */
15356 p = highlight_has_attr(id, HL_ITALIC, modec);
15357 break;
15358
15359 case 'n': /* name */
15360 p = get_highlight_name(NULL, id - 1);
15361 break;
15362
15363 case 'r': /* reverse */
15364 p = highlight_has_attr(id, HL_INVERSE, modec);
15365 break;
15366
15367 case 's': /* standout */
15368 p = highlight_has_attr(id, HL_STANDOUT, modec);
15369 break;
15370
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015371 case 'u':
15372 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15373 /* underline */
15374 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15375 else
15376 /* undercurl */
15377 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378 break;
15379 }
15380
15381 if (p != NULL)
15382 p = vim_strsave(p);
15383#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015384 rettv->v_type = VAR_STRING;
15385 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386}
15387
15388/*
15389 * "synIDtrans(id)" function
15390 */
15391/*ARGSUSED*/
15392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015393f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015394 typval_T *argvars;
15395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396{
15397 int id;
15398
15399#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015400 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401
15402 if (id > 0)
15403 id = syn_get_final_id(id);
15404 else
15405#endif
15406 id = 0;
15407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015408 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409}
15410
15411/*
15412 * "system()" function
15413 */
15414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015415f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015416 typval_T *argvars;
15417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015419 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015421 char_u *infile = NULL;
15422 char_u buf[NUMBUFLEN];
15423 int err = FALSE;
15424 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015426 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015427 {
15428 /*
15429 * Write the string to a temp file, to be used for input of the shell
15430 * command.
15431 */
15432 if ((infile = vim_tempname('i')) == NULL)
15433 {
15434 EMSG(_(e_notmp));
15435 return;
15436 }
15437
15438 fd = mch_fopen((char *)infile, WRITEBIN);
15439 if (fd == NULL)
15440 {
15441 EMSG2(_(e_notopen), infile);
15442 goto done;
15443 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015444 p = get_tv_string_buf_chk(&argvars[1], buf);
15445 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015446 {
15447 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015448 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015449 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015450 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15451 err = TRUE;
15452 if (fclose(fd) != 0)
15453 err = TRUE;
15454 if (err)
15455 {
15456 EMSG(_("E677: Error writing temp file"));
15457 goto done;
15458 }
15459 }
15460
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015461 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15462 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015463
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464#ifdef USE_CR
15465 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015466 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467 {
15468 char_u *s;
15469
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015470 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471 {
15472 if (*s == CAR)
15473 *s = NL;
15474 }
15475 }
15476#else
15477# ifdef USE_CRNL
15478 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015479 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480 {
15481 char_u *s, *d;
15482
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015483 d = res;
15484 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485 {
15486 if (s[0] == CAR && s[1] == NL)
15487 ++s;
15488 *d++ = *s;
15489 }
15490 *d = NUL;
15491 }
15492# endif
15493#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015494
15495done:
15496 if (infile != NULL)
15497 {
15498 mch_remove(infile);
15499 vim_free(infile);
15500 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015501 rettv->v_type = VAR_STRING;
15502 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503}
15504
15505/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015506 * "tabpagebuflist()" function
15507 */
15508/* ARGSUSED */
15509 static void
15510f_tabpagebuflist(argvars, rettv)
15511 typval_T *argvars;
15512 typval_T *rettv;
15513{
15514#ifndef FEAT_WINDOWS
15515 rettv->vval.v_number = 0;
15516#else
15517 tabpage_T *tp;
15518 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015519
15520 if (argvars[0].v_type == VAR_UNKNOWN)
15521 wp = firstwin;
15522 else
15523 {
15524 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15525 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015526 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015527 }
15528 if (wp == NULL)
15529 rettv->vval.v_number = 0;
15530 else
15531 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015532 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015533 rettv->vval.v_number = 0;
15534 else
15535 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015536 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015537 if (list_append_number(rettv->vval.v_list,
15538 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015539 break;
15540 }
15541 }
15542#endif
15543}
15544
15545
15546/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015547 * "tabpagenr()" function
15548 */
15549/* ARGSUSED */
15550 static void
15551f_tabpagenr(argvars, rettv)
15552 typval_T *argvars;
15553 typval_T *rettv;
15554{
15555 int nr = 1;
15556#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015557 char_u *arg;
15558
15559 if (argvars[0].v_type != VAR_UNKNOWN)
15560 {
15561 arg = get_tv_string_chk(&argvars[0]);
15562 nr = 0;
15563 if (arg != NULL)
15564 {
15565 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015566 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015567 else
15568 EMSG2(_(e_invexpr2), arg);
15569 }
15570 }
15571 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015572 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015573#endif
15574 rettv->vval.v_number = nr;
15575}
15576
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015577
15578#ifdef FEAT_WINDOWS
15579static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15580
15581/*
15582 * Common code for tabpagewinnr() and winnr().
15583 */
15584 static int
15585get_winnr(tp, argvar)
15586 tabpage_T *tp;
15587 typval_T *argvar;
15588{
15589 win_T *twin;
15590 int nr = 1;
15591 win_T *wp;
15592 char_u *arg;
15593
15594 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15595 if (argvar->v_type != VAR_UNKNOWN)
15596 {
15597 arg = get_tv_string_chk(argvar);
15598 if (arg == NULL)
15599 nr = 0; /* type error; errmsg already given */
15600 else if (STRCMP(arg, "$") == 0)
15601 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15602 else if (STRCMP(arg, "#") == 0)
15603 {
15604 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15605 if (twin == NULL)
15606 nr = 0;
15607 }
15608 else
15609 {
15610 EMSG2(_(e_invexpr2), arg);
15611 nr = 0;
15612 }
15613 }
15614
15615 if (nr > 0)
15616 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15617 wp != twin; wp = wp->w_next)
15618 ++nr;
15619 return nr;
15620}
15621#endif
15622
15623/*
15624 * "tabpagewinnr()" function
15625 */
15626/* ARGSUSED */
15627 static void
15628f_tabpagewinnr(argvars, rettv)
15629 typval_T *argvars;
15630 typval_T *rettv;
15631{
15632 int nr = 1;
15633#ifdef FEAT_WINDOWS
15634 tabpage_T *tp;
15635
15636 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15637 if (tp == NULL)
15638 nr = 0;
15639 else
15640 nr = get_winnr(tp, &argvars[1]);
15641#endif
15642 rettv->vval.v_number = nr;
15643}
15644
15645
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015646/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015647 * "tagfiles()" function
15648 */
15649/*ARGSUSED*/
15650 static void
15651f_tagfiles(argvars, rettv)
15652 typval_T *argvars;
15653 typval_T *rettv;
15654{
15655 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015656 tagname_T tn;
15657 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015658
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015659 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015660 {
15661 rettv->vval.v_number = 0;
15662 return;
15663 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015664
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015665 for (first = TRUE; ; first = FALSE)
15666 if (get_tagfname(&tn, first, fname) == FAIL
15667 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015668 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015669 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015670}
15671
15672/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015673 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015674 */
15675 static void
15676f_taglist(argvars, rettv)
15677 typval_T *argvars;
15678 typval_T *rettv;
15679{
15680 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015681
15682 tag_pattern = get_tv_string(&argvars[0]);
15683
15684 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015685 if (*tag_pattern == NUL)
15686 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015687
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015688 if (rettv_list_alloc(rettv) == OK)
15689 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015690}
15691
15692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693 * "tempname()" function
15694 */
15695/*ARGSUSED*/
15696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015697f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015698 typval_T *argvars;
15699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700{
15701 static int x = 'A';
15702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015703 rettv->v_type = VAR_STRING;
15704 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705
15706 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15707 * names. Skip 'I' and 'O', they are used for shell redirection. */
15708 do
15709 {
15710 if (x == 'Z')
15711 x = '0';
15712 else if (x == '9')
15713 x = 'A';
15714 else
15715 {
15716#ifdef EBCDIC
15717 if (x == 'I')
15718 x = 'J';
15719 else if (x == 'R')
15720 x = 'S';
15721 else
15722#endif
15723 ++x;
15724 }
15725 } while (x == 'I' || x == 'O');
15726}
15727
15728/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015729 * "test(list)" function: Just checking the walls...
15730 */
15731/*ARGSUSED*/
15732 static void
15733f_test(argvars, rettv)
15734 typval_T *argvars;
15735 typval_T *rettv;
15736{
15737 /* Used for unit testing. Change the code below to your liking. */
15738#if 0
15739 listitem_T *li;
15740 list_T *l;
15741 char_u *bad, *good;
15742
15743 if (argvars[0].v_type != VAR_LIST)
15744 return;
15745 l = argvars[0].vval.v_list;
15746 if (l == NULL)
15747 return;
15748 li = l->lv_first;
15749 if (li == NULL)
15750 return;
15751 bad = get_tv_string(&li->li_tv);
15752 li = li->li_next;
15753 if (li == NULL)
15754 return;
15755 good = get_tv_string(&li->li_tv);
15756 rettv->vval.v_number = test_edit_score(bad, good);
15757#endif
15758}
15759
15760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761 * "tolower(string)" function
15762 */
15763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015764f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015765 typval_T *argvars;
15766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767{
15768 char_u *p;
15769
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015770 p = vim_strsave(get_tv_string(&argvars[0]));
15771 rettv->v_type = VAR_STRING;
15772 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773
15774 if (p != NULL)
15775 while (*p != NUL)
15776 {
15777#ifdef FEAT_MBYTE
15778 int l;
15779
15780 if (enc_utf8)
15781 {
15782 int c, lc;
15783
15784 c = utf_ptr2char(p);
15785 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015786 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787 /* TODO: reallocate string when byte count changes. */
15788 if (utf_char2len(lc) == l)
15789 utf_char2bytes(lc, p);
15790 p += l;
15791 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015792 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793 p += l; /* skip multi-byte character */
15794 else
15795#endif
15796 {
15797 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15798 ++p;
15799 }
15800 }
15801}
15802
15803/*
15804 * "toupper(string)" function
15805 */
15806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015807f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015808 typval_T *argvars;
15809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015811 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015812 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015813}
15814
15815/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015816 * "tr(string, fromstr, tostr)" function
15817 */
15818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015819f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015820 typval_T *argvars;
15821 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015822{
15823 char_u *instr;
15824 char_u *fromstr;
15825 char_u *tostr;
15826 char_u *p;
15827#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015828 int inlen;
15829 int fromlen;
15830 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015831 int idx;
15832 char_u *cpstr;
15833 int cplen;
15834 int first = TRUE;
15835#endif
15836 char_u buf[NUMBUFLEN];
15837 char_u buf2[NUMBUFLEN];
15838 garray_T ga;
15839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015840 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015841 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15842 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015843
15844 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015845 rettv->v_type = VAR_STRING;
15846 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015847 if (fromstr == NULL || tostr == NULL)
15848 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015849 ga_init2(&ga, (int)sizeof(char), 80);
15850
15851#ifdef FEAT_MBYTE
15852 if (!has_mbyte)
15853#endif
15854 /* not multi-byte: fromstr and tostr must be the same length */
15855 if (STRLEN(fromstr) != STRLEN(tostr))
15856 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015857#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015858error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015859#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015860 EMSG2(_(e_invarg2), fromstr);
15861 ga_clear(&ga);
15862 return;
15863 }
15864
15865 /* fromstr and tostr have to contain the same number of chars */
15866 while (*instr != NUL)
15867 {
15868#ifdef FEAT_MBYTE
15869 if (has_mbyte)
15870 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015871 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015872 cpstr = instr;
15873 cplen = inlen;
15874 idx = 0;
15875 for (p = fromstr; *p != NUL; p += fromlen)
15876 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015877 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015878 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15879 {
15880 for (p = tostr; *p != NUL; p += tolen)
15881 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015882 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015883 if (idx-- == 0)
15884 {
15885 cplen = tolen;
15886 cpstr = p;
15887 break;
15888 }
15889 }
15890 if (*p == NUL) /* tostr is shorter than fromstr */
15891 goto error;
15892 break;
15893 }
15894 ++idx;
15895 }
15896
15897 if (first && cpstr == instr)
15898 {
15899 /* Check that fromstr and tostr have the same number of
15900 * (multi-byte) characters. Done only once when a character
15901 * of instr doesn't appear in fromstr. */
15902 first = FALSE;
15903 for (p = tostr; *p != NUL; p += tolen)
15904 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015905 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015906 --idx;
15907 }
15908 if (idx != 0)
15909 goto error;
15910 }
15911
15912 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015913 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015914 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015915
15916 instr += inlen;
15917 }
15918 else
15919#endif
15920 {
15921 /* When not using multi-byte chars we can do it faster. */
15922 p = vim_strchr(fromstr, *instr);
15923 if (p != NULL)
15924 ga_append(&ga, tostr[p - fromstr]);
15925 else
15926 ga_append(&ga, *instr);
15927 ++instr;
15928 }
15929 }
15930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015931 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015932}
15933
15934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935 * "type(expr)" function
15936 */
15937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015938f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015939 typval_T *argvars;
15940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015941{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015942 int n;
15943
15944 switch (argvars[0].v_type)
15945 {
15946 case VAR_NUMBER: n = 0; break;
15947 case VAR_STRING: n = 1; break;
15948 case VAR_FUNC: n = 2; break;
15949 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015950 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015951 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15952 }
15953 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015954}
15955
15956/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015957 * "values(dict)" function
15958 */
15959 static void
15960f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015961 typval_T *argvars;
15962 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015963{
15964 dict_list(argvars, rettv, 1);
15965}
15966
15967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015968 * "virtcol(string)" function
15969 */
15970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015971f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015972 typval_T *argvars;
15973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974{
15975 colnr_T vcol = 0;
15976 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015977 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015978
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015979 fp = var2fpos(&argvars[0], FALSE, &fnum);
15980 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15981 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015982 {
15983 getvvcol(curwin, fp, NULL, NULL, &vcol);
15984 ++vcol;
15985 }
15986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015987 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988}
15989
15990/*
15991 * "visualmode()" function
15992 */
15993/*ARGSUSED*/
15994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015995f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015996 typval_T *argvars;
15997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998{
15999#ifdef FEAT_VISUAL
16000 char_u str[2];
16001
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016002 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003 str[0] = curbuf->b_visual_mode_eval;
16004 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016005 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006
16007 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016008 if ((argvars[0].v_type == VAR_NUMBER
16009 && argvars[0].vval.v_number != 0)
16010 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016011 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012 curbuf->b_visual_mode_eval = NUL;
16013#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016014 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015#endif
16016}
16017
16018/*
16019 * "winbufnr(nr)" function
16020 */
16021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016022f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016023 typval_T *argvars;
16024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025{
16026 win_T *wp;
16027
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016028 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016030 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016032 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033}
16034
16035/*
16036 * "wincol()" function
16037 */
16038/*ARGSUSED*/
16039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016040f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016041 typval_T *argvars;
16042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043{
16044 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016045 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016046}
16047
16048/*
16049 * "winheight(nr)" function
16050 */
16051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016052f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016053 typval_T *argvars;
16054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055{
16056 win_T *wp;
16057
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016058 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016060 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016062 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063}
16064
16065/*
16066 * "winline()" function
16067 */
16068/*ARGSUSED*/
16069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016070f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016071 typval_T *argvars;
16072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073{
16074 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016075 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076}
16077
16078/*
16079 * "winnr()" function
16080 */
16081/* ARGSUSED */
16082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016083f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016084 typval_T *argvars;
16085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086{
16087 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016088
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016090 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016092 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093}
16094
16095/*
16096 * "winrestcmd()" function
16097 */
16098/* ARGSUSED */
16099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016100f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016101 typval_T *argvars;
16102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103{
16104#ifdef FEAT_WINDOWS
16105 win_T *wp;
16106 int winnr = 1;
16107 garray_T ga;
16108 char_u buf[50];
16109
16110 ga_init2(&ga, (int)sizeof(char), 70);
16111 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16112 {
16113 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16114 ga_concat(&ga, buf);
16115# ifdef FEAT_VERTSPLIT
16116 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16117 ga_concat(&ga, buf);
16118# endif
16119 ++winnr;
16120 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016121 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016123 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016125 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016127 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128}
16129
16130/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016131 * "winrestview()" function
16132 */
16133/* ARGSUSED */
16134 static void
16135f_winrestview(argvars, rettv)
16136 typval_T *argvars;
16137 typval_T *rettv;
16138{
16139 dict_T *dict;
16140
16141 if (argvars[0].v_type != VAR_DICT
16142 || (dict = argvars[0].vval.v_dict) == NULL)
16143 EMSG(_(e_invarg));
16144 else
16145 {
16146 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16147 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16148#ifdef FEAT_VIRTUALEDIT
16149 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16150#endif
16151 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016152 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016153
16154 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
16155#ifdef FEAT_DIFF
16156 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16157#endif
16158 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16159 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16160
16161 check_cursor();
16162 changed_cline_bef_curs();
16163 invalidate_botline();
16164 redraw_later(VALID);
16165
16166 if (curwin->w_topline == 0)
16167 curwin->w_topline = 1;
16168 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16169 curwin->w_topline = curbuf->b_ml.ml_line_count;
16170#ifdef FEAT_DIFF
16171 check_topfill(curwin, TRUE);
16172#endif
16173 }
16174}
16175
16176/*
16177 * "winsaveview()" function
16178 */
16179/* ARGSUSED */
16180 static void
16181f_winsaveview(argvars, rettv)
16182 typval_T *argvars;
16183 typval_T *rettv;
16184{
16185 dict_T *dict;
16186
16187 dict = dict_alloc();
16188 if (dict == NULL)
16189 return;
16190 rettv->v_type = VAR_DICT;
16191 rettv->vval.v_dict = dict;
16192 ++dict->dv_refcount;
16193
16194 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16195 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16196#ifdef FEAT_VIRTUALEDIT
16197 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16198#endif
16199 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16200
16201 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16202#ifdef FEAT_DIFF
16203 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16204#endif
16205 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16206 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16207}
16208
16209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 * "winwidth(nr)" function
16211 */
16212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016213f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016214 typval_T *argvars;
16215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216{
16217 win_T *wp;
16218
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016219 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016221 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 else
16223#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016224 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016226 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227#endif
16228}
16229
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016231 * "writefile()" function
16232 */
16233 static void
16234f_writefile(argvars, rettv)
16235 typval_T *argvars;
16236 typval_T *rettv;
16237{
16238 int binary = FALSE;
16239 char_u *fname;
16240 FILE *fd;
16241 listitem_T *li;
16242 char_u *s;
16243 int ret = 0;
16244 int c;
16245
16246 if (argvars[0].v_type != VAR_LIST)
16247 {
16248 EMSG2(_(e_listarg), "writefile()");
16249 return;
16250 }
16251 if (argvars[0].vval.v_list == NULL)
16252 return;
16253
16254 if (argvars[2].v_type != VAR_UNKNOWN
16255 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16256 binary = TRUE;
16257
16258 /* Always open the file in binary mode, library functions have a mind of
16259 * their own about CR-LF conversion. */
16260 fname = get_tv_string(&argvars[1]);
16261 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16262 {
16263 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16264 ret = -1;
16265 }
16266 else
16267 {
16268 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16269 li = li->li_next)
16270 {
16271 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16272 {
16273 if (*s == '\n')
16274 c = putc(NUL, fd);
16275 else
16276 c = putc(*s, fd);
16277 if (c == EOF)
16278 {
16279 ret = -1;
16280 break;
16281 }
16282 }
16283 if (!binary || li->li_next != NULL)
16284 if (putc('\n', fd) == EOF)
16285 {
16286 ret = -1;
16287 break;
16288 }
16289 if (ret < 0)
16290 {
16291 EMSG(_(e_write));
16292 break;
16293 }
16294 }
16295 fclose(fd);
16296 }
16297
16298 rettv->vval.v_number = ret;
16299}
16300
16301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016303 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 */
16305 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016306var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016307 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016309 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016311 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016313 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314
Bram Moolenaara5525202006-03-02 22:52:09 +000016315 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016316 if (varp->v_type == VAR_LIST)
16317 {
16318 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016319 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016320 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016321
16322 l = varp->vval.v_list;
16323 if (l == NULL)
16324 return NULL;
16325
16326 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016327 pos.lnum = list_find_nr(l, 0L, &error);
16328 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016329 return NULL; /* invalid line number */
16330
16331 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016332 pos.col = list_find_nr(l, 1L, &error);
16333 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016334 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016335 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016336 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016337 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016338 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016339 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016340
Bram Moolenaara5525202006-03-02 22:52:09 +000016341#ifdef FEAT_VIRTUALEDIT
16342 /* Get the virtual offset. Defaults to zero. */
16343 pos.coladd = list_find_nr(l, 2L, &error);
16344 if (error)
16345 pos.coladd = 0;
16346#endif
16347
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016348 return &pos;
16349 }
16350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016351 name = get_tv_string_chk(varp);
16352 if (name == NULL)
16353 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354 if (name[0] == '.') /* cursor */
16355 return &curwin->w_cursor;
16356 if (name[0] == '\'') /* mark */
16357 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016358 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16360 return NULL;
16361 return pp;
16362 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016363
16364#ifdef FEAT_VIRTUALEDIT
16365 pos.coladd = 0;
16366#endif
16367
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016368 if (name[0] == 'w' && lnum)
16369 {
16370 pos.col = 0;
16371 if (name[1] == '0') /* "w0": first visible line */
16372 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016373 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016374 pos.lnum = curwin->w_topline;
16375 return &pos;
16376 }
16377 else if (name[1] == '$') /* "w$": last visible line */
16378 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016379 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016380 pos.lnum = curwin->w_botline - 1;
16381 return &pos;
16382 }
16383 }
16384 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385 {
16386 if (lnum)
16387 {
16388 pos.lnum = curbuf->b_ml.ml_line_count;
16389 pos.col = 0;
16390 }
16391 else
16392 {
16393 pos.lnum = curwin->w_cursor.lnum;
16394 pos.col = (colnr_T)STRLEN(ml_get_curline());
16395 }
16396 return &pos;
16397 }
16398 return NULL;
16399}
16400
16401/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016402 * Convert list in "arg" into a position and optional file number.
16403 * When "fnump" is NULL there is no file number, only 3 items.
16404 * Note that the column is passed on as-is, the caller may want to decrement
16405 * it to use 1 for the first column.
16406 * Return FAIL when conversion is not possible, doesn't check the position for
16407 * validity.
16408 */
16409 static int
16410list2fpos(arg, posp, fnump)
16411 typval_T *arg;
16412 pos_T *posp;
16413 int *fnump;
16414{
16415 list_T *l = arg->vval.v_list;
16416 long i = 0;
16417 long n;
16418
16419 /* List must be: [fnum, lnum, col, coladd] */
16420 if (arg->v_type != VAR_LIST || l == NULL
16421 || l->lv_len != (fnump == NULL ? 3 : 4))
16422 return FAIL;
16423
16424 if (fnump != NULL)
16425 {
16426 n = list_find_nr(l, i++, NULL); /* fnum */
16427 if (n < 0)
16428 return FAIL;
16429 if (n == 0)
16430 n = curbuf->b_fnum; /* current buffer */
16431 *fnump = n;
16432 }
16433
16434 n = list_find_nr(l, i++, NULL); /* lnum */
16435 if (n < 0)
16436 return FAIL;
16437 posp->lnum = n;
16438
16439 n = list_find_nr(l, i++, NULL); /* col */
16440 if (n < 0)
16441 return FAIL;
16442 posp->col = n;
16443
16444#ifdef FEAT_VIRTUALEDIT
16445 n = list_find_nr(l, i, NULL);
16446 if (n < 0)
16447 return FAIL;
16448 posp->coladd = n;
16449#endif
16450
16451 return OK;
16452}
16453
16454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455 * Get the length of an environment variable name.
16456 * Advance "arg" to the first character after the name.
16457 * Return 0 for error.
16458 */
16459 static int
16460get_env_len(arg)
16461 char_u **arg;
16462{
16463 char_u *p;
16464 int len;
16465
16466 for (p = *arg; vim_isIDc(*p); ++p)
16467 ;
16468 if (p == *arg) /* no name found */
16469 return 0;
16470
16471 len = (int)(p - *arg);
16472 *arg = p;
16473 return len;
16474}
16475
16476/*
16477 * Get the length of the name of a function or internal variable.
16478 * "arg" is advanced to the first non-white character after the name.
16479 * Return 0 if something is wrong.
16480 */
16481 static int
16482get_id_len(arg)
16483 char_u **arg;
16484{
16485 char_u *p;
16486 int len;
16487
16488 /* Find the end of the name. */
16489 for (p = *arg; eval_isnamec(*p); ++p)
16490 ;
16491 if (p == *arg) /* no name found */
16492 return 0;
16493
16494 len = (int)(p - *arg);
16495 *arg = skipwhite(p);
16496
16497 return len;
16498}
16499
16500/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016501 * Get the length of the name of a variable or function.
16502 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016504 * Return -1 if curly braces expansion failed.
16505 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016506 * If the name contains 'magic' {}'s, expand them and return the
16507 * expanded name in an allocated string via 'alias' - caller must free.
16508 */
16509 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016510get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511 char_u **arg;
16512 char_u **alias;
16513 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016514 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016515{
16516 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517 char_u *p;
16518 char_u *expr_start;
16519 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520
16521 *alias = NULL; /* default to no alias */
16522
16523 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16524 && (*arg)[2] == (int)KE_SNR)
16525 {
16526 /* hard coded <SNR>, already translated */
16527 *arg += 3;
16528 return get_id_len(arg) + 3;
16529 }
16530 len = eval_fname_script(*arg);
16531 if (len > 0)
16532 {
16533 /* literal "<SID>", "s:" or "<SNR>" */
16534 *arg += len;
16535 }
16536
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016538 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016540 p = find_name_end(*arg, &expr_start, &expr_end,
16541 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542 if (expr_start != NULL)
16543 {
16544 char_u *temp_string;
16545
16546 if (!evaluate)
16547 {
16548 len += (int)(p - *arg);
16549 *arg = skipwhite(p);
16550 return len;
16551 }
16552
16553 /*
16554 * Include any <SID> etc in the expanded string:
16555 * Thus the -len here.
16556 */
16557 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16558 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016559 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560 *alias = temp_string;
16561 *arg = skipwhite(p);
16562 return (int)STRLEN(temp_string);
16563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564
16565 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016566 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 EMSG2(_(e_invexpr2), *arg);
16568
16569 return len;
16570}
16571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016572/*
16573 * Find the end of a variable or function name, taking care of magic braces.
16574 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16575 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016576 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016577 * Return a pointer to just after the name. Equal to "arg" if there is no
16578 * valid name.
16579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016581find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582 char_u *arg;
16583 char_u **expr_start;
16584 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016585 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016587 int mb_nest = 0;
16588 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589 char_u *p;
16590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016591 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016593 *expr_start = NULL;
16594 *expr_end = NULL;
16595 }
16596
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016597 /* Quick check for valid starting character. */
16598 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16599 return arg;
16600
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016601 for (p = arg; *p != NUL
16602 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016603 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016604 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016605 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016606 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016607 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016608 if (*p == '\'')
16609 {
16610 /* skip over 'string' to avoid counting [ and ] inside it. */
16611 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16612 ;
16613 if (*p == NUL)
16614 break;
16615 }
16616 else if (*p == '"')
16617 {
16618 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16619 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16620 if (*p == '\\' && p[1] != NUL)
16621 ++p;
16622 if (*p == NUL)
16623 break;
16624 }
16625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016626 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016628 if (*p == '[')
16629 ++br_nest;
16630 else if (*p == ']')
16631 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016634 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016636 if (*p == '{')
16637 {
16638 mb_nest++;
16639 if (expr_start != NULL && *expr_start == NULL)
16640 *expr_start = p;
16641 }
16642 else if (*p == '}')
16643 {
16644 mb_nest--;
16645 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16646 *expr_end = p;
16647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649 }
16650
16651 return p;
16652}
16653
16654/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016655 * Expands out the 'magic' {}'s in a variable/function name.
16656 * Note that this can call itself recursively, to deal with
16657 * constructs like foo{bar}{baz}{bam}
16658 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16659 * "in_start" ^
16660 * "expr_start" ^
16661 * "expr_end" ^
16662 * "in_end" ^
16663 *
16664 * Returns a new allocated string, which the caller must free.
16665 * Returns NULL for failure.
16666 */
16667 static char_u *
16668make_expanded_name(in_start, expr_start, expr_end, in_end)
16669 char_u *in_start;
16670 char_u *expr_start;
16671 char_u *expr_end;
16672 char_u *in_end;
16673{
16674 char_u c1;
16675 char_u *retval = NULL;
16676 char_u *temp_result;
16677 char_u *nextcmd = NULL;
16678
16679 if (expr_end == NULL || in_end == NULL)
16680 return NULL;
16681 *expr_start = NUL;
16682 *expr_end = NUL;
16683 c1 = *in_end;
16684 *in_end = NUL;
16685
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016686 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016687 if (temp_result != NULL && nextcmd == NULL)
16688 {
16689 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16690 + (in_end - expr_end) + 1));
16691 if (retval != NULL)
16692 {
16693 STRCPY(retval, in_start);
16694 STRCAT(retval, temp_result);
16695 STRCAT(retval, expr_end + 1);
16696 }
16697 }
16698 vim_free(temp_result);
16699
16700 *in_end = c1; /* put char back for error messages */
16701 *expr_start = '{';
16702 *expr_end = '}';
16703
16704 if (retval != NULL)
16705 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016706 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016707 if (expr_start != NULL)
16708 {
16709 /* Further expansion! */
16710 temp_result = make_expanded_name(retval, expr_start,
16711 expr_end, temp_result);
16712 vim_free(retval);
16713 retval = temp_result;
16714 }
16715 }
16716
16717 return retval;
16718}
16719
16720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016722 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 */
16724 static int
16725eval_isnamec(c)
16726 int c;
16727{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016728 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16729}
16730
16731/*
16732 * Return TRUE if character "c" can be used as the first character in a
16733 * variable or function name (excluding '{' and '}').
16734 */
16735 static int
16736eval_isnamec1(c)
16737 int c;
16738{
16739 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740}
16741
16742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743 * Set number v: variable to "val".
16744 */
16745 void
16746set_vim_var_nr(idx, val)
16747 int idx;
16748 long val;
16749{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016750 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751}
16752
16753/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016754 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755 */
16756 long
16757get_vim_var_nr(idx)
16758 int idx;
16759{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016760 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761}
16762
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016763#if defined(FEAT_AUTOCMD) || defined(PROTO)
16764/*
16765 * Get string v: variable value. Uses a static buffer, can only be used once.
16766 */
16767 char_u *
16768get_vim_var_str(idx)
16769 int idx;
16770{
16771 return get_tv_string(&vimvars[idx].vv_tv);
16772}
16773#endif
16774
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775/*
16776 * Set v:count, v:count1 and v:prevcount.
16777 */
16778 void
16779set_vcount(count, count1)
16780 long count;
16781 long count1;
16782{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016783 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16784 vimvars[VV_COUNT].vv_nr = count;
16785 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786}
16787
16788/*
16789 * Set string v: variable to a copy of "val".
16790 */
16791 void
16792set_vim_var_string(idx, val, len)
16793 int idx;
16794 char_u *val;
16795 int len; /* length of "val" to use or -1 (whole string) */
16796{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016797 /* Need to do this (at least) once, since we can't initialize a union.
16798 * Will always be invoked when "v:progname" is set. */
16799 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16800
Bram Moolenaare9a41262005-01-15 22:18:47 +000016801 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016803 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016805 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016807 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808}
16809
16810/*
16811 * Set v:register if needed.
16812 */
16813 void
16814set_reg_var(c)
16815 int c;
16816{
16817 char_u regname;
16818
16819 if (c == 0 || c == ' ')
16820 regname = '"';
16821 else
16822 regname = c;
16823 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016824 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 set_vim_var_string(VV_REG, &regname, 1);
16826}
16827
16828/*
16829 * Get or set v:exception. If "oldval" == NULL, return the current value.
16830 * Otherwise, restore the value to "oldval" and return NULL.
16831 * Must always be called in pairs to save and restore v:exception! Does not
16832 * take care of memory allocations.
16833 */
16834 char_u *
16835v_exception(oldval)
16836 char_u *oldval;
16837{
16838 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016839 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840
Bram Moolenaare9a41262005-01-15 22:18:47 +000016841 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842 return NULL;
16843}
16844
16845/*
16846 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16847 * Otherwise, restore the value to "oldval" and return NULL.
16848 * Must always be called in pairs to save and restore v:throwpoint! Does not
16849 * take care of memory allocations.
16850 */
16851 char_u *
16852v_throwpoint(oldval)
16853 char_u *oldval;
16854{
16855 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016856 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857
Bram Moolenaare9a41262005-01-15 22:18:47 +000016858 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859 return NULL;
16860}
16861
16862#if defined(FEAT_AUTOCMD) || defined(PROTO)
16863/*
16864 * Set v:cmdarg.
16865 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16866 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16867 * Must always be called in pairs!
16868 */
16869 char_u *
16870set_cmdarg(eap, oldarg)
16871 exarg_T *eap;
16872 char_u *oldarg;
16873{
16874 char_u *oldval;
16875 char_u *newval;
16876 unsigned len;
16877
Bram Moolenaare9a41262005-01-15 22:18:47 +000016878 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016879 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016881 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016882 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016883 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 }
16885
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016886 if (eap->force_bin == FORCE_BIN)
16887 len = 6;
16888 else if (eap->force_bin == FORCE_NOBIN)
16889 len = 8;
16890 else
16891 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016892
16893 if (eap->read_edit)
16894 len += 7;
16895
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016896 if (eap->force_ff != 0)
16897 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16898# ifdef FEAT_MBYTE
16899 if (eap->force_enc != 0)
16900 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016901 if (eap->bad_char != 0)
16902 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016903# endif
16904
16905 newval = alloc(len + 1);
16906 if (newval == NULL)
16907 return NULL;
16908
16909 if (eap->force_bin == FORCE_BIN)
16910 sprintf((char *)newval, " ++bin");
16911 else if (eap->force_bin == FORCE_NOBIN)
16912 sprintf((char *)newval, " ++nobin");
16913 else
16914 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016915
16916 if (eap->read_edit)
16917 STRCAT(newval, " ++edit");
16918
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016919 if (eap->force_ff != 0)
16920 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16921 eap->cmd + eap->force_ff);
16922# ifdef FEAT_MBYTE
16923 if (eap->force_enc != 0)
16924 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16925 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016926 if (eap->bad_char != 0)
16927 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16928 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016929# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016930 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016931 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932}
16933#endif
16934
16935/*
16936 * Get the value of internal variable "name".
16937 * Return OK or FAIL.
16938 */
16939 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016940get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941 char_u *name;
16942 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016943 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016944 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016945{
16946 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016947 typval_T *tv = NULL;
16948 typval_T atv;
16949 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951
16952 /* truncate the name, so that we can use strcmp() */
16953 cc = name[len];
16954 name[len] = NUL;
16955
16956 /*
16957 * Check for "b:changedtick".
16958 */
16959 if (STRCMP(name, "b:changedtick") == 0)
16960 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016961 atv.v_type = VAR_NUMBER;
16962 atv.vval.v_number = curbuf->b_changedtick;
16963 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964 }
16965
16966 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967 * Check for user-defined variables.
16968 */
16969 else
16970 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016971 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016973 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974 }
16975
Bram Moolenaare9a41262005-01-15 22:18:47 +000016976 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016978 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016979 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980 ret = FAIL;
16981 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016982 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016983 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984
16985 name[len] = cc;
16986
16987 return ret;
16988}
16989
16990/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016991 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16992 * Also handle function call with Funcref variable: func(expr)
16993 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16994 */
16995 static int
16996handle_subscript(arg, rettv, evaluate, verbose)
16997 char_u **arg;
16998 typval_T *rettv;
16999 int evaluate; /* do more than finding the end */
17000 int verbose; /* give error messages */
17001{
17002 int ret = OK;
17003 dict_T *selfdict = NULL;
17004 char_u *s;
17005 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017006 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017007
17008 while (ret == OK
17009 && (**arg == '['
17010 || (**arg == '.' && rettv->v_type == VAR_DICT)
17011 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17012 && !vim_iswhite(*(*arg - 1)))
17013 {
17014 if (**arg == '(')
17015 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017016 /* need to copy the funcref so that we can clear rettv */
17017 functv = *rettv;
17018 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017019
17020 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017021 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017022 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017023 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17024 &len, evaluate, selfdict);
17025
17026 /* Clear the funcref afterwards, so that deleting it while
17027 * evaluating the arguments is possible (see test55). */
17028 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017029
17030 /* Stop the expression evaluation when immediately aborting on
17031 * error, or when an interrupt occurred or an exception was thrown
17032 * but not caught. */
17033 if (aborting())
17034 {
17035 if (ret == OK)
17036 clear_tv(rettv);
17037 ret = FAIL;
17038 }
17039 dict_unref(selfdict);
17040 selfdict = NULL;
17041 }
17042 else /* **arg == '[' || **arg == '.' */
17043 {
17044 dict_unref(selfdict);
17045 if (rettv->v_type == VAR_DICT)
17046 {
17047 selfdict = rettv->vval.v_dict;
17048 if (selfdict != NULL)
17049 ++selfdict->dv_refcount;
17050 }
17051 else
17052 selfdict = NULL;
17053 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17054 {
17055 clear_tv(rettv);
17056 ret = FAIL;
17057 }
17058 }
17059 }
17060 dict_unref(selfdict);
17061 return ret;
17062}
17063
17064/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017065 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17066 * value).
17067 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017068 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017069alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017070{
Bram Moolenaar33570922005-01-25 22:26:29 +000017071 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017072}
17073
17074/*
17075 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076 * The string "s" must have been allocated, it is consumed.
17077 * Return NULL for out of memory, the variable otherwise.
17078 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017079 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017080alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081 char_u *s;
17082{
Bram Moolenaar33570922005-01-25 22:26:29 +000017083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017085 rettv = alloc_tv();
17086 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017088 rettv->v_type = VAR_STRING;
17089 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090 }
17091 else
17092 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017093 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094}
17095
17096/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017097 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017099 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017100free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017101 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102{
17103 if (varp != NULL)
17104 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017105 switch (varp->v_type)
17106 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017107 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017108 func_unref(varp->vval.v_string);
17109 /*FALLTHROUGH*/
17110 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017111 vim_free(varp->vval.v_string);
17112 break;
17113 case VAR_LIST:
17114 list_unref(varp->vval.v_list);
17115 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017116 case VAR_DICT:
17117 dict_unref(varp->vval.v_dict);
17118 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017119 case VAR_NUMBER:
17120 case VAR_UNKNOWN:
17121 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017122 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017123 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017124 break;
17125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126 vim_free(varp);
17127 }
17128}
17129
17130/*
17131 * Free the memory for a variable value and set the value to NULL or 0.
17132 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017133 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017134clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017135 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136{
17137 if (varp != NULL)
17138 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017139 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017141 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017142 func_unref(varp->vval.v_string);
17143 /*FALLTHROUGH*/
17144 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017145 vim_free(varp->vval.v_string);
17146 varp->vval.v_string = NULL;
17147 break;
17148 case VAR_LIST:
17149 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017150 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017151 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017152 case VAR_DICT:
17153 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017154 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017155 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017156 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017157 varp->vval.v_number = 0;
17158 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017159 case VAR_UNKNOWN:
17160 break;
17161 default:
17162 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017164 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165 }
17166}
17167
17168/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017169 * Set the value of a variable to NULL without freeing items.
17170 */
17171 static void
17172init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017173 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017174{
17175 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017176 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017177}
17178
17179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180 * Get the number value of a variable.
17181 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017182 * For incompatible types, return 0.
17183 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17184 * caller of incompatible types: it sets *denote to TRUE if "denote"
17185 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186 */
17187 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017188get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017189 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017191 int error = FALSE;
17192
17193 return get_tv_number_chk(varp, &error); /* return 0L on error */
17194}
17195
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017196 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017197get_tv_number_chk(varp, denote)
17198 typval_T *varp;
17199 int *denote;
17200{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017201 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017203 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017205 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017206 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017207 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017208 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017209 break;
17210 case VAR_STRING:
17211 if (varp->vval.v_string != NULL)
17212 vim_str2nr(varp->vval.v_string, NULL, NULL,
17213 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017214 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017215 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017216 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017217 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017218 case VAR_DICT:
17219 EMSG(_("E728: Using a Dictionary as a number"));
17220 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017221 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017222 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017223 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017225 if (denote == NULL) /* useful for values that must be unsigned */
17226 n = -1;
17227 else
17228 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017229 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230}
17231
17232/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017233 * Get the lnum from the first argument.
17234 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017235 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236 */
17237 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017238get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017239 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240{
Bram Moolenaar33570922005-01-25 22:26:29 +000017241 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242 linenr_T lnum;
17243
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017244 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245 if (lnum == 0) /* no valid number, try using line() */
17246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017247 rettv.v_type = VAR_NUMBER;
17248 f_line(argvars, &rettv);
17249 lnum = rettv.vval.v_number;
17250 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251 }
17252 return lnum;
17253}
17254
17255/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017256 * Get the lnum from the first argument.
17257 * Also accepts "$", then "buf" is used.
17258 * Returns 0 on error.
17259 */
17260 static linenr_T
17261get_tv_lnum_buf(argvars, buf)
17262 typval_T *argvars;
17263 buf_T *buf;
17264{
17265 if (argvars[0].v_type == VAR_STRING
17266 && argvars[0].vval.v_string != NULL
17267 && argvars[0].vval.v_string[0] == '$'
17268 && buf != NULL)
17269 return buf->b_ml.ml_line_count;
17270 return get_tv_number_chk(&argvars[0], NULL);
17271}
17272
17273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 * Get the string value of a variable.
17275 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017276 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17277 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278 * If the String variable has never been set, return an empty string.
17279 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017280 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17281 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 */
17283 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017284get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017285 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286{
17287 static char_u mybuf[NUMBUFLEN];
17288
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017289 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290}
17291
17292 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017293get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017294 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017295 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017297 char_u *res = get_tv_string_buf_chk(varp, buf);
17298
17299 return res != NULL ? res : (char_u *)"";
17300}
17301
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017302 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017303get_tv_string_chk(varp)
17304 typval_T *varp;
17305{
17306 static char_u mybuf[NUMBUFLEN];
17307
17308 return get_tv_string_buf_chk(varp, mybuf);
17309}
17310
17311 static char_u *
17312get_tv_string_buf_chk(varp, buf)
17313 typval_T *varp;
17314 char_u *buf;
17315{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017316 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017318 case VAR_NUMBER:
17319 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17320 return buf;
17321 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017322 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017323 break;
17324 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017325 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017326 break;
17327 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017328 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017329 break;
17330 case VAR_STRING:
17331 if (varp->vval.v_string != NULL)
17332 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017333 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017334 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017335 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017336 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017338 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339}
17340
17341/*
17342 * Find variable "name" in the list of variables.
17343 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017344 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017345 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017346 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017348 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017349find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017351 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017354 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355
Bram Moolenaara7043832005-01-21 11:56:39 +000017356 ht = find_var_ht(name, &varname);
17357 if (htp != NULL)
17358 *htp = ht;
17359 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017361 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362}
17363
17364/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017365 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017366 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017368 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017369find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017370 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017371 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017372 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017373{
Bram Moolenaar33570922005-01-25 22:26:29 +000017374 hashitem_T *hi;
17375
17376 if (*varname == NUL)
17377 {
17378 /* Must be something like "s:", otherwise "ht" would be NULL. */
17379 switch (varname[-2])
17380 {
17381 case 's': return &SCRIPT_SV(current_SID).sv_var;
17382 case 'g': return &globvars_var;
17383 case 'v': return &vimvars_var;
17384 case 'b': return &curbuf->b_bufvar;
17385 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017386#ifdef FEAT_WINDOWS
17387 case 't': return &curtab->tp_winvar;
17388#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017389 case 'l': return current_funccal == NULL
17390 ? NULL : &current_funccal->l_vars_var;
17391 case 'a': return current_funccal == NULL
17392 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017393 }
17394 return NULL;
17395 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017396
17397 hi = hash_find(ht, varname);
17398 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017399 {
17400 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017401 * worked find the variable again. Don't auto-load a script if it was
17402 * loaded already, otherwise it would be loaded every time when
17403 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017404 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017405 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017406 hi = hash_find(ht, varname);
17407 if (HASHITEM_EMPTY(hi))
17408 return NULL;
17409 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017410 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017411}
17412
17413/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017414 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017415 * Set "varname" to the start of name without ':'.
17416 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017417 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017418find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419 char_u *name;
17420 char_u **varname;
17421{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017422 hashitem_T *hi;
17423
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 if (name[1] != ':')
17425 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017426 /* The name must not start with a colon or #. */
17427 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428 return NULL;
17429 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017430
17431 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017432 hi = hash_find(&compat_hashtab, name);
17433 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017434 return &compat_hashtab;
17435
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017437 return &globvarht; /* global variable */
17438 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439 }
17440 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017441 if (*name == 'g') /* global variable */
17442 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017443 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17444 */
17445 if (vim_strchr(name + 2, ':') != NULL
17446 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017447 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017448 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017449 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017451 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017452#ifdef FEAT_WINDOWS
17453 if (*name == 't') /* tab page variable */
17454 return &curtab->tp_vars.dv_hashtab;
17455#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017456 if (*name == 'v') /* v: variable */
17457 return &vimvarht;
17458 if (*name == 'a' && current_funccal != NULL) /* function argument */
17459 return &current_funccal->l_avars.dv_hashtab;
17460 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17461 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462 if (*name == 's' /* script variable */
17463 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17464 return &SCRIPT_VARS(current_SID);
17465 return NULL;
17466}
17467
17468/*
17469 * Get the string value of a (global/local) variable.
17470 * Returns NULL when it doesn't exist.
17471 */
17472 char_u *
17473get_var_value(name)
17474 char_u *name;
17475{
Bram Moolenaar33570922005-01-25 22:26:29 +000017476 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477
Bram Moolenaara7043832005-01-21 11:56:39 +000017478 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 if (v == NULL)
17480 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017481 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482}
17483
17484/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017485 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486 * sourcing this script and when executing functions defined in the script.
17487 */
17488 void
17489new_script_vars(id)
17490 scid_T id;
17491{
Bram Moolenaara7043832005-01-21 11:56:39 +000017492 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017493 hashtab_T *ht;
17494 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017495
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17497 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017498 /* Re-allocating ga_data means that an ht_array pointing to
17499 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017500 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017501 for (i = 1; i <= ga_scripts.ga_len; ++i)
17502 {
17503 ht = &SCRIPT_VARS(i);
17504 if (ht->ht_mask == HT_INIT_SIZE - 1)
17505 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017506 sv = &SCRIPT_SV(i);
17507 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017508 }
17509
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 while (ga_scripts.ga_len < id)
17511 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017512 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17513 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515 }
17516 }
17517}
17518
17519/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017520 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17521 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 */
17523 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017524init_var_dict(dict, dict_var)
17525 dict_T *dict;
17526 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527{
Bram Moolenaar33570922005-01-25 22:26:29 +000017528 hash_init(&dict->dv_hashtab);
17529 dict->dv_refcount = 99999;
17530 dict_var->di_tv.vval.v_dict = dict;
17531 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017532 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017533 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17534 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535}
17536
17537/*
17538 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017539 * Frees all allocated variables and the value they contain.
17540 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541 */
17542 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017543vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017544 hashtab_T *ht;
17545{
17546 vars_clear_ext(ht, TRUE);
17547}
17548
17549/*
17550 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17551 */
17552 static void
17553vars_clear_ext(ht, free_val)
17554 hashtab_T *ht;
17555 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556{
Bram Moolenaara7043832005-01-21 11:56:39 +000017557 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017558 hashitem_T *hi;
17559 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560
Bram Moolenaar33570922005-01-25 22:26:29 +000017561 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017562 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017563 for (hi = ht->ht_array; todo > 0; ++hi)
17564 {
17565 if (!HASHITEM_EMPTY(hi))
17566 {
17567 --todo;
17568
Bram Moolenaar33570922005-01-25 22:26:29 +000017569 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017570 * ht_array might change then. hash_clear() takes care of it
17571 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017572 v = HI2DI(hi);
17573 if (free_val)
17574 clear_tv(&v->di_tv);
17575 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17576 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017577 }
17578 }
17579 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017580 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581}
17582
Bram Moolenaara7043832005-01-21 11:56:39 +000017583/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017584 * Delete a variable from hashtab "ht" at item "hi".
17585 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017588delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017589 hashtab_T *ht;
17590 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017591{
Bram Moolenaar33570922005-01-25 22:26:29 +000017592 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017593
17594 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017595 clear_tv(&di->di_tv);
17596 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597}
17598
17599/*
17600 * List the value of one internal variable.
17601 */
17602 static void
17603list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017604 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 char_u *prefix;
17606{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017607 char_u *tofree;
17608 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017609 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017610
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017611 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017612 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017613 s == NULL ? (char_u *)"" : s);
17614 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615}
17616
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617 static void
17618list_one_var_a(prefix, name, type, string)
17619 char_u *prefix;
17620 char_u *name;
17621 int type;
17622 char_u *string;
17623{
17624 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17625 if (name != NULL) /* "a:" vars don't have a name stored */
17626 msg_puts(name);
17627 msg_putchar(' ');
17628 msg_advance(22);
17629 if (type == VAR_NUMBER)
17630 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017631 else if (type == VAR_FUNC)
17632 msg_putchar('*');
17633 else if (type == VAR_LIST)
17634 {
17635 msg_putchar('[');
17636 if (*string == '[')
17637 ++string;
17638 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017639 else if (type == VAR_DICT)
17640 {
17641 msg_putchar('{');
17642 if (*string == '{')
17643 ++string;
17644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 else
17646 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017647
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017649
17650 if (type == VAR_FUNC)
17651 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652}
17653
17654/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017655 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656 * If the variable already exists, the value is updated.
17657 * Otherwise the variable is created.
17658 */
17659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017660set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017662 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017663 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664{
Bram Moolenaar33570922005-01-25 22:26:29 +000017665 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017667 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017668 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017670 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017671 {
17672 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17673 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17674 ? name[2] : name[0]))
17675 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017676 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017677 return;
17678 }
17679 if (function_exists(name))
17680 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017681 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017682 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017683 return;
17684 }
17685 }
17686
Bram Moolenaara7043832005-01-21 11:56:39 +000017687 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017688 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017689 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017690 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017691 return;
17692 }
17693
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017694 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017695 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017697 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017698 if (var_check_ro(v->di_flags, name)
17699 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017700 return;
17701 if (v->di_tv.v_type != tv->v_type
17702 && !((v->di_tv.v_type == VAR_STRING
17703 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017704 && (tv->v_type == VAR_STRING
17705 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017706 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017707 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017708 return;
17709 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017710
17711 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017712 * Handle setting internal v: variables separately: we don't change
17713 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017714 */
17715 if (ht == &vimvarht)
17716 {
17717 if (v->di_tv.v_type == VAR_STRING)
17718 {
17719 vim_free(v->di_tv.vval.v_string);
17720 if (copy || tv->v_type != VAR_STRING)
17721 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17722 else
17723 {
17724 /* Take over the string to avoid an extra alloc/free. */
17725 v->di_tv.vval.v_string = tv->vval.v_string;
17726 tv->vval.v_string = NULL;
17727 }
17728 }
17729 else if (v->di_tv.v_type != VAR_NUMBER)
17730 EMSG2(_(e_intern2), "set_var()");
17731 else
17732 v->di_tv.vval.v_number = get_tv_number(tv);
17733 return;
17734 }
17735
17736 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737 }
17738 else /* add a new variable */
17739 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017740 /* Make sure the variable name is valid. */
17741 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017742 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17743 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017744 {
17745 EMSG2(_(e_illvar), varname);
17746 return;
17747 }
17748
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017749 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17750 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017751 if (v == NULL)
17752 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017753 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017754 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017756 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757 return;
17758 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017759 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017762 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017763 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017764 else
17765 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017766 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017767 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017768 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770}
17771
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017772/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017773 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17774 * Also give an error message.
17775 */
17776 static int
17777var_check_ro(flags, name)
17778 int flags;
17779 char_u *name;
17780{
17781 if (flags & DI_FLAGS_RO)
17782 {
17783 EMSG2(_(e_readonlyvar), name);
17784 return TRUE;
17785 }
17786 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17787 {
17788 EMSG2(_(e_readonlysbx), name);
17789 return TRUE;
17790 }
17791 return FALSE;
17792}
17793
17794/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017795 * Return TRUE if typeval "tv" is set to be locked (immutable).
17796 * Also give an error message, using "name".
17797 */
17798 static int
17799tv_check_lock(lock, name)
17800 int lock;
17801 char_u *name;
17802{
17803 if (lock & VAR_LOCKED)
17804 {
17805 EMSG2(_("E741: Value is locked: %s"),
17806 name == NULL ? (char_u *)_("Unknown") : name);
17807 return TRUE;
17808 }
17809 if (lock & VAR_FIXED)
17810 {
17811 EMSG2(_("E742: Cannot change value of %s"),
17812 name == NULL ? (char_u *)_("Unknown") : name);
17813 return TRUE;
17814 }
17815 return FALSE;
17816}
17817
17818/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017819 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017820 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017821 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017824copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017825 typval_T *from;
17826 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017828 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017829 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017830 switch (from->v_type)
17831 {
17832 case VAR_NUMBER:
17833 to->vval.v_number = from->vval.v_number;
17834 break;
17835 case VAR_STRING:
17836 case VAR_FUNC:
17837 if (from->vval.v_string == NULL)
17838 to->vval.v_string = NULL;
17839 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017840 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017841 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017842 if (from->v_type == VAR_FUNC)
17843 func_ref(to->vval.v_string);
17844 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017845 break;
17846 case VAR_LIST:
17847 if (from->vval.v_list == NULL)
17848 to->vval.v_list = NULL;
17849 else
17850 {
17851 to->vval.v_list = from->vval.v_list;
17852 ++to->vval.v_list->lv_refcount;
17853 }
17854 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017855 case VAR_DICT:
17856 if (from->vval.v_dict == NULL)
17857 to->vval.v_dict = NULL;
17858 else
17859 {
17860 to->vval.v_dict = from->vval.v_dict;
17861 ++to->vval.v_dict->dv_refcount;
17862 }
17863 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017864 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017865 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017866 break;
17867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868}
17869
17870/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017871 * Make a copy of an item.
17872 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017873 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17874 * reference to an already copied list/dict can be used.
17875 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017876 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017877 static int
17878item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017879 typval_T *from;
17880 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017881 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017882 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017883{
17884 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017885 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017886
Bram Moolenaar33570922005-01-25 22:26:29 +000017887 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017888 {
17889 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017890 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017891 }
17892 ++recurse;
17893
17894 switch (from->v_type)
17895 {
17896 case VAR_NUMBER:
17897 case VAR_STRING:
17898 case VAR_FUNC:
17899 copy_tv(from, to);
17900 break;
17901 case VAR_LIST:
17902 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017903 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017904 if (from->vval.v_list == NULL)
17905 to->vval.v_list = NULL;
17906 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17907 {
17908 /* use the copy made earlier */
17909 to->vval.v_list = from->vval.v_list->lv_copylist;
17910 ++to->vval.v_list->lv_refcount;
17911 }
17912 else
17913 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17914 if (to->vval.v_list == NULL)
17915 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017916 break;
17917 case VAR_DICT:
17918 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017919 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017920 if (from->vval.v_dict == NULL)
17921 to->vval.v_dict = NULL;
17922 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17923 {
17924 /* use the copy made earlier */
17925 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17926 ++to->vval.v_dict->dv_refcount;
17927 }
17928 else
17929 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17930 if (to->vval.v_dict == NULL)
17931 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017932 break;
17933 default:
17934 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017935 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017936 }
17937 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017938 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017939}
17940
17941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942 * ":echo expr1 ..." print each argument separated with a space, add a
17943 * newline at the end.
17944 * ":echon expr1 ..." print each argument plain.
17945 */
17946 void
17947ex_echo(eap)
17948 exarg_T *eap;
17949{
17950 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017951 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017952 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953 char_u *p;
17954 int needclr = TRUE;
17955 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017956 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957
17958 if (eap->skip)
17959 ++emsg_skip;
17960 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17961 {
17962 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017963 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964 {
17965 /*
17966 * Report the invalid expression unless the expression evaluation
17967 * has been cancelled due to an aborting error, an interrupt, or an
17968 * exception.
17969 */
17970 if (!aborting())
17971 EMSG2(_(e_invexpr2), p);
17972 break;
17973 }
17974 if (!eap->skip)
17975 {
17976 if (atstart)
17977 {
17978 atstart = FALSE;
17979 /* Call msg_start() after eval1(), evaluating the expression
17980 * may cause a message to appear. */
17981 if (eap->cmdidx == CMD_echo)
17982 msg_start();
17983 }
17984 else if (eap->cmdidx == CMD_echo)
17985 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017986 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017987 if (p != NULL)
17988 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017990 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017992 if (*p != TAB && needclr)
17993 {
17994 /* remove any text still there from the command */
17995 msg_clr_eos();
17996 needclr = FALSE;
17997 }
17998 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017999 }
18000 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018001 {
18002#ifdef FEAT_MBYTE
18003 if (has_mbyte)
18004 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018005 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018006
18007 (void)msg_outtrans_len_attr(p, i, echo_attr);
18008 p += i - 1;
18009 }
18010 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018012 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018015 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018017 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018 arg = skipwhite(arg);
18019 }
18020 eap->nextcmd = check_nextcmd(arg);
18021
18022 if (eap->skip)
18023 --emsg_skip;
18024 else
18025 {
18026 /* remove text that may still be there from the command */
18027 if (needclr)
18028 msg_clr_eos();
18029 if (eap->cmdidx == CMD_echo)
18030 msg_end();
18031 }
18032}
18033
18034/*
18035 * ":echohl {name}".
18036 */
18037 void
18038ex_echohl(eap)
18039 exarg_T *eap;
18040{
18041 int id;
18042
18043 id = syn_name2id(eap->arg);
18044 if (id == 0)
18045 echo_attr = 0;
18046 else
18047 echo_attr = syn_id2attr(id);
18048}
18049
18050/*
18051 * ":execute expr1 ..." execute the result of an expression.
18052 * ":echomsg expr1 ..." Print a message
18053 * ":echoerr expr1 ..." Print an error
18054 * Each gets spaces around each argument and a newline at the end for
18055 * echo commands
18056 */
18057 void
18058ex_execute(eap)
18059 exarg_T *eap;
18060{
18061 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018062 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 int ret = OK;
18064 char_u *p;
18065 garray_T ga;
18066 int len;
18067 int save_did_emsg;
18068
18069 ga_init2(&ga, 1, 80);
18070
18071 if (eap->skip)
18072 ++emsg_skip;
18073 while (*arg != NUL && *arg != '|' && *arg != '\n')
18074 {
18075 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018076 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 {
18078 /*
18079 * Report the invalid expression unless the expression evaluation
18080 * has been cancelled due to an aborting error, an interrupt, or an
18081 * exception.
18082 */
18083 if (!aborting())
18084 EMSG2(_(e_invexpr2), p);
18085 ret = FAIL;
18086 break;
18087 }
18088
18089 if (!eap->skip)
18090 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018091 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 len = (int)STRLEN(p);
18093 if (ga_grow(&ga, len + 2) == FAIL)
18094 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018095 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 ret = FAIL;
18097 break;
18098 }
18099 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 ga.ga_len += len;
18103 }
18104
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018105 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 arg = skipwhite(arg);
18107 }
18108
18109 if (ret != FAIL && ga.ga_data != NULL)
18110 {
18111 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018112 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018114 out_flush();
18115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116 else if (eap->cmdidx == CMD_echoerr)
18117 {
18118 /* We don't want to abort following commands, restore did_emsg. */
18119 save_did_emsg = did_emsg;
18120 EMSG((char_u *)ga.ga_data);
18121 if (!force_abort)
18122 did_emsg = save_did_emsg;
18123 }
18124 else if (eap->cmdidx == CMD_execute)
18125 do_cmdline((char_u *)ga.ga_data,
18126 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18127 }
18128
18129 ga_clear(&ga);
18130
18131 if (eap->skip)
18132 --emsg_skip;
18133
18134 eap->nextcmd = check_nextcmd(arg);
18135}
18136
18137/*
18138 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18139 * "arg" points to the "&" or '+' when called, to "option" when returning.
18140 * Returns NULL when no option name found. Otherwise pointer to the char
18141 * after the option name.
18142 */
18143 static char_u *
18144find_option_end(arg, opt_flags)
18145 char_u **arg;
18146 int *opt_flags;
18147{
18148 char_u *p = *arg;
18149
18150 ++p;
18151 if (*p == 'g' && p[1] == ':')
18152 {
18153 *opt_flags = OPT_GLOBAL;
18154 p += 2;
18155 }
18156 else if (*p == 'l' && p[1] == ':')
18157 {
18158 *opt_flags = OPT_LOCAL;
18159 p += 2;
18160 }
18161 else
18162 *opt_flags = 0;
18163
18164 if (!ASCII_ISALPHA(*p))
18165 return NULL;
18166 *arg = p;
18167
18168 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18169 p += 4; /* termcap option */
18170 else
18171 while (ASCII_ISALPHA(*p))
18172 ++p;
18173 return p;
18174}
18175
18176/*
18177 * ":function"
18178 */
18179 void
18180ex_function(eap)
18181 exarg_T *eap;
18182{
18183 char_u *theline;
18184 int j;
18185 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187 char_u *name = NULL;
18188 char_u *p;
18189 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018190 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191 garray_T newargs;
18192 garray_T newlines;
18193 int varargs = FALSE;
18194 int mustend = FALSE;
18195 int flags = 0;
18196 ufunc_T *fp;
18197 int indent;
18198 int nesting;
18199 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018200 dictitem_T *v;
18201 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018202 static int func_nr = 0; /* number for nameless function */
18203 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018204 hashtab_T *ht;
18205 int todo;
18206 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018207 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208
18209 /*
18210 * ":function" without argument: list functions.
18211 */
18212 if (ends_excmd(*eap->arg))
18213 {
18214 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018215 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018216 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018217 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018218 {
18219 if (!HASHITEM_EMPTY(hi))
18220 {
18221 --todo;
18222 fp = HI2UF(hi);
18223 if (!isdigit(*fp->uf_name))
18224 list_func_head(fp, FALSE);
18225 }
18226 }
18227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228 eap->nextcmd = check_nextcmd(eap->arg);
18229 return;
18230 }
18231
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018232 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018233 * ":function /pat": list functions matching pattern.
18234 */
18235 if (*eap->arg == '/')
18236 {
18237 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18238 if (!eap->skip)
18239 {
18240 regmatch_T regmatch;
18241
18242 c = *p;
18243 *p = NUL;
18244 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18245 *p = c;
18246 if (regmatch.regprog != NULL)
18247 {
18248 regmatch.rm_ic = p_ic;
18249
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018250 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018251 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18252 {
18253 if (!HASHITEM_EMPTY(hi))
18254 {
18255 --todo;
18256 fp = HI2UF(hi);
18257 if (!isdigit(*fp->uf_name)
18258 && vim_regexec(&regmatch, fp->uf_name, 0))
18259 list_func_head(fp, FALSE);
18260 }
18261 }
18262 }
18263 }
18264 if (*p == '/')
18265 ++p;
18266 eap->nextcmd = check_nextcmd(p);
18267 return;
18268 }
18269
18270 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018271 * Get the function name. There are these situations:
18272 * func normal function name
18273 * "name" == func, "fudi.fd_dict" == NULL
18274 * dict.func new dictionary entry
18275 * "name" == NULL, "fudi.fd_dict" set,
18276 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18277 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018278 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018279 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18280 * dict.func existing dict entry that's not a Funcref
18281 * "name" == NULL, "fudi.fd_dict" set,
18282 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018285 name = trans_function_name(&p, eap->skip, 0, &fudi);
18286 paren = (vim_strchr(p, '(') != NULL);
18287 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288 {
18289 /*
18290 * Return on an invalid expression in braces, unless the expression
18291 * evaluation has been cancelled due to an aborting error, an
18292 * interrupt, or an exception.
18293 */
18294 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018295 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018296 if (!eap->skip && fudi.fd_newkey != NULL)
18297 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018298 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 else
18302 eap->skip = TRUE;
18303 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018304
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305 /* An error in a function call during evaluation of an expression in magic
18306 * braces should not cause the function not to be defined. */
18307 saved_did_emsg = did_emsg;
18308 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309
18310 /*
18311 * ":function func" with only function name: list function.
18312 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018313 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314 {
18315 if (!ends_excmd(*skipwhite(p)))
18316 {
18317 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018318 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319 }
18320 eap->nextcmd = check_nextcmd(p);
18321 if (eap->nextcmd != NULL)
18322 *p = NUL;
18323 if (!eap->skip && !got_int)
18324 {
18325 fp = find_func(name);
18326 if (fp != NULL)
18327 {
18328 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018329 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018331 if (FUNCLINE(fp, j) == NULL)
18332 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333 msg_putchar('\n');
18334 msg_outnum((long)(j + 1));
18335 if (j < 9)
18336 msg_putchar(' ');
18337 if (j < 99)
18338 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018339 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340 out_flush(); /* show a line at a time */
18341 ui_breakcheck();
18342 }
18343 if (!got_int)
18344 {
18345 msg_putchar('\n');
18346 msg_puts((char_u *)" endfunction");
18347 }
18348 }
18349 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018350 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018351 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018352 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 }
18354
18355 /*
18356 * ":function name(arg1, arg2)" Define function.
18357 */
18358 p = skipwhite(p);
18359 if (*p != '(')
18360 {
18361 if (!eap->skip)
18362 {
18363 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018364 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365 }
18366 /* attempt to continue by skipping some text */
18367 if (vim_strchr(p, '(') != NULL)
18368 p = vim_strchr(p, '(');
18369 }
18370 p = skipwhite(p + 1);
18371
18372 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18373 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18374
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018375 if (!eap->skip)
18376 {
18377 /* Check the name of the function. */
18378 if (name != NULL)
18379 arg = name;
18380 else
18381 arg = fudi.fd_newkey;
18382 if (arg != NULL)
18383 {
18384 if (*arg == K_SPECIAL)
18385 j = 3;
18386 else
18387 j = 0;
18388 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18389 : eval_isnamec(arg[j])))
18390 ++j;
18391 if (arg[j] != NUL)
18392 emsg_funcname(_(e_invarg2), arg);
18393 }
18394 }
18395
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396 /*
18397 * Isolate the arguments: "arg1, arg2, ...)"
18398 */
18399 while (*p != ')')
18400 {
18401 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18402 {
18403 varargs = TRUE;
18404 p += 3;
18405 mustend = TRUE;
18406 }
18407 else
18408 {
18409 arg = p;
18410 while (ASCII_ISALNUM(*p) || *p == '_')
18411 ++p;
18412 if (arg == p || isdigit(*arg)
18413 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18414 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18415 {
18416 if (!eap->skip)
18417 EMSG2(_("E125: Illegal argument: %s"), arg);
18418 break;
18419 }
18420 if (ga_grow(&newargs, 1) == FAIL)
18421 goto erret;
18422 c = *p;
18423 *p = NUL;
18424 arg = vim_strsave(arg);
18425 if (arg == NULL)
18426 goto erret;
18427 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18428 *p = c;
18429 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018430 if (*p == ',')
18431 ++p;
18432 else
18433 mustend = TRUE;
18434 }
18435 p = skipwhite(p);
18436 if (mustend && *p != ')')
18437 {
18438 if (!eap->skip)
18439 EMSG2(_(e_invarg2), eap->arg);
18440 break;
18441 }
18442 }
18443 ++p; /* skip the ')' */
18444
Bram Moolenaare9a41262005-01-15 22:18:47 +000018445 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446 for (;;)
18447 {
18448 p = skipwhite(p);
18449 if (STRNCMP(p, "range", 5) == 0)
18450 {
18451 flags |= FC_RANGE;
18452 p += 5;
18453 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018454 else if (STRNCMP(p, "dict", 4) == 0)
18455 {
18456 flags |= FC_DICT;
18457 p += 4;
18458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459 else if (STRNCMP(p, "abort", 5) == 0)
18460 {
18461 flags |= FC_ABORT;
18462 p += 5;
18463 }
18464 else
18465 break;
18466 }
18467
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018468 /* When there is a line break use what follows for the function body.
18469 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18470 if (*p == '\n')
18471 line_arg = p + 1;
18472 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473 EMSG(_(e_trailing));
18474
18475 /*
18476 * Read the body of the function, until ":endfunction" is found.
18477 */
18478 if (KeyTyped)
18479 {
18480 /* Check if the function already exists, don't let the user type the
18481 * whole function before telling him it doesn't work! For a script we
18482 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018483 if (!eap->skip && !eap->forceit)
18484 {
18485 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18486 EMSG(_(e_funcdict));
18487 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018488 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018491 if (!eap->skip && did_emsg)
18492 goto erret;
18493
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494 msg_putchar('\n'); /* don't overwrite the function name */
18495 cmdline_row = msg_row;
18496 }
18497
18498 indent = 2;
18499 nesting = 0;
18500 for (;;)
18501 {
18502 msg_scroll = TRUE;
18503 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018504 sourcing_lnum_off = sourcing_lnum;
18505
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018506 if (line_arg != NULL)
18507 {
18508 /* Use eap->arg, split up in parts by line breaks. */
18509 theline = line_arg;
18510 p = vim_strchr(theline, '\n');
18511 if (p == NULL)
18512 line_arg += STRLEN(line_arg);
18513 else
18514 {
18515 *p = NUL;
18516 line_arg = p + 1;
18517 }
18518 }
18519 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520 theline = getcmdline(':', 0L, indent);
18521 else
18522 theline = eap->getline(':', eap->cookie, indent);
18523 if (KeyTyped)
18524 lines_left = Rows - 1;
18525 if (theline == NULL)
18526 {
18527 EMSG(_("E126: Missing :endfunction"));
18528 goto erret;
18529 }
18530
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018531 /* Detect line continuation: sourcing_lnum increased more than one. */
18532 if (sourcing_lnum > sourcing_lnum_off + 1)
18533 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18534 else
18535 sourcing_lnum_off = 0;
18536
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537 if (skip_until != NULL)
18538 {
18539 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18540 * don't check for ":endfunc". */
18541 if (STRCMP(theline, skip_until) == 0)
18542 {
18543 vim_free(skip_until);
18544 skip_until = NULL;
18545 }
18546 }
18547 else
18548 {
18549 /* skip ':' and blanks*/
18550 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18551 ;
18552
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018553 /* Check for "endfunction". */
18554 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018556 if (line_arg == NULL)
18557 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558 break;
18559 }
18560
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018561 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562 * at "end". */
18563 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18564 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018565 else if (STRNCMP(p, "if", 2) == 0
18566 || STRNCMP(p, "wh", 2) == 0
18567 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 || STRNCMP(p, "try", 3) == 0)
18569 indent += 2;
18570
18571 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018572 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018574 if (*p == '!')
18575 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 p += eval_fname_script(p);
18577 if (ASCII_ISALPHA(*p))
18578 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018579 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 if (*skipwhite(p) == '(')
18581 {
18582 ++nesting;
18583 indent += 2;
18584 }
18585 }
18586 }
18587
18588 /* Check for ":append" or ":insert". */
18589 p = skip_range(p, NULL);
18590 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18591 || (p[0] == 'i'
18592 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18593 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18594 skip_until = vim_strsave((char_u *)".");
18595
18596 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18597 arg = skipwhite(skiptowhite(p));
18598 if (arg[0] == '<' && arg[1] =='<'
18599 && ((p[0] == 'p' && p[1] == 'y'
18600 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18601 || (p[0] == 'p' && p[1] == 'e'
18602 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18603 || (p[0] == 't' && p[1] == 'c'
18604 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18605 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18606 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018607 || (p[0] == 'm' && p[1] == 'z'
18608 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609 ))
18610 {
18611 /* ":python <<" continues until a dot, like ":append" */
18612 p = skipwhite(arg + 2);
18613 if (*p == NUL)
18614 skip_until = vim_strsave((char_u *)".");
18615 else
18616 skip_until = vim_strsave(p);
18617 }
18618 }
18619
18620 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018621 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018622 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018623 if (line_arg == NULL)
18624 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018626 }
18627
18628 /* Copy the line to newly allocated memory. get_one_sourceline()
18629 * allocates 250 bytes per line, this saves 80% on average. The cost
18630 * is an extra alloc/free. */
18631 p = vim_strsave(theline);
18632 if (p != NULL)
18633 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018634 if (line_arg == NULL)
18635 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018636 theline = p;
18637 }
18638
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018639 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18640
18641 /* Add NULL lines for continuation lines, so that the line count is
18642 * equal to the index in the growarray. */
18643 while (sourcing_lnum_off-- > 0)
18644 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018645
18646 /* Check for end of eap->arg. */
18647 if (line_arg != NULL && *line_arg == NUL)
18648 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649 }
18650
18651 /* Don't define the function when skipping commands or when an error was
18652 * detected. */
18653 if (eap->skip || did_emsg)
18654 goto erret;
18655
18656 /*
18657 * If there are no errors, add the function
18658 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018659 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018660 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018661 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018662 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018663 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018664 emsg_funcname("E707: Function name conflicts with variable: %s",
18665 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018666 goto erret;
18667 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018668
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018669 fp = find_func(name);
18670 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018672 if (!eap->forceit)
18673 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018674 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018675 goto erret;
18676 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018677 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018678 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018679 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018680 name);
18681 goto erret;
18682 }
18683 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018684 ga_clear_strings(&(fp->uf_args));
18685 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018686 vim_free(name);
18687 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689 }
18690 else
18691 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018692 char numbuf[20];
18693
18694 fp = NULL;
18695 if (fudi.fd_newkey == NULL && !eap->forceit)
18696 {
18697 EMSG(_(e_funcdict));
18698 goto erret;
18699 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018700 if (fudi.fd_di == NULL)
18701 {
18702 /* Can't add a function to a locked dictionary */
18703 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18704 goto erret;
18705 }
18706 /* Can't change an existing function if it is locked */
18707 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18708 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018709
18710 /* Give the function a sequential number. Can only be used with a
18711 * Funcref! */
18712 vim_free(name);
18713 sprintf(numbuf, "%d", ++func_nr);
18714 name = vim_strsave((char_u *)numbuf);
18715 if (name == NULL)
18716 goto erret;
18717 }
18718
18719 if (fp == NULL)
18720 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018721 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018722 {
18723 int slen, plen;
18724 char_u *scriptname;
18725
18726 /* Check that the autoload name matches the script name. */
18727 j = FAIL;
18728 if (sourcing_name != NULL)
18729 {
18730 scriptname = autoload_name(name);
18731 if (scriptname != NULL)
18732 {
18733 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018734 plen = (int)STRLEN(p);
18735 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018736 if (slen > plen && fnamecmp(p,
18737 sourcing_name + slen - plen) == 0)
18738 j = OK;
18739 vim_free(scriptname);
18740 }
18741 }
18742 if (j == FAIL)
18743 {
18744 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18745 goto erret;
18746 }
18747 }
18748
18749 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 if (fp == NULL)
18751 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018752
18753 if (fudi.fd_dict != NULL)
18754 {
18755 if (fudi.fd_di == NULL)
18756 {
18757 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018758 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018759 if (fudi.fd_di == NULL)
18760 {
18761 vim_free(fp);
18762 goto erret;
18763 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018764 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18765 {
18766 vim_free(fudi.fd_di);
18767 goto erret;
18768 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018769 }
18770 else
18771 /* overwrite existing dict entry */
18772 clear_tv(&fudi.fd_di->di_tv);
18773 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018774 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018775 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018776 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018777
18778 /* behave like "dict" was used */
18779 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018780 }
18781
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018783 STRCPY(fp->uf_name, name);
18784 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018786 fp->uf_args = newargs;
18787 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018788#ifdef FEAT_PROFILE
18789 fp->uf_tml_count = NULL;
18790 fp->uf_tml_total = NULL;
18791 fp->uf_tml_self = NULL;
18792 fp->uf_profiling = FALSE;
18793 if (prof_def_func())
18794 func_do_profile(fp);
18795#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018796 fp->uf_varargs = varargs;
18797 fp->uf_flags = flags;
18798 fp->uf_calls = 0;
18799 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018800 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801
18802erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803 ga_clear_strings(&newargs);
18804 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018805ret_free:
18806 vim_free(skip_until);
18807 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810}
18811
18812/*
18813 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018814 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018816 * flags:
18817 * TFN_INT: internal function name OK
18818 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819 * Advances "pp" to just after the function name (if no error).
18820 */
18821 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018822trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 char_u **pp;
18824 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018825 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018826 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018828 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829 char_u *start;
18830 char_u *end;
18831 int lead;
18832 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018834 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018835
18836 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018837 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018839
18840 /* Check for hard coded <SNR>: already translated function ID (from a user
18841 * command). */
18842 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18843 && (*pp)[2] == (int)KE_SNR)
18844 {
18845 *pp += 3;
18846 len = get_id_len(pp) + 3;
18847 return vim_strnsave(start, len);
18848 }
18849
18850 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18851 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018853 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018855
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018856 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18857 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018858 if (end == start)
18859 {
18860 if (!skip)
18861 EMSG(_("E129: Function name required"));
18862 goto theend;
18863 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018864 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018865 {
18866 /*
18867 * Report an invalid expression in braces, unless the expression
18868 * evaluation has been cancelled due to an aborting error, an
18869 * interrupt, or an exception.
18870 */
18871 if (!aborting())
18872 {
18873 if (end != NULL)
18874 EMSG2(_(e_invarg2), start);
18875 }
18876 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018877 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018878 goto theend;
18879 }
18880
18881 if (lv.ll_tv != NULL)
18882 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018883 if (fdp != NULL)
18884 {
18885 fdp->fd_dict = lv.ll_dict;
18886 fdp->fd_newkey = lv.ll_newkey;
18887 lv.ll_newkey = NULL;
18888 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018889 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018890 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18891 {
18892 name = vim_strsave(lv.ll_tv->vval.v_string);
18893 *pp = end;
18894 }
18895 else
18896 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018897 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18898 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018899 EMSG(_(e_funcref));
18900 else
18901 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018902 name = NULL;
18903 }
18904 goto theend;
18905 }
18906
18907 if (lv.ll_name == NULL)
18908 {
18909 /* Error found, but continue after the function name. */
18910 *pp = end;
18911 goto theend;
18912 }
18913
18914 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018915 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018916 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018917 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18918 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18919 {
18920 /* When there was "s:" already or the name expanded to get a
18921 * leading "s:" then remove it. */
18922 lv.ll_name += 2;
18923 len -= 2;
18924 lead = 2;
18925 }
18926 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018927 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018928 {
18929 if (lead == 2) /* skip over "s:" */
18930 lv.ll_name += 2;
18931 len = (int)(end - lv.ll_name);
18932 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018933
18934 /*
18935 * Copy the function name to allocated memory.
18936 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18937 * Accept <SNR>123_name() outside a script.
18938 */
18939 if (skip)
18940 lead = 0; /* do nothing */
18941 else if (lead > 0)
18942 {
18943 lead = 3;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018944 if (eval_fname_sid(lv.ll_exp_name != NULL ? lv.ll_exp_name : *pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018945 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018946 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018947 if (current_SID <= 0)
18948 {
18949 EMSG(_(e_usingsid));
18950 goto theend;
18951 }
18952 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18953 lead += (int)STRLEN(sid_buf);
18954 }
18955 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018956 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018957 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018958 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018959 goto theend;
18960 }
18961 name = alloc((unsigned)(len + lead + 1));
18962 if (name != NULL)
18963 {
18964 if (lead > 0)
18965 {
18966 name[0] = K_SPECIAL;
18967 name[1] = KS_EXTRA;
18968 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018969 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018970 STRCPY(name + 3, sid_buf);
18971 }
18972 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18973 name[len + lead] = NUL;
18974 }
18975 *pp = end;
18976
18977theend:
18978 clear_lval(&lv);
18979 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980}
18981
18982/*
18983 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18984 * Return 2 if "p" starts with "s:".
18985 * Return 0 otherwise.
18986 */
18987 static int
18988eval_fname_script(p)
18989 char_u *p;
18990{
18991 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18992 || STRNICMP(p + 1, "SNR>", 4) == 0))
18993 return 5;
18994 if (p[0] == 's' && p[1] == ':')
18995 return 2;
18996 return 0;
18997}
18998
18999/*
19000 * Return TRUE if "p" starts with "<SID>" or "s:".
19001 * Only works if eval_fname_script() returned non-zero for "p"!
19002 */
19003 static int
19004eval_fname_sid(p)
19005 char_u *p;
19006{
19007 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19008}
19009
19010/*
19011 * List the head of the function: "name(arg1, arg2)".
19012 */
19013 static void
19014list_func_head(fp, indent)
19015 ufunc_T *fp;
19016 int indent;
19017{
19018 int j;
19019
19020 msg_start();
19021 if (indent)
19022 MSG_PUTS(" ");
19023 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019024 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025 {
19026 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019027 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028 }
19029 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019030 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019032 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033 {
19034 if (j)
19035 MSG_PUTS(", ");
19036 msg_puts(FUNCARG(fp, j));
19037 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019038 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039 {
19040 if (j)
19041 MSG_PUTS(", ");
19042 MSG_PUTS("...");
19043 }
19044 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019045 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019046 if (p_verbose > 0)
19047 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048}
19049
19050/*
19051 * Find a function by name, return pointer to it in ufuncs.
19052 * Return NULL for unknown function.
19053 */
19054 static ufunc_T *
19055find_func(name)
19056 char_u *name;
19057{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019058 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019060 hi = hash_find(&func_hashtab, name);
19061 if (!HASHITEM_EMPTY(hi))
19062 return HI2UF(hi);
19063 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064}
19065
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019066#if defined(EXITFREE) || defined(PROTO)
19067 void
19068free_all_functions()
19069{
19070 hashitem_T *hi;
19071
19072 /* Need to start all over every time, because func_free() may change the
19073 * hash table. */
19074 while (func_hashtab.ht_used > 0)
19075 for (hi = func_hashtab.ht_array; ; ++hi)
19076 if (!HASHITEM_EMPTY(hi))
19077 {
19078 func_free(HI2UF(hi));
19079 break;
19080 }
19081}
19082#endif
19083
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019084/*
19085 * Return TRUE if a function "name" exists.
19086 */
19087 static int
19088function_exists(name)
19089 char_u *name;
19090{
19091 char_u *p = name;
19092 int n = FALSE;
19093
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019094 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019095 if (p != NULL)
19096 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019097 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019098 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019099 else
19100 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019101 vim_free(p);
19102 }
19103 return n;
19104}
19105
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019106/*
19107 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019108 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019109 */
19110 static int
19111builtin_function(name)
19112 char_u *name;
19113{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019114 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19115 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019116}
19117
Bram Moolenaar05159a02005-02-26 23:04:13 +000019118#if defined(FEAT_PROFILE) || defined(PROTO)
19119/*
19120 * Start profiling function "fp".
19121 */
19122 static void
19123func_do_profile(fp)
19124 ufunc_T *fp;
19125{
19126 fp->uf_tm_count = 0;
19127 profile_zero(&fp->uf_tm_self);
19128 profile_zero(&fp->uf_tm_total);
19129 if (fp->uf_tml_count == NULL)
19130 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19131 (sizeof(int) * fp->uf_lines.ga_len));
19132 if (fp->uf_tml_total == NULL)
19133 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19134 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19135 if (fp->uf_tml_self == NULL)
19136 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19137 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19138 fp->uf_tml_idx = -1;
19139 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19140 || fp->uf_tml_self == NULL)
19141 return; /* out of memory */
19142
19143 fp->uf_profiling = TRUE;
19144}
19145
19146/*
19147 * Dump the profiling results for all functions in file "fd".
19148 */
19149 void
19150func_dump_profile(fd)
19151 FILE *fd;
19152{
19153 hashitem_T *hi;
19154 int todo;
19155 ufunc_T *fp;
19156 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019157 ufunc_T **sorttab;
19158 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019159
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019160 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019161 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19162
Bram Moolenaar05159a02005-02-26 23:04:13 +000019163 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19164 {
19165 if (!HASHITEM_EMPTY(hi))
19166 {
19167 --todo;
19168 fp = HI2UF(hi);
19169 if (fp->uf_profiling)
19170 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019171 if (sorttab != NULL)
19172 sorttab[st_len++] = fp;
19173
Bram Moolenaar05159a02005-02-26 23:04:13 +000019174 if (fp->uf_name[0] == K_SPECIAL)
19175 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19176 else
19177 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19178 if (fp->uf_tm_count == 1)
19179 fprintf(fd, "Called 1 time\n");
19180 else
19181 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19182 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19183 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19184 fprintf(fd, "\n");
19185 fprintf(fd, "count total (s) self (s)\n");
19186
19187 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19188 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019189 if (FUNCLINE(fp, i) == NULL)
19190 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019191 prof_func_line(fd, fp->uf_tml_count[i],
19192 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019193 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19194 }
19195 fprintf(fd, "\n");
19196 }
19197 }
19198 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019199
19200 if (sorttab != NULL && st_len > 0)
19201 {
19202 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19203 prof_total_cmp);
19204 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19205 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19206 prof_self_cmp);
19207 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19208 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019209}
Bram Moolenaar73830342005-02-28 22:48:19 +000019210
19211 static void
19212prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19213 FILE *fd;
19214 ufunc_T **sorttab;
19215 int st_len;
19216 char *title;
19217 int prefer_self; /* when equal print only self time */
19218{
19219 int i;
19220 ufunc_T *fp;
19221
19222 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19223 fprintf(fd, "count total (s) self (s) function\n");
19224 for (i = 0; i < 20 && i < st_len; ++i)
19225 {
19226 fp = sorttab[i];
19227 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19228 prefer_self);
19229 if (fp->uf_name[0] == K_SPECIAL)
19230 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19231 else
19232 fprintf(fd, " %s()\n", fp->uf_name);
19233 }
19234 fprintf(fd, "\n");
19235}
19236
19237/*
19238 * Print the count and times for one function or function line.
19239 */
19240 static void
19241prof_func_line(fd, count, total, self, prefer_self)
19242 FILE *fd;
19243 int count;
19244 proftime_T *total;
19245 proftime_T *self;
19246 int prefer_self; /* when equal print only self time */
19247{
19248 if (count > 0)
19249 {
19250 fprintf(fd, "%5d ", count);
19251 if (prefer_self && profile_equal(total, self))
19252 fprintf(fd, " ");
19253 else
19254 fprintf(fd, "%s ", profile_msg(total));
19255 if (!prefer_self && profile_equal(total, self))
19256 fprintf(fd, " ");
19257 else
19258 fprintf(fd, "%s ", profile_msg(self));
19259 }
19260 else
19261 fprintf(fd, " ");
19262}
19263
19264/*
19265 * Compare function for total time sorting.
19266 */
19267 static int
19268#ifdef __BORLANDC__
19269_RTLENTRYF
19270#endif
19271prof_total_cmp(s1, s2)
19272 const void *s1;
19273 const void *s2;
19274{
19275 ufunc_T *p1, *p2;
19276
19277 p1 = *(ufunc_T **)s1;
19278 p2 = *(ufunc_T **)s2;
19279 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19280}
19281
19282/*
19283 * Compare function for self time sorting.
19284 */
19285 static int
19286#ifdef __BORLANDC__
19287_RTLENTRYF
19288#endif
19289prof_self_cmp(s1, s2)
19290 const void *s1;
19291 const void *s2;
19292{
19293 ufunc_T *p1, *p2;
19294
19295 p1 = *(ufunc_T **)s1;
19296 p2 = *(ufunc_T **)s2;
19297 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19298}
19299
Bram Moolenaar05159a02005-02-26 23:04:13 +000019300#endif
19301
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019302/* The names of packages that once were loaded is remembered. */
19303static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
19304
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019305/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019306 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019307 * Return TRUE if a package was loaded.
19308 */
19309 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019310script_autoload(name, reload)
19311 char_u *name;
19312 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019313{
19314 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019315 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019316 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019317 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019318
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019319 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019320 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019321 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019322 return FALSE;
19323
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019324 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019325
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019326 /* Find the name in the list of previously loaded package names. Skip
19327 * "autoload/", it's always the same. */
19328 for (i = 0; i < ga_loaded.ga_len; ++i)
19329 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19330 break;
19331 if (!reload && i < ga_loaded.ga_len)
19332 ret = FALSE; /* was loaded already */
19333 else
19334 {
19335 /* Remember the name if it wasn't loaded already. */
19336 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19337 {
19338 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19339 tofree = NULL;
19340 }
19341
19342 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019343 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019344 ret = TRUE;
19345 }
19346
19347 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019348 return ret;
19349}
19350
19351/*
19352 * Return the autoload script name for a function or variable name.
19353 * Returns NULL when out of memory.
19354 */
19355 static char_u *
19356autoload_name(name)
19357 char_u *name;
19358{
19359 char_u *p;
19360 char_u *scriptname;
19361
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019362 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019363 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19364 if (scriptname == NULL)
19365 return FALSE;
19366 STRCPY(scriptname, "autoload/");
19367 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019368 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019369 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019370 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019371 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019372 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019373}
19374
Bram Moolenaar071d4272004-06-13 20:20:40 +000019375#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19376
19377/*
19378 * Function given to ExpandGeneric() to obtain the list of user defined
19379 * function names.
19380 */
19381 char_u *
19382get_user_func_name(xp, idx)
19383 expand_T *xp;
19384 int idx;
19385{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019386 static long_u done;
19387 static hashitem_T *hi;
19388 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389
19390 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019392 done = 0;
19393 hi = func_hashtab.ht_array;
19394 }
19395 if (done < func_hashtab.ht_used)
19396 {
19397 if (done++ > 0)
19398 ++hi;
19399 while (HASHITEM_EMPTY(hi))
19400 ++hi;
19401 fp = HI2UF(hi);
19402
19403 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19404 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405
19406 cat_func_name(IObuff, fp);
19407 if (xp->xp_context != EXPAND_USER_FUNC)
19408 {
19409 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019410 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411 STRCAT(IObuff, ")");
19412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413 return IObuff;
19414 }
19415 return NULL;
19416}
19417
19418#endif /* FEAT_CMDL_COMPL */
19419
19420/*
19421 * Copy the function name of "fp" to buffer "buf".
19422 * "buf" must be able to hold the function name plus three bytes.
19423 * Takes care of script-local function names.
19424 */
19425 static void
19426cat_func_name(buf, fp)
19427 char_u *buf;
19428 ufunc_T *fp;
19429{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019430 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431 {
19432 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019433 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019434 }
19435 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019436 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437}
19438
19439/*
19440 * ":delfunction {name}"
19441 */
19442 void
19443ex_delfunction(eap)
19444 exarg_T *eap;
19445{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019446 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447 char_u *p;
19448 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019449 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450
19451 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019452 name = trans_function_name(&p, eap->skip, 0, &fudi);
19453 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019455 {
19456 if (fudi.fd_dict != NULL && !eap->skip)
19457 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460 if (!ends_excmd(*skipwhite(p)))
19461 {
19462 vim_free(name);
19463 EMSG(_(e_trailing));
19464 return;
19465 }
19466 eap->nextcmd = check_nextcmd(p);
19467 if (eap->nextcmd != NULL)
19468 *p = NUL;
19469
19470 if (!eap->skip)
19471 fp = find_func(name);
19472 vim_free(name);
19473
19474 if (!eap->skip)
19475 {
19476 if (fp == NULL)
19477 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019478 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479 return;
19480 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019481 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 {
19483 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19484 return;
19485 }
19486
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019487 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019489 /* Delete the dict item that refers to the function, it will
19490 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019491 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019493 else
19494 func_free(fp);
19495 }
19496}
19497
19498/*
19499 * Free a function and remove it from the list of functions.
19500 */
19501 static void
19502func_free(fp)
19503 ufunc_T *fp;
19504{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019505 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019506
19507 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019508 ga_clear_strings(&(fp->uf_args));
19509 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019510#ifdef FEAT_PROFILE
19511 vim_free(fp->uf_tml_count);
19512 vim_free(fp->uf_tml_total);
19513 vim_free(fp->uf_tml_self);
19514#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019515
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019516 /* remove the function from the function hashtable */
19517 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19518 if (HASHITEM_EMPTY(hi))
19519 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019520 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019521 hash_remove(&func_hashtab, hi);
19522
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019523 vim_free(fp);
19524}
19525
19526/*
19527 * Unreference a Function: decrement the reference count and free it when it
19528 * becomes zero. Only for numbered functions.
19529 */
19530 static void
19531func_unref(name)
19532 char_u *name;
19533{
19534 ufunc_T *fp;
19535
19536 if (name != NULL && isdigit(*name))
19537 {
19538 fp = find_func(name);
19539 if (fp == NULL)
19540 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019541 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019542 {
19543 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019544 * when "uf_calls" becomes zero. */
19545 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019546 func_free(fp);
19547 }
19548 }
19549}
19550
19551/*
19552 * Count a reference to a Function.
19553 */
19554 static void
19555func_ref(name)
19556 char_u *name;
19557{
19558 ufunc_T *fp;
19559
19560 if (name != NULL && isdigit(*name))
19561 {
19562 fp = find_func(name);
19563 if (fp == NULL)
19564 EMSG2(_(e_intern2), "func_ref()");
19565 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019566 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567 }
19568}
19569
19570/*
19571 * Call a user function.
19572 */
19573 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019574call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019575 ufunc_T *fp; /* pointer to function */
19576 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019577 typval_T *argvars; /* arguments */
19578 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579 linenr_T firstline; /* first line of range */
19580 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019581 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582{
Bram Moolenaar33570922005-01-25 22:26:29 +000019583 char_u *save_sourcing_name;
19584 linenr_T save_sourcing_lnum;
19585 scid_T save_current_SID;
19586 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019587 int save_did_emsg;
19588 static int depth = 0;
19589 dictitem_T *v;
19590 int fixvar_idx = 0; /* index in fixvar[] */
19591 int i;
19592 int ai;
19593 char_u numbuf[NUMBUFLEN];
19594 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019595#ifdef FEAT_PROFILE
19596 proftime_T wait_start;
19597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598
19599 /* If depth of calling is getting too high, don't execute the function */
19600 if (depth >= p_mfd)
19601 {
19602 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019603 rettv->v_type = VAR_NUMBER;
19604 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605 return;
19606 }
19607 ++depth;
19608
19609 line_breakcheck(); /* check for CTRL-C hit */
19610
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019611 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019612 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019614 fc.rettv = rettv;
19615 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 fc.linenr = 0;
19617 fc.returned = FALSE;
19618 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019620 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 fc.dbg_tick = debug_tick;
19622
Bram Moolenaar33570922005-01-25 22:26:29 +000019623 /*
19624 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19625 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19626 * each argument variable and saves a lot of time.
19627 */
19628 /*
19629 * Init l: variables.
19630 */
19631 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019632 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019633 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019634 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19635 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019636 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019637 name = v->di_key;
19638 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019639 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19640 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19641 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019642 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019643 v->di_tv.vval.v_dict = selfdict;
19644 ++selfdict->dv_refcount;
19645 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019646
Bram Moolenaar33570922005-01-25 22:26:29 +000019647 /*
19648 * Init a: variables.
19649 * Set a:0 to "argcount".
19650 * Set a:000 to a list with room for the "..." arguments.
19651 */
19652 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19653 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019654 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019655 v = &fc.fixvar[fixvar_idx++].var;
19656 STRCPY(v->di_key, "000");
19657 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19658 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19659 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019660 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019661 v->di_tv.vval.v_list = &fc.l_varlist;
19662 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19663 fc.l_varlist.lv_refcount = 99999;
19664
19665 /*
19666 * Set a:firstline to "firstline" and a:lastline to "lastline".
19667 * Set a:name to named arguments.
19668 * Set a:N to the "..." arguments.
19669 */
19670 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19671 (varnumber_T)firstline);
19672 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19673 (varnumber_T)lastline);
19674 for (i = 0; i < argcount; ++i)
19675 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019676 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019677 if (ai < 0)
19678 /* named argument a:name */
19679 name = FUNCARG(fp, i);
19680 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019681 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019682 /* "..." argument a:1, a:2, etc. */
19683 sprintf((char *)numbuf, "%d", ai + 1);
19684 name = numbuf;
19685 }
19686 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19687 {
19688 v = &fc.fixvar[fixvar_idx++].var;
19689 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19690 }
19691 else
19692 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019693 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19694 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019695 if (v == NULL)
19696 break;
19697 v->di_flags = DI_FLAGS_RO;
19698 }
19699 STRCPY(v->di_key, name);
19700 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19701
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019702 /* Note: the values are copied directly to avoid alloc/free.
19703 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019704 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019705 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019706
19707 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19708 {
19709 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19710 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019711 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019712 }
19713 }
19714
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715 /* Don't redraw while executing the function. */
19716 ++RedrawingDisabled;
19717 save_sourcing_name = sourcing_name;
19718 save_sourcing_lnum = sourcing_lnum;
19719 sourcing_lnum = 1;
19720 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019721 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722 if (sourcing_name != NULL)
19723 {
19724 if (save_sourcing_name != NULL
19725 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19726 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19727 else
19728 STRCPY(sourcing_name, "function ");
19729 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19730
19731 if (p_verbose >= 12)
19732 {
19733 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019734 verbose_enter_scroll();
19735
Bram Moolenaar555b2802005-05-19 21:08:39 +000019736 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737 if (p_verbose >= 14)
19738 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019739 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019740 char_u numbuf[NUMBUFLEN];
19741 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019742
19743 msg_puts((char_u *)"(");
19744 for (i = 0; i < argcount; ++i)
19745 {
19746 if (i > 0)
19747 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019748 if (argvars[i].v_type == VAR_NUMBER)
19749 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750 else
19751 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019752 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019753 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019755 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756 }
19757 }
19758 msg_puts((char_u *)")");
19759 }
19760 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019761
19762 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763 --no_wait_return;
19764 }
19765 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019766#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019767 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019768 {
19769 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19770 func_do_profile(fp);
19771 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019772 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019773 {
19774 ++fp->uf_tm_count;
19775 profile_start(&fp->uf_tm_start);
19776 profile_zero(&fp->uf_tm_children);
19777 }
19778 script_prof_save(&wait_start);
19779 }
19780#endif
19781
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019783 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784 save_did_emsg = did_emsg;
19785 did_emsg = FALSE;
19786
19787 /* call do_cmdline() to execute the lines */
19788 do_cmdline(NULL, get_func_line, (void *)&fc,
19789 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19790
19791 --RedrawingDisabled;
19792
19793 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019794 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019796 clear_tv(rettv);
19797 rettv->v_type = VAR_NUMBER;
19798 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 }
19800
Bram Moolenaar05159a02005-02-26 23:04:13 +000019801#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019802 if (do_profiling == PROF_YES && (fp->uf_profiling
19803 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019804 {
19805 profile_end(&fp->uf_tm_start);
19806 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19807 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019808 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019809 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019810 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019811 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19812 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019813 }
19814 }
19815#endif
19816
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 /* when being verbose, mention the return value */
19818 if (p_verbose >= 12)
19819 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019821 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019824 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019825 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019826 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19827 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019828 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019830 char_u buf[MSG_BUF_LEN];
19831 char_u numbuf[NUMBUFLEN];
19832 char_u *tofree;
19833
Bram Moolenaar555b2802005-05-19 21:08:39 +000019834 /* The value may be very long. Skip the middle part, so that we
19835 * have some idea how it starts and ends. smsg() would always
19836 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019837 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019838 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019839 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019840 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841 }
19842 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019843
19844 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845 --no_wait_return;
19846 }
19847
19848 vim_free(sourcing_name);
19849 sourcing_name = save_sourcing_name;
19850 sourcing_lnum = save_sourcing_lnum;
19851 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019852#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019853 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019854 script_prof_restore(&wait_start);
19855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856
19857 if (p_verbose >= 12 && sourcing_name != NULL)
19858 {
19859 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019860 verbose_enter_scroll();
19861
Bram Moolenaar555b2802005-05-19 21:08:39 +000019862 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019864
19865 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866 --no_wait_return;
19867 }
19868
19869 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019870 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871
Bram Moolenaar33570922005-01-25 22:26:29 +000019872 /* The a: variables typevals were not alloced, only free the allocated
19873 * variables. */
19874 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19875
19876 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877 --depth;
19878}
19879
19880/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019881 * Add a number variable "name" to dict "dp" with value "nr".
19882 */
19883 static void
19884add_nr_var(dp, v, name, nr)
19885 dict_T *dp;
19886 dictitem_T *v;
19887 char *name;
19888 varnumber_T nr;
19889{
19890 STRCPY(v->di_key, name);
19891 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19892 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19893 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019894 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019895 v->di_tv.vval.v_number = nr;
19896}
19897
19898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 * ":return [expr]"
19900 */
19901 void
19902ex_return(eap)
19903 exarg_T *eap;
19904{
19905 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019906 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 int returning = FALSE;
19908
19909 if (current_funccal == NULL)
19910 {
19911 EMSG(_("E133: :return not inside a function"));
19912 return;
19913 }
19914
19915 if (eap->skip)
19916 ++emsg_skip;
19917
19918 eap->nextcmd = NULL;
19919 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019920 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 {
19922 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019923 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019925 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 }
19927 /* It's safer to return also on error. */
19928 else if (!eap->skip)
19929 {
19930 /*
19931 * Return unless the expression evaluation has been cancelled due to an
19932 * aborting error, an interrupt, or an exception.
19933 */
19934 if (!aborting())
19935 returning = do_return(eap, FALSE, TRUE, NULL);
19936 }
19937
19938 /* When skipping or the return gets pending, advance to the next command
19939 * in this line (!returning). Otherwise, ignore the rest of the line.
19940 * Following lines will be ignored by get_func_line(). */
19941 if (returning)
19942 eap->nextcmd = NULL;
19943 else if (eap->nextcmd == NULL) /* no argument */
19944 eap->nextcmd = check_nextcmd(arg);
19945
19946 if (eap->skip)
19947 --emsg_skip;
19948}
19949
19950/*
19951 * Return from a function. Possibly makes the return pending. Also called
19952 * for a pending return at the ":endtry" or after returning from an extra
19953 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019954 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019955 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956 * FALSE when the return gets pending.
19957 */
19958 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019959do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960 exarg_T *eap;
19961 int reanimate;
19962 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019963 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964{
19965 int idx;
19966 struct condstack *cstack = eap->cstack;
19967
19968 if (reanimate)
19969 /* Undo the return. */
19970 current_funccal->returned = FALSE;
19971
19972 /*
19973 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19974 * not in its finally clause (which then is to be executed next) is found.
19975 * In this case, make the ":return" pending for execution at the ":endtry".
19976 * Otherwise, return normally.
19977 */
19978 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19979 if (idx >= 0)
19980 {
19981 cstack->cs_pending[idx] = CSTP_RETURN;
19982
19983 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019984 /* A pending return again gets pending. "rettv" points to an
19985 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019987 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988 else
19989 {
19990 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019991 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019993 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019995 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996 {
19997 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019998 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019999 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 else
20001 EMSG(_(e_outofmem));
20002 }
20003 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020004 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005
20006 if (reanimate)
20007 {
20008 /* The pending return value could be overwritten by a ":return"
20009 * without argument in a finally clause; reset the default
20010 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020011 current_funccal->rettv->v_type = VAR_NUMBER;
20012 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013 }
20014 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020015 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016 }
20017 else
20018 {
20019 current_funccal->returned = TRUE;
20020
20021 /* If the return is carried out now, store the return value. For
20022 * a return immediately after reanimation, the value is already
20023 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020024 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020026 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020027 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020029 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030 }
20031 }
20032
20033 return idx < 0;
20034}
20035
20036/*
20037 * Free the variable with a pending return value.
20038 */
20039 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020040discard_pending_return(rettv)
20041 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042{
Bram Moolenaar33570922005-01-25 22:26:29 +000020043 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044}
20045
20046/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020047 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 * is an allocated string. Used by report_pending() for verbose messages.
20049 */
20050 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020051get_return_cmd(rettv)
20052 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020054 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020055 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020056 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020058 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020059 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020060 if (s == NULL)
20061 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020062
20063 STRCPY(IObuff, ":return ");
20064 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20065 if (STRLEN(s) + 8 >= IOSIZE)
20066 STRCPY(IObuff + IOSIZE - 4, "...");
20067 vim_free(tofree);
20068 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069}
20070
20071/*
20072 * Get next function line.
20073 * Called by do_cmdline() to get the next line.
20074 * Returns allocated string, or NULL for end of function.
20075 */
20076/* ARGSUSED */
20077 char_u *
20078get_func_line(c, cookie, indent)
20079 int c; /* not used */
20080 void *cookie;
20081 int indent; /* not used */
20082{
Bram Moolenaar33570922005-01-25 22:26:29 +000020083 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020084 ufunc_T *fp = fcp->func;
20085 char_u *retval;
20086 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087
20088 /* If breakpoints have been added/deleted need to check for it. */
20089 if (fcp->dbg_tick != debug_tick)
20090 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020091 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092 sourcing_lnum);
20093 fcp->dbg_tick = debug_tick;
20094 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020095#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020096 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020097 func_line_end(cookie);
20098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099
Bram Moolenaar05159a02005-02-26 23:04:13 +000020100 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020101 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20102 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103 retval = NULL;
20104 else
20105 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020106 /* Skip NULL lines (continuation lines). */
20107 while (fcp->linenr < gap->ga_len
20108 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20109 ++fcp->linenr;
20110 if (fcp->linenr >= gap->ga_len)
20111 retval = NULL;
20112 else
20113 {
20114 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20115 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020116#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020117 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020118 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020119#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 }
20122
20123 /* Did we encounter a breakpoint? */
20124 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20125 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020126 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020128 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129 sourcing_lnum);
20130 fcp->dbg_tick = debug_tick;
20131 }
20132
20133 return retval;
20134}
20135
Bram Moolenaar05159a02005-02-26 23:04:13 +000020136#if defined(FEAT_PROFILE) || defined(PROTO)
20137/*
20138 * Called when starting to read a function line.
20139 * "sourcing_lnum" must be correct!
20140 * When skipping lines it may not actually be executed, but we won't find out
20141 * until later and we need to store the time now.
20142 */
20143 void
20144func_line_start(cookie)
20145 void *cookie;
20146{
20147 funccall_T *fcp = (funccall_T *)cookie;
20148 ufunc_T *fp = fcp->func;
20149
20150 if (fp->uf_profiling && sourcing_lnum >= 1
20151 && sourcing_lnum <= fp->uf_lines.ga_len)
20152 {
20153 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020154 /* Skip continuation lines. */
20155 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20156 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020157 fp->uf_tml_execed = FALSE;
20158 profile_start(&fp->uf_tml_start);
20159 profile_zero(&fp->uf_tml_children);
20160 profile_get_wait(&fp->uf_tml_wait);
20161 }
20162}
20163
20164/*
20165 * Called when actually executing a function line.
20166 */
20167 void
20168func_line_exec(cookie)
20169 void *cookie;
20170{
20171 funccall_T *fcp = (funccall_T *)cookie;
20172 ufunc_T *fp = fcp->func;
20173
20174 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20175 fp->uf_tml_execed = TRUE;
20176}
20177
20178/*
20179 * Called when done with a function line.
20180 */
20181 void
20182func_line_end(cookie)
20183 void *cookie;
20184{
20185 funccall_T *fcp = (funccall_T *)cookie;
20186 ufunc_T *fp = fcp->func;
20187
20188 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20189 {
20190 if (fp->uf_tml_execed)
20191 {
20192 ++fp->uf_tml_count[fp->uf_tml_idx];
20193 profile_end(&fp->uf_tml_start);
20194 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020195 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020196 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20197 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020198 }
20199 fp->uf_tml_idx = -1;
20200 }
20201}
20202#endif
20203
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204/*
20205 * Return TRUE if the currently active function should be ended, because a
20206 * return was encountered or an error occured. Used inside a ":while".
20207 */
20208 int
20209func_has_ended(cookie)
20210 void *cookie;
20211{
Bram Moolenaar33570922005-01-25 22:26:29 +000020212 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213
20214 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20215 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020216 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 || fcp->returned);
20218}
20219
20220/*
20221 * return TRUE if cookie indicates a function which "abort"s on errors.
20222 */
20223 int
20224func_has_abort(cookie)
20225 void *cookie;
20226{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020227 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228}
20229
20230#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20231typedef enum
20232{
20233 VAR_FLAVOUR_DEFAULT,
20234 VAR_FLAVOUR_SESSION,
20235 VAR_FLAVOUR_VIMINFO
20236} var_flavour_T;
20237
20238static var_flavour_T var_flavour __ARGS((char_u *varname));
20239
20240 static var_flavour_T
20241var_flavour(varname)
20242 char_u *varname;
20243{
20244 char_u *p = varname;
20245
20246 if (ASCII_ISUPPER(*p))
20247 {
20248 while (*(++p))
20249 if (ASCII_ISLOWER(*p))
20250 return VAR_FLAVOUR_SESSION;
20251 return VAR_FLAVOUR_VIMINFO;
20252 }
20253 else
20254 return VAR_FLAVOUR_DEFAULT;
20255}
20256#endif
20257
20258#if defined(FEAT_VIMINFO) || defined(PROTO)
20259/*
20260 * Restore global vars that start with a capital from the viminfo file
20261 */
20262 int
20263read_viminfo_varlist(virp, writing)
20264 vir_T *virp;
20265 int writing;
20266{
20267 char_u *tab;
20268 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020269 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270
20271 if (!writing && (find_viminfo_parameter('!') != NULL))
20272 {
20273 tab = vim_strchr(virp->vir_line + 1, '\t');
20274 if (tab != NULL)
20275 {
20276 *tab++ = '\0'; /* isolate the variable name */
20277 if (*tab == 'S') /* string var */
20278 is_string = TRUE;
20279
20280 tab = vim_strchr(tab, '\t');
20281 if (tab != NULL)
20282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 if (is_string)
20284 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020285 tv.v_type = VAR_STRING;
20286 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288 }
20289 else
20290 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020291 tv.v_type = VAR_NUMBER;
20292 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020294 set_var(virp->vir_line + 1, &tv, FALSE);
20295 if (is_string)
20296 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297 }
20298 }
20299 }
20300
20301 return viminfo_readline(virp);
20302}
20303
20304/*
20305 * Write global vars that start with a capital to the viminfo file
20306 */
20307 void
20308write_viminfo_varlist(fp)
20309 FILE *fp;
20310{
Bram Moolenaar33570922005-01-25 22:26:29 +000020311 hashitem_T *hi;
20312 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020313 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020314 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020315 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020316 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020317 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318
20319 if (find_viminfo_parameter('!') == NULL)
20320 return;
20321
20322 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020323
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020324 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020325 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020327 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020329 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020330 this_var = HI2DI(hi);
20331 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020332 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020333 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020334 {
20335 case VAR_STRING: s = "STR"; break;
20336 case VAR_NUMBER: s = "NUM"; break;
20337 default: continue;
20338 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020339 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020340 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020341 if (p != NULL)
20342 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020343 vim_free(tofree);
20344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 }
20346 }
20347}
20348#endif
20349
20350#if defined(FEAT_SESSION) || defined(PROTO)
20351 int
20352store_session_globals(fd)
20353 FILE *fd;
20354{
Bram Moolenaar33570922005-01-25 22:26:29 +000020355 hashitem_T *hi;
20356 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020357 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 char_u *p, *t;
20359
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020360 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020361 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020363 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020365 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020366 this_var = HI2DI(hi);
20367 if ((this_var->di_tv.v_type == VAR_NUMBER
20368 || this_var->di_tv.v_type == VAR_STRING)
20369 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020370 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020371 /* Escape special characters with a backslash. Turn a LF and
20372 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020373 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020374 (char_u *)"\\\"\n\r");
20375 if (p == NULL) /* out of memory */
20376 break;
20377 for (t = p; *t != NUL; ++t)
20378 if (*t == '\n')
20379 *t = 'n';
20380 else if (*t == '\r')
20381 *t = 'r';
20382 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020383 this_var->di_key,
20384 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20385 : ' ',
20386 p,
20387 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20388 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020389 || put_eol(fd) == FAIL)
20390 {
20391 vim_free(p);
20392 return FAIL;
20393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 vim_free(p);
20395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 }
20397 }
20398 return OK;
20399}
20400#endif
20401
Bram Moolenaar661b1822005-07-28 22:36:45 +000020402/*
20403 * Display script name where an item was last set.
20404 * Should only be invoked when 'verbose' is non-zero.
20405 */
20406 void
20407last_set_msg(scriptID)
20408 scid_T scriptID;
20409{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020410 char_u *p;
20411
Bram Moolenaar661b1822005-07-28 22:36:45 +000020412 if (scriptID != 0)
20413 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020414 p = home_replace_save(NULL, get_scriptname(scriptID));
20415 if (p != NULL)
20416 {
20417 verbose_enter();
20418 MSG_PUTS(_("\n\tLast set from "));
20419 MSG_PUTS(p);
20420 vim_free(p);
20421 verbose_leave();
20422 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020423 }
20424}
20425
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426#endif /* FEAT_EVAL */
20427
20428#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20429
20430
20431#ifdef WIN3264
20432/*
20433 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20434 */
20435static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20436static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20437static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20438
20439/*
20440 * Get the short pathname of a file.
20441 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20442 */
20443 static int
20444get_short_pathname(fnamep, bufp, fnamelen)
20445 char_u **fnamep;
20446 char_u **bufp;
20447 int *fnamelen;
20448{
20449 int l,len;
20450 char_u *newbuf;
20451
20452 len = *fnamelen;
20453
20454 l = GetShortPathName(*fnamep, *fnamep, len);
20455 if (l > len - 1)
20456 {
20457 /* If that doesn't work (not enough space), then save the string
20458 * and try again with a new buffer big enough
20459 */
20460 newbuf = vim_strnsave(*fnamep, l);
20461 if (newbuf == NULL)
20462 return 0;
20463
20464 vim_free(*bufp);
20465 *fnamep = *bufp = newbuf;
20466
20467 l = GetShortPathName(*fnamep,*fnamep,l+1);
20468
20469 /* Really should always succeed, as the buffer is big enough */
20470 }
20471
20472 *fnamelen = l;
20473 return 1;
20474}
20475
20476/*
20477 * Create a short path name. Returns the length of the buffer it needs.
20478 * Doesn't copy over the end of the buffer passed in.
20479 */
20480 static int
20481shortpath_for_invalid_fname(fname, bufp, fnamelen)
20482 char_u **fname;
20483 char_u **bufp;
20484 int *fnamelen;
20485{
20486 char_u *s, *p, *pbuf2, *pbuf3;
20487 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020488 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489
20490 /* Make a copy */
20491 len2 = *fnamelen;
20492 pbuf2 = vim_strnsave(*fname, len2);
20493 pbuf3 = NULL;
20494
20495 s = pbuf2 + len2 - 1; /* Find the end */
20496 slen = 1;
20497 plen = len2;
20498
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020499 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500 {
20501 --s;
20502 ++slen;
20503 --plen;
20504 }
20505
20506 do
20507 {
20508 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020509 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 {
20511 --s;
20512 ++slen;
20513 --plen;
20514 }
20515 if (s <= pbuf2)
20516 break;
20517
20518 /* Remeber the character that is about to be blatted */
20519 ch = *s;
20520 *s = 0; /* get_short_pathname requires a null-terminated string */
20521
20522 /* Try it in situ */
20523 p = pbuf2;
20524 if (!get_short_pathname(&p, &pbuf3, &plen))
20525 {
20526 vim_free(pbuf2);
20527 return -1;
20528 }
20529 *s = ch; /* Preserve the string */
20530 } while (plen == 0);
20531
20532 if (plen > 0)
20533 {
20534 /* Remeber the length of the new string. */
20535 *fnamelen = len = plen + slen;
20536 vim_free(*bufp);
20537 if (len > len2)
20538 {
20539 /* If there's not enough space in the currently allocated string,
20540 * then copy it to a buffer big enough.
20541 */
20542 *fname= *bufp = vim_strnsave(p, len);
20543 if (*fname == NULL)
20544 return -1;
20545 }
20546 else
20547 {
20548 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20549 *fname = *bufp = pbuf2;
20550 if (p != pbuf2)
20551 strncpy(*fname, p, plen);
20552 pbuf2 = NULL;
20553 }
20554 /* Concat the next bit */
20555 strncpy(*fname + plen, s, slen);
20556 (*fname)[len] = '\0';
20557 }
20558 vim_free(pbuf3);
20559 vim_free(pbuf2);
20560 return 0;
20561}
20562
20563/*
20564 * Get a pathname for a partial path.
20565 */
20566 static int
20567shortpath_for_partial(fnamep, bufp, fnamelen)
20568 char_u **fnamep;
20569 char_u **bufp;
20570 int *fnamelen;
20571{
20572 int sepcount, len, tflen;
20573 char_u *p;
20574 char_u *pbuf, *tfname;
20575 int hasTilde;
20576
20577 /* Count up the path seperators from the RHS.. so we know which part
20578 * of the path to return.
20579 */
20580 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020581 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582 if (vim_ispathsep(*p))
20583 ++sepcount;
20584
20585 /* Need full path first (use expand_env() to remove a "~/") */
20586 hasTilde = (**fnamep == '~');
20587 if (hasTilde)
20588 pbuf = tfname = expand_env_save(*fnamep);
20589 else
20590 pbuf = tfname = FullName_save(*fnamep, FALSE);
20591
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020592 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020593
20594 if (!get_short_pathname(&tfname, &pbuf, &len))
20595 return -1;
20596
20597 if (len == 0)
20598 {
20599 /* Don't have a valid filename, so shorten the rest of the
20600 * path if we can. This CAN give us invalid 8.3 filenames, but
20601 * there's not a lot of point in guessing what it might be.
20602 */
20603 len = tflen;
20604 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20605 return -1;
20606 }
20607
20608 /* Count the paths backward to find the beginning of the desired string. */
20609 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020610 {
20611#ifdef FEAT_MBYTE
20612 if (has_mbyte)
20613 p -= mb_head_off(tfname, p);
20614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615 if (vim_ispathsep(*p))
20616 {
20617 if (sepcount == 0 || (hasTilde && sepcount == 1))
20618 break;
20619 else
20620 sepcount --;
20621 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020623 if (hasTilde)
20624 {
20625 --p;
20626 if (p >= tfname)
20627 *p = '~';
20628 else
20629 return -1;
20630 }
20631 else
20632 ++p;
20633
20634 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20635 vim_free(*bufp);
20636 *fnamelen = (int)STRLEN(p);
20637 *bufp = pbuf;
20638 *fnamep = p;
20639
20640 return 0;
20641}
20642#endif /* WIN3264 */
20643
20644/*
20645 * Adjust a filename, according to a string of modifiers.
20646 * *fnamep must be NUL terminated when called. When returning, the length is
20647 * determined by *fnamelen.
20648 * Returns valid flags.
20649 * When there is an error, *fnamep is set to NULL.
20650 */
20651 int
20652modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20653 char_u *src; /* string with modifiers */
20654 int *usedlen; /* characters after src that are used */
20655 char_u **fnamep; /* file name so far */
20656 char_u **bufp; /* buffer for allocated file name or NULL */
20657 int *fnamelen; /* length of fnamep */
20658{
20659 int valid = 0;
20660 char_u *tail;
20661 char_u *s, *p, *pbuf;
20662 char_u dirname[MAXPATHL];
20663 int c;
20664 int has_fullname = 0;
20665#ifdef WIN3264
20666 int has_shortname = 0;
20667#endif
20668
20669repeat:
20670 /* ":p" - full path/file_name */
20671 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20672 {
20673 has_fullname = 1;
20674
20675 valid |= VALID_PATH;
20676 *usedlen += 2;
20677
20678 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20679 if ((*fnamep)[0] == '~'
20680#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20681 && ((*fnamep)[1] == '/'
20682# ifdef BACKSLASH_IN_FILENAME
20683 || (*fnamep)[1] == '\\'
20684# endif
20685 || (*fnamep)[1] == NUL)
20686
20687#endif
20688 )
20689 {
20690 *fnamep = expand_env_save(*fnamep);
20691 vim_free(*bufp); /* free any allocated file name */
20692 *bufp = *fnamep;
20693 if (*fnamep == NULL)
20694 return -1;
20695 }
20696
20697 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020698 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699 {
20700 if (vim_ispathsep(*p)
20701 && p[1] == '.'
20702 && (p[2] == NUL
20703 || vim_ispathsep(p[2])
20704 || (p[2] == '.'
20705 && (p[3] == NUL || vim_ispathsep(p[3])))))
20706 break;
20707 }
20708
20709 /* FullName_save() is slow, don't use it when not needed. */
20710 if (*p != NUL || !vim_isAbsName(*fnamep))
20711 {
20712 *fnamep = FullName_save(*fnamep, *p != NUL);
20713 vim_free(*bufp); /* free any allocated file name */
20714 *bufp = *fnamep;
20715 if (*fnamep == NULL)
20716 return -1;
20717 }
20718
20719 /* Append a path separator to a directory. */
20720 if (mch_isdir(*fnamep))
20721 {
20722 /* Make room for one or two extra characters. */
20723 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20724 vim_free(*bufp); /* free any allocated file name */
20725 *bufp = *fnamep;
20726 if (*fnamep == NULL)
20727 return -1;
20728 add_pathsep(*fnamep);
20729 }
20730 }
20731
20732 /* ":." - path relative to the current directory */
20733 /* ":~" - path relative to the home directory */
20734 /* ":8" - shortname path - postponed till after */
20735 while (src[*usedlen] == ':'
20736 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20737 {
20738 *usedlen += 2;
20739 if (c == '8')
20740 {
20741#ifdef WIN3264
20742 has_shortname = 1; /* Postpone this. */
20743#endif
20744 continue;
20745 }
20746 pbuf = NULL;
20747 /* Need full path first (use expand_env() to remove a "~/") */
20748 if (!has_fullname)
20749 {
20750 if (c == '.' && **fnamep == '~')
20751 p = pbuf = expand_env_save(*fnamep);
20752 else
20753 p = pbuf = FullName_save(*fnamep, FALSE);
20754 }
20755 else
20756 p = *fnamep;
20757
20758 has_fullname = 0;
20759
20760 if (p != NULL)
20761 {
20762 if (c == '.')
20763 {
20764 mch_dirname(dirname, MAXPATHL);
20765 s = shorten_fname(p, dirname);
20766 if (s != NULL)
20767 {
20768 *fnamep = s;
20769 if (pbuf != NULL)
20770 {
20771 vim_free(*bufp); /* free any allocated file name */
20772 *bufp = pbuf;
20773 pbuf = NULL;
20774 }
20775 }
20776 }
20777 else
20778 {
20779 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20780 /* Only replace it when it starts with '~' */
20781 if (*dirname == '~')
20782 {
20783 s = vim_strsave(dirname);
20784 if (s != NULL)
20785 {
20786 *fnamep = s;
20787 vim_free(*bufp);
20788 *bufp = s;
20789 }
20790 }
20791 }
20792 vim_free(pbuf);
20793 }
20794 }
20795
20796 tail = gettail(*fnamep);
20797 *fnamelen = (int)STRLEN(*fnamep);
20798
20799 /* ":h" - head, remove "/file_name", can be repeated */
20800 /* Don't remove the first "/" or "c:\" */
20801 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20802 {
20803 valid |= VALID_HEAD;
20804 *usedlen += 2;
20805 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020806 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 --tail;
20808 *fnamelen = (int)(tail - *fnamep);
20809#ifdef VMS
20810 if (*fnamelen > 0)
20811 *fnamelen += 1; /* the path separator is part of the path */
20812#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020813 while (tail > s && !after_pathsep(s, tail))
20814 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020815 }
20816
20817 /* ":8" - shortname */
20818 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20819 {
20820 *usedlen += 2;
20821#ifdef WIN3264
20822 has_shortname = 1;
20823#endif
20824 }
20825
20826#ifdef WIN3264
20827 /* Check shortname after we have done 'heads' and before we do 'tails'
20828 */
20829 if (has_shortname)
20830 {
20831 pbuf = NULL;
20832 /* Copy the string if it is shortened by :h */
20833 if (*fnamelen < (int)STRLEN(*fnamep))
20834 {
20835 p = vim_strnsave(*fnamep, *fnamelen);
20836 if (p == 0)
20837 return -1;
20838 vim_free(*bufp);
20839 *bufp = *fnamep = p;
20840 }
20841
20842 /* Split into two implementations - makes it easier. First is where
20843 * there isn't a full name already, second is where there is.
20844 */
20845 if (!has_fullname && !vim_isAbsName(*fnamep))
20846 {
20847 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20848 return -1;
20849 }
20850 else
20851 {
20852 int l;
20853
20854 /* Simple case, already have the full-name
20855 * Nearly always shorter, so try first time. */
20856 l = *fnamelen;
20857 if (!get_short_pathname(fnamep, bufp, &l))
20858 return -1;
20859
20860 if (l == 0)
20861 {
20862 /* Couldn't find the filename.. search the paths.
20863 */
20864 l = *fnamelen;
20865 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20866 return -1;
20867 }
20868 *fnamelen = l;
20869 }
20870 }
20871#endif /* WIN3264 */
20872
20873 /* ":t" - tail, just the basename */
20874 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20875 {
20876 *usedlen += 2;
20877 *fnamelen -= (int)(tail - *fnamep);
20878 *fnamep = tail;
20879 }
20880
20881 /* ":e" - extension, can be repeated */
20882 /* ":r" - root, without extension, can be repeated */
20883 while (src[*usedlen] == ':'
20884 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20885 {
20886 /* find a '.' in the tail:
20887 * - for second :e: before the current fname
20888 * - otherwise: The last '.'
20889 */
20890 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20891 s = *fnamep - 2;
20892 else
20893 s = *fnamep + *fnamelen - 1;
20894 for ( ; s > tail; --s)
20895 if (s[0] == '.')
20896 break;
20897 if (src[*usedlen + 1] == 'e') /* :e */
20898 {
20899 if (s > tail)
20900 {
20901 *fnamelen += (int)(*fnamep - (s + 1));
20902 *fnamep = s + 1;
20903#ifdef VMS
20904 /* cut version from the extension */
20905 s = *fnamep + *fnamelen - 1;
20906 for ( ; s > *fnamep; --s)
20907 if (s[0] == ';')
20908 break;
20909 if (s > *fnamep)
20910 *fnamelen = s - *fnamep;
20911#endif
20912 }
20913 else if (*fnamep <= tail)
20914 *fnamelen = 0;
20915 }
20916 else /* :r */
20917 {
20918 if (s > tail) /* remove one extension */
20919 *fnamelen = (int)(s - *fnamep);
20920 }
20921 *usedlen += 2;
20922 }
20923
20924 /* ":s?pat?foo?" - substitute */
20925 /* ":gs?pat?foo?" - global substitute */
20926 if (src[*usedlen] == ':'
20927 && (src[*usedlen + 1] == 's'
20928 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20929 {
20930 char_u *str;
20931 char_u *pat;
20932 char_u *sub;
20933 int sep;
20934 char_u *flags;
20935 int didit = FALSE;
20936
20937 flags = (char_u *)"";
20938 s = src + *usedlen + 2;
20939 if (src[*usedlen + 1] == 'g')
20940 {
20941 flags = (char_u *)"g";
20942 ++s;
20943 }
20944
20945 sep = *s++;
20946 if (sep)
20947 {
20948 /* find end of pattern */
20949 p = vim_strchr(s, sep);
20950 if (p != NULL)
20951 {
20952 pat = vim_strnsave(s, (int)(p - s));
20953 if (pat != NULL)
20954 {
20955 s = p + 1;
20956 /* find end of substitution */
20957 p = vim_strchr(s, sep);
20958 if (p != NULL)
20959 {
20960 sub = vim_strnsave(s, (int)(p - s));
20961 str = vim_strnsave(*fnamep, *fnamelen);
20962 if (sub != NULL && str != NULL)
20963 {
20964 *usedlen = (int)(p + 1 - src);
20965 s = do_string_sub(str, pat, sub, flags);
20966 if (s != NULL)
20967 {
20968 *fnamep = s;
20969 *fnamelen = (int)STRLEN(s);
20970 vim_free(*bufp);
20971 *bufp = s;
20972 didit = TRUE;
20973 }
20974 }
20975 vim_free(sub);
20976 vim_free(str);
20977 }
20978 vim_free(pat);
20979 }
20980 }
20981 /* after using ":s", repeat all the modifiers */
20982 if (didit)
20983 goto repeat;
20984 }
20985 }
20986
20987 return valid;
20988}
20989
20990/*
20991 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20992 * "flags" can be "g" to do a global substitute.
20993 * Returns an allocated string, NULL for error.
20994 */
20995 char_u *
20996do_string_sub(str, pat, sub, flags)
20997 char_u *str;
20998 char_u *pat;
20999 char_u *sub;
21000 char_u *flags;
21001{
21002 int sublen;
21003 regmatch_T regmatch;
21004 int i;
21005 int do_all;
21006 char_u *tail;
21007 garray_T ga;
21008 char_u *ret;
21009 char_u *save_cpo;
21010
21011 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21012 save_cpo = p_cpo;
21013 p_cpo = (char_u *)"";
21014
21015 ga_init2(&ga, 1, 200);
21016
21017 do_all = (flags[0] == 'g');
21018
21019 regmatch.rm_ic = p_ic;
21020 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21021 if (regmatch.regprog != NULL)
21022 {
21023 tail = str;
21024 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21025 {
21026 /*
21027 * Get some space for a temporary buffer to do the substitution
21028 * into. It will contain:
21029 * - The text up to where the match is.
21030 * - The substituted text.
21031 * - The text after the match.
21032 */
21033 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21034 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21035 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21036 {
21037 ga_clear(&ga);
21038 break;
21039 }
21040
21041 /* copy the text up to where the match is */
21042 i = (int)(regmatch.startp[0] - tail);
21043 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21044 /* add the substituted text */
21045 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21046 + ga.ga_len + i, TRUE, TRUE, FALSE);
21047 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021048 /* avoid getting stuck on a match with an empty string */
21049 if (tail == regmatch.endp[0])
21050 {
21051 if (*tail == NUL)
21052 break;
21053 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21054 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 }
21056 else
21057 {
21058 tail = regmatch.endp[0];
21059 if (*tail == NUL)
21060 break;
21061 }
21062 if (!do_all)
21063 break;
21064 }
21065
21066 if (ga.ga_data != NULL)
21067 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21068
21069 vim_free(regmatch.regprog);
21070 }
21071
21072 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21073 ga_clear(&ga);
21074 p_cpo = save_cpo;
21075
21076 return ret;
21077}
21078
21079#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */